|
1 | 1 | #[macro_use] |
2 | 2 | extern crate criterion; |
| 3 | +extern crate pairing; |
3 | 4 | extern crate rand; |
4 | 5 | extern crate threshold_crypto; |
5 | 6 |
|
6 | 7 | use criterion::Criterion; |
| 8 | +use pairing::bls12_381::Fr; |
7 | 9 | use threshold_crypto::poly::Poly; |
8 | 10 |
|
9 | 11 | mod poly_benches { |
10 | 12 | use super::*; |
| 13 | + use rand::Rng; |
11 | 14 |
|
12 | | - // Benchmarks multiplication of two degree 3 polynomials. |
| 15 | + // Benchmarks multiplication of two polynomials. |
13 | 16 | fn multiplication(c: &mut Criterion) { |
14 | 17 | let mut rng = rand::thread_rng(); |
15 | | - let lhs = Poly::random(3, &mut rng).unwrap(); |
16 | | - let rhs = Poly::random(3, &mut rng).unwrap(); |
17 | | - c.bench_function("Polynomial multiplication", move |b| b.iter(|| &lhs * &rhs)); |
| 18 | + c.bench_function_over_inputs( |
| 19 | + "Polynomial multiplication", |
| 20 | + move |b, &°| { |
| 21 | + let rand_factors = || { |
| 22 | + let lhs = Poly::random(deg, &mut rng).unwrap(); |
| 23 | + let rhs = Poly::random(deg, &mut rng).unwrap(); |
| 24 | + (lhs, rhs) |
| 25 | + }; |
| 26 | + b.iter_with_setup(rand_factors, |(lhs, rhs)| &lhs * &rhs) |
| 27 | + }, |
| 28 | + &[5, 10, 20, 40], |
| 29 | + ); |
18 | 30 | } |
19 | 31 |
|
20 | | - // Benchmarks Lagrange interpolation for a degree 3 polynomial. |
| 32 | + // Benchmarks Lagrange interpolation for a polynomial. |
21 | 33 | fn interpolate(c: &mut Criterion) { |
22 | | - // Points from the the polynomial: `y(x) = 5x^3 + 0x^2 + x - 2`. |
23 | | - let sample_points = vec![(-1, -8), (2, 40), (3, 136), (5, 628)]; |
24 | | - c.bench_function("Polynomial interpolation", move |b| { |
25 | | - b.iter(|| Poly::interpolate(sample_points.clone()).unwrap()) |
26 | | - }); |
| 34 | + let mut rng = rand::thread_rng(); |
| 35 | + c.bench_function_over_inputs( |
| 36 | + "Polynomial interpolation", |
| 37 | + move |b, &°| { |
| 38 | + let rand_samples = || (0..=deg).map(|i| (i, rng.gen::<Fr>())).collect::<Vec<_>>(); |
| 39 | + b.iter_with_setup(rand_samples, |samples| Poly::interpolate(samples).unwrap()) |
| 40 | + }, |
| 41 | + &[5, 10, 20, 40], |
| 42 | + ); |
27 | 43 | } |
28 | 44 |
|
29 | 45 | criterion_group!{ |
|
0 commit comments