arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/poly-iop/benches/bench.rs
poly-iop/benches/bench.rsRaw
  1use arithmetic::{VPAuxInfo, VirtualPolynomial};
  2use ark_bls12_381::{Bls12_381, Fr};
  3use ark_poly::{DenseMultilinearExtension, MultilinearExtension};
  4use ark_std::test_rng;
  5use pcs::{prelude::KZGMultilinearPCS, PolynomialCommitmentScheme};
  6use poly_iop::prelude::{
  7    identity_permutation_mle, PermutationCheck, PolyIOP, PolyIOPErrors, SumCheck, ZeroCheck,
  8};
  9use std::{marker::PhantomData, rc::Rc, time::Instant};
 10
 11type KZG = KZGMultilinearPCS<Bls12_381>;
 12
 13fn main() -> Result<(), PolyIOPErrors> {
 14    bench_permutation_check()?;
 15    println!("\n\n");
 16    bench_sum_check()?;
 17    println!("\n\n");
 18    bench_zero_check()
 19}
 20
 21fn 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
 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
 43                println!(
 44                    "sum check proving time for {} variables and {} degree: {} ns",
 45                    nv,
 46                    degree,
 47                    start.elapsed().as_nanos() / repetition as u128
 48                );
 49                let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
 50                <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?
 51            };
 52
 53            {
 54                let start = Instant::now();
 55
 56                for _ in 0..repetition {
 57                    let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
 58                    let subclaim = <PolyIOP<Fr> as SumCheck<Fr>>::verify(
 59                        asserted_sum,
 60                        &proof,
 61                        &poly_info,
 62                        &mut transcript,
 63                    )?;
 64                    assert!(
 65                        poly.evaluate(&subclaim.point).unwrap() == subclaim.expected_evaluation,
 66                        "wrong subclaim"
 67                    );
 68                }
 69                println!(
 70                    "sum check verification time for {} variables and {} degree: {} ns",
 71                    nv,
 72                    degree,
 73                    start.elapsed().as_nanos() / repetition as u128
 74                );
 75            }
 76
 77            println!("====================================");
 78        }
 79    }
 80    Ok(())
 81}
 82
 83fn bench_zero_check() -> Result<(), PolyIOPErrors> {
 84    let mut rng = test_rng();
 85    for degree in 2..4 {
 86        for nv in 4..20 {
 87            let repetition = if nv < 10 {
 88                100
 89            } else if nv < 20 {
 90                50
 91            } else {
 92                10
 93            };
 94
 95            let poly = VirtualPolynomial::rand_zero(nv, (degree, degree + 1), 2, &mut rng)?;
 96            let poly_info = poly.aux_info.clone();
 97            let proof = {
 98                let start = Instant::now();
 99                let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
100                transcript.append_message(b"testing", b"initializing transcript for testing")?;
101                let proof = <PolyIOP<Fr> as ZeroCheck<Fr>>::prove(&poly, &mut transcript)?;
102
103                println!(
104                    "zero check proving time for {} variables and {} degree: {} ns",
105                    nv,
106                    degree,
107                    start.elapsed().as_nanos() / repetition as u128
108                );
109                proof
110            };
111
112            {
113                let start = Instant::now();
114                let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
115                transcript.append_message(b"testing", b"initializing transcript for testing")?;
116                let subclaim =
117                    <PolyIOP<Fr> as ZeroCheck<Fr>>::verify(&proof, &poly_info, &mut transcript)?
118                        .sum_check_sub_claim;
119                assert!(
120                    poly.evaluate(&subclaim.point)? == subclaim.expected_evaluation,
121                    "wrong subclaim"
122                );
123                println!(
124                    "zero check verification time for {} variables and {} degree: {} ns",
125                    nv,
126                    degree,
127                    start.elapsed().as_nanos() / repetition as u128
128                );
129            }
130
131            println!("====================================");
132        }
133    }
134    Ok(())
135}
136
137fn bench_permutation_check() -> Result<(), PolyIOPErrors> {
138    let mut rng = test_rng();
139
140    for nv in 4..20 {
141        let srs = KZG::gen_srs_for_testing(&mut rng, nv + 1)?;
142        let (pcs_param, _) = KZG::trim(&srs, nv + 1, Some(nv + 1))?;
143
144        let repetition = if nv < 10 {
145            100
146        } else if nv < 20 {
147            50
148        } else {
149            10
150        };
151
152        let w = Rc::new(DenseMultilinearExtension::rand(nv, &mut rng));
153
154        // s_perm is the identity map
155        let s_perm = identity_permutation_mle(nv);
156
157        let proof = {
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
163            let (proof, _q_x) = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::prove(
164                &pcs_param,
165                &w,
166                &w,
167                &s_perm,
168                &mut transcript,
169            )?;
170
171            println!(
172                "permutation check proving time for {} variables: {} ns",
173                nv,
174                start.elapsed().as_nanos() / repetition as u128
175            );
176            proof
177        };
178
179        {
180            let poly_info = VPAuxInfo {
181                max_degree: 2,
182                num_variables: nv,
183                phantom: PhantomData::default(),
184            };
185
186            let start = Instant::now();
187            let mut transcript =
188                <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
189            transcript.append_message(b"testing", b"initializing transcript for testing")?;
190            let _perm_check_sum_claim = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::verify(
191                &proof,
192                &poly_info,
193                &mut transcript,
194            )?;
195            println!(
196                "permutation check verification time for {} variables: {} ns",
197                nv,
198                start.elapsed().as_nanos() / repetition as u128
199            );
200        }
201
202        println!("====================================");
203    }
204
205    Ok(())
206}