Skip to content

Commit 56ebdae

Browse files
alxiongakonring
andcommitted
avoid Result<bool>, fix a bug in vess.verify
Co-authored-by: Anders Konring <[email protected]>
1 parent b564bca commit 56ebdae

File tree

3 files changed

+31
-17
lines changed

3 files changed

+31
-17
lines changed

timeboost-crypto/src/feldman.rs

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl<C: CurveGroup> VerifiableSecretSharing for FeldmanVss<C> {
8989
node_idx: usize,
9090
share: &Self::SecretShare,
9191
commitment: &Self::Commitment,
92-
) -> Result<bool, VssError> {
92+
) -> Result<(), VssError> {
9393
let n = pp.n.get() as usize;
9494
let t = pp.t.get() as usize;
9595

@@ -113,7 +113,11 @@ impl<C: CurveGroup> VerifiableSecretSharing for FeldmanVss<C> {
113113
VssError::InternalError("commitments and powers mismatched length".to_string())
114114
})?;
115115

116-
Ok(C::generator().mul(share) == eval_in_exp)
116+
if C::generator().mul(share) == eval_in_exp {
117+
Ok(())
118+
} else {
119+
Err(VssError::FailedVerification)
120+
}
117121
}
118122

119123
fn reconstruct(
@@ -167,29 +171,27 @@ mod tests {
167171
let (shares, commitment) = FeldmanVss::<C>::share(&pp, rng, secret);
168172
for (node_idx, s) in shares.iter().enumerate() {
169173
// happy path
170-
assert!(FeldmanVss::<C>::verify(&pp, node_idx, s, &commitment).unwrap());
174+
assert!(FeldmanVss::<C>::verify(&pp, node_idx, s, &commitment).is_ok());
171175

172176
// sad path
173177
// wrong node_idx should fail
174-
assert!(
175-
!FeldmanVss::<C>::verify(&pp, node_idx + 1, s, &commitment).unwrap_or(false)
176-
);
178+
assert!(FeldmanVss::<C>::verify(&pp, node_idx + 1, s, &commitment).is_err());
177179

178180
// wrong secret share should fail
179181
assert!(
180-
!FeldmanVss::<C>::verify(
182+
FeldmanVss::<C>::verify(
181183
&pp,
182184
node_idx,
183185
&C::ScalarField::rand(rng),
184186
&commitment,
185187
)
186-
.unwrap()
188+
.is_err()
187189
);
188190

189191
// wrong commitment should fail
190192
let mut bad_comm = commitment.clone();
191193
bad_comm[1] = C::Affine::default();
192-
assert!(!FeldmanVss::<C>::verify(&pp, node_idx, s, &bad_comm).unwrap());
194+
assert!(FeldmanVss::<C>::verify(&pp, node_idx, s, &bad_comm).is_err());
193195

194196
// incomplete/dropped commitment should fail
195197
bad_comm.pop();

timeboost-crypto/src/traits/dkg.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ pub trait VerifiableSecretSharing {
3636
/// - `share`: the secret share to verify
3737
/// - `commitment`: the global commitment (if any)
3838
///
39-
/// Returns Ok(true) if valid, Ok(false) if invalid, or an appropriate `VssError` otherwise.
39+
/// Returns Ok(()) if valid, or an appropriate `VssError` otherwise.
4040
fn verify(
4141
pp: &Self::PublicParam,
4242
node_idx: usize,
4343
share: &Self::SecretShare,
4444
commitment: &Self::Commitment,
45-
) -> Result<bool, VssError>;
45+
) -> Result<(), VssError>;
4646

4747
/// Reconstructs the original secret from a set of (index, share) pairs.
4848
///
@@ -66,6 +66,8 @@ pub enum VssError {
6666
InvalidShare(usize, String),
6767
#[error("invalid VSS commitment")]
6868
InvalidCommitment,
69+
#[error("failed verification: share does not match commitment")]
70+
FailedVerification,
6971
#[error("failed to reconstruct: {0}")]
7072
FailedReconstruction(String),
7173
#[error("internal err: {0}")]

timeboost-crypto/src/vess.rs

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ impl<C: CurveGroup> ShoupVess<C> {
372372
ct: &VessCiphertext,
373373
comm: &<FeldmanVss<C> as VerifiableSecretSharing>::Commitment,
374374
aad: &[u8],
375-
) -> Result<bool, VessError> {
375+
) -> Result<(), VessError> {
376376
let mut verifier_state = self.io_pattern(aad).to_verifier_state(&ct.transcript);
377377

378378
// verifier logic until Step 4b
@@ -407,10 +407,14 @@ impl<C: CurveGroup> ShoupVess<C> {
407407
.expect("subset_size > 0, so is shifted_polys.len()")
408408
.as_ref(),
409409
);
410+
411+
let mut unshifted_comm = vec![];
410412
for (shifted, delta) in shifted_comm.into_iter().zip(comm.iter()) {
411413
// g^omega'' / C in paper
412-
hasher.update(serialize_to_vec![shifted - delta]?)
414+
unshifted_comm.push(shifted - delta);
413415
}
416+
let unshifted_comm = C::normalize_batch(&unshifted_comm);
417+
hasher.update(serialize_to_vec![unshifted_comm]?);
414418

415419
let mre_ct = mre_cts
416420
.pop_front()
@@ -435,7 +439,11 @@ impl<C: CurveGroup> ShoupVess<C> {
435439
debug_assert!(mre_cts.is_empty());
436440
debug_assert!(seeds.is_empty());
437441

438-
Ok(h != hasher.finalize().as_slice())
442+
if h == hasher.finalize().as_slice() {
443+
Ok(())
444+
} else {
445+
Err(VessError::FailedVerification)
446+
}
439447
}
440448

441449
/// Decrypt with a decryption key `recv_sk` (labeled with node_idx, see `LabeledDecryptionKey`)
@@ -473,7 +481,7 @@ impl<C: CurveGroup> ShoupVess<C> {
473481
let share = shifted_eval - unshifted_eval;
474482

475483
// check correctness
476-
if FeldmanVss::<C>::verify(&self.vss_pp, node_idx, &share, &comm)? {
484+
if FeldmanVss::<C>::verify(&self.vss_pp, node_idx, &share, &comm).is_ok() {
477485
return Ok(share);
478486
}
479487
}
@@ -643,6 +651,8 @@ pub enum VessError {
643651
IndexOutOfBound(usize, usize),
644652
#[error("wrong vss commitment supplied")]
645653
WrongCommitment,
654+
#[error("failed verification: proof verification failed")]
655+
FailedVerification,
646656
#[error("decryption fail")]
647657
DecryptionFailed,
648658
}
@@ -692,10 +702,10 @@ mod tests {
692702
let aad = b"Associated data";
693703
let (ct, comm) = vess.encrypted_shares(&recv_pks, secret, aad).unwrap();
694704

695-
assert!(vess.verify(&recv_pks, &ct, &comm, aad).unwrap());
705+
assert!(vess.verify(&recv_pks, &ct, &comm, aad).is_ok());
696706
for labeled_recv_sk in labeled_sks {
697707
let share = vess.decrypt_share(&labeled_recv_sk, &ct, aad).unwrap();
698-
assert!(Vss::verify(&vess.vss_pp, labeled_recv_sk.node_idx, &share, &comm).unwrap());
708+
assert!(Vss::verify(&vess.vss_pp, labeled_recv_sk.node_idx, &share, &comm).is_ok());
699709
}
700710
}
701711

0 commit comments

Comments
 (0)