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.

91 lines
2.5 KiB

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