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.

162 lines
4.8 KiB

4 years ago
  1. #![allow(non_snake_case)]
  2. extern crate byteorder;
  3. extern crate core;
  4. extern crate criterion;
  5. extern crate digest;
  6. extern crate libspartan;
  7. extern crate merlin;
  8. extern crate rand;
  9. extern crate sha3;
  10. use libspartan::commitments::Commitments;
  11. use libspartan::commitments::MultiCommitGens;
  12. use libspartan::dense_mlpoly::DensePolynomial;
  13. use libspartan::math::Math;
  14. use libspartan::nizk::DotProductProof;
  15. use libspartan::scalar::Scalar;
  16. use libspartan::sumcheck::ZKSumcheckInstanceProof;
  17. use libspartan::transcript::ProofTranscript;
  18. use merlin::Transcript;
  19. use rand::rngs::OsRng;
  20. use criterion::*;
  21. fn prove_benchmark(c: &mut Criterion) {
  22. for &s in [10, 12, 16, 20].iter() {
  23. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  24. let mut group = c.benchmark_group("zksumcheck_prove_benchmark");
  25. group.plot_config(plot_config);
  26. // produce tables
  27. let gens_n = MultiCommitGens::new(3, b"test-m");
  28. let gens_1 = MultiCommitGens::new(1, b"test-1");
  29. let num_rounds = s;
  30. let n = s.pow2();
  31. let mut csprng: OsRng = OsRng;
  32. let vec_A = (0..n)
  33. .map(|_i| Scalar::random(&mut csprng))
  34. .collect::<Vec<Scalar>>();
  35. let vec_B = (0..n)
  36. .map(|_i| Scalar::random(&mut csprng))
  37. .collect::<Vec<Scalar>>();
  38. let claim = DotProductProof::compute_dotproduct(&vec_A, &vec_B);
  39. let mut poly_A = DensePolynomial::new(vec_A);
  40. let mut poly_B = DensePolynomial::new(vec_B);
  41. let blind_claim = Scalar::random(&mut csprng);
  42. let comb_func =
  43. |poly_A_comp: &Scalar, poly_B_comp: &Scalar| -> Scalar { poly_A_comp * poly_B_comp };
  44. let name = format!("zksumcheck_prove_{}", n);
  45. group.bench_function(&name, move |b| {
  46. b.iter(|| {
  47. let mut random_tape = {
  48. let mut csprng: OsRng = OsRng;
  49. let mut tape = Transcript::new(b"proof");
  50. tape.append_scalar(b"init_randomness", &Scalar::random(&mut csprng));
  51. tape
  52. };
  53. let mut prover_transcript = Transcript::new(b"example");
  54. ZKSumcheckInstanceProof::prove_quad(
  55. black_box(&claim),
  56. black_box(&blind_claim),
  57. black_box(num_rounds),
  58. black_box(&mut poly_A),
  59. black_box(&mut poly_B),
  60. black_box(comb_func),
  61. black_box(&gens_1),
  62. black_box(&gens_n),
  63. black_box(&mut prover_transcript),
  64. black_box(&mut random_tape),
  65. )
  66. });
  67. });
  68. group.finish();
  69. }
  70. }
  71. fn verify_benchmark(c: &mut Criterion) {
  72. for &s in [10, 12, 16, 20].iter() {
  73. let plot_config = PlotConfiguration::default().summary_scale(AxisScale::Logarithmic);
  74. let mut group = c.benchmark_group("zksumcheck_verify_benchmark");
  75. group.plot_config(plot_config);
  76. // produce tables
  77. let gens_n = MultiCommitGens::new(3, b"test-m");
  78. let gens_1 = MultiCommitGens::new(1, b"test-1");
  79. let num_rounds = s;
  80. let n = s.pow2();
  81. let mut csprng: OsRng = OsRng;
  82. let vec_A = (0..n)
  83. .map(|_i| Scalar::random(&mut csprng))
  84. .collect::<Vec<Scalar>>();
  85. let vec_B = (0..n)
  86. .map(|_i| Scalar::random(&mut csprng))
  87. .collect::<Vec<Scalar>>();
  88. let claim = DotProductProof::compute_dotproduct(&vec_A, &vec_B);
  89. let mut poly_A = DensePolynomial::new(vec_A);
  90. let mut poly_B = DensePolynomial::new(vec_B);
  91. let blind_claim = Scalar::random(&mut csprng);
  92. let comb_func =
  93. |poly_A_comp: &Scalar, poly_B_comp: &Scalar| -> Scalar { poly_A_comp * poly_B_comp };
  94. let mut random_tape = {
  95. let mut csprng: OsRng = OsRng;
  96. let mut tape = Transcript::new(b"proof");
  97. tape.append_scalar(b"init_randomness", &Scalar::random(&mut csprng));
  98. tape
  99. };
  100. let mut prover_transcript = Transcript::new(b"example");
  101. let (proof, _r, _v, _blind_post_claim) = ZKSumcheckInstanceProof::prove_quad(
  102. &claim,
  103. &blind_claim,
  104. num_rounds,
  105. &mut poly_A,
  106. &mut poly_B,
  107. comb_func,
  108. &gens_1,
  109. &gens_n,
  110. &mut prover_transcript,
  111. &mut random_tape,
  112. );
  113. let name = format!("zksumcheck_verify_{}", n);
  114. let degree_bound = 2;
  115. let comm_claim = claim.commit(&blind_claim, &gens_1).compress();
  116. group.bench_function(&name, move |b| {
  117. b.iter(|| {
  118. let mut verifier_transcript = Transcript::new(b"example");
  119. assert!(proof
  120. .verify(
  121. black_box(&comm_claim),
  122. black_box(num_rounds),
  123. black_box(degree_bound),
  124. black_box(&gens_1),
  125. black_box(&gens_n),
  126. black_box(&mut verifier_transcript)
  127. )
  128. .is_ok())
  129. });
  130. });
  131. group.finish();
  132. }
  133. }
  134. fn set_duration() -> Criterion {
  135. Criterion::default().sample_size(10)
  136. // .measurement_time(Duration::new(0, 50000000))
  137. }
  138. criterion_group! {
  139. name = benches_r1cs;
  140. config = set_duration();
  141. targets = verify_benchmark, prove_benchmark
  142. }
  143. criterion_main!(benches_r1cs);