Skip to content

Commit af3ac2e

Browse files
committed
converted round 2 to use more errors
1 parent 7fc596c commit af3ac2e

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed

src/dkg/round2.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -147,16 +147,16 @@ pub fn export_secret_package<R: RngCore + CryptoRng>(
147147
pkg: &SecretPackage,
148148
identity: &Identity,
149149
csrng: R,
150-
) -> io::Result<Vec<u8>> {
150+
) -> Result<Vec<u8>, IronfishFrostError> {
151151
let serializable = SerializableSecretPackage::from(pkg.clone());
152152
if serializable.identifier != identity.to_frost_identifier() {
153-
return Err(io::Error::other("identity mismatch"));
153+
return Err(IronfishFrostError::InvalidInput(
154+
"identity mismatch".to_string(),
155+
));
154156
}
155157

156158
let mut serialized = Vec::new();
157-
serializable
158-
.serialize_into(&mut serialized)
159-
.expect("serialization failed");
159+
serializable.serialize_into(&mut serialized)?;
160160
Ok(multienc::encrypt(&serialized, [identity], csrng))
161161
}
162162

@@ -441,30 +441,38 @@ where
441441
}
442442

443443
// Sanity check
444-
assert_eq!(round1_public_packages.len(), identities.len());
445-
assert_eq!(round1_public_packages.len(), round1_frost_packages.len());
444+
if round1_public_packages.len() != identities.len()
445+
|| round1_public_packages.len() != round1_frost_packages.len()
446+
{
447+
return Err(IronfishFrostError::InvalidInput(
448+
"incorrect number of packages or identities provided".to_string(),
449+
));
450+
}
446451

447452
// The public package for `self_identity` must be excluded from `frost::keys::dkg::part2`
448453
// inputs
449454
round1_frost_packages
450455
.remove(&self_identity.to_frost_identifier())
451-
.expect("missing public package for self_identity");
456+
.ok_or(IronfishFrostError::InvalidInput(
457+
"missing public package for self_identity".to_string(),
458+
))?;
452459

453460
// Run the FROST DKG round 2
454461
let (round2_secret_package, round2_packages) =
455462
frost::keys::dkg::part2(round1_secret_package.clone(), &round1_frost_packages)?;
456463

457464
// Encrypt the secret package
458465
let encrypted_secret_package =
459-
export_secret_package(&round2_secret_package, &self_identity, &mut csrng)
460-
.map_err(IronfishFrostError::EncryptionError)?;
466+
export_secret_package(&round2_secret_package, &self_identity, &mut csrng)?;
461467

462468
// Convert the Identifier->Package map to an Identity->PublicPackage map
463469
let mut round2_public_packages = Vec::new();
464470
for (identifier, package) in round2_packages {
465471
let identity = *identities
466472
.get(&identifier)
467-
.expect("round2 generated package for unknown identifier");
473+
.ok_or(IronfishFrostError::InvalidInput(
474+
"round2 generated package for unknown identifier".to_string(),
475+
))?;
468476

469477
let public_package = PublicPackage::new(
470478
self_identity.clone(),
@@ -520,7 +528,9 @@ where
520528
let identity = Identity::deserialize_from(participant)?;
521529
let round2_public_package = round2_packages
522530
.remove(&identity.to_frost_identifier())
523-
.expect("missing round 2 public package for participant");
531+
.ok_or(IronfishFrostError::InvalidInput(
532+
"missing round 2 public package for particiant".to_string(),
533+
))?;
524534
round2_public_packages.push(round2_public_package);
525535
}
526536

0 commit comments

Comments
 (0)