mirror of
https://github.com/arnaucube/Nova.git
synced 2026-02-27 21:36:41 +01:00
update benches (#94)
This commit is contained in:
@@ -37,11 +37,11 @@ flate2 = "1.0"
|
|||||||
criterion = "0.3.1"
|
criterion = "0.3.1"
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "compressed-snark"
|
name = "recursive-snark"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[[bench]]
|
[[bench]]
|
||||||
name = "recursive-snark"
|
name = "compressed-snark"
|
||||||
harness = false
|
harness = false
|
||||||
|
|
||||||
[features]
|
[features]
|
||||||
|
|||||||
@@ -1,8 +1,14 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
|
||||||
|
use core::marker::PhantomData;
|
||||||
use criterion::*;
|
use criterion::*;
|
||||||
|
use ff::PrimeField;
|
||||||
use nova_snark::{
|
use nova_snark::{
|
||||||
traits::{circuit::TrivialTestCircuit, Group},
|
traits::{
|
||||||
|
circuit::{StepCircuit, TrivialTestCircuit},
|
||||||
|
Group,
|
||||||
|
},
|
||||||
CompressedSNARK, PublicParams, RecursiveSNARK,
|
CompressedSNARK, PublicParams, RecursiveSNARK,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
@@ -11,33 +17,30 @@ type G1 = pasta_curves::pallas::Point;
|
|||||||
type G2 = pasta_curves::vesta::Point;
|
type G2 = pasta_curves::vesta::Point;
|
||||||
type S1 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>;
|
type S1 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G1>;
|
||||||
type S2 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>;
|
type S2 = nova_snark::spartan_with_ipa_pc::RelaxedR1CSSNARK<G2>;
|
||||||
type C1 = TrivialTestCircuit<<G1 as Group>::Scalar>;
|
type C1 = NonTrivialTestCircuit<<G1 as Group>::Scalar>;
|
||||||
type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
|
type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
|
||||||
|
|
||||||
fn compressed_snark_benchmark(c: &mut Criterion) {
|
|
||||||
let num_samples = 10;
|
|
||||||
bench_compressed_snark(c, num_samples);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_duration() -> Criterion {
|
|
||||||
Criterion::default().warm_up_time(Duration::from_millis(3000))
|
|
||||||
}
|
|
||||||
|
|
||||||
criterion_group! {
|
criterion_group! {
|
||||||
name = compressed_snark;
|
name = compressed_snark;
|
||||||
config = set_duration();
|
config = Criterion::default().warm_up_time(Duration::from_millis(3000));
|
||||||
targets = compressed_snark_benchmark
|
targets = bench_compressed_snark
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_main!(compressed_snark);
|
criterion_main!(compressed_snark);
|
||||||
|
|
||||||
fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
|
fn bench_compressed_snark(c: &mut Criterion) {
|
||||||
let mut group = c.benchmark_group("CompressedSNARK");
|
let num_samples = 10;
|
||||||
|
|
||||||
|
// we vary the number of constraints in the step circuit
|
||||||
|
for &log_num_cons_in_step_circuit in [0, 15, 16, 17, 18, 19, 20].iter() {
|
||||||
|
let num_cons = 1 << log_num_cons_in_step_circuit;
|
||||||
|
|
||||||
|
let mut group = c.benchmark_group(format!("RecursiveSNARK-StepCircuitSize-{}", num_cons));
|
||||||
group.sample_size(num_samples);
|
group.sample_size(num_samples);
|
||||||
|
|
||||||
// Produce public parameters
|
// Produce public parameters
|
||||||
let pp = PublicParams::<G1, G2, C1, C2>::setup(
|
let pp = PublicParams::<G1, G2, C1, C2>::setup(
|
||||||
TrivialTestCircuit::default(),
|
NonTrivialTestCircuit::new(num_cons),
|
||||||
TrivialTestCircuit::default(),
|
TrivialTestCircuit::default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -49,10 +52,10 @@ fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
let res = RecursiveSNARK::prove_step(
|
let res = RecursiveSNARK::prove_step(
|
||||||
&pp,
|
&pp,
|
||||||
recursive_snark,
|
recursive_snark,
|
||||||
TrivialTestCircuit::default(),
|
NonTrivialTestCircuit::new(num_cons),
|
||||||
TrivialTestCircuit::default(),
|
TrivialTestCircuit::default(),
|
||||||
<G1 as Group>::Scalar::one(),
|
<G1 as Group>::Scalar::one(),
|
||||||
<G2 as Group>::Scalar::zero(),
|
<G2 as Group>::Scalar::one(),
|
||||||
);
|
);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
let recursive_snark_unwrapped = res.unwrap();
|
let recursive_snark_unwrapped = res.unwrap();
|
||||||
@@ -62,7 +65,7 @@ fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
&pp,
|
&pp,
|
||||||
i + 1,
|
i + 1,
|
||||||
<G1 as Group>::Scalar::one(),
|
<G1 as Group>::Scalar::one(),
|
||||||
<G2 as Group>::Scalar::zero(),
|
<G2 as Group>::Scalar::one(),
|
||||||
);
|
);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
|
|
||||||
@@ -86,15 +89,14 @@ fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
let compressed_snark = res.unwrap();
|
let compressed_snark = res.unwrap();
|
||||||
|
|
||||||
// Benchmark the verification time
|
// Benchmark the verification time
|
||||||
let name = "Verify";
|
group.bench_function("Verify", |b| {
|
||||||
group.bench_function(name, |b| {
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
assert!(black_box(&compressed_snark)
|
assert!(black_box(&compressed_snark)
|
||||||
.verify(
|
.verify(
|
||||||
black_box(&pp),
|
black_box(&pp),
|
||||||
black_box(num_steps),
|
black_box(num_steps),
|
||||||
black_box(<G1 as Group>::Scalar::zero()),
|
black_box(<G1 as Group>::Scalar::one()),
|
||||||
black_box(<G2 as Group>::Scalar::zero()),
|
black_box(<G2 as Group>::Scalar::one()),
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
})
|
})
|
||||||
@@ -102,3 +104,51 @@ fn bench_compressed_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
|
|
||||||
group.finish();
|
group.finish();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
struct NonTrivialTestCircuit<F: PrimeField> {
|
||||||
|
num_cons: usize,
|
||||||
|
_p: PhantomData<F>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> NonTrivialTestCircuit<F>
|
||||||
|
where
|
||||||
|
F: PrimeField,
|
||||||
|
{
|
||||||
|
pub fn new(num_cons: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
num_cons,
|
||||||
|
_p: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<F> StepCircuit<F> for NonTrivialTestCircuit<F>
|
||||||
|
where
|
||||||
|
F: PrimeField,
|
||||||
|
{
|
||||||
|
fn synthesize<CS: ConstraintSystem<F>>(
|
||||||
|
&self,
|
||||||
|
cs: &mut CS,
|
||||||
|
z: AllocatedNum<F>,
|
||||||
|
) -> Result<AllocatedNum<F>, SynthesisError> {
|
||||||
|
// Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
|
||||||
|
let mut x = z;
|
||||||
|
let mut y = x.clone();
|
||||||
|
for i in 0..self.num_cons {
|
||||||
|
y = x.square(cs.namespace(|| format!("x_sq_{}", i)))?;
|
||||||
|
x = y.clone();
|
||||||
|
}
|
||||||
|
Ok(y)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute(&self, z: &F) -> F {
|
||||||
|
let mut x = *z;
|
||||||
|
let mut y = x;
|
||||||
|
for _i in 0..self.num_cons {
|
||||||
|
y = x * x;
|
||||||
|
x = y;
|
||||||
|
}
|
||||||
|
y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,41 +1,42 @@
|
|||||||
#![allow(non_snake_case)]
|
#![allow(non_snake_case)]
|
||||||
|
|
||||||
|
use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
|
||||||
|
use core::marker::PhantomData;
|
||||||
use criterion::*;
|
use criterion::*;
|
||||||
|
use ff::PrimeField;
|
||||||
use nova_snark::{
|
use nova_snark::{
|
||||||
traits::{circuit::TrivialTestCircuit, Group},
|
traits::{
|
||||||
|
circuit::{StepCircuit, TrivialTestCircuit},
|
||||||
|
Group,
|
||||||
|
},
|
||||||
PublicParams, RecursiveSNARK,
|
PublicParams, RecursiveSNARK,
|
||||||
};
|
};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
type G1 = pasta_curves::pallas::Point;
|
type G1 = pasta_curves::pallas::Point;
|
||||||
type G2 = pasta_curves::vesta::Point;
|
type G2 = pasta_curves::vesta::Point;
|
||||||
type C1 = TrivialTestCircuit<<G1 as Group>::Scalar>;
|
type C1 = NonTrivialTestCircuit<<G1 as Group>::Scalar>;
|
||||||
type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
|
type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
|
||||||
|
|
||||||
fn recursive_snark_benchmark(c: &mut Criterion) {
|
|
||||||
let num_samples = 10;
|
|
||||||
bench_recursive_snark(c, num_samples);
|
|
||||||
}
|
|
||||||
|
|
||||||
fn set_duration() -> Criterion {
|
|
||||||
Criterion::default().warm_up_time(Duration::from_millis(3000))
|
|
||||||
}
|
|
||||||
|
|
||||||
criterion_group! {
|
criterion_group! {
|
||||||
name = recursive_snark;
|
name = recursive_snark;
|
||||||
config = set_duration();
|
config = Criterion::default().warm_up_time(Duration::from_millis(3000));
|
||||||
targets = recursive_snark_benchmark
|
targets = bench_recursive_snark
|
||||||
}
|
}
|
||||||
|
|
||||||
criterion_main!(recursive_snark);
|
criterion_main!(recursive_snark);
|
||||||
|
|
||||||
fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
|
fn bench_recursive_snark(c: &mut Criterion) {
|
||||||
let mut group = c.benchmark_group("RecursiveSNARK".to_string());
|
// we vary the number of constraints in the step circuit
|
||||||
group.sample_size(num_samples);
|
for &log_num_cons_in_step_circuit in [0, 15, 16, 17, 18, 19, 20].iter() {
|
||||||
|
let num_cons = 1 << log_num_cons_in_step_circuit;
|
||||||
|
|
||||||
|
let mut group = c.benchmark_group(format!("RecursiveSNARK-StepCircuitSize-{}", num_cons));
|
||||||
|
group.sample_size(10);
|
||||||
|
|
||||||
// Produce public parameters
|
// Produce public parameters
|
||||||
let pp = PublicParams::<G1, G2, C1, C2>::setup(
|
let pp = PublicParams::<G1, G2, C1, C2>::setup(
|
||||||
TrivialTestCircuit::default(),
|
NonTrivialTestCircuit::new(num_cons),
|
||||||
TrivialTestCircuit::default(),
|
TrivialTestCircuit::default(),
|
||||||
);
|
);
|
||||||
|
|
||||||
@@ -50,10 +51,10 @@ fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
let res = RecursiveSNARK::prove_step(
|
let res = RecursiveSNARK::prove_step(
|
||||||
&pp,
|
&pp,
|
||||||
recursive_snark,
|
recursive_snark,
|
||||||
TrivialTestCircuit::default(),
|
NonTrivialTestCircuit::new(num_cons),
|
||||||
TrivialTestCircuit::default(),
|
TrivialTestCircuit::default(),
|
||||||
<G1 as Group>::Scalar::one(),
|
<G1 as Group>::Scalar::one(),
|
||||||
<G2 as Group>::Scalar::zero(),
|
<G2 as Group>::Scalar::one(),
|
||||||
);
|
);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
let recursive_snark_unwrapped = res.unwrap();
|
let recursive_snark_unwrapped = res.unwrap();
|
||||||
@@ -63,7 +64,7 @@ fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
&pp,
|
&pp,
|
||||||
i + 1,
|
i + 1,
|
||||||
<G1 as Group>::Scalar::one(),
|
<G1 as Group>::Scalar::one(),
|
||||||
<G2 as Group>::Scalar::zero(),
|
<G2 as Group>::Scalar::one(),
|
||||||
);
|
);
|
||||||
assert!(res.is_ok());
|
assert!(res.is_ok());
|
||||||
|
|
||||||
@@ -77,10 +78,10 @@ fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
assert!(RecursiveSNARK::prove_step(
|
assert!(RecursiveSNARK::prove_step(
|
||||||
black_box(&pp),
|
black_box(&pp),
|
||||||
black_box(recursive_snark.clone()),
|
black_box(recursive_snark.clone()),
|
||||||
|
black_box(NonTrivialTestCircuit::new(num_cons)),
|
||||||
black_box(TrivialTestCircuit::default()),
|
black_box(TrivialTestCircuit::default()),
|
||||||
black_box(TrivialTestCircuit::default()),
|
black_box(<G1 as Group>::Scalar::one()),
|
||||||
black_box(<G1 as Group>::Scalar::zero()),
|
black_box(<G2 as Group>::Scalar::one()),
|
||||||
black_box(<G2 as Group>::Scalar::zero()),
|
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
})
|
})
|
||||||
@@ -89,18 +90,65 @@ fn bench_recursive_snark(c: &mut Criterion, num_samples: usize) {
|
|||||||
let recursive_snark = recursive_snark.unwrap();
|
let recursive_snark = recursive_snark.unwrap();
|
||||||
|
|
||||||
// Benchmark the verification time
|
// Benchmark the verification time
|
||||||
let name = "Verify";
|
group.bench_function("Verify", |b| {
|
||||||
group.bench_function(name, |b| {
|
|
||||||
b.iter(|| {
|
b.iter(|| {
|
||||||
assert!(black_box(&recursive_snark)
|
assert!(black_box(&recursive_snark)
|
||||||
.verify(
|
.verify(
|
||||||
black_box(&pp),
|
black_box(&pp),
|
||||||
black_box(num_warmup_steps),
|
black_box(num_warmup_steps),
|
||||||
black_box(<G1 as Group>::Scalar::zero()),
|
black_box(<G1 as Group>::Scalar::one()),
|
||||||
black_box(<G2 as Group>::Scalar::zero()),
|
black_box(<G2 as Group>::Scalar::one()),
|
||||||
)
|
)
|
||||||
.is_ok());
|
.is_ok());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
group.finish();
|
group.finish();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default)]
|
||||||
|
struct NonTrivialTestCircuit<F: PrimeField> {
|
||||||
|
num_cons: usize,
|
||||||
|
_p: PhantomData<F>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<F> NonTrivialTestCircuit<F>
|
||||||
|
where
|
||||||
|
F: PrimeField,
|
||||||
|
{
|
||||||
|
pub fn new(num_cons: usize) -> Self {
|
||||||
|
Self {
|
||||||
|
num_cons,
|
||||||
|
_p: Default::default(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
impl<F> StepCircuit<F> for NonTrivialTestCircuit<F>
|
||||||
|
where
|
||||||
|
F: PrimeField,
|
||||||
|
{
|
||||||
|
fn synthesize<CS: ConstraintSystem<F>>(
|
||||||
|
&self,
|
||||||
|
cs: &mut CS,
|
||||||
|
z: AllocatedNum<F>,
|
||||||
|
) -> Result<AllocatedNum<F>, SynthesisError> {
|
||||||
|
// Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
|
||||||
|
let mut x = z;
|
||||||
|
let mut y = x.clone();
|
||||||
|
for i in 0..self.num_cons {
|
||||||
|
y = x.square(cs.namespace(|| format!("x_sq_{}", i)))?;
|
||||||
|
x = y.clone();
|
||||||
|
}
|
||||||
|
Ok(y)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn compute(&self, z: &F) -> F {
|
||||||
|
let mut x = *z;
|
||||||
|
let mut y = x;
|
||||||
|
for _i in 0..self.num_cons {
|
||||||
|
y = x * x;
|
||||||
|
x = y;
|
||||||
|
}
|
||||||
|
y
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user