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
22impl<ConstraintF: Field> ConstraintSystem<ConstraintF> for TestConstraintCounter {
23 type Root = Self;
24
25 fn alloc<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
26 where
27 F: FnOnce() -> Result<ConstraintF, SynthesisError>,
28 A: FnOnce() -> AR,
29 AR: Into<String>,
30 {
31 let var = Variable::new_unchecked(Index::Aux(self.num_aux));
32 self.num_aux += 1;
33 Ok(var)
34 }
35
36 fn alloc_input<F, A, AR>(&mut self, _: A, _: F) -> Result<Variable, SynthesisError>
37 where
38 F: FnOnce() -> Result<ConstraintF, SynthesisError>,
39 A: FnOnce() -> AR,
40 AR: Into<String>,
41 {
42 let var = Variable::new_unchecked(Index::Input(self.num_inputs));
43 self.num_inputs += 1;
44
45 Ok(var)
46 }
47
48 fn enforce<A, AR, LA, LB, LC>(&mut self, _: A, _: LA, _: LB, _: LC)
49 where
50 A: FnOnce() -> AR,
51 AR: Into<String>,
52 LA: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
53 LB: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
54 LC: FnOnce(LinearCombination<ConstraintF>) -> LinearCombination<ConstraintF>,
55 {
56 self.num_constraints += 1;
57 }
58
59 fn push_namespace<NR, N>(&mut self, _: N)
60 where
61 NR: Into<String>,
62 N: FnOnce() -> NR,
63 {}
64
65 fn pop_namespace(&mut self) { }
66
67 fn get_root(&mut self) -> &mut Self::Root {
68 self
69 }
70
71 fn num_constraints(&self) -> usize {
72 self.num_constraints
73 }
74}