R1CS (Rank-1 Constraint System)
Overview
R1CS is the constraint language used by SNARKs such as Groth16, Spartan, and Marlin. Every computation is encoded as a set of constraints of the form:
where is the witness vector (all intermediate values plus public inputs) and are coefficient vectors. Each constraint captures exactly one multiplication gate.
Structure
An R1CS instance consists of three matrices where is the number of constraints and is the number of variables. The prover must find a witness such that:
where denotes the Hadamard (element-wise) product.
What costs a constraint
- Multiplication: each field multiplication costs one R1CS constraint
- Addition: free (absorbed into the linear combinations )
- Constant multiplication: free (scalar factor in the linear combination)
- Exponentiation : costs constraints via square-and-multiply
Example: in R1CS
Computing requires 3 multiplication constraints:
| Step | Constraint | New variable |
|---|---|---|
Cost model for AO hash functions
In R1CS, the dominant cost comes from the S-box layer because each S-box evaluation requires multiple multiplications.
S-box costs
| S-box type | Expression | R1CS constraints |
|---|---|---|
| (cube) | 2 | |
| See example above | 3 | |
| 4 | ||
| Must compute explicitly | Same as via witness inversion | |
| Flystel (Anemoi) | Quadratic subfunction | ~4 |
Impact of partial rounds
Poseidon's partial rounds apply the S-box to only one state element instead of all elements. In R1CS this saves constraints per partial round, where is the constraint cost of one S-box evaluation.
For Poseidon with and :
- Full round: constraints (S-box) + 0 (MDS, linear)
- Partial round: constraints (S-box)
- Total: constraints for S-boxes alone
Inverse S-box cost
Rescue uses both and in alternating rounds. In R1CS, computing is done by having the prover supply as a witness and adding the constraint . This costs the same as , so Rescue's alternating structure provides no R1CS savings compared to using in every round.
MDS layer
The MDS matrix multiplication is a linear operation. In R1CS, linear operations are free because they are absorbed into the linear combinations of the next constraint. The MDS layer adds zero additional constraints.
Proof systems using R1CS
- Groth16: constant-size proofs (3 group elements), trusted setup per circuit
- Spartan: transparent setup, uses sum-check protocol
- Marlin: universal trusted setup (updateable)
References
- Gennaro, Gentry, Parno, Raykova. "Quadratic Span Programs and Succinct NIZKs without PCPs" (EUROCRYPT 2013) ePrint 2012/215
- Groth. "On the Size of Pairing-based Non-interactive Arguments" (EUROCRYPT 2016) ePrint 2016/260
- Setty. "Spartan: Efficient and general-purpose zkSNARKs without trusted setup" (CRYPTO 2020) ePrint 2019/550