-
Notifications
You must be signed in to change notification settings - Fork 22
perf: DKG benchmarks implementations #2623
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
a6442a9
52b0f4f
f6ca176
f12512a
814da75
007ea06
99c5be1
54c185e
67bb740
fb618b8
9bcc6cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,150 @@ | ||
| #![allow(clippy::indexing_slicing)] | ||
|
|
||
| use criterion::{criterion_group, criterion_main, Criterion}; | ||
| use rand::seq::SliceRandom as _; | ||
| use rand_core::SeedableRng; | ||
|
|
||
| mod bench_utils; | ||
| use crate::bench_utils::{ | ||
| analyze_received_sizes, prepare_dkg, PreparedOutputs, MAX_MALICIOUS, SAMPLE_SIZE, | ||
| }; | ||
|
|
||
| use threshold_signatures::{ | ||
| confidential_key_derivation::ciphersuite::BLS12381SHA256, | ||
| frost_ed25519::Ed25519Sha512, | ||
| frost_secp256k1::Secp256K1Sha256, | ||
| keygen, | ||
| protocol::Protocol, | ||
| test_utils::{ | ||
| run_protocol_and_take_snapshots, run_simulated_protocol, MockCryptoRng, Simulator, | ||
| }, | ||
| Ciphersuite, Element, KeygenOutput, ReconstructionLowerBound, Scalar, | ||
| }; | ||
|
|
||
| fn threshold() -> ReconstructionLowerBound { | ||
| ReconstructionLowerBound::from(*MAX_MALICIOUS + 1) | ||
| } | ||
|
|
||
| fn participants_num() -> usize { | ||
| *MAX_MALICIOUS + 1 | ||
| } | ||
|
|
||
|
Comment on lines
+24
to
+31
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Hm I think it would be nice to make these lazy loaded constants like
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. same here |
||
| type PreparedSimulatedDkg<C> = PreparedOutputs<KeygenOutput<C>>; | ||
|
|
||
| /// Benches the DKG protocol for Secp256k1 | ||
| fn bench_dkg_secp256k1(c: &mut Criterion) { | ||
| let num = participants_num(); | ||
| let max_malicious = *MAX_MALICIOUS; | ||
| let mut sizes = Vec::with_capacity(*SAMPLE_SIZE); | ||
|
|
||
| let mut group = c.benchmark_group("dkg"); | ||
| group.sample_size(*SAMPLE_SIZE); | ||
| group.bench_function( | ||
| format!("dkg_secp256k1_MAX_MALICIOUS_{max_malicious}_PARTICIPANTS_{num}"), | ||
| |b| { | ||
| b.iter_batched( | ||
| || { | ||
| let preps = prepare_simulated_dkg::<Secp256K1Sha256>(threshold()); | ||
| sizes.push(preps.simulator.get_view_size()); | ||
| preps | ||
| }, | ||
| |preps| run_simulated_protocol(preps.participant, preps.protocol, preps.simulator), | ||
| criterion::BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
| analyze_received_sizes(&sizes, true); | ||
|
Comment on lines
+35
to
+56
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we could reuse code for all 3 implementations |
||
| } | ||
|
|
||
| /// Benches the DKG protocol for Ed25519 | ||
| fn bench_dkg_ed25519(c: &mut Criterion) { | ||
| let num = participants_num(); | ||
| let max_malicious = *MAX_MALICIOUS; | ||
| let mut sizes = Vec::with_capacity(*SAMPLE_SIZE); | ||
|
|
||
| let mut group = c.benchmark_group("dkg"); | ||
| group.sample_size(*SAMPLE_SIZE); | ||
| group.bench_function( | ||
| format!("dkg_ed25519_MAX_MALICIOUS_{max_malicious}_PARTICIPANTS_{num}"), | ||
| |b| { | ||
| b.iter_batched( | ||
| || { | ||
| let preps = prepare_simulated_dkg::<Ed25519Sha512>(threshold()); | ||
| sizes.push(preps.simulator.get_view_size()); | ||
| preps | ||
| }, | ||
| |preps| run_simulated_protocol(preps.participant, preps.protocol, preps.simulator), | ||
| criterion::BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
| analyze_received_sizes(&sizes, true); | ||
| } | ||
|
|
||
| /// Benches the DKG protocol for BLS12-381 | ||
| fn bench_dkg_bls12381(c: &mut Criterion) { | ||
| let num = participants_num(); | ||
| let max_malicious = *MAX_MALICIOUS; | ||
| let mut sizes = Vec::with_capacity(*SAMPLE_SIZE); | ||
|
|
||
| let mut group = c.benchmark_group("dkg"); | ||
| group.sample_size(*SAMPLE_SIZE); | ||
| group.bench_function( | ||
| format!("dkg_bls12381_MAX_MALICIOUS_{max_malicious}_PARTICIPANTS_{num}"), | ||
| |b| { | ||
| b.iter_batched( | ||
| || { | ||
| let preps = prepare_simulated_dkg::<BLS12381SHA256>(threshold()); | ||
| sizes.push(preps.simulator.get_view_size()); | ||
| preps | ||
| }, | ||
| |preps| run_simulated_protocol(preps.participant, preps.protocol, preps.simulator), | ||
| criterion::BatchSize::SmallInput, | ||
| ); | ||
| }, | ||
| ); | ||
| analyze_received_sizes(&sizes, true); | ||
| } | ||
|
|
||
| criterion_group!( | ||
| benches, | ||
| bench_dkg_secp256k1, | ||
| bench_dkg_ed25519, | ||
| bench_dkg_bls12381 | ||
| ); | ||
| criterion_main!(benches); | ||
|
|
||
| /****************************** Helpers ******************************/ | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not a fan of these comments. I find it nicer to create sub modules if there's a natural division of the code
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I will create a new issue for this. It's gonna make things simpler to review. |
||
| /// Used to simulate DKG keygen for benchmarking | ||
| fn prepare_simulated_dkg<C: Ciphersuite>( | ||
| threshold: ReconstructionLowerBound, | ||
| ) -> PreparedSimulatedDkg<C> | ||
| where | ||
| Element<C>: Send, | ||
| Scalar<C>: Send, | ||
| { | ||
| let mut rng = MockCryptoRng::seed_from_u64(42); | ||
| let preps = prepare_dkg::<C, _>(participants_num(), threshold, &mut rng); | ||
| let participants: Vec<_> = preps.iter().map(|(p, _)| *p).collect(); | ||
| let (_, protocol_snapshot) = run_protocol_and_take_snapshots(preps) | ||
| .expect("Running protocol with snapshot should not have issues"); | ||
|
|
||
| // choose the real_participant at random | ||
| let real_participant = *participants | ||
| .choose(&mut rng) | ||
| .expect("participant list is not empty"); | ||
|
|
||
| let real_protocol = keygen::<C>(&participants, real_participant, threshold, rng) | ||
| .map(|p| Box::new(p) as Box<dyn Protocol<Output = KeygenOutput<C>>>) | ||
| .expect("Keygen should succeed"); | ||
|
|
||
| // now preparing the simulator | ||
| let simulated_protocol = | ||
| Simulator::new(real_participant, protocol_snapshot).expect("Simulator should not be empty"); | ||
|
|
||
| PreparedSimulatedDkg { | ||
| participant: real_participant, | ||
| protocol: real_protocol, | ||
| simulator: simulated_protocol, | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, fine for benches
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used though?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I couldn't find it, but the clippy has the last word