130 lines
3.7 KiB

  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::r1csinstance::R1CSInstance;
  10. use libspartan::spartan::{SNARKGens, SNARK};
  11. use merlin::Transcript;
  12. use criterion::*;
  13. fn snark_encode_benchmark(c: &mut Criterion) {
  14. for &s in [10, 12, 16].iter() {
  15. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  16. let mut group = c.benchmark_group("SNARK_encode_benchmark");
  17. group.plot_config(plot_config);
  18. let num_vars = (2 as usize).pow(s as u32);
  19. let num_cons = num_vars;
  20. let num_inputs = 10;
  21. let (inst, _vars, _input) =
  22. R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  23. let n = inst.get_num_vars();
  24. // produce public parameters
  25. let gens = SNARKGens::new(&inst.size());
  26. let name = format!("SNARK_encode_{}", n);
  27. group.bench_function(&name, move |b| {
  28. b.iter(|| {
  29. SNARK::encode(black_box(&inst), black_box(&gens));
  30. });
  31. });
  32. group.finish();
  33. }
  34. }
  35. fn snark_prove_benchmark(c: &mut Criterion) {
  36. for &s in [10, 12, 16].iter() {
  37. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  38. let mut group = c.benchmark_group("SNARK_prove_benchmark");
  39. group.plot_config(plot_config);
  40. let num_vars = (2 as usize).pow(s as u32);
  41. let num_cons = num_vars;
  42. let num_inputs = 10;
  43. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  44. let n = inst.get_num_vars();
  45. // produce public parameters
  46. let gens = SNARKGens::new(&inst.size());
  47. // encode the R1CS instance
  48. let (_comm, decomm) = SNARK::encode(&inst, &gens);
  49. // produce a proof
  50. let name = format!("SNARK_prove_{}", n);
  51. group.bench_function(&name, move |b| {
  52. b.iter(|| {
  53. let mut prover_transcript = Transcript::new(b"example");
  54. SNARK::prove(
  55. black_box(&inst),
  56. black_box(&decomm),
  57. black_box(vars.clone()),
  58. black_box(&input),
  59. black_box(&gens),
  60. black_box(&mut prover_transcript),
  61. );
  62. });
  63. });
  64. group.finish();
  65. }
  66. }
  67. fn snark_verify_benchmark(c: &mut Criterion) {
  68. for &s in [10, 12, 16].iter() {
  69. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  70. let mut group = c.benchmark_group("SNARK_verify_benchmark");
  71. group.plot_config(plot_config);
  72. let num_vars = (2 as usize).pow(s as u32);
  73. let num_cons = num_vars;
  74. let num_inputs = 10;
  75. let (inst, vars, input) = R1CSInstance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  76. let n = inst.get_num_vars();
  77. // produce public parameters
  78. let gens = SNARKGens::new(&inst.size());
  79. // encode the R1CS instance
  80. let (comm, decomm) = SNARK::encode(&inst, &gens);
  81. // produce a proof of satisfiability
  82. let mut prover_transcript = Transcript::new(b"example");
  83. let proof = SNARK::prove(&inst, &decomm, vars, &input, &gens, &mut prover_transcript);
  84. let name = format!("SNARK_verify_{}", n);
  85. group.bench_function(&name, move |b| {
  86. b.iter(|| {
  87. let mut verifier_transcript = Transcript::new(b"example");
  88. assert!(proof
  89. .verify(
  90. black_box(&comm),
  91. black_box(&input),
  92. black_box(&mut verifier_transcript),
  93. black_box(&gens)
  94. )
  95. .is_ok());
  96. });
  97. });
  98. group.finish();
  99. }
  100. }
  101. fn set_duration() -> Criterion {
  102. Criterion::default().sample_size(10)
  103. }
  104. criterion_group! {
  105. name = benches_snark;
  106. config = set_duration();
  107. targets = snark_encode_benchmark, snark_prove_benchmark, snark_verify_benchmark
  108. }
  109. criterion_main!(benches_snark);