Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R1CS: Add constraints_len, deferred_constraints_len and total_constraints_len #322

Merged
merged 2 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/r1cs/constraint_system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ pub trait ConstraintSystem {
input_assignments: Option<(Scalar, Scalar)>,
) -> Result<(Variable, Variable, Variable), R1CSError>;

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

/// Enforce the explicit constraint that
/// ```text
Expand Down
10 changes: 10 additions & 0 deletions src/r1cs/metrics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/// A struct that contains metrics about a constraint system.
///
/// See [`ConstraintSystem::metrics`](::r1cs::ConstraintSystem::metrics).
#[derive(Debug, Clone)]
pub struct Metrics {
pub multipliers: usize,
rubdos marked this conversation as resolved.
Show resolved Hide resolved
pub constraints: usize,
rubdos marked this conversation as resolved.
Show resolved Hide resolved
pub phase_one_constraints: usize,
rubdos marked this conversation as resolved.
Show resolved Hide resolved
pub phase_two_constraints: usize,
rubdos marked this conversation as resolved.
Show resolved Hide resolved
}
2 changes: 2 additions & 0 deletions src/r1cs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ mod notes {}

mod constraint_system;
mod linear_combination;
mod metrics;
mod proof;
mod prover;
mod verifier;
Expand All @@ -13,6 +14,7 @@ pub use self::constraint_system::{
ConstraintSystem, RandomizableConstraintSystem, RandomizedConstraintSystem,
};
pub use self::linear_combination::{LinearCombination, Variable};
pub use self::metrics::Metrics;
pub use self::proof::R1CSProof;
pub use self::prover::Prover;
pub use self::verifier::Verifier;
Expand Down
14 changes: 10 additions & 4 deletions src/r1cs/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ use super::{
use crate::errors::R1CSError;
use crate::generators::{BulletproofGens, PedersenGens};
use crate::inner_product_proof::InnerProductProof;
use crate::r1cs::Metrics;
use crate::transcript::TranscriptProtocol;

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

fn multipliers_len(&self) -> usize {
self.secrets.a_L.len()
fn metrics(&self) -> Metrics {
Metrics {
multipliers: self.secrets.a_L.len(),
constraints: self.constraints.len() + self.deferred_constraints.len(),
phase_one_constraints: self.constraints.len(),
phase_two_constraints: self.deferred_constraints.len(),
}
}

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

fn multipliers_len(&self) -> usize {
self.prover.multipliers_len()
fn metrics(&self) -> Metrics {
self.prover.metrics()
}

fn constrain(&mut self, lc: LinearCombination) {
Expand Down
14 changes: 10 additions & 4 deletions src/r1cs/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use super::{

use crate::errors::R1CSError;
use crate::generators::{BulletproofGens, PedersenGens};
use crate::r1cs::Metrics;
use crate::transcript::TranscriptProtocol;

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

fn multipliers_len(&self) -> usize {
self.num_vars
fn metrics(&self) -> Metrics {
Metrics {
multipliers: self.num_vars,
constraints: self.constraints.len() + self.deferred_constraints.len(),
phase_one_constraints: self.constraints.len(),
phase_two_constraints: self.deferred_constraints.len(),
}
}

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

fn multipliers_len(&self) -> usize {
self.verifier.multipliers_len()
fn metrics(&self) -> Metrics {
self.verifier.metrics()
}

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