Summarizing R1CS transformation
R1CS encodes arithmetic circuits as sets of constraints where products of linear combinations of wires equal another linear combination, enabling succinct zero-knowledge proofs via zk-SNARKs. The trusted setup generates a common reference string with trapdoor elements that are critical for security and must be produced via multi-party computation to prevent misuse.
A typical “NP statement” you want to prove in systems such as Groth16 or Pinocchio is of the form
There exists an assignment to the inputs and intermediate wires of this arithmetic circuit such that, when you evaluate it over a field ( \mathbb{F}_p), the output wire has the prescribed value.
At the front end these statements are described as circuits built from additions and multiplications mod (p). The back end of a SNARK does not work directly with gates and wires, it expects a system of quadratic equations of a very particular shape: a rank‑1 constraint system.
An R1CS consists of three sets of vectors (A_i,B_i,C_i\in\mathbb{F}_p^n) and asks you to find a vector (\mathbf{w}\in\mathbb{F}_p^n) such that for all constraints (i),
[
\bigl\langle A_i,\mathbf{w}\bigr\rangle \cdot \bigl\langle B_i,\mathbf{w}\bigr\rangle ;=; \bigl\langle C_i,\mathbf{w}\bigr\rangle,
]
where (\langle\cdot,\cdot\rangle) is the usual dot product. Each inner product is a linear combination of the witness components, so each constraint has the rank‑1 form “linear × linear = linear”. There are standard, mechanical ways to turn your circuit into such a system:
- You give a variable (w_j) to every wire in the circuit, including constants and inputs.
- For an addition gate (w_k = w_i + w_j) you add a constraint with (A_i=1,A_j=1,A_k=-1) and all other entries zero, and take (B) as ([1,0,\dots,0]) so that (\langle A,\mathbf{w}\rangle \cdot \langle B,\mathbf{w}\rangle = 0 = \langle C,\mathbf{w}\rangle) encodes (w_i+w_j-w_k=0).
- For a multiplication gate (w_k = w_i \cdot w_j) you choose (A) with a 1 in position (i), (B) with a 1 in position (j), and (C) with a 1 in position (k), so that (\langle A,\mathbf{w}\rangle \cdot \langle B,\mathbf{w}\rangle = \langle C,\mathbf{w}\rangle) encodes (w_i\cdot w_j - w_k = 0).
By chaining these constraints for every gate you obtain an R1CS that is satisfiable if and only if the original circuit is satisfiable. Many implementations then go one step further and encode the R1CS as a quadratic arithmetic program (QAP): three sets of polynomials (U(x),V(x),W(x)) such that ((\sum_j w_j U_j(t))\cdot(\sum_j w_j V_j(t))-(\sum_j w_j W_j(t))) vanishes at a prescribed set of points. This polynomial form is what pairing‑based SNARKs commit to and test with a single pairing equation.
To “prove knowledge of a witness without revealing it”, the prover commits to their (\mathbf{w}) in a binding way and then uses the homomorphisms provided by the cryptographic setup to show that all the R1CS equations are satisfied. In Groth16, for example, the prover evaluates the QAP polynomials at a secret trapdoor value (s) that only exists in exponentiated form in the reference string, produces group elements (g^{\sum_j w_j U_j(s)}), (g^{\sum_j w_j V_j(s)}), etc., and a proof that the product relation holds. Zero‑knowledge is achieved by adding random blinding terms so that the group elements reveal nothing about (\mathbf{w}), while soundness is argued under a knowledge‑of‑exponent assumption: if you can produce a tuple of group elements satisfying the check, then you “know” coefficients (\mathbf{w}) that make all the constraints zero.
All of this relies on a common reference string (CRS) tailored to the chosen circuit or R1CS. The CRS contains group elements of the form (g^{s^i}) and, in zk‑SNARKs, also (g^{\alpha s^i}, g^{\beta s^i}, g^{\delta}), etc., for random, secret scalars (s,\alpha,\beta,\delta). These values let the prover do polynomial arithmetic “in the exponent” and the verifier do a handful of pairing checks; they must not learn (s,\alpha,\beta,\delta) themselves, because someone who knows all trapdoor values can forge proofs. The CRS is therefore generated in a “trusted setup” phase: a party or a multi‑party computation samples (s,\alpha,\beta,\dots), publishes the corresponding group elements, and destroys the sampled scalars. If at least one participant is honest and erases their “toxic waste”, no one can fake proofs, but everyone can use the CRS to produce and verify succinct zero‑knowledge proofs for that circuit.