Skip to content

Commit 56da41d

Browse files
committed
add vote protocol benchmarks
1 parent d0b5784 commit 56da41d

File tree

3 files changed

+134
-4
lines changed

3 files changed

+134
-4
lines changed

rust/catalyst-voting/Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ license.workspace = true
1212
[lints]
1313
workspace = true
1414

15+
[[bench]]
16+
name = "vote_protocol"
17+
harness = false
18+
1519
[dependencies]
1620
anyhow = "1.0.89"
1721
rand_core = { version = "0.6.4", features = ["getrandom"] }
@@ -21,7 +25,8 @@ ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }
2125
blake2b_simd = "1.0.2"
2226

2327
[dev-dependencies]
24-
proptest = {version = "1.5.0" }
28+
criterion = "0.5.1"
29+
proptest = { version = "1.5.0" }
2530
# Potentially it could be replaced with using `proptest::property_test` attribute macro,
2631
# after this PR will be merged https://github.com/proptest-rs/proptest/pull/523
2732
test-strategy = "0.4.0"
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
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);

rust/catalyst-voting/tests/voting_test.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,17 @@ use proptest::prelude::ProptestConfig;
1717
use test_strategy::{proptest, Arbitrary};
1818

1919
const VOTING_OPTIONS: usize = 3;
20+
const VOTERS_NUMBER: usize = 100;
2021

2122
#[derive(Arbitrary, Debug)]
2223
struct Voter {
2324
voting_power: u32,
24-
// range from 0 to `VOTING_OPTIONS`
25-
#[strategy(0..3_usize)]
25+
#[strategy(0..VOTING_OPTIONS)]
2626
choice: usize,
2727
}
2828

2929
#[proptest(ProptestConfig::with_cases(1))]
30-
fn voting_test(voters: [Voter; 100]) {
30+
fn voting_test(voters: [Voter; VOTERS_NUMBER]) {
3131
let election_secret_key = ElectionSecretKey::random_with_default_rng();
3232
let election_public_key = election_secret_key.public_key();
3333
let voter_proof_commitment = VoterProofCommitment::random_with_default_rng();

0 commit comments

Comments
 (0)