Try   HackMD

Introduction to ZK-STARKs

Remco Bloemen remco@0x.org


Disclaimer: contains math

  • If you don't understand something
    • Not your fault, this stuff is hard
    • Nobody understands it fully
  • If you don't understand anything
    • My fault, anything can be explained at some level
  • If you do understand everything
    • Collect your Turing Award & Fields Medal
    • Many open questions

Zero knowledge proofs

We know some algorithm

F(X,Y).

I give you

X and
Z
and proof that “I know an
Y
such that
F(X,Y)=Z
” without revealing
Y
.

  • X
    public input, old balances.
  • Y
    secret input, trades.
  • Z
    public output, new balances.

Scalable DEX

“I know an

Y such that
F(X,Y)=Z

  • public input
    X
    : (merkle root of) old balances.
  • secret input
    Y
    : trades.
  • public output
    Z
    : (merkle root of) new balances.

F verifies maker and taker signatures on the trades and updates the balances.


Naive solution

  • I give you
    X
    ,
    Y
    and
    Z
    .
  • You compute
    F(X,Y)
    and verify that it is
    Z
    .

Problems:

  • 📀 I need to send data size
    O(X+Y+Z)
    , i.e. all the trades.
    💾 We want
    O(X+Z+F)
    , only merkle roots.
  • ⏳ You need to do computations
    O(F)
    .
    ⌛ We want constant gas.
  • 🤫 You now know
    Y
    , the secret input.
    🤷 We don't care.

Math refresher: Polynomials


Constant
a0
Linear
a0+a1x
Parabola
a0+a1x+a2x2
Cubic
a0+a1x+a2x2+a3x3
Quartic
a0+a1x+a2x2+a3x3+a4x4
a0+a1x+a2x2++anxn

Can be uniquely described in three ways:

  • n+1
    Coefficients
  • n+1
    Points
  • n
    Zeros* and a scaling factor

(* Zeros might be imaginary.)


Can do math with them:

  • Add
    deg(P+Q)=max(degP,degQ)
    .
  • Multiply
    deg(P×Q)=degP+degQ
    .
  • Divide
    degPQ=degPdegQ
  • Division works when zeros match.

Toy example: Fibonnacci


We want to prove the 1000-th Fibonacci number starting from a public and a secret value. Take

F(X,Y)=Z to mean the following:

F0:=XFi:=Fi2+Fi1F1:=YZ:=F1000


Computational trace


Computation with

n steps and
w
registers. The trace
T
is a
n×w
table.
Here
n=1000
and
w=2
. Restate algorithm as constraints on
Ti

Example:

X=3,
Y=4
:

n
Tn,0
Tn,1
0 3 4
1 4 7
2 7 11
3 11 18
999
F999
F1000

Encode the algorithm as a set of transition constraints:

Ti+1,0=Ti,1Ti+1,1=Ti,0+Ti,1

and boundary constraints:

T0,0=XT999,1=Z


‟I know

y such that
f(x,y)=z
.”

‟I know a trace

T such that the constraints hold.”


Trace polynomials


For each register

j, create a polynomial
Pj(x)
of degree
999
such that
Pj(i)=Ti,j
for
i=0999
.

(Actual implementation uses

Pj(ωi)=Ti,j with
ω
a
n
-root of unity to allow
O(nlogn)
FFT and FRI. Also rounds
n
up to the next power of two. Ignore for now.)


Consider the constraint

Ti+1,1=Ti,0+Ti,1 for
i=0999
:

P1(i+1)=P0(i)+P1(i) for
i=0999

P1(i+1)(P0(i)+P1(i))=0 for
i=0999

Q(x)=P1(x+1)(P0(x)+P1(x)) is zero when
x
is an integer
0999
.


R(x)=(x0)(x1)(x2)(x999) is a polynomial and is zero only when
x
is an integer
0999
.

This means

C(x)=Q(x)R(x)

is also a polynomial.


Create functions that are polynomial only when the constraints are satisfied:

Transition constraints:

Ti+1,0=Ti,1C0(x)=P0(x+1)P1(x)[0998]i(xi)Ti+1,1=Ti,0+Ti,1C1(x)=P1(x+1)(P0(x)+P1(x))[0998]i(xi)

Boundary constraints:

T0,0=XC2(x)=P0(x)Xx0T999,1=ZC3(x)=P1(x)Zx999


‟I know

y such that
f(x,y)=z
.”

‟I know a trace

T such that the constraints hold.”

‟I know polynomials

P0 and
P1
such that
C0
,
C1
,
C2
,
C3
are polynomial.”


Interactive proof


I give you

X,
Z
and a merkle roots of
P0
and
P1
.

You give me random values

α0,
α1
,
α2
,
α3
.


Fast Reed-Solomon Interactive Oracle Proof II


P(x)=a0+a1x+a2x2+a3x3+anxn

Given a random number

β, we can fold the coefficients and get a polynomial of degree
n2
.

P(x)=(a0+a1β)+(a2+a3β)x++(an1+anβ)xn2

This can be computed using:

P(x)=P(x)+(β2x12)(P(x)P(x))


P(x)=a0+a1x+a2x2+a3x3+anxn

Given a random number

β, we can fold the coefficients and get a polynomial of degree
n2
.

P(x)=(a0+a1β)+(a2+a3β)x++(an1anβ)xn2


P(x)=P(x)+(β2x12)(P(x)P(x))

P(x)=a0+a1x+a2x2+a3x3++an1xn1+anxnP(x)=a0a1x+a2x2a3x3+an1xn1+anxnP(x)P(x)=2a1x+2a3x3++2an1xn1β2x(P(x)P(x))=a1β+a3βx2++an1βxn212(P(x)P(x))=a1x+a3x3++an1βxn1(β2x12)(P(x)P(x))=a1βa1x+a3βx2a3x3++an1βxn1

P(x)=(a0+a1β)+(a2+a3β)x++(an1+anβ)xn2


I compute

C(x)=α0C0(x)+α1C1(x)+α2C2(x)+α3C3(x).

I give you the merkle root of

C and claim
degC=1024
.

You give me a random value

𝛽0.


I give you the merkle root of

C and claim
degC=512
.

You give me a random value

𝛽1.


I give you the constant

C.


You verify

C using
X
,
Y
, the
α
s and the
𝛽
s.


Fiat-Shamir transform


All you do is give me random numbers. Why don't I replace you by a pseudo random number generator!

Seed PRNG with all prover messages, extract random 'verfier' messages.

Send all the proof at once.