You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

84 lines
1.9 KiB

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