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.

114 lines
3.3 KiB

4 years ago
4 years ago
4 years ago
4 years ago
  1. extern crate byteorder;
  2. extern crate core;
  3. extern crate criterion;
  4. extern crate digest;
  5. extern crate libspartan;
  6. extern crate merlin;
  7. extern crate rand;
  8. extern crate sha3;
  9. use libspartan::dense_mlpoly::EqPolynomial;
  10. use libspartan::math::Math;
  11. use libspartan::r1csinstance::R1CSInstance;
  12. use libspartan::r1csproof::{R1CSGens, R1CSProof};
  13. use libspartan::random::RandomTape;
  14. use libspartan::scalar::Scalar;
  15. use libspartan::transcript::ProofTranscript;
  16. use merlin::Transcript;
  17. use rand::rngs::OsRng;
  18. use criterion::*;
  19. fn prove_benchmark(c: &mut Criterion) {
  20. for &s in [10, 12, 16].iter() {
  21. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  22. let mut group = c.benchmark_group("r1cs_prove_benchmark");
  23. group.plot_config(plot_config);
  24. let num_vars = s.pow2();
  25. let num_cons = num_vars;
  26. let num_inputs = 10;
  27. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  28. let n = inst.get_num_vars();
  29. let gens = R1CSGens::new(num_cons, num_vars, b"test-m");
  30. let name = format!("r1cs_prove_{}", n);
  31. group.bench_function(&name, move |b| {
  32. b.iter(|| {
  33. let mut random_tape = RandomTape::new(b"proof");
  34. let mut prover_transcript = Transcript::new(b"example");
  35. R1CSProof::prove(
  36. black_box(&inst),
  37. black_box(vars.clone()),
  38. black_box(&input),
  39. black_box(&gens),
  40. black_box(&mut prover_transcript),
  41. black_box(&mut random_tape),
  42. )
  43. });
  44. });
  45. group.finish();
  46. }
  47. }
  48. fn verify_benchmark(c: &mut Criterion) {
  49. for &s in [10, 12, 16, 20].iter() {
  50. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  51. let mut group = c.benchmark_group("r1cs_verify_benchmark");
  52. group.plot_config(plot_config);
  53. let num_vars = s.pow2();
  54. let num_cons = num_vars;
  55. let num_inputs = 10;
  56. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  57. let n = inst.get_num_vars();
  58. let gens = R1CSGens::new(num_cons, num_vars, b"test-m");
  59. let mut random_tape = RandomTape::new(b"proof");
  60. let mut prover_transcript = Transcript::new(b"example");
  61. let (proof, rx, ry) = R1CSProof::prove(
  62. &inst,
  63. vars,
  64. &input,
  65. &gens,
  66. &mut prover_transcript,
  67. &mut random_tape,
  68. );
  69. let eval_table_rx = EqPolynomial::new(rx.clone()).evals();
  70. let eval_table_ry = EqPolynomial::new(ry.clone()).evals();
  71. let inst_evals = inst.evaluate_with_tables(&eval_table_rx, &eval_table_ry);
  72. let name = format!("r1cs_verify_{}", n);
  73. group.bench_function(&name, move |b| {
  74. b.iter(|| {
  75. let mut verifier_transcript = Transcript::new(b"example");
  76. assert!(proof
  77. .verify(
  78. black_box(num_vars),
  79. black_box(num_cons),
  80. black_box(&input),
  81. black_box(&inst_evals),
  82. black_box(&mut verifier_transcript),
  83. black_box(&gens)
  84. )
  85. .is_ok());
  86. });
  87. });
  88. group.finish();
  89. }
  90. }
  91. fn set_duration() -> Criterion {
  92. Criterion::default().sample_size(10)
  93. // .measurement_time(Duration::new(0, 50000000))
  94. }
  95. criterion_group! {
  96. name = benches_r1cs;
  97. config = set_duration();
  98. targets = prove_benchmark, verify_benchmark
  99. }
  100. criterion_main!(benches_r1cs);