Frievalds' Algorithm for Probabilistic Proof of Matrix Products

July 19, 2024

Algorithm

  • Let A,B,C∈Fpn×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 A⋅B=CA\cdot B = C in O(k⋅n2)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 r∈Fpr \in \mathbb{F}_p and let x∈Fpn=(r0,r1,…,rn−1)x \in \mathbb{F}_p^n = (r^0,r^1,\dots,r^{n-1})
  • We can probabilistically compare the product matrix A⋅BA \cdot B with CC by multiplying both by xx and checking the resulting vectors for equality.
  • Instead of having to calculate A⋅BA\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 A⋅B=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
      A⋅B=C⇒∀x∈Fpn(A⋅Bx=Cx)A\cdot B = C \Rightarrow \forall x \in \mathbb{F}_p^n (A\cdot Bx= Cx)
    • If A⋅Bx=CxA\cdot Bx= Cx, it could be the case that A⋅BA \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 A⋅B=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=[c1⋅⋅xc2⋅⋅x⋮c2⋅⋅x]=[∑i=1nc1i⋅ri−1∑i=1nc2i⋅ri−1⋮∑i=1ncni⋅ri−1]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 A⋅BA\cdot B as DD for convenience. Since
      A⋅B=D⇒∀x∈Fpn(A⋅Bx=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=A⋅Bx=Dx=[d1⋅⋅xd2⋅⋅x⋮d2⋅⋅x]=[∑i=1nd1i⋅ri−1∑i=1nd2i⋅ri−1⋮∑i=1ndni⋅ri−1]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 A⋅BA\cdot B and CC in each entry. We can probabilistically check equality of row ii of A⋅BA\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=A⋅BD = C = A\cdot B
      ∃i∈{1,…,n}yi≠zi→C≠A⋅B\exists i \in \{1,\dots,n\} y_i\neq z_i \rightarrow C \neq A\cdot B

Error Probability

  • If A⋅B≠CA\cdot B \neq C, the error probability is the probability that y=zy= z, causing the algorithm to output EQUAL.
  • If A⋅B≠CA\cdot B \neq C, then there is at least one pair (i,j)∈{1,…,n}(i,j) \in \{1,\dots,n\} such that Dij≠CijD_{ij} \neq C_{ij}. Then row Di⋅≠Ci⋅D_{i\cdot} \neq C_{i\cdot}. Since yiy_i and ziz_i are Reed-Solomon fingerprints of the nn-dimensional vectors Di⋅D_{i\cdot} and Ci⋅C_{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:
    Pr⁡r∈Fp[yi=zi]≤(n−1p≈1n)\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}
    Pr⁡r∈Fp[A⋅B≠C∧A⋅Bx=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 x∈Fpn=(r0,r1,…,rn−1)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, x∈Fpn=(x1,…,xn)x \in \mathbb{F}_p^n = (x_1,\dots,x_n). In this algorithm we'll verify that (A⋅B)x=Cx(A\cdot B)x = Cx by creating a new vector and checking that each of its entries is 0.

q=(A⋅B)x−Cxq = (A\cdot B)x - Cx

If q=(0,…,0)Tq = (0, \dots, 0)^T then (A⋅B)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 AB−C=0AB-C=0. Then
q=(A⋅B)x−Cxq = (A\cdot B)x-Cx
q=((A⋅B)−C)xq= ((A\cdot B)-C) x
q=0⋅xq= 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 x∈Fpnx \in \mathbb{F}_p^n, the algorithm outputs EQUAL, therefore the error probability is zero.

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

If C≠ABC \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=AB−CE = AB-C. Then q=((A⋅B)−C)x=Exq= ((A\cdot B)-C) x = Ex. Since C≠ABC \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≠(A⋅B)ijC_{ij}\neq (A\cdot B)_{ij}. Then eij≠0e_{ij}\neq 0. The ithith entry in qq is the inner product of row ii of EE and xx:

qi=∑k=1neik⋅xkq_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=eij⋅xj+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

Pr⁡xj∈Fp[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

Pr⁡xj∈Fp[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.

Pr⁡xj∈Fp[q1=0∧⋯∧qi=0∧⋯∧qn=0]≤Pr⁡xj∈Fp[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 >1−1∣Fp∣\gt 1 - \frac{1}{|\mathbb{F}_p|}.


Profile picture

Written by Breads