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.

135 lines
3.8 KiB

  1. extern crate libspartan;
  2. extern crate merlin;
  3. use libspartan::{
  4. parameters::poseidon_params, poseidon_transcript::PoseidonTranscript, Instance, SNARKGens, SNARK,
  5. };
  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 params = poseidon_params();
  37. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  38. // produce public parameters
  39. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  40. // produce a commitment to R1CS instance
  41. let (comm, decomm) = SNARK::encode(&inst, &gens);
  42. // produce a proof
  43. let name = format!("SNARK_prove_{}", num_cons);
  44. group.bench_function(&name, move |b| {
  45. b.iter(|| {
  46. let mut prover_transcript = PoseidonTranscript::new(&params);
  47. SNARK::prove(
  48. black_box(&inst),
  49. black_box(&comm),
  50. black_box(&decomm),
  51. black_box(vars.clone()),
  52. black_box(&inputs),
  53. black_box(&gens),
  54. black_box(&mut prover_transcript),
  55. );
  56. });
  57. });
  58. group.finish();
  59. }
  60. }
  61. fn snark_verify_benchmark(c: &mut Criterion) {
  62. for &s in [10, 12, 16].iter() {
  63. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  64. let mut group = c.benchmark_group("SNARK_verify_benchmark");
  65. group.plot_config(plot_config);
  66. let params = poseidon_params();
  67. let num_vars = (2_usize).pow(s as u32);
  68. let num_cons = num_vars;
  69. let num_inputs = 10;
  70. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  71. // produce public parameters
  72. let gens = SNARKGens::new(num_cons, num_vars, num_inputs, num_cons);
  73. // produce a commitment to R1CS instance
  74. let (comm, decomm) = SNARK::encode(&inst, &gens);
  75. // produce a proof of satisfiability
  76. let mut prover_transcript = PoseidonTranscript::new(&params);
  77. let proof = SNARK::prove(
  78. &inst,
  79. &comm,
  80. &decomm,
  81. vars,
  82. &inputs,
  83. &gens,
  84. &mut prover_transcript,
  85. );
  86. // verify the proof
  87. let name = format!("SNARK_verify_{}", num_cons);
  88. group.bench_function(&name, move |b| {
  89. b.iter(|| {
  90. let mut verifier_transcript = PoseidonTranscript::new(&params);
  91. assert!(proof
  92. .verify(
  93. black_box(&comm),
  94. black_box(&inputs),
  95. black_box(&mut verifier_transcript),
  96. black_box(&gens)
  97. )
  98. .is_ok());
  99. });
  100. });
  101. group.finish();
  102. }
  103. }
  104. fn set_duration() -> Criterion {
  105. Criterion::default().sample_size(10)
  106. }
  107. criterion_group! {
  108. name = benches_snark;
  109. config = set_duration();
  110. targets = snark_verify_benchmark
  111. }
  112. criterion_main!(benches_snark);