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.

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