Try   HackMD

Origami A Folding Scheme for Halo2 Lookups

Yan X Zhang and Aard Vark

(with thanks to Yi Sun, Jonathan Wang, Lev Soukhanov, Nicolas Mohnblatt)

This hasn't been checked. Use at your own risk.

Halo2 introduced a ZK lookup scheme, building on the ideas of Plookup. Given two vectors of numbers (actually, elements of some large finite field

F)
A=(a1,,am)
and
S=(s1,,sn)
, a lookup argument is an argument that

each

ai is equal to some
sj
.

A does not have to contain every element of
S
. Also,
A
can contain duplicates (as can
S
). But every element of
A
has to appear somewhere in
S
.

Nova introduced the idea of folding: a way to aggregate SNARKs (roughly, by taking random linear combinations of witness vectors) so that a large number of incrimental computations can be verified all at once. Nova described an explicit folding scheme for R1CS systems. Sangria gave a folding scheme for PLONKish arithmetizations, but also suggested (under Future Work Higher Degree Custom Gates) that the same folding technique could work for any polynomial "custom gates".

The purpose of this note is to describe an explicit folding scheme for a Halo2 lookup argument. This is a special case of a general folding scheme for polynomial custom gates.

The Setup

Generally, we use capital letters as vectors, and the matching lower case letters refer to their coordinates. For example, a vector

S would have its entries labeled
(s1,,sn)
.

Imagine we have

N lookup arguments that we want to combine into one. In other words, we have
N
vectors
A(1),A(2),,A(N)
, and
N
vectors
S(1),S(2),,S(N)
. We know that each entry of
A(1)
is equal to some entry of
S(1)
, each entry of
A(2)
is equal to some entry of
S(2)
, and so forth.

(One common use case is evaluating a function by lookup table. In this case, the vectors

S(1),S(2),,S(N) are all equal to some publicly known
S
which encodes the values of the function; the vectors
A(1),A(2),,A(N)
encode some claimed evaluations of the function at specific points.)

In any case, we have

N lookup arguments that we want to fold together. The main ideas are as follows.

  1. Halo2 tells us how to verify a single lookup, using permutations of
    A(i)
    and
    S(i)
    and a number of polynomial constraints.
    In more detail, for each
    i
    , the prover finds a permutation
    A(i)
    of
    A(i)
    and a permutation
    S(i)
    of
    S(i)
    such that, for each
    j
    , either
    • aj(i)=aj1(i)
      , or
    • aj(i)=sj(i)
      .
  2. For each
    i
    , the prover and verifier carry out a grand product protocol to prove that
    A(i)
    is a permutation of
    A(i)
    and
    S(i)
    is a permutation of
    S(i)
    .
  • First, the verifier sends the prover random challenges
    β(i)
    and
    γ(i)
    (for each
    i
    ).
  • Based on those random challenges, the prover creates two new vectors
    W(i)
    and
    Z(i)
    , the "grand product vectors".
  1. In order to verify the lookup, the verifier checks a list of polynomial identities involving the vectors
    A(i)
    ,
    A(i)
    ,
    S(i)
    ,
    S(i)
    ,
    W(i)
    , and
    Z(i)
    , and the random challenge scalars
    β(i)
    and
    γ(i)
    .
  2. To fold lookups together, we take a random linear combination of all the associated vectors
    A(i),A(i),S(i),S(i),W(i),Z(i)
    , as well as the challenges
    β(i)
    and
    γ(i)
    .
  3. To verify the folded lookup requires a bit of algebra with the polynomial constraints, since nonlinear polynomials do not play well with taking linear combinations.

An example

Suppose we want to verify that

3,7,3,5 are odd numbers, while
6,4,4,4
are even.

A(1)
S(1)
A(2)
S(2)
3 1 6 2
7 3 4 4
3 5 4 6
5 7 4 8

First of all, the prover computes permutations

A(1),S(1),A(2),S(2) such that each entry of
A(1)
is equal, either to the previous entry of
A(1)
, or to the same entry of
S(1)
. Similarly for
A(2)
and
S(2)
. Here's one way to do this:

A(1)
S(1)
A(2)
S(2)
3 3 4 4
3 1 4 8
5 5 4 2
7 7 6 6

You can check that:

  • A(1)=(3,3,5,7)
    is a permutation of
    A(1)=(3,7,3,5)
    , and similarly for the other three columns.
  • Each entry of
    A(1)
    is either the same as the entry of
    S(1)
    next to it, or a repeat of the previous entry of
    A(1)
    . The first entry
    3
    , the
    5
    and the
    7
    are all the same as the adjacent entries of
    S(1)
    , while the second
    3
    is a repeat of the first.
  • The same holds for
    A(2)
    .

Next, the prover sends the verifier commitments to all eight vectors above. The verifier replies with random challenges

β(1),γ(1),β(2),γ(2).

The prover uses the random challenges to compute grand products

Z(1),W(1),Z(2),W(2). Since the algebra is messy, we won't show an example here. What's important to know is just that the lookup problem can be translated into checking some polynomial conditions
fi(β(1),γ(1),A(1),S(1),A(1),S(1),W(1),Z(1))=0
and
fi(β(2),γ(2),A(2),S(2),A(2),S(2),W(2),Z(2))=0.

So far we have two lookup arguments: one for the odd numbers, and one for the evens. Now let's fold them together. First, the prover will send the verifier commitments to some cross-terms

B1,B2,B3,B4. (These are an algebraic artifact related to the nonlinearity of the polynomials
f1,f2,f3,f4
.)

Then the verifier sends the prover a random challenge

r. (In our example, let's imagine
r=100
.)

Finally, the prover computes the linear combinations

A(1)+rA(2),S(1)+rS(2) and so forth.

A1+rA(2)
S(1)+rS(2)
A(1)+rA(2)
S(1)+rS(2)
603 201 403 403
407 403 403 801
403 605 405 205
405 807 607 607

Something strange happened here: The "lookup" and "permutation" relations are no longer satisfied. The number

603 appears in
A(1)+rA(2)
but not in
S(1)+rS(2)
. The vector
A(1)+rA(2)
is not a permutation of
A(1)+rA(2)
. It looks like we've thrown out all the nice properties that made the lookup argument work.

But in fact polynomials save the day! The "folded" vectors will still satisfy a polynomial identity that we'll be able to write down, and that's what the prover will verify.

The Procedure

Relaxed AIRs

To start, we introduce the main object, a relaxed AIR, in the style of Sangria. The main idea of a relaxed AIR, like the relaxed R1CS analogue from Nova, is a polynomial constraint that involves a "slack term." While we can do this for general custom gates following the outline of Sangria, for this presentation we will specialize to lookups.

In the Halo2 setup, the prover

P starts with
(A,S)
and ends up with vectors
(A,S,A,S,Z,W)
, using 2 scalars
β
and
γ
supplied by the verifier
V
(or Fiat-Shamir) to construct the grand products
Z
and
W
. The prover
P
then needs to prove that the following 7 equations hold:

  1. (1QblindQlast)(Z[1](A+β)Z(A+β))=0
  2. (1QblindQlast)(W[1](S+γ)W(S+γ))=0
  3. Qlast(Z2Z)=0
  4. Qlast(W2W)=0
  5. (1QblindQlast)(AS)(AA[1])=0
  6. Q0(AS)=0
  7. Q0(Z1)=0
  8. Q0(W1)=0
    .

Call these equations

f1=0 through
f7=0
respectively. In these equations, the
Q
vectors are constant and public. Just like in Halo2, they are used to implement zero knowledge.

  • Let
    t=2
    . (In general,
    t
    is the maximum number of distinct "shifts" that occur in any of the polynomial constraints. Since we have
    Z
    and
    Z[1]
    in the same constraint meaning the polynomial
    fi,j
    involves both
    Zj
    and
    Zj1
    we take
    t=2
    for this protocol.)
  • To implement zero knowledge, only the first
    nt
    rows of the vectors
    A,S,
    will participate in the lookup. The last
    t
    rows will be chosen by the prover
    P
    at random.
  • Q0
    :
    q00=1
    and
    qi0=1
    for all
    i0
    . (In the Halo2 writeup, this
    Q0
    is denoted
    0
    and called a Lagrange basis polynomial which of course is what
    Q0
    becomes when you encode a column as a polynomial.)
  • Qblind
    :
    qiblind=0
    for
    ntin1
    , and
    1
    otherwise (i.e.
    Qblind
    selects the last
    t
    rows).
  • Qlast
    :
    qilast=1
    for
    i=nt1
    , and
    0
    otherwise (i.e.
    Qlast
    selects the
    (nt1)
    -st row, the last row before the "blind" rows).

This notation is very compressed. A couple of points:

  1. Each polynomial equation
    fi=0
    corresponds to
    n
    polynomial equations of the same form
    fi,j(a1,,an,a1,,an,,w1,,wn,β,γ)=0,

    where each capital letter
    X{A,A,S,S,Z,W}
    corresponds to substituting
    xj
    for
    fi,j
    ,
    xj
    being the
    j
    -th coordinate for the vector
    X
    . The coordinates of the
    Q
    vectors, the
    qi
    's, do not appear as arguments since they are constants.
  2. We use
    Z[±1]
    to mean that when we unpack equation
    fi,j
    , instead of substituting
    zj
    , we substitute
    zj±1
    .
  3. For each vector
    X{A,A,S,S,Z,W}
    , each
    fi,j
    will only depend on entries
    Xj
    , and possibly
    Xj1
    .

In any single

fi,j, most of the arguments would not be used. Examples:

  1. Consider the third equation
    f3=Z2Z=0.
    This is shorthand for
    n
    constraints of the form
    f3,i()=zi2zi=0
    for each
    i
    , where
    zi
    is the
    i
    -th coordinate of
    Z
    .
  2. In the first equation,
    f1
    would correspond to
    n
    constraints
    f1,j
    , where
    fi,j()=(1QjblindQjlast)(zj1(aj+β)zj(aj+β))=0.

For lookups, we define the following "relaxed versions" of the equations above:

  1. (1QblindQlast)(Z([1])(A+β)Z(A+β))=E1
  2. (1QblindQlast)(W([1])(S+γ)W(S+γ))=E2
  3. Qlast(Z2uZ)=E3
  4. Qlast(W2uW)=E4
    ,
  5. (1QblindQlast)(AS)(AA[1])=E5
    ,
  6. Q0(AS)=0
  7. Q0(Zu)=0
  8. Q0(Wu)=0
    .

We define a relaxed lookup instance to be a tuple

(u,β,γ,A,S,A,S,Z,W,E) satisfying the above 8 equations (where
E
is shorthand for the four vectors
E1,E2,E3,E4,E5
). (In contrast to a "standard" lookup instance, which contains only the data
(β,γ,A,S,A,S,Z,W)
, a relaxed lookup instance also contains the scaling factor
u
and slack vectors
Ei
.)

Whenever we have two instances

(u(1),β(1),γ(1),A(1),S(1),A(1),S(1),Z(1),W(1),E(1)) and
(u(2),β(2),γ(2),A(2),S(2),A(2),S(2),Z(2),W(2),E(2))
to fold together, we also define 5 "cross terms"

B1=(1QblindQlast)((Z(1)[1](A(2)+β(2))+Z(2)[1](A(1)+β(1))Z(1)(A(2)+β(2))Z(2)(A(1)+β(1)))B2=(1QblindQlast)(W(1)[1](S(2)+β(2))+W(2)[1](S(1)+β(1))W(1)(S(2)+β(2))W(2)(S(1)+β(1)))B3=Qlast(2Z(1)Z(2)u(1)Z(2)u(2)Z(1)B4=Qlast(2W(1)W(2)u(1)W(2)u(2)W(1)B5=(1QblindQlast)(2A(1)A(2)S(1)A(2)S(2)A(1)A(1)A(2)[1]A(2)A(1)[1]+S(1)A(2)[1]+S(2)A(1)[1])

corresponding to the first

5 relaxed equations. (This is because those equations are quadratic and the remainder are linear; we explain this a bit more below)

We now explain the new notation. First,

u is a "homogenizing" scalar
u
considered as an additional input to each
fj
, so each relaxed
fj
is actually
n
constraints of the form
fj,i(a1,,an,a1,,an,,w1,,wn,u,β,γ)=ej,i,

where:

  1. each
    Ej
    is a vector
    (ej,1,,ej,n)
    ;
  2. for
    j6
    , we have
    Ej=(0,,0)
    .

For example, recall that

f3 used to encode
n
constraints of form
f3,i=qilast(zi2zi)=0;

the new relaxed
f3
would instead encode
n
constraints of the form
f3,i=qilast(zi2ziu)=e3,i.

It remains to understand the roles of the cross terms

Bi and the slack terms
Ei
, which is the key insight in Nova / Sangria. The main idea here is that we really want the
fi
equations to satisfy constraints of the form

fi(X1+rX2)=fi(X1)+rfi(X2),
where
X1
and
X2
are shorthand meaning "all the arguments." Concretely, this expression is
n
equalities of the form

fi,j(a1,1+ra2,1,a2,1+ra2,2,,zn,1+rzn,2,u1+ru2,β1+rβ2,γ1+rγ2)=fi,j(a1,1,a2,1,,zn,1,u1,β1,γ1)+rfi,j(a2,1,a2,2,,zn,2,u2,β2,γ2),

where the two arguments

X1 and
X2
encode
(A1,S1,)
and
(A2,S2,)
where, for example,
A1=(a1,1,,an,1)
and
A2=(a1,2,,an,2)
.

It turns out that the last

3 polynomials
f5
through
f7
already satisfy this relationship, because they are linear. Because
f1
through
f4
are not linear, this relationship does not hold. However, we can introduce the cross terms
Bi
to get:

fi(X1+rX2)=fi(X1)+r2fi(X2)+rBi(X1,X2).

Each

Bi corresponds to
n
equations, and take all the inputs from both
X1
and
X2
. For example,
B3
corresponds to, as
j
runs from
1
to
n
,
B3,j(a1,1,,zn,1,a1,2,,zn,2,u1,u2,β1,β2,γ1,γ2)=qjlast(2zj,1zj,2u1zj,2u2zj,1).

To see this explicitly with

f3=Z2uZ, we compute

f3(X1+rX2)=(Z1+rZ2)2(u1+ru2)(Z1+rZ2)=(Z12+2rZ1Z2+r2Z22)u1Z1ru1Z2ru2Z1r2u2Z2=(Z12u1Z1)+r2(Z22u2Z2)+r(2Z1Z2u1Z2u2Z1)=f3(X1)+r2f3(X2)+rB3(X1,X2),

as desired. By doing this, the

Bi's are acting as an "error term" due to
fi
being nonlinear, and the
Ei
's act as an accumulated error due to all the
Bi
's.

Details of the Protocol (Halo2 lookups)

We first define a "one-round protocol" as follows:

Protocol 1:

INPUT (to

P): 2 relaxed instance-witness pairs
I(i)=(u(i),β(i),γ(i),A(i),S(i),(A)(i),(S)(i),Z(i),W(i),E(i))
,
i{1,2}
.
OUTPUT (of
P
): 1 relaxed instance-witness pair
I=(u,β,γ,A,S,A,S,Z,W,E)
.
INPUT (to
V
): 2 relaxed committed instances
I(i)=(u(i),β(i),γ(i),A(i),S(i),(A)(i),(S)(i),Z(i),W(i),E(i))
,
i{1,2}
.
OUTPUT (of
V
): 1 relaxed committed instance
I=(u,β,γ,A,S,A,S,Z,W,E)
.

  1. Let

    T={Z,W,E}.
    P
    computes all commitments
    {X(1),X(2)}XT
    and sends them (including opening randomness) to
    V
    .

    • In detail,
      P
      includes values
      {ρX(1)ρX(2)}XT
      , where
      ρX(i)
      is the commitment randomness for
      X(i)
      ; that is, for all
      XT
      and
      i{1,2}
      ,
      X(i)=Com(pp,X(i),ρX(i))
      .
    • Important: we assume commitments for
      T=A,S,A,S
      have already been given to
      V
      (this will be evident once we get to Protocol 2)
  2. P computes the cross terms
    Bj
    for
    j{1,,5}
    , and commits all the
    Bj
    as well.

  3. V samples a random challenge
    rF
    , and sends
    r
    to
    P
    .

  4. For each

    XT{u}{E},
    P
    computes the "folded"
    X=X(1)+rX(2).
    For each
    j{1,,5}
    ,
    P
    computes
    Ej=Ej(1)+r2Ej(2)+rBj,
    obtaining
    E=(E1,E2,E3,E4)
    .

    • For example,
      β=β(1)+rβ(2)
      and
      Z=Z(1)+rZ(2)
      .
    • Recall that
      E(1)=(E1(1),,E4(i))
      and
      E2=(E1(2),,E4(2))
      . Each of the
      Ej(i)
      's are themselves vectors.
  5. P returns
    I=(u,β,γ,A,S,A,S,Z,W,E)
    .

  6. For each

    XT{u},
    V
    computes the "folded"
    X=X(1)+rX(2)
    . For each
    j{1,,5}
    ,
    V
    computes
    Ej=Ej(1)+r2Ej(2)+rBj,
    obtaining
    E=(E1,E2,E3,E4)
    .

  7. V returns
    I=(u,β,γ,A,S,A,S,Z,W,E)
    .

The above encodes a single folding step. We now give the entire protocol:

Protocol 2

INPUT (to

P):
N
lookup instances
I(i)~=(A(i),S(i))
, for
i{1,,N}
.
OUTPUT (of
P
): 1 relaxed lookup instance-witness pair
Icml=(u,β,γ,A,S,A,S,Z,W,E)
.
INPUT (to
V
):
N
relaxed committed instances
I(i)=(u(i),β(i),γ(i),A(i),S(i),(A)(i),(S)(i),Z(i),W(i),E(i))
.
OUTPUT (of
V
): 1 folded relaxed committed instance
Icml=(u,β,γ,A,S,A,S,Z,W,E)
.

  1. To initialize folding,

    P initializes a "cumulative lookup instance"
    Icml
    where
    u,β,γ,A,,E
    are all equal to zero.

  2. V initializes a cumulative committed instance
    Icml
    where
    u,β,γ,A,,E
    are all equal to zero.

  3. When we fold in a new lookup instance

    I(i)~=(A(i),S(i)),
    P
    constructs a relaxed lookup instance
    I(i)=(u(i),β(i),γ(i),A(i),S(i),,E(i))
    by:

    • constructing permutations
      (A)(i)
      and
      (S)(i)
      as in Halo2,
    • sending commitments (including opening randomness) of
      A(i),A(i),S(i),S(i)
      to
      V
      ,
    • requesting random challenges
      β(i)
      and
      γ(i)
      from
      V
      (or Fiat-Shamir) for that round,
    • constructing
      Z(i)
      and
      W(i)
      from
      ((A)(i),(S)(i),β(i),γ(i))
      as in Halo2,
    • setting
      u(i)=1
      and
      Ej(i)=(0,,0)
      for all
      j
      ,
  4. P and
    V
    run Protocol 1.

  5. P overwrites
    IcmlProtocol_1P(Icml,I(i)),

    that is: apply Protocol 1, using the current
    Icml
    as the first input and the new relaxed instance
    I(i)
    as the second input. During each step, we need a new
    r=r(i)
    for our "folding randomness".

  6. During Protocol 1,

    V builds the committed relaxed instance
    I(i)=(u(i),β(i),γ(i),A(i),S(i),(A)(i),(S)(i),Z(i),W(i),E(i))

    out of commitments sent from
    P
    . Using
    I(i)
    ,
    V
    overwrites
    IcmlProtocol_1V(Icml,I(i)).

  7. After

    N steps, the folding is complete. All that remains is for
    P
    to convince
    V
    that the final folded tuple
    Icml
    is a legitimate witness to the committed instance
    Icml
    , which is done as in the usual Halo2.

Comparison with Sangria

Scope

Sangria had outlined an approach to custom gates. Our lookup protocol is almost, but not quite, a special case of that procedure, mainly because of the roles of

β and
γ
. Instead, what we have here is a special case of "custom gates with verifier randomness," a concept that's a slight generalization of Sangria's approach to custom gates.

For completeness, we make explicit many of the ideas in Sangria's outline in our writeup of the general folding scheme.

Knowledge soundness

This protocol satisfies knowledge soundness: a cheating prover cannot convince the verifier to accept a folded proof unless the prover actually knows

N satisfying witnesses. We give a proof of knowledge soundness in our writeup.

In outline, the proof (very similar to as done in Sangria and Nova) is as follows. The idea is to imagine an extractor that interacts with the prover. The extractor is allowed to rewind the prover to a previous state. In practice, this means the extractor (playing the role of verifier) can send the prover different challenges

r, and see how the prover responds. Like in Nova and Sangria, by testing enough different values
r
and doing a bit of algebra, the extractor can recover the witnesses
N
that were folded together. Once the extractor can recover the
N
folded witnesses, since Halo2 lookups themselves are knowledge sound, we know the prover must know
N
valid lookup witnesses.

Our proof of knowledge soundness in the general scheme is only a bit more complicated than that of Sangria, partly due to the polynomials of arbitrary degree (although again Sangria had an outline already in Section 3.3) and partly due to the fact that we have "verifier randomness," as stated above. We have to take care that the verifier-provided randomness

β and
γ
does not mess things up.