arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/r1cs-std/src/test_constraint_counter.rs
r1cs-std/src/test_constraint_counter.rsRaw
 1use crate::String;
 2use algebra::Field;
 3use r1cs_core::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable};
 4
 5/// Constraint counter for testing purposes.
 6pub struct TestConstraintCounter {
 7    pub num_inputs:      usize,
 8    pub num_aux:         usize,
 9    pub num_constraints: usize,
10}
11
12impl TestConstraintCounter {
13    pub fn new() -> TestConstraintCounter {
14        TestConstraintCounter {
15            num_aux: 0,
16            num_inputs: 0,
17            num_constraints: 0,
18        }
19    }
20
21    pub fn num_constraints(&self) -> usize {
22        self.num_constraints
23    }
24}
25
26impl<ConstraintF: Field> ConstraintSystem<ConstraintF> for TestConstraintCounter {
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: Into<String>,
34    {
35        let var = Variable::new_unchecked(Index::Aux(self.num_aux));
36        self.num_aux += 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: Into<String>,
45    {
46        let var = Variable::new_unchecked(Index::Input(self.num_inputs));
47        self.num_inputs += 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: Into<String>,
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: Into<String>,
66        N: FnOnce() -> NR,
67    {}
68
69    fn pop_namespace(&mut self) { }
70
71    fn get_root(&mut self) -> &mut Self::Root {
72        self
73    }
74
75    fn num_constraints(&self) -> usize {
76        self.num_constraints
77    }
78}