1+ //! Trait definitions and implementations for arithmetic constraint systems.
2+ //!
3+ //! This module provides core abstractions for working with different types of arithmetic
4+ //! constraint systems like R1CS (Rank-1 Constraint Systems) and CCS (Customizable Constraint Systems).
5+ //! The key trait [`Arith`] defines a generic interface that constraint systems must implement,
6+ //! allowing the rest of the library to work with different constraint system implementations in a
7+ //! uniform way.
8+ //!
9+ //! # Key Traits
10+ //!
11+ //! * [`Arith`] - Core trait defining operations that constraint systems must support
12+ //! * [`ArithGadget`] - In-circuit counterpart operations for constraint systems
13+ //! * [`ArithSampler`] - Optional trait for sampling random satisfying witness-instance pairs
14+ //! * [`ArithSerializer`] - Serialization support for constraint systems
15+ //!
16+ //! # Modules
17+ //!
18+ //! * [`ccs`] - Implementation of Customizable Constraint Systems (CCS)
19+ //! * [`r1cs`] - Implementation of Rank-1 Constraint Systems (R1CS)
20+ //!
21+ //! # Examples of Supported Systems
22+ //!
23+ //! The module supports various constraint system types including:
24+ //!
25+ //! * Plain R1CS - Traditional rank-1 constraint systems
26+ //! * Nova's Relaxed R1CS - Modified R1CS with relaxation terms
27+ //! * ProtoGalaxy's Relaxed R1CS - Alternative relaxation approach
28+ //! * CCS - Customizable constraint systems with different witness/instance types
29+ //!
30+ //! Each system can define its own witness (`W`) and instance (`U`) types while implementing
31+ //! the common [`Arith`] interface, allowing for flexible constraint system implementations
32+ //! while maintaining a consistent API.
33+ //!
34+ //! # Evaluation Types
35+ //!
36+ //! Different constraint systems can have different evaluation semantics:
37+ //!
38+ //! * Plain R1CS evaluates to `Az ∘ Bz - Cz`
39+ //! * Nova's Relaxed R1CS evaluates to `Az ∘ Bz - uCz`
40+ //! * ProtoGalaxy's version evaluates to `Az ∘ Bz - Cz`
41+ //! * CCS can have various evaluation forms
42+ //!
43+ //! The [`Arith`] trait's associated `Evaluation` type allows each system to define its own
44+ //! evaluation result type while maintaining type safety.
45+
146use ark_ec:: CurveGroup ;
247use ark_relations:: r1cs:: SynthesisError ;
348use ark_std:: rand:: RngCore ;
@@ -42,6 +87,7 @@ pub mod r1cs;
4287/// elements, [`crate::folding::hypernova::Witness`] and [`crate::folding::hypernova::lcccs::LCCCS`],
4388/// or [`crate::folding::hypernova::Witness`] and [`crate::folding::hypernova::cccs::CCCS`].
4489pub trait Arith < W , U > : Clone {
90+ /// The type for the output of the relation's evalation.
4591 type Evaluation ;
4692
4793 /// Returns a dummy witness and instance
@@ -64,11 +110,16 @@ pub trait Arith<W, U>: Clone {
64110 /// - Evaluating the relaxed R1CS in Nova at `W = (w, e, ...)` and
65111 /// `U = (u, x, ...)` returns `Az ∘ Bz - uCz`, where `z = [u, x, w]`.
66112 ///
67- /// - Evaluating the relaxed R1CS in ProtoGalaxy at `W = (w, ...)` and
113+ /// - Evaluating the relaxed R1CS in [`crate::folding::protogalaxy`] at `W = (w, ...)` and
68114 /// `U = (x, e, β, ...)` returns `Az ∘ Bz - Cz`, where `z = [1, x, w]`.
69115 ///
70116 /// However, we use `Self::Evaluation` to represent the evaluation result
71117 /// for future extensibility.
118+ ///
119+ /// # Errors
120+ /// Returns an error if:
121+ /// - The dimensions of vectors don't match the expected sizes
122+ /// - The evaluation calculations fail
72123 fn eval_relation ( & self , w : & W , u : & U ) -> Result < Self :: Evaluation , Error > ;
73124
74125 /// Checks if the evaluation result is valid. The witness `w` and instance
@@ -82,16 +133,27 @@ pub trait Arith<W, U>: Clone {
82133 /// - The evaluation `v` of relaxed R1CS in Nova at satisfying `W` and `U`
83134 /// should be equal to the error term `e` in the witness.
84135 ///
85- /// - The evaluation `v` of relaxed R1CS in ProtoGalaxy at satisfying `W`
136+ /// - The evaluation `v` of relaxed R1CS in [`crate::folding::protogalaxy`] at satisfying `W`
86137 /// and `U` should satisfy `e = Σ pow_i(β) v_i`, where `e` is the error
87138 /// term in the committed instance.
139+ ///
140+ /// # Errors
141+ /// Returns an error if:
142+ /// - The evaluation result is not valid for the given witness and instance
143+ /// - The evaluation result fails to satisfy the constraint system requirements
88144 fn check_evaluation ( w : & W , u : & U , v : Self :: Evaluation ) -> Result < ( ) , Error > ;
89145
90146 /// Checks if witness `w` and instance `u` satisfy the constraint system
91147 /// `self` by first computing the evaluation result and then checking the
92148 /// validity of the evaluation result.
93149 ///
94150 /// Used only for testing.
151+ ///
152+ /// # Errors
153+ /// Returns an error if:
154+ /// - The evaluation fails (`eval_relation` errors)
155+ /// - The evaluation check fails (`check_evaluation` errors)
156+ /// - The witness and instance do not satisfy the constraint system
95157 fn check_relation ( & self , w : & W , u : & U ) -> Result < ( ) , Error > {
96158 let e = self . eval_relation ( w, u) ?;
97159 Self :: check_evaluation ( w, u, e)
@@ -125,6 +187,12 @@ pub trait ArithSerializer {
125187/// [HyperNova]: https://eprint.iacr.org/2023/573.pdf
126188pub trait ArithSampler < C : CurveGroup , W , U > : Arith < W , U > {
127189 /// Samples a random witness and instance that satisfy the constraint system.
190+ ///
191+ /// # Errors
192+ /// Returns an error if:
193+ /// - Sampling of random values fails
194+ /// - The commitment scheme operations fail
195+ /// - The sampled values do not satisfy the constraint system
128196 fn sample_witness_instance < CS : CommitmentScheme < C , true > > (
129197 & self ,
130198 params : & CS :: ProverParams ,
@@ -135,15 +203,28 @@ pub trait ArithSampler<C: CurveGroup, W, U>: Arith<W, U> {
135203/// `ArithGadget` defines the in-circuit counterparts of operations specified in
136204/// `Arith` on constraint systems.
137205pub trait ArithGadget < WVar , UVar > {
206+ /// The type for the output of the relation's evalation.
138207 type Evaluation ;
139208
140209 /// Evaluates the constraint system `self` at witness `w` and instance `u`.
141210 /// Returns the evaluation result.
211+ ///
212+ /// # Errors
213+ /// Returns a `SynthesisError` if:
214+ /// - Circuit constraint generation fails
215+ /// - Variable allocation fails
216+ /// - Required witness values are missing
142217 fn eval_relation ( & self , w : & WVar , u : & UVar ) -> Result < Self :: Evaluation , SynthesisError > ;
143218
144219 /// Generates constraints for enforcing that witness `w` and instance `u`
145220 /// satisfy the constraint system `self` by first computing the evaluation
146221 /// result and then checking the validity of the evaluation result.
222+ ///
223+ /// # Errors
224+ /// Returns a `SynthesisError` if:
225+ /// - The evaluation fails (`eval_relation` errors)
226+ /// - The enforcement of evaluation constraints fails
227+ /// - Circuit constraint generation fails
147228 fn enforce_relation ( & self , w : & WVar , u : & UVar ) -> Result < ( ) , SynthesisError > {
148229 let e = self . eval_relation ( w, u) ?;
149230 Self :: enforce_evaluation ( w, u, e)
@@ -152,5 +233,10 @@ pub trait ArithGadget<WVar, UVar> {
152233 /// Generates constraints for enforcing that the evaluation result is valid.
153234 /// The witness `w` and instance `u` are also parameters, because the
154235 /// validity check may need information contained in `w` and/or `u`.
236+ ///
237+ /// # Errors
238+ /// Returns a `SynthesisError` if:
239+ /// - Circuit constraint generation fails for the evaluation check
240+ /// - The enforcement of evaluation constraints fails
155241 fn enforce_evaluation ( w : & WVar , u : & UVar , e : Self :: Evaluation ) -> Result < ( ) , SynthesisError > ;
156242}
0 commit comments