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.

271 lines
8.9 KiB

2 years ago
2 years ago
2 years ago
2 years ago
  1. use arithmetic::{identity_permutation_mles, 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, sync::Arc, 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 ws = vec![Arc::new(DenseMultilinearExtension::rand(nv, &mut rng))];
  131. // identity map
  132. let perms = identity_permutation_mles(nv, 1);
  133. let proof =
  134. {
  135. let start = Instant::now();
  136. let mut transcript =
  137. <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
  138. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  139. let (proof, _q_x, _frac_poly) = <PolyIOP<Fr> as PermutationCheck<
  140. Bls12_381,
  141. KZG,
  142. >>::prove(
  143. &pcs_param, &ws, &ws, &perms, &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 fs = vec![Arc::new(f)];
  193. let gs = vec![Arc::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, _frac_poly) =
  199. <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::prove(
  200. &pcs_param,
  201. &fs,
  202. &gs,
  203. &mut transcript,
  204. )?;
  205. println!(
  206. "product check proving time for {} variables: {} ns",
  207. nv,
  208. start.elapsed().as_nanos() / repetition as u128
  209. );
  210. proof
  211. };
  212. {
  213. let poly_info = VPAuxInfo {
  214. max_degree: 2,
  215. num_variables: nv,
  216. phantom: PhantomData::default(),
  217. };
  218. let start = Instant::now();
  219. let mut transcript = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::init_transcript();
  220. transcript.append_message(b"testing", b"initializing transcript for testing")?;
  221. let _perm_check_sum_claim = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::verify(
  222. &proof,
  223. &poly_info,
  224. &mut transcript,
  225. )?;
  226. println!(
  227. "product check verification time for {} variables: {} ns",
  228. nv,
  229. start.elapsed().as_nanos() / repetition as u128
  230. );
  231. }
  232. println!("====================================");
  233. }
  234. Ok(())
  235. }