Skip to content

Commit ad5ec99

Browse files
committed
add missing benchmarks
1 parent 56da41d commit ad5ec99

File tree

1 file changed

+88
-18
lines changed

1 file changed

+88
-18
lines changed

rust/catalyst-voting/benches/vote_protocol.rs

Lines changed: 88 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,20 @@
66
clippy::similar_names
77
)]
88

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,
9+
use catalyst_voting::{
10+
crypto::default_rng,
11+
vote_protocol::{
12+
committee::{ElectionPublicKey, ElectionSecretKey},
13+
tally::{
14+
decrypt_tally,
15+
proof::{generate_tally_proof, verify_tally_proof},
16+
tally, DecryptionTallySetup,
17+
},
18+
voter::{
19+
encrypt_vote,
20+
proof::{generate_voter_proof, verify_voter_proof, VoterProofCommitment},
21+
Vote,
22+
},
1523
},
1624
};
1725
use criterion::{criterion_group, criterion_main, Criterion};
@@ -27,20 +35,27 @@ const VOTERS_NUMBER: usize = 100;
2735

2836
#[derive(Arbitrary, Debug)]
2937
struct Voter {
30-
_voting_power: u32,
38+
voting_power: u32,
3139
#[strategy(0..VOTING_OPTIONS)]
3240
choice: usize,
3341
}
3442

3543
fn initial_setup() -> (
36-
[Voter; VOTERS_NUMBER],
44+
Vec<usize>,
45+
Vec<u64>,
3746
ElectionSecretKey,
3847
ElectionPublicKey,
3948
VoterProofCommitment,
4049
) {
4150
let mut runner = TestRunner::default();
4251

43-
let voters = any::<[Voter; VOTERS_NUMBER]>()
52+
let (choices, voting_powers) = any::<[Voter; VOTERS_NUMBER]>()
53+
.prop_map(|voter| {
54+
(
55+
voter.iter().map(|v| v.choice).collect(),
56+
voter.iter().map(|v| v.voting_power.into()).collect(),
57+
)
58+
})
4459
.new_tree(&mut runner)
4560
.unwrap()
4661
.current();
@@ -50,31 +65,34 @@ fn initial_setup() -> (
5065
let election_public_key = election_secret_key.public_key();
5166

5267
(
53-
voters,
68+
choices,
69+
voting_powers,
5470
election_secret_key,
5571
election_public_key,
5672
voter_proof_commitment,
5773
)
5874
}
5975

76+
#[allow(clippy::too_many_lines)]
6077
fn vote_protocol_benches(c: &mut Criterion) {
61-
let (voters, _election_secret_key, election_public_key, voter_proof_commitment) =
62-
initial_setup();
78+
let mut group = c.benchmark_group("vote protocol benchmark");
79+
group.sample_size(10);
6380

64-
let votes: Vec<_> = voters
81+
let (choices, voting_powers, election_secret_key, election_public_key, voter_proof_commitment) =
82+
initial_setup();
83+
let votes: Vec<_> = choices
6584
.iter()
66-
.map(|voter| Vote::new(voter.choice, VOTING_OPTIONS).unwrap())
85+
.map(|choice| Vote::new(*choice, VOTING_OPTIONS).unwrap())
6786
.collect();
68-
69-
let mut group = c.benchmark_group("vote protocol benchmark");
87+
let mut rng = default_rng();
7088

7189
let mut encrypted_votes = Vec::new();
7290
let mut randomness = Vec::new();
7391
group.bench_function("vote encryption", |b| {
7492
b.iter(|| {
7593
(encrypted_votes, randomness) = votes
7694
.iter()
77-
.map(|vote| encrypt_vote_with_default_rng(vote, &election_public_key))
95+
.map(|vote| encrypt_vote(vote, &election_public_key, &mut rng))
7896
.unzip();
7997
});
8098
});
@@ -87,12 +105,13 @@ fn vote_protocol_benches(c: &mut Criterion) {
87105
.zip(encrypted_votes.iter())
88106
.zip(randomness.iter())
89107
.map(|((v, enc_v), r)| {
90-
generate_voter_proof_with_default_rng(
108+
generate_voter_proof(
91109
v,
92110
enc_v.clone(),
93111
r.clone(),
94112
&election_public_key,
95113
&voter_proof_commitment,
114+
&mut rng,
96115
)
97116
.unwrap()
98117
})
@@ -117,6 +136,57 @@ fn vote_protocol_benches(c: &mut Criterion) {
117136
});
118137
});
119138

139+
let mut encrypted_tallies = Vec::new();
140+
group.bench_function("tally", |b| {
141+
b.iter(|| {
142+
encrypted_tallies = (0..VOTING_OPTIONS)
143+
.map(|voting_option| {
144+
tally(voting_option, &encrypted_votes, &voting_powers).unwrap()
145+
})
146+
.collect();
147+
});
148+
});
149+
150+
let total_voting_power = voting_powers.iter().sum();
151+
let mut decryption_tally_setup = None;
152+
group.bench_function("decryption tally setup initialization", |b| {
153+
b.iter(|| {
154+
decryption_tally_setup = Some(DecryptionTallySetup::new(total_voting_power).unwrap());
155+
});
156+
});
157+
let decryption_tally_setup = decryption_tally_setup.unwrap();
158+
159+
let mut decrypted_tallies = Vec::new();
160+
group.bench_function("decrypt tally", |b| {
161+
b.iter(|| {
162+
decrypted_tallies = encrypted_tallies
163+
.iter()
164+
.map(|t| decrypt_tally(t, &election_secret_key, &decryption_tally_setup).unwrap())
165+
.collect();
166+
});
167+
});
168+
169+
let mut tally_proofs = Vec::new();
170+
group.bench_function("tally proof generation", |b| {
171+
b.iter(|| {
172+
tally_proofs = encrypted_tallies
173+
.iter()
174+
.map(|t| generate_tally_proof(t, &election_secret_key, &mut rng))
175+
.collect();
176+
});
177+
});
178+
179+
group.bench_function("tally proof verification", |b| {
180+
b.iter(|| {
181+
let is_ok = tally_proofs
182+
.iter()
183+
.zip(encrypted_tallies.iter())
184+
.zip(decrypted_tallies.iter())
185+
.all(|((p, enc_t), t)| verify_tally_proof(enc_t, *t, &election_public_key, p));
186+
assert!(is_ok);
187+
});
188+
});
189+
120190
group.finish();
121191
}
122192

0 commit comments

Comments
 (0)