From 951a6226c94521a754f9c5dc9b3c9129f5b736c0 Mon Sep 17 00:00:00 2001 From: Georgios Konstantopoulos Date: Mon, 23 Mar 2020 09:32:05 +0200 Subject: [PATCH] feat: TestConstraintCounter --- r1cs-std/src/lib.rs | 1 + r1cs-std/src/test_constraint_counter.rs | 74 +++++++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 r1cs-std/src/test_constraint_counter.rs diff --git a/r1cs-std/src/lib.rs b/r1cs-std/src/lib.rs index 74b0607..b0180c2 100644 --- a/r1cs-std/src/lib.rs +++ b/r1cs-std/src/lib.rs @@ -34,6 +34,7 @@ use ralloc::{collections::BTreeMap, string::String, vec::Vec}; use std::{collections::BTreeMap, string::String, vec::Vec}; pub mod test_constraint_system; +pub mod test_constraint_counter; pub mod bits; pub use self::bits::*; diff --git a/r1cs-std/src/test_constraint_counter.rs b/r1cs-std/src/test_constraint_counter.rs new file mode 100644 index 0000000..6fc4c72 --- /dev/null +++ b/r1cs-std/src/test_constraint_counter.rs @@ -0,0 +1,74 @@ +use crate::String; +use algebra::Field; +use r1cs_core::{ConstraintSystem, Index, LinearCombination, SynthesisError, Variable}; + +/// Constraint counter for testing purposes. +pub struct TestConstraintCounter { + pub num_inputs: usize, + pub num_aux: usize, + pub num_constraints: usize, +} + +impl TestConstraintCounter { + pub fn new() -> TestConstraintCounter { + TestConstraintCounter { + num_aux: 0, + num_inputs: 0, + num_constraints: 0, + } + } +} + +impl ConstraintSystem for TestConstraintCounter { + type Root = Self; + + fn alloc(&mut self, _: A, _: F) -> Result + where + F: FnOnce() -> Result, + A: FnOnce() -> AR, + AR: Into, + { + let var = Variable::new_unchecked(Index::Aux(self.num_aux)); + self.num_aux += 1; + Ok(var) + } + + fn alloc_input(&mut self, _: A, _: F) -> Result + where + F: FnOnce() -> Result, + A: FnOnce() -> AR, + AR: Into, + { + let var = Variable::new_unchecked(Index::Input(self.num_inputs)); + self.num_inputs += 1; + + Ok(var) + } + + fn enforce(&mut self, _: A, _: LA, _: LB, _: LC) + where + A: FnOnce() -> AR, + AR: Into, + LA: FnOnce(LinearCombination) -> LinearCombination, + LB: FnOnce(LinearCombination) -> LinearCombination, + LC: FnOnce(LinearCombination) -> LinearCombination, + { + self.num_constraints += 1; + } + + fn push_namespace(&mut self, _: N) + where + NR: Into, + 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 + } +} \ No newline at end of file