Skip to content

Commit f78a8ec

Browse files
authored
Merge pull request #79 from iron-fish/feat/hughy/round2-min
implements minimized round2 version
2 parents 819b37d + 0b4b83f commit f78a8ec

File tree

1 file changed

+106
-0
lines changed

1 file changed

+106
-0
lines changed

src/dkg/round2.rs

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,51 @@ where
460460
))
461461
}
462462

463+
pub fn round2_min<R>(
464+
secret: &participant::Secret,
465+
participants: Vec<&[u8]>,
466+
round1_secret_package: &[u8],
467+
round1_frost_packages: Vec<&[u8]>,
468+
mut csrng: R,
469+
) -> Result<(Vec<u8>, Vec<Package>), IronfishFrostError>
470+
where
471+
R: RngCore + CryptoRng,
472+
{
473+
let self_identity = secret.to_identity();
474+
let round1_secret_package = round1::import_secret_package(round1_secret_package, secret)?;
475+
476+
let mut round1_packages = BTreeMap::new();
477+
for i in 0..participants.len() {
478+
let identity = Identity::deserialize_from(participants[i])?;
479+
480+
let identifier = identity.to_frost_identifier();
481+
482+
let round1_package = Round1Package::deserialize(round1_frost_packages[i])?;
483+
round1_packages.insert(identifier, round1_package);
484+
}
485+
486+
// Run the FROST DKG round 2
487+
let (round2_secret_package, mut round2_packages) =
488+
frost::keys::dkg::part2(round1_secret_package.clone(), &round1_packages)?;
489+
490+
// Encrypt the secret package
491+
let encrypted_secret_package =
492+
export_secret_package(&round2_secret_package, &self_identity, &mut csrng)?;
493+
494+
// Convert the Identifier->Package map to a Vec<Package> ordered by the
495+
// Identifier's position in the 'participants' input Vec
496+
let mut round2_public_packages = Vec::new();
497+
for participant in participants {
498+
let identity = Identity::deserialize_from(participant)?;
499+
let round2_public_package = round2_packages
500+
.remove(&identity.to_frost_identifier())
501+
.expect("missing round 2 public package for participant");
502+
round2_public_packages.push(round2_public_package);
503+
}
504+
505+
Ok((encrypted_secret_package, round2_public_packages))
506+
}
507+
463508
#[cfg(test)]
464509
mod tests {
465510
use super::*;
@@ -670,6 +715,67 @@ mod tests {
670715
.expect("round 2 public packages missing package for identity3");
671716
}
672717

718+
#[test]
719+
fn round2_min() {
720+
let secret = participant::Secret::random(thread_rng());
721+
let identity1 = secret.to_identity();
722+
let identity2 = participant::Secret::random(thread_rng()).to_identity();
723+
let identity3 = participant::Secret::random(thread_rng()).to_identity();
724+
725+
let (round1_secret_package, _) = round1::round1(
726+
&identity1,
727+
2,
728+
[&identity1, &identity2, &identity3],
729+
thread_rng(),
730+
)
731+
.expect("round 1 failed");
732+
733+
let (_, package2) = round1::round1(
734+
&identity2,
735+
2,
736+
[&identity1, &identity2, &identity3],
737+
thread_rng(),
738+
)
739+
.expect("round 1 failed");
740+
741+
let (_, package3) = round1::round1(
742+
&identity3,
743+
2,
744+
[&identity1, &identity2, &identity3],
745+
thread_rng(),
746+
)
747+
.expect("round 1 failed");
748+
749+
let id2_ser: &[u8] = &identity2.serialize();
750+
let id3_ser: &[u8] = &identity3.serialize();
751+
let participants = vec![id2_ser, id3_ser];
752+
753+
let pkg2_ser = package2
754+
.frost_package()
755+
.serialize()
756+
.expect("serialization failed");
757+
let pkg3_ser = package3
758+
.frost_package()
759+
.serialize()
760+
.expect("serialization failed");
761+
762+
let round1_frost_packages: Vec<&[u8]> = vec![&pkg2_ser[..], &pkg3_ser[..]];
763+
764+
let (secret_package, round2_public_packages) = super::round2_min(
765+
&secret,
766+
participants,
767+
&round1_secret_package,
768+
round1_frost_packages,
769+
thread_rng(),
770+
)
771+
.expect("round 2 failed");
772+
773+
import_secret_package(&secret_package, &secret)
774+
.expect("round 2 secret package import failed");
775+
776+
assert_eq!(round2_public_packages.len(), 2);
777+
}
778+
673779
#[test]
674780
fn round2_duplicate_packages() {
675781
let secret = participant::Secret::random(thread_rng());

0 commit comments

Comments
 (0)