Skip to content

Commit 9046c4f

Browse files
committed
Consistent use of Iterators for Dkg and Resharing.
1 parent 584b718 commit 9046c4f

File tree

3 files changed

+36
-44
lines changed

3 files changed

+36
-44
lines changed

timeboost-crypto/src/feldman.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -302,41 +302,42 @@ impl<C: CurveGroup> KeyResharing<Self> for FeldmanVss<C> {
302302
old_pp: &FeldmanVssPublicParam<C>,
303303
new_pp: &FeldmanVssPublicParam<C>,
304304
recv_node_idx: usize,
305-
reshares: impl ExactSizeIterator<Item = (usize, C::ScalarField, FeldmanCommitment<C>)> + Clone,
305+
reshares: impl Iterator<Item = (usize, C::ScalarField, FeldmanCommitment<C>)>,
306306
) -> Result<(C::ScalarField, FeldmanCommitment<C>), VssError> {
307-
// input validation
308307
let n = old_pp.n.get();
309-
if reshares.len() == 0 {
310-
return Err(VssError::EmptyReshare);
311-
}
312-
for (idx, _, _) in reshares.clone() {
308+
309+
let mut eval_points = Vec::new();
310+
let mut recv_shares = Vec::new();
311+
let mut row_commitments = Vec::new();
312+
313+
for (idx, share, commitment) in reshares {
313314
if idx >= n {
314315
return Err(VssError::IndexOutOfBound(n - 1, idx));
315316
}
317+
eval_points.push(C::ScalarField::from(idx as u64 + 1));
318+
recv_shares.push(share);
319+
row_commitments.push(commitment);
320+
}
321+
322+
if eval_points.is_empty() {
323+
return Err(VssError::EmptyReshare);
316324
}
317325

318326
let new_n = new_pp.n.get();
319327
let new_t = new_pp.t.get();
320328
if recv_node_idx >= new_n {
321329
return Err(VssError::IndexOutOfBound(new_n - 1, recv_node_idx));
322330
}
323-
for (_, _, row_commitment) in reshares.clone() {
324-
if row_commitment.len() != new_t {
325-
return Err(VssError::InvalidCommitment);
326-
}
331+
332+
if row_commitments.iter().any(|c| c.len() != new_t) {
333+
return Err(VssError::InvalidCommitment);
327334
}
328335

329336
// interpolate reshares to get new secret share
330-
let eval_points: Vec<_> = reshares
331-
.clone()
332-
.map(|(idx, _, _)| C::ScalarField::from(idx as u64 + 1))
333-
.collect();
334-
let recv_reshares: Vec<_> = reshares.clone().map(|(_, share, _)| share).collect();
335-
let new_secret = interpolate::<C>(&eval_points, &recv_reshares)
337+
let new_secret = interpolate::<C>(&eval_points, &recv_shares)
336338
.map_err(|e| VssError::FailedCombine(e.to_string()))?;
337339

338340
// interpolate in the exponent to get new Feldman commitment
339-
let row_commitments: Vec<_> = reshares.map(|(_, _, commitment)| commitment).collect();
340341
let new_commitment = (0..new_t)
341342
.into_par_iter()
342343
.map(|j| {
@@ -346,6 +347,7 @@ impl<C: CurveGroup> KeyResharing<Self> for FeldmanVss<C> {
346347
.map_err(|e| VssError::FailedCombine(e.to_string()))
347348
})
348349
.collect::<Result<Vec<_>, VssError>>()?;
350+
349351
let new_commitment = C::normalize_batch(&new_commitment);
350352

351353
Ok((new_secret, new_commitment.into()))

timeboost-crypto/src/traits/dkg.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ pub trait KeyResharing<VSS: VerifiableSecretSharing> {
112112
old_pp: &VSS::PublicParam,
113113
new_pp: &VSS::PublicParam,
114114
recv_node_idx: usize,
115-
reshares: impl ExactSizeIterator<Item = (usize, VSS::SecretShare, VSS::Commitment)> + Clone,
115+
reshares: impl Iterator<Item = (usize, VSS::SecretShare, VSS::Commitment)>,
116116
) -> Result<(VSS::Secret, VSS::Commitment), VssError>;
117117
}
118118

timeboost-types/src/decryption.rs

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ impl ThresholdKey {
8181
dealings: I,
8282
) -> anyhow::Result<Self>
8383
where
84-
I: ExactSizeIterator<Item = (usize, VssShare, VssCommitment)> + Clone,
84+
I: Iterator<Item = (usize, VssShare, VssCommitment)>,
8585
{
8686
let old_pp = FeldmanVssPublicParam::from(old_committee);
8787
let new_pp = FeldmanVssPublicParam::from(new_committee);
@@ -600,38 +600,28 @@ impl<'a> DkgSubsetRef<'a> {
600600
)?;
601601

602602
dealings_iter.result()?;
603-
604603
Ok(dec_key)
605604
}
606605
Some(combkey) => {
607-
let Some(prev) = prev else {
608-
return Err(anyhow!("previous key store missing"));
609-
};
606+
let prev = prev.ok_or_else(|| anyhow!("previous key store missing"))?;
607+
let mut dealings_iter = ResultIter::new(self.bundles.iter().map(|b| {
608+
let node_idx = b.origin().0.into();
609+
let pub_share = combkey
610+
.get_pub_share(node_idx)
611+
.ok_or(VessError::FailedVerification)?;
612+
vess.decrypt_reshare(curr.committee(), dkg_sk, b.vess_ct(), DKG_AAD, *pub_share)
613+
.map(|s| (node_idx, s, b.comm().clone()))
614+
}));
610615

611-
let dealings: Vec<_> = self
612-
.bundles
613-
.iter()
614-
.map(|b| {
615-
let node_idx = b.origin().0.into();
616-
let pub_share = combkey
617-
.get_pub_share(node_idx)
618-
.ok_or(VessError::FailedVerification)?;
619-
let s = vess.decrypt_reshare(
620-
curr.committee(),
621-
dkg_sk,
622-
b.vess_ct(),
623-
DKG_AAD,
624-
*pub_share,
625-
)?;
626-
Ok((node_idx, s, b.comm().clone()))
627-
})
628-
.collect::<Result<Vec<_>, VessError>>()?;
629-
ThresholdKey::from_resharing(
616+
let dec_key = ThresholdKey::from_resharing(
630617
prev.committee(),
631618
curr.committee(),
632619
dkg_sk.node_idx(),
633-
dealings.into_iter(),
634-
)
620+
&mut dealings_iter,
621+
)?;
622+
623+
dealings_iter.result()?;
624+
Ok(dec_key)
635625
}
636626
}
637627
}

0 commit comments

Comments
 (0)