Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
Don't panic with invalid ThresholdParameters (#38)
Browse files Browse the repository at this point in the history
* fix: don't panic when creating threshold params with invalid values
chore: fix a typo

* Appease clippy

* Update src/parameters.rs

Co-authored-by: Linda Guiga <[email protected]>

---------

Co-authored-by: Linda Guiga <[email protected]>
Co-authored-by: Robin Salen <[email protected]>
  • Loading branch information
3 people authored Apr 11, 2024
1 parent ed205b6 commit 2560f0a
Show file tree
Hide file tree
Showing 10 changed files with 117 additions and 154 deletions.
3 changes: 2 additions & 1 deletion benches/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const NUMBER_OF_PARTICIPANTS: u32 = 5;
const THRESHOLD_OF_PARTICIPANTS: u32 = 3;

fn criterion_benchmark(c: &mut Criterion) {
let params = ThresholdParameters::new(NUMBER_OF_PARTICIPANTS, THRESHOLD_OF_PARTICIPANTS);
let params =
ThresholdParameters::new(NUMBER_OF_PARTICIPANTS, THRESHOLD_OF_PARTICIPANTS).unwrap();
let rng = OsRng;

c.bench_function("Participant creation (dealer)", move |b| {
Expand Down
3 changes: 2 additions & 1 deletion benches/sign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ const NUMBER_OF_PARTICIPANTS: u32 = 5;
const THRESHOLD_OF_PARTICIPANTS: u32 = 3;

fn criterion_benchmark(c: &mut Criterion) {
let params = ThresholdParameters::new(NUMBER_OF_PARTICIPANTS, THRESHOLD_OF_PARTICIPANTS);
let params =
ThresholdParameters::new(NUMBER_OF_PARTICIPANTS, THRESHOLD_OF_PARTICIPANTS).unwrap();
let rng = OsRng;

let mut participants = Vec::<ParticipantDKG>::with_capacity(NUMBER_OF_PARTICIPANTS as usize);
Expand Down
30 changes: 15 additions & 15 deletions src/dkg/key_generation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
//! # fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
//! // Set up key shares for a threshold signature scheme which needs at least
//! // 2-out-of-3 signers.
//! let params = ThresholdParameters::new(3,2);
//! let params = ThresholdParameters::new(3, 2)?;
//! let mut rng = OsRng;
//!
//! // Alice, Bob, and Carol each generate their secret polynomial coefficients
Expand Down Expand Up @@ -204,7 +204,7 @@
//! # fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
//! // Set up key shares for a threshold signature scheme which needs at least
//! // 2-out-of-3 signers.
//! let params = ThresholdParameters::new(3,2);
//! let params = ThresholdParameters::new(3, 2)?;
//! let mut rng = OsRng;
//!
//! // Alice, Bob, and Carol each generate their secret polynomial coefficients
Expand Down Expand Up @@ -320,7 +320,7 @@
//! assert!(carol_group_key == bob_group_key);
//!
//! // Instantiate another configuration of threshold signature.
//! let new_params = ThresholdParameters::new(4,3);
//! let new_params = ThresholdParameters::new(4,3)?;
//!
//! // Alexis, Barbara, Claire and David each generate their Diffie-Hellman
//! // private key, as well as a zero-knowledge proof to it.
Expand Down Expand Up @@ -790,7 +790,7 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundOne, C> {
let parameters = ThresholdParameters::new(
parameters.n - misbehaving_participants.len() as u32,
parameters.t,
);
)?;

let state = ActualState {
parameters,
Expand Down Expand Up @@ -852,7 +852,7 @@ impl<C: CipherSuite> DistributedKeyGeneration<RoundOne, C> {
let parameters = ThresholdParameters::new(
parameters.n - misbehaving_participants.len() as u32,
parameters.t,
);
)?;

let state = ActualState {
parameters,
Expand Down Expand Up @@ -1214,7 +1214,7 @@ pub(crate) mod test {
> {
type Dkg<T> = DistributedKeyGeneration<T, Secp256k1Sha256>;

let params = ThresholdParameters::new(n1, t1);
let params = ThresholdParameters::new(n1, t1)?;
let rng = OsRng;

let mut participants = Vec::<Participant<Secp256k1Sha256>>::new();
Expand Down Expand Up @@ -1306,7 +1306,7 @@ pub(crate) mod test {
}

if let (Some(n2), Some(t2)) = (n2, t2) {
let new_params = ThresholdParameters::new(n2, t2);
let new_params = ThresholdParameters::new(n2, t2)?;

let mut signers = Vec::<Participant<Secp256k1Sha256>>::new();
let mut signers_dh_secret_keys = Vec::<DiffieHellmanPrivateKey<Secp256k1Sha256>>::new();
Expand Down Expand Up @@ -1390,7 +1390,7 @@ pub(crate) mod test {

#[test]
fn nizk_of_secret_key() {
let params = ThresholdParameters::new(3, 2);
let params = ThresholdParameters::new(3, 2).unwrap();
let rng = OsRng;

let (p, _, _) = Participant::<Secp256k1Sha256>::new_dealer(params, 1, rng).unwrap();
Expand Down Expand Up @@ -1496,7 +1496,7 @@ pub(crate) mod test {
#[test]
fn keygen_2_out_of_3_with_malicious_party_high_degree_commitment_polynomial() {
fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
let params = ThresholdParameters::new(3, 2);
let params = ThresholdParameters::new(3, 2)?;
let rng = OsRng;

let (p1, p1coeffs, p1_dh_sk) =
Expand Down Expand Up @@ -1550,7 +1550,7 @@ pub(crate) mod test {
#[test]
fn keygen_static_2_out_of_3_with_common_participants() {
fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
let params = ThresholdParameters::new(3, 2);
let params = ThresholdParameters::new(3, 2)?;
let rng = OsRng;

let (dealer1, dealer1coeffs, dealer1_dh_sk) =
Expand Down Expand Up @@ -1828,7 +1828,7 @@ pub(crate) mod test {
#[test]
fn keygen_verify_complaint() {
fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
let params = ThresholdParameters::new(3, 2);
let params = ThresholdParameters::new(3, 2)?;
let rng = OsRng;

let (p1, p1coeffs, dh_sk1) =
Expand Down Expand Up @@ -2093,7 +2093,7 @@ pub(crate) mod test {
#[test]
fn keygen_verify_complaint_during_resharing() {
fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
let params_dealers = ThresholdParameters::new(3, 2);
let params_dealers = ThresholdParameters::new(3, 2)?;
let rng = OsRng;

let (dealer1, dealer1coeffs, dealer1_dh_sk) =
Expand Down Expand Up @@ -2214,7 +2214,7 @@ pub(crate) mod test {
assert!(dealer1_group_key == dealer2_group_key);
assert!(dealer2_group_key == dealer3_group_key);

let params_signers = ThresholdParameters::<Secp256k1Sha256>::new(5, 3);
let params_signers = ThresholdParameters::<Secp256k1Sha256>::new(5, 3)?;
let (signer1, signer1_dh_sk) = Participant::new_signer(params_signers, 1, rng).unwrap();
let (signer2, signer2_dh_sk) = Participant::new_signer(params_signers, 2, rng).unwrap();
let (signer3, signer3_dh_sk) = Participant::new_signer(params_signers, 3, rng).unwrap();
Expand Down Expand Up @@ -2409,7 +2409,7 @@ pub(crate) mod test {
#[test]
fn test_serialization() {
fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
let params = ThresholdParameters::new(3, 2);
let params = ThresholdParameters::new(3, 2)?;
let rng = OsRng;

let (p1, p1coeffs, p1_dh_sk) = Participant::new_dealer(params, 1, rng).unwrap();
Expand Down Expand Up @@ -2618,7 +2618,7 @@ pub(crate) mod test {
#[test]
fn individual_public_key_share() {
fn do_test() -> FrostResult<Secp256k1Sha256, ()> {
let params = ThresholdParameters::new(3, 2);
let params = ThresholdParameters::new(3, 2)?;
let rng = OsRng;

let (p1, p1coeffs, p1_dh_sk) =
Expand Down
2 changes: 1 addition & 1 deletion src/dkg/participant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ mod test {

#[test]
fn index_zero_is_invalid() {
let params = ThresholdParameters::new(3, 2);
let params = ThresholdParameters::new(3, 2).unwrap();
let rng = OsRng;

let result = Participant::<Secp256k1Sha256>::new_dealer(params, 0, rng);
Expand Down
146 changes: 53 additions & 93 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,112 +55,72 @@ pub enum Error<C: CipherSuite> {
InvalidSignature,
/// Misbehaving participants
MisbehavingParticipants(Vec<u32>),
/// A valid [`ThresholdParams`] requires non-zero participants, non-zero
/// threshold and more participants than the threshold.
InvalidThresholdParams,
/// Custom error
Custom(String),
}

impl<C: CipherSuite> core::fmt::Display for Error<C> {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
match self {
Error::SerializationError => {
write!(f, "An error happened while serializing.")
}
Error::DeserializationError => {
write!(f, "An error happened while deserializing.")
}
Error::CompressionError => {
write!(f, "An error happened while compressing a point.")
}
Error::SerializationError => write!(f, "An error happened while serializing."),
Error::DeserializationError => write!(f, "An error happened while deserializing."),
Error::CompressionError => write!(f, "An error happened while compressing a point."),
Error::DecompressionError => {
write!(f, "An error happened while decompressing a point.")
}
Error::DecryptionError => {
write!(f, "Could not decrypt encrypted share.")
}
Error::EncryptionError => {
write!(f, "Could not encrypt secret share.")
}
Error::ShareVerificationError => {
write!(f, "The secret share is not correct.")
}
Error::ComplaintVerificationError => {
write!(f, "The complaint is not correct.")
}
Error::IndexIsZero => {
write!(f, "The indexs of a participant cannot be 0.")
}
Error::InvalidGroupKey => {
write!(
f,
"Could not generate a valid group key with the given commitments."
)
}
Error::InvalidProofOfKnowledge => {
write!(
f,
"The NiZK proof of knowledge of the secret key is not correct."
)
}
Error::InvalidCommitmentLength => {
write!(
f,
"The length of this commitment does not correspond to the threshold parameter."
)
}
Error::MissingShares => {
write!(f, "Some shares are missing.")
}
Error::NoEncryptedShares => {
write!(f, "Could not retrieve encrypted shares.")
}
Error::Complaint(complaints) => {
write!(f, "{:?}", complaints)
}
Error::InvalidMSMParameters => {
write!(
f,
"The provided slices of points and scalars do not match in length."
)
}
Error::InvalidNumberOfParticipants(nb, n_params) => {
write!(
f,
"The number of participants {} does not match Dkg instance parameters {}.",
nb, n_params
)
}
Error::TooManyInvalidParticipants(indices) => {
write!(
f,
"Too many invalid participants to continue the Dkg: {:?}",
indices
)
}
Error::MissingCommitmentShares => {
write!(
f,
"The participant is missing commitment shares for signing."
)
}
Error::DecryptionError => write!(f, "Could not decrypt encrypted share."),
Error::EncryptionError => write!(f, "Could not encrypt secret share."),
Error::ShareVerificationError => write!(f, "The secret share is not correct."),
Error::ComplaintVerificationError => write!(f, "The complaint is not correct."),
Error::IndexIsZero => write!(f, "The indexs of a participant cannot be 0."),
Error::InvalidGroupKey => write!(
f,
"Could not generate a valid group key with the given commitments."
),
Error::InvalidProofOfKnowledge => write!(
f,
"The NiZK proof of knowledge of the secret key is not correct."
),
Error::InvalidCommitmentLength => write!(
f,
"The length of this commitment does not correspond to the threshold parameter."
),
Error::MissingShares => write!(f, "Some shares are missing."),
Error::NoEncryptedShares => write!(f, "Could not retrieve encrypted shares."),
Error::Complaint(complaints) => write!(f, "{:?}", complaints),
Error::InvalidMSMParameters => write!(
f,
"The provided slices of points and scalars do not match in length."
),
Error::InvalidNumberOfParticipants(nb, n_params) => write!(
f,
"The number of participants {} does not match Dkg instance parameters {}.",
nb, n_params
),
Error::TooManyInvalidParticipants(indices) => write!(
f,
"Too many invalid participants to continue the Dkg: {:?}",
indices
),
Error::MissingCommitmentShares => write!(
f,
"The participant is missing commitment shares for signing."
),
Error::InvalidBindingFactor => {
write!(f, "Could not compute the participant binding factor.")
}
Error::InvalidChallenge => {
write!(f, "Could not compute the signature challenge.")
}
Error::InvalidSignature => {
write!(f, "The threshold signature is not correct.")
}
Error::MisbehavingParticipants(indices) => {
write!(
f,
"These participants provided invalid partial signatures: {:?}",
indices
)
}
Error::Custom(string) => {
write!(f, "{:?}", string)
}
Error::InvalidChallenge => write!(f, "Could not compute the signature challenge."),
Error::InvalidSignature => write!(f, "The threshold signature is not correct."),
Error::MisbehavingParticipants(indices) => write!(
f,
"These participants provided invalid partial signatures: {:?}",
indices
),
Error::InvalidThresholdParams => write!(f, "Invalid threshold parameters"),
Error::Custom(string) => write!(f, "{:?}", string),
}
}
}
Expand Down
Loading

0 comments on commit 2560f0a

Please sign in to comment.