R1CS to QAP

In this module I explored R1CS arithmetization and it transformation to QAP

This section would be diving how a problem represented R1CS can be transformed into QAP

Explore these chapter of the Rareskill book:

let's play with a quick example on transforming R1CS to QAP.

The Rank one constraint system is a very flexible way to represent an arithmetic circuit and to most proving systems, R1CS is the data structure in which the compute is presented to to the proving system.

In this write-up, I will be exploring how to go from an arithmetic circuit (in polynomial form) to QAP.

Example;

x3āˆ—x+5=21x^3 * x + 5 = 21

reducing this equation to a format that can be reduced to R1CS

a=xāˆ—xa = x * x
b=aāˆ—xb = a * x
outāˆ’5=bāˆ—xout - 5 = b * x

this is what the witness would look like, recall the witness consists of the;

w=[1,x,a,b,out]w = [1, x, a, b, out]
w=[1,2,4,8,21]w = [1,2,4,8,21]

recall the R1CS format is as follows; Cw=Awā‹…BwCw=Awā‹…Bw

to represent this in this format;

CC would be influenced by [a,b,outāˆ’5][a,b,out-5] AA would be influenced by [x,a,b][x,a,b] BB would be influenced by [x][x]

C=(0010000010āˆ’50001)C = \begin{pmatrix} 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ -5 & 0 & 0 & 0 & 1 \\ \end{pmatrix}
A=(010000010000010)A = \begin{pmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ \end{pmatrix}
B=(010000100001000)B = \begin{pmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ \end{pmatrix}

The R1CS representation of this would be given as;

(0010000010āˆ’50001)[1,2,4,8,21]=(010000010000010)[1,2,4,8,21]∘(010000100001000)[1,2,4,8,21]\begin{pmatrix} 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ -5 & 0 & 0 & 0 & 1 \\ \end{pmatrix} [1,2,4,8,21] = \begin{pmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 & 0 \\ \end{pmatrix} [1,2,4,8,21] ∘ \begin{pmatrix} 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 & 0 \\ \end{pmatrix} [1,2,4,8,21]

Now transforming this R1CS to its QAP representation.

In QAP we would be representing the R1CS structure using polynomials, but these polynomials completely implement the same logic done by the R1CS.

In the construction used, the number of polynomials & their degree would depend on the length of the solution vector & the number of gates. The solution vector contains 5 elements, so we can construct 5 polynomials. Each gate contributes 1 point to each polynomial. We have 3 gates here, we get 3 points per polynomial. 3 points allows us to define a polynomial of maximum degree 2. So we can construct 5 polynomials each with a maximum degree of 2. So we can transform our matrices collectively into 5 polynomials, each of degree 2.

for the first column of C; y=[0,0,āˆ’5]y = [0, 0, -5] x=[1,2,3]x=[1,2,3]

poly0=āˆ’52(xāˆ’2)(xāˆ’1)=āˆ’5x22+15x2āˆ’5 poly_0 = -\frac{5}{2} (x-2) (x-1) \\ = \\ -\frac{5 x^2}{2}+\frac{15 x}{2}-5

for the second column, there is no need to do this, because this would result in a Zero polynomial.

for the 3rd column of C; y=[1,0,0]y = [1, 0, 0] x=[1,2,3]x = [1, 2, 3]

poly2=(xāˆ’22āˆ’1)(xāˆ’1)+1=x22āˆ’5x2+3poly_2 = \left(\frac{x-2}{2}-1\right) (x-1)+1 \\ = \\ \frac{x^2}{2}-\frac{5 x}{2}+3 for the 4th column of C; y=[0,1,0]y = [0,1,0] x=[1,2,3]x=[1,2,3]

poly3=(3āˆ’x)(xāˆ’1)=āˆ’x2+4xāˆ’3poly_3 = (3-x) (x-1) \\ = \\ -x^2+4 x-3

for the 5th column of C; y=[0,0,1]y = [0,0,1] x=[1,2,3]x=[1,2,3]

poly4=12(xāˆ’2)(xāˆ’1)=x22āˆ’3x2+1poly_4 = \frac{1}{2} (x-2) (x-1) \\ = \\ \frac{x^2}{2}-\frac{3 x}{2}+1

This construct the QAP representation of C, we would be making use of the co-efficient of these vectors;

C=(āˆ’5152āˆ’520003āˆ’5212āˆ’34āˆ’112āˆ’322)C = \begin{pmatrix} -5 & \frac{15}{2} & \frac{-5}{2}\\ 0 & 0 & 0\\ 3 & \frac{-5}{2} & \frac{1}{2}\\ -3 & 4 & -1\\ \frac{1}{2} & \frac{-3}{2} & 2\\ \end{pmatrix}

This is how C would be represented in QAP form;

But we are not done yet, there is just one more step,

We have to multiply CC and ww, CC is of the dimension of 3X53X5 and ww is of the dimension of 5X15X1 this would result in a matrix of dimension 3x13x1, take this array as the co-efficient of a polynomial, that Polynomial is the QAP representation of CC.

It is truly epic seeing these huge matrices reduced to a single polynomial!

Last updated