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.

206 lines
6.9 KiB

  1. use arithmetic::{VPAuxInfo, VirtualPolynomial};
  2. use ark_bls12_381::{Bls12_381, Fr};
  3. use ark_poly::{DenseMultilinearExtension, MultilinearExtension};
  4. use ark_std::test_rng;
  5. use pcs::{prelude::KZGMultilinearPCS, PolynomialCommitmentScheme};
  6. use poly_iop::prelude::{
  7. identity_permutation_mle, PermutationCheck, PolyIOP, PolyIOPErrors, SumCheck, ZeroCheck,
  8. };
  9. use std::{marker::PhantomData, rc::Rc, time::Instant};
  10. type KZG = KZGMultilinearPCS<Bls12_381>;
  11. fn main() -> Result<(), PolyIOPErrors> {
  12. bench_permutation_check()?;
  13. println!("\n\n");
  14. bench_sum_check()?;
  15. println!("\n\n");
  16. bench_zero_check()
  17. }
  18. fn bench_sum_check() -> Result<(), PolyIOPErrors> {
  19. let mut rng = test_rng();
  20. for degree in 2..4 {
  21. for nv in 4..25 {
  22. let repetition = if nv < 10 {
  23. 100
  24. } else if nv < 20 {
  25. 50
  26. } else {
  27. 10
  28. };
  29. let (poly, asserted_sum) =
  30. VirtualPolynomial::rand(nv, (degree, degree + 1), 2, &mut rng)?;
  31. let poly_info = poly.aux_info.clone();
  32. let proof = {
  33. let start = Instant::now();
  34. for _ in 0..repetition {
  35. let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
  36. let _proof = <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?;
  37. }
  38. println!(
  39. "sum check proving time for {} variables and {} degree: {} ns",
  40. nv,
  41. degree,
  42. start.elapsed().as_nanos() / repetition as u128
  43. );
  44. let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
  45. <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?
  46. };
  47. {
  48. let start = Instant::now();
  49. for _ in 0..repetition {
  50. let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
  51. let subclaim = <PolyIOP<Fr> as SumCheck<Fr>>::verify(
  52. asserted_sum,
  53. &proof,
  54. &poly_info,
  55. &mut transcript,
  56. )?;
  57. assert!(
  58. poly.evaluate(&subclaim.point).unwrap() == subclaim.expected_evaluation,
  59. "wrong subclaim"
  60. );
  61. }
  62. println!(
  63. "sum check verification time for {} variables and {} degree: {} ns",
  64. nv,
  65. degree,
  66. start.elapsed().as_nanos() / repetition as u128
  67. );
  68. }
  69. println!("====================================");
  70. }
  71. }
  72. Ok(())
  73. }
  74. fn bench_zero_check() -> Result<(), PolyIOPErrors> {
  75. let mut rng = test_rng();
  76. for degree in 2..4 {
  77. for nv in 4..20 {
  78. let repetition = if nv < 10 {
  79. 100
  80. } else if nv < 20 {
  81. 50
  82. } else {
  83. 10
  84. };
  85. let poly = VirtualPolynomial::rand_zero(nv, (degree, degree + 1), 2, &mut rng)?;
  86. let poly_info = poly.aux_info.clone();
  87. let proof = {
  88. let start = Instant::now();
  89. let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
  90. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  91. let proof = <PolyIOP<Fr> as ZeroCheck<Fr>>::prove(&poly, &mut transcript)?;
  92. println!(
  93. "zero check proving time for {} variables and {} degree: {} ns",
  94. nv,
  95. degree,
  96. start.elapsed().as_nanos() / repetition as u128
  97. );
  98. proof
  99. };
  100. {
  101. let start = Instant::now();
  102. let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
  103. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  104. let subclaim =
  105. <PolyIOP<Fr> as ZeroCheck<Fr>>::verify(&proof, &poly_info, &mut transcript)?
  106. .sum_check_sub_claim;
  107. assert!(
  108. poly.evaluate(&subclaim.point)? == subclaim.expected_evaluation,
  109. "wrong subclaim"
  110. );
  111. println!(
  112. "zero check verification time for {} variables and {} degree: {} ns",
  113. nv,
  114. degree,
  115. start.elapsed().as_nanos() / repetition as u128
  116. );
  117. }
  118. println!("====================================");
  119. }
  120. }
  121. Ok(())
  122. }
  123. fn bench_permutation_check() -> Result<(), PolyIOPErrors> {
  124. let mut rng = test_rng();
  125. for nv in 4..20 {
  126. let srs = KZG::gen_srs_for_testing(&mut rng, nv + 1)?;
  127. let (pcs_param, _) = KZG::trim(&srs, nv + 1, Some(nv + 1))?;
  128. let repetition = if nv < 10 {
  129. 100
  130. } else if nv < 20 {
  131. 50
  132. } else {
  133. 10
  134. };
  135. let w = Rc::new(DenseMultilinearExtension::rand(nv, &mut rng));
  136. // s_perm is the identity map
  137. let s_perm = identity_permutation_mle(nv);
  138. let proof = {
  139. let start = Instant::now();
  140. let mut transcript =
  141. <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
  142. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  143. let (proof, _q_x) = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::prove(
  144. &pcs_param,
  145. &w,
  146. &w,
  147. &s_perm,
  148. &mut transcript,
  149. )?;
  150. println!(
  151. "permutation check proving time for {} variables: {} ns",
  152. nv,
  153. start.elapsed().as_nanos() / repetition as u128
  154. );
  155. proof
  156. };
  157. {
  158. let poly_info = VPAuxInfo {
  159. max_degree: 2,
  160. num_variables: nv,
  161. phantom: PhantomData::default(),
  162. };
  163. let start = Instant::now();
  164. let mut transcript =
  165. <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
  166. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  167. let _perm_check_sum_claim = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::verify(
  168. &proof,
  169. &poly_info,
  170. &mut transcript,
  171. )?;
  172. println!(
  173. "permutation check verification time for {} variables: {} ns",
  174. nv,
  175. start.elapsed().as_nanos() / repetition as u128
  176. );
  177. }
  178. println!("====================================");
  179. }
  180. Ok(())
  181. }