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.

121 lines
3.6 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 as 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 as 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(&decomm),
  48. black_box(vars.clone()),
  49. black_box(&inputs),
  50. black_box(&gens),
  51. black_box(&mut prover_transcript),
  52. );
  53. });
  54. });
  55. group.finish();
  56. }
  57. }
  58. fn snark_verify_benchmark(c: &mut Criterion) {
  59. for &s in [10, 12, 16].iter() {
  60. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  61. let mut group = c.benchmark_group("SNARK_verify_benchmark");
  62. group.plot_config(plot_config);
  63. let num_vars = (2 as usize).pow(s as u32);
  64. let num_cons = num_vars;
  65. let num_inputs = 10;
  66. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  67. // produce public parameters
  68. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  69. // produce a commitment to R1CS instance
  70. let (comm, decomm) = SNARK::encode(&inst, &gens);
  71. // produce a proof of satisfiability
  72. let mut prover_transcript = Transcript::new(b"example");
  73. let proof = SNARK::prove(&inst, &decomm, vars, &inputs, &gens, &mut prover_transcript);
  74. // verify the proof
  75. let name = format!("SNARK_verify_{}", num_cons);
  76. group.bench_function(&name, move |b| {
  77. b.iter(|| {
  78. let mut verifier_transcript = Transcript::new(b"example");
  79. assert!(proof
  80. .verify(
  81. black_box(&comm),
  82. black_box(&inputs),
  83. black_box(&mut verifier_transcript),
  84. black_box(&gens)
  85. )
  86. .is_ok());
  87. });
  88. });
  89. group.finish();
  90. }
  91. }
  92. fn set_duration() -> Criterion {
  93. Criterion::default().sample_size(10)
  94. }
  95. criterion_group! {
  96. name = benches_snark;
  97. config = set_duration();
  98. targets = snark_encode_benchmark, snark_prove_benchmark, snark_verify_benchmark
  99. }
  100. criterion_main!(benches_snark);