Skip to content

Commit

Permalink
Remove unwraps
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiono11 authored and burdges committed May 1, 2024
1 parent bc80387 commit 480cede
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 40 deletions.
4 changes: 3 additions & 1 deletion src/olaf/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ pub enum DKGError {
InvalidSecretShare(Identifier),
/// Invalid secret.
InvalidSecret,
/// Unknown identifier in round 1 public messages.
UnknownIdentifierRound1PublicMessages(Identifier),
/// Unknown identifier in round 2 public messages.
UnknownIdentifierRound2PublicMessages(Identifier),
/// Unknown identifier in round 2 private messages.
UnknownIdentifierRound2PrivateMessages(Identifier),
UnknownIdentifierRound2PrivateMessages,
/// Identifier cannot be a zero scalar.
InvalidIdentifier,
/// Incorrect number of identifiers.
Expand Down
66 changes: 37 additions & 29 deletions src/olaf/simplpedpop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ fn derive_secret_key_from_secret<R: RngCore + CryptoRng>(secret: &Secret, mut rn
bytes[..32].copy_from_slice(&secret_bytes[..]);
bytes[32..].copy_from_slice(&nonce[..]);

SecretKey::from_bytes(&bytes[..]).unwrap() // This never fails because bytes has length 64 and the key is a scalar
SecretKey::from_bytes(&bytes[..])
.expect("This never fails because bytes has length 64 and the key is a scalar")
}

/// A secret share, which corresponds to an evaluation of a value that identifies a participant in a secret polynomial.
Expand Down Expand Up @@ -263,15 +264,17 @@ pub mod round1 {
self.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap()
.expect("This never fails because the minimum threshold of the protocol is 2")
.compress()
.0
.cmp(
&other
.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap()
.expect(
"This never fails because the minimum threshold of the protocol is 2",
)
.compress()
.0,
)
Expand Down Expand Up @@ -299,22 +302,32 @@ pub mod round1 {
let secret_polynomial = loop {
let temp_polynomial = Polynomial::generate(&mut rng, *parameters.threshold() - 1);
// There must be a secret, which is the constant coefficient of the secret polynomial
if temp_polynomial.coefficients.first().unwrap() != &Scalar::ZERO {
if temp_polynomial
.coefficients
.first()
.expect("This never fails because the minimum threshold of the protocol is 2")
!= &Scalar::ZERO
{
break temp_polynomial;
}
};

let secret_polynomial_commitment = PolynomialCommitment::commit(&secret_polynomial);

// This secret key will be used to sign the proof of possession and the certificate
let secret_key =
derive_secret_key_from_secret(secret_polynomial.coefficients.first().unwrap(), rng);
let secret_key = derive_secret_key_from_secret(
secret_polynomial
.coefficients
.first()
.expect("This never fails because the minimum threshold of the protocol is 2"),
rng,
);

let public_key = PublicKey::from_point(
*secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap(),
.expect("This never fails because the minimum threshold of the protocol is 2"),
);

let proof_of_possession =
Expand Down Expand Up @@ -472,7 +485,7 @@ pub mod round2 {
.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap();
.expect("This never fails because the minimum threshold of the protocol is 2");

let messages = generate_messages(
&public_data,
Expand All @@ -496,7 +509,7 @@ pub mod round2 {
.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap()
.expect("This never fails because the minimum threshold of the protocol is 2")
.compress();

// Writes the data of all the participants in the transcript ordered by their identifiers
Expand All @@ -505,7 +518,7 @@ pub mod round2 {
.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap()
.expect("This never fails because the minimum threshold of the protocol is 2")
.compress();

if own_first_coefficient_compressed.0 < message_first_coefficient_compressed.0
Expand Down Expand Up @@ -590,7 +603,7 @@ pub mod round2 {
msg.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap()
.expect("This never fails because the minimum threshold of the protocol is 2")
.compress()
.0
})
Expand All @@ -600,7 +613,7 @@ pub mod round2 {
.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap();
.expect("This never fails because the minimum threshold of the protocol is 2");

secret_commitments.insert(own_secret_commitment.compress().0);

Expand All @@ -618,7 +631,10 @@ pub mod round2 {
others_identifiers.insert(Identifier(random_scalar));
}

let own_identifier = *others_identifiers.iter().nth(index).unwrap();
let own_identifier = *others_identifiers
.iter()
.nth(index)
.expect("This never fails because the index < len");
others_identifiers.remove(&own_identifier);

for (id, message) in others_identifiers.iter().zip(round1_public_messages_set) {
Expand All @@ -644,7 +660,7 @@ pub mod round2 {
.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap(),
.expect("This never fails because the minimum threshold of the protocol is 2"),
);
public_keys.push(public_key);
proofs_of_possession.push(round1_public_message.proof_of_possession);
Expand Down Expand Up @@ -676,7 +692,7 @@ pub mod round2 {
*msg.secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap()
.expect("This never fails because the minimum threshold of the protocol is 2")
})
.collect();

Expand Down Expand Up @@ -861,16 +877,6 @@ pub mod round3 {
let mut secret_shares = BTreeMap::new();

for (i, (identifier, private_message)) in round2_private_messages.iter().enumerate() {
if !round2_public_data
.identifiers
.others_identifiers
.contains(identifier)
{
return Err(DKGError::UnknownIdentifierRound2PrivateMessages(
*identifier,
));
}

let secret_share = private_message.encrypted_secret_share.decrypt(
secret,
&round2_public_data.public_keys[i].into_point(),
Expand All @@ -884,7 +890,7 @@ pub mod round3 {
let evaluation = round2_public_data
.round1_public_messages
.get(identifier)
.unwrap()
.ok_or(DKGError::UnknownIdentifierRound1PublicMessages(*identifier))?
.secret_polynomial_commitment
.evaluate(&round2_public_data.identifiers.own_identifier.0);

Expand All @@ -906,8 +912,10 @@ pub mod round3 {
let mut total_secret_share = Scalar::ZERO;

for id in &identifiers.others_identifiers {
// This never fails because we previously checked
total_secret_share += secret_shares.get(id).unwrap().0;
total_secret_share += secret_shares
.get(id)
.ok_or(DKGError::UnknownIdentifierRound2PrivateMessages)?
.0;
}

total_secret_share += own_secret_share;
Expand Down Expand Up @@ -960,7 +968,7 @@ pub mod round3 {
*total_secret_polynomial_commitment
.coefficients_commitments
.first()
.unwrap(),
.expect("This never fails because the minimum threshold of the protocol is 2"),
);

Ok((shared_public_key, group_public_key_shares))
Expand Down
28 changes: 18 additions & 10 deletions src/olaf/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -827,7 +827,7 @@ mod tests {
) = round1();

let (
participants_round2_public_data,
mut participants_round2_public_data,
participants_round2_messages,
participants_sets_of_participants,
identifiers_vec,
Expand All @@ -845,13 +845,6 @@ mod tests {
.map(|msg| msg.public_message().clone())
.collect();

let mut identifiers_vec2 = identifiers_vec.clone();

identifiers_vec2.pop();

let unknown_identifier = Identifier(Scalar::ONE);
identifiers_vec2.push(unknown_identifier);

let participants_round2_private_messages: Vec<
BTreeMap<Identifier, round2::PrivateMessage>,
> = participants_round2_messages
Expand All @@ -876,13 +869,28 @@ mod tests {

for (i, round_messages) in participants_round2_private_messages.iter().enumerate() {
if let Some(message) = round_messages.get(&participants.own_identifier) {
messages_for_participant.insert(identifiers_vec2[i], message.clone());
messages_for_participant.insert(identifiers_vec[i], message.clone());
}
}

round2_private_messages.push(messages_for_participant);
}

let unknown_identifier = Identifier(Scalar::ONE);

let private_message = round2_private_messages[0].pop_first().unwrap().1;
round2_private_messages[0].insert(unknown_identifier, private_message);

let public_message = participants_round2_public_data[0]
.round1_public_messages
.pop_first()
.unwrap()
.1;

participants_round2_public_data[0]
.round1_public_messages
.insert(unknown_identifier, public_message);

let result = round3::run(
&received_round2_public_messages,
&participants_round2_public_data[0],
Expand All @@ -895,7 +903,7 @@ mod tests {
Ok(_) => panic!("Expected an error, but got Ok."),
Err(e) => assert_eq!(
e,
DKGError::UnknownIdentifierRound2PrivateMessages(unknown_identifier),
DKGError::UnknownIdentifierRound2PrivateMessages,
"Expected DKGError::UnknownIdentifierRound2PrivateMessages."
),
}
Expand Down

0 comments on commit 480cede

Please sign in to comment.