R1CS
Introduction
The Rank 1 Constraint System (R1CS) is one of the common arithmetization techniques used today. In short, it uses 3 matrices to constrain two inputs to the gate and 1 output of the gate.
If the equation above is valid for a given A,B,C constraint matrices, we consider z a valid witness (also known as trace).
So how do we get these constraint matrices for a target computation? For the sake of simplicity, let's say we want to prove that we know a solution to x3+x+5=35.
Flattening to gates
First we convert our x3+x+5 program into simple statements which typically look like x=y or x=y⊕z. So x3+x+5 can be flattened to:
sym_1 = x * x
y = sym_1 * x
sym_2 = y + x
out = sym_2 + 5Gates to R1CS
In this step we convert the equations above into an R1CS matrix representation. At each row of R1CS matrices, we have 3 row vectors a,b,c which we multiply with column vector z. This solution vector z should satisfy
Each vector's length is equal to the number of variables in our program plus 1 for constant 1. So in the case above, we have ~one,x,sym1,sym2,sym3,~out so we need vectors of size 6. We need the extra constant 1 or identity multiplier to deal with situations involving addition in our scheme as well as to add constants. So in total for our case above we have:
The reason for having three vectors is due to the nature of the constraints R1CS is designed to represent. Remember, in circuit computation, we have conceptual gates. These gates have a left input, a right input, and one output. So, basically, the a vector represents the left input, the b vector represents the right input, and the c vector represents the output.
To express sym1=x⋅x, what we need to say is input 1 and input 2 is x and result is sym1
Similarly, to express y=sym1⋅x:
For sym2=(x+y)⋅1, we have:
And for ~out=(sym2+5)⋅1, we have:
This gives us the R1CS matrix representation:
So by knowing z=[1,3,35,9,27,30], you prove that you know the solution since it satisfies all the constraints.
References
Written by Batzorig Zorigoo of Fractalyze
Last updated