HashCloak
      • 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
    • 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
    • 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 Help
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
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
  • 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
    # Semaphore Noir Tutorial # Installation To install semaphore-noir, we rely on local dependencies currently. It shall be moved to the npm registry in future updates. 1. Clone the repositories and switch to correct branch ```bash git clone https://github.com/hashcloak/semaphore-noir.git git clone https://github.com/hashcloak/snark-artifacts.git cd semaphore-noir && git fetch && git switch noir-support cd snark-artifacts && git fetch && git switch semaphore-noir ``` 2. In [proof/package.json](https://github.com/hashcloak/semaphore-noir/blob/noir-support/packages/proof/package.json#L58) and [noir-proof-batch/package.json](), update the dependencies for `@zk-kit/artifacts` to the local path of `snark-artifacts` cloned in step 1. 3. Build snark-artifact ```bash cd snark-artifact/packages/artifacts pnpm install pnpm build ``` 4. Build and test semaphore-noir ```bash cd semaphore-noir yarn install yarn build yarn test ``` # Semaphore Noir identities / groups Semaphore identities and groups in Semaphore Noir are identical with the Semaphore V4. Please refer to the guides for [identities](https://docs.semaphore.pse.dev/guides/identities) and [groups](https://docs.semaphore.pse.dev/guides/groups). Note: for installation, run ```bash yarn add "@semaphore-protocol/proof@file:<YOUR_PATH_TO>/hashcloak-noir/semaphore-noir/packages/identity" yarn add "@semaphore-protocol/proof@file:<YOUR_PATH_TO>/hashcloak-noir/semaphore-noir/packages/group" ``` # Semaphore Noir proof ## Install identity, group and proof ```bash yarn add "@semaphore-protocol/proof@file:<YOUR_PATH_TO>/hashcloak-noir/semaphore-noir/packages/identity" yarn add "@semaphore-protocol/proof@file:<YOUR_PATH_TO>/hashcloak-noir/semaphore-noir/packages/group" yarn add "@semaphore-protocol/proof@file:<YOUR_PATH_TO>/hashcloak-noir/semaphore-noir/packages/proof" ``` 0. Import necessary packages ```ts= import { Group } from "@semaphore-protocol/group" import { Identity } from "@semaphore-protocol/identity" import { generateNoirProof, verifyNoirProof, SemaphoreNoirProof, SemaphoreNoirBackend, initSemaphoreNoirBackend, getMerkleTreeDepth } from "@semaphore-protocol/proof" ``` 1. Create identities & groups For details on creating identities and groups, refer to the guides above. In this example, we will simply create an identity and a group as: ```ts= const identity = new Identity("secret") const group = new Group([identity.commitment, 1n, 2n, 3n, 4n, 5n, 6n]) ``` 2. Choose the scope and message Each proof requires a scope, on which each user may only generate one valid proof. The scope, together with the user's private key, is used to generate the nullifier, which is the value you can actually use to check whether a proof with that scope has already been generated by that user. In a voting application where double-voting must be prevented, the scope could be the ballot id, or the Merkle root of the group. ```ts= const message = "Hello world" const scope = "Scope" ``` 3. Initialize a Noir proving backend To improve efficiency, we separate the initialization of a proving backend with the actual proving step. That way, we can reuse the proving backend as long as the `merkle_depth` in the backend can generate a Merkle tree big enough to hold all the members of the group we are proving / verifying. A Semaphore group is stored in a Merkle tree. The depth of a tree decides how many members a group can hold ($2^{merkle\_depth}$). The smaller the depth is, the more efficient the ZK circuit become. ```ts= const merkleTreeDepth = 4 // a helper function to calculate the tree depth // const merkleTreeDepth = getMerkleTreeDepth(identity, group) const backend = await initSemaphoreNoirBackend(merkleTreeDepth) ``` 4. Generate and Verify a proof ```ts= const proof = await generateNoirProof(identity, group, message, scope, backend) const isValid = await verifyNoirProof(proof, backend) console.log(isValid) // true ``` # Semaphore Noir Contracts We updated the Semaphore Solidity contracts that it is now using a [UltraHonk verifier](https://github.com/hashcloak/semaphore-noir/blob/noir-support/packages/contracts-noir/contracts/base/SemaphoreNoirVerifier.sol). Other functionalities should be similar to the Semaphore V4. ## Deploy the Contracts To interact with the contract, first deploy the contracts with the [deploy script](https://github.com/hashcloak/semaphore-noir/blob/noir-support/packages/contracts-noir/tasks/deploy.ts). (Deployed contracts on testnet can be found [here](https://github.com/hashcloak/semaphore-noir/blob/noir-support-part2/packages/utils/src/networks/deployed-contracts-noir.json)) 1. Setup [hardhat.config.ts](https://github.com/hashcloak/semaphore-noir/blob/noir-support/packages/contracts-noir/hardhat.config.ts) with intended networks. ```ts // e.g. local testnet networks: { test: { url: "http://127.0.0.1:8545/", accounts: ["private_key"] }, ... }, ``` 3. Run the deploy script ``` yarn compile yarn deploy --network <network_name> ``` ## Interact with the Contracts 0. Setup ether.js ```ts= // import group, proof, identity as above import { ethers } from "ethers"; // we are using local testnet in this example const provider = new ethers.JsonRpcProvider("http://127.0.0.1:8545/") const signer = await provider.getSigner() const semaphoreContract = new ethers.Contract("SemaphoreNoir_addr", SemaphoreNoir_abi, signer) ``` 1. Create group on-chain and off-chain ```ts= // create a Semaphore identity const identity = new Identity("0") // members of the group const members = Array.from({ length: 3 }, (_, i) => new Identity(i.toString())).map( ({ commitment }) => commitment ) // create a group with the members locally const group = new Group(members) // create a group and add 3 members on chain let tx = await semaphoreContract["createGroup(address)"](signer) await tx.wait() // groupId = 1 since it is the first group we create const groupId = 1 // add members to group tx = await semaphoreContract.addMembers(groupId, members) await tx.wait() ``` 2. Update and Remove Members ```ts= // Remove the third member. { // off-chain group.removeMember(2) const { siblings } = group.generateMerkleProof(2) //on-chain let tx = await semaphoreContract.removeMember(groupId, members[2], siblings) await tx.wait() } // Update the second member. { // off-chain group.updateMember(1, members[2]) const { siblings } = group.generateMerkleProof(1) // on-chain let tx = await semaphoreContract.updateMember(groupId, members[1], members[2], siblings) await tx.wait() } ``` 3. Create and Verify Proofs We can directly use the `proof` package mentioned above to create a proof to verify on-chain. However, the on-chain verifier uses a different hash function (keccak) than the off-chain verifier (poseidon). Thus we have to add an additional flag to indicate the use of keccak when generating a proof for the on-chain verifier. ```ts= const treeDepth = getMerkleTreeDepth(identity, group) const backend = await initSemaphoreNoirBackend(treeDepth) // generate a proof with keccak = true const proof = await generateNoirProof(identity, group, "msg", group.root, backend, true) tx = await semaphoreContract.validateProof(groupId, proof) // tx will emit a "ProofValidated" event if success const receipt = await tx.wait() console.log(receipt.logs) ```

    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