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.

131 lines
3.8 KiB

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