Skip to content

Commit

Permalink
add test
Browse files Browse the repository at this point in the history
  • Loading branch information
xoloki committed Dec 17, 2024
1 parent efed758 commit 9401949
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/state_machine/coordinator/fire.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1282,8 +1282,8 @@ pub mod test {
fire::Coordinator as FireCoordinator,
test::{
check_signature_shares, coordinator_state_machine, equal_after_save_load,
feedback_messages, feedback_mutated_messages, new_coordinator, run_dkg_sign,
setup, setup_with_timeouts, start_dkg_round,
feedback_messages, feedback_mutated_messages, gen_nonces, new_coordinator,
run_dkg_sign, setup, setup_with_timeouts, start_dkg_round,
},
Config, Coordinator as CoordinatorTrait, State,
},
Expand Down Expand Up @@ -2812,4 +2812,14 @@ pub mod test {
assert_eq!(coordinator.current_sign_id, id);
}
}

#[test]
fn gen_nonces_v1() {
gen_nonces::<FireCoordinator<v1::Aggregator>, v1::Signer>(5, 1);
}

#[test]
fn gen_nonces_v2() {
gen_nonces::<FireCoordinator<v2::Aggregator>, v2::Signer>(5, 1);
}
}
68 changes: 68 additions & 0 deletions src/state_machine/coordinator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,7 @@ pub mod fire;
#[allow(missing_docs)]
pub mod test {
use hashbrown::{HashMap, HashSet};
use rand_core::OsRng;
use std::{sync::Once, time::Duration};
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

Expand Down Expand Up @@ -972,4 +973,71 @@ pub mod test {

assert_eq!(signers, loaded_signers);
}

pub fn gen_nonces<Coordinator: CoordinatorTrait, SignerType: SignerTrait>(
num_signers: u32,
keys_per_signer: u32,
) {
let mut rng = OsRng;

let (mut coordinators, mut signers) =
run_dkg::<Coordinator, SignerType>(num_signers, keys_per_signer);

let msg = "It was many and many a year ago, in a kingdom by the sea"
.as_bytes()
.to_vec();

let signature_type = SignatureType::Frost;

// Start a signing round
let message = coordinators
.first_mut()
.unwrap()
.start_signing_round(&msg, signature_type)
.unwrap();
assert_eq!(
coordinators.first_mut().unwrap().get_state(),
State::NonceGather(signature_type)
);

// Send the message to all signers and gather responses by sharing with all other signers and coordinator
let (outbound_messages, operation_results) =
feedback_messages(&mut coordinators, &mut signers, &[message]);
assert!(operation_results.is_empty());
assert_eq!(
coordinators.first_mut().unwrap().get_state(),
State::SigShareGather(signature_type)
);

assert_eq!(outbound_messages.len(), 1);
match &outbound_messages[0].msg {
Message::SignatureShareRequest(_) => {}
_ => {
panic!("Expected SignatureShareRequest message");
}
}

let messages1 = signers[0]
.process(&outbound_messages[0].msg, &mut rng)
.unwrap();

let messages2 = signers[0]
.process(&outbound_messages[0].msg, &mut rng)
.unwrap();

for (message1, message2) in messages1.into_iter().zip(messages2) {
let share1 = if let Message::SignatureShareResponse(response) = message1 {
response.signature_shares[0].clone()
} else {
panic!("");
};
let share2 = if let Message::SignatureShareResponse(response) = message2 {
response.signature_shares[0].clone()
} else {
panic!("");
};

assert!(share1.z_i != share2.z_i);
}
}
}

0 comments on commit 9401949

Please sign in to comment.