Knowledge soundness of Groth16

Together with my colleagues Ryan Cao and Kunming Jiang, we re-derived the knowledge soundness of Groth16 as an entertaining exercise. Groth16 is a pairing-based ZK-SNARK. See here for an excellent tutorial on Groth16, or check out the paper itself.

R1CS & QAPs

A witness is $$ \mathbf a = (a_0,\ldots,a_\ell,a_{\ell+1},\ldots,a_m) \in \mathbb{F}^{m+1}, $$ where $a_0,\ldots,a_\ell$ are public and $a_{\ell+1},\ldots,a_m$ are private; by convention, $a_0=1$.

Constraints are expressed via $(m+1)\times n$ matrices $U,V,W$. The constraints are satisfied by $\mathbf a$ if $$\begin{equation}\label{1}\tag{1} (\mathbf a U)\circ(\mathbf a V)=\mathbf a W, \end{equation}$$ where $\circ$ is the Hadamard product (so this is an equality of vectors).

Assume that $|\mathbb{F}|\ge n$. Choose $n$ distinct field elements $\omega_j\in\mathbb{F}$, $j=1,\ldots,n$, and interpolate over the constraints to obtain polynomials $$ u_i,v_i,w_i\in\mathbb{F}[x]^{<n}, \qquad i=0,\ldots,m, $$ such that $$ u_i(\omega_j)=U_{ij},\qquad v_i(\omega_j)=V_{ij},\qquad w_i(\omega_j)=W_{ij} $$ for every $i$ and $j$. Thus, if $$ f_{\mathbf a}(x) := \left(\sum_{i=0}^{m}a_i u_i(x)\right) \left(\sum_{i=0}^{m}a_i v_i(x)\right) -\sum_{i=0}^{m}a_i w_i(x), $$ then $f_{\mathbf a}(x)\in\mathbb{F}[x]^{< 2n-1}$ for any $\mathbf a$ and $f_{\mathbf a}(\omega_j)=0$ for every $j$ if and only if \eqref{1} holds. Equivalently, $\mathbf a$ satisfies the R1CS constraints if and only if the polynomial $$ t(x):=\prod_{j=1}^{n}(x-\omega_j) $$ divides $f_{\mathbf a}$. This is the equivalence of R1CS and QAP.

Bilinear groups

The groups $G_1,G_2,G_T$ are cyclic groups of prime order $p$, written additively. The elements $g_1,g_2$ are generators of $G_1,G_2$, respectively, and $$ e:G_1\times G_2\longrightarrow G_T $$ is a bilinear map such that $e(g_1,g_2)$ is a generator of $G_T$. In particular, $\mathbb{F}=\mathbb{F}_p$ is the scalar field of the groups. Write $$ [a]_1:=a\,g_1, \qquad [a]_2:=a\,g_2, \qquad a\in\mathbb{F}. $$

Trusted setup

Choose random “trapdoor values” $$ \alpha,\beta,\gamma,\delta,\tau \xleftarrow{\$}\mathbb{F}, $$ and publish the following CRS:

[α]1, [β]1, [β]2, [γ]2, [δ]1, [δ]2,[τj]1, [τj]2,0j<n,[Φj]1:=[δ1τjt(τ)]1,0j<n,[Ψi]1:=[γ1(αvi(τ)+βui(τ)+wi(τ))]1,0i,[Ψi]1:=[δ1(αvi(τ)+βui(τ)+wi(τ))]1,<im.\begin{align*} & [\alpha]_1,\ [\beta]_1,\ [\beta]_2 ,\ [\gamma]_2, \ [\delta]_1,\ [\delta]_2, \\ & [\tau^j]_1,\ [\tau^j]_2, && 0\le j<n, \\ & [\Phi_j]_1 :=[\delta^{-1}\tau^j t(\tau)]_1, && 0\le j<n, \\ & [\Psi_i]_1 :=\left[\gamma^{-1} \bigl(\alpha v_i(\tau)+\beta u_i(\tau)+w_i(\tau)\bigr) \right]_1, && 0\le i\le\ell, \\ & [\Psi_i]_1 :=\left[\delta^{-1} \bigl(\alpha v_i(\tau)+\beta u_i(\tau)+w_i(\tau)\bigr) \right]_1, && \ell<i\le m. \end{align*}

In brief, $\alpha,\beta$ force the prover to use the constraints; $\gamma,\delta$ prevent the prover from using the $[\Psi_i]_1$ with $0\le i\le\ell$ (corresponding to the public inputs) in a proof; and the $[\Phi_j]_1$ are used to construct $[\delta^{-1}f_{\mathbf a}(\tau)]_1$.

Proof format

A proof is $ ([A]_1,[B]_2,[C]_1)\in G_1\times G_2\times G_1.$

Verifier checks

Given a proof $ ([A]_1,[B]_2,[C]_1)\in G_1\times G_2\times G_1$ and public witness values $a_0, \dots, a_\ell$ the verifier checks $$ e([A]_1,[B]_2) =e([\alpha]_1,[\beta]_2) +e([C]_1,[\delta]_2) +e\left(\sum_{i=0}^{\ell}a_i[\Psi_i]_1,[\gamma]_2\right),$$ accepting the proof if equality holds.

Honest prover

Sample $r,s\xleftarrow{\$}\mathbb{F}$ for zero knowledge, and set

[A]1:=[α]1+i=0mai[ui(τ)]1+r[δ]1,[B]2:=[β]2+i=0mai[vi(τ)]2+s[δ]2,[C]1:=<imai[Ψi]1+[δ1f𝐚(τ)]1+s([α]1+i=0mai[ui(τ)]1+r[δ]1)+r([β]1+i=0mai[vi(τ)]1+s[δ]1)rs[δ]1.\begin{align*} [A]_1 &:=[\alpha]_1 +\sum_{i=0}^{m}a_i[u_i(\tau)]_1 +r[\delta]_1, \\ [B]_2 &:=[\beta]_2 +\sum_{i=0}^{m}a_i[v_i(\tau)]_2 +s[\delta]_2, \\ [C]_1 &:=\sum_{\ell<i\le m}a_i[\Psi_i]_1 +[\delta^{-1}f_{\mathbf a}(\tau)]_1 \\ &\quad +s\left( [\alpha]_1+\sum_{i=0}^{m}a_i[u_i(\tau)]_1+r[\delta]_1 \right) \\ &\quad +r\left( [\beta]_1+\sum_{i=0}^{m}a_i[v_i(\tau)]_1+s[\delta]_1 \right) -rs[\delta]_1. \end{align*}

Proof malleability

If $([A]_1,[B]_2,[C]_1)$ satisfies the verifier check, then so does $$ (\lambda[A]_1,\lambda^{-1}[B]_2,[C]_1) $$ for every $\lambda\in\mathbb{F}^{\times}$.

Knowledge soundness assumptions

It is assumed that the prover/adversary uses only “generic” operations, i.e., the group operations, the pairing operation, and equality checks. This is the generic group model (“GGM”) extended to pairings. The GGM basically says that the particular way in which group elements are symbolically represented may not be used.

In particular, any $G_1$ or $G_2$ element used by the prover/adversary is an affine combination of $G_1$ or $G_2$ elements from the CRS, respectively. Elements of $G_T$ may be quadratic because of the pairing. Call this assumption $(\ast_1)$.

It is furthermore assumed that any equation involving elements of the CRS either always holds, irrespective of the particular CRS sampled (so the equation holds “by design”), or instead holds with only negligible probability. Thus the CRS is “disclosure free” in the language of the paper. Call this assumption $(\ast_2)$.

We will show statistical knowledge soundness in the generic bilinear group model: any prover strategy involving only generic operations contains sufficient information to recover the witness values.

By $(\ast_1)$, for any proof $([A]_1,[B]_2,[C]_1)$,

[A]1=Aα[α]1+Aβ[β]1+Aδ[δ]1+j=0n1Aτ,j[τj]1+j=0n1AΦj[Φj]1+i=0AΨi[Ψi]1+<imAΨi[Ψi]1\begin{align*} [A]_1 ={}& A_\alpha[\alpha]_1 +A_\beta[\beta]_1 +A_\delta[\delta]_1 +\sum_{j=0}^{n-1}A_{\tau,j}[\tau^j]_1 \\ &+\sum_{j=0}^{n-1}A_{\Phi_j}[\Phi_j]_1 +\sum_{i=0}^{\ell}A_{\Psi_i}[\Psi_i]_1 +\sum_{\ell<i\le m}A_{\Psi_i}[\Psi_i]_1 \end{align*}

for some coefficients $$ A_\alpha,A_\beta,A_\delta, A_{\Psi_i},A_{\Phi_j},A_{\tau,j}\in\mathbb{F}. $$

Moreover, by $(\ast_2)$, these coefficients are unique with overwhelming probability. For the soundness proof, however, $(\ast_2)$ is not needed at this point, since the extractor is assumed to have access to the particular coefficients used by the prover.

Similarly, $$ [C]_1=C_\alpha[\alpha]_1+\cdots \qquad\text{(with the same CRS basis as for $[A]_1$),} $$ while $$ [B]_2 =B_\beta[\beta]_2+B_\gamma[\gamma]_2+B_\delta[\delta]_2 +\sum_{j=0}^{n-1}B_{\tau^j}[\tau^j]_2. $$

Recovering the witness

First steps

Suppose that the verifier check passes. Both sides of the verifier-check equation are $e(g_1,g_2)$ multiplied by a Laurent polynomial in $\alpha,\beta,\gamma,\delta,\tau$. We proceed by comparing coefficients, writing, for example, $\mathrm{LHS}_{\alpha\beta}\in\mathbb{F}$ for the coefficient of $\alpha\beta$ on the left-hand side. For $\alpha\beta$, $$ \mathrm{LHS}_{\alpha\beta}=A_\alpha B_\beta, \qquad \mathrm{RHS}_{\alpha\beta}=1. $$ Thus $A_\alpha=B_\beta^{-1}$. By proof malleability, we may assume without loss of generality that $$ A_\alpha=B_\beta=1. $$ For $\beta^2$, $$ \mathrm{LHS}_{\beta^2}=A_\beta B_\beta, \qquad \mathrm{RHS}_{\beta^2}=0, $$ so $$ A_\beta=0. $$ For $\alpha\delta$, $$ \mathrm{LHS}_{\alpha\delta} =A_\alpha B_\delta=B_\delta, \qquad \mathrm{RHS}_{\alpha\delta}=C_\alpha, $$ so $$ C_\alpha=B_\delta. $$ For $\beta\delta$, $$ \mathrm{LHS}_{\beta\delta} =A_\beta B_\delta+A_\delta B_\beta=A_\delta, \qquad \mathrm{RHS}_{\beta\delta}=C_\beta, $$ so $$ C_\beta=A_\delta. $$ At this point we do not expect (or need, for knowledge soundness) to determine $A_\delta$ and $B_\delta$, since these are freely chosen by an honest prover as $r$ and $s$, respectively. For $\alpha\gamma$, $$ \mathrm{LHS}_{\alpha\gamma}=A_\alpha B_\gamma, \qquad \mathrm{RHS}_{\alpha\gamma}=0, $$ so $$ B_\gamma=0. $$ For $\delta\tau^j$, $$ \mathrm{LHS}_{\delta\tau^j} =A_\delta B_{\tau^j}+B_\delta A_{\tau^j}, \qquad \mathrm{RHS}_{\delta\tau^j}=C_{\tau^j}, $$ so $$ C_{\tau^j}=A_\delta B_{\tau^j}+B_\delta A_{\tau^j}. $$ For $\delta^2$, $$ \mathrm{LHS}_{\delta^2}=A_\delta B_\delta=C_\alpha C_\beta, \qquad \mathrm{RHS}_{\delta^2}=C_\delta, $$ so $$ C_\delta=C_\alpha C_\beta. $$

Deducing knowledge soundness

The coefficient of $\delta^{-1}$

Write $$ A=A_{\delta^1}\delta^1 +A_{\delta^0}\delta^0 +A_{\delta^{-1}}\delta^{-1} $$ for Laurent polynomials $A_{\delta^1},A_{\delta^0},A_{\delta^{-1}}$. Notice that all of the $A_{\Phi_j}$ and all $A_{\Psi_i}$ with $\ell < i \le m$ occur in $A_{\delta^{-1}}$. Similarly, $$ B=B_{\delta^1}\delta^1+B_{\delta^0}\delta^0, $$ where $B_{\delta^0}=\beta+\cdots$. The remaining summands cannot eliminate $\beta$, so $B_{\delta^0}\ne0$. Comparing now the $\delta^{-1}$ coefficients of the two sides of the verifier check as Laurent polynomials in the remaining variables, we have $$ \mathrm{LHS}_{\delta^{-1}} =B_{\delta^0}A_{\delta^{-1}}, \qquad \mathrm{RHS}_{\delta^{-1}}=0, $$ and hence $A_{\delta^{-1}}=0$. Thus $$ \sum_{j=0}^{n-1}A_{\Phi_j}[\Phi_j]_1 +\sum_{\ell < i\le m}A_{\Psi_i}[\Psi_i]_1 =0. $$ This is the $\delta^{-1}A_{\delta^{-1}}$ part of $[A]_1$. We did not need to show that the individual coefficients $A_{\Phi_j}$ and $A_{\Psi_i}$ are zero, although this could now be concluded using disclosure freeness.

The coefficient of $\gamma^{-1}\delta^0$

Summarizing what we know thus far, \begin{align*} A &=\alpha+A_\delta\delta +\sum_{i=0}^{\ell}A_{\Psi_i}\gamma^{-1} k_i(\alpha,\beta,\tau) +\sum_{j=0}^{n-1}A_{\tau^j}\tau^j, \\ B &=\beta+B_\delta\delta +\sum_{j=0}^{n-1}B_{\tau^j}\tau^j, \end{align*} where $A_\delta,B_\delta,B_{\tau^j}\in\mathbb{F}$ and $$ k_i(\alpha,\beta,\tau) :=\alpha v_i(\tau)+\beta u_i(\tau)+w_i(\tau).$$ Consider the $\mathbb{F}[\alpha,\beta,\tau]$-valued coefficients of $\gamma^{-1}\delta^0$. We have $$ \mathrm{LHS}_{\gamma^{-1}\delta^0} =\left(\beta+\sum_{j=0}^{n-1}B_{\tau^j}\tau^j\right) \left(\sum_{i=0}^{\ell}A_{\Psi_i} k_i(\alpha,\beta,\tau)\right). $$ The left factor is nonzero, while inspection of the verifier check gives $$ \mathrm{RHS}_{\gamma^{-1}\delta^0}=0. $$ Therefore $$ \sum_{i=0}^{\ell}A_{\Psi_i}k_i(\alpha,\beta,\tau)=0, $$ and hence $$ \sum_{i=0}^{\ell}A_{\Psi_i}[\Psi_i]_1=0. $$

Recovering the witness values

Consider $$ \mathrm{LHS}_{\alpha^1\delta^0\beta^0}, \mathrm{RHS}_{\alpha^1\delta^0\beta^0} \in\mathbb{F}[\gamma^{\pm1},\tau]. $$ We have \begin{align*} \mathrm{LHS}_{\alpha^1\delta^0\beta^0} &=\sum_{j=0}^{n-1}B_{\tau^j}\tau^j, \\ \mathrm{RHS}_{\alpha^1\delta^0\beta^0} &=\sum_{\ell < i\le m}C_{\Psi_i}v_i(\tau) +\sum_{i=0}^{\ell}a_i v_i(\tau). \end{align*} Relabel the coefficients $C_{\Psi_i}$ as $a_i$ for $\ell < i \le m$, (these correspond to the private inputs). We have shown $$ \sum_{j=0}^{n-1}B_{\tau^j}\tau^j =\sum_{i=0}^{m}a_i v_i(\tau), $$ and therefore $$ \sum_{j=0}^{n-1}B_{\tau^j}[\tau^j]_2 =\sum_{i=0}^{m}a_i[v_i(\tau)]_2. $$ Considering instead $$ \mathrm{LHS}_{\alpha^0\delta^0\beta^1}, \mathrm{RHS}_{\alpha^0\delta^0\beta^1} \in\mathbb{F}[\gamma^{\pm1},\tau], $$ it follows in the same way that $$ \sum_{j=0}^{n-1}A_{\tau^j}[\tau^j]_1 =\sum_{i=0}^{m}a_i[u_i(\tau)]_1. $$

The remaining coefficients & QAP satisfaction

Consider now $$ \mathrm{LHS}_{\alpha^0\beta^0\gamma^0\delta^0}, \mathrm{RHS}_{\alpha^0\beta^0\gamma^0\delta^0} \in\mathbb{F}[\tau]. $$ By the above, \begin{align*} A&=\alpha+A_\delta\delta+\sum_{i=0}^{m}a_i u_i(\tau), \\ B&=\beta+B_\delta\delta+\sum_{i=0}^{m}a_i v_i(\tau). \end{align*} Hence $$ \mathrm{LHS}_{\alpha^0\beta^0\gamma^0\delta^0} =\left(\sum_{i=0}^{m}a_i u_i(\tau)\right) \left(\sum_{i=0}^{m}a_i v_i(\tau)\right), $$ while \begin{align*} \mathrm{RHS}_{\alpha^0\beta^0\gamma^0\delta^0} ={}&\sum_{j=0}^{n-1}C_{\Phi_j}\tau^j t(\tau) +\sum_{\ell < i \le m}C_{\Psi_i}w_i(\tau) +\sum_{i=0}^{\ell}a_i w_i(\tau). \end{align*} Thus, after the relabeling $C_{\Psi_i}=a_i$ for $\ell< i\le m$, $$ \left(\sum_{i=0}^{m}a_i u_i(\tau)\right) \left(\sum_{i=0}^{m}a_i v_i(\tau)\right) -\sum_{i=0}^{m}a_i w_i(\tau) =\left(\sum_{j=0}^{n-1}C_{\Phi_j}\tau^j\right)t(\tau). $$ By disclosure freeness, $f_{\mathbf a} (X)$ thus divides $t(X)$ and the QAP is therefore satisfied. In particular, the prover knows the witness $$ \mathbf a =\bigl(a_0,\ldots,a_\ell, C_{\Psi_{\ell+1}},\ldots,C_{\Psi_m}\bigr), $$ whose first $\ell+1$ entries are public. This establishes knowledge soundness.

When is a polynomial determined by evaluations? Polynomial interpolation over commutative rings with unity.

A polynomial with coefficients in a field and of degree $< n$ is determined by its evaluations at any $n$ distinct points. A common way to see this is via Lagrange interpolation. But what happens in the more general case where the coefficients come from a commutative ring $R$ with $1$? It’s easy to see that the statement fails. Consider e.g. $R = \mathbb{Z}/ 8\mathbb{Z}$, and let $f(X) = 4X + 4X^2$. Then $f$ vanishes everywhere on R (easy to check), despite having degree two. In particular, there are multiple polynomials of degree $< 3$ (viz. $f$ and the zero polynomial) that vanish at three distinct points e.g. $1, 2, 3$.

The circumstances under which a polynomial over a commutative ring is determined by its evaluations can be determined by considering the Vandermonde matrix. Recall that, given points $c_1, \dots, c_n \in R$, the Vandermonde matrix $V$ is the $n$ x $n$ matrix consisting of powers of the $c_i$. The matrix-vector product of $V$ and the vector of the coefficients of a polynomial $f$ then gives the vector of evaluations $f(c_i)$ of $f$ at the points $c_i$. Interpolation goes the other way, i.e. from evaluations to coefficients. So we’d like to be able to invert the Vandermonde matrix.

As it happens, a square matrix over a commutative ring $R$ with $1$ is invertible if and only if its determinant is invertible in $R$ (the construction of the inverse matrix in terms of the adjugate demonstrates this). The determinant of the Vandermonde can be shown (using only column operations and properties of the determinant) to be the product of the differences $c_i – c_j$ for $i \ne j$. Thus we see that a polynomial $f \in R[X]$ of degree $< n$ is determined by its evaluations at $n$ distinct points if the differences of these evaluation points are invertible in $R$.

In fact, we can do much better: the differences don’t need to have inverses in $R$, they just need to be invertible in a larger ring: it in fact suffices that the differences are not zero divisors in $R$. For suppose that this is the case. Let $S$ be the multiplicative closure of the set of pairwise differences. Then $S$ contains no zero divisors, and so $R$ can be considered as a subring of its localization $S^{-1}R$ at the subset $S$. Importantly, the pairwise differences have inverses in $S^{-1}R$. Hence, by the above argument, any polynomial of degree $< n$ with coefficients in $S^{-1}R$ is determined by its evaluations at our points, and this of course continues to hold when the coefficients (and evaluations) lie in the subring $R$.

To return to the problematic example above: for any three distinct points in $R = \mathbb{Z}/ 8\mathbb{Z}$, either at least two of them are odd, or at least two of them are even, and in either case there will be a pair of distinct points whose difference is even and hence either zero or a zero divisor.

Efficient polynomial interpolation on $0, 1, 2, .. $ (the inverse Vandermonde on integer nodes)

The Vandermonde matrix computes evaluations of polynomials from their coefficients via a matrix-vector product. The Vandermonde matrices are nested, i.e. each Vandermonde matrix is the principal submatrix of any larger Vandermonde matrix that uses (an extension of) the same sequence of evaluation points. For example, here is the (square) Vandermonde matrix for the evaluation points $0, 1, 2, 3$; the Vandermonde matrix for $0, 1, 2$ is the $3$ x $3$ principal (i.e. top-left) submatrix:

If the inverse Vandermonde matrix exists, then the inverse computation (from evaluations to coefficients, i.e. polynomial interpolation), can also be performed as a matrix-vector product (for the inverse of the Vandermonde matrix to exist, it must be square and the evaluation points must be distinct). Unfortunately, the inverse Vandermonde matrices are no longer “nested” in the above sense. For example, here are the inverse Vandermonde matrices for evaluation points $0, 1, 2$ and $0, 1, 2, 3$.

Who cares? Well, it would be nice if they were nested since then one could pre-compute a sufficiently large inverse Vandermonde matrix, and be able to interpolate any polynomial that came along. But it just isn’t so! However, for the particular case where the evaluation points are the non-negative integers (and indeed in many more general cases), the neatness can be restored using a certain triangular decomposition of the Vandermonde matrix and its inverse.

Let’s write $V_n$ for the Vandermonde matrix defined by the evaluation points $0, 1, .., n$. Then $V_n$ can be expressed as a product of a lower-triangular, diagonal and upper-triangular matrices $L_n$, $D_n$, and $U_n$ i.e. $V_n = L_n D_n U_n$. Thus $V_n^{-1} = U_n^{-1} D_n^{-1} L_n^{-1}$. It turns out that all of these factors form nested families in the sense above. For example, $U_n$ is the principal $n$ x $n$ submatrix of any $U_{n+k}$, while $L_n^{-1}$ is the principal $n$ x $n$ submatrix of $L_{n+k}^{-1}$, and so on. Thus if we pre-compute $L_n^{-1}, D_n^{-1}, U_n^{-1}$ then we’ll be able to interpolate any polynomial of degree at most $n$ (by selecting the appropriate principal submatrix of each factor, and then performing three matrix-vector multiplications).

Furthermore, the entries of $L^{-1}, D^{-1}, U^{-1}$ (also of $L$, $D$, $U$) are given by pleasing and useful recursive formulae, and these can be used to increase the size of your precomputed matrices as required. For instance, $(L^{-1})_{i,j} = (-1)^{i-j} \binom{i}{j}$ and the identity $$\binom{i}{j} = \binom{i-1}{j-1} + \binom{i-1}{j}$$ is easily adapted to a recurrence on the $(L^{-1})_{i,j}$ that allows us to extend $L^{-1}$ as needed. The diagonal entries of $D^{-1}$ are given by $(D^{-1})_{i,i} = \frac{1}{i!}$ (recurrence obvious) while the entries of $U^{-1}$ are given by the (signed) Sterling numbers of the first kind via $(U^{-1})_{i,j} = s(j,i)$. These quantities satisfy the recurrence $$s(j, i) = s(j-1, i-1) – (j-1) s(j-1,i)$$ with boundary conditions $s(0,0) = 1$ and $s(k, 0) = s(0, k) = 0$ for all $k > 0$.

These formulae were first derived in Vandermonde matrices on integer nodes (Eisinberg, Franzé, Pugliese; 1998). Another useful (and freely available) reference is Symmetric functions and the Vandermonde matrix (Oruç & Akmaz; 2004) which deals with the case of $q$-integer nodes (take $q=0$ in their Theorem 4.1 to obtain the formulae above).