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.

130 lines
3.7 KiB

  1. extern crate libspartan;
  2. extern crate merlin;
  3. use libspartan::{Instance, SNARKGens, SNARK};
  4. use merlin::Transcript;
  5. use criterion::*;
  6. fn snark_encode_benchmark(c: &mut Criterion) {
  7. for &s in [10, 12, 16].iter() {
  8. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  9. let mut group = c.benchmark_group("SNARK_encode_benchmark");
  10. group.plot_config(plot_config);
  11. let num_vars = (2_usize).pow(s as u32);
  12. let num_cons = num_vars;
  13. let num_inputs = 10;
  14. let (inst, _vars, _inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  15. // produce public parameters
  16. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  17. // produce a commitment to R1CS instance
  18. let name = format!("SNARK_encode_{}", num_cons);
  19. group.bench_function(&name, move |b| {
  20. b.iter(|| {
  21. SNARK::encode(black_box(&inst), black_box(&gens));
  22. });
  23. });
  24. group.finish();
  25. }
  26. }
  27. fn snark_prove_benchmark(c: &mut Criterion) {
  28. for &s in [10, 12, 16].iter() {
  29. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  30. let mut group = c.benchmark_group("SNARK_prove_benchmark");
  31. group.plot_config(plot_config);
  32. let num_vars = (2_usize).pow(s as u32);
  33. let num_cons = num_vars;
  34. let num_inputs = 10;
  35. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  36. // produce public parameters
  37. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  38. // produce a commitment to R1CS instance
  39. let (comm, decomm) = SNARK::encode(&inst, &gens);
  40. // produce a proof
  41. let name = format!("SNARK_prove_{}", num_cons);
  42. group.bench_function(&name, move |b| {
  43. b.iter(|| {
  44. let mut prover_transcript = Transcript::new(b"example");
  45. SNARK::prove(
  46. black_box(&inst),
  47. black_box(&comm),
  48. black_box(&decomm),
  49. black_box(vars.clone()),
  50. black_box(&inputs),
  51. black_box(&gens),
  52. black_box(&mut prover_transcript),
  53. );
  54. });
  55. });
  56. group.finish();
  57. }
  58. }
  59. fn snark_verify_benchmark(c: &mut Criterion) {
  60. for &s in [10, 12, 16].iter() {
  61. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  62. let mut group = c.benchmark_group("SNARK_verify_benchmark");
  63. group.plot_config(plot_config);
  64. let num_vars = (2_usize).pow(s as u32);
  65. let num_cons = num_vars;
  66. let num_inputs = 10;
  67. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  68. // produce public parameters
  69. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  70. // produce a commitment to R1CS instance
  71. let (comm, decomm) = SNARK::encode(&inst, &gens);
  72. // produce a proof of satisfiability
  73. let mut prover_transcript = Transcript::new(b"example");
  74. let proof = SNARK::prove(
  75. &inst,
  76. &comm,
  77. &decomm,
  78. vars,
  79. &inputs,
  80. &gens,
  81. &mut prover_transcript,
  82. );
  83. // verify the proof
  84. let name = format!("SNARK_verify_{}", num_cons);
  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(&inputs),
  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);