testudo/benches/dotproduct.rs
benches/dotproduct.rsRaw
1extern crate byteorder;
2extern crate core;
3extern crate criterion;
4extern crate digest;
5extern crate libspartan;
6extern crate merlin;
7extern crate rand;
8extern crate sha3;
9
10use libspartan::math::Math;
11use libspartan::nizk::DotProductProof;
12use libspartan::scalar::Scalar;
13use libspartan::scalar::ScalarBytes;
14use rand::rngs::OsRng;
15
16use criterion::*;
17
18fn dotproduct_benchmark_dalek(c: &mut Criterion) {
19 let mut csprng: OsRng = OsRng;
20
21 for &s in [20].iter() {
22 let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
23 let mut group = c.benchmark_group("dotproduct_benchmark_dalek");
24 group.plot_config(plot_config);
25
26 let n = (s as usize).pow2();
27 let vec_a = (0..n)
28 .map(|_i| ScalarBytes::random(&mut csprng))
29 .collect::<Vec<ScalarBytes>>();
30 let vec_b = (0..n)
31 .map(|_i| ScalarBytes::random(&mut csprng))
32 .collect::<Vec<ScalarBytes>>();
33
34 let name = format!("dotproduct_dalek_{}", n);
35 group.bench_function(&name, move |b| {
36 b.iter(|| compute_dotproduct(black_box(&vec_a), black_box(&vec_b)));
37 });
38 group.finish();
39 }
40}
41
42fn compute_dotproduct(a: &Vec<ScalarBytes>, b: &Vec<ScalarBytes>) -> ScalarBytes {
43 let mut res = ScalarBytes::zero();
44 for i in 0..a.len() {
45 res = &res + &a[i] * &b[i];
46 }
47 res
48}
49
50fn dotproduct_benchmark_opt(c: &mut Criterion) {
51 let mut csprng: OsRng = OsRng;
52
53 for &s in [20].iter() {
54 let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
55 let mut group = c.benchmark_group("dotproduct_benchmark_opt");
56 group.plot_config(plot_config);
57
58 let n = (s as usize).pow2();
59 let vec_a = (0..n)
60 .map(|_i| Scalar::random(&mut csprng))
61 .collect::<Vec<Scalar>>();
62 let vec_b = (0..n)
63 .map(|_i| Scalar::random(&mut csprng))
64 .collect::<Vec<Scalar>>();
65
66 let name = format!("dotproduct_opt_{}", n);
67 group.bench_function(&name, move |b| {
68 b.iter(|| DotProductProof::compute_dotproduct(black_box(&vec_a), black_box(&vec_b)));
69 });
70 group.finish();
71 }
72}
73
74fn set_duration() -> Criterion {
75 Criterion::default().sample_size(10)
76 // .measurement_time(Duration::new(0, 50000000))
77}
78
79criterion_group! {
80name = benches_dotproduct;
81config = set_duration();
82targets = dotproduct_benchmark_dalek, dotproduct_benchmark_opt
83}
84
85criterion_main!(benches_dotproduct);