Gauss labs
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Help
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Write
Owners
  • Owners
  • Signed-in users
  • Everyone
Owners Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    2
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # CKKS SIMD encoding & decoding We need a way to map a vector in $C^{N/2}$ to polynomial in $R/X^N+1$. Define the $M^{th}$ cyclotomic polynomial $$X^N+1 = \prod_{\omega_j \in P_M} X - \omega_j$$ where $P_M = {\zeta^i | i \in Z^*_M}$ (i.e. the set of all $M^{th}$ primitive root of unity) Since $X-\omega_j$ are co-primes, due to CRT, there exist following isomorphism: $$X^N+1 \cong \prod_{\omega_j \in P_M} X - \omega_j$$ The map that reduces polynomial in $R/X^N+1$ to its representation in $X-\omega_j$ is a simple evaluation of polynomial over $\omega_j$. Reducing the polynomial for each $\omega_j$ materialises into a metric vector product. More formally, <!-- This implies we can map a polynomial in ring $\mod X^N+1$ to vector of complex values. Inverse of the map gives us a way to map vector of complex values to a polynomial with real coefficients in $\mod X^N+1$. --> Define $M^{th} = 2N^{th}$ root of unity $\zeta = e^{\pi i / N}$ and the set: $$\{\zeta_j, \bar{\zeta_j}\ 0 \le j \lt N/2 \}$$ where $\zeta_j = \zeta^{5^{j}}$. The set consists of all $M^{th}$ primitive root of unity and equals the other set $\{\zeta^i | i \in Z^*_M\} = P_M$. This is because integer 5 has order M/4 and spans across $Z^*_M$ with -1 (Take this as granted) Define map $\sigma(m)$ that sends $m \in \mathbf{R}/X^N+1$ to $z \in C^N/2$ (i.e. $\prod_{\omega_j \in P_M} X - \omega_j$) as $$\begin{bmatrix} z \\ \bar{z} \end{bmatrix} = \sigma(m) = \begin{bmatrix} U \\ \bar{U} \end{bmatrix} \cdot m$$ where $$U = \begin{matrix} 1 & \zeta_0 & \zeta_0^2 & \cdots & \zeta_0^{N-1}\\ 1 & \zeta_1 & \zeta_1^2 & \cdots & \zeta_1^{N-1}\\ \vdots \\ 1 & \zeta_{N/2-1} & \zeta_{N/2-1}^2 & \cdots & \zeta_{N/2-1}^{N-1}\\ \end{matrix}$$ and $\bar{U}$ is conjugate matrix of $U$. Observe that: 1. The map evaluates $m$ at values of $\zeta_j$ and $\bar{\zeta_j}$. Matrix $U$ contains powers of $\zeta_j$ and matrix $\bar{U}$ contains powers of $\bar{\zeta_j}$. 2. First half of resulting vector of complex values is complex conjugate of second half. This is because $\zeta_j$ is complex conjugate of $\bar{\zeta_j}$ 3. Due to 2, if we were to map a vector of $C^N$ to $\mathbf{R}/X^N+1$, we can only utilise half of the vector space (i.e. can only encode N/2 complex elements). Inverse of $\sigma$ which sends $z \in C^{N/2}$ to $m \in \mathbf{R}/X^N+1$ is: $$ \begin{bmatrix} U \\ \bar{U} \end{bmatrix}^{-1} = \frac{1}{N} \begin{bmatrix} \bar{U}^T & U^T\\ \end{bmatrix} $$ $$ \sigma^{-1}(z): \frac{1}{N} \begin{bmatrix} \bar{U}^T & U^T\\ \end{bmatrix} \cdot \begin{bmatrix} z \\ \bar{z} \end{bmatrix} = \frac{1}{N} \begin{bmatrix} \bar{U}^Tz + U^T \bar{z} \\ \end{bmatrix} $$ **Encoding** Given a complex vector $z \in C^{N/2}$ we find $m \in \mathbf{R}/X^N+1$ as: $$ m = \sigma^{-1}(z) = \frac{1}{N} \begin{bmatrix} \bar{U}^Tz + U^T \bar{z} \\ \end{bmatrix} $$ However, in practice we want to encode $z$ in $Z_Q/X^N+1$ which requires a few additional steps: 1. After encoding $z$ into $m \in R/X^N+1$, we scale $m$'s coefficients by $\Delta$ and then randomly round resulting coefficients to find scaled $m \in Z/X^N+1$. 2. Coefficients of $m$ are $\in Z$. We map coefficients from signed interval to unsigned interval $[0, Q)$ and output $m \in Z_Q/X^N+1$. **Decoding** To map $m \in Z_Q/X^N+1$ to $z \in C^{N/2}$, 1. map coefficients of $m$ from unsigned interval in $Z_Q$ to signed interval $\in Z$ to find $m \in Z/X^N+1$. 2. Scale down the coefficients of $m$ by $\Delta$ to find $m \in \mathbf{R}/X^N+1$. 3. Apply $\sigma(m)$ to find $z \in C/X^N+1$ *Note: $\Delta$ may vary across the circuit and \Delta used for decoding may not equal \Delta used for encoding.* **Encoding and decoding using Special(I)FFT** Observe that $X^N+1$ splits into $X^{N/2}-i$ and $X^{N/2}+i$, which implies each polynomial in ring $R/X^N+1$ has unique representation in ring $R/X^{N/2}-i$. We can reduce representation of polynomial in ring $R/X^N+1$ to ring $R/X^{N/2}-i$ by simply setting term $X^{N/2} = i$. And the inverse is the opposite: set the real values of polynomial in $R/X^{N/2}-i$ as first half coefficients and set imaginary values of polynomial in $R/X^{N/2}-i$ as second half coefficients of polynomial in ring $R/X^N+1$ We cannot map a vector $z \in C^{N/2}$ to $X^{N/2} - i$ directly efficiently. Instead observe that following change of base for $F(X) \in R/X^N-i$ from $X \to \omega X$ where $\omega$ is $2N^{th}$ root of unity results in: $$F(X) = H(X) + G(X) (X^N-i)$$ $$F(\omega X) = H(\omega X) + G(\omega X) ((\omega X)^{N/2}-i)$$ Since $\omega^{N/2} = i$, $$F(\omega X) = H(\omega X) + iG(\omega X) (X^{N/2}-1)$$ $$F(\omega X) = H(\omega X)\mod{X^{N/2}-1}$$ Therefore, using Inverse FFT (Inverse cyclic convolution), we map $z \in C^{N/2}$ to $R/X^N-1$ and then multiply coefficients by powers of $\omega^{-1}$ to find the corresponding representation in ring $R/X^{N/2}-i$. Hence, we can encode $z \in C^{N/2}$ to ring $R/X^N+1$ by (1) using FFT of size N/2 to find its representation in $R/X^{N/2}-1$, followed by (2) complex multiplication by powers of $\omega^{i}$ to find representation in $R/X^{N/2}-i$, and then (3) setting real values as first half coefficients and imaginary values as second half coefficients to finally find polynomial in ring $R/X^N+1$ To decode $m \in R/X^N+1$, extract first half coeffcients and set them as reals of polynomial in $R/X^{N/2}-i$ and second half coefficients as the imaginary values. Then multiply the polynomial by powers of $\omega$ to find corresponding representation in $R/X^N-1$. Followed by FFT transform to find $z \in C^{N/2}$. Algorithm 1 of [paper](https://eprint.iacr.org/2018/1043.pdf) shows how to pack all these steps into single FFT like operation. We refer to them as SpecialFFT (decoding) / SpecialIFFT (encoding). ## Rotations Let $\rho_k$ be the following map: $$\rho_k: m(X) \rightarrow m(X^{k})$$ To send slot element at slot $i$ to slot $j$ define, $$k = 5^{i-j} \mod{M}$$ Given $m(X)$, which encrypts slots $(z_i)_{0 \le i \lt N/2} \in C^{N/2}$, map $\rho_k$ outputs $$\tilde{m}(X) = m(X^k) = \rho_k(m(X))$$ where $\tilde{m}(X)$ encrypts slots $(\tilde{z}_j)_{0 \le j \lt N/2}$ and $\tilde{z}_j = z_i$ To understand why notice: $$\tilde{z}_j = \tilde{m}(\zeta_j) = m(\zeta_j^{5^{i-j}}) = m(\zeta^{5^j(5^{i-j})}) = m(\zeta^{5^i}) = m(\zeta_i) = z_i$$ ## Conjugation We know that $$\zeta^{-1} = \bar{{\zeta}}$$ Conjugation of $m(X)$ that encrypts slots $(z_i)_{0 \le i \lt N/2} \in C^{N/2}$ is defined as map: $$\tilde{m}(X) = m(X^{-1}) = \rho_{-1}(m(X))$$ where $\tilde{m}(X)$ encrypts slots $(\tilde{z}_i)_{0 \le i \lt N/2}$. This follows from: $$\tilde{z}_i = \tilde{m}(\zeta_i) = m(\zeta_i^{-1}) = m(\bar{\zeta_i}) = \bar{z}_i$$ ## References 1. https://eprint.iacr.org/2016/421.pdf # Homomorphically switching from Coefficient to SIMD encoding Recall that a vector of complex values $z \in C^{N/2}$ is encoded as plaintext $m \in R/X^N+1$ as: $$ m = \frac{1}{N} \begin{bmatrix} \bar{U}^Tz + U^T \bar{z} \\ \end{bmatrix} $$ Observe that we can extract upper half coefficients of $m$ and lower half coefficients of $m$ by splitting $U$ into left and right halves. Define $$U_0 = \begin{matrix} 1 & \zeta_0 & \zeta_0^2 & \cdots & \zeta_0^{\frac{N}{2}-1}\\ 1 & \zeta_1 & \zeta_1^2 & \cdots & \zeta_1^{\frac{N}{2}-1}\\ \vdots \\ 1 & \zeta_{N/2-1} & \zeta_{N/2-1}^2 & \cdots & \zeta_{N/2-1}^{\frac{N}{2}-1}\\ \end{matrix}$$ AND $$U_1 = \begin{matrix} \zeta_0^{\frac{N}{2}} & \zeta_0^{\frac{N}{2}+1} & \zeta_0^{\frac{N}{2}+2} & \cdots & \zeta_0^{N-1}\\ \zeta_1^{\frac{N}{2}} & \zeta_1^{\frac{N}{2}+1} & \zeta_1^{\frac{N}{2}+2} & \cdots & \zeta_1^{N-1}\\ \vdots \\ \zeta_{\frac{N}{2}-1}^{\frac{N}{2}} & \zeta_{\frac{N}{2}-1}^{\frac{N}{2}+1} & \zeta_{\frac{N}{2}-1}^{\frac{N}{2}+2} & \cdots & \zeta_{\frac{N}{2}-1}^{N-1}\\ \end{matrix}$$ Where $$U = U_0 | U_1$$ Then we can extract first half, $z'_0$, and second half, $z'_1$, coefficients of message $m$ via linear transforms on the plaintext $z$ using the identity: $$ z'_k = \frac{1}{N} \begin{bmatrix} \bar{U_k}^Tz + U_k^T \bar{z} \\ \end{bmatrix} $$ **How we do calculate $\bar{U_k}^Tz$ (or $U_k^T \bar{z}$) homomorphically?** It is a simple matrix vector product and can be evaluated via rotations, multiplications, and additions while consuming just one multiplicative depth (because there are no consecutive multiplications on ciphertexts). For techniques on efficient evalution of matrix vector product homomorhpically I will refer you to section 4.3 of https://eprint.iacr.org/2014/106.pdf However, doing this via naive matrix vector multiplication will be too expensive in terms of runtime. This is because the dimension of matrix $U$ is really large (for ex. $2^{15}$). Hence, we need to figure out a faster way to evaluate the same matrix. If you are familiar with FFTs/NTTs, then you will notice that encoding equation for $z$ can be evaluated using FFTs and, indeed, in practice to encode vector $z$ as $m$ we use a FFT like algorithm. Can we leverage FFT view of the algorithm do speed up evaluating $\bar{U_k}^Tz$? Well, yes. Following is matrix formulation of FFTs: ![image](https://hackmd.io/_uploads/r1-ymh1l0.png) *credit: https://www2.math.uconn.edu/~olshevsky/classes/2018_Spring/math3511/FFT.pdf* $F_N$ can recursively be written as product of $\log{N}$ matrices. The interesting thing about the matrices on the right is all of them are sparse. In fact, each has atmost 3 non-zero diagonals. This is good news for us because we can reduce rotations and scalar multiplication to at-most $3*\log{N}$. However, as you will notice, this increases the no. of consecutive multiplications on input vector to $\log{N}$, hence multiplicatve depth increases to $\log{N}$. We can reduce the depth consumed by composing consecutive matrices into groups of $r$, thus reducing the depth to $\log{N}/r$, at the cost of runtime (since combining cosecutive matrices causes no. of non-zero diagonals in each matrix to increase). ## References 1. https://eprint.iacr.org/2016/421.pdf 2. https://eprint.iacr.org/2018/1073.pdf

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password

    or

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully