Skip to content

Commit

Permalink
adding groth16 comparison
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkolasg committed Jun 5, 2023
1 parent e492b90 commit b885a91
Showing 1 changed file with 76 additions and 8 deletions.
84 changes: 76 additions & 8 deletions benches/testudo.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
use std::marker::PhantomData;
use std::time::Instant;

use ark_crypto_primitives::sponge::poseidon::PoseidonConfig;
use ark_crypto_primitives::sponge::Absorb;
use ark_ec::pairing::Pairing;
use ark_ff::PrimeField;
use ark_groth16::prepare_verifying_key;
use ark_groth16::Groth16;
use ark_r1cs_std::fields::fp::FpVar;
use ark_r1cs_std::prelude::AllocVar;
use ark_relations::r1cs::ConstraintSynthesizer;
use ark_relations::r1cs::ConstraintSystem;
use ark_serialize::*;
use libtestudo::parameters::PoseidonConfiguration;
use libtestudo::{
Expand All @@ -12,6 +19,42 @@ use libtestudo::{
Instance,
};
use serde::Serialize;
use std::ops::Mul;

fn main() {
bench_with_bls12_377();
// bench_with_bls12_381();
// bench_with_ark_blst();
}
struct GrothCircuit<F: PrimeField> {
n_constraints: usize,
_p: PhantomData<F>,
}

impl<F: PrimeField> GrothCircuit<F> {
pub fn new(n_constraints: usize) -> Self {
GrothCircuit {
n_constraints,
_p: PhantomData,
}
}
}

impl<F: PrimeField> ConstraintSynthesizer<F> for GrothCircuit<F> {
fn generate_constraints(
self,
cs: ark_relations::r1cs::ConstraintSystemRef<F>,
) -> ark_relations::r1cs::Result<()> {
let a = F::rand(&mut rand::thread_rng());
let b = F::rand(&mut rand::thread_rng());
let av = FpVar::new_witness(cs.clone(), || Ok(a))?;
let bv = FpVar::new_witness(cs.clone(), || Ok(b))?;
for _ in 0..self.n_constraints {
av.clone().mul(bv.clone());
}
Ok(())
}
}

#[derive(Default, Clone, Serialize)]
struct BenchmarkResults {
Expand All @@ -22,12 +65,7 @@ struct BenchmarkResults {
sat_proof_size: usize,
eval_proof_size: usize,
total_proof_size: usize,
}

fn main() {
bench_with_bls12_377();
// bench_with_bls12_381();
// bench_with_ark_blst();
g16_proving_time: u128,
}

fn bench_with_ark_blst() {
Expand All @@ -52,8 +90,9 @@ where
E::ScalarField: Absorb,
{
let mut writer = csv::Writer::from_path(file_name).expect("unable to open csv writer");
for &s in [4, 5, 10, 12, 14, 16, 18, 20, 22, 24, 26].iter() {
println!("Running for {} inputs", s);
for &s in [5, 10, 15, 20, 24, 26].iter() {
//for &s in [4].iter() {
println!("Running for {} constraints", s);
let mut br = BenchmarkResults::default();
let num_vars = (2_usize).pow(s as u32);
let num_cons = num_vars;
Expand Down Expand Up @@ -119,9 +158,38 @@ where
let duration = start.elapsed().as_millis();
br.testudo_verification_time = duration;

groth16_bench::<E>(num_cons, &mut br);
writer
.serialize(br)
.expect("unable to write results to csv");
writer.flush().expect("wasn't able to flush");
}
}

fn groth16_bench<E: Pairing>(n_constraints: usize, res: &mut BenchmarkResults) {
let params = {
let c = GrothCircuit::<E::ScalarField>::new(n_constraints);
Groth16::<E>::generate_random_parameters_with_reduction(c, &mut rand::thread_rng()).unwrap()
};
let pvk = prepare_verifying_key(&params.vk);
println!("Running G16 proving for {} constraints", n_constraints);
let number_constraints = {
let circuit = GrothCircuit::<E::ScalarField>::new(n_constraints);
let cs = ConstraintSystem::<E::ScalarField>::new_ref();
circuit.generate_constraints(cs.clone()).unwrap();
cs.num_constraints() as u64
};
assert_eq!(number_constraints as usize, n_constraints);
let start = Instant::now();
let proof = Groth16::<E>::create_random_proof_with_reduction(
GrothCircuit::<E::ScalarField>::new(n_constraints),
&params,
&mut rand::thread_rng(),
)
.expect("proof creation failed");
let proving_time = start.elapsed().as_millis();
res.g16_proving_time = proving_time;

let r = Groth16::<E>::verify_proof(&pvk, &proof, &[]).unwrap();
assert!(r);
}

0 comments on commit b885a91

Please sign in to comment.