forked from poanetwork/threshold_crypto
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbench.rs
134 lines (123 loc) · 4.21 KB
/
bench.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
use criterion::{criterion_group, criterion_main, Criterion};
use ff::Field;
use threshold_crypto::poly::Poly;
use threshold_crypto::Scalar;
const TEST_DEGREES: [usize; 4] = [5, 10, 20, 40];
const TEST_THRESHOLDS: [usize; 4] = [5, 10, 20, 40];
const RNG_SEED: [u8; 16] = *b"0123456789abcdef";
mod poly_benches {
use super::*;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
/// Benchmarks multiplication of two polynomials.
fn multiplication(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs(
"Polynomial multiplication",
move |b, &°| {
let rand_factors = || {
let lhs = Poly::random(deg, &mut rng);
let rhs = Poly::random(deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs * &rhs)
},
&TEST_DEGREES,
);
}
/// Benchmarks subtraction of two polynomials
fn subtraction(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs(
"Polynomial subtraction",
move |b, &°| {
let rand_factors = || {
let lhs = Poly::random(deg, &mut rng);
let rhs = Poly::random(deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs - &rhs)
},
&TEST_DEGREES,
);
}
/// Benchmarks addition of two polynomials
fn addition(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs(
"Polynomial addition",
move |b, &°| {
let rand_factors = || {
let lhs = Poly::random(deg, &mut rng);
let rhs = Poly::random(deg, &mut rng);
(lhs, rhs)
};
b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs + &rhs)
},
&TEST_DEGREES,
);
}
/// Benchmarks Lagrange interpolation for a polynomial.
fn interpolate(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
c.bench_function_over_inputs(
"Polynomial interpolation",
move |b, &°| {
b.iter_with_setup(
|| {
(0..=deg)
.map(|i| (i, Scalar::random(&mut rng)))
.collect::<Vec<_>>()
},
Poly::interpolate,
)
},
&TEST_DEGREES,
);
}
criterion_group! {
name = poly_benches;
config = Criterion::default();
targets = multiplication, interpolate, addition, subtraction,
}
}
mod public_key_set_benches {
use super::*;
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
use std::collections::BTreeMap;
use threshold_crypto::SecretKeySet;
/// Benchmarks combining signatures
fn combine_signatures(c: &mut Criterion) {
let mut rng = XorShiftRng::from_seed(RNG_SEED);
let msg = "Test message";
c.bench_function_over_inputs(
"Combine Signatures",
move |b, &&threshold| {
let sk_set = SecretKeySet::random(threshold, &mut rng);
let pk_set = sk_set.public_keys();
let sigs: BTreeMap<_, _> = (0..=threshold)
.map(|i| {
let sig = sk_set.secret_key_share(i).sign(msg);
(i, sig)
})
.collect();
b.iter(|| {
pk_set
.combine_signatures(&sigs)
.expect("could not combine signatures");
})
},
&TEST_THRESHOLDS,
);
}
criterion_group! {
name = public_key_set_benches;
config = Criterion::default();
targets = combine_signatures,
}
}
criterion_main!(
poly_benches::poly_benches,
public_key_set_benches::public_key_set_benches
);