# HeMat An method for HE matrices multiplication and transpose. A matrix is encoded in slots as a long vector i.e., concating the rows. Matrix multiplication/transpose computation use linear transformation to permute the slots. $A\cdot B=\sum_{k=1}^d A_k\odot B_k$ where $A_k$ and $B_k$ are permutations of $A,B$ of $\dim d$ . $A_0$ can be computed from $A$ with $O(d)$ rotations and $A_k$ , $1\le k \le d$ can be computed with $O(1)$ rotations. Similarly applies to $B_k s. Matrix multiplication takes $O(d)$ operation compared to $O(d^2)$ in best standard solutions. ![[Pasted image 20231108182948.png]] $A_k=\phi^k\circ \sigma(A)$, $B_k=\psi^k\circ\tau(B)$ . where: $ \begin{align} \sigma(A)_{i,j}&=A_{i,i+j}\\ \tau(A)_{i,j}&=A_{i_j,j}\\ \phi(A)_{i,j}&=A_{i,j+1}\\ \psi(A)_{i,j}&=A_{i+1,j} \end{align} $ ## Correctness $\begin{align}(A\cdot B)_{i,j}&=\sum_{k=0}^{d-1} A_{i,k}\cdot B_{k,j}\\ &= \sum_{k=0}^{d-1} A_{i,i+j+k}\cdot B_{i+j+k,j}\\&= \sum_{k=0}^{d-1} \sigma(A)_{i,j+k}\cdot \tau(B)_{i+k,j}\\&= \sum_{k=0}^{d-1}\big(\phi^k\circ\sigma(A)\big)_{i,j}\cdot \big(\psi^k\circ\tau(B)\big)_{i,j} \end{align}$ This means that $A\cdot B=\sum_{k=1}^d A_k\odot B_k$ Note: the second equation is true as iterating over $\{k| k=0,\ldots,d\}=\{ i+j+k\bmod d|k=0,\ldots,d\}$ ## Analysis of non-zero diagonals (number of rotations) $\sigma$ has $2d-1$ $\tau$ has $d$ $\phi^k$ (column shifting) has 2 $\psi^k$ (row shifting) has 1 ## Baby-step-giant-step optimization Rotations for $\sigma$ can be reduced to $3\sqrt{d}$ and for $\tau$ down to $2\sqrt{d}$ . $\sigma$ has $2d-1$ diagonal and so let $k$ be in range $(-d,d)$ then $k=\sqrt{d}\cdot i+j$ for $0\le i < \sqrt{d}, 0\le j<\sqrt{d}$. $U^\sigma\cdot a=\sum_{-\sqrt{d}\le i < \sqrt{d}}\bigg(\sum_{0\le j<\sqrt{d}} u^\sigma_{\sqrt{d}\cdot i+j}\odot \rho(a;\sqrt{d}\cdot i+j)\bigg)=\sum_{-\sqrt{d}\le i < \sqrt{d}}\rho\bigg(\sum_{0\le j<\sqrt{d}}a_{i,j}\ ;\sqrt{d}\cdot i\bigg)$ where $a_{i,j}=\rho\bigg(u^\sigma_{\sqrt{d}\cdot i+j}\ ; -\sqrt{d}\cdot i\bigg)\odot\rho(a\ ;j)$ The left $\rho$ is computed over plaintext. Computing $\sum_{j}\rho(a\ ; j)$ requires $\sqrt{d}$ rotations and additions. Computing the outer sum require $2\sqrt{d}$ rotations. Total $3\sqrt{d}$ rotations. Similarly, $\tau$ can be computed by $2\sqrt{d}$ rotations. ## Optimizing $\phi$ $\phi^k$ has 2 diagonals $v_k$ and $v_{k-d}$. The sum of $\rho(v_k;-k)$ and $\rho(v_{k-d};\ d-k))$ generates a plaintext the have 1's in all its slots i.e., $A_0=\rho(v_k;-k)\odot A_0 + \rho(v_{k-d};d-k)\odot A_0$ We have $\phi^k(A_0)=v_k\odot \text{Rot}(A_0;k)+v_{k-d}\odot\text{Rot}(A_0;k-d)$ . we can reduce the two multiplications down to one by using one more rotation, one more addition and expressing the two terms with $\rho(v_k;-k)\odot A_0$ as follows: $\begin{align} v_k\odot \text{Rot}(A_0;k)&=\text{Rot}\big(\rho(v_k;-k)\odot A_0;k\big) \\ \\ v_{k-d}\odot\text{Rot}(A_0;k-d)&=\text{Rot}\big(\rho(v_{k-d}; d-k)\odot A_0 ; k-d\big)\\ &=\text{Rot}\big(A_0-\rho(v_k;-k)\odot A_0 ;k-d\big) \end{align}$ for each $k$ , $0\le k\le d$ compute $\rho(v_k;-k)\odot A_0$ . Reference: [[@Secure Outsourced Matrix Computation and Application to Neural Networks (HeMat)]] Tag: #math ## Created 2023-11-08 18:17