From c6f2d6b5fb47fabfae4b71148456f0272150f21f Mon Sep 17 00:00:00 2001 From: natalie Date: Thu, 23 Mar 2023 22:24:33 +0000 Subject: [PATCH] Update keygen_with_dealer to return a hashmap (#288) * Change keygen_with_dealer to return a HashMap (#282) Update docs * Add vscode folder to gitignore --- .gitignore | 1 + frost-core/src/benches.rs | 15 ++++++--------- frost-core/src/frost/keys.rs | 11 ++++++++--- frost-core/src/tests.rs | 16 +++++++--------- frost-ed25519/README.md | 10 ++++++---- frost-ed25519/src/lib.rs | 4 +++- frost-ed448/README.md | 10 ++++++---- frost-ed448/src/lib.rs | 3 ++- frost-p256/README.md | 10 ++++++---- frost-p256/src/lib.rs | 4 +++- frost-rerandomized/src/tests.rs | 15 ++++++--------- frost-ristretto255/README.md | 10 ++++++---- frost-ristretto255/src/lib.rs | 3 ++- frost-secp256k1/README.md | 10 ++++++---- frost-secp256k1/src/lib.rs | 3 ++- 15 files changed, 70 insertions(+), 55 deletions(-) diff --git a/.gitignore b/.gitignore index b3e1d16..5d3d773 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ Cargo.lock *~ **/.DS_Store +.vscode/* diff --git a/frost-core/src/benches.rs b/frost-core/src/benches.rs index 0274139..b8a2785 100644 --- a/frost-core/src/benches.rs +++ b/frost-core/src/benches.rs @@ -102,15 +102,12 @@ pub fn bench_sign( frost::keys::keygen_with_dealer::(max_signers, min_signers, rng).unwrap(); // Verifies the secret shares from the dealer - let key_packages: HashMap<_, _> = shares - .into_iter() - .map(|share| { - ( - share.identifier, - frost::keys::KeyPackage::try_from(share).unwrap(), - ) - }) - .collect(); + let mut key_packages: HashMap, frost::keys::KeyPackage> = + HashMap::new(); + + for (k, v) in shares { + key_packages.insert(k, frost::keys::KeyPackage::try_from(v).unwrap()); + } group.bench_with_input( BenchmarkId::new("Round 1", min_signers), diff --git a/frost-core/src/frost/keys.rs b/frost-core/src/frost/keys.rs index f07aeb4..49e7ff4 100644 --- a/frost-core/src/frost/keys.rs +++ b/frost-core/src/frost/keys.rs @@ -323,7 +323,7 @@ pub fn keygen_with_dealer( max_signers: u16, min_signers: u16, rng: &mut R, -) -> Result<(Vec>, PublicKeyPackage), Error> { +) -> Result<(HashMap, SecretShare>, PublicKeyPackage), Error> { let mut bytes = [0; 64]; rng.fill_bytes(&mut bytes); @@ -336,13 +336,18 @@ pub fn keygen_with_dealer( let mut signer_pubkeys: HashMap, VerifyingShare> = HashMap::with_capacity(max_signers as usize); - for secret_share in &secret_shares { + let mut secret_shares_by_id: HashMap, SecretShare> = + HashMap::with_capacity(max_signers as usize); + + for secret_share in secret_shares { let signer_public = secret_share.value.into(); signer_pubkeys.insert(secret_share.identifier, signer_public); + + secret_shares_by_id.insert(secret_share.identifier, secret_share); } Ok(( - secret_shares, + secret_shares_by_id, PublicKeyPackage { signer_pubkeys, group_public, diff --git a/frost-core/src/tests.rs b/frost-core/src/tests.rs index 0315b59..ba19d98 100644 --- a/frost-core/src/tests.rs +++ b/frost-core/src/tests.rs @@ -48,15 +48,13 @@ pub fn check_sign_with_dealer( frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng).unwrap(); // Verifies the secret shares from the dealer - let key_packages: HashMap, frost::keys::KeyPackage> = shares - .into_iter() - .map(|share| { - ( - share.identifier, - frost::keys::KeyPackage::try_from(share).unwrap(), - ) - }) - .collect(); + let mut key_packages: HashMap, frost::keys::KeyPackage> = + HashMap::new(); + + for (k, v) in shares { + let key_package = frost::keys::KeyPackage::try_from(v).unwrap(); + key_packages.insert(k, key_package); + } check_sign(min_signers, key_packages, rng, pubkeys) } diff --git a/frost-ed25519/README.md b/frost-ed25519/README.md index 0022d3d..d355b43 100644 --- a/frost-ed25519/README.md +++ b/frost-ed25519/README.md @@ -21,10 +21,12 @@ let (shares, pubkeys) = frost::keys::keygen_with_dealer(max_signers, min_signers // Verifies the secret shares from the dealer and store them in a HashMap. // In practice, the KeyPackages must be sent to its respective participants // through a confidential and authenticated channel. -let key_packages: HashMap<_, _> = shares - .into_iter() - .map(|share| Ok((share.identifier, frost::keys::KeyPackage::try_from(share)?))) - .collect::>()?; +let mut key_packages: HashMap<_, _> = HashMap::new(); + +for (k, v) in shares { + let key_package = frost::keys::KeyPackage::try_from(v)?; + key_packages.insert(k, key_package); +} let mut nonces = HashMap::new(); let mut commitments = HashMap::new(); diff --git a/frost-ed25519/src/lib.rs b/frost-ed25519/src/lib.rs index 9673651..be4bab6 100644 --- a/frost-ed25519/src/lib.rs +++ b/frost-ed25519/src/lib.rs @@ -200,6 +200,8 @@ pub type Identifier = frost::Identifier; /// FROST(Ed25519, SHA-512) keys, key generation, key shares. pub mod keys { + use std::collections::HashMap; + use super::*; /// Allows all participants' keys to be generated using a central, trusted @@ -208,7 +210,7 @@ pub mod keys { max_signers: u16, min_signers: u16, mut rng: RNG, - ) -> Result<(Vec, PublicKeyPackage), Error> { + ) -> Result<(HashMap, PublicKeyPackage), Error> { frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng) } diff --git a/frost-ed448/README.md b/frost-ed448/README.md index 61391af..de00eee 100644 --- a/frost-ed448/README.md +++ b/frost-ed448/README.md @@ -21,10 +21,12 @@ let (shares, pubkeys) = frost::keys::keygen_with_dealer(max_signers, min_signers // Verifies the secret shares from the dealer and store them in a HashMap. // In practice, the KeyPackages must be sent to its respective participants // through a confidential and authenticated channel. -let key_packages: HashMap<_, _> = shares - .into_iter() - .map(|share| Ok((share.identifier, frost::keys::KeyPackage::try_from(share)?))) - .collect::>()?; +let mut key_packages: HashMap<_, _> = HashMap::new(); + +for (k, v) in shares { + let key_package = frost::keys::KeyPackage::try_from(v)?; + key_packages.insert(k, key_package); +} let mut nonces = HashMap::new(); let mut commitments = HashMap::new(); diff --git a/frost-ed448/src/lib.rs b/frost-ed448/src/lib.rs index 32705d8..d6449f7 100644 --- a/frost-ed448/src/lib.rs +++ b/frost-ed448/src/lib.rs @@ -196,6 +196,7 @@ pub type Identifier = frost::Identifier; /// FROST(Ed448, SHAKE256) keys, key generation, key shares. pub mod keys { use super::*; + use std::collections::HashMap; /// Allows all participants' keys to be generated using a central, trusted /// dealer. @@ -203,7 +204,7 @@ pub mod keys { max_signers: u16, min_signers: u16, mut rng: RNG, - ) -> Result<(Vec, PublicKeyPackage), Error> { + ) -> Result<(HashMap, PublicKeyPackage), Error> { frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng) } diff --git a/frost-p256/README.md b/frost-p256/README.md index d16f673..e3774fb 100644 --- a/frost-p256/README.md +++ b/frost-p256/README.md @@ -21,10 +21,12 @@ let (shares, pubkeys) = frost::keys::keygen_with_dealer(max_signers, min_signers // Verifies the secret shares from the dealer and store them in a HashMap. // In practice, the KeyPackages must be sent to its respective participants // through a confidential and authenticated channel. -let key_packages: HashMap<_, _> = shares - .into_iter() - .map(|share| Ok((share.identifier, frost::keys::KeyPackage::try_from(share)?))) - .collect::>()?; +let mut key_packages: HashMap<_, _> = HashMap::new(); + +for (k, v) in shares { + let key_package = frost::keys::KeyPackage::try_from(v)?; + key_packages.insert(k, key_package); +} let mut nonces = HashMap::new(); let mut commitments = HashMap::new(); diff --git a/frost-p256/src/lib.rs b/frost-p256/src/lib.rs index f33ada0..b53d216 100644 --- a/frost-p256/src/lib.rs +++ b/frost-p256/src/lib.rs @@ -224,6 +224,8 @@ pub type Identifier = frost::Identifier

; /// FROST(P-256, SHA-256) keys, key generation, key shares. pub mod keys { + use std::collections::HashMap; + use super::*; /// Allows all participants' keys to be generated using a central, trusted @@ -232,7 +234,7 @@ pub mod keys { max_signers: u16, min_signers: u16, mut rng: RNG, - ) -> Result<(Vec, PublicKeyPackage), Error> { + ) -> Result<(HashMap, PublicKeyPackage), Error> { frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng) } diff --git a/frost-rerandomized/src/tests.rs b/frost-rerandomized/src/tests.rs index a08ad41..8c5ebc1 100644 --- a/frost-rerandomized/src/tests.rs +++ b/frost-rerandomized/src/tests.rs @@ -22,15 +22,12 @@ pub fn check_randomized_sign_with_dealer frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng).unwrap(); // Verifies the secret shares from the dealer - let key_packages: HashMap, frost::keys::KeyPackage> = shares - .into_iter() - .map(|share| { - ( - share.identifier, - frost::keys::KeyPackage::try_from(share).unwrap(), - ) - }) - .collect(); + let mut key_packages: HashMap, frost::keys::KeyPackage> = + HashMap::new(); + + for (k, v) in shares { + key_packages.insert(k, frost::keys::KeyPackage::try_from(v).unwrap()); + } let mut nonces: HashMap, frost::round1::SigningNonces> = HashMap::new(); let mut commitments: HashMap, frost::round1::SigningCommitments> = diff --git a/frost-ristretto255/README.md b/frost-ristretto255/README.md index 219c0c9..923e57f 100644 --- a/frost-ristretto255/README.md +++ b/frost-ristretto255/README.md @@ -21,10 +21,12 @@ let (shares, pubkeys) = frost::keys::keygen_with_dealer(max_signers, min_signers // Verifies the secret shares from the dealer and store them in a HashMap. // In practice, the KeyPackages must be sent to its respective participants // through a confidential and authenticated channel. -let key_packages: HashMap<_, _> = shares - .into_iter() - .map(|share| Ok((share.identifier, frost::keys::KeyPackage::try_from(share)?))) - .collect::>()?; +let mut key_packages: HashMap<_, _> = HashMap::new(); + +for (k, v) in shares { + let key_package = frost::keys::KeyPackage::try_from(v)?; + key_packages.insert(k, key_package); +} let mut nonces = HashMap::new(); let mut commitments = HashMap::new(); diff --git a/frost-ristretto255/src/lib.rs b/frost-ristretto255/src/lib.rs index 82e4aeb..9270ba5 100644 --- a/frost-ristretto255/src/lib.rs +++ b/frost-ristretto255/src/lib.rs @@ -190,6 +190,7 @@ pub type Identifier = frost::Identifier; /// FROST(ristretto255, SHA-512) keys, key generation, key shares. pub mod keys { use super::*; + use std::collections::HashMap; /// Allows all participants' keys to be generated using a central, trusted /// dealer. @@ -197,7 +198,7 @@ pub mod keys { max_signers: u16, min_signers: u16, mut rng: RNG, - ) -> Result<(Vec, PublicKeyPackage), Error> { + ) -> Result<(HashMap, PublicKeyPackage), Error> { frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng) } diff --git a/frost-secp256k1/README.md b/frost-secp256k1/README.md index ad2b0d2..af179e0 100644 --- a/frost-secp256k1/README.md +++ b/frost-secp256k1/README.md @@ -21,10 +21,12 @@ let (shares, pubkeys) = frost::keys::keygen_with_dealer(max_signers, min_signers // Verifies the secret shares from the dealer and store them in a HashMap. // In practice, the KeyPackages must be sent to its respective participants // through a confidential and authenticated channel. -let key_packages: HashMap<_, _> = shares - .into_iter() - .map(|share| Ok((share.identifier, frost::keys::KeyPackage::try_from(share)?))) - .collect::>()?; +let mut key_packages: HashMap<_, _> = HashMap::new(); + +for (k, v) in shares { + let key_package = frost::keys::KeyPackage::try_from(v)?; + key_packages.insert(k, key_package); +} let mut nonces = HashMap::new(); let mut commitments = HashMap::new(); diff --git a/frost-secp256k1/src/lib.rs b/frost-secp256k1/src/lib.rs index 56308ac..581a9bc 100644 --- a/frost-secp256k1/src/lib.rs +++ b/frost-secp256k1/src/lib.rs @@ -225,6 +225,7 @@ pub type Identifier = frost::Identifier; /// FROST(secp256k1, SHA-256) keys, key generation, key shares. pub mod keys { use super::*; + use std::collections::HashMap; /// Allows all participants' keys to be generated using a central, trusted /// dealer. @@ -232,7 +233,7 @@ pub mod keys { max_signers: u16, min_signers: u16, mut rng: RNG, - ) -> Result<(Vec, PublicKeyPackage), Error> { + ) -> Result<(HashMap, PublicKeyPackage), Error> { frost::keys::keygen_with_dealer(max_signers, min_signers, &mut rng) }