arnaucube
arnaucube/NovaPublic Self-hosted repository
Nova/benches/compute-digest.rs
benches/compute-digest.rsRaw
 1use std::{marker::PhantomData, time::Duration};
 2
 3use bellperson::{gadgets::num::AllocatedNum, ConstraintSystem, SynthesisError};
 4use criterion::{black_box, criterion_group, criterion_main, Criterion};
 5use ff::PrimeField;
 6use nova_snark::{
 7  traits::{
 8    circuit::{StepCircuit, TrivialTestCircuit},
 9    Group,
10  },
11  PublicParams,
12};
13
14type G1 = pasta_curves::pallas::Point;
15type G2 = pasta_curves::vesta::Point;
16type C1 = NonTrivialTestCircuit<<G1 as Group>::Scalar>;
17type C2 = TrivialTestCircuit<<G2 as Group>::Scalar>;
18
19criterion_group! {
20name = compute_digest;
21config = Criterion::default().warm_up_time(Duration::from_millis(3000)).sample_size(10);
22targets = bench_compute_digest
23}
24
25criterion_main!(compute_digest);
26
27fn bench_compute_digest(c: &mut Criterion) {
28  c.bench_function("compute_digest", |b| {
29    b.iter(|| {
30      PublicParams::<G1, G2, C1, C2>::setup(black_box(C1::new(10)), black_box(C2::default()))
31    })
32  });
33}
34
35#[derive(Clone, Debug, Default)]
36struct NonTrivialTestCircuit<F: PrimeField> {
37  num_cons: usize,
38  _p: PhantomData<F>,
39}
40
41impl<F> NonTrivialTestCircuit<F>
42where
43  F: PrimeField,
44{
45  pub fn new(num_cons: usize) -> Self {
46    Self {
47      num_cons,
48      _p: Default::default(),
49    }
50  }
51}
52impl<F> StepCircuit<F> for NonTrivialTestCircuit<F>
53where
54  F: PrimeField,
55{
56  fn arity(&self) -> usize {
57    1
58  }
59
60  fn synthesize<CS: ConstraintSystem<F>>(
61    &self,
62    cs: &mut CS,
63    z: &[AllocatedNum<F>],
64  ) -> Result<Vec<AllocatedNum<F>>, SynthesisError> {
65    // Consider a an equation: `x^2 = y`, where `x` and `y` are respectively the input and output.
66    let mut x = z[0].clone();
67    let mut y = x.clone();
68    for i in 0..self.num_cons {
69      y = x.square(cs.namespace(|| format!("x_sq_{i}")))?;
70      x = y.clone();
71    }
72    Ok(vec![y])
73  }
74
75  fn output(&self, z: &[F]) -> Vec<F> {
76    let mut x = z[0];
77    let mut y = x;
78    for _i in 0..self.num_cons {
79      y = x * x;
80      x = y;
81    }
82    vec![y]
83  }
84}