pub struct ConstraintSystem<F: Field> {
pub mode: SynthesisMode,
pub num_instance_variables: usize,
pub num_witness_variables: usize,
pub num_constraints: usize,
pub num_linear_combinations: usize,
pub optimization_goal: OptimizationGoal,
pub instance_assignment: Vec<F>,
pub witness_assignment: Vec<F>,
pub cache_map: Rc<RefCell<BTreeMap<TypeId, Box<dyn Any>>>>,
/* private fields */
}
Expand description
An Rank-One ConstraintSystem
. Enforces constraints of the form
⟨a_i, z⟩ ⋅ ⟨b_i, z⟩ = ⟨c_i, z⟩
, where a_i
, b_i
, and c_i
are linear
combinations over variables, and z
is the concrete assignment to these
variables.
Fields§
§mode: SynthesisMode
The mode in which the constraint system is operating. self
can either
be in setup mode (i.e., self.mode == SynthesisMode::Setup
) or in
proving mode (i.e., self.mode == SynthesisMode::Prove
). If we are
in proving mode, then we have the additional option of whether or
not to construct the A, B, and C matrices of the constraint system
(see below).
num_instance_variables: usize
The number of variables that are “public inputs” to the constraint system.
num_witness_variables: usize
The number of variables that are “private inputs” to the constraint system.
num_constraints: usize
The number of constraints in the constraint system.
num_linear_combinations: usize
The number of linear combinations
optimization_goal: OptimizationGoal
The parameter we aim to minimize in this constraint system (either the number of constraints or their total weight).
instance_assignment: Vec<F>
Assignments to the public input variables. This is empty if self.mode == SynthesisMode::Setup
.
witness_assignment: Vec<F>
Assignments to the private input variables. This is empty if self.mode == SynthesisMode::Setup
.
cache_map: Rc<RefCell<BTreeMap<TypeId, Box<dyn Any>>>>
Map for gadgets to cache computation results.
Implementations§
Source§impl<F: Field> ConstraintSystem<F>
impl<F: Field> ConstraintSystem<F>
Sourcepub fn new_ref() -> ConstraintSystemRef<F>
pub fn new_ref() -> ConstraintSystemRef<F>
Create a new ConstraintSystemRef<F>
.
Sourcepub fn set_mode(&mut self, mode: SynthesisMode)
pub fn set_mode(&mut self, mode: SynthesisMode)
Set self.mode
to mode
.
Sourcepub fn is_in_setup_mode(&self) -> bool
pub fn is_in_setup_mode(&self) -> bool
Check whether self.mode == SynthesisMode::Setup
.
Sourcepub fn optimization_goal(&self) -> OptimizationGoal
pub fn optimization_goal(&self) -> OptimizationGoal
Check whether this constraint system aims to optimize weight, number of constraints, or neither.
Sourcepub fn set_optimization_goal(&mut self, goal: OptimizationGoal)
pub fn set_optimization_goal(&mut self, goal: OptimizationGoal)
Specify whether this constraint system should aim to optimize weight, number of constraints, or neither.
Sourcepub fn should_construct_matrices(&self) -> bool
pub fn should_construct_matrices(&self) -> bool
Check whether or not self
will construct matrices.
Sourcepub fn zero() -> Variable
pub fn zero() -> Variable
Return a variable representing the constant “zero” inside the constraint system.
Sourcepub fn one() -> Variable
pub fn one() -> Variable
Return a variable representing the constant “one” inside the constraint system.
Sourcepub fn new_input_variable<Func>(&mut self, f: Func) -> Result<Variable>
pub fn new_input_variable<Func>(&mut self, f: Func) -> Result<Variable>
Obtain a variable representing a new public instance input.
Sourcepub fn new_witness_variable<Func>(&mut self, f: Func) -> Result<Variable>
pub fn new_witness_variable<Func>(&mut self, f: Func) -> Result<Variable>
Obtain a variable representing a new private witness input.
Sourcepub fn new_lc(&mut self, lc: LinearCombination<F>) -> Result<Variable>
pub fn new_lc(&mut self, lc: LinearCombination<F>) -> Result<Variable>
Obtain a variable representing a linear combination.
Sourcepub fn enforce_constraint(
&mut self,
a: LinearCombination<F>,
b: LinearCombination<F>,
c: LinearCombination<F>,
) -> Result<()>
pub fn enforce_constraint( &mut self, a: LinearCombination<F>, b: LinearCombination<F>, c: LinearCombination<F>, ) -> Result<()>
Enforce a R1CS constraint with the name name
.
Sourcepub fn transform_lc_map(
&mut self,
transformer: &mut dyn FnMut(&ConstraintSystem<F>, usize, &mut LinearCombination<F>) -> (usize, Option<Vec<F>>),
)
pub fn transform_lc_map( &mut self, transformer: &mut dyn FnMut(&ConstraintSystem<F>, usize, &mut LinearCombination<F>) -> (usize, Option<Vec<F>>), )
Transform the map of linear combinations. Specifically, allow the creation of additional witness assignments.
This method is used as a subroutine of inline_all_lcs
and outline_lcs
.
The transformer function is given a references of this constraint system (&self),
number of times used, and a mutable reference of the linear combination to be transformed.
(&ConstraintSystem
The transformer function returns the number of new witness variables needed
and a vector of new witness assignments (if not in the setup mode).
(usize, Option<Vec
Sourcepub fn inline_all_lcs(&mut self)
pub fn inline_all_lcs(&mut self)
Naively inlines symbolic linear combinations into the linear combinations that use them.
Useful for standard pairing-based SNARKs where addition gates are cheap. For example, in the SNARKs such as [Groth16] and [Groth-Maller17], addition gates do not contribute to the size of the multi-scalar multiplication, which is the dominating cost.
Sourcepub fn finalize(&mut self)
pub fn finalize(&mut self)
Finalize the constraint system (either by outlining or inlining, if an optimization goal is set).
Sourcepub fn to_matrices(&self) -> Option<ConstraintMatrices<F>>
pub fn to_matrices(&self) -> Option<ConstraintMatrices<F>>
This step must be called after constraint generation has completed, and after all symbolic LCs have been inlined into the places that they are used.
Sourcepub fn is_satisfied(&self) -> Result<bool>
pub fn is_satisfied(&self) -> Result<bool>
If self
is satisfied, outputs Ok(true)
.
If self
is unsatisfied, outputs Ok(false)
.
If self.is_in_setup_mode()
, outputs Err(())
.
Sourcepub fn which_is_unsatisfied(&self) -> Result<Option<String>>
pub fn which_is_unsatisfied(&self) -> Result<Option<String>>
If self
is satisfied, outputs Ok(None)
.
If self
is unsatisfied, outputs Some(i)
, where i
is the index of
the first unsatisfied constraint. If self.is_in_setup_mode()
, outputs
Err(())
.
Sourcepub fn assigned_value(&self, v: Variable) -> Option<F>
pub fn assigned_value(&self, v: Variable) -> Option<F>
Obtain the assignment corresponding to the Variable
v
.
Trait Implementations§
Source§impl<F: Clone + Field> Clone for ConstraintSystem<F>
impl<F: Clone + Field> Clone for ConstraintSystem<F>
Source§fn clone(&self) -> ConstraintSystem<F>
fn clone(&self) -> ConstraintSystem<F>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl<F> Freeze for ConstraintSystem<F>
impl<F> !RefUnwindSafe for ConstraintSystem<F>
impl<F> !Send for ConstraintSystem<F>
impl<F> !Sync for ConstraintSystem<F>
impl<F> Unpin for ConstraintSystem<F>where
F: Unpin,
impl<F> !UnwindSafe for ConstraintSystem<F>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)