Sanaz Taheri-Boshrooyeh
    • 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
    # Off-chain tree root ## Contract: State and functions and their gas costs Smart contract state contains - An ordered list of group members public keys mapped to the incremental indices. - Tree calculations are performed off-chain. The root of the tree and auth path of pubkeys are not available publicly. | Method/Cost | Operation cost | Calldata cost | Estimate per pubkey | | :-------------- | :------------------------ | :------------ | :------------------ | | Single Deposit | `store_word` + `base_fee` | `word` | 40k | | Batch Deposit | `store_word` \* `B` | `word` \* `B` | 20k | | Single Withdraw | `hash` + `base_fee` | `word` | 40k | | Batch Withdraw | `B` \* `hash` | `word` \* `B` | 20k | Estimates are for `depth` = 20 (This table is borrowed from https://hackmd.io/JoxnlDq3RT6WhtA-KBxtYg?both#A-Pubkey-map. The depth has no effect on the gas cost) In the description below, epoch equals M seconds. ## User local state The user maintains the entire tree. The full tree with depth `20` will require `67MB` storage. She can also maintain a partial tree which lowers the storage overhead to `0.128 KB`. Howeer, there is a problem with slashing when users only hold the partial tree. The user always persists the tree `root`s for the last `E` epochs. ## Registration The user sends a transaction containing her `pk` and `X` Eth (in addition to the transaction fee) to the smart contract. That transaction invokes the register function of the contract. As the result, the user's `pk` gets inserted into the list of pks. The user needs to listen for an insertion event to be emitted with her `pk` and some `index`. After that, the user can start using waku-rln-relay. The user also needs to read the state of the contract and downloads all the registered pks. The user then creates a Merkle tree and its `root` out of the group pks. The user can use that tree to compute her authentication path `auth_path`. The estimated gas cost is `40k`. This is for performing the insertion without locking any ether for the sake of slashing. **TODO** One thing to keep in mind is that the state of the contract will expand as a function of number of public keys registered in it. It isn't unlikely that we'll hit a limit here in regards to contract storage. Currently on Ethereum mainnet, there seems to be a 24kb limit for contract creation. It isn't clear if this limit is the same for updating a contract. The contract size limit of 24kb was introduced as an anti DoS mechanism. This also relates to the storage rent discussion (which doesn't seem to have lead anywhere at the time of this writing, but might change with things like stateless clients in Eth2). Things might look different on L2s etc. To be investigated further. ## Synchronizing with the contract The user keeps listening to the state of the contract for the pk **insertion** and pk **deletion** events. <!--@TODO Oskar: This has a requirement on listening for events, which should probably not be underestimated. If the user isn't running their own node and light nodes aren't solid enough but using something like Infura (initially) etc we would probably want to be mindful of how often we poll for this. This will impact requirements on how uptodate root is. This is in addition to privacy concerns etc, which are out of the scope of this document probably --> <!--@TODO: this seems to assume they will be online, I think the more likely scenario is they'll be mostly online and then you have to query based on block height, so getEvents(<event-bloom-topic>, block height, latest) => [DepositEvent, ...]--> If the user goes offline, then she can catch up with the current state of the group by looking at the current state of the contract (no need for listening to the past events). A deleted pk is replcaced with a zero, which indicates the deletion operation/event. She reflects each perceived update into her local Merkle tree and recomputes the `root` and her `auth_path`. The full tree with depth 20 will require `67 MB` storage. The user always persists the tree `root`s for the last `E` epochs (Number of blocks, or seconds). ## Prove The user selects a message. Measures the current epoch (measure the number of epochs passed since the Unix epoch). Uses her local Merkle tree to obtain the root and her auth path (the Merkle tree gets constantly updated in the background during the synchronization with the contract phase, think of it as a parallel process that keeps the tree updated based on the incoming events) Computes Shamir shares of her sk, the nullifier and creates zkSNARK proof. Publishes the message with the proof and other public inputs (epoch, root, Shamir shares, nullifier) to the waku-rln-relay pubsub topic. No gas cost. ## Verify The user receives a `message` with the `proof` (via waku-relay). checks the pubsub topic to match the waku-rln-relay pubsub topic. The user checks whether the tree `root` attached to the message matches any of the roots recorded within the last `E` epochs (this is a raw idea and needs more thoughts, there is no enforcement, it is just a parameter). If no, discards the message. Otherwise, checks the `nullifier`, if there is a match, checks whether the messages are the same or not (compare the `share_x` components), if they are different, proceeds with slashing. Otherwise, verifies the `proof`. If the `proof` is verified, relays the message, otherwise, discards. Note that we are assuming the verifier is honest, there's no verification on chain or dispute mechanism. (Same is true for on-chain case). If the prover is ahead of the verifier, the prover can attach the version of the tree to the message, and then the verifier can resalize she is behind and then can update herself. No gas cost. ## Slash The slasher reconstructs the spammer `sk_spam` using the two distinct Shamir shares. The slasher identifies the `index_spam` of the spammer's pk in the group (the index of the tree leaf holding the `pk`). It does so by comparing the `H(sk_spam)` with all the leaves of the tree. Therefore, the entire list of pks (tree leaves) should be available for this search to work out. The slasher sends a transaction containing the `sk_spam` and the `index_spam` to the contract which in turn invokes a function corresponding to slashing (pk deletion). The deletion/slashing function checks whether `H(sk) = list[index]`. Accordingly, the fund of the deleted member is transferred to the transaction owner. <!--@TODO: We currently have an open issue regarding race i.e., where two users simultaneously slash the same user, however, based on our latest discussion, this seems not a big issue --> The estimated gas cost is `40k`. # On-chain tree root ## Contract: State and functions and their gas costs State of the smart contract contains - `RMT`: The rightmost branch of the tree (that includes the tree root as well) ``` update = hash * depth (to raclculate the root) update_batch = hash * (depth - b) + hash * (B-1) inclusion_check = hash * depth (inclusion check is to prove that the deleted pk belongs to the tree) ``` | Method/Cost | Operation cost | Calldata cost | Estimate per pubkey | | :---------------- | :--------------------------------- | :---------------------- | :------------------ | | Single Deposit (Register) | `update` | `word` | ~406k | | Batch Deposit | `update_batch` | `word` \* `B` | ~23.02k | | Single Withdraw (Delete) | `update` + `inclusion_check` | `word` \* `depth` (`depth` is for the `auth_path`) | ~812.5k | Estimates are for `depth` = 20 (this table is derived from https://hackmd.io/JoxnlDq3RT6WhtA-KBxtYg?both#A-Pubkey-map, the values are updated for `d=20` and `B=128` In the description below, epoch equals M seconds. ## User local state The user maintains her `auth path` and the `root`. For a tree with depth `d=20`, she requires: - `auth path` = 20 hash values - `root` = 1 hash value Unlike the off-chain case, there is no need to store the entire tree. The user always caches the tree `root`s for the last `E` epochs. While the tree root for all the past epochs is available on the chain, caching some portion of epochs locally speeds up the message verification process, otherwise, the user has to read all the emitted roots from the contract per incoming message with a mismatching root. ## Registration The user sends a transaction that contains her `pk` together with `X` ether to be locked on the contract. The `X` ether is in addition to the `gas fee` she has to pay for the transaction to be mined. That transaction calls the register function of the contract. That function recalculates the tree root after the inclusion of `pk` in the tree. The registration function emits the insertion event with the inserted `pk`, its `index`, and the new `root'`. The user needs to listen for an insertion event to be emitted with her `pk` and some `index` and `root`. After that, the user can start using waku-rln-relay. The user computes her `auth_path` using the state of the contract i.e., `RMT` at the time of the insertion of her `pk`. <!--@TODO I need to double-check to see if the auth_path can be constructed from RMT --> Estimated gas cost `406k`. This is the cost for performing the insertion without locking any ether for the sake of slashing. ## Synchronization with the contract The user keeps listening to the state of the contract for the pk **insertion** and pk **deletion** events. With each perceived update she updates her `auth path` and stores the new perceived `root`. The `Pk` insertion event emits the inserted `pk`, `index`, `auth_path` and the new `root`. She recalculates her `auth path` using the new `pk`, adn the new `pk`'s `auth_path` (no further info is needed). The `pk` deletion event carries the deleted `pk_del`, `index_del`, `auth_path_del`, and the `root'` i.e., the updated root. The user updates her `auth_path` using the `auth path_del`. For the offline users, they have to look at the past events to update their auth paths. This might be aggregated with some event aggregation service such as The Graph. Otherwise query time/complexity will be a function of offline time. ## Prove The user selects a `message`. Measures the current epoch (measure the number of epochs passed since the Unix epoch). She uses her `auth_path` and the Merkle tree `root` to generate the proof. Note that the tree `root` and `auth_path` get constantly updated in the background during the synchronization with the contract phase, think of it as a parallel process that keeps the tree updated based on the incoming events. Computes Shamir shares of her `sk`, the `nullifier` and creates zkSNARK `proof`. Publishes the `message` with the `proof` and other public inputs (`epoch`, `root`, `Shamir shares`, `nullifier`) to the waku-rln-relay pubsub topic. No gas cost. ## Verify The user receives a `message` with the `proof` (via waku-relay). checks the pubsub topic to match the waku-rln-relay pubsub topic. The user checks whether the tree `root` attached to the message matches any of the roots recorded within the last E epochs (this is a raw idea and needs more thoughts). If no, discards the message. Otherwise, checks the `nullifier`, if there is a match, checks whether the messages are the same or not (compare the `share_x` components), if they are different, proceeds with slashing. Otherwise, verifies the `proof`. If the `proof` is verified, relays the message, otherwise, discards. Note that, users do not and MUST not accept messages whose proofs use an old root, the reason being that a spammer may attempt to use an old version of the tree in which her pk was still part of the group. (**a proposal**) All the roots preceding a pk deletion event should be deleted. **TODO** There are some subtleties to consider here. A prover can either be ahead of a verifier's root or be in the past. Both cases should have well-defined semantics in terms of how up to date you are to the chain assumptions, etc. Example: 100 nodes, 10 light nodes that can only do batch query of past events (e.g. Infura rate limit) and 1 spammer. After spammer slashed, the tradeoff is between (a) Damage they can do to system over some elastic/slack period vs (b) Damage this does to 10 light nodes in terms of requiring them to be up to date. Argument could be made that relay nodes should just DO THEIR JOBS. But Infura might make you pay $$ :( Isolate as parameters and make behaviour explicit, then we can just set some reasonable defaults for now. No gas cost. ## Slash The slasher reconstructs the spammer `sk_spam` using the two distinct Shamir shares. Identifying the spammer `index_spam` and `auth_path_spam`: The user needs to go through the history of events of the contract and identifies the index of the spammer's pk in the group (the index of the tree leaf holding the pk). It does so by comparing the `H(sk_spam)` with all the inserted pks. The slasher also needs to find the `auth_path_spam`. This can be done by either 1- querying a full node that persists the entire tree, or 2- by recalculating the tree from scratch locally. The slasher sends a transaction containing the `sk` and the `index` and the `auth_path_spam` to the contract which in turn invokes a function corresponding to slashing (pk deletion). The deletion/slashing function checks the inclusion of the `pk_spam`. Accordingly, the fund of the deleted member is transferred to the transaction owner. The function also recalculates the root based on the submitted `auht_path_spam`. (Side note: We currently have an open issue regarding race i.e., where two users simultaneously slash the same user). The estimated gas cost is `812.5k`.

    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