Skip to content

Commit 987e87d

Browse files
committed
add bench for vess
1 parent 69ae488 commit 987e87d

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed

timeboost-crypto/Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,7 @@ criterion = { workspace = true }
4040
[[bench]]
4141
name = "decryption"
4242
harness = false
43+
44+
[[bench]]
45+
name = "vess"
46+
harness = false

timeboost-crypto/benches/vess.rs

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
use std::{collections::BTreeMap, iter::repeat_with};
2+
3+
use ark_ec::CurveGroup;
4+
use ark_std::{
5+
UniformRand,
6+
rand::{Rng, SeedableRng, rngs::StdRng},
7+
};
8+
use criterion::{Criterion, criterion_group, criterion_main};
9+
use timeboost_crypto::mre::{self, LabeledDecryptionKey};
10+
use timeboost_crypto::vess::ShoupVess;
11+
12+
const KB: usize = 1 << 10;
13+
14+
/// Helper function to create a test committee with specified parameters
15+
fn create_test_committee(epoch: u64, size: usize) -> multisig::Committee {
16+
let keypairs: Vec<multisig::Keypair> =
17+
(0..size).map(|_| multisig::Keypair::generate()).collect();
18+
multisig::Committee::new(
19+
epoch,
20+
keypairs
21+
.iter()
22+
.enumerate()
23+
.map(|(i, kp)| (i as u8, kp.public_key())),
24+
)
25+
}
26+
27+
fn shoup_vess<C: CurveGroup>(c: &mut Criterion, vess: ShoupVess<C>) {
28+
let rng = &mut StdRng::seed_from_u64(42);
29+
let committee_sizes = [13];
30+
let secret = C::ScalarField::rand(rng);
31+
32+
for committee_size in committee_sizes {
33+
let benchmark_group_name = |op_name| format!("ShoupVESS_{committee_size}_{op_name}");
34+
35+
// Create a test committee
36+
let committee = create_test_committee(0, committee_size);
37+
let n = committee.size().get();
38+
let aad = b"vess aad";
39+
40+
// prepare their encryption keys for secure communication
41+
let recv_sks: Vec<mre::DecryptionKey<C>> = repeat_with(|| mre::DecryptionKey::rand(rng))
42+
.take(n)
43+
.collect();
44+
let recv_pks: BTreeMap<usize, mre::EncryptionKey<C>> = recv_sks
45+
.iter()
46+
.enumerate()
47+
.map(|(i, sk)| (i, mre::EncryptionKey::from(sk)))
48+
.collect();
49+
let labeled_sks: Vec<LabeledDecryptionKey<C>> = recv_sks
50+
.into_iter()
51+
.enumerate()
52+
.map(|(i, sk)| sk.label(i))
53+
.collect();
54+
55+
// benchmark encrypt_shares
56+
c.bench_function(&benchmark_group_name("encrypt"), |b| {
57+
b.iter(|| {
58+
vess.encrypt_shares(&committee, recv_pks.values(), secret, aad)
59+
.unwrap();
60+
})
61+
});
62+
63+
// benchmark verify
64+
let (ct, comm) = vess
65+
.encrypt_shares(&committee, recv_pks.values(), secret, aad)
66+
.unwrap();
67+
println!(
68+
"{}: ciphertext: {} KB",
69+
benchmark_group_name("size"),
70+
ct.as_bytes().len() / KB
71+
);
72+
c.bench_function(&benchmark_group_name("verify"), |b| {
73+
b.iter(|| {
74+
vess.verify_shares(&committee, recv_pks.values(), &ct, &comm, aad)
75+
.unwrap();
76+
})
77+
});
78+
79+
// benchmark decrypt
80+
// select a random receiver to test its decryption
81+
let recv_idx = rng.gen_range(0..committee_size);
82+
let labeled_recv_sk = &labeled_sks[recv_idx];
83+
c.bench_function(&benchmark_group_name("decrypt"), |b| {
84+
b.iter(|| {
85+
vess.decrypt_share(&committee, labeled_recv_sk, &ct, aad)
86+
.unwrap();
87+
})
88+
});
89+
}
90+
}
91+
92+
fn vess_main(c: &mut Criterion) {
93+
shoup_vess(c, ShoupVess::<ark_bls12_381::G1Projective>::new_fast());
94+
}
95+
96+
criterion_group!(name = benches; config = Criterion::default().sample_size(10); targets = vess_main);
97+
98+
criterion_main!(benches);

0 commit comments

Comments
 (0)