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 \(\mathbb{F}\)) \(A = (a_1, \ldots, a_m)\) and \(S = (s_1, \ldots, s_n)\), a lookup argument is an argument that
each \(a_i\) is equal to some \(s_j\).
\(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.
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 \((s_1, \ldots, s_n)\).
Imagine we have \(N\) lookup arguments that we want to combine into one. In other words, we have \(N\) vectors \(A^{(1)}, A^{(2)}, \ldots, A^{(N)}\), and \(N\) vectors \(S^{(1)}, S^{(2)}, \ldots, 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)}, \ldots, S^{(N)}\) are all equal to some publicly known \(S\) which encodes the values of the function; the vectors \(A^{(1)}, A^{(2)}, \ldots, 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.
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:
Next, the prover sends the verifier commitments to all eight vectors above. The verifier replies with random challenges \(\beta^{(1)}, \gamma^{(1)}, \beta^{(2)}, \gamma^{(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 \[f_i(\beta^{(1)}, \gamma^{(1)}, A^{(1)}, S^{(1)}, A'^{(1)}, S'^{(1)}, W^{(1)}, Z^{(1)}) = 0\] and \[f_i(\beta^{(2)}, \gamma^{(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 \(B_1, B_2, B_3, B_4\). (These are an algebraic artifact related to the nonlinearity of the polynomials \(f_1, f_2, f_3, f_4\).)
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)} + r A^{(2)}, S^{(1)} + r S^{(2)}\) and so forth.
\(A_1 + r A^{(2)}\) | \(S^{(1)} + r S^{(2)}\) | \(A'^{(1)} + r A'^{(2)}\) | \(S'^{(1)} + r S'^{(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)} + r A^{(2)}\) but not in \(S^{(1)} + r S^{(2)}\). The vector \(A'^{(1)} + r A'^{(2)}\) is not a permutation of \(A^{(1)} + r A^{(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.
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 \(\mathcal{P}\) starts with \((A, S)\) and ends up with vectors \((A, S, A', S', Z, W)\), using 2 scalars \(\beta\) and \(\gamma\) supplied by the verifier \(\mathcal{V}\) (or Fiat-Shamir) to construct the grand products \(Z\) and \(W\). The prover \(\mathcal{P}\) then needs to prove that the following 7 equations hold:
Call these equations \(f_1 = 0\) through \(f_7 = 0\) respectively. In these equations, the \(Q^*\) vectors are constant and public. Just like in Halo2, they are used to implement zero knowledge.
This notation is very compressed. A couple of points:
In any single \(f_{i, j}\), most of the arguments would not be used. Examples:
For lookups, we define the following "relaxed versions" of the equations above:
We define a relaxed lookup instance to be a tuple \((u, \beta, \gamma, A, S, A', S', Z, W, E)\) satisfying the above 8 equations (where \(E\) is shorthand for the four vectors \(E_1, E_2, E_3, E_4, E_5\)). (In contrast to a "standard" lookup instance, which contains only the data \((\beta, \gamma, A, S, A', S', Z, W)\), a relaxed lookup instance also contains the scaling factor \(u\) and slack vectors \(E_i\).)
Whenever we have two instances \((u^{(1)}, \beta^{(1)}, \gamma^{(1)}, A^{(1)}, S^{(1)}, A'^{(1)}, S'^{(1)}, Z^{(1)}, W^{(1)}, E^{(1)})\) and \((u^{(2)}, \beta^{(2)}, \gamma^{(2)}, A^{(2)}, S^{(2)}, A'^{(2)}, S'^{(2)}, Z^{(2)}, W^{(2)}, E^{(2)})\) to fold together, we also define 5 "cross terms"
\begin{eqnarray} B_{1} & = & (1 - Q^{blind} - Q^{last}) \cdot \\ & & \big( (Z^{(1)}[-1] (A'^{(2)} + \beta^{(2)}) + Z^{(2)}[-1] (A'^{(1)} + \beta^{(1)}) \\ & & - Z^{(1)} (A^{(2)} + \beta^{(2)}) - Z^{(2)} (A^{(1)} + \beta^{(1)}) \big) \\ B_{2} & = & (1 - Q^{blind} - Q^{last}) \cdot \\ & & \big( W^{(1)}[-1] (S'^{(2)} + \beta^{(2)}) + W^{(2)}[-1] (S'^{(1)} + \beta^{(1)}) \\ & & - W^{(1)} (S^{(2)} + \beta^{(2)}) - W^{(2)} (S^{(1)} + \beta^{(1)}) \big) \\ B_{3} & = & Q^{last} \cdot (2 Z^{(1)} Z^{(2)} - u^{(1)} Z^{(2)} - u^{(2)} Z^{(1)} \\ B_{4} & = & Q^{last} \cdot (2 W^{(1)} W^{(2)} - u^{(1)} W^{(2)} - u^{(2)} W^{(1)} \\ B_5 & = & (1 - Q^{blind} - Q^{last}) \cdot \\ & & (2 A'^{(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]) \end{eqnarray}
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 \(f_j\), so each relaxed \(f_j\) is actually \(n\) constraints of the form
\[f_{j, i} (a_1, \ldots, a_n, a'_1, \ldots, a'_n, \ldots, w_1, \ldots, w_n, u, \beta, \gamma) = e_{j, i},\]
where:
For example, recall that \(f_3\) used to encode \(n\) constraints of form
\[f_{3, i} = q_i^{last} (z_i^2 - z_i) = 0;\]
the new relaxed \(f_3\) would instead encode \(n\) constraints of the form
\[f_{3, i} = q_i^{last} (z_i^2 - z_iu) = e_{3, i}.\]
It remains to understand the roles of the cross terms \(B_i\) and the slack terms \(E_i\), which is the key insight in Nova / Sangria. The main idea here is that we really want the \(f_i\) equations to satisfy constraints of the form
\[f_i(X_1 + rX_2) = f_i(X_1) + r f_i(X_2),\]
where \(X_1\) and \(X_2\) are shorthand meaning "all the arguments." Concretely, this expression is \(n\) equalities of the form
\begin{align} & f_{i, j}(a_{1,1} + ra_{2, 1}, a_{2, 1} + ra_{2,2}, \ldots, z_{n, 1} + rz_{n, 2}, u_1 + ru_2, \beta_1 + r\beta_2, \gamma_1 + r\gamma_2) \\ = & f_{i, j}(a_{1,1}, a_{2, 1}, \ldots, z_{n, 1}, u_1, \beta_1, \gamma_1) + rf_{i, j}(a_{2, 1}, a_{2,2}, \ldots, z_{n, 2}, u_2, \beta_2, \gamma_2), \end{align}
where the two arguments \(X_1\) and \(X_2\) encode \((A_1, S_1, \ldots)\) and \((A_2, S_2, \ldots)\) where, for example, \(A_1 = (a_{1, 1}, \ldots, a_{n, 1})\) and \(A_2 = (a_{1, 2}, \ldots, a_{n, 2})\).
It turns out that the last \(3\) polynomials \(f_5\) through \(f_7\) already satisfy this relationship, because they are linear. Because \(f_1\) through \(f_4\) are not linear, this relationship does not hold. However, we can introduce the cross terms \(B_i\) to get:
\[ f_i(X_1 + rX_2) = f_i(X_1) + r^2 f_i(X_2) + r B_{i}(X_1, X_2).\]
Each \(B_i\) corresponds to \(n\) equations, and take all the inputs from both \(X_1\) and \(X_2\). For example, \(B_3\) corresponds to, as \(j\) runs from \(1\) to \(n\),
\[ B_{3, j}(a_{1, 1}, \ldots, z_{n, 1}, a_{1, 2}, \ldots, z_{n, 2}, u_1, u_2, \beta_1, \beta_2, \gamma_1, \gamma_2) = q^{last}_j (2z_{j, 1}z_{j, 2} - u_1 z_{j, 2} - u_2 z_{j, 1}).\]
To see this explicitly with \(f_3 = Z^2 - uZ,\) we compute
\(\begin{align} f_{3}(X_1 + rX_2) & = (Z_1 + rZ_2)^2 - (u_1 + ru_2) (Z_1 + rZ_2) \\ & = (Z_1^2 + 2r Z_1 Z_2 + r^2Z_2^2) - u_1Z_1 - ru_1Z_2 - ru_2Z_1 - r^2 u_2 Z_2 \\ & = (Z_1^2 - u_1Z_1) + r^2(Z_2^2 - u_2Z_2) + r(2Z_1Z_2 - u_1Z_2 - u_2Z_1) \\ & = f_{3}(X_1) + r^2 f_{3}(X_2) + r B_{3}(X_1, X_2), \end{align}\)
as desired. By doing this, the \(B_i\)'s are acting as an "error term" due to \(f_i\) being nonlinear, and the \(E_i\)'s act as an accumulated error due to all the \(B_i\)'s.
We first define a "one-round protocol" as follows:
Protocol 1:
INPUT (to \(\mathcal{P}\)): 2 relaxed instance-witness pairs \(I^{(i)} = (u^{(i)}, \beta^{(i)}, \gamma^{(i)}, A^{(i)}, S^{(i)}, (A')^{(i)}, (S')^{(i)}, Z^{(i)}, W^{(i)}, E^{(i)})\), \(i \in \{1, 2\}\).
OUTPUT (of \(\mathcal{P}\)): 1 relaxed instance-witness pair \(I = (u, \beta, \gamma, A, S, A', S', Z, W, E)\).
INPUT (to \(\mathcal{V}\)): 2 relaxed committed instances \(\overline{I^{(i)}} = (u^{(i)}, \beta^{(i)}, \gamma^{(i)},\overline{A^{(i)}}, \overline{S^{(i)}}, \overline{(A')^{(i)}}, \overline{(S')^{(i)}}, \overline{Z^{(i)}}, \overline{W^{(i)}}, \overline{E^{(i)}})\), \(i \in \{1, 2\}\).
OUTPUT (of \(\mathcal{V}\)): 1 relaxed committed instance \(\overline{I} = (u, \beta, \gamma, \overline{A}, \overline{S}, \overline{A'}, \overline{S'}, \overline{Z}, \overline{W}, \overline{E})\).
Let \(T = \{Z, W, E\}\). \(\mathcal{P}\) computes all commitments \(\{\overline{X^{(1)}}, \overline{X^{(2)}}\}_{X \in T}\) and sends them (including opening randomness) to \(\mathcal{V}\).
\(\mathcal{P}\) computes the cross terms \(B_{j}\) for \(j \in \{1, \ldots, 5\}\), and commits all the \(\overline{B_{j}}\) as well.
\(\mathcal{V}\) samples a random challenge \(r \in \mathbb{F}\), and sends \(r\) to \(\mathcal{P}\).
For each \(X \in {T \cup \{u\} \backslash \{E\}}\), \(\mathcal{P}\) computes the "folded" \(X = X^{(1)} + r X^{(2)}.\) For each \(j \in \{1, \ldots, 5\}\), \(\mathcal{P}\) computes \(E_j = E_{j}^{(1)} + r^2 E_{j}^{(2)} + r B_{j},\) obtaining \(E = (E_1, E_2, E_3, E_4)\).
\(\mathcal{P}\) returns \(I = (u, \beta, \gamma, A, S, A', S', Z, W, E)\).
For each \(X \in {T \cup \{u\}}\), \(\mathcal{V}\) computes the "folded" \(\overline{X} = \overline{X}^{(1)} + r \overline{X}^{(2)}\). For each \(j \in \{1, \ldots, 5\}\), \(\mathcal{V}\) computes \(\overline{E_j} = \overline{E_{j}^{(1)}} + r^2 \overline{E_{j}^{(2)}} + r \overline{B_{j}},\) obtaining \(\overline{E} = (\overline{E_1}, \overline{E_2}, \overline{E_3}, \overline{E_4})\).
\(\mathcal{V}\) returns \(\overline{I} = (u, \beta, \gamma, \overline{A}, \overline{S}, \overline{A'}, \overline{S'}, \overline{Z}, \overline{W}, \overline{E})\).
The above encodes a single folding step. We now give the entire protocol:
Protocol 2
INPUT (to \(\mathcal{P}\)): \(N\) lookup instances \(\widetilde{I^{(i)}} = (A^{(i)}, S^{(i)})\), for \(i \in \{1, \ldots, N\}\).
OUTPUT (of \(\mathcal{P}\)): 1 relaxed lookup instance-witness pair \(I^{cml} = (u, \beta, \gamma, A, S, A', S', Z, W, E)\).
INPUT (to \(\mathcal{V}\)): \(N\) relaxed committed instances \(\overline{I^{(i)}} = (u^{(i)}, \beta^{(i)}, \gamma^{(i)},\overline{A^{(i)}}, \overline{S^{(i)}}, \overline{(A')^{(i)}}, \overline{(S')^{(i)}}, \overline{Z^{(i)}}, \overline{W^{(i)}}, \overline{E^{(i)}})\).
OUTPUT (of \(\mathcal{V}\)): 1 folded relaxed committed instance \(\overline{I^{cml}} = (u, \beta, \gamma,\overline{A}, \overline{S}, \overline{A'}, \overline{S'}, \overline{Z}, \overline{W}, \overline{E})\).
To initialize folding, \(\mathcal{P}\) initializes a "cumulative lookup instance" \(I^{cml}\) where \(u, \beta, \gamma, A, \ldots, E\) are all equal to zero.
\(\mathcal{V}\) initializes a cumulative committed instance \(\overline{I^{cml}}\) where \(u, \beta, \gamma, \overline{A}, \ldots, \overline{E}\) are all equal to zero.
When we fold in a new lookup instance \(\widetilde{I^{(i)}} = (A^{(i)}, S^{(i)})\), \(\mathcal{P}\) constructs a relaxed lookup instance \(I^{(i)} = (u^{(i)}, \beta^{(i)}, \gamma^{(i)}, A^{(i)}, S^{(i)}, \ldots, E^{(i)})\) by:
\(\mathcal{P}\) and \(\mathcal{V}\) run Protocol 1.
\(\mathcal{P}\) overwrites
\[ I^{cml} \leftarrow \texttt{Protocol_1}_{\mathcal{P}}(I^{cml}, I^{(i)}),\]
that is: apply Protocol 1, using the current \(I^{cml}\) 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".
During Protocol 1, \(\mathcal{V}\) builds the committed relaxed instance
\(\overline{I^{(i)}} = (u^{(i)}, \beta^{(i)}, \gamma^{(i)},\overline{A^{(i)}}, \overline{S^{(i)}}, \overline{(A')^{(i)}}, \overline{(S')^{(i)}}, \overline{Z^{(i)}}, \overline{W^{(i)}}, \overline{E^{(i)}})\)
out of commitments sent from \(\mathcal{P}\). Using \(\overline{I^{(i)}}\), \(\mathcal{V}\) overwrites
\[ \overline{I^{cml}} \leftarrow \texttt{Protocol_1}_{\mathcal{V}}(\overline{I^{cml}}, \overline{I^{(i)}}).\]
After \(N\) steps, the folding is complete. All that remains is for \(\mathcal{P}\) to convince \(\mathcal{V}\) that the final folded tuple \(I^{cml}\) is a legitimate witness to the committed instance \(\overline{I^{cml}}\), which is done as in the usual Halo2.
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 \(\beta\) and \(\gamma\). 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.
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 \(\beta\) and \(\gamma\) does not mess things up.