From 71536bf56ce300eaaf91f312442ea2c9698097e5 Mon Sep 17 00:00:00 2001 From: Tony Zhou Date: Wed, 4 Dec 2024 09:39:50 -0600 Subject: [PATCH] Refactor import order and clean up whitespace in the code (#101) --- src/main.rs | 2 +- src/net.rs | 8 ++-- src/state_machine/coordinator/fire.rs | 2 +- src/state_machine/coordinator/frost.rs | 3 +- src/state_machine/coordinator/mod.rs | 4 +- src/state_machine/signer/mod.rs | 61 +++++++++++++++++--------- src/taproot.rs | 3 +- src/util.rs | 2 +- src/v1.rs | 3 +- src/v2.rs | 2 +- 10 files changed, 55 insertions(+), 35 deletions(-) diff --git a/src/main.rs b/src/main.rs index 96190773..f8bb55ad 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ use std::{env, time}; -use wsts::{common::test_helpers::gen_signer_ids, traits::Aggregator, v1, v2, util::create_rng}; +use wsts::{common::test_helpers::gen_signer_ids, traits::Aggregator, util::create_rng, v1, v2}; #[allow(non_snake_case)] fn main() { diff --git a/src/net.rs b/src/net.rs index 5c352316..2d305ff8 100644 --- a/src/net.rs +++ b/src/net.rs @@ -605,8 +605,8 @@ impl Packet { mod test { use super::*; use crate::schnorr::ID; - use rand_core::{CryptoRng, RngCore}; use crate::util::create_rng; + use rand_core::{CryptoRng, RngCore}; #[derive(Clone, Debug)] #[allow(dead_code)] @@ -624,12 +624,12 @@ mod test { let coordinator_public_key = ecdsa::PublicKey::new(&coordinator_private_key).unwrap(); let signer_private_key = Scalar::random(rng); let signer_public_key = ecdsa::PublicKey::new(&signer_private_key).unwrap(); - + let mut signer_ids_map = HashMap::new(); let mut key_ids_map = HashMap::new(); signer_ids_map.insert(0, signer_public_key); key_ids_map.insert(1, signer_public_key); - + let public_keys = PublicKeys { signers: signer_ids_map, key_ids: key_ids_map, @@ -651,7 +651,7 @@ mod test { Self::new(&mut rng) } } - + #[test] fn dkg_begin_verify_msg() { let test_config = TestConfig::default(); diff --git a/src/state_machine/coordinator/fire.rs b/src/state_machine/coordinator/fire.rs index 1d9d44bd..25bff2bb 100644 --- a/src/state_machine/coordinator/fire.rs +++ b/src/state_machine/coordinator/fire.rs @@ -1282,8 +1282,8 @@ pub mod test { DkgError, OperationResult, SignError, }, traits::{Aggregator as AggregatorTrait, Signer as SignerTrait}, - v1, v2, util::create_rng, + v1, v2, }; use hashbrown::HashMap; use std::{thread, time::Duration}; diff --git a/src/state_machine/coordinator/frost.rs b/src/state_machine/coordinator/frost.rs index c8922a4b..d18df472 100644 --- a/src/state_machine/coordinator/frost.rs +++ b/src/state_machine/coordinator/frost.rs @@ -774,10 +774,9 @@ pub mod test { Config, Coordinator as CoordinatorTrait, State, }, traits::Aggregator as AggregatorTrait, - v1, v2, util::create_rng, + v1, v2, }; - #[test] fn new_coordinator_v1() { diff --git a/src/state_machine/coordinator/mod.rs b/src/state_machine/coordinator/mod.rs index 5a19e8fe..38ce55b2 100644 --- a/src/state_machine/coordinator/mod.rs +++ b/src/state_machine/coordinator/mod.rs @@ -567,7 +567,9 @@ pub mod test { inbound_messages.extend(outbound_messages); } for signer in signers.iter_mut() { - let outbound_messages = signer.process_inbound_messages(&feedback_messages, &mut rng).unwrap(); + let outbound_messages = signer + .process_inbound_messages(&feedback_messages, &mut rng) + .unwrap(); inbound_messages.extend(outbound_messages); } for coordinator in coordinators.iter_mut() { diff --git a/src/state_machine/signer/mod.rs b/src/state_machine/signer/mod.rs index 13bfcd5c..8e5f88d0 100644 --- a/src/state_machine/signer/mod.rs +++ b/src/state_machine/signer/mod.rs @@ -182,7 +182,7 @@ impl Signer { rng: &mut R, ) -> Self { assert!(threshold <= total_keys); - + let signer = SignerType::new( signer_id, &key_ids, @@ -324,7 +324,7 @@ impl Signer { Message::DkgPublicShares(dkg_public_shares) => self.dkg_public_share(dkg_public_shares), Message::DkgPrivateShares(dkg_private_shares) => { self.dkg_private_shares(dkg_private_shares, rng) - } + } Message::SignatureShareRequest(sign_share_request) => { self.sign_share_request(sign_share_request) } @@ -346,10 +346,7 @@ impl Signer { } /// DKG is done so compute secrets - pub fn dkg_ended( - &mut self, - rng: &mut R, - ) -> Result { + pub fn dkg_ended(&mut self, rng: &mut R) -> Result { if !self.can_dkg_end() { return Ok(Message::DkgEnd(DkgEnd { dkg_id: self.dkg_id, @@ -649,7 +646,6 @@ impl Signer { &mut self, rng: &mut R, ) -> Result, Error> { - let mut msgs = vec![]; let comms = self.signer.get_poly_commitments(rng); @@ -726,8 +722,7 @@ impl Signer { let dst_public_key = Point::try_from(&compressed).unwrap(); let shared_secret = make_shared_secret(&self.network_private_key, &dst_public_key); - let encrypted_share = - encrypt(&shared_secret, &private_share.to_bytes(), rng)?; + let encrypted_share = encrypt(&shared_secret, &private_share.to_bytes(), rng)?; encrypted_shares.insert(*dst_key_id, encrypted_share); } @@ -812,8 +807,10 @@ impl Signer { }, Err(e) => { warn!("Failed to decrypt dkg private share from src_id {} to dst_id {}: {:?}", src_id, dst_key_id, e); - self.invalid_private_shares - .insert(src_signer_id, self.make_bad_private_share(src_signer_id, rng)); + self.invalid_private_shares.insert( + src_signer_id, + self.make_bad_private_share(src_signer_id, rng), + ); } } } @@ -837,7 +834,6 @@ impl Signer { signer_id: u32, rng: &mut R, ) -> BadPrivateShare { - let a = self.network_private_key; let A = a * G; let B = Point::try_from(&Compressed::from( @@ -899,8 +895,8 @@ pub mod test { PublicKeys, }, traits::Signer as SignerTrait, - v1, v2, util::create_rng, + v1, v2, }; #[test] @@ -915,8 +911,16 @@ pub mod test { fn dkg_public_share() { let mut rng = create_rng(); - let mut signer = - Signer::::new(1, 1, 1, 1, vec![1], Default::default(), Default::default(), &mut rng); + let mut signer = Signer::::new( + 1, + 1, + 1, + 1, + vec![1], + Default::default(), + Default::default(), + &mut rng, + ); let public_share = DkgPublicShares { dkg_id: 0, signer_id: 0, @@ -944,8 +948,16 @@ pub mod test { fn public_shares_done() { let mut rng = create_rng(); - let mut signer = - Signer::::new(1, 1, 1, 1, vec![1], Default::default(), Default::default(), &mut rng); + let mut signer = Signer::::new( + 1, + 1, + 1, + 1, + vec![1], + Default::default(), + Default::default(), + &mut rng, + ); // publich_shares_done starts out as false assert!(!signer.public_shares_done()); @@ -982,7 +994,8 @@ pub mod test { public_keys.signers.insert(0, public_key.clone()); public_keys.key_ids.insert(1, public_key.clone()); - let mut signer = Signer::::new(1, 1, 1, 0, vec![1], private_key, public_keys, &mut rng); + let mut signer = + Signer::::new(1, 1, 1, 0, vec![1], private_key, public_keys, &mut rng); // can_dkg_end starts out as false assert!(!signer.can_dkg_end()); @@ -1035,8 +1048,16 @@ pub mod test { .with(EnvFilter::from_default_env()) .init();*/ let mut rng = create_rng(); - let mut signer = - Signer::::new(1, 1, 1, 0, vec![1], Default::default(), Default::default(), &mut rng); + let mut signer = Signer::::new( + 1, + 1, + 1, + 0, + vec![1], + Default::default(), + Default::default(), + &mut rng, + ); if let Ok(Message::DkgEnd(dkg_end)) = signer.dkg_ended(&mut rng) { match dkg_end.status { diff --git a/src/taproot.rs b/src/taproot.rs index 1f7c50ae..816503bd 100644 --- a/src/taproot.rs +++ b/src/taproot.rs @@ -148,8 +148,7 @@ pub mod test_helpers { mod test { use super::{test_helpers, Point, Scalar, SchnorrProof, G}; - use crate::{compute, traits::Aggregator, traits::Signer, v1, v2, util::create_rng}; - + use crate::{compute, traits::Aggregator, traits::Signer, util::create_rng, v1, v2}; #[test] #[allow(non_snake_case)] diff --git a/src/util.rs b/src/util.rs index 0abd9c7d..402e2e7b 100644 --- a/src/util.rs +++ b/src/util.rs @@ -1,5 +1,5 @@ use aes_gcm::{aead::Aead, Aes256Gcm, Error as AesGcmError, KeyInit, Nonce}; -use rand_core::{CryptoRng, RngCore, OsRng}; +use rand_core::{CryptoRng, OsRng, RngCore}; use sha2::{Digest, Sha256}; use crate::curve::{point::Point, scalar::Scalar}; diff --git a/src/v1.rs b/src/v1.rs index b36c9d25..fa1a66ac 100644 --- a/src/v1.rs +++ b/src/v1.rs @@ -18,7 +18,6 @@ use crate::{ vss::VSS, }; - #[derive(Clone, Debug, Eq, PartialEq)] /// A FROST party, which encapsulates a single polynomial, nonce, and key pub struct Party { @@ -790,8 +789,8 @@ mod tests { use crate::traits; use crate::traits::test_helpers::run_compute_secrets_missing_private_shares; use crate::traits::{Aggregator, Signer}; - use crate::v1; use crate::util::create_rng; + use crate::v1; use num_traits::Zero; diff --git a/src/v2.rs b/src/v2.rs index 0a56c13f..a24e2354 100644 --- a/src/v2.rs +++ b/src/v2.rs @@ -732,13 +732,13 @@ pub mod test_helpers { #[cfg(test)] mod tests { + use crate::util::create_rng; use crate::{ traits::{ self, test_helpers::run_compute_secrets_missing_private_shares, Aggregator, Signer, }, v2, }; - use crate::util::create_rng; #[test] fn party_save_load() {