Jonathan Wang
    • Create new note
    • Create a note from template
      • 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
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee
    • 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
    • Engagement control
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control 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
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee
  • 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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # circom-ecc spec ## Preliminaries ### 'Signs' in circom: A native number in circom is an element of $\{0,\dotsc,r - 1\}$ where $r$ is the baby JubJub prime. We map these elements to the integers as follows: ``` toZ(z) = z-r if r//2 +1 <= x < r toZ(z) = z, otherwise. ``` We have $z$ and $toZ(z)$ are always congruent to each other mod $r$. **Lemma 1.** Let $a,b,c \in \mathbb F_r$. Assume that $|toZ(a)| |toZ(b)| + |toZ(c)| < r/2$. Then $toZ(a\cdot b + c) = toZ(a)toZ(b) + toZ(c)$. *Proof.* We know that $ab +c \equiv toZ(ab+c) \pmod r$. Since $a \equiv toZ(a) \pmod r$, $b \equiv toZ(b) \pmod r$, $c \equiv toZ(c) \pmod r$, we have $$ toZ(ab + c) \equiv toZ(a)toZ(b) + toZ(c) \pmod r. $$ The LHS is uniquely determined by being in range $(-r/2, r/2)$. The assumption implies the RHS is also in range $(-r/2,r/2)$ by triangle inequality. Thus LHS = RHS. $\square$ ### Proper BigInt representation Given parameters `n, k` we say that a length `k` array `a` is a **proper BigInt representation** if each **register** `0 <= a[i] < 2^n` for all `i in range(k)`. The BigInt being represented is $$ \sum_{i=0}^{k-1} a_i \cdot 2^{n \cdot i} $$ In other words, this is base `2^n` little-endian notation. We assume throughout that `n <= 252` so `2^n < r/2` (`r` is 254 bits). ### Signed overflow representation Given parameters `n, k` we say that a length `k` array `a` is a **signed overflow representation** if $a_i < r/2$ for all $i$ and the integer being represented is $$ \sum_{i=0}^{k-1} toZ(a_i) \cdot 2^{n\cdot i} $$ Note the $a_i$ are allowed to have absolute value $|toZ(a_i)|$ possibly larger than `2^n`. We will say **max overflow bits** of `a` to mean ``` max_{i in range(k)} abs(toZ(a[i])).bit_length() ``` ## Circuits ### `BigMultNoCarry(n, ka, kb, m_out)` [Signed Version] [circom-ecdsa](https://github.com/0xPARC/circom-ecdsa/blob/08c2c905b918b563c81a71086e493cb9d39c5a08/circuits/bigint.circom#L179) Aka `BigMultShortLongUnequal` in [circom-pairing](https://github.com/yi-sun/circom-pairing/blob/743d761f07254ea6407d29ba05f29886cfd14aec/circuits/bigint.circom#L277) Multiplication of two polynomials in one variable `X`. #### Inputs: - `a[]` is array of length `ka` as [signed overflow representation](#Signed-overflow-representation) - `b[]` is array of length `kb` - Say $|toZ(a_i)| < B_a$ for $0 \le i < k_a$ and $|toZ(b_i)| < B_b$ for $0 \le i < k_b$. Then assume that $$ min(k_a, k_b) B_a \cdot B_b < r/2$$ #### Output: - `out[]` is array of length `ka + kb - 1` - `out[]` is uniquely defined by the polynomial identity $$ \sum_{i=0}^{k_a + k_b - 2} toZ(out_i) \cdot X^i = \left( \sum_{i=0}^{k_a - 1} toZ(a_i) \cdot X^i \right) \left( \sum_{i=0}^{k_b - 1} toZ(b_i) \cdot X^i \right) $$ as polynomials in the variable $X$. #### Parameters: - `n` is passed for convenience but not actually used! - `ka, kb` - [Convenience; not used in proof] For code readability/verification, `m_out` is passed as a parameter to indicate that all `toZ(out[i])` for `0 <= i < ka + kb - 1` **should** have absolute value `< 2^{m_out}`. #### Proof: `out[]` is given as witness values. Compute $$\begin{align} out(x) &= \sum_{i=0}^{k_a + k_b - 2} out_i \cdot x^i \\ a(x) &= \sum_{i=0}^{k_a - 1} a_i \cdot x^i \\ b(x) &= \sum_{i=0}^{k_b - 1} b_i \cdot x^i \end{align} $$ for values $x = 0,1,\dotsc, k_a + k_b - 2$. These terms are linear in the signals $out_i, a_i, b_i$. Constrain $out(x) === a(x) b(x)$ for $x = 0,\dotsc, k_a+k_b - 2$. This means that $out(X) - a(X)b(X)$ is a degree $\le k_a + k_b - 2$ polynomial with at least $k_a + k_b - 1$ roots. This implies $out(X) - a(X)b(X) = 0$. Expanding the polynomial multiplication $a(X)b(X)$, we deduce that $$ out_i = \sum_{\substack{0\le j < k_a \\ 0\le i-j < k_b}} a_j \cdot b_{i-j}.$$ Apply $toZ$ to both sides. The extra assumption implies that $$ \sum_{\substack{0\le j < k_a \\ 0\le i-j < k_b}} |toZ(a_j)| \cdot |toZ(b_{i-j})| < r/2 $$ By repeated applications of Lemma 1, we deduce that $$ toZ(out_i) = toZ\left(\sum_{\substack{0\le j < k_a \\ 0\le i-j < k_b}} a_j \cdot b_{i-j} \right) = \sum_{\substack{0\le j < k_a \\ 0\le i-j < k_b}} toZ(a_j) \cdot toZ(b_{i-j}). $$ This proves the claim. ### `PrimeReduce(n, k, m, p, m_out)` [circom-pairing](https://github.com/yi-sun/circom-pairing/blob/743d761f07254ea6407d29ba05f29886cfd14aec/circuits/bigint.circom#L786) #### Parameters - `n, k, m` - `p[]` is length `k` array with `p[i]` in `[0, 2^n)` representing the positive integer $p = \sum_{i=0}^{k-1} p_i \cdot 2^{n\cdot i}$. Note: $p$ does not actually need to be prime for this template! #### Input: - `in` is length `k + m` array - Say $|toZ(in_i)| < B$ for $0 \le i < k + m$. Assume that $B \cdot (1 + 2^n\cdot m) \le 2^{252} < r/2$. #### Output: - `out` is length `k` array - The integer $\sum_{i=0}^k toZ(out_i) \cdot 2^{n\cdot i}$ is congruent modulo $p$ to the integer $\sum_{i=0}^{k+m-1} toZ(in_i) \cdot 2^{n\cdot i}$. #### Proof: For $i \ge k$, we precompute $r_i = 2^{n\cdot i} \pmod p$, where $r_i$ is an integer in $[0, p)$. Since $p = \sum_{i=0}^{k-1} p_i \cdot 2^{n\cdot i}$ and $p_i \in [0, 2^n)$, we know that $p \in [0, 2^{n\cdot k})$. Therefore $r_i \in [0, 2^{n\cdot k})$. Thus we can also decompose $r_i$ in base $2^n$ as $$ r_i = \sum_{j=0}^{k-1} r_{ij} 2^{n\cdot j}. $$ For $i \ge k$, we have $$ toZ(in_i) \cdot 2^{n\cdot i} \equiv \sum_{j=0}^{k-1} toZ(in_i) \cdot r_{ij} \cdot 2^{nj} \pmod p $$ Summing, we get $$\begin{align} \sum_{i=0}^{k+m-1} toZ(in_i) 2^{n\cdot i} &\equiv \sum_{i=0}^{k-1} toZ(in_i) 2^{n\cdot i} + \sum_{i=k}^{k+m-1} \sum_{j=0}^{k-1} toZ(in_i) r_{ij} 2^{n\cdot j} \pmod p \\ &\equiv \sum_{i=0}^{k-1} \left( toZ(in_i) + \sum_{j=k}^{k+m-1} toZ(in_j) r_{ji} \right) 2^{n\cdot i} \pmod p \end{align}$$ We define $out_i := in_i + \sum_{j=k}^{k+m-1} in_j \cdot r_{ji}$ for $i=0,\dotsc,k-1$. Since $r_{ji} \in [0, 2^n)$, we have $$ |toZ(in_i)| + \sum_{j=k}^{k+m-1} |toZ(in_j)| |r_{ji}| < B(1+2^n m) < r/2 $$ Thus Lemma 1 implies that $$ toZ(out_i) = toZ(in_i) + \sum_{j=k}^{k+m-1} toZ(in_j) r_{ji}. $$ This proves the claim. ### `CheckCarryToZero(n, m, k)` circom-ecdsa [circom-pairing](https://github.com/yi-sun/circom-pairing/blob/743d761f07254ea6407d29ba05f29886cfd14aec/circuits/bigint.circom#L742) #### Parameters - `n, m, k` - $\epsilon$ - Assume $m -1 + \epsilon < 252$` = r.bit_length() - 2` #### Input: - `in` is length `k` array - Assume that $|toZ(in_i)| < 2^{m-1}$ for $i=0,\dotsc,k-1$ #### Output: - No output - Constrain that $\sum_{i=0}^{k-1} toZ(in_i)\cdot 2^{i\cdot n} == 0$ #### Proof We proceed inductively by "carrying". The condition for $\sum_{i=0}^{k-1} toZ(in_i) \cdot 2^{i \cdot n} == 0$, is equivalent to producing witnesses $c_0,\dotsc, c_k \in \mathbb Z$ such that: - $toZ(in_0) = c_0 \cdot 2^n$ - $toZ(in_i) + c_{i-1} = c_i \cdot 2^n$ for $i=1,\dotsc,k-2$ - $toZ(in_{k-1}) + c_{k-2} == 0$ Observe that $$ c_i \cdot 2^n \le |toZ(in_i)| + |toZ(in_{i-1})|/2^n + \dotsb < 2^{m-1}( 1 + 2^{-n} + 2^{-2n} + \dotsb) < 2^m$$. Therefore if we range check $c_i \in [-2^{m-1-n+\epsilon}, 2^{m-1-n+\epsilon})$ for $\epsilon \ge 1$, then the above conditions are equivalent to: - $in_0 = c_0 \cdot 2^n$ - $in_i + c_{i-1} = c_i \cdot 2^n$ for $i=1,\dotsc,k-2$ - $in_{k-1} + c_{k-2} == 0$ where we abuse notation and now consider $c_i$ as elements of $\mathbb F_r$. (We are using Lemma 1, which says $toZ(c_0) toZ(2^n) = toZ(c_0\cdot 2^n)$.) This proves the claim. **Aztec optimization:** Let $a_i = toZ(in_i)$. Let $w$ denote some small window size. Note that after multiplying both sides of $a_{i} + c_{i-1} = c_{i} \cdot 2^n$ by $2^{nw}$, it can be expanded to $$ a_{i} \cdot 2^{n\cdot w} + a_{i-1} \cdot 2^{n\cdot (w-1)} + \dotsb + a_{i-w} + c_{i-w-1} = c_{i} \cdot 2^{n\cdot (w+1)} $$ This is valid as long as $m - 1 + \epsilon + n w < \log_2(r) - 1$. So we should set $w = \lfloor (\log_2(r) - m - \epsilon) / n \rfloor$. We can now proceed exactly as in the previous proof, except that we only need to range check $c_i \in [-2^{m-1-n+\epsilon}, 2^{m-1-n+\epsilon})$ every $w + 1$ steps, starting with $i = w$.

    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