ark_relations/gr1cs/mod.rs
1//! Core interface for working with Generalized Rank-1 Constraint Systems
2//! (GR1CS).
3mod constraint_system_ref;
4mod namespace;
5#[macro_use]
6mod constraint_system;
7mod assignment;
8
9pub(crate) mod field_interner;
10mod lc_map;
11
12/// Interface for specifying strategies for reducing the number of constraints
13/// that public input/instance variables are involved in.
14pub mod instance_outliner;
15pub mod predicate;
16
17#[cfg(feature = "std")]
18pub mod trace;
19
20#[cfg(test)]
21mod tests;
22///////////////////////////////////////////////////////////////////////////////////////
23
24#[cfg(feature = "std")]
25pub use crate::gr1cs::trace::{ConstraintLayer, ConstraintTrace, TraceStep, TracingMode};
26
27use ark_std::vec::Vec;
28pub use tracing::info_span;
29
30pub use ark_ff::{Field, ToConstraintField};
31
32pub use crate::{
33 gr1cs::{
34 assignment::Assignments, constraint_system::ConstraintSystem,
35 constraint_system_ref::ConstraintSystemRef,
36 predicate::polynomial_constraint::R1CS_PREDICATE_LABEL,
37 },
38 lc,
39 utils::{
40 error::SynthesisError,
41 linear_combination::LinearCombination,
42 matrix::{mat_vec_mul, transpose, Matrix},
43 variable::Variable,
44 Result,
45 },
46};
47pub use namespace::Namespace;
48///////////////////////////////////////////////////////////////////////////////////////
49
50/// Computations are expressed in terms of generalized rank-1 constraint systems
51/// (GR1CS). The `generate_constraints` method is called to generate constraints
52/// for both CRS generation and for proving.
53// TODO: Think: should we replace this with just a closure?
54pub trait ConstraintSynthesizer<F: Field> {
55 /// Drives generation of new constraints inside `cs`.
56 ///
57 /// # Errors
58 ///
59 /// Returns an error if constraints cannot be generated successfully.
60 fn generate_constraints(self, cs: ConstraintSystemRef<F>) -> crate::gr1cs::Result<()>;
61}
62
63///////////////////////////////////////////////////////////////////////////////////////
64
65/// In GR1CS a constraint is a vector of linear combinations associated with a
66/// predicate
67pub type Constraint = Vec<Variable>;
68
69/// Each predicate is associated with a label
70pub type Label = ark_std::string::String;
71///////////////////////////////////////////////////////////////////////////////////////
72
73/// Defines the mode of operation of a `ConstraintSystem`.
74#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
75pub enum SynthesisMode {
76 /// Indicate to the `ConstraintSystem` that it should only generate
77 /// constraint matrices and not populate the variable assignments.
78 Setup,
79 /// Indicate to the `ConstraintSystem` that it populate the variable
80 /// assignments. If additionally `construct_matrices == true`, then generate
81 /// the matrices as in the `Setup` case.
82 Prove {
83 /// If `construct_matrices == true`, then generate
84 /// the matrices as in the `Setup` case.
85 construct_matrices: bool,
86 /// If `construct_matrices == true`, then generate
87 /// the matrices as in the `Setup` case.
88 generate_lc_assignments: bool,
89 },
90}
91
92///////////////////////////////////////////////////////////////////////////////////////
93
94/// Defines the parameter to optimize for a `ConstraintSystem`.
95#[derive(Copy, Clone, Debug, Eq, PartialEq, Ord, PartialOrd)]
96pub enum OptimizationGoal {
97 /// Make no attempt to optimize.
98 None,
99 /// Minimize the number of constraints by inlining the linear combinations.
100 Constraints,
101 /// Minimize the total weight of the constraints (the number of nonzero
102 /// entries across all constraints) by outlining the linear combinations
103 /// and creating new witness variables.
104 #[deprecated]
105 Weight,
106}
107
108///////////////////////////////////////////////////////////////////////////////////////