Path: blob/main/frontier/10-snarks-starks/sage/10b-r1cs-constraints.ipynb
483 views
Notebook 10b: R1CS Constraints
Module 10. SNARKs and STARKs
Motivating Question. We can represent computations as arithmetic circuits with addition and multiplication gates (Notebook 10a). But proof systems need a more structured format, one where every constraint has the same shape. Rank-1 Constraint Systems (R1CS) rewrite each multiplication gate as a matrix equation: . How do we transform a circuit into matrices, and what is the "witness" vector ?
Prerequisites. You should be comfortable with:
Arithmetic circuits and flattening (Notebook 10a)
Matrix-vector operations over finite fields (Module 02)
Learning objectives. By the end of this notebook you will be able to:
Transform an arithmetic circuit into R1CS matrices , , .
Construct the witness vector from a circuit evaluation.
Verify that a witness satisfies all R1CS constraints.
Understand why R1CS is the standard intermediate representation for SNARKs.
1. From Circuits to Constraints
Bridge from Notebook 10a. In the previous notebook, we represented as a sequence of gates. Now we'll convert each multiplication gate into a rank-1 constraint. Addition gates get absorbed into the constraint structure, they're free!
A Rank-1 Constraint System over a field consists of:
A witness vector containing all wire values (including a leading ).
Three matrices , one row per constraint.
The constraint: for each row , .
Each multiplication gate becomes one row. Addition gates are incorporated by adjusting matrix entries.
2. The Witness Vector
For our running example , the flattened circuit is:
| Gate | Operation | Constraint |
|---|---|---|
| 1 | multiplication | |
| 2 | multiplication | |
| 3 | addition (free!) | |
| 4 | addition (free!) |
The witness vector contains all values:
The leading is a convention that lets us include constants in constraints.
3. Building the R1CS Matrices
Each multiplication gate becomes a constraint .
Gate 1:
Left input: → , so
Right input: → , so
Output: → , so
Gate 2:
Left: →
Right: →
Output: →
What about the addition gates? Gate 3 says . We don't create a separate constraint, instead, wherever appears later, we substitute . Gate 4 says . We add an output constraint: , which can be written as the multiplication .
Gate 3 (output):
Left: →
Right: → (the 5 is in the
oneposition)Output: →
4. Checking the Witness
A witness satisfies R1CS if and only if for every row :
Checkpoint 1. R1CS is a completeness and soundness check:
Completeness: If the prover honestly evaluates the circuit, the witness satisfies all constraints.
Soundness: If any wire value is wrong, at least one constraint will fail.
The matrices , , are public. Only the witness is (partially) secret.
5. Why Only Multiplication Gates?
Addition gates don't become separate constraints because addition is linear. If , we can always substitute wherever appears in the A, B, or C vectors.
Multiplication is the only non-linear operation, so it's the only one that needs its own constraint.
| Operation | R1CS Cost | Why |
|---|---|---|
| 1 constraint | Non-linear, needs explicit check | |
| 0 constraints | Linear, absorbed into adjacent constraints | |
| (constant mul) | 0 constraints | Linear, absorbed as coefficient |
This is why circuit designers count multiplication gates as the true measure of circuit complexity.
6. A Second Example:
Let's build R1CS for a simpler function to reinforce the process.
7. Building R1CS Programmatically
Let's write a function that converts a flat circuit into R1CS matrices.
Checkpoint 2. The auto-generated R1CS only captures the multiplication gates. In a full SNARK system, the output equality constraint () is enforced separately by the verifier comparing public inputs. The R1CS ensures internal consistency of the computation.
8. Hadamard Product View
We can express all constraints at once using the Hadamard product (element-wise multiplication):
where denotes element-wise multiplication of vectors.
9. R1CS Size and Sparsity
R1CS matrices are typically very sparse, each row has only a few non-zero entries. This sparsity is exploited for efficiency.
Misconception alert. "R1CS constraints are hard to write." For simple circuits, yes, it's tedious. In practice, nobody writes R1CS by hand. Tools like Circom (a domain-specific language) compile high-level circuits to R1CS automatically. We do it by hand here to understand what those tools generate.
10. R1CS for a Multi-Variable Function
Let's build R1CS for .
Flattening:
(multiplication)
(addition, free)
(multiplication)
Checkpoint 3. The key insight of R1CS: every computation reduces to checking that three matrices, multiplied by the witness vector and then Hadamard-multiplied, produce zero. This uniform structure is exactly what the QAP polynomial transformation (next notebook) needs to convert discrete constraint checking into a single polynomial divisibility check.
11. Exercises
Exercise 1 (Worked): R1CS for
Problem. Write R1CS matrices for . Construct the witness for and verify.
Solution:
Exercise 2 (Guided): R1CS for
Problem. Write R1CS for . Test with .
Fill in the TODOs:
Exercise 3 (Independent): Boolean R1CS
Problem.
Write R1CS for the computation: "Given Boolean inputs , compute ."
Add Boolean constraints: and .
Build the full R1CS (3 constraints total) and test with all 4 input combinations.
Show that a non-Boolean witness (e.g., ) fails the Boolean constraint.
Summary
| Concept | Key Fact |
|---|---|
| R1CS | Rank-1 Constraint System: |
| Witness | Vector containing all wire values (including leading 1) |
| Multiplication gates | Each becomes one R1CS constraint |
| Addition gates | Free, absorbed into matrix entries |
| Hadamard form | , all constraints at once |
| Sparsity | R1CS matrices have very few non-zero entries per row |
R1CS transforms a circuit into a uniform algebraic structure. In the next notebook, we'll use polynomial interpolation to convert these discrete constraints into a single polynomial equation, the Quadratic Arithmetic Program (QAP).
Next: 10c: QAP Construction