Skip to content

Commit afdb17a

Browse files
committed
Work
1 parent 4f7fea5 commit afdb17a

File tree

1 file changed

+35
-17
lines changed

1 file changed

+35
-17
lines changed

relations/src/r1cs/constraint_system.rs

Lines changed: 35 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,11 @@ pub trait WitnessGenerator<F: Field>: InstanceGenerator<F> {
4444
#[derive(Debug, Clone)]
4545
pub struct ConstraintSystem<F: Field> {
4646
/// The mode in which the constraint system is operating. `self` can either
47-
/// be in setup mode (i.e., `self.mode == SynthesisMode::Setup`) or in
48-
/// proving mode (i.e., `self.mode == SynthesisMode::Prove`). If we are
49-
/// in proving mode, then we have the additional option of whether or
47+
/// be in setup mode (`self.mode == SynthesisMode::Setup`), in
48+
/// proving mode (`self.mode == SynthesisMode::Prove`), or in verifying mode
49+
/// (`self.mode == SynthesisMode::Verify`).
50+
///
51+
/// If we are in proving mode, then we have the additional option of whether or
5052
/// not to construct the A, B, and C matrices of the constraint system
5153
/// (see below).
5254
pub mode: SynthesisMode,
@@ -99,14 +101,17 @@ pub enum SynthesisMode {
99101
/// Indicate to the `ConstraintSystem` that it should only generate
100102
/// constraint matrices and not populate the variable assignments.
101103
Setup,
102-
/// Indicate to the `ConstraintSystem` that it populate the variable
104+
/// Indicate to the `ConstraintSystem` that it should populate the witnessvariable
103105
/// assignments. If additionally `construct_matrices == true`, then generate
104106
/// the matrices as in the `Setup` case.
105107
Prove {
106108
/// If `construct_matrices == true`, then generate
107109
/// the matrices as in the `Setup` case.
108110
construct_matrices: bool,
109111
},
112+
/// Indicate to the `ConstraintSystem` that it populate the instance variable
113+
/// assignments.
114+
Verify,
110115
}
111116

112117
/// Defines the parameter to optimize for a `ConstraintSystem`.
@@ -181,6 +186,11 @@ impl<F: Field> ConstraintSystem<F> {
181186
self.mode == SynthesisMode::Setup
182187
}
183188

189+
/// Check whether `self.mode == SynthesisMode::Verify`.
190+
pub fn is_in_verify_mode(&self) -> bool {
191+
self.mode == SynthesisMode::Verify
192+
}
193+
184194
/// Check whether this constraint system aims to optimize weight,
185195
/// number of constraints, or neither.
186196
pub fn optimization_goal(&self) -> OptimizationGoal {
@@ -204,6 +214,7 @@ impl<F: Field> ConstraintSystem<F> {
204214
match self.mode {
205215
SynthesisMode::Setup => true,
206216
SynthesisMode::Prove { construct_matrices } => construct_matrices,
217+
SynthesisMode::Verify => false,
207218
}
208219
}
209220

@@ -230,6 +241,8 @@ impl<F: Field> ConstraintSystem<F> {
230241
let index = self.num_instance_variables;
231242
self.num_instance_variables += 1;
232243

244+
// Only generate instance variable assignments when `self.mode` is either
245+
// `SynthesisMode::Prove` or `SynthesisMode::Verify`.
233246
if !self.is_in_setup_mode() {
234247
self.instance_assignment.push(f()?);
235248
}
@@ -245,7 +258,7 @@ impl<F: Field> ConstraintSystem<F> {
245258
let index = self.num_witness_variables;
246259
self.num_witness_variables += 1;
247260

248-
if !self.is_in_setup_mode() {
261+
if !(self.is_in_setup_mode() || self.is_in_verify_mode()) {
249262
self.witness_assignment.push(f()?);
250263
}
251264
Ok(Variable::Witness(index))
@@ -278,13 +291,14 @@ impl<F: Field> ConstraintSystem<F> {
278291
self.a_constraints.push(a_index);
279292
self.b_constraints.push(b_index);
280293
self.c_constraints.push(c_index);
294+
#[cfg(feature = "std")]
295+
{
296+
let trace = ConstraintTrace::capture();
297+
self.constraint_traces.push(trace);
298+
}
281299
}
282300
self.num_constraints += 1;
283-
#[cfg(feature = "std")]
284-
{
285-
let trace = ConstraintTrace::capture();
286-
self.constraint_traces.push(trace);
287-
}
301+
288302
Ok(())
289303
}
290304

@@ -538,10 +552,7 @@ impl<F: Field> ConstraintSystem<F> {
538552
/// after all symbolic LCs have been inlined into the places that they
539553
/// are used.
540554
pub fn to_matrices(&self) -> Option<ConstraintMatrices<F>> {
541-
if let SynthesisMode::Prove {
542-
construct_matrices: false,
543-
} = self.mode
544-
{
555+
if !self.should_construct_matrices() {
545556
None
546557
} else {
547558
let a: Vec<_> = self
@@ -601,7 +612,7 @@ impl<F: Field> ConstraintSystem<F> {
601612
/// the first unsatisfied constraint. If `self.is_in_setup_mode()`, outputs
602613
/// `Err(())`.
603614
pub fn which_is_unsatisfied(&self) -> crate::r1cs::Result<Option<String>> {
604-
if self.is_in_setup_mode() {
615+
if self.is_in_setup_mode() || self.is_in_verify_mode() {
605616
Err(SynthesisError::AssignmentMissing)
606617
} else {
607618
for i in 0..self.num_constraints {
@@ -753,7 +764,7 @@ impl<F: Field> ConstraintSystemRef<F> {
753764

754765
/// Consumes self to return the inner `ConstraintSystem<F>`. Returns
755766
/// `None` if `Self::CS` is `None` or if any other references to
756-
/// `Self::CS` exist.
767+
/// `Self::CS` exist.
757768
pub fn into_inner(self) -> Option<ConstraintSystem<F>> {
758769
match self {
759770
Self::CS(a) => Rc::try_unwrap(a).ok().map(|s| s.into_inner()),
@@ -791,6 +802,13 @@ impl<F: Field> ConstraintSystemRef<F> {
791802
.map_or(false, |cs| cs.borrow().is_in_setup_mode())
792803
}
793804

805+
/// Check whether `self.mode == SynthesisMode::Verify`.
806+
#[inline]
807+
pub fn is_in_verify_mode(&self) -> bool {
808+
self.inner()
809+
.map_or(false, |cs| cs.borrow().is_in_verify_mode())
810+
}
811+
794812
/// Returns the number of constraints.
795813
#[inline]
796814
pub fn num_constraints(&self) -> usize {
@@ -864,7 +882,7 @@ impl<F: Field> ConstraintSystemRef<F> {
864882
self.inner()
865883
.ok_or(SynthesisError::MissingCS)
866884
.and_then(|cs| {
867-
if !self.is_in_setup_mode() {
885+
if !(self.is_in_setup_mode() || self.is_in_verify_mode()) {
868886
// This is needed to avoid double-borrows, because `f`
869887
// might itself mutably borrow `cs` (eg: `f = || g.value()`).
870888
let value = f();

0 commit comments

Comments
 (0)