Skip to content

Commit fdb68ff

Browse files
rubdosoleganza
andauthored
R1CS: Add constraints_len, deferred_constraints_len and total_constraints_len (#322)
* Add Metrics struct Add Metrics struct to count the different kinds of constraints, which comes in handy when developing and benchmarking larger circuits. This deprecates and removes the `multipliers_len()` method. * Add documentation for the new Metrics struct Co-authored-by: Oleg Andreev <[email protected]> Co-authored-by: Oleg Andreev <[email protected]>
1 parent 2ba1cef commit fdb68ff

File tree

5 files changed

+38
-10
lines changed

5 files changed

+38
-10
lines changed

src/r1cs/constraint_system.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ pub trait ConstraintSystem {
6666
input_assignments: Option<(Scalar, Scalar)>,
6767
) -> Result<(Variable, Variable, Variable), R1CSError>;
6868

69-
/// Counts the amount of allocated multipliers.
70-
fn multipliers_len(&self) -> usize;
69+
/// Counts the amount of constraints in the constraint system.
70+
fn metrics(&self) -> crate::r1cs::Metrics;
7171

7272
/// Enforce the explicit constraint that
7373
/// ```text

src/r1cs/metrics.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
/// A struct that contains metrics about a constraint system.
2+
///
3+
/// See [`ConstraintSystem::metrics`](::r1cs::ConstraintSystem::metrics).
4+
#[derive(Debug, Clone)]
5+
pub struct Metrics {
6+
/// Number of multiplicative constraints in the constraint system.
7+
pub multipliers: usize,
8+
/// Total number of linear constraints in the constraint system.
9+
pub constraints: usize,
10+
/// Number of linear constraints added in pre-randomization phase.
11+
pub phase_one_constraints: usize,
12+
/// Number of linear constraints added in the randomization phase.
13+
pub phase_two_constraints: usize,
14+
}

src/r1cs/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ mod notes {}
55

66
mod constraint_system;
77
mod linear_combination;
8+
mod metrics;
89
mod proof;
910
mod prover;
1011
mod verifier;
@@ -13,6 +14,7 @@ pub use self::constraint_system::{
1314
ConstraintSystem, RandomizableConstraintSystem, RandomizedConstraintSystem,
1415
};
1516
pub use self::linear_combination::{LinearCombination, Variable};
17+
pub use self::metrics::Metrics;
1618
pub use self::proof::R1CSProof;
1719
pub use self::prover::Prover;
1820
pub use self::verifier::Verifier;

src/r1cs/prover.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ use super::{
1616
use crate::errors::R1CSError;
1717
use crate::generators::{BulletproofGens, PedersenGens};
1818
use crate::inner_product_proof::InnerProductProof;
19+
use crate::r1cs::Metrics;
1920
use crate::transcript::TranscriptProtocol;
2021

2122
/// A [`ConstraintSystem`] implementation for use by the prover.
@@ -166,8 +167,13 @@ impl<'g, T: BorrowMut<Transcript>> ConstraintSystem for Prover<'g, T> {
166167
Ok((l_var, r_var, o_var))
167168
}
168169

169-
fn multipliers_len(&self) -> usize {
170-
self.secrets.a_L.len()
170+
fn metrics(&self) -> Metrics {
171+
Metrics {
172+
multipliers: self.secrets.a_L.len(),
173+
constraints: self.constraints.len() + self.deferred_constraints.len(),
174+
phase_one_constraints: self.constraints.len(),
175+
phase_two_constraints: self.deferred_constraints.len(),
176+
}
171177
}
172178

173179
fn constrain(&mut self, lc: LinearCombination) {
@@ -213,8 +219,8 @@ impl<'g, T: BorrowMut<Transcript>> ConstraintSystem for RandomizingProver<'g, T>
213219
self.prover.allocate_multiplier(input_assignments)
214220
}
215221

216-
fn multipliers_len(&self) -> usize {
217-
self.prover.multipliers_len()
222+
fn metrics(&self) -> Metrics {
223+
self.prover.metrics()
218224
}
219225

220226
fn constrain(&mut self, lc: LinearCombination) {

src/r1cs/verifier.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ use super::{
1414

1515
use crate::errors::R1CSError;
1616
use crate::generators::{BulletproofGens, PedersenGens};
17+
use crate::r1cs::Metrics;
1718
use crate::transcript::TranscriptProtocol;
1819

1920
/// A [`ConstraintSystem`] implementation for use by the verifier.
@@ -117,8 +118,13 @@ impl<T: BorrowMut<Transcript>> ConstraintSystem for Verifier<T> {
117118
Ok((l_var, r_var, o_var))
118119
}
119120

120-
fn multipliers_len(&self) -> usize {
121-
self.num_vars
121+
fn metrics(&self) -> Metrics {
122+
Metrics {
123+
multipliers: self.num_vars,
124+
constraints: self.constraints.len() + self.deferred_constraints.len(),
125+
phase_one_constraints: self.constraints.len(),
126+
phase_two_constraints: self.deferred_constraints.len(),
127+
}
122128
}
123129

124130
fn constrain(&mut self, lc: LinearCombination) {
@@ -165,8 +171,8 @@ impl<T: BorrowMut<Transcript>> ConstraintSystem for RandomizingVerifier<T> {
165171
self.verifier.allocate_multiplier(input_assignments)
166172
}
167173

168-
fn multipliers_len(&self) -> usize {
169-
self.verifier.multipliers_len()
174+
fn metrics(&self) -> Metrics {
175+
self.verifier.metrics()
170176
}
171177

172178
fn constrain(&mut self, lc: LinearCombination) {

0 commit comments

Comments
 (0)