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.

213 lines
7.1 KiB

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