From 81191c0659bb9bf1f4ef901e3d6cc428fc45eb22 Mon Sep 17 00:00:00 2001 From: Denis Varlakov Date: Wed, 27 Nov 2024 14:42:45 +0100 Subject: [PATCH] Update round-based to v0.4 Signed-off-by: Denis Varlakov --- Cargo.lock | 37 ++-------- Cargo.toml | 6 +- cggmp21/Cargo.toml | 2 +- cggmp21/src/signing.rs | 2 +- tests/Cargo.toml | 3 +- tests/src/bin/measure_perf.rs | 92 +++++++++---------------- tests/tests/it/key_refresh.rs | 100 +++++++++++---------------- tests/tests/it/keygen.rs | 63 ++++++----------- tests/tests/it/pipeline.rs | 110 ++++++++++++------------------ tests/tests/it/signing.rs | 106 +++++++++++----------------- tests/tests/it/stark_prehashed.rs | 35 ++++------ 11 files changed, 202 insertions(+), 354 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d89b8e25..9df08489 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -342,7 +342,6 @@ dependencies = [ "starknet-providers", "starknet-signers", "test-case", - "tokio", "url", ] @@ -1815,25 +1814,21 @@ dependencies = [ [[package]] name = "round-based" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "55f473f56ecfa6457a75c0922c4e51ad036d4a1d27b0a697ac3a9b26f74acc3b" +version = "0.4.0" +source = "git+https://github.com/LFDT-Lockness/round-based?branch=impr-sim#e662e7142eb3c5d96f3202c6cc7f853a2db0486b" dependencies = [ "displaydoc", "futures-util", "phantom-type 0.3.1", "round-based-derive", "thiserror", - "tokio", - "tokio-stream", "tracing", ] [[package]] name = "round-based-derive" -version = "0.2.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1c3f220fb17bab108a448f516ce4ec470584675233ab3a799915ba71295da32e" +version = "0.2.2" +source = "git+https://github.com/LFDT-Lockness/round-based?branch=impr-sim#e662e7142eb3c5d96f3202c6cc7f853a2db0486b" dependencies = [ "proc-macro2", "quote", @@ -2448,21 +2443,9 @@ dependencies = [ "mio", "pin-project-lite", "socket2", - "tokio-macros", "winapi", ] -[[package]] -name = "tokio-macros" -version = "1.8.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d266c00fde287f55d3f1c3e96c500c362a2b8c695076ec180f27918820bc6df8" -dependencies = [ - "proc-macro2", - "quote", - "syn 1.0.101", -] - [[package]] name = "tokio-rustls" version = "0.24.1" @@ -2473,18 +2456,6 @@ dependencies = [ "tokio", ] -[[package]] -name = "tokio-stream" -version = "0.1.10" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f6edf2d6bc038a43d31353570e27270603f4648d18f5ed10c0e179abe43255af" -dependencies = [ - "futures-core", - "pin-project-lite", - "tokio", - "tokio-util", -] - [[package]] name = "tokio-util" version = "0.7.4" diff --git a/Cargo.toml b/Cargo.toml index f7e54d2f..263f06bd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,7 +17,7 @@ key-share = { version = "0.6", path = "key-share", default-features = false } generic-ec = { version = "0.4.1", default-features = false } generic-ec-zkp = { version = "0.4.1", default-features = false } -round-based = { version = "0.3", default-features = false } +round-based = { version = "0.4", default-features = false } paillier-zk = "0.4.1" udigest = { version = "0.2.1", default-features = false } @@ -48,3 +48,7 @@ generic-tests = "0.1" [patch.crates-io.hd-wallet] git = "https://github.com/LFDT-Lockness/hd-wallet" branch = "dt" + +[patch.crates-io.round-based] +git = "https://github.com/LFDT-Lockness/round-based" +branch = "impr-sim" diff --git a/cggmp21/Cargo.toml b/cggmp21/Cargo.toml index 016bbd1b..5b9e99ea 100644 --- a/cggmp21/Cargo.toml +++ b/cggmp21/Cargo.toml @@ -38,7 +38,7 @@ hex = { workspace = true, default-features = false, features = ["serde"] } hd-wallet = { workspace = true, optional = true, features = ["std"] } [dev-dependencies] -round-based = { workspace = true, features = ["derive", "dev"] } +round-based = { workspace = true, features = ["derive", "sim"] } rand = { workspace = true } rand_dev = { workspace = true } diff --git a/cggmp21/src/signing.rs b/cggmp21/src/signing.rs index a8cce168..9f5d92bd 100644 --- a/cggmp21/src/signing.rs +++ b/cggmp21/src/signing.rs @@ -96,7 +96,7 @@ pub struct PartialSignature { } /// ECDSA signature -#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize, Debug)] #[serde(bound = "")] pub struct Signature { /// $r$ component of signature diff --git a/tests/Cargo.toml b/tests/Cargo.toml index c1768d21..3efa9bbe 100644 --- a/tests/Cargo.toml +++ b/tests/Cargo.toml @@ -19,14 +19,13 @@ rand_dev = { workspace = true } sha2 = { workspace = true } -round-based = { workspace = true, features = ["derive", "dev", "state-machine"] } +round-based = { workspace = true, features = ["derive", "sim", "state-machine"] } generic-ec = { workspace = true, features = ["serde", "all-curves"] } bpaf = "0.7" include_dir = "0.7" ciborium = "0.2" -tokio = { version = "1", features = ["macros"] } futures = "0.3" lazy_static = "1.4" diff --git a/tests/src/bin/measure_perf.rs b/tests/src/bin/measure_perf.rs index 337feb5f..494a5c4b 100644 --- a/tests/src/bin/measure_perf.rs +++ b/tests/src/bin/measure_perf.rs @@ -8,11 +8,9 @@ use cggmp21::{ }; use rand::Rng; use rand_dev::DevRng; -use round_based::simulation::Simulation; use sha2::Sha256; type E = generic_ec::curves::Secp256k1; -type D = sha2::Sha256; struct Args { n: Vec, @@ -55,17 +53,16 @@ fn args() -> Args { .to_options() .run() } -#[tokio::main(flavor = "current_thread")] -async fn main() { +fn main() { let args = args(); if args.custom_sec_level { - do_becnhmarks::(args).await + do_becnhmarks::(args) } else { - do_becnhmarks::(args).await + do_becnhmarks::(args) } } -async fn do_becnhmarks(args: Args) { +fn do_becnhmarks(args: Args) { let mut rng = DevRng::new(); for n in args.n { @@ -89,11 +86,7 @@ async fn do_becnhmarks(args: Args) { let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let mut simulation = - Simulation::>::new(); - - let outputs = (0..n).map(|i| { - let party = simulation.add_party(); + let outputs = round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); let mut profiler = PerfProfiler::new(); @@ -108,11 +101,10 @@ async fn do_becnhmarks(args: Args) { let report = profiler.get_report().context("get perf report")?; Ok::<_, anyhow::Error>((key_share, report)) } - }); - - let outputs = futures::future::try_join_all(outputs) - .await - .expect("non-threshold keygen failed"); + }) + .unwrap() + .expect_ok() + .into_vec(); if args.bench_non_threshold_keygen { println!("Non-threshold DKG"); @@ -132,13 +124,7 @@ async fn do_becnhmarks(args: Args) { let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let mut simulation = - Simulation::>::with_capacity( - (2 * n * n).into(), - ); - - let outputs = (0..n).map(|i| { - let party = simulation.add_party(); + let outputs = round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); let mut profiler = PerfProfiler::new(); @@ -154,11 +140,10 @@ async fn do_becnhmarks(args: Args) { let report = profiler.get_report().context("get perf report")?; Ok::<_, anyhow::Error>((key_share, report)) } - }); - - let outputs = futures::future::try_join_all(outputs) - .await - .expect("threshold keygen failed"); + }) + .unwrap() + .expect_ok() + .into_vec(); println!("Threshold DKG"); println!("{}", outputs[0].1.clone().display_io(false)); @@ -174,12 +159,9 @@ async fn do_becnhmarks(args: Args) { let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let mut simulation = Simulation::>::new(); - let mut primes = cggmp21_tests::CACHED_PRIMES.iter::(); - let outputs = (0..n).map(|i| { - let party = simulation.add_party(); + let outputs = round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); let pregen = primes.next().expect("Can't get pregenerated prime"); @@ -194,11 +176,10 @@ async fn do_becnhmarks(args: Args) { let report = profiler.get_report().context("get perf report")?; Ok::<_, anyhow::Error>((aux_data, report)) } - }); - - let outputs = futures::future::try_join_all(outputs) - .await - .expect("key refresh failed"); + }) + .unwrap() + .expect_ok() + .into_vec(); if args.bench_aux_data_gen { println!("Auxiliary data generation protocol"); @@ -261,29 +242,24 @@ async fn do_becnhmarks(args: Args) { let message_to_sign = b"Dfns rules!"; let message_to_sign = DataToSign::digest::(message_to_sign); - use cggmp21::signing::msg::Msg; - let mut simulation = Simulation::>::new(); - - let mut outputs = vec![]; - for (i, share) in (0..).zip(&shares) { - let party = simulation.add_party(); - let mut party_rng = rng.fork(); + let perf_reports = + round_based::simulation::run_with_setup(&shares, |i, party, share| { + let mut party_rng = rng.fork(); - let mut profiler = PerfProfiler::new(); + let mut profiler = PerfProfiler::new(); - outputs.push(async move { - let _signature = cggmp21::signing(eid, i, signers_indexes_at_keygen, share) - .set_progress_tracer(&mut profiler) - .sign(&mut party_rng, party, message_to_sign) - .await - .context("signing failed")?; - profiler.get_report().context("get perf report") + async move { + let _signature = cggmp21::signing(eid, i, signers_indexes_at_keygen, share) + .set_progress_tracer(&mut profiler) + .sign(&mut party_rng, party, message_to_sign) + .await + .context("signing failed")?; + profiler.get_report().context("get perf report") + } }) - } - - let perf_reports = futures::future::try_join_all(outputs) - .await - .expect("signing failed"); + .unwrap() + .expect_ok() + .into_vec(); println!("Signing protocol"); println!("{}", perf_reports[0].clone().display_io(false)); diff --git a/tests/tests/it/key_refresh.rs b/tests/tests/it/key_refresh.rs index d9fad605..46c5b30d 100644 --- a/tests/tests/it/key_refresh.rs +++ b/tests/tests/it/key_refresh.rs @@ -3,7 +3,6 @@ mod generic { use generic_ec::Point; use rand::seq::SliceRandom; use rand::Rng; - use round_based::simulation::Simulation; use sha2::Sha256; use cggmp21::{ @@ -15,8 +14,7 @@ mod generic { #[test_case::case(3, false; "n3")] #[test_case::case(5, false; "n5")] #[test_case::case(5, true; "n5-reliable")] - #[tokio::test] - async fn key_refresh_works(n: u16, reliable_broadcast: bool) + fn key_refresh_works(n: u16, reliable_broadcast: bool) where Point: generic_ec::coords::HasAffineX, { @@ -29,16 +27,14 @@ mod generic { #[cfg(feature = "hd-wallet")] assert!(shares[0].chain_code.is_some()); - let mut primes = cggmp21_tests::CACHED_PRIMES.iter(); + let mut primes = cggmp21_tests::CACHED_PRIMES.iter::(); // Perform refresh let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let mut simulation = - Simulation::>::new(); - let outputs = shares.iter().map(|share| { - let party = simulation.add_party(); + + let key_shares = round_based::simulation::run_with_setup(&shares, |_i, party, share| { let mut party_rng = rng.fork(); let pregenerated_data = primes.next().expect("Can't fetch primes"); async move { @@ -47,11 +43,10 @@ mod generic { .start(&mut party_rng, party) .await } - }); - - let key_shares = futures::future::try_join_all(outputs) - .await - .expect("keygen failed"); + }) + .unwrap() + .expect_ok() + .into_vec(); // validate key shares @@ -89,15 +84,12 @@ mod generic { // attempt to sign with new shares and verify the signature - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); let message_to_sign = cggmp21::signing::DataToSign::digest::(&[42; 100]); let participants = &(0..n).collect::>(); - let outputs = key_shares.iter().map(|share| { - let party = simulation.add_party(); + let sig = round_based::simulation::run_with_setup(&key_shares, |_i, party, share| { let mut party_rng = rng.fork(); async move { cggmp21::signing(eid, share.core.i, participants, share) @@ -105,23 +97,19 @@ mod generic { .sign(&mut party_rng, party, message_to_sign) .await } - }); - let signatures = futures::future::try_join_all(outputs) - .await - .expect("signing failed"); - - for signature in &signatures { - signature - .verify(&key_shares[0].core.shared_public_key, &message_to_sign) - .expect("signature is not valid"); - } + }) + .unwrap() + .expect_ok() + .expect_eq(); + + sig.verify(&key_shares[0].core.shared_public_key, &message_to_sign) + .expect("signature is not valid"); } #[test_case::case(2, 3, false; "t2n3")] #[test_case::case(3, 5, false; "t3n5")] #[test_case::case(3, 5, true; "t3n5-reliable")] - #[tokio::test] - async fn aux_gen_works(t: u16, n: u16, reliable_broadcast: bool) + fn aux_gen_works(t: u16, n: u16, reliable_broadcast: bool) where Point: generic_ec::coords::HasAffineX, { @@ -130,18 +118,14 @@ mod generic { let shares = cggmp21_tests::CACHED_SHARES .get_shares::(Some(t), n, false) .expect("retrieve cached shares"); - let mut primes = cggmp21_tests::CACHED_PRIMES.iter(); + let mut primes = cggmp21_tests::CACHED_PRIMES.iter::(); // Perform refresh - let mut simulation = - Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let outputs = (0..n).map(|i| { - let party = simulation.add_party(); + let aux_infos = round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); let pregenerated_data = primes.next().expect("Can't fetch primes"); async move { @@ -150,11 +134,10 @@ mod generic { .start(&mut party_rng, party) .await } - }); - - let aux_infos = futures::future::try_join_all(outputs) - .await - .expect("keygen failed"); + }) + .unwrap() + .expect_ok() + .into_vec(); // validate key shares @@ -173,8 +156,6 @@ mod generic { // attempt to sign with new shares and verify the signature - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); @@ -187,25 +168,22 @@ mod generic { println!("Signers: {participants:?}"); let participants_shares = participants.iter().map(|i| &key_shares[usize::from(*i)]); - let outputs = participants_shares.zip(0..).map(|(share, i)| { - let party = simulation.add_party(); - let mut party_rng = rng.fork(); - async move { - cggmp21::signing(eid, i, participants, share) - .enforce_reliable_broadcast(reliable_broadcast) - .sign(&mut party_rng, party, message_to_sign) - .await - } - }); - let signatures = futures::future::try_join_all(outputs) - .await - .expect("signing failed"); - - for signature in &signatures { - signature - .verify(&key_shares[0].core.shared_public_key, &message_to_sign) - .expect("signature is not valid"); - } + let sig = + round_based::simulation::run_with_setup(participants_shares, |i, party, share| { + let mut party_rng = rng.fork(); + async move { + cggmp21::signing(eid, i, participants, share) + .enforce_reliable_broadcast(reliable_broadcast) + .sign(&mut party_rng, party, message_to_sign) + .await + } + }) + .unwrap() + .expect_ok() + .expect_eq(); + + sig.verify(&key_shares[0].core.shared_public_key, &message_to_sign) + .expect("signature is not valid"); } #[instantiate_tests()] diff --git a/tests/tests/it/keygen.rs b/tests/tests/it/keygen.rs index d21c53ac..6ea83e14 100644 --- a/tests/tests/it/keygen.rs +++ b/tests/tests/it/keygen.rs @@ -5,13 +5,8 @@ mod generic { use generic_ec::{Curve, Point}; use rand::{seq::SliceRandom, Rng}; use rand_dev::DevRng; - use round_based::simulation::{Simulation, SimulationSync}; - use sha2::Sha256; - use cggmp21::keygen::{NonThresholdMsg, ThresholdMsg}; - use cggmp21::{ - key_share::reconstruct_secret_key, security_level::SecurityLevel128, ExecutionId, - }; + use cggmp21::{key_share::reconstruct_secret_key, ExecutionId}; #[test_case::case(3, false, false; "n3")] #[test_case::case(5, false, false; "n5")] @@ -22,37 +17,31 @@ mod generic { #[cfg_attr(feature = "hd-wallet", test_case::case(5, false, true; "n5-hd"))] #[cfg_attr(feature = "hd-wallet", test_case::case(7, false, true; "n7-hd"))] #[cfg_attr(feature = "hd-wallet", test_case::case(10, false, true; "n10-hd"))] - #[tokio::test] - async fn keygen_works(n: u16, reliable_broadcast: bool, hd_wallet: bool) { + fn keygen_works(n: u16, reliable_broadcast: bool, hd_wallet: bool) { #[cfg(not(feature = "hd-wallet"))] assert!(!hd_wallet); let mut rng = DevRng::new(); - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let mut outputs = vec![]; - for i in 0..n { - let party = simulation.add_party(); + let key_shares = round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); - outputs.push(async move { + async move { let keygen = - cggmp21::keygen(eid, i, n).enforce_reliable_broadcast(reliable_broadcast); + cggmp21::keygen::(eid, i, n).enforce_reliable_broadcast(reliable_broadcast); #[cfg(feature = "hd-wallet")] let keygen = keygen.hd_wallet(hd_wallet); keygen.start(&mut party_rng, party).await - }) - } - - let key_shares = futures::future::try_join_all(outputs) - .await - .expect("keygen failed"); + } + }) + .unwrap() + .expect_ok() + .into_vec(); validate_keygen_output(&mut rng, &key_shares, hd_wallet); } @@ -62,30 +51,20 @@ mod generic { #[test_case::case(3, 5, true, false; "t3n5-reliable")] #[cfg_attr(feature = "hd-wallet", test_case::case(2, 3, false, true; "t2n3-hd"))] #[cfg_attr(feature = "hd-wallet", test_case::case(3, 5, false, true; "t3n5-hd"))] - #[tokio::test] - async fn threshold_keygen_works( - t: u16, - n: u16, - reliable_broadcast: bool, - hd_wallet: bool, - ) { + fn threshold_keygen_works(t: u16, n: u16, reliable_broadcast: bool, hd_wallet: bool) { #[cfg(not(feature = "hd-wallet"))] assert!(!hd_wallet); let mut rng = DevRng::new(); - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let mut outputs = vec![]; - for i in 0..n { - let party = simulation.add_party(); + let key_shares = round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); - outputs.push(async move { - let keygen = cggmp21::keygen(eid, i, n) + async move { + let keygen = cggmp21::keygen::(eid, i, n) .enforce_reliable_broadcast(reliable_broadcast) .set_threshold(t); @@ -93,12 +72,11 @@ mod generic { let keygen = keygen.hd_wallet(hd_wallet); keygen.start(&mut party_rng, party).await - }) - } - - let key_shares = futures::future::try_join_all(outputs) - .await - .expect("keygen failed"); + } + }) + .unwrap() + .expect_ok() + .into_vec(); validate_keygen_output(&mut rng, &key_shares, hd_wallet); } @@ -118,7 +96,7 @@ mod generic { .take(n.into()) .collect::>(); - let mut simulation = SimulationSync::with_capacity(n); + let mut simulation = round_based::simulation::Simulation::with_capacity(n); for (i, party_rng) in (0..).zip(&mut party_rng) { simulation.add_party({ let keygen = cggmp21::keygen::(eid, i, n).set_threshold(t); @@ -132,6 +110,7 @@ mod generic { let key_shares = simulation .run() .unwrap() + .into_vec() .into_iter() .collect::, _>>() .unwrap(); diff --git a/tests/tests/it/pipeline.rs b/tests/tests/it/pipeline.rs index a50c7085..6a4396f1 100644 --- a/tests/tests/it/pipeline.rs +++ b/tests/tests/it/pipeline.rs @@ -3,32 +3,28 @@ mod generic { use generic_ec::{Curve, Point}; use rand::{seq::SliceRandom, Rng, RngCore}; use rand_dev::DevRng; - use round_based::simulation::Simulation; use sha2::Sha256; use cggmp21::{ key_share::{AnyKeyShare, IncompleteKeyShare, KeyShare}, - keygen::ThresholdMsg, - security_level::SecurityLevel128, ExecutionId, }; #[test_case::case(2, 3, false; "t2n3")] #[test_case::case(3, 5, false; "t3n5")] #[cfg_attr(feature = "hd-wallet", test_case::case(3, 5, true; "t3n5-hd"))] - #[tokio::test] - async fn full_pipeline_works(t: u16, n: u16, hd_enabled: bool) + fn full_pipeline_works(t: u16, n: u16, hd_enabled: bool) where E: Curve + cggmp21_tests::CurveParams, Point: generic_ec::coords::HasAffineX, { let mut rng = DevRng::new(); - let incomplete_shares = run_keygen(t, n, hd_enabled, &mut rng).await; - let shares = run_aux_gen(incomplete_shares, &mut rng).await; - run_signing(&shares, hd_enabled, &mut rng).await; + let incomplete_shares = run_keygen(t, n, hd_enabled, &mut rng); + let shares = run_aux_gen(incomplete_shares, &mut rng); + run_signing(&shares, hd_enabled, &mut rng); } - async fn run_keygen( + fn run_keygen( t: u16, n: u16, hd_enabled: bool, @@ -40,49 +36,37 @@ mod generic { #[cfg(not(feature = "hd-wallet"))] assert!(!hd_enabled); - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let mut outputs = vec![]; - for i in 0..n { - let party = simulation.add_party(); + round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); - outputs.push(async move { + async move { let keygen = cggmp21::keygen(eid, i, n).set_threshold(t); #[cfg(feature = "hd-wallet")] let keygen = keygen.hd_wallet(hd_enabled); keygen.start(&mut party_rng, party).await - }) - } - - futures::future::try_join_all(outputs) - .await - .expect("keygen failed") + } + }) + .unwrap() + .expect_ok() + .into_vec() } - async fn run_aux_gen( - shares: Vec>, - rng: &mut DevRng, - ) -> Vec> + fn run_aux_gen(shares: Vec>, rng: &mut DevRng) -> Vec> where E: Curve, { let mut primes = cggmp21_tests::CACHED_PRIMES.iter(); let n = shares.len().try_into().unwrap(); - let mut simulation = - Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); - let outputs = (0..n).map(|i| { - let party = simulation.add_party(); + let aux_infos = round_based::simulation::run(n, |i, party| { let mut party_rng = rng.fork(); let pregenerated_data = primes.next().expect("Can't fetch primes"); async move { @@ -90,11 +74,10 @@ mod generic { .start(&mut party_rng, party) .await } - }); - - let aux_infos = futures::future::try_join_all(outputs) - .await - .expect("keygen failed"); + }) + .unwrap() + .expect_ok() + .into_vec(); shares .into_iter() @@ -105,7 +88,7 @@ mod generic { .collect() } - async fn run_signing(shares: &[KeyShare], random_derivation_path: bool, rng: &mut DevRng) + fn run_signing(shares: &[KeyShare], random_derivation_path: bool, rng: &mut DevRng) where E: Curve + cggmp21_tests::CurveParams, Point: generic_ec::coords::HasAffineX, @@ -123,8 +106,6 @@ mod generic { None }; - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); @@ -140,33 +121,31 @@ mod generic { println!("Signers: {participants:?}"); let participants_shares = participants.iter().map(|i| &shares[usize::from(*i)]); - let mut outputs = vec![]; - for (i, share) in (0..).zip(participants_shares) { - let party = simulation.add_party(); - let mut party_rng = rng.fork(); - - #[cfg(feature = "hd-wallet")] - let derivation_path = derivation_path.clone(); - - outputs.push(async move { - let signing = cggmp21::signing(eid, i, participants, share); + let sig = + round_based::simulation::run_with_setup(participants_shares, |i, party, share| { + let mut party_rng = rng.fork(); #[cfg(feature = "hd-wallet")] - let signing = if let Some(derivation_path) = derivation_path { - signing - .set_derivation_path_with_algo::(derivation_path) - .unwrap() - } else { - signing - }; - - signing.sign(&mut party_rng, party, message_to_sign).await - }); - } - - let signatures = futures::future::try_join_all(outputs) - .await - .expect("signing failed"); + let derivation_path = derivation_path.clone(); + + async move { + let signing = cggmp21::signing(eid, i, participants, share); + + #[cfg(feature = "hd-wallet")] + let signing = if let Some(derivation_path) = derivation_path { + signing + .set_derivation_path_with_algo::(derivation_path) + .unwrap() + } else { + signing + }; + + signing.sign(&mut party_rng, party, message_to_sign).await + } + }) + .unwrap() + .expect_ok() + .expect_eq(); #[cfg(feature = "hd-wallet")] let public_key = if let Some(path) = &derivation_path { @@ -183,11 +162,8 @@ mod generic { #[cfg(not(feature = "hd-wallet"))] let public_key = shares[0].shared_public_key; - signatures[0] - .verify(&public_key, &message_to_sign) + sig.verify(&public_key, &message_to_sign) .expect("signature is not valid"); - - assert!(signatures.iter().all(|s_i| signatures[0] == *s_i)); } #[instantiate_tests()] diff --git a/tests/tests/it/signing.rs b/tests/tests/it/signing.rs index 0a6af3b1..54142e3d 100644 --- a/tests/tests/it/signing.rs +++ b/tests/tests/it/signing.rs @@ -5,15 +5,14 @@ use generic_ec::{coords::HasAffineX, Curve, Point}; use rand::seq::SliceRandom; use rand::{Rng, RngCore}; use rand_dev::DevRng; -use round_based::simulation::{Simulation, SimulationSync}; use sha2::Sha256; use cggmp21::key_share::AnyKeyShare; -use cggmp21::signing::{msg::Msg, DataToSign}; +use cggmp21::signing::DataToSign; use cggmp21::{security_level::SecurityLevel128, ExecutionId}; cggmp21_tests::test_suite! { - async_test = signing_works, + test = signing_works, generics = all_curves, suites = [ n2(None, 2, false, false), @@ -32,7 +31,7 @@ cggmp21_tests::test_suite! { ] } -async fn signing_works(t: Option, n: u16, reliable_broadcast: bool, hd_wallet: bool) +fn signing_works(t: Option, n: u16, reliable_broadcast: bool, hd_wallet: bool) where E: Curve + cggmp21_tests::CurveParams, Point: HasAffineX, @@ -46,8 +45,6 @@ where .get_shares::(t, n, hd_wallet) .expect("retrieve cached shares"); - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); @@ -70,34 +67,26 @@ where println!("Signers: {participants:?}"); let participants_shares = participants.iter().map(|i| &shares[usize::from(*i)]); - let mut outputs = vec![]; - for (i, share) in (0..).zip(participants_shares) { - let party = simulation.add_party(); + let sig = round_based::simulation::run_with_setup(participants_shares, |i, party, share| { let mut party_rng = rng.fork(); - #[cfg(feature = "hd-wallet")] - let derivation_path = derivation_path.clone(); - - outputs.push(async move { - let signing = cggmp21::signing(eid, i, participants, share) - .enforce_reliable_broadcast(reliable_broadcast); + let signing = cggmp21::signing(eid, i, participants, share) + .enforce_reliable_broadcast(reliable_broadcast); - #[cfg(feature = "hd-wallet")] - let signing = if let Some(derivation_path) = derivation_path { - signing - .set_derivation_path_with_algo::(derivation_path) - .unwrap() - } else { - signing - }; - - signing.sign(&mut party_rng, party, message_to_sign).await - }); - } + #[cfg(feature = "hd-wallet")] + let signing = if let Some(derivation_path) = derivation_path.clone() { + signing + .set_derivation_path_with_algo::(derivation_path) + .unwrap() + } else { + signing + }; - let signatures = futures::future::try_join_all(outputs) - .await - .expect("signing failed"); + async move { signing.sign(&mut party_rng, party, message_to_sign).await } + }) + .unwrap() + .expect_ok() + .expect_eq(); #[cfg(feature = "hd-wallet")] let public_key = if let Some(path) = &derivation_path { @@ -114,18 +103,15 @@ where #[cfg(not(feature = "hd-wallet"))] let public_key = shares[0].shared_public_key; - signatures[0] - .verify(&public_key, &message_to_sign) + sig.verify(&public_key, &message_to_sign) .expect("signature is not valid"); - assert!(signatures.iter().all(|s_i| signatures[0] == *s_i)); - - E::ExVerifier::verify(&public_key, &signatures[0], &original_message_to_sign) + E::ExVerifier::verify(&public_key, &sig, &original_message_to_sign) .expect("external verification failed") } cggmp21_tests::test_suite! { - async_test = signing_with_presigs, + test = signing_with_presigs, generics = all_curves, suites = [ t3n5(Some(3), 5, false), @@ -134,7 +120,7 @@ cggmp21_tests::test_suite! { ] } -async fn signing_with_presigs(t: Option, n: u16, hd_wallet: bool) +fn signing_with_presigs(t: Option, n: u16, hd_wallet: bool) where E: Curve + cggmp21_tests::CurveParams, Point: HasAffineX, @@ -148,8 +134,6 @@ where .get_shares::(t, n, hd_wallet) .expect("retrieve cached shares"); - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = ExecutionId::new(&eid); @@ -162,21 +146,19 @@ where let participants_shares = participants.iter().map(|i| &shares[usize::from(*i)]); - let mut outputs = vec![]; - for (i, share) in (0..).zip(participants_shares) { - let party = simulation.add_party(); - let mut party_rng = rng.fork(); - - outputs.push(async move { - cggmp21::signing(eid, i, participants, share) - .generate_presignature(&mut party_rng, party) - .await - }); - } + let presigs = + round_based::simulation::run_with_setup(participants_shares, |i, party, share| { + let mut party_rng = rng.fork(); - let presignatures = futures::future::try_join_all(outputs) - .await - .expect("signing failed"); + async move { + cggmp21::signing(eid, i, participants, share) + .generate_presignature(&mut party_rng, party) + .await + } + }) + .unwrap() + .expect_ok() + .into_vec(); // Now, that we have presignatures generated, we learn (generate) a messages to sign // and the derivation path (if hd is enabled) @@ -191,7 +173,7 @@ where None }; - let partial_signatures = presignatures + let partial_signatures = presigs .into_iter() .map(|presig| { #[cfg(feature = "hd-wallet")] @@ -289,7 +271,7 @@ where .take(n.into()) .collect::>(); - let mut simulation = SimulationSync::with_capacity(n); + let mut simulation = round_based::simulation::Simulation::with_capacity(n); for ((i, share), signer_rng) in (0..).zip(participants_shares).zip(&mut signer_rng) { simulation.add_party({ @@ -308,12 +290,7 @@ where }) } - let signatures = simulation - .run() - .unwrap() - .into_iter() - .collect::, _>>() - .unwrap(); + let sig = simulation.run().unwrap().expect_ok().expect_eq(); #[cfg(feature = "hd-wallet")] let public_key = if let Some(path) = &derivation_path { @@ -330,12 +307,9 @@ where #[cfg(not(feature = "hd-wallet"))] let public_key = shares[0].shared_public_key; - signatures[0] - .verify(&public_key, &message_to_sign) + sig.verify(&public_key, &message_to_sign) .expect("signature is not valid"); - assert!(signatures.iter().all(|s_i| signatures[0] == *s_i)); - - E::ExVerifier::verify(&public_key, &signatures[0], &original_message_to_sign) + E::ExVerifier::verify(&public_key, &sig, &original_message_to_sign) .expect("external verification failed") } diff --git a/tests/tests/it/stark_prehashed.rs b/tests/tests/it/stark_prehashed.rs index 410a0e22..dcc3aab6 100644 --- a/tests/tests/it/stark_prehashed.rs +++ b/tests/tests/it/stark_prehashed.rs @@ -1,13 +1,11 @@ -use cggmp21::{key_share::AnyKeyShare, security_level::SecurityLevel128, signing::msg::Msg}; +use cggmp21::{key_share::AnyKeyShare, security_level::SecurityLevel128}; use cggmp21_tests::{convert_from_stark_scalar, convert_stark_scalar}; use generic_ec::{coords::HasAffineX, curves::Stark}; use rand::{seq::SliceRandom, Rng}; use rand_dev::DevRng; -use round_based::simulation::Simulation; -use sha2::Sha256; -#[tokio::test] -async fn sign_transaction() { +#[test] +fn sign_transaction() { let mut rng = DevRng::new(); let t = Some(2); let n = 3; @@ -16,8 +14,6 @@ async fn sign_transaction() { .get_shares::(t, n, false) .expect("retrieve cached shares"); - let mut simulation = Simulation::>::new(); - let eid: [u8; 32] = rng.gen(); let eid = cggmp21::ExecutionId::new(&eid); @@ -74,33 +70,28 @@ async fn sign_transaction() { println!("Signers: {participants:?}"); let participants_shares = participants.iter().map(|i| &shares[usize::from(*i)]); - let mut outputs = vec![]; - for (i, share) in (0..).zip(participants_shares) { - let party = simulation.add_party(); + let sig = round_based::simulation::run_with_setup(participants_shares, |i, party, share| { let mut party_rng = rng.fork(); - outputs.push(async move { + async move { cggmp21::signing(eid, i, participants, share) .sign(&mut party_rng, party, cggmp_transaction_hash) .await - }); - } - - let signatures = futures::future::try_join_all(outputs) - .await - .expect("signing failed"); + } + }) + .unwrap() + .expect_ok() + .expect_eq(); // verify with our lib - signatures[0] - .verify(&shares[0].core.shared_public_key, &cggmp_transaction_hash) + sig.verify(&shares[0].core.shared_public_key, &cggmp_transaction_hash) .expect("signature is not valid"); - assert!(signatures.iter().all(|s_i| signatures[0] == *s_i)); // verify with starknet lib let public_key_x = shares[0].core.shared_public_key.x().unwrap().to_scalar(); let public_key = convert_stark_scalar(&public_key_x).unwrap(); - let r = convert_stark_scalar(&signatures[0].r).unwrap(); - let s = convert_stark_scalar(&signatures[0].s).unwrap(); + let r = convert_stark_scalar(&sig.r).unwrap(); + let s = convert_stark_scalar(&sig.s).unwrap(); let r = starknet_crypto::verify(&public_key, &transaction_hash, &r, &s).unwrap(); assert!(r, "failed to verify signature");