# Quadratic Arithmetic Programs: from Zero to Hero

`def qeval(x):    y = x**3    return x + y + 5`

# Flattening

The first step is a “flattening” procedure, where we convert the original code, which may contain arbitrarily complex statements and expressions, into a sequence of statements that are of two forms:`x = y` (where `y` can be a variable or a number)and `x = y (op) z` (where `op` can be +, -, *, / and `y` and `z` can be variables, numbers or themselves sub-expressions). You can think of each of these statements as being kind of like logic gates in a circuit. The result of the flattening process for the above code is as follows:

`sym_1 = x * xy = sym_1 * xsym_2 = y + x~out = sym_2 + 5`

# Gates to R1CS

Now, we convert this into something called a rank-1 constraint system (R1CS). An R1CS is a sequence of groups of three vectors `(a, b, c)`, and the solution to an R1CS is a vector `s`, where `s` must satisfy the equation `s . a * s . b - s . c = 0`, where `.` represents the dot product - in simpler terms, if we "zip together" `a` and `s`, multiplying the two values in the same positions, and then take the sum of these products, then do the same to `b` and `s` and then `c` and `s`, then the third result equals the product of the first two results. For example, this is a satisfied R1CS:

`'~one', 'x', '~out', 'sym_1', 'y', 'sym_2'`
`a = [0, 1, 0, 0, 0, 0]b = [0, 1, 0, 0, 0, 0]c = [0, 0, 0, 1, 0, 0]`
`a = [0, 0, 0, 1, 0, 0]b = [0, 1, 0, 0, 0, 0]c = [0, 0, 0, 0, 1, 0]`
`a = [0, 1, 0, 0, 1, 0]b = [1, 0, 0, 0, 0, 0]c = [0, 0, 0, 0, 0, 1]`
`a = [5, 0, 0, 0, 0, 1]b = [1, 0, 0, 0, 0, 0]c = [0, 0, 1, 0, 0, 0]`
`[1, 3, 35, 9, 27, 30]`
`A[0, 1, 0, 0, 0, 0][0, 0, 0, 1, 0, 0][0, 1, 0, 0, 1, 0][5, 0, 0, 0, 0, 1]B[0, 1, 0, 0, 0, 0][0, 1, 0, 0, 0, 0][1, 0, 0, 0, 0, 0][1, 0, 0, 0, 0, 0]C[0, 0, 0, 1, 0, 0][0, 0, 0, 0, 1, 0][0, 0, 0, 0, 0, 1][0, 0, 1, 0, 0, 0]`

# R1CS to QAP

The next step is taking this R1CS and converting it into QAP form, which implements the exact same logic except using polynomials instead of dot products. We do this as follows. We go from four groups of three vectors of length six to six groups of three degree-3 polynomials, where evaluating the polynomials at each x coordinate represents one of the constraints. That is, if we evaluate the polynomials at x=1, then we get our first set of vectors, if we evaluate the polynomials at x=2, then we get our second set of vectors, and so on.

`(x - 2) * (x - 3)`
`(x - 2) * (x - 3) * 3 / ((1 - 2) * (1 - 3))`
`1.5 * x**2 - 7.5 * x + 9`
`1.5 * x**2 - 5.5 * x + 7`
`A polynomials[-5.0, 9.166, -5.0, 0.833][8.0, -11.333, 5.0, -0.666][0.0, 0.0, 0.0, 0.0][-6.0, 9.5, -4.0, 0.5][4.0, -7.0, 3.5, -0.5][-1.0, 1.833, -1.0, 0.166]B polynomials[3.0, -5.166, 2.5, -0.333][-2.0, 5.166, -2.5, 0.333][0.0, 0.0, 0.0, 0.0][0.0, 0.0, 0.0, 0.0][0.0, 0.0, 0.0, 0.0][0.0, 0.0, 0.0, 0.0]C polynomials[0.0, 0.0, 0.0, 0.0][0.0, 0.0, 0.0, 0.0][-1.0, 1.833, -1.0, 0.166][4.0, -4.333, 1.5, -0.166][-6.0, 9.5, -4.0, 0.5][4.0, -7.0, 3.5, -0.5]`
`A results at x=1010000B results at x=1010000C results at x=1000100`

# Checking the QAP

Now what’s the point of this crazy transformation? The answer is that instead of checking the constraints in the R1CS individually, we can now check all of the constraints at the same time by doing the dot product check on the polynomials.

`A . s = [43.0, -73.333, 38.5, -5.166]B . s = [-3.0, 10.333, -5.0, 0.666]C . s = [-41.0, 71.666, -24.5, 2.833]`
`t = [-88.0, 592.666, -1063.777, 805.833, -294.777, 51.5, -3.444]`
`Z = [24, -50, 35, -10, 1]`
`h = t / Z = [-3.666, 17.055, -3.444]`

--

--

## More from Vitalik Buterin

Love podcasts or audiobooks? Learn on the go with our new app.