1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use crate::{
curves::Field,
gadgets::r1cs::{ConstraintSystem, Index, LinearCombination, Variable},
};
use snarkvm_errors::gadgets::SynthesisError;
#[derive(Default)]
pub struct ConstraintCounter {
pub num_inputs: usize,
pub num_aux: usize,
pub num_constraints: usize,
}
impl ConstraintCounter {
pub fn num_constraints(&self) -> usize {
self.num_constraints
}
}
impl<ConstraintF: Field> ConstraintSystem<ConstraintF> for ConstraintCounter {
type Root = Self;
fn alloc<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<ConstraintF, SynthesisError>,
A: FnOnce() -> AR,
AR: AsRef<str>,
{
let var = Variable::new_unchecked(Index::Aux(self.num_aux));
self.num_aux += 1;
Ok(var)
}
fn alloc_input<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
where
F: FnOnce() -> Result<ConstraintF, SynthesisError>,
A: FnOnce() -> AR,
AR: AsRef<str>,
{
let var = Variable::new_unchecked(Index::Input(self.num_inputs));
self.num_inputs += 1;
Ok(var)
}
fn enforce<A, AR, LA, LB, LC>(&mut self, _: A, _: LA, _: LB, _: LC)
where
A: FnOnce() -> AR,
AR: AsRef<str>,
LA: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
LB: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
LC: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
{
self.num_constraints += 1;
}
fn push_namespace<NR, N>(&mut self, _: N)
where
NR: AsRef<str>,
N: FnOnce() -> NR,
{
}
fn pop_namespace(&mut self) {}
fn get_root(&mut self) -> &mut Self::Root {
self
}
fn num_constraints(&self) -> usize {
self.num_constraints
}
}