You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
2.3 KiB

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