pub struct ConstraintSystem<F: Field> {
pub assignments: Assignments<F>,
pub cache_map: Rc<RefCell<BTreeMap<TypeId, Box<dyn Any>>>>,
/* private fields */
}Expand description
A GR1CS ConstraintSystem. Enforces constraints of the form
L_i(⟨M_{i,1}, z⟩ , ⟨M_{i,2}, z⟩ , ..., ⟨M_{i,t_i}, z⟩) = 0
More Information: https://eprint.iacr.org/2024/1245
Fields§
§assignments: Assignments<F>Assignments to the input, witness, and lc 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() -> Self
pub fn new() -> Self
Create a new and empty ConstraintSystem<F>.
Note that by default, the constraint system is
registered with the R1CS predicate.
Sourcepub fn new_ref() -> ConstraintSystemRef<F>
pub fn new_ref() -> ConstraintSystemRef<F>
Create a new ConstraintSystemRef<F>.
Sourcepub fn get_all_predicates_num_constraints(&self) -> IndexMap<Label, usize>
pub fn get_all_predicates_num_constraints(&self) -> IndexMap<Label, usize>
Returns a mapping from predicate label to number of constraints for that predicate
Sourcepub fn get_predicate_num_constraints(
&self,
predicate_label: &str,
) -> Option<usize>
pub fn get_predicate_num_constraints( &self, predicate_label: &str, ) -> Option<usize>
Returns the number of constraints for a given predicate
Sourcepub fn get_all_predicate_arities(&self) -> IndexMap<Label, usize>
pub fn get_all_predicate_arities(&self) -> IndexMap<Label, usize>
Returns a mapping from predicate label to arity for that predicate
Sourcepub fn get_predicate_arity(&self, predicate_label: &str) -> Option<usize>
pub fn get_predicate_arity(&self, predicate_label: &str) -> Option<usize>
Returns the type of the predicate with the given label
Sourcepub fn get_all_predicate_types(&self) -> BTreeMap<Label, Predicate<F>>
pub fn get_all_predicate_types(&self) -> BTreeMap<Label, Predicate<F>>
Returns a mapping from predicate labels to their types
Sourcepub fn get_predicate_type(&self, predicate_label: &str) -> Option<Predicate<F>>
pub fn get_predicate_type(&self, predicate_label: &str) -> Option<Predicate<F>>
Returns the type of the predicate with the given label
Sourcepub fn instance_assignment(&self) -> Result<&[F]>
pub fn instance_assignment(&self) -> Result<&[F]>
Returns the assignment to the public input variables of the constraint
Sourcepub fn witness_assignment(&self) -> Result<&[F]>
pub fn witness_assignment(&self) -> Result<&[F]>
Returns the assignment to the private input variables of the constraint
Sourcepub fn num_constraints(&self) -> usize
pub fn num_constraints(&self) -> usize
Returns the number of constraints which is the sum of the number of constraints in each predicate.
Sourcepub fn num_instance_variables(&self) -> usize
pub fn num_instance_variables(&self) -> usize
Returns the number of instance variables.
Sourcepub fn num_witness_variables(&self) -> usize
pub fn num_witness_variables(&self) -> usize
Returns the number of witness variables.
Sourcepub fn num_variables(&self) -> usize
pub fn num_variables(&self) -> usize
Returns the number of witness variables.
Sourcepub fn num_predicates(&self) -> usize
pub fn num_predicates(&self) -> usize
Returns the number of predicates in the constraint system
Sourcepub fn enforce_constraint(
&mut self,
predicate_label: &str,
lcs: impl IntoIterator<Item = Box<dyn FnOnce() -> LinearCombination<F>>, IntoIter: ExactSizeIterator>,
) -> Result<()>
pub fn enforce_constraint( &mut self, predicate_label: &str, lcs: impl IntoIterator<Item = Box<dyn FnOnce() -> LinearCombination<F>>, IntoIter: ExactSizeIterator>, ) -> Result<()>
Enforce a constraint in the constraint system. It takes a predicate name and enforces a vector of linear combinations of the length of the arity of the predicate enforces the constraint.
Sourcepub fn enforce_constraint_arity_2(
&mut self,
predicate_label: &str,
a: impl FnOnce() -> LinearCombination<F>,
b: impl FnOnce() -> LinearCombination<F>,
) -> Result<()>
pub fn enforce_constraint_arity_2( &mut self, predicate_label: &str, a: impl FnOnce() -> LinearCombination<F>, b: impl FnOnce() -> LinearCombination<F>, ) -> Result<()>
Enforce a constraint for a predicate with arity 2.
Sourcepub fn enforce_constraint_arity_3(
&mut self,
predicate_label: &str,
a: impl FnOnce() -> LinearCombination<F>,
b: impl FnOnce() -> LinearCombination<F>,
c: impl FnOnce() -> LinearCombination<F>,
) -> Result<()>
pub fn enforce_constraint_arity_3( &mut self, predicate_label: &str, a: impl FnOnce() -> LinearCombination<F>, b: impl FnOnce() -> LinearCombination<F>, c: impl FnOnce() -> LinearCombination<F>, ) -> Result<()>
Enforce a constraint for a predicate with arity 3.
Sourcepub fn enforce_constraint_arity_4(
&mut self,
predicate_label: &str,
a: impl FnOnce() -> LinearCombination<F>,
b: impl FnOnce() -> LinearCombination<F>,
c: impl FnOnce() -> LinearCombination<F>,
d: impl FnOnce() -> LinearCombination<F>,
) -> Result<()>
pub fn enforce_constraint_arity_4( &mut self, predicate_label: &str, a: impl FnOnce() -> LinearCombination<F>, b: impl FnOnce() -> LinearCombination<F>, c: impl FnOnce() -> LinearCombination<F>, d: impl FnOnce() -> LinearCombination<F>, ) -> Result<()>
Enforce a constraint for a predicate with arity 4.
Sourcepub fn enforce_constraint_arity_5(
&mut self,
predicate_label: &str,
a: impl FnOnce() -> LinearCombination<F>,
b: impl FnOnce() -> LinearCombination<F>,
c: impl FnOnce() -> LinearCombination<F>,
d: impl FnOnce() -> LinearCombination<F>,
e: impl FnOnce() -> LinearCombination<F>,
) -> Result<()>
pub fn enforce_constraint_arity_5( &mut self, predicate_label: &str, a: impl FnOnce() -> LinearCombination<F>, b: impl FnOnce() -> LinearCombination<F>, c: impl FnOnce() -> LinearCombination<F>, d: impl FnOnce() -> LinearCombination<F>, e: impl FnOnce() -> LinearCombination<F>, ) -> Result<()>
Enforce a constraint for a predicate with arity 5.
Sourcepub fn enforce_r1cs_constraint(
&mut self,
a: impl FnOnce() -> LinearCombination<F>,
b: impl FnOnce() -> LinearCombination<F>,
c: impl FnOnce() -> LinearCombination<F>,
) -> Result<()>
pub fn enforce_r1cs_constraint( &mut self, a: impl FnOnce() -> LinearCombination<F>, b: impl FnOnce() -> LinearCombination<F>, c: impl FnOnce() -> LinearCombination<F>, ) -> Result<()>
Enforce a constraint in the constraint system. It takes a predicate name and enforces a vector of linear combinations of the length of the arity of the predicate enforces the constraint.
Sourcepub fn enforce_sr1cs_constraint(
&mut self,
a: impl FnOnce() -> LinearCombination<F>,
b: impl FnOnce() -> LinearCombination<F>,
) -> Result<()>
pub fn enforce_sr1cs_constraint( &mut self, a: impl FnOnce() -> LinearCombination<F>, b: impl FnOnce() -> LinearCombination<F>, ) -> Result<()>
Enforce a constraint in the constraint system. It takes a predicate name and enforces a vector of linear combinations of the length of the arity of the predicate enforces the constraint.
Sourcepub fn new_lc(
&mut self,
lc: impl FnOnce() -> LinearCombination<F>,
) -> Result<Variable>
pub fn new_lc( &mut self, lc: impl FnOnce() -> LinearCombination<F>, ) -> Result<Variable>
Adds a new linear combination to the constraint system.
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.
Returns true if
- There is an underlying
ConstraintSystem, and - It is in setup mode.
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 should_generate_lc_assignments(&self) -> bool
pub fn should_generate_lc_assignments(&self) -> bool
Check whether or not self will construct matrices.
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 register_predicate(
&mut self,
predicate_label: &str,
predicate: PredicateConstraintSystem<F>,
) -> Result<()>
pub fn register_predicate( &mut self, predicate_label: &str, predicate: PredicateConstraintSystem<F>, ) -> Result<()>
Register a predicate in the constraint system with a given label.
Sourcepub fn remove_predicate(&mut self, predicate_label: &str)
pub fn remove_predicate(&mut self, predicate_label: &str)
Remove the predicate with the given label from the constraint system.
Sourcepub fn has_predicate(&self, predicate_label: &str) -> bool
pub fn has_predicate(&self, predicate_label: &str) -> bool
check if there is a predicate with the given label
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.
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() or if self == None, 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(s,i), where s is the label of
the unsatisfied prediacate and i is the index of
the first unsatisfied constraint in that predicate.
If self.is_in_setup_mode() or self == None, outputs Err(()).
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 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 to_matrices(&self) -> Result<BTreeMap<Label, Vec<Matrix<F>>>>
pub fn to_matrices(&self) -> Result<BTreeMap<Label, Vec<Matrix<F>>>>
Get the matrices corresponding to the predicates.and the corresponding set of matrices
Sourcepub fn get_lc(&self, var: Variable) -> LinearCombination<F>
pub fn get_lc(&self, var: Variable) -> LinearCombination<F>
Get the linear combination corresponding to the given lc_index.
Sourcepub fn perform_instance_outlining(
&mut self,
outliner: InstanceOutliner<F>,
) -> Result<()>
pub fn perform_instance_outlining( &mut self, outliner: InstanceOutliner<F>, ) -> Result<()>
Outlines the instances in the constraint system.
This function creates a new witness variable for each instance
variable and uses these witness variables in the constraints
instead of instance variables. This technique is useful for
verifier succinctness in some SNARKs like Garuda, Pari and
PolyMath.
After the function call, instances are only
used in the C matrix of r1cs
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 more