arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/hyperplonk/benches/bench.rs
hyperplonk/benches/bench.rsRaw
  1use std::{env, fs::File, time::Instant};
  2
  3use ark_bls12_381::{Bls12_381, Fr};
  4use ark_serialize::Write;
  5use ark_std::test_rng;
  6use hyperplonk::{
  7    prelude::{CustomizedGates, HyperPlonkErrors, MockCircuit},
  8    HyperPlonkSNARK,
  9};
 10use pcs::{
 11    prelude::{MultilinearKzgPCS, MultilinearUniversalParams, UnivariateUniversalParams},
 12    PolynomialCommitmentScheme,
 13};
 14use poly_iop::PolyIOP;
 15use rayon::ThreadPoolBuilder;
 16
 17fn main() -> Result<(), HyperPlonkErrors> {
 18    let args: Vec<String> = env::args().collect();
 19    let thread = args[1].parse().unwrap_or(12);
 20
 21    ThreadPoolBuilder::new()
 22        .num_threads(thread)
 23        .build_global()
 24        .unwrap();
 25    bench_vanilla_plonk(thread)?;
 26    for degree in [1, 2, 4, 8, 16, 32] {
 27        bench_high_degree_plonk(degree, thread)?;
 28    }
 29
 30    Ok(())
 31}
 32
 33fn bench_vanilla_plonk(thread: usize) -> Result<(), HyperPlonkErrors> {
 34    let mut rng = test_rng();
 35    let pcs_srs = MultilinearKzgPCS::<Bls12_381>::gen_srs_for_testing(&mut rng, 22)?;
 36
 37    let filename = format!("vanilla nv {}.txt", thread);
 38    let mut file = File::create(filename).unwrap();
 39    for nv in 1..16 {
 40        let vanilla_gate = CustomizedGates::vanilla_plonk_gate();
 41        bench_mock_circuit_zkp_helper(&mut file, nv, &vanilla_gate, &pcs_srs)?;
 42    }
 43
 44    Ok(())
 45}
 46
 47fn bench_high_degree_plonk(degree: usize, thread: usize) -> Result<(), HyperPlonkErrors> {
 48    let mut rng = test_rng();
 49    let pcs_srs = MultilinearKzgPCS::<Bls12_381>::gen_srs_for_testing(&mut rng, 22)?;
 50
 51    let filename = format!("high degree {} thread {}.txt", degree, thread);
 52    let mut file = File::create(filename).unwrap();
 53    for nv in 1..16 {
 54        let vanilla_gate = CustomizedGates::vanilla_plonk_gate();
 55        bench_mock_circuit_zkp_helper(&mut file, nv, &vanilla_gate, &pcs_srs)?;
 56    }
 57
 58    Ok(())
 59}
 60
 61fn bench_mock_circuit_zkp_helper(
 62    file: &mut File,
 63    nv: usize,
 64    gate: &CustomizedGates,
 65    pcs_srs: &(
 66        MultilinearUniversalParams<Bls12_381>,
 67        UnivariateUniversalParams<Bls12_381>,
 68    ),
 69) -> Result<(), HyperPlonkErrors> {
 70    let repetition = if nv < 10 {
 71        10
 72    } else if nv < 20 {
 73        5
 74    } else {
 75        2
 76    };
 77
 78    //==========================================================
 79    let start = Instant::now();
 80    for _ in 0..repetition {
 81        let circuit = MockCircuit::<Fr>::new(1 << nv, gate);
 82        assert!(circuit.is_satisfied());
 83    }
 84    println!(
 85        "mock circuit gen for {} variables: {} ns",
 86        nv,
 87        start.elapsed().as_nanos() / repetition as u128
 88    );
 89
 90    let circuit = MockCircuit::<Fr>::new(1 << nv, gate);
 91    assert!(circuit.is_satisfied());
 92    let index = circuit.index;
 93    //==========================================================
 94    // generate pk and vks
 95    let start = Instant::now();
 96    for _ in 0..repetition {
 97        let (_pk, _vk) = <PolyIOP<Fr> as HyperPlonkSNARK<
 98            Bls12_381,
 99            MultilinearKzgPCS<Bls12_381>,
100        >>::preprocess(&index, &pcs_srs)?;
101    }
102    println!(
103        "key extraction for {} variables: {} us",
104        nv,
105        start.elapsed().as_micros() / repetition as u128
106    );
107    let (pk, vk) =
108        <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::preprocess(
109            &index, &pcs_srs,
110        )?;
111    //==========================================================
112    // generate a proof
113    let start = Instant::now();
114    for _ in 0..repetition {
115        let _proof =
116            <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::prove(
117                &pk,
118                &circuit.witnesses[0].coeff_ref(),
119                &circuit.witnesses,
120            )?;
121    }
122    let t = start.elapsed().as_micros() / repetition as u128;
123    println!("proving for {} variables: {} us", nv, t);
124    file.write_all(format!("{} {}\n", nv, t).as_ref()).unwrap();
125
126    let proof = <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::prove(
127        &pk,
128        &circuit.witnesses[0].coeff_ref(),
129        &circuit.witnesses,
130    )?;
131    //==========================================================
132    // verify a proof
133    let start = Instant::now();
134    for _ in 0..repetition {
135        let verify =
136            <PolyIOP<Fr> as HyperPlonkSNARK<Bls12_381, MultilinearKzgPCS<Bls12_381>>>::verify(
137                &vk,
138                &circuit.witnesses[0].coeff_ref(),
139                &proof,
140            )?;
141        assert!(verify);
142    }
143    println!(
144        "verifying for {} variables: {} us",
145        nv,
146        start.elapsed().as_micros() / repetition as u128
147    );
148    Ok(())
149}