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