Frievalds' Algorithm for Probabilistic Proof of Matrix Products

July 19, 2024

Algorithm

  • Let A,B,CFpn×nA, B, C \in \mathbb{F}_p^{n\times n} be n×nn \times n matrices where p>n2p > n^2 and pp is prime. Frievalds' algorithm will enable us to probabilistically prove that AB=CA\cdot B = C in O(kn2)O(k\cdot n^2) time.
    • Reading a single n×nn \times n matrix is already an O(n2)O(n^2) operation
    • Multiplying an n×nn \times n matrix by a vector is an O(n2)O(n^2) operation since each entry in the output vector is a sum of nn terms, and there are nn total entries
  • Pick a random element rFpr \in \mathbb{F}_p and let xFpn=(r0,r1,,rn1)x \in \mathbb{F}_p^n = (r^0,r^1,\dots,r^{n-1})
  • We can probabilistically compare the product matrix ABA \cdot B with CC by multiplying both by xx and checking the resulting vectors for equality.
  • Instead of having to calculate ABA\cdot B, which would take more than O(n2)O(n^2) time, we can calculate w=Bxw = Bx and then calculate AwAw which can be done in O(n2)O(n^2).
    • If AB=CA \cdot B = C then they represent the same linear transformation so they will always map the same input vectors to the same output vector
      AB=CxFpn(ABx=Cx)A\cdot B = C \Rightarrow \forall x \in \mathbb{F}_p^n (A\cdot Bx= Cx)
    • If ABx=CxA\cdot Bx= Cx, it could be the case that ABA \cdot B and CC are linear transformations which happen to behave in the same way for the input vector xx. There is no guarantee that AB=CA \cdot B = C.
      • Division by a vector is not defined, so we can't cancel the xx on each side.
  • Let y=Cxy = Cx.
    • Each entry in yy is the Reed-Solomon fingerprint of the corresponding row of CC, evaluated at rr. The iith entry of yy is a linear combination of the standard monomial basis, where the coefficients are given by the entries of the iith row of CC, evaluated at rr:
      y=Cx=[c1xc2xc2x]=[i=1nc1iri1i=1nc2iri1i=1ncniri1]y = Cx = \begin{bmatrix} c_{1\cdot}\cdot x \\ c_{2\cdot}\cdot x \\ \vdots \\ c_{2\cdot}\cdot x \\ \end{bmatrix} = \begin{bmatrix} \sum_{i=1}^{n} c_{1i}\cdot r^{i-1} \\ \sum_{i=1}^{n} c_{2i}\cdot r^{i-1} \\ \vdots \\ \sum_{i=1}^{n} c_{ni}\cdot r^{i-1} \\ \end{bmatrix}
  • Let z=Dxz = Dx.
    • To compute zz, first calculate BxBx, which results in an nn-dimensional vector, and then multiply it by AA. Both steps take O(n2)O(n^2) time.
    • We'll represent the product of ABA\cdot B as DD for convenience. Since
      AB=DxFpn(ABx=Dx)A\cdot B = D \Rightarrow \forall x \in \mathbb{F}_p^n (A \cdot B x = Dx)
    • The entries in zz are Reed-Solomon fingerprints of the corresponding rows of the product matrix DD evaluated at rr
      z=ABx=Dx=[d1xd2xd2x]=[i=1nd1iri1i=1nd2iri1i=1ndniri1]z = A\cdot Bx = Dx = \begin{bmatrix} d_{1\cdot}\cdot x \\ d_{2\cdot}\cdot x \\ \vdots \\ d_{2\cdot}\cdot x \\ \end{bmatrix} = \begin{bmatrix} \sum_{i=1}^{n} d_{1i}\cdot r^{i-1} \\ \sum_{i=1}^{n} d_{2i}\cdot r^{i-1} \\ \vdots \\ \sum_{i=1}^{n} d_{ni}\cdot r^{i-1} \\ \end{bmatrix}
  • Now we have two nn-dimensional vectors, yy and zz, containing Reed-Solomon fingerprints of the rows of their corresponding matrices ABA\cdot B and CC in each entry. We can probabilistically check equality of row ii of ABA\cdot B and row ii of CC by checking that yi=ziy_i = z_i.
    • If for all i{1,,n}i \in \{1,\dots,n\} yi=ziy_i=z_i, it is likely that D=C=ABD = C = A\cdot B
      i{1,,n}yiziCAB\exists i \in \{1,\dots,n\} y_i\neq z_i \rightarrow C \neq A\cdot B

Error Probability

  • If ABCA\cdot B \neq C, the error probability is the probability that y=zy= z, causing the algorithm to output EQUAL.
  • If ABCA\cdot B \neq C, then there is at least one pair (i,j){1,,n}(i,j) \in \{1,\dots,n\} such that DijCijD_{ij} \neq C_{ij}. Then row DiCiD_{i\cdot} \neq C_{i\cdot}. Since yiy_i and ziz_i are Reed-Solomon fingerprints of the nn-dimensional vectors DiD_{i\cdot} and CiC_{i\cdot}, the probability that they are the same for different vectors is bound by the vector length divided by the size of the field:
    PrrFp[yi=zi](n1p1n)\Pr_{r \in \mathbb{F}_p}[y_i = z_i] \leq (\frac{n-1}{p} \approx \frac{1}{n})
    • Since we picked p>n2p \gt n^2 we can bound the error probability to 1n\frac{1}{n}
  • The probability that the full vectors yy and zz are equal is at least as low as the probability that they are equal at the iith entry, so the error probability is 1n\leq \frac{1}{n}
    PrrFp[ABCABx=Cx]1n\Pr_{r\in \mathbb{F}_p}[A\cdot B \neq C \land A\cdot Bx = Cx] \leq \frac{1}{n}

Analysis With a Fully Random Vector

Instead of using xFpn=(r0,r1,,rn1)x \in \mathbb{F}_p^n = (r^0,r^1,\dots,r^{n-1}), we can use a totally random vector of elements in Fp\mathbb{F}_p, xFpn=(x1,,xn)x \in \mathbb{F}_p^n = (x_1,\dots,x_n). In this algorithm we'll verify that (AB)x=Cx(A\cdot B)x = Cx by creating a new vector and checking that each of its entries is 0.

q=(AB)xCxq = (A\cdot B)x - Cx

If q=(0,,0)Tq = (0, \dots, 0)^T then (AB)x=Cx(A\cdot B)x = Cx so the algorithm will output EQUAL. If q(0,,0)Tq \neq (0, \dots, 0)^T, the algorithm outputs NOT-EQUAL.

1. If C = AB, the error probability is zero

If C=ABC = AB, the error probability is the probability that q(0,,0)Tq \neq (0, \dots, 0)^T, causing the algorithm to return NOT-EQUAL. If C=ABC = AB, then ABC=0AB-C=0. Then
q=(AB)xCxq = (A\cdot B)x-Cx
q=((AB)C)xq= ((A\cdot B)-C) x
q=0xq= 0\cdot x
q=0q = 0, causing the algorithm to output EQUAL.
Since xx was any vector in Fpn\mathbb{F}_p^n, we can conclude that if C=ABC = AB, for all xFpnx \in \mathbb{F}_p^n, the algorithm outputs EQUAL, therefore the error probability is zero.

2. If CABC \neq AB, the error probability is 1Fp\frac{1}{|\mathbb{F}_p|}

If CABC \neq AB, the error probability is the probability that q=(0,,0)Fpnq = (0, \dots, 0) \in \mathbb{F}_p^n, which causes the algorithm to output EQUAL. Let E=ABCE = AB-C. Then q=((AB)C)x=Exq= ((A\cdot B)-C) x = Ex. Since CABC \neq AB, EE is not the zero matrix and thus EE has at least one nonzero entry. Let (i,j){1,,n}2(i,j) \in \{1,\dots,n\}^2 be a pair such that Cij(AB)ijC_{ij}\neq (A\cdot B)_{ij}. Then eij0e_{ij}\neq 0. The ithith entry in qq is the inner product of row ii of EE and xx:

qi=k=1neikxkq_i = \sum_{k=1}^{n} e_{ik}\cdot x_k

We can express this sum in terms of the nonzero entry of EE, eije_{ij} times its corresponding entry in xx, xjx_j plus a constant rr representing the remaining terms in the sum.

qi=eijxj+rq_i = e_{ij}\cdot x_j + r

Then qi=0    xj=reijq_i = 0 \iff x_j = \frac{-r}{e_{ij}}. Since xjx_j is a random element of Fp\mathbb{F}_p, and Fp\mathbb{F}_p contains pp elements

PrxjFp[xj=reij]=1p\Pr_{x_j \in \mathbb{F}_p} [x_j = \frac{-r}{e_{ij}}] = \frac{1}{p}

So the probability that the iith entry of qq is zero even though the iith row of EE contains a nonzero entry is

PrxjFp[qi=0]=1p\Pr_{x_j \in \mathbb{F}_p} [q_i = 0] = \frac{1}{p}

and the probability that the entire vector qq is 0 is at least as low as the probability that the iith entry is 0.

PrxjFp[q1=0qi=0qn=0]PrxjFp[qi=0]1p\Pr_{x_j \in \mathbb{F}_p} [q_1 = 0 \land \dots \land q_i = 0 \land \dots\land q_n = 0] \leq \Pr_{x_j \in \mathbb{F}_p} [q_i = 0] \leq \frac{1}{p}

Therefore, if there is even one (i,j){1,,n}2(i,j) \in \{1,\dots,n\}^2 such that Cij(AB)ijC_{ij} \neq (AB)_{ij} then the probability that the algorithm outputs NOT-EQUAL is >11Fp\gt 1 - \frac{1}{|\mathbb{F}_p|}.


Profile picture

Written by Breads