Skip to content

Commit

Permalink
Convert all HashMaps to BTreeMaps (#547)
Browse files Browse the repository at this point in the history
Convert HashMaps to BTreeMaps (#476)
  • Loading branch information
natalieesk authored Sep 22, 2023
1 parent a5dc479 commit ba3ef7d
Show file tree
Hide file tree
Showing 43 changed files with 252 additions and 256 deletions.
2 changes: 1 addition & 1 deletion book/src/tutorial/dkg.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ calls
[`dkg::part2()`](https://docs.rs/frost-ristretto255/latest/frost_ristretto255/keys/dkg/fn.part2.html)
passing their own previously created `round1::SecretPackage` and the list of
received `round1::Packages`. It returns a `round2::SecretPackage` and a
`HashMap` mapping other participants's `Identifier`s to `round2::Package`s:
`BTreeMap` mapping other participants's `Identifier`s to `round2::Package`s:

```rust,no_run,noplayground
{{#include ../../../frost-ristretto255/dkg.md:dkg_part2}}
Expand Down
2 changes: 1 addition & 1 deletion book/src/tutorial/trusted-dealer.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ channel](https://frost.zfnd.org/terminology.html#peer-to-peer-channel).

To generate the key shares, the dealer calls
[`generate_with_dealer()`](https://docs.rs/frost-ristretto255/latest/frost_ristretto255/keys/fn.generate_with_dealer.html).
It returns a `HashMap` mapping the (automatically generated) `Identifier`s to
It returns a `BTreeMap` mapping the (automatically generated) `Identifier`s to
their respective `SecretShare`s, and a `PublicKeyPackage` which contains the
`VerifyingShare` for each participant and the group public key (`VerifyingKey`).

Expand Down
10 changes: 5 additions & 5 deletions frost-core/src/benches.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Ciphersuite-generic benchmark functions.
#![allow(clippy::unwrap_used)]

use std::collections::{BTreeMap, HashMap};
use std::collections::BTreeMap;

use criterion::{BenchmarkId, Criterion, Throughput};
use rand_core::{CryptoRng, RngCore};
Expand Down Expand Up @@ -113,8 +113,8 @@ pub fn bench_sign<C: Ciphersuite, R: RngCore + CryptoRng + Clone>(
.unwrap();

// Verifies the secret shares from the dealer
let mut key_packages: HashMap<frost::Identifier<C>, frost::keys::KeyPackage<C>> =
HashMap::new();
let mut key_packages: BTreeMap<frost::Identifier<C>, frost::keys::KeyPackage<C>> =
BTreeMap::new();

for (k, v) in shares {
key_packages.insert(k, frost::keys::KeyPackage::try_from(v).unwrap());
Expand All @@ -137,7 +137,7 @@ pub fn bench_sign<C: Ciphersuite, R: RngCore + CryptoRng + Clone>(
},
);

let mut nonces: HashMap<_, _> = HashMap::new();
let mut nonces: BTreeMap<_, _> = BTreeMap::new();
let mut commitments: BTreeMap<_, _> = BTreeMap::new();

for participant_index in 1..=min_signers {
Expand Down Expand Up @@ -173,7 +173,7 @@ pub fn bench_sign<C: Ciphersuite, R: RngCore + CryptoRng + Clone>(
},
);

let mut signature_shares = HashMap::new();
let mut signature_shares = BTreeMap::new();
for participant_identifier in nonces.keys() {
let key_package = key_packages.get(participant_identifier).unwrap();
let nonces_to_use = &nonces.get(participant_identifier).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions frost-core/src/frost.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
//! generation and the FROST rounds.
use std::{
collections::{BTreeMap, BTreeSet, HashMap},
collections::{BTreeMap, BTreeSet},
fmt::{self, Debug},
};

Expand Down Expand Up @@ -408,7 +408,7 @@ where
/// service attack due to publishing an invalid signature.
pub fn aggregate<C>(
signing_package: &SigningPackage<C>,
signature_shares: &HashMap<Identifier<C>, round2::SignatureShare<C>>,
signature_shares: &BTreeMap<Identifier<C>, round2::SignatureShare<C>>,
pubkeys: &keys::PublicKeyPackage<C>,
) -> Result<Signature<C>, Error<C>>
where
Expand Down
16 changes: 7 additions & 9 deletions frost-core/src/frost/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![allow(clippy::type_complexity)]

use std::{
collections::{BTreeSet, HashMap, HashSet},
collections::{BTreeMap, BTreeSet, HashSet},
convert::TryFrom,
default::Default,
fmt::{self, Debug},
Expand Down Expand Up @@ -458,7 +458,7 @@ pub fn generate_with_dealer<C: Ciphersuite, R: RngCore + CryptoRng>(
min_signers: u16,
identifiers: IdentifierList<C>,
rng: &mut R,
) -> Result<(HashMap<Identifier<C>, SecretShare<C>>, PublicKeyPackage<C>), Error<C>> {
) -> Result<(BTreeMap<Identifier<C>, SecretShare<C>>, PublicKeyPackage<C>), Error<C>> {
let mut bytes = [0; 64];
rng.fill_bytes(&mut bytes);

Expand All @@ -479,7 +479,7 @@ pub fn split<C: Ciphersuite, R: RngCore + CryptoRng>(
min_signers: u16,
identifiers: IdentifierList<C>,
rng: &mut R,
) -> Result<(HashMap<Identifier<C>, SecretShare<C>>, PublicKeyPackage<C>), Error<C>> {
) -> Result<(BTreeMap<Identifier<C>, SecretShare<C>>, PublicKeyPackage<C>), Error<C>> {
validate_num_of_signers(min_signers, max_signers)?;

if let IdentifierList::Custom(identifiers) = &identifiers {
Expand All @@ -501,11 +501,9 @@ pub fn split<C: Ciphersuite, R: RngCore + CryptoRng>(
generate_secret_shares(key, max_signers, min_signers, coefficients, identifiers)?
}
};
let mut verifying_shares: HashMap<Identifier<C>, VerifyingShare<C>> =
HashMap::with_capacity(max_signers as usize);
let mut verifying_shares: BTreeMap<Identifier<C>, VerifyingShare<C>> = BTreeMap::new();

let mut secret_shares_by_id: HashMap<Identifier<C>, SecretShare<C>> =
HashMap::with_capacity(max_signers as usize);
let mut secret_shares_by_id: BTreeMap<Identifier<C>, SecretShare<C>> = BTreeMap::new();

for secret_share in secret_shares {
let signer_public = secret_share.signing_share.into();
Expand Down Expand Up @@ -673,7 +671,7 @@ pub struct PublicKeyPackage<C: Ciphersuite> {
pub(crate) header: Header<C>,
/// The verifying shares for all participants. Used to validate signature
/// shares they generate.
pub(crate) verifying_shares: HashMap<Identifier<C>, VerifyingShare<C>>,
pub(crate) verifying_shares: BTreeMap<Identifier<C>, VerifyingShare<C>>,
/// The joint public key for the entire group.
pub(crate) verifying_key: VerifyingKey<C>,
}
Expand All @@ -684,7 +682,7 @@ where
{
/// Create a new [`PublicKeyPackage`] instance.
pub fn new(
verifying_shares: HashMap<Identifier<C>, VerifyingShare<C>>,
verifying_shares: BTreeMap<Identifier<C>, VerifyingShare<C>>,
verifying_key: VerifyingKey<C>,
) -> Self {
Self {
Expand Down
18 changes: 9 additions & 9 deletions frost-core/src/frost/keys/dkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
//! [Feldman's VSS]: https://www.cs.umd.edu/~gasarch/TOPICS/secretsharing/feldmanVSS.pdf
//! [secure broadcast channel]: https://frost.zfnd.org/terminology.html#broadcast-channel
use std::{collections::HashMap, iter};
use std::{collections::BTreeMap, iter};

use rand_core::{CryptoRng, RngCore};

Expand Down Expand Up @@ -346,19 +346,19 @@ where
/// must be sent to each participant who has the given identifier in the map key.
pub fn part2<C: Ciphersuite>(
secret_package: round1::SecretPackage<C>,
round1_packages: &HashMap<Identifier<C>, round1::Package<C>>,
round1_packages: &BTreeMap<Identifier<C>, round1::Package<C>>,
) -> Result<
(
round2::SecretPackage<C>,
HashMap<Identifier<C>, round2::Package<C>>,
BTreeMap<Identifier<C>, round2::Package<C>>,
),
Error<C>,
> {
if round1_packages.len() != (secret_package.max_signers - 1) as usize {
return Err(Error::IncorrectNumberOfPackages);
}

let mut round2_packages = HashMap::new();
let mut round2_packages = BTreeMap::new();

for (sender_identifier, round1_package) in round1_packages {
let ell = *sender_identifier;
Expand Down Expand Up @@ -407,14 +407,14 @@ pub fn part2<C: Ciphersuite>(
/// Computes the verifying shares of the other participants for the third step
/// of the DKG protocol.
fn compute_verifying_shares<C: Ciphersuite>(
round1_packages: &HashMap<Identifier<C>, round1::Package<C>>,
round1_packages: &BTreeMap<Identifier<C>, round1::Package<C>>,
round2_secret_package: &round2::SecretPackage<C>,
) -> Result<HashMap<Identifier<C>, VerifyingShare<C>>, Error<C>> {
) -> Result<BTreeMap<Identifier<C>, VerifyingShare<C>>, Error<C>> {
// Round 2, Step 4
//
// > Any participant can compute the public verification share of any other participant
// > by calculating Y_i = ∏_{j=1}^n ∏_{k=0}^{t−1} φ_{jk}^{i^k mod q}.
let mut others_verifying_shares = HashMap::new();
let mut others_verifying_shares = BTreeMap::new();

// Note that in this loop, "i" refers to the other participant whose public verification share
// we are computing, and not the current participant.
Expand Down Expand Up @@ -464,8 +464,8 @@ fn compute_verifying_shares<C: Ciphersuite>(
/// signatures.
pub fn part3<C: Ciphersuite>(
round2_secret_package: &round2::SecretPackage<C>,
round1_packages: &HashMap<Identifier<C>, round1::Package<C>>,
round2_packages: &HashMap<Identifier<C>, round2::Package<C>>,
round1_packages: &BTreeMap<Identifier<C>, round1::Package<C>>,
round2_packages: &BTreeMap<Identifier<C>, round2::Package<C>>,
) -> Result<(KeyPackage<C>, PublicKeyPackage<C>), Error<C>> {
if round1_packages.len() != (round2_secret_package.max_signers - 1) as usize {
return Err(Error::IncorrectNumberOfPackages);
Expand Down
12 changes: 6 additions & 6 deletions frost-core/src/frost/keys/repairable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
//! The RTS is used to help a signer (participant) repair their lost share. This is achieved
//! using a subset of the other signers know here as `helpers`.
use std::collections::{BTreeSet, HashMap};
use std::collections::{BTreeMap, BTreeSet};

use crate::{
frost::{compute_lagrange_coefficient, Identifier},
Expand All @@ -19,13 +19,13 @@ use super::{generate_coefficients, SecretShare, SigningShare, VerifiableSecretSh
/// where `helpers` contains the identifiers of all the helpers (including `helper_i`), and `share_i`
/// is the share of `helper_i`.
///
/// Returns a HashMap mapping which value should be sent to which participant.
/// Returns a BTreeMap mapping which value should be sent to which participant.
pub fn repair_share_step_1<C: Ciphersuite, R: RngCore + CryptoRng>(
helpers: &[Identifier<C>],
share_i: &SecretShare<C>,
rng: &mut R,
participant: Identifier<C>,
) -> Result<HashMap<Identifier<C>, Scalar<C>>, Error<C>> {
) -> Result<BTreeMap<Identifier<C>, Scalar<C>>, Error<C>> {
if helpers.len() < 2 {
return Err(Error::InvalidMinSigners);
}
Expand All @@ -46,19 +46,19 @@ pub fn repair_share_step_1<C: Ciphersuite, R: RngCore + CryptoRng>(
/// Compute the last delta value given the (generated uniformly at random) remaining ones
/// since they all must add up to `zeta_i * share_i`.
///
/// Returns a HashMap mapping which value should be sent to which participant.
/// Returns a BTreeMap mapping which value should be sent to which participant.
fn compute_last_random_value<C: Ciphersuite>(
helpers: &BTreeSet<Identifier<C>>,
share_i: &SecretShare<C>,
random_values: &Vec<Scalar<C>>,
participant: Identifier<C>,
) -> Result<HashMap<Identifier<C>, Scalar<C>>, Error<C>> {
) -> Result<BTreeMap<Identifier<C>, Scalar<C>>, Error<C>> {
// Calculate Lagrange Coefficient for helper_i
let zeta_i = compute_lagrange_coefficient(helpers, Some(participant), share_i.identifier)?;

let lhs = zeta_i * share_i.signing_share.0;

let mut out: HashMap<Identifier<C>, Scalar<C>> = helpers
let mut out: BTreeMap<Identifier<C>, Scalar<C>> = helpers
.iter()
.copied()
.zip(random_values.iter().copied())
Expand Down
Loading

0 comments on commit ba3ef7d

Please sign in to comment.