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::Fr;
  3use ark_poly::{DenseMultilinearExtension, MultilinearExtension};
  4use ark_std::{test_rng, UniformRand};
  5use poly_iop::prelude::{
  6    identity_permutation_mle, PermutationCheck, PolyIOP, PolyIOPErrors, SumCheck, ZeroCheck,
  7};
  8use std::{marker::PhantomData, time::Instant};
  9
 10fn main() -> Result<(), PolyIOPErrors> {
 11    bench_permutation_check()?;
 12    println!("\n\n");
 13    bench_sum_check()?;
 14    println!("\n\n");
 15    bench_zero_check()
 16}
 17
 18fn bench_sum_check() -> Result<(), PolyIOPErrors> {
 19    let mut rng = test_rng();
 20    for degree in 2..4 {
 21        for nv in 4..25 {
 22            let repetition = if nv < 10 {
 23                100
 24            } else if nv < 20 {
 25                50
 26            } else {
 27                10
 28            };
 29
 30            let (poly, asserted_sum) =
 31                VirtualPolynomial::rand(nv, (degree, degree + 1), 2, &mut rng)?;
 32            let poly_info = poly.aux_info.clone();
 33            let proof = {
 34                let start = Instant::now();
 35                for _ in 0..repetition {
 36                    let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
 37                    let _proof = <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?;
 38                }
 39
 40                println!(
 41                    "sum check proving time for {} variables and {} degree: {} ns",
 42                    nv,
 43                    degree,
 44                    start.elapsed().as_nanos() / repetition as u128
 45                );
 46                let mut transcript = <PolyIOP<Fr> as SumCheck<Fr>>::init_transcript();
 47                <PolyIOP<Fr> as SumCheck<Fr>>::prove(&poly, &mut transcript)?
 48            };
 49
 50            {
 51                let start = Instant::now();
 52
 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                    assert!(
 62                        poly.evaluate(&subclaim.point).unwrap() == subclaim.expected_evaluation,
 63                        "wrong subclaim"
 64                    );
 65                }
 66                println!(
 67                    "sum check verification time for {} variables and {} degree: {} ns",
 68                    nv,
 69                    degree,
 70                    start.elapsed().as_nanos() / repetition as u128
 71                );
 72            }
 73
 74            println!("====================================");
 75        }
 76    }
 77    Ok(())
 78}
 79
 80fn bench_zero_check() -> Result<(), PolyIOPErrors> {
 81    let mut rng = test_rng();
 82    for degree in 2..4 {
 83        for nv in 4..20 {
 84            let repetition = if nv < 10 {
 85                100
 86            } else if nv < 20 {
 87                50
 88            } else {
 89                10
 90            };
 91
 92            let poly = VirtualPolynomial::rand_zero(nv, (degree, degree + 1), 2, &mut rng)?;
 93            let poly_info = poly.aux_info.clone();
 94            let proof = {
 95                let start = Instant::now();
 96                let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
 97                transcript.append_message(b"testing", b"initializing transcript for testing")?;
 98                let proof = <PolyIOP<Fr> as ZeroCheck<Fr>>::prove(&poly, &mut transcript)?;
 99
100                println!(
101                    "zero check proving time for {} variables and {} degree: {} ns",
102                    nv,
103                    degree,
104                    start.elapsed().as_nanos() / repetition as u128
105                );
106                proof
107            };
108
109            {
110                let start = Instant::now();
111                let mut transcript = <PolyIOP<Fr> as ZeroCheck<Fr>>::init_transcript();
112                transcript.append_message(b"testing", b"initializing transcript for testing")?;
113                let subclaim =
114                    <PolyIOP<Fr> as ZeroCheck<Fr>>::verify(&proof, &poly_info, &mut transcript)?
115                        .sum_check_sub_claim;
116                assert!(
117                    poly.evaluate(&subclaim.point)? == subclaim.expected_evaluation,
118                    "wrong subclaim"
119                );
120                println!(
121                    "zero check verification time for {} variables and {} degree: {} ns",
122                    nv,
123                    degree,
124                    start.elapsed().as_nanos() / repetition as u128
125                );
126            }
127
128            println!("====================================");
129        }
130    }
131    Ok(())
132}
133
134fn bench_permutation_check() -> Result<(), PolyIOPErrors> {
135    let mut rng = test_rng();
136
137    for nv in 4..20 {
138        let repetition = if nv < 10 {
139            100
140        } else if nv < 20 {
141            50
142        } else {
143            10
144        };
145
146        let w = DenseMultilinearExtension::rand(nv, &mut rng);
147
148        // s_perm is the identity map
149        let s_perm = identity_permutation_mle(nv);
150
151        let proof = {
152            let start = Instant::now();
153            let mut transcript = <PolyIOP<Fr> as PermutationCheck<Fr>>::init_transcript();
154            transcript.append_message(b"testing", b"initializing transcript for testing")?;
155
156            let mut challenge =
157                <PolyIOP<Fr> as PermutationCheck<Fr>>::generate_challenge(&mut transcript)?;
158
159            let prod_x_and_aux = <PolyIOP<Fr> as PermutationCheck<Fr>>::compute_prod_evals(
160                &challenge, &w, &w, &s_perm,
161            )?;
162
163            let prod_x_binding = mock_commit(&prod_x_and_aux[0]);
164
165            <PolyIOP<Fr> as PermutationCheck<Fr>>::update_challenge(
166                &mut challenge,
167                &mut transcript,
168                &prod_x_binding,
169            )?;
170
171            let proof = <PolyIOP<Fr> as PermutationCheck<Fr>>::prove(
172                &prod_x_and_aux,
173                &challenge,
174                &mut transcript,
175            )?;
176
177            println!(
178                "permutation check proving time for {} variables: {} ns",
179                nv,
180                start.elapsed().as_nanos() / repetition as u128
181            );
182            proof
183        };
184
185        {
186            let poly_info = VPAuxInfo {
187                max_degree: 2,
188                num_variables: nv,
189                phantom: PhantomData::default(),
190            };
191
192            let start = Instant::now();
193            let mut transcript = <PolyIOP<Fr> as PermutationCheck<Fr>>::init_transcript();
194            transcript.append_message(b"testing", b"initializing transcript for testing")?;
195            let _subclaim =
196                <PolyIOP<Fr> as PermutationCheck<Fr>>::verify(&proof, &poly_info, &mut transcript)?;
197            println!(
198                "permutation check verification time for {} variables: {} ns",
199                nv,
200                start.elapsed().as_nanos() / repetition as u128
201            );
202        }
203
204        println!("====================================");
205    }
206
207    Ok(())
208}
209
210fn mock_commit(_f: &DenseMultilinearExtension<Fr>) -> Fr {
211    let mut rng = test_rng();
212    Fr::rand(&mut rng)
213}