snarkvm_r1cs/
constraint_counter.rs1use crate::{errors::SynthesisError, ConstraintSystem, Index, LinearCombination, Variable};
16use snarkvm_fields::Field;
17
18#[derive(Default)]
20pub struct ConstraintCounter {
21 pub num_public_variables: usize,
22 pub num_private_variables: usize,
23 pub num_constraints: usize,
24}
25
26impl<ConstraintF: Field> ConstraintSystem<ConstraintF> for ConstraintCounter {
27 type Root = Self;
28
29 fn alloc<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
30 where
31 F: FnOnce() -> Result<ConstraintF, SynthesisError>,
32 A: FnOnce() -> AR,
33 AR: AsRef<str>,
34 {
35 let var = Variable::new_unchecked(Index::Private(self.num_private_variables));
36 self.num_private_variables += 1;
37 Ok(var)
38 }
39
40 fn alloc_input<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
41 where
42 F: FnOnce() -> Result<ConstraintF, SynthesisError>,
43 A: FnOnce() -> AR,
44 AR: AsRef<str>,
45 {
46 let var = Variable::new_unchecked(Index::Public(self.num_public_variables));
47 self.num_public_variables += 1;
48
49 Ok(var)
50 }
51
52 fn enforce<A, AR, LA, LB, LC>(&mut self, _: A, _: LA, _: LB, _: LC)
53 where
54 A: FnOnce() -> AR,
55 AR: AsRef<str>,
56 LA: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
57 LB: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
58 LC: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
59 {
60 self.num_constraints += 1;
61 }
62
63 fn push_namespace<NR, N>(&mut self, _: N)
64 where
65 NR: AsRef<str>,
66 N: FnOnce() -> NR,
67 {
68 }
69
70 fn pop_namespace(&mut self) {}
71
72 fn get_root(&mut self) -> &mut Self::Root {
73 self
74 }
75
76 fn num_constraints(&self) -> usize {
77 self.num_constraints
78 }
79
80 fn num_public_variables(&self) -> usize {
81 self.num_public_variables
82 }
83
84 fn num_private_variables(&self) -> usize {
85 self.num_private_variables
86 }
87
88 fn is_in_setup_mode(&self) -> bool {
89 true
90 }
91}