Skip to content

Commit 4cec6d5

Browse files
authored
Merge pull request #84 from iron-fish/mat/more-errors
replace some panics with result returning
2 parents 7fc596c + 9b41486 commit 4cec6d5

File tree

3 files changed

+39
-22
lines changed

3 files changed

+39
-22
lines changed

src/dkg/round1.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -146,16 +146,16 @@ pub fn export_secret_package<R: RngCore + CryptoRng>(
146146
pkg: &SecretPackage,
147147
identity: &Identity,
148148
csrng: R,
149-
) -> io::Result<Vec<u8>> {
149+
) -> Result<Vec<u8>, IronfishFrostError> {
150150
let serializable = <&SerializableSecretPackage>::from(pkg);
151151
if serializable.identifier != identity.to_frost_identifier() {
152-
return Err(io::Error::other("identity mismatch"));
152+
return Err(IronfishFrostError::InvalidInput(
153+
"identity mismatch".to_string(),
154+
));
153155
}
154156

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

@@ -267,7 +267,7 @@ impl PublicPackage {
267267
}
268268

269269
pub fn deserialize_from<R: io::Read>(mut reader: R) -> Result<Self, IronfishFrostError> {
270-
let identity = Identity::deserialize_from(&mut reader).expect("reading identity failed");
270+
let identity = Identity::deserialize_from(&mut reader)?;
271271

272272
let frost_package = read_variable_length_bytes(&mut reader)?;
273273
let frost_package = Package::deserialize(&frost_package)?;
@@ -320,8 +320,7 @@ where
320320
)?;
321321

322322
let encrypted_secret_package =
323-
export_secret_package(&secret_package, self_identity, &mut csrng)
324-
.map_err(IronfishFrostError::EncryptionError)?;
323+
export_secret_package(&secret_package, self_identity, &mut csrng)?;
325324

326325
let group_secret_key_shard = GroupSecretKeyShard::random(&mut csrng);
327326

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

src/dkg/round3.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,11 @@ where
210210
}
211211

212212
// Sanity check
213-
assert_eq!(round1_public_packages.len(), round1_frost_packages.len());
213+
if round1_public_packages.len() != round1_frost_packages.len() {
214+
return Err(IronfishFrostError::InvalidInput(
215+
"incorrect number of round 1 packages provided".to_string(),
216+
));
217+
}
214218

215219
// The public package for `identity` must be excluded from `frost::keys::dkg::part3`
216220
// inputs
@@ -264,7 +268,11 @@ where
264268
}
265269
}
266270

267-
assert_eq!(round2_public_packages.len(), round2_frost_packages.len());
271+
if round2_public_packages.len() != round2_frost_packages.len() {
272+
return Err(IronfishFrostError::InvalidInput(
273+
"incorrect number of round 2 packages provided".to_string(),
274+
));
275+
}
268276

269277
let (key_package, public_key_package) = part3(
270278
&round2_secret_package,

0 commit comments

Comments
 (0)