From 56da41db4ff2c26c7fc8feafedf030b185b9aad2 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 18 Oct 2024 17:04:42 +0300 Subject: [PATCH 01/10] add vote protocol benchmarks --- rust/catalyst-voting/Cargo.toml | 7 +- rust/catalyst-voting/benches/vote_protocol.rs | 125 ++++++++++++++++++ rust/catalyst-voting/tests/voting_test.rs | 6 +- 3 files changed, 134 insertions(+), 4 deletions(-) create mode 100644 rust/catalyst-voting/benches/vote_protocol.rs diff --git a/rust/catalyst-voting/Cargo.toml b/rust/catalyst-voting/Cargo.toml index d45d6b5be8..ec47dcdd3b 100644 --- a/rust/catalyst-voting/Cargo.toml +++ b/rust/catalyst-voting/Cargo.toml @@ -12,6 +12,10 @@ license.workspace = true [lints] workspace = true +[[bench]] +name = "vote_protocol" +harness = false + [dependencies] anyhow = "1.0.89" rand_core = { version = "0.6.4", features = ["getrandom"] } @@ -21,7 +25,8 @@ ed25519-dalek = { version = "2.1.1", features = ["rand_core"] } blake2b_simd = "1.0.2" [dev-dependencies] -proptest = {version = "1.5.0" } +criterion = "0.5.1" +proptest = { version = "1.5.0" } # Potentially it could be replaced with using `proptest::property_test` attribute macro, # after this PR will be merged https://github.com/proptest-rs/proptest/pull/523 test-strategy = "0.4.0" diff --git a/rust/catalyst-voting/benches/vote_protocol.rs b/rust/catalyst-voting/benches/vote_protocol.rs new file mode 100644 index 0000000000..a9090691f0 --- /dev/null +++ b/rust/catalyst-voting/benches/vote_protocol.rs @@ -0,0 +1,125 @@ +//! `catalyst_voting::vote_protocol` benchmark +#![allow( + missing_docs, + clippy::missing_docs_in_private_items, + clippy::unwrap_used, + clippy::similar_names +)] + +use catalyst_voting::vote_protocol::{ + committee::{ElectionPublicKey, ElectionSecretKey}, + voter::{ + encrypt_vote_with_default_rng, + proof::{generate_voter_proof_with_default_rng, verify_voter_proof, VoterProofCommitment}, + Vote, + }, +}; +use criterion::{criterion_group, criterion_main, Criterion}; +use proptest::{ + prelude::{any, Strategy}, + strategy::ValueTree, + test_runner::TestRunner, +}; +use test_strategy::Arbitrary; + +const VOTING_OPTIONS: usize = 3; +const VOTERS_NUMBER: usize = 100; + +#[derive(Arbitrary, Debug)] +struct Voter { + _voting_power: u32, + #[strategy(0..VOTING_OPTIONS)] + choice: usize, +} + +fn initial_setup() -> ( + [Voter; VOTERS_NUMBER], + ElectionSecretKey, + ElectionPublicKey, + VoterProofCommitment, +) { + let mut runner = TestRunner::default(); + + let voters = any::<[Voter; VOTERS_NUMBER]>() + .new_tree(&mut runner) + .unwrap() + .current(); + + let election_secret_key = ElectionSecretKey::random_with_default_rng(); + let voter_proof_commitment = VoterProofCommitment::random_with_default_rng(); + let election_public_key = election_secret_key.public_key(); + + ( + voters, + election_secret_key, + election_public_key, + voter_proof_commitment, + ) +} + +fn vote_protocol_benches(c: &mut Criterion) { + let (voters, _election_secret_key, election_public_key, voter_proof_commitment) = + initial_setup(); + + let votes: Vec<_> = voters + .iter() + .map(|voter| Vote::new(voter.choice, VOTING_OPTIONS).unwrap()) + .collect(); + + let mut group = c.benchmark_group("vote protocol benchmark"); + + let mut encrypted_votes = Vec::new(); + let mut randomness = Vec::new(); + group.bench_function("vote encryption", |b| { + b.iter(|| { + (encrypted_votes, randomness) = votes + .iter() + .map(|vote| encrypt_vote_with_default_rng(vote, &election_public_key)) + .unzip(); + }); + }); + + let mut voter_proofs = Vec::new(); + group.bench_function("voter proof generation", |b| { + b.iter(|| { + voter_proofs = votes + .iter() + .zip(encrypted_votes.iter()) + .zip(randomness.iter()) + .map(|((v, enc_v), r)| { + generate_voter_proof_with_default_rng( + v, + enc_v.clone(), + r.clone(), + &election_public_key, + &voter_proof_commitment, + ) + .unwrap() + }) + .collect(); + }); + }); + + group.bench_function("voter proof verification", |b| { + b.iter(|| { + let is_ok = voter_proofs + .iter() + .zip(encrypted_votes.iter()) + .all(|(p, enc_v)| { + verify_voter_proof( + enc_v.clone(), + &election_public_key, + &voter_proof_commitment, + p, + ) + }); + assert!(is_ok); + }); + }); + + group.finish(); +} + +criterion_group!(benches, vote_protocol_benches); + +criterion_main!(benches); diff --git a/rust/catalyst-voting/tests/voting_test.rs b/rust/catalyst-voting/tests/voting_test.rs index e4b4242dd5..1e699bd4b8 100644 --- a/rust/catalyst-voting/tests/voting_test.rs +++ b/rust/catalyst-voting/tests/voting_test.rs @@ -17,17 +17,17 @@ use proptest::prelude::ProptestConfig; use test_strategy::{proptest, Arbitrary}; const VOTING_OPTIONS: usize = 3; +const VOTERS_NUMBER: usize = 100; #[derive(Arbitrary, Debug)] struct Voter { voting_power: u32, - // range from 0 to `VOTING_OPTIONS` - #[strategy(0..3_usize)] + #[strategy(0..VOTING_OPTIONS)] choice: usize, } #[proptest(ProptestConfig::with_cases(1))] -fn voting_test(voters: [Voter; 100]) { +fn voting_test(voters: [Voter; VOTERS_NUMBER]) { let election_secret_key = ElectionSecretKey::random_with_default_rng(); let election_public_key = election_secret_key.public_key(); let voter_proof_commitment = VoterProofCommitment::random_with_default_rng(); From ad5ec99b1947fa42746a6b3fdad210389d3c2f7e Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 18 Oct 2024 18:07:52 +0300 Subject: [PATCH 02/10] add missing benchmarks --- rust/catalyst-voting/benches/vote_protocol.rs | 106 +++++++++++++++--- 1 file changed, 88 insertions(+), 18 deletions(-) diff --git a/rust/catalyst-voting/benches/vote_protocol.rs b/rust/catalyst-voting/benches/vote_protocol.rs index a9090691f0..3028ed2974 100644 --- a/rust/catalyst-voting/benches/vote_protocol.rs +++ b/rust/catalyst-voting/benches/vote_protocol.rs @@ -6,12 +6,20 @@ clippy::similar_names )] -use catalyst_voting::vote_protocol::{ - committee::{ElectionPublicKey, ElectionSecretKey}, - voter::{ - encrypt_vote_with_default_rng, - proof::{generate_voter_proof_with_default_rng, verify_voter_proof, VoterProofCommitment}, - Vote, +use catalyst_voting::{ + crypto::default_rng, + vote_protocol::{ + committee::{ElectionPublicKey, ElectionSecretKey}, + tally::{ + decrypt_tally, + proof::{generate_tally_proof, verify_tally_proof}, + tally, DecryptionTallySetup, + }, + voter::{ + encrypt_vote, + proof::{generate_voter_proof, verify_voter_proof, VoterProofCommitment}, + Vote, + }, }, }; use criterion::{criterion_group, criterion_main, Criterion}; @@ -27,20 +35,27 @@ const VOTERS_NUMBER: usize = 100; #[derive(Arbitrary, Debug)] struct Voter { - _voting_power: u32, + voting_power: u32, #[strategy(0..VOTING_OPTIONS)] choice: usize, } fn initial_setup() -> ( - [Voter; VOTERS_NUMBER], + Vec, + Vec, ElectionSecretKey, ElectionPublicKey, VoterProofCommitment, ) { let mut runner = TestRunner::default(); - let voters = any::<[Voter; VOTERS_NUMBER]>() + let (choices, voting_powers) = any::<[Voter; VOTERS_NUMBER]>() + .prop_map(|voter| { + ( + voter.iter().map(|v| v.choice).collect(), + voter.iter().map(|v| v.voting_power.into()).collect(), + ) + }) .new_tree(&mut runner) .unwrap() .current(); @@ -50,23 +65,26 @@ fn initial_setup() -> ( let election_public_key = election_secret_key.public_key(); ( - voters, + choices, + voting_powers, election_secret_key, election_public_key, voter_proof_commitment, ) } +#[allow(clippy::too_many_lines)] fn vote_protocol_benches(c: &mut Criterion) { - let (voters, _election_secret_key, election_public_key, voter_proof_commitment) = - initial_setup(); + let mut group = c.benchmark_group("vote protocol benchmark"); + group.sample_size(10); - let votes: Vec<_> = voters + let (choices, voting_powers, election_secret_key, election_public_key, voter_proof_commitment) = + initial_setup(); + let votes: Vec<_> = choices .iter() - .map(|voter| Vote::new(voter.choice, VOTING_OPTIONS).unwrap()) + .map(|choice| Vote::new(*choice, VOTING_OPTIONS).unwrap()) .collect(); - - let mut group = c.benchmark_group("vote protocol benchmark"); + let mut rng = default_rng(); let mut encrypted_votes = Vec::new(); let mut randomness = Vec::new(); @@ -74,7 +92,7 @@ fn vote_protocol_benches(c: &mut Criterion) { b.iter(|| { (encrypted_votes, randomness) = votes .iter() - .map(|vote| encrypt_vote_with_default_rng(vote, &election_public_key)) + .map(|vote| encrypt_vote(vote, &election_public_key, &mut rng)) .unzip(); }); }); @@ -87,12 +105,13 @@ fn vote_protocol_benches(c: &mut Criterion) { .zip(encrypted_votes.iter()) .zip(randomness.iter()) .map(|((v, enc_v), r)| { - generate_voter_proof_with_default_rng( + generate_voter_proof( v, enc_v.clone(), r.clone(), &election_public_key, &voter_proof_commitment, + &mut rng, ) .unwrap() }) @@ -117,6 +136,57 @@ fn vote_protocol_benches(c: &mut Criterion) { }); }); + let mut encrypted_tallies = Vec::new(); + group.bench_function("tally", |b| { + b.iter(|| { + encrypted_tallies = (0..VOTING_OPTIONS) + .map(|voting_option| { + tally(voting_option, &encrypted_votes, &voting_powers).unwrap() + }) + .collect(); + }); + }); + + let total_voting_power = voting_powers.iter().sum(); + let mut decryption_tally_setup = None; + group.bench_function("decryption tally setup initialization", |b| { + b.iter(|| { + decryption_tally_setup = Some(DecryptionTallySetup::new(total_voting_power).unwrap()); + }); + }); + let decryption_tally_setup = decryption_tally_setup.unwrap(); + + let mut decrypted_tallies = Vec::new(); + group.bench_function("decrypt tally", |b| { + b.iter(|| { + decrypted_tallies = encrypted_tallies + .iter() + .map(|t| decrypt_tally(t, &election_secret_key, &decryption_tally_setup).unwrap()) + .collect(); + }); + }); + + let mut tally_proofs = Vec::new(); + group.bench_function("tally proof generation", |b| { + b.iter(|| { + tally_proofs = encrypted_tallies + .iter() + .map(|t| generate_tally_proof(t, &election_secret_key, &mut rng)) + .collect(); + }); + }); + + group.bench_function("tally proof verification", |b| { + b.iter(|| { + let is_ok = tally_proofs + .iter() + .zip(encrypted_tallies.iter()) + .zip(decrypted_tallies.iter()) + .all(|((p, enc_t), t)| verify_tally_proof(enc_t, *t, &election_public_key, p)); + assert!(is_ok); + }); + }); + group.finish(); } From 908b1a40a33671dad3a27cb69102c91c189ce041 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Sat, 19 Oct 2024 20:25:43 +0300 Subject: [PATCH 03/10] refactor --- .../src/crypto/zk_unit_vector/decoding.rs | 24 ++++++++--- rust/catalyst-voting/src/txs/v1/decoding.rs | 21 ++++++---- .../src/vote_protocol/tally/mod.rs | 40 +++++++++++++------ 3 files changed, 60 insertions(+), 25 deletions(-) diff --git a/rust/catalyst-voting/src/crypto/zk_unit_vector/decoding.rs b/rust/catalyst-voting/src/crypto/zk_unit_vector/decoding.rs index a6faaf50a5..45dacd1c9b 100644 --- a/rust/catalyst-voting/src/crypto/zk_unit_vector/decoding.rs +++ b/rust/catalyst-voting/src/crypto/zk_unit_vector/decoding.rs @@ -27,22 +27,34 @@ impl UnitVectorProof { let ann = (0..len) .map(|i| { let bytes = read_array(reader)?; - Announcement::from_bytes(&bytes) - .map_err(|e| anyhow!("Cannot decode announcement at {i}, error: {e}.")) + Announcement::from_bytes(&bytes).map_err(|e| { + anyhow!( + "Cannot decode announcement at {i}, \ + error: {e}." + ) + }) }) .collect::>()?; let dl = (0..len) .map(|i| { let bytes = read_array(reader)?; - Ciphertext::from_bytes(&bytes) - .map_err(|e| anyhow!("Cannot decode ciphertext at {i}, error: {e}.")) + Ciphertext::from_bytes(&bytes).map_err(|e| { + anyhow!( + "Cannot decode ciphertext at {i}, \ + error: {e}." + ) + }) }) .collect::>()?; let rr = (0..len) .map(|i| { let bytes = read_array(reader)?; - ResponseRandomness::from_bytes(&bytes) - .map_err(|e| anyhow!("Cannot decode response randomness at {i}, error: {e}.")) + ResponseRandomness::from_bytes(&bytes).map_err(|e| { + anyhow!( + "Cannot decode response randomness at {i}, \ + error: {e}." + ) + }) }) .collect::>()?; diff --git a/rust/catalyst-voting/src/txs/v1/decoding.rs b/rust/catalyst-voting/src/txs/v1/decoding.rs index b6fe7ef594..58e56c517a 100644 --- a/rust/catalyst-voting/src/txs/v1/decoding.rs +++ b/rust/catalyst-voting/src/txs/v1/decoding.rs @@ -112,14 +112,16 @@ impl Tx { let padding_tag = read_be_u8(reader).map_err(|_| anyhow!("Missing padding tag field."))?; ensure!( padding_tag == PADDING_TAG, - "Invalid padding tag field value, must be equals to {PADDING_TAG}, provided: {padding_tag}.", + "Invalid padding tag field value, must be equals to {PADDING_TAG}, \ + provided: {padding_tag}.", ); let fragment_tag = read_be_u8(reader).map_err(|_| anyhow!("Missing fragment tag field."))?; ensure!( fragment_tag == FRAGMENT_TAG, - "Invalid fragment tag field value, must be equals to {FRAGMENT_TAG}, provided: {fragment_tag}.", + "Invalid fragment tag field value, must be equals to {FRAGMENT_TAG}, \ + provided: {fragment_tag}.", ); let vote_plan_id = @@ -148,7 +150,8 @@ impl Tx { }, tag => { bail!( - "Invalid vote tag value, must be equals to {PUBLIC_VOTE_TAG} or {PRIVATE_VOTE_TAG}, provided: {tag}" + "Invalid vote tag value, \ + must be equals to {PUBLIC_VOTE_TAG} or {PRIVATE_VOTE_TAG}, provided: {tag}" ) }, }; @@ -160,20 +163,23 @@ impl Tx { read_be_u8(reader).map_err(|_| anyhow!("Missing inputs amount field."))?; ensure!( inputs_amount == NUMBER_OF_INPUTS, - "Invalid number of inputs, expected: {NUMBER_OF_INPUTS}, provided: {inputs_amount}", + "Invalid number of inputs, expected: {NUMBER_OF_INPUTS}, \ + provided: {inputs_amount}", ); let outputs_amount = read_be_u8(reader).map_err(|_| anyhow!("Missing outputs amount field."))?; ensure!( outputs_amount == NUMBER_OF_OUTPUTS, - "Invalid number of outputs, expected: {NUMBER_OF_OUTPUTS}, provided: {outputs_amount}", + "Invalid number of outputs, expected: {NUMBER_OF_OUTPUTS}, \ + provided: {outputs_amount}", ); let input_tag = read_be_u8(reader).map_err(|_| anyhow!("Missing input tag field."))?; ensure!( input_tag == INPUT_TAG, - "Invalid input tag, expected: {INPUT_TAG}, provided: {input_tag}", + "Invalid input tag, expected: {INPUT_TAG}, \ + provided: {input_tag}", ); // skip value @@ -187,7 +193,8 @@ impl Tx { let witness_tag = read_be_u8(reader).map_err(|_| anyhow!("Missing witness tag field."))?; ensure!( witness_tag == WITNESS_TAG, - "Invalid witness tag, expected: {WITNESS_TAG}, provided: {witness_tag}", + "Invalid witness tag, expected: {WITNESS_TAG}, \ + provided: {witness_tag}", ); // Skip nonce field diff --git a/rust/catalyst-voting/src/vote_protocol/tally/mod.rs b/rust/catalyst-voting/src/vote_protocol/tally/mod.rs index 33ee9269d9..c302b18aba 100644 --- a/rust/catalyst-voting/src/vote_protocol/tally/mod.rs +++ b/rust/catalyst-voting/src/vote_protocol/tally/mod.rs @@ -58,18 +58,22 @@ pub fn tally( ) -> anyhow::Result { ensure!( votes.len() == voting_powers.len(), - "Votes and voting power length mismatch. Votes amount: {0}. Voting powers amount: {1}.", + "Votes and voting power length mismatch. Votes amount: {0}. \ + Voting powers amount: {1}.", votes.len(), voting_powers.len(), ); - let mut ciphertexts_per_voting_option = Vec::new(); - for (i, vote) in votes.iter().enumerate() { - let ciphertext = vote - .get_ciphertext_for_choice(voting_option) - .ok_or(anyhow!("Invalid encrypted vote at index {i}. Does not have a ciphertext for the voting option {voting_option}.") )?; - ciphertexts_per_voting_option.push(ciphertext); - } + let ciphertexts_per_voting_option = votes + .iter() + .enumerate() + .map(|(i, v)| { + v.get_ciphertext_for_choice(voting_option).ok_or(anyhow!( + "Invalid encrypted vote at index {i}. \ + Does not have a ciphertext for the voting option {voting_option}." + )) + }) + .collect::>>()?; let zero_ciphertext = Ciphertext::zero(); @@ -97,9 +101,21 @@ pub fn decrypt_tally( ) -> anyhow::Result { let ge = decrypt(&tally_result.0, &secret_key.0); - let res = setup - .discrete_log_setup - .discrete_log(ge) - .map_err(|_| anyhow!("Cannot decrypt tally result. Provided an invalid secret key or invalid encrypted tally result."))?; + let res = setup.discrete_log_setup.discrete_log(ge).map_err(|_| { + anyhow!( + "Cannot decrypt tally result. \ + Provided an invalid secret key or invalid encrypted tally result." + ) + })?; Ok(res) } + +#[test] +fn test() { + let i = 10; + let voting_option = 1; + println!( + "Invalid encrypted vote at index {i}. \ + Does not have a ciphertext for the voting option {voting_option}." + ); +} From 71bb33d97eac4401d8a5b33b211784d907f09d3c Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Tue, 22 Oct 2024 11:10:38 +0300 Subject: [PATCH 04/10] cleanup --- rust/catalyst-voting/src/vote_protocol/tally/mod.rs | 10 ---------- 1 file changed, 10 deletions(-) diff --git a/rust/catalyst-voting/src/vote_protocol/tally/mod.rs b/rust/catalyst-voting/src/vote_protocol/tally/mod.rs index 018fa1c854..6613d6de75 100644 --- a/rust/catalyst-voting/src/vote_protocol/tally/mod.rs +++ b/rust/catalyst-voting/src/vote_protocol/tally/mod.rs @@ -109,13 +109,3 @@ pub fn decrypt_tally( })?; Ok(res) } - -#[test] -fn test() { - let i = 10; - let voting_option = 1; - println!( - "Invalid encrypted vote at index {i}. \ - Does not have a ciphertext for the voting option {voting_option}." - ); -} From 151a390b271a1be4ffe3c59a80736da8e88886d7 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Tue, 22 Oct 2024 17:39:34 +0300 Subject: [PATCH 05/10] wip --- rust/catalyst-voting/benches/vote_protocol.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/rust/catalyst-voting/benches/vote_protocol.rs b/rust/catalyst-voting/benches/vote_protocol.rs index 3028ed2974..bc3d00eef5 100644 --- a/rust/catalyst-voting/benches/vote_protocol.rs +++ b/rust/catalyst-voting/benches/vote_protocol.rs @@ -40,9 +40,12 @@ struct Voter { choice: usize, } +struct Choices(Vec); +struct VotingPowers(Vec); + fn initial_setup() -> ( - Vec, - Vec, + Choices, + VotingPowers, ElectionSecretKey, ElectionPublicKey, VoterProofCommitment, @@ -65,8 +68,8 @@ fn initial_setup() -> ( let election_public_key = election_secret_key.public_key(); ( - choices, - voting_powers, + Choices(choices), + VotingPowers(voting_powers), election_secret_key, election_public_key, voter_proof_commitment, @@ -81,6 +84,7 @@ fn vote_protocol_benches(c: &mut Criterion) { let (choices, voting_powers, election_secret_key, election_public_key, voter_proof_commitment) = initial_setup(); let votes: Vec<_> = choices + .0 .iter() .map(|choice| Vote::new(*choice, VOTING_OPTIONS).unwrap()) .collect(); @@ -141,13 +145,13 @@ fn vote_protocol_benches(c: &mut Criterion) { b.iter(|| { encrypted_tallies = (0..VOTING_OPTIONS) .map(|voting_option| { - tally(voting_option, &encrypted_votes, &voting_powers).unwrap() + tally(voting_option, &encrypted_votes, &voting_powers.0).unwrap() }) .collect(); }); }); - let total_voting_power = voting_powers.iter().sum(); + let total_voting_power = voting_powers.0.iter().sum(); let mut decryption_tally_setup = None; group.bench_function("decryption tally setup initialization", |b| { b.iter(|| { From e39de53f03962b51b6d848572e2fe713307c642f Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 25 Oct 2024 11:20:29 +0300 Subject: [PATCH 06/10] try --- rust/Earthfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/Earthfile b/rust/Earthfile index e7911ea50d..c9ad60d3b7 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -1,6 +1,6 @@ VERSION 0.8 -IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.2.15 AS rust-ci +IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:feat/rust-bench AS rust-ci COPY_SRC: FUNCTION From fa4a15c836d2b282d786f357e8a082012a09a601 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 25 Oct 2024 11:21:00 +0300 Subject: [PATCH 07/10] fix cargo config.toml file --- rust/.cargo/config.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/rust/.cargo/config.toml b/rust/.cargo/config.toml index 02c2314075..6641db1faa 100644 --- a/rust/.cargo/config.toml +++ b/rust/.cargo/config.toml @@ -77,8 +77,8 @@ lint-vscode = "clippy --message-format=json-diagnostic-rendered-ansi --all-targe docs = "doc --release --no-deps --document-private-items --bins --lib --examples" # nightly docs build broken... when they are'nt we can enable these docs... --unit-graph --timings=html,json -Z unstable-options" -testunit = "nextest run --release --bins --lib --tests --benches --no-fail-fast -P ci" -testcov = "llvm-cov nextest --release --bins --lib --tests --benches --no-fail-fast -P ci" +testunit = "nextest run --release --bins --lib --tests --no-fail-fast -P ci" +testcov = "llvm-cov nextest --release --bins --lib --tests --no-fail-fast -P ci" testdocs = "test --doc --release" # Rust formatting, MUST be run with +nightly From 479777ac1ece0dac7ca4a3195af69e7ba610f3b8 Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 25 Oct 2024 11:23:32 +0300 Subject: [PATCH 08/10] update deny.toml --- rust/deny.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/rust/deny.toml b/rust/deny.toml index 26ec8794bb..77f0259f18 100644 --- a/rust/deny.toml +++ b/rust/deny.toml @@ -79,6 +79,7 @@ allow = [ "Unicode-3.0", "MPL-2.0", "Zlib", + "MIT-0", ] exceptions = [ #{ allow = ["Zlib"], crate = "tinyvec" }, From f778693159a22b74372afc2db9baf5747477ec3b Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 25 Oct 2024 12:37:00 +0300 Subject: [PATCH 09/10] wip --- rust/catalyst-voting/benches/vote_protocol.rs | 32 +++++++++++++++---- 1 file changed, 26 insertions(+), 6 deletions(-) diff --git a/rust/catalyst-voting/benches/vote_protocol.rs b/rust/catalyst-voting/benches/vote_protocol.rs index bc3d00eef5..55f9227a94 100644 --- a/rust/catalyst-voting/benches/vote_protocol.rs +++ b/rust/catalyst-voting/benches/vote_protocol.rs @@ -1,4 +1,9 @@ //! `catalyst_voting::vote_protocol` benchmark +//! +//! To run these benchmarks use +//! ```shell +//! SAMPLE_SIZE= VOTERS_NUMBER= cargo bench -p catalyst-voting vote_protocol +//! ``` #![allow( missing_docs, clippy::missing_docs_in_private_items, @@ -24,14 +29,19 @@ use catalyst_voting::{ }; use criterion::{criterion_group, criterion_main, Criterion}; use proptest::{ - prelude::{any, Strategy}, + prelude::{any_with, Strategy}, + sample::size_range, strategy::ValueTree, test_runner::TestRunner, }; use test_strategy::Arbitrary; +const VOTERS_NUMBER_ENV: &str = "VOTERS_NUMBER"; +const SAMPLE_SIZE_ENV: &str = "SAMPLE_SIZE"; +const DEFAULT_SAMPLE_SIZE: usize = 10; +const DEFAULT_VOTERS_NUMBER: usize = 1; + const VOTING_OPTIONS: usize = 3; -const VOTERS_NUMBER: usize = 100; #[derive(Arbitrary, Debug)] struct Voter { @@ -43,7 +53,9 @@ struct Voter { struct Choices(Vec); struct VotingPowers(Vec); -fn initial_setup() -> ( +fn rand_generate_vote_data( + voters_number: usize, +) -> ( Choices, VotingPowers, ElectionSecretKey, @@ -52,7 +64,7 @@ fn initial_setup() -> ( ) { let mut runner = TestRunner::default(); - let (choices, voting_powers) = any::<[Voter; VOTERS_NUMBER]>() + let (choices, voting_powers) = any_with::>((size_range(voters_number), ())) .prop_map(|voter| { ( voter.iter().map(|v| v.choice).collect(), @@ -78,11 +90,19 @@ fn initial_setup() -> ( #[allow(clippy::too_many_lines)] fn vote_protocol_benches(c: &mut Criterion) { + let sample_size = std::env::var(SAMPLE_SIZE_ENV) + .map(|s| s.parse().unwrap()) + .unwrap_or(DEFAULT_SAMPLE_SIZE); + let voters_number = std::env::var(VOTERS_NUMBER_ENV) + .map(|s| s.parse().unwrap()) + .unwrap_or(DEFAULT_VOTERS_NUMBER); + let mut group = c.benchmark_group("vote protocol benchmark"); - group.sample_size(10); + group.sample_size(sample_size); let (choices, voting_powers, election_secret_key, election_public_key, voter_proof_commitment) = - initial_setup(); + rand_generate_vote_data(voters_number); + let votes: Vec<_> = choices .0 .iter() From 72f513ffb10f675f5cec477e53debecfd2fe1e8f Mon Sep 17 00:00:00 2001 From: Mr-Leshiy Date: Fri, 25 Oct 2024 17:18:50 +0300 Subject: [PATCH 10/10] bump cat-ci/rust version --- rust/Earthfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rust/Earthfile b/rust/Earthfile index c9ad60d3b7..0339348a2c 100644 --- a/rust/Earthfile +++ b/rust/Earthfile @@ -1,6 +1,6 @@ VERSION 0.8 -IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:feat/rust-bench AS rust-ci +IMPORT github.com/input-output-hk/catalyst-ci/earthly/rust:v3.2.19 AS rust-ci COPY_SRC: FUNCTION