arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/poly-iop/benches/bench.rs
poly-iop/benches/bench.rsRaw
  1use arithmetic::{identity_permutation_mle, VPAuxInfo, VirtualPolynomial};
  2use ark_bls12_381::{Bls12_381, Fr};
  3use ark_poly::{DenseMultilinearExtension, MultilinearExtension};
  4use ark_std::test_rng;
  5use pcs::{prelude::MultilinearKzgPCS, PolynomialCommitmentScheme};
  6use poly_iop::prelude::{
  7    PermutationCheck, PolyIOP, PolyIOPErrors, ProductCheck, SumCheck, ZeroCheck,
  8};
  9use std::{marker::PhantomData, rc::Rc, time::Instant};
 10
 11type KZG = MultilinearKzgPCS<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_prod_check()?;
 19    println!("\n\n");
 20    bench_zero_check()
 21}
 22
 23fn bench_sum_check() -> Result<(), PolyIOPErrors> {
 24    let mut rng = test_rng();
 25    for degree in 2..4 {
 26        for nv in 4..25 {
 27            let repetition = if nv < 10 {
 28                100
 29            } else if nv < 20 {
 30                50
 31            } else {
 32                10
 33            };
 34
 35            let (poly, asserted_sum) =
 36                VirtualPolynomial::rand(nv, (degree, degree + 1), 2, &mut rng)?;
 37            let poly_info = poly.aux_info.clone();
 38            let proof = {
 39                let start = Instant::now();
 40                for _ in 0..repetition {
 41                    let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
 42                    let _proof = <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?;
 43                }
 44
 45                println!(
 46                    "sum check proving time for {} variables and {} degree: {} ns",
 47                    nv,
 48                    degree,
 49                    start.elapsed().as_nanos() / repetition as u128
 50                );
 51                let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
 52                <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?
 53            };
 54
 55            {
 56                let start = Instant::now();
 57
 58                for _ in 0..repetition {
 59                    let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
 60                    let subclaim = <PolyIOP<Fr> as SumCheck<Fr>>::verify(
 61                        asserted_sum,
 62                        &proof,
 63                        &poly_info,
 64                        &mut transcript,
 65                    )?;
 66                    assert!(
 67                        poly.evaluate(&subclaim.point).unwrap() == subclaim.expected_evaluation,
 68                        "wrong subclaim"
 69                    );
 70                }
 71                println!(
 72                    "sum check verification time for {} variables and {} degree: {} ns",
 73                    nv,
 74                    degree,
 75                    start.elapsed().as_nanos() / repetition as u128
 76                );
 77            }
 78
 79            println!("====================================");
 80        }
 81    }
 82    Ok(())
 83}
 84
 85fn bench_zero_check() -> Result<(), PolyIOPErrors> {
 86    let mut rng = test_rng();
 87    for degree in 2..4 {
 88        for nv in 4..20 {
 89            let repetition = if nv < 10 {
 90                100
 91            } else if nv < 20 {
 92                50
 93            } else {
 94                10
 95            };
 96
 97            let poly = VirtualPolynomial::rand_zero(nv, (degree, degree + 1), 2, &mut rng)?;
 98            let poly_info = poly.aux_info.clone();
 99            let proof = {
100                let start = Instant::now();
101                let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
102                transcript.append_message(b"testing", b"initializing transcript for testing")?;
103                let proof = <PolyIOP<Fr> as ZeroCheck<Fr>>::prove(&poly, &mut transcript)?;
104
105                println!(
106                    "zero check proving time for {} variables and {} degree: {} ns",
107                    nv,
108                    degree,
109                    start.elapsed().as_nanos() / repetition as u128
110                );
111                proof
112            };
113
114            {
115                let start = Instant::now();
116                let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
117                transcript.append_message(b"testing", b"initializing transcript for testing")?;
118                let zero_subclaim =
119                    <PolyIOP<Fr> as ZeroCheck<Fr>>::verify(&proof, &poly_info, &mut transcript)?;
120                assert!(
121                    poly.evaluate(&zero_subclaim.point)? == zero_subclaim.expected_evaluation,
122                    "wrong subclaim"
123                );
124                println!(
125                    "zero check verification time for {} variables and {} degree: {} ns",
126                    nv,
127                    degree,
128                    start.elapsed().as_nanos() / repetition as u128
129                );
130            }
131
132            println!("====================================");
133        }
134    }
135    Ok(())
136}
137
138fn bench_permutation_check() -> Result<(), PolyIOPErrors> {
139    let mut rng = test_rng();
140
141    for nv in 4..20 {
142        let srs = KZG::gen_srs_for_testing(&mut rng, nv + 1)?;
143        let (pcs_param, _) = KZG::trim(&srs, nv + 1, Some(nv + 1))?;
144
145        let repetition = if nv < 10 {
146            100
147        } else if nv < 20 {
148            50
149        } else {
150            10
151        };
152
153        let w = Rc::new(DenseMultilinearExtension::rand(nv, &mut rng));
154
155        // s_perm is the identity map
156        let s_perm = identity_permutation_mle(nv);
157
158        let proof = {
159            let start = Instant::now();
160            let mut transcript =
161                <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
162            transcript.append_message(b"testing", b"initializing transcript for testing")?;
163
164            let (proof, _q_x) = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::prove(
165                &pcs_param,
166                &w,
167                &w,
168                &s_perm,
169                &mut transcript,
170            )?;
171
172            println!(
173                "permutation check proving time for {} variables: {} ns",
174                nv,
175                start.elapsed().as_nanos() / repetition as u128
176            );
177            proof
178        };
179
180        {
181            let poly_info = VPAuxInfo {
182                max_degree: 2,
183                num_variables: nv,
184                phantom: PhantomData::default(),
185            };
186
187            let start = Instant::now();
188            let mut transcript =
189                <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::init_transcript();
190            transcript.append_message(b"testing", b"initializing transcript for testing")?;
191            let _perm_check_sum_claim = <PolyIOP<Fr> as PermutationCheck<Bls12_381, KZG>>::verify(
192                &proof,
193                &poly_info,
194                &mut transcript,
195            )?;
196            println!(
197                "permutation check verification time for {} variables: {} ns",
198                nv,
199                start.elapsed().as_nanos() / repetition as u128
200            );
201        }
202
203        println!("====================================");
204    }
205
206    Ok(())
207}
208
209fn bench_prod_check() -> Result<(), PolyIOPErrors> {
210    let mut rng = test_rng();
211
212    for nv in 4..20 {
213        let srs = KZG::gen_srs_for_testing(&mut rng, nv + 1)?;
214        let (pcs_param, _) = KZG::trim(&srs, nv + 1, Some(nv + 1))?;
215
216        let repetition = if nv < 10 {
217            100
218        } else if nv < 20 {
219            50
220        } else {
221            10
222        };
223
224        let f: DenseMultilinearExtension<Fr> = DenseMultilinearExtension::rand(nv, &mut rng);
225        let mut g = f.clone();
226        g.evaluations.reverse();
227        let f = Rc::new(f);
228        let g = Rc::new(g);
229
230        let proof = {
231            let start = Instant::now();
232            let mut transcript = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::init_transcript();
233            transcript.append_message(b"testing", b"initializing transcript for testing")?;
234
235            let (proof, _prod_x) = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::prove(
236                &pcs_param,
237                &f,
238                &g,
239                &mut transcript,
240            )?;
241
242            println!(
243                "product check proving time for {} variables: {} ns",
244                nv,
245                start.elapsed().as_nanos() / repetition as u128
246            );
247            proof
248        };
249
250        {
251            let poly_info = VPAuxInfo {
252                max_degree: 2,
253                num_variables: nv,
254                phantom: PhantomData::default(),
255            };
256
257            let start = Instant::now();
258            let mut transcript = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::init_transcript();
259            transcript.append_message(b"testing", b"initializing transcript for testing")?;
260            let _perm_check_sum_claim = <PolyIOP<Fr> as ProductCheck<Bls12_381, KZG>>::verify(
261                &proof,
262                &poly_info,
263                &mut transcript,
264            )?;
265            println!(
266                "product check verification time for {} variables: {} ns",
267                nv,
268                start.elapsed().as_nanos() / repetition as u128
269            );
270        }
271
272        println!("====================================");
273    }
274
275    Ok(())
276}