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.

149 lines
4.6 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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 sha3;
  8. use std::time::{Duration, SystemTime};
  9. use libspartan::{
  10. parameters::POSEIDON_PARAMETERS_FR_377, poseidon_transcript::PoseidonTranscript, Instance,
  11. NIZKGens, NIZK,
  12. };
  13. use criterion::*;
  14. fn nizk_prove_benchmark(c: &mut Criterion) {
  15. for &s in [24, 28, 30].iter() {
  16. let mut group = c.benchmark_group("R1CS_prove_benchmark");
  17. let num_vars = (2_usize).pow(s as u32);
  18. let num_cons = num_vars;
  19. let num_inputs = 10;
  20. let start = SystemTime::now();
  21. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  22. let end = SystemTime::now();
  23. let duration = end.duration_since(start).unwrap();
  24. println!(
  25. "Generating r1cs instance with {} constraints took {} ms",
  26. num_cons,
  27. duration.as_millis()
  28. );
  29. let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
  30. let name = format!("R1CS_prove_{}", num_vars);
  31. group
  32. .measurement_time(Duration::from_secs(60))
  33. .bench_function(&name, move |b| {
  34. b.iter(|| {
  35. let mut prover_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  36. NIZK::prove(
  37. black_box(&inst),
  38. black_box(vars.clone()),
  39. black_box(&inputs),
  40. black_box(&gens),
  41. black_box(&mut prover_transcript),
  42. );
  43. });
  44. });
  45. group.finish();
  46. }
  47. }
  48. fn nizk_verify_benchmark(c: &mut Criterion) {
  49. for &s in [4, 6, 8, 10, 12, 16, 20, 24, 28, 30].iter() {
  50. let mut group = c.benchmark_group("R1CS_verify_benchmark");
  51. let num_vars = (2_usize).pow(s as u32);
  52. let num_cons = num_vars;
  53. // these are the public io
  54. let num_inputs = 10;
  55. let start = SystemTime::now();
  56. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  57. let end = SystemTime::now();
  58. let duration = end.duration_since(start).unwrap();
  59. println!(
  60. "Generating r1cs instance with {} constraints took {} ms",
  61. num_cons,
  62. duration.as_millis()
  63. );
  64. let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
  65. // produce a proof of satisfiability
  66. let mut prover_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  67. let proof = NIZK::prove(&inst, vars, &inputs, &gens, &mut prover_transcript);
  68. let name = format!("R1CS_verify_{}", num_cons);
  69. group
  70. .measurement_time(Duration::from_secs(60))
  71. .bench_function(&name, move |b| {
  72. b.iter(|| {
  73. let mut verifier_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  74. assert!(proof
  75. .verify(
  76. black_box(&inst),
  77. black_box(&inputs),
  78. black_box(&mut verifier_transcript),
  79. black_box(&gens),
  80. )
  81. .is_ok());
  82. });
  83. });
  84. group.finish();
  85. }
  86. }
  87. fn nizk_verify_groth16_benchmark(c: &mut Criterion) {
  88. for &s in [4, 6, 8, 10, 12, 16, 20, 24, 28, 30].iter() {
  89. let mut group = c.benchmark_group("R1CS_verify_groth16_benchmark");
  90. let num_vars = (2_usize).pow(s as u32);
  91. let num_cons = num_vars;
  92. // these are the public io
  93. let num_inputs = 10;
  94. let start = SystemTime::now();
  95. let (inst, vars, inputs) = Instance::produce_synthetic_r1cs(num_cons, num_vars, num_inputs);
  96. let end = SystemTime::now();
  97. let duration = end.duration_since(start).unwrap();
  98. println!(
  99. "Generating r1cs instance with {} constraints took {} ms",
  100. num_cons,
  101. duration.as_millis()
  102. );
  103. // produce a proof of satisfiability
  104. let mut prover_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  105. let gens = NIZKGens::new(num_cons, num_vars, num_inputs);
  106. let proof = NIZK::prove(&inst, vars, &inputs, &gens, &mut prover_transcript);
  107. let name = format!("R1CS_verify_groth16_{}", num_cons);
  108. group
  109. .measurement_time(Duration::from_secs(60))
  110. .bench_function(&name, move |b| {
  111. b.iter(|| {
  112. let mut verifier_transcript = PoseidonTranscript::new(&POSEIDON_PARAMETERS_FR_377);
  113. assert!(proof
  114. .verify_groth16(
  115. black_box(&inst),
  116. black_box(&inputs),
  117. black_box(&mut verifier_transcript),
  118. black_box(&gens)
  119. )
  120. .is_ok());
  121. });
  122. });
  123. group.finish();
  124. }
  125. }
  126. fn set_duration() -> Criterion {
  127. Criterion::default().sample_size(2)
  128. }
  129. criterion_group! {
  130. name = benches_nizk;
  131. config = set_duration();
  132. targets = nizk_prove_benchmark, nizk_verify_benchmark, nizk_verify_groth16_benchmark
  133. }
  134. criterion_main!(benches_nizk);