snarkvm_r1cs/
constraint_counter.rs

1// Copyright (C) 2019-2023 Aleo Systems Inc.
2// This file is part of the snarkVM library.
3
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7// http://www.apache.org/licenses/LICENSE-2.0
8
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use crate::{errors::SynthesisError, ConstraintSystem, Index, LinearCombination, Variable};
16use snarkvm_fields::Field;
17
18/// Constraint counter for testing purposes.
19#[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}