Skip to content

Commit f62f367

Browse files
committed
Move extract_key method to Subset
1 parent fe90dab commit f62f367

File tree

2 files changed

+74
-78
lines changed

2 files changed

+74
-78
lines changed

timeboost-sequencer/src/decrypt.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -718,11 +718,11 @@ impl Worker {
718718
}
719719

720720
if let Some((&subset, _)) = counts.iter().find(|(_, count)| **count >= threshold) {
721-
let acc = DkgAccumulator::from_subset(current.to_owned(), subset.to_owned());
722-
let dec_key = acc
723-
.extract_key(&self.dkg_sk, prev)
724-
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
721+
let acc = DkgAccumulator::from_subset(current.clone(), subset.to_owned());
725722
self.tracker.insert(committee.id(), acc);
723+
let dec_key = subset
724+
.extract_key(current.to_owned(), &self.dkg_sk, prev)
725+
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
726726

727727
self.dec_key.set(dec_key);
728728
self.state = WorkerState::Running;
@@ -765,11 +765,11 @@ impl Worker {
765765
}
766766

767767
if let Some((&subset, _)) = counts.iter().find(|(_, count)| **count >= threshold) {
768-
let acc = DkgAccumulator::from_subset(current.to_owned(), subset.to_owned());
769-
let next_dec_key = acc
770-
.extract_key(&self.dkg_sk, Some(prev.to_owned()))
771-
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
768+
let acc = DkgAccumulator::from_subset(current.clone(), subset.to_owned());
772769
self.tracker.insert(current.committee().id(), acc);
770+
let next_dec_key = subset
771+
.extract_key(current.clone(), &self.dkg_sk, Some(prev.to_owned()))
772+
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
773773

774774
info!(committee_id = %current.committee().id(), node = %self.label, "handover finished");
775775
self.state = WorkerState::HandoverComplete;
@@ -828,14 +828,14 @@ impl Worker {
828828
let acc = self
829829
.tracker
830830
.entry(*committee_id)
831-
.or_insert_with(|| DkgAccumulator::new_dkg(key_store.to_owned()));
831+
.or_insert_with(|| DkgAccumulator::new_dkg(key_store.clone()));
832832

833833
acc.try_add(bundle)
834834
.map_err(|e| DecrypterError::Dkg(format!("unable to add dkg bundle: {e}")))?;
835835

836-
if acc.try_finalize().is_some() {
837-
let dec_key = acc
838-
.extract_key(&self.dkg_sk, None)
836+
if let Some(subset) = acc.try_finalize() {
837+
let dec_key = subset
838+
.extract_key(key_store.to_owned(), &self.dkg_sk, None)
839839
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
840840
self.dec_key.set(dec_key);
841841
self.state = WorkerState::Running;
@@ -888,8 +888,8 @@ impl Worker {
888888

889889
if committee.contains_key(&self.label) {
890890
// node is a member of the next committee; decrypting reshares immediately
891-
let next_dec_key = acc
892-
.extract_key(&self.dkg_sk, Some(current))
891+
let next_dec_key = subset
892+
.extract_key(next, &self.dkg_sk, Some(current))
893893
.map_err(|e| DecrypterError::Dkg(e.to_string()))?;
894894
self.state = WorkerState::ResharingComplete(next_dec_key);
895895
} else {

timeboost-types/src/decryption.rs

Lines changed: 60 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -484,70 +484,6 @@ impl DkgAccumulator {
484484
complete: true,
485485
}
486486
}
487-
488-
/// Extract the new threshold decryption key from the accumulator.
489-
pub fn extract_key(
490-
&self,
491-
dkg_sk: &LabeledDkgDecKey,
492-
prev: Option<KeyStore>,
493-
) -> anyhow::Result<DecryptionKey> {
494-
if !self.completed() {
495-
return Err(anyhow!(
496-
"attempt to extract key from incomplete accumulator"
497-
));
498-
};
499-
let vess = Vess::new_fast();
500-
501-
match &self.mode {
502-
AccumulatorMode::Dkg => {
503-
let mut dealings_iter = ResultIter::new(self.bundles().iter().map(|b| {
504-
vess.decrypt_share(self.committee(), dkg_sk, b.vess_ct(), DKG_AAD)
505-
.map(|s| (s, b.comm().clone()))
506-
}));
507-
508-
let dec_key = DecryptionKey::from_dkg(
509-
self.committee().size().into(),
510-
dkg_sk.node_idx(),
511-
&mut dealings_iter,
512-
)?;
513-
514-
dealings_iter.result()?;
515-
516-
Ok(dec_key)
517-
}
518-
AccumulatorMode::Resharing(combkey) => {
519-
let Some(prev) = prev else {
520-
return Err(anyhow!("previous key store missing"));
521-
};
522-
let dealings: Vec<_> = self
523-
.bundles()
524-
.iter()
525-
.enumerate()
526-
.map(|(i, b)| {
527-
let node_idx = b.origin().0.into();
528-
let pub_share = combkey
529-
.get_pub_share(node_idx)
530-
.ok_or(VessError::FailedVerification)?;
531-
let s = vess.decrypt_reshare(
532-
self.committee(),
533-
dkg_sk,
534-
b.vess_ct(),
535-
DKG_AAD,
536-
*pub_share,
537-
)?;
538-
Ok((i, s, b.comm().clone()))
539-
})
540-
.collect::<Result<Vec<_>, VessError>>()?;
541-
542-
DecryptionKey::from_resharing(
543-
prev.committee(),
544-
self.committee(),
545-
dkg_sk.node_idx(),
546-
dealings.into_iter(),
547-
)
548-
}
549-
}
550-
}
551487
}
552488

553489
/// A unified subset that can represent both DKG and Resharing results.
@@ -605,6 +541,66 @@ impl DkgSubset {
605541
pub fn is_resharing(&self) -> bool {
606542
self.combkey.is_some()
607543
}
544+
545+
/// Extract the new threshold decryption key from the subset.
546+
pub fn extract_key(
547+
&self,
548+
curr: KeyStore,
549+
dkg_sk: &LabeledDkgDecKey,
550+
prev: Option<KeyStore>,
551+
) -> anyhow::Result<DecryptionKey> {
552+
let vess = Vess::new_fast();
553+
554+
match &self.combkey {
555+
None => {
556+
let mut dealings_iter = ResultIter::new(self.bundles().iter().map(|b| {
557+
vess.decrypt_share(curr.committee(), dkg_sk, b.vess_ct(), DKG_AAD)
558+
.map(|s| (s, b.comm().clone()))
559+
}));
560+
561+
let dec_key = DecryptionKey::from_dkg(
562+
curr.committee().size().into(),
563+
dkg_sk.node_idx(),
564+
&mut dealings_iter,
565+
)?;
566+
567+
dealings_iter.result()?;
568+
569+
Ok(dec_key)
570+
}
571+
Some(combkey) => {
572+
let Some(prev) = prev else {
573+
return Err(anyhow!("previous key store missing"));
574+
};
575+
let dealings: Vec<_> = self
576+
.bundles()
577+
.iter()
578+
.enumerate()
579+
.map(|(i, b)| {
580+
let node_idx = b.origin().0.into();
581+
let pub_share = combkey
582+
.get_pub_share(node_idx)
583+
.ok_or(VessError::FailedVerification)?;
584+
let s = vess.decrypt_reshare(
585+
curr.committee(),
586+
dkg_sk,
587+
b.vess_ct(),
588+
DKG_AAD,
589+
*pub_share,
590+
)?;
591+
Ok((i, s, b.comm().clone()))
592+
})
593+
.collect::<Result<Vec<_>, VessError>>()?;
594+
595+
DecryptionKey::from_resharing(
596+
prev.committee(),
597+
curr.committee(),
598+
dkg_sk.node_idx(),
599+
dealings.into_iter(),
600+
)
601+
}
602+
}
603+
}
608604
}
609605

610606
/// Wrapper iterator that bridges type conversion

0 commit comments

Comments
 (0)