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.

85 lines
2.3 KiB

1 year ago
1 year ago
  1. use ark_bls12_381::{Bls12_381, Fr};
  2. use ark_ff::UniformRand;
  3. use ark_poly::{DenseMultilinearExtension, MultilinearExtension};
  4. use ark_std::{sync::Arc, test_rng};
  5. use std::time::Instant;
  6. use subroutines::pcs::{
  7. prelude::{MultilinearKzgPCS, PCSError, PolynomialCommitmentScheme},
  8. StructuredReferenceString,
  9. };
  10. fn main() -> Result<(), PCSError> {
  11. bench_pcs()
  12. }
  13. fn bench_pcs() -> Result<(), PCSError> {
  14. let mut rng = test_rng();
  15. // normal polynomials
  16. let uni_params = MultilinearKzgPCS::<Bls12_381>::gen_srs_for_testing(&mut rng, 24)?;
  17. for nv in 4..25 {
  18. let repetition = if nv < 10 {
  19. 10
  20. } else if nv < 20 {
  21. 5
  22. } else {
  23. 2
  24. };
  25. let poly = Arc::new(DenseMultilinearExtension::rand(nv, &mut rng));
  26. let (ck, vk) = uni_params.trim(nv)?;
  27. let point: Vec<_> = (0..nv).map(|_| Fr::rand(&mut rng)).collect();
  28. // commit
  29. let com = {
  30. let start = Instant::now();
  31. for _ in 0..repetition {
  32. let _commit = MultilinearKzgPCS::commit(&ck, &poly)?;
  33. }
  34. println!(
  35. "KZG commit for {} variables: {} ns",
  36. nv,
  37. start.elapsed().as_nanos() / repetition as u128
  38. );
  39. MultilinearKzgPCS::commit(&ck, &poly)?
  40. };
  41. // open
  42. let (proof, value) = {
  43. let start = Instant::now();
  44. for _ in 0..repetition {
  45. let _open = MultilinearKzgPCS::open(&ck, &poly, &point)?;
  46. }
  47. println!(
  48. "KZG open for {} variables: {} ns",
  49. nv,
  50. start.elapsed().as_nanos() / repetition as u128
  51. );
  52. MultilinearKzgPCS::open(&ck, &poly, &point)?
  53. };
  54. // verify
  55. {
  56. let start = Instant::now();
  57. for _ in 0..repetition {
  58. assert!(MultilinearKzgPCS::verify(
  59. &vk, &com, &point, &value, &proof
  60. )?);
  61. }
  62. println!(
  63. "KZG verify for {} variables: {} ns",
  64. nv,
  65. start.elapsed().as_nanos() / repetition as u128
  66. );
  67. }
  68. println!("====================================");
  69. }
  70. Ok(())
  71. }