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.

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