Skip to content

Commit

Permalink
Add constraints_len, deferred_constraints_len and `total_constrai…
Browse files Browse the repository at this point in the history
…nts_len`.

Add methods to count the different kinds of constraints, which comes in
handy when developing and benchmarking larger circuits.
  • Loading branch information
rubdos committed May 14, 2020
1 parent d287c7a commit 67f4bd7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/r1cs/constraint_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,17 @@ pub trait ConstraintSystem {
/// Counts the amount of allocated multipliers.
fn multipliers_len(&self) -> usize;

/// Counts the amount of first-phase constraints in the circuit.
fn constraints_len(&self) -> usize;

/// Counts the amount of deferred or "second-phase" constraints in the circuit.
fn deferred_constraints_len(&self) -> usize;

/// Counts the total amount of constraints in the circuit.
fn total_constraints_len(&self) -> usize {
self.constraints_len() + self.deferred_constraints_len()
}

/// Enforce the explicit constraint that
/// ```text
/// lc = 0
Expand Down
16 changes: 16 additions & 0 deletions src/r1cs/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,14 @@ impl<'g, T: BorrowMut<Transcript>> ConstraintSystem for Prover<'g, T> {
self.secrets.a_L.len()
}

fn constraints_len(&self) -> usize {
self.constraints.len()
}

fn deferred_constraints_len(&self) -> usize {
self.deferred_constraints.len()
}

fn constrain(&mut self, lc: LinearCombination) {
// TODO: check that the linear combinations are valid
// (e.g. that variables are valid, that the linear combination evals to 0 for prover, etc).
Expand Down Expand Up @@ -213,6 +221,14 @@ impl<'g, T: BorrowMut<Transcript>> ConstraintSystem for RandomizingProver<'g, T>
self.prover.allocate_multiplier(input_assignments)
}

fn constraints_len(&self) -> usize {
self.prover.constraints.len()
}

fn deferred_constraints_len(&self) -> usize {
self.prover.deferred_constraints.len()
}

fn multipliers_len(&self) -> usize {
self.prover.multipliers_len()
}
Expand Down
16 changes: 16 additions & 0 deletions src/r1cs/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ impl<T: BorrowMut<Transcript>> ConstraintSystem for Verifier<T> {
self.num_vars
}

fn constraints_len(&self) -> usize {
self.constraints.len()
}

fn deferred_constraints_len(&self) -> usize {
self.deferred_constraints.len()
}

fn constrain(&mut self, lc: LinearCombination) {
// TODO: check that the linear combinations are valid
// (e.g. that variables are valid, that the linear combination
Expand Down Expand Up @@ -169,6 +177,14 @@ impl<T: BorrowMut<Transcript>> ConstraintSystem for RandomizingVerifier<T> {
self.verifier.multipliers_len()
}

fn constraints_len(&self) -> usize {
self.verifier.constraints.len()
}

fn deferred_constraints_len(&self) -> usize {
self.verifier.deferred_constraints.len()
}

fn constrain(&mut self, lc: LinearCombination) {
self.verifier.constrain(lc)
}
Expand Down

0 comments on commit 67f4bd7

Please sign in to comment.