-
Notifications
You must be signed in to change notification settings - Fork 1
feat(rust/catalyst-voting): Vote protocol benchmarks #65
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
Merged
Merged
Changes from 5 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
56da41d
add vote protocol benchmarks
Mr-Leshiy ad5ec99
add missing benchmarks
Mr-Leshiy 908b1a4
refactor
Mr-Leshiy aee40b0
Merge branch 'main' into feat/add-voting-benchmark
Mr-Leshiy 71bb33d
cleanup
Mr-Leshiy 151a390
wip
Mr-Leshiy e25c70a
Merge branch 'main' into feat/add-voting-benchmark
minikin e39de53
try
Mr-Leshiy fa4a15c
fix cargo config.toml file
Mr-Leshiy 479777a
update deny.toml
Mr-Leshiy f778693
wip
Mr-Leshiy 72f513f
bump cat-ci/rust version
Mr-Leshiy e6d94d4
Merge branch 'main' into feat/add-voting-benchmark
minikin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| //! `catalyst_voting::vote_protocol` benchmark | ||
| #![allow( | ||
| missing_docs, | ||
| clippy::missing_docs_in_private_items, | ||
| clippy::unwrap_used, | ||
| clippy::similar_names | ||
| )] | ||
|
|
||
| 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}; | ||
| 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() -> ( | ||
| Vec<usize>, | ||
| Vec<u64>, | ||
| ElectionSecretKey, | ||
| ElectionPublicKey, | ||
| VoterProofCommitment, | ||
| ) { | ||
| let mut runner = TestRunner::default(); | ||
|
|
||
| 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(); | ||
|
|
||
| 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(); | ||
|
|
||
| ( | ||
| 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 mut group = c.benchmark_group("vote protocol benchmark"); | ||
| group.sample_size(10); | ||
|
|
||
| let (choices, voting_powers, election_secret_key, election_public_key, voter_proof_commitment) = | ||
| initial_setup(); | ||
| let votes: Vec<_> = choices | ||
| .iter() | ||
| .map(|choice| Vote::new(*choice, VOTING_OPTIONS).unwrap()) | ||
| .collect(); | ||
| let mut rng = default_rng(); | ||
|
|
||
| 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(vote, &election_public_key, &mut rng)) | ||
| .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( | ||
| v, | ||
| enc_v.clone(), | ||
| r.clone(), | ||
| &election_public_key, | ||
| &voter_proof_commitment, | ||
| &mut rng, | ||
| ) | ||
| .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); | ||
| }); | ||
| }); | ||
|
|
||
| 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(); | ||
| } | ||
|
|
||
| criterion_group!(benches, vote_protocol_benches); | ||
|
|
||
| criterion_main!(benches); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.