|
| 1 | +//! `catalyst_voting::vote_protocol` benchmark |
| 2 | +#![allow( |
| 3 | + missing_docs, |
| 4 | + clippy::missing_docs_in_private_items, |
| 5 | + clippy::unwrap_used, |
| 6 | + clippy::similar_names |
| 7 | +)] |
| 8 | + |
| 9 | +use catalyst_voting::vote_protocol::{ |
| 10 | + committee::{ElectionPublicKey, ElectionSecretKey}, |
| 11 | + voter::{ |
| 12 | + encrypt_vote_with_default_rng, |
| 13 | + proof::{generate_voter_proof_with_default_rng, verify_voter_proof, VoterProofCommitment}, |
| 14 | + Vote, |
| 15 | + }, |
| 16 | +}; |
| 17 | +use criterion::{criterion_group, criterion_main, Criterion}; |
| 18 | +use proptest::{ |
| 19 | + prelude::{any, Strategy}, |
| 20 | + strategy::ValueTree, |
| 21 | + test_runner::TestRunner, |
| 22 | +}; |
| 23 | +use test_strategy::Arbitrary; |
| 24 | + |
| 25 | +const VOTING_OPTIONS: usize = 3; |
| 26 | +const VOTERS_NUMBER: usize = 100; |
| 27 | + |
| 28 | +#[derive(Arbitrary, Debug)] |
| 29 | +struct Voter { |
| 30 | + _voting_power: u32, |
| 31 | + #[strategy(0..VOTING_OPTIONS)] |
| 32 | + choice: usize, |
| 33 | +} |
| 34 | + |
| 35 | +fn initial_setup() -> ( |
| 36 | + [Voter; VOTERS_NUMBER], |
| 37 | + ElectionSecretKey, |
| 38 | + ElectionPublicKey, |
| 39 | + VoterProofCommitment, |
| 40 | +) { |
| 41 | + let mut runner = TestRunner::default(); |
| 42 | + |
| 43 | + let voters = any::<[Voter; VOTERS_NUMBER]>() |
| 44 | + .new_tree(&mut runner) |
| 45 | + .unwrap() |
| 46 | + .current(); |
| 47 | + |
| 48 | + let election_secret_key = ElectionSecretKey::random_with_default_rng(); |
| 49 | + let voter_proof_commitment = VoterProofCommitment::random_with_default_rng(); |
| 50 | + let election_public_key = election_secret_key.public_key(); |
| 51 | + |
| 52 | + ( |
| 53 | + voters, |
| 54 | + election_secret_key, |
| 55 | + election_public_key, |
| 56 | + voter_proof_commitment, |
| 57 | + ) |
| 58 | +} |
| 59 | + |
| 60 | +fn vote_protocol_benches(c: &mut Criterion) { |
| 61 | + let (voters, _election_secret_key, election_public_key, voter_proof_commitment) = |
| 62 | + initial_setup(); |
| 63 | + |
| 64 | + let votes: Vec<_> = voters |
| 65 | + .iter() |
| 66 | + .map(|voter| Vote::new(voter.choice, VOTING_OPTIONS).unwrap()) |
| 67 | + .collect(); |
| 68 | + |
| 69 | + let mut group = c.benchmark_group("vote protocol benchmark"); |
| 70 | + |
| 71 | + let mut encrypted_votes = Vec::new(); |
| 72 | + let mut randomness = Vec::new(); |
| 73 | + group.bench_function("vote encryption", |b| { |
| 74 | + b.iter(|| { |
| 75 | + (encrypted_votes, randomness) = votes |
| 76 | + .iter() |
| 77 | + .map(|vote| encrypt_vote_with_default_rng(vote, &election_public_key)) |
| 78 | + .unzip(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + let mut voter_proofs = Vec::new(); |
| 83 | + group.bench_function("voter proof generation", |b| { |
| 84 | + b.iter(|| { |
| 85 | + voter_proofs = votes |
| 86 | + .iter() |
| 87 | + .zip(encrypted_votes.iter()) |
| 88 | + .zip(randomness.iter()) |
| 89 | + .map(|((v, enc_v), r)| { |
| 90 | + generate_voter_proof_with_default_rng( |
| 91 | + v, |
| 92 | + enc_v.clone(), |
| 93 | + r.clone(), |
| 94 | + &election_public_key, |
| 95 | + &voter_proof_commitment, |
| 96 | + ) |
| 97 | + .unwrap() |
| 98 | + }) |
| 99 | + .collect(); |
| 100 | + }); |
| 101 | + }); |
| 102 | + |
| 103 | + group.bench_function("voter proof verification", |b| { |
| 104 | + b.iter(|| { |
| 105 | + let is_ok = voter_proofs |
| 106 | + .iter() |
| 107 | + .zip(encrypted_votes.iter()) |
| 108 | + .all(|(p, enc_v)| { |
| 109 | + verify_voter_proof( |
| 110 | + enc_v.clone(), |
| 111 | + &election_public_key, |
| 112 | + &voter_proof_commitment, |
| 113 | + p, |
| 114 | + ) |
| 115 | + }); |
| 116 | + assert!(is_ok); |
| 117 | + }); |
| 118 | + }); |
| 119 | + |
| 120 | + group.finish(); |
| 121 | +} |
| 122 | + |
| 123 | +criterion_group!(benches, vote_protocol_benches); |
| 124 | + |
| 125 | +criterion_main!(benches); |
0 commit comments