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.

276 lines
9.1 KiB

  1. use arithmetic::{identity_permutation_mle, 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::MultilinearKzgPCS, PolynomialCommitmentScheme};
  6. use poly_iop::prelude::{
  7. PermutationCheck, PolyIOP, PolyIOPErrors, ProductCheck, SumCheck, ZeroCheck,
  8. };
  9. use std::{marker::PhantomData, rc::Rc, time::Instant};
  10. type KZG = MultilinearKzgPCS<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_prod_check()?;
  17. println!("\n\n");
  18. bench_zero_check()
  19. }
  20. fn bench_sum_check() -> Result<(), PolyIOPErrors> {
  21. let mut rng = test_rng();
  22. for degree in 2..4 {
  23. for nv in 4..25 {
  24. let repetition = if nv < 10 {
  25. 100
  26. } else if nv < 20 {
  27. 50
  28. } else {
  29. 10
  30. };
  31. let (poly, asserted_sum) =
  32. VirtualPolynomial::rand(nv, (degree, degree + 1), 2, &mut rng)?;
  33. let poly_info = poly.aux_info.clone();
  34. let proof = {
  35. let start = Instant::now();
  36. for _ in 0..repetition {
  37. let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
  38. let _proof = <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?;
  39. }
  40. println!(
  41. "sum check proving time for {} variables and {} degree: {} ns",
  42. nv,
  43. degree,
  44. start.elapsed().as_nanos() / repetition as u128
  45. );
  46. let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
  47. <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?
  48. };
  49. {
  50. let start = Instant::now();
  51. for _ in 0..repetition {
  52. let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
  53. let subclaim = <PolyIOP<Fr> as SumCheck<Fr>>::verify(
  54. asserted_sum,
  55. &proof,
  56. &poly_info,
  57. &mut transcript,
  58. )?;
  59. assert!(
  60. poly.evaluate(&subclaim.point).unwrap() == subclaim.expected_evaluation,
  61. "wrong subclaim"
  62. );
  63. }
  64. println!(
  65. "sum check verification time for {} variables and {} degree: {} ns",
  66. nv,
  67. degree,
  68. start.elapsed().as_nanos() / repetition as u128
  69. );
  70. }
  71. println!("====================================");
  72. }
  73. }
  74. Ok(())
  75. }
  76. fn bench_zero_check() -> Result<(), PolyIOPErrors> {
  77. let mut rng = test_rng();
  78. for degree in 2..4 {
  79. for nv in 4..20 {
  80. let repetition = if nv < 10 {
  81. 100
  82. } else if nv < 20 {
  83. 50
  84. } else {
  85. 10
  86. };
  87. let poly = VirtualPolynomial::rand_zero(nv, (degree, degree + 1), 2, &mut rng)?;
  88. let poly_info = poly.aux_info.clone();
  89. let proof = {
  90. let start = Instant::now();
  91. let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
  92. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  93. let proof = <PolyIOP<Fr> as ZeroCheck<Fr>>::prove(&poly, &mut transcript)?;
  94. println!(
  95. "zero check proving time for {} variables and {} degree: {} ns",
  96. nv,
  97. degree,
  98. start.elapsed().as_nanos() / repetition as u128
  99. );
  100. proof
  101. };
  102. {
  103. let start = Instant::now();
  104. let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
  105. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  106. let zero_subclaim =
  107. <PolyIOP<Fr> as ZeroCheck<Fr>>::verify(&proof, &poly_info, &mut transcript)?;
  108. assert!(
  109. poly.evaluate(&zero_subclaim.point)? == zero_subclaim.expected_evaluation,
  110. "wrong subclaim"
  111. );
  112. println!(
  113. "zero check verification time for {} variables and {} degree: {} ns",
  114. nv,
  115. degree,
  116. start.elapsed().as_nanos() / repetition as u128
  117. );
  118. }
  119. println!("====================================");
  120. }
  121. }
  122. Ok(())
  123. }
  124. fn bench_permutation_check() -> Result<(), PolyIOPErrors> {
  125. let mut rng = test_rng();
  126. for nv in 4..20 {
  127. let srs = KZG::gen_srs_for_testing(&mut rng, nv + 1)?;
  128. let (pcs_param, _) = KZG::trim(&srs, nv + 1, Some(nv + 1))?;
  129. let repetition = if nv < 10 {
  130. 100
  131. } else if nv < 20 {
  132. 50
  133. } else {
  134. 10
  135. };
  136. let w = Rc::new(DenseMultilinearExtension::rand(nv, &mut rng));
  137. // s_perm is the identity map
  138. let s_perm = identity_permutation_mle(nv);
  139. let proof = {
  140. let start = Instant::now();
  141. let mut transcript =
  142. <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
  143. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  144. let (proof, _q_x) = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::prove(
  145. &pcs_param,
  146. &w,
  147. &w,
  148. &s_perm,
  149. &mut transcript,
  150. )?;
  151. println!(
  152. "permutation check proving time for {} variables: {} ns",
  153. nv,
  154. start.elapsed().as_nanos() / repetition as u128
  155. );
  156. proof
  157. };
  158. {
  159. let poly_info = VPAuxInfo {
  160. max_degree: 2,
  161. num_variables: nv,
  162. phantom: PhantomData::default(),
  163. };
  164. let start = Instant::now();
  165. let mut transcript =
  166. <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
  167. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  168. let _perm_check_sum_claim = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::verify(
  169. &proof,
  170. &poly_info,
  171. &mut transcript,
  172. )?;
  173. println!(
  174. "permutation check verification time for {} variables: {} ns",
  175. nv,
  176. start.elapsed().as_nanos() / repetition as u128
  177. );
  178. }
  179. println!("====================================");
  180. }
  181. Ok(())
  182. }
  183. fn bench_prod_check() -> Result<(), PolyIOPErrors> {
  184. let mut rng = test_rng();
  185. for nv in 4..20 {
  186. let srs = KZG::gen_srs_for_testing(&mut rng, nv + 1)?;
  187. let (pcs_param, _) = KZG::trim(&srs, nv + 1, Some(nv + 1))?;
  188. let repetition = if nv < 10 {
  189. 100
  190. } else if nv < 20 {
  191. 50
  192. } else {
  193. 10
  194. };
  195. let f: DenseMultilinearExtension<Fr> = DenseMultilinearExtension::rand(nv, &mut rng);
  196. let mut g = f.clone();
  197. g.evaluations.reverse();
  198. let f = Rc::new(f);
  199. let g = Rc::new(g);
  200. let proof = {
  201. let start = Instant::now();
  202. let mut transcript = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::init_transcript();
  203. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  204. let (proof, _prod_x) = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::prove(
  205. &pcs_param,
  206. &f,
  207. &g,
  208. &mut transcript,
  209. )?;
  210. println!(
  211. "product check proving time for {} variables: {} ns",
  212. nv,
  213. start.elapsed().as_nanos() / repetition as u128
  214. );
  215. proof
  216. };
  217. {
  218. let poly_info = VPAuxInfo {
  219. max_degree: 2,
  220. num_variables: nv,
  221. phantom: PhantomData::default(),
  222. };
  223. let start = Instant::now();
  224. let mut transcript = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::init_transcript();
  225. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  226. let _perm_check_sum_claim = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::verify(
  227. &proof,
  228. &poly_info,
  229. &mut transcript,
  230. )?;
  231. println!(
  232. "product check verification time for {} variables: {} ns",
  233. nv,
  234. start.elapsed().as_nanos() / repetition as u128
  235. );
  236. }
  237. println!("====================================");
  238. }
  239. Ok(())
  240. }