HackMD
  • Prime
    Prime  Full-text search on all paid plans
    Search anywhere and reach everything in a Workspace with Prime plan.
    Got it
      • Create new note
      • Create a note from template
    • Prime  Full-text search on all paid plans
      Prime  Full-text search on all paid plans
      Search anywhere and reach everything in a Workspace with Prime plan.
      Got it
      • Options
      • Versions and GitHub Sync
      • Transfer ownership
      • Delete this note
      • Template
      • Save as template
      • Insert from template
      • Export
      • Dropbox
      • Google Drive
      • Gist
      • Import
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
      • Download
      • Markdown
      • HTML
      • Raw HTML
      • Sharing Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Note Permission
      • Read
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • Write
        • Owners
        • Signed-in users
        • Everyone
        Owners Signed-in users Everyone
      • More (Comment, Invitee)
      • Publishing
        Everyone on the web can find and read all notes of this public team.
        After the note is published, everyone on the web can find and read this note.
        See all published notes on profile page.
      • Commenting Enable
        Disabled Forbidden Owners Signed-in users Everyone
      • Permission
        • Forbidden
        • Owners
        • Signed-in users
        • Everyone
      • Invitee
      • No invitee
    Menu Sharing Create Help
    Create Create new note Create a note from template
    Menu
    Options
    Versions and GitHub Sync Transfer ownership Delete this note
    Export
    Dropbox Google Drive Gist
    Import
    Dropbox Google Drive Gist Clipboard
    Download
    Markdown HTML Raw HTML
    Back
    Sharing
    Sharing Link copied
    /edit
    View mode
    • Edit mode
    • View mode
    • Book mode
    • Slide mode
    Edit mode View mode Book mode Slide mode
    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
    More (Comment, Invitee)
    Publishing
    Everyone on the web can find and read all notes of this public team.
    After the note is published, everyone on the web can find and read this note.
    See all published notes on profile page.
    More (Comment, Invitee)
    Commenting Enable
    Disabled Forbidden Owners Signed-in users Everyone
    Permission
    Owners
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Invitee
    No invitee
       owned this note    owned this note      
    Published Linked with GitHub
    Like BookmarkBookmarked
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # Private order matching & zk atomic swap Private order matching uses the 'Socialist Millionare' protocol to match the trade orders without revealing the details. The matched result can also be used to enforce a zk transfer pool to execute a zk atomic swap correctly. ## Private order matching ### Step 1. Make a new order Let's assume that Alice wants to get 100 DAI by giving 1 ETH. And Bob wants to get 1 ETH by giving 100 DAI. Alice's order: `'GET DAI' (1, 100)(ETH/DAI)` Bob's order: `'GET ETH' (1, 100)(ETH/DAI)` ``` sell = field('GET DAI') buy = field('GET ETH') order = field('(1, 100)(ETH/DAI)') ``` | What only Alice knows | What everyone knows | What only Bob knows | | -------- | -------- | -------- | |$order_a$ | $sell$, $buy$, $pk_a$, $pk_b$ | $order_b$ | ### Step 2. Create the homomorphic hidings for each other. In this situation, Alice and Bob will generates new homomorphic hidings for the 'socialist millionare' protocol using their own salts and each other's public key. As a result, each homomorphic hidings are below: - Alice's homomorphic hiding: $H_a = g^{sell \cdot pk_b \cdot salt_{a} \cdot order_a}$ - Alice's public salt: $S_a = g^{sell \cdot pk_b \cdot salt_{a}}$ - Bob's homomorphic hiding: $H_b = g^{buy \cdot pk_a \cdot salt_{b} \cdot order_b}$ - Bob's public salt: $S_b = g^{buy \cdot pk_a \cdot salt_{b}}$ And they share their homomorphic hidings each other. | What only Alice knows | What everyone knows | What only Bob knows | | -------- | -------- | -------- | |$order_a$, $salt_a$ | $sell$, $buy$, $pk_a$, $pk_b$, $H_a$, $H_b$, $S_a$, $S_b$ | $order_b$, $salt_b$| ### Step 3. Alice and Bob checks that they have a same order. Now using the given values, Alice and Bob check that they have a same order by themselves. - If Alice's calculation $H_b == order_a \cdot S_b$ is true, she knows that $order_a == order_b$. - If Bob calculate $H_a == order_b \cdot S_a$ is true, he also knows that $order_b == order_a$. - Now they know that they have a same order each other. | What Alice only knows | What everyone knows | What Bob only knows | | -------- | -------- | -------- | |$order_a$, $salt_a$ , $order_a \cdot S_b == H_b$| $sell$, $buy$, $pk_a$, $pk_b$, $H_a$, $H_b$, $S_a$, $S_b$ | $order_b$, $salt_b$, $order_b \cdot S_a == H_a$| ## Using $H$ for zk atomic swap To make the atomic swap happen with the matched order, use the combined homomorphic hiding $H$ to combine 2 zk transfers. Here $H$ equals to the below: $H = g^{sell \cdot buy \cdot pk_a \cdot pk_b \cdot salt_a \cdot salt_b \cdot order}$ And then Alice's and Bob's zk transfers for the atomic swap should look like this: #### Alice's zk transfer for the atomic swap | - NoteA1(input) | + NoteA2(output) | + NoteA3(output) | Sum | | --------------- | ---------------- | ---------------- | --- | | ETH: 3 | ETH: 2 | ETH: 1 | 0 | | DAI: 500 | DAI: 500 | DAI: 0 | 0 | | Owner: Alice | Owner: Alice | Owner: Bob | | #### Bob's zk transfer for the atomic swap | - NoteB1(input) | + NoteB2(output) | + NoteB3(output) | Sum | | --------------- | ---------------- | ---------------- | --- | | ETH: 8 | ETH: 8 | ETH: 0 | 0 | | DAI: 900 | DAI: 800 | DAI: 100 | 0 | | Owner: Bob | Owner: Bob | Owner: Alice | | In this pair, Alice splits her asset into two notes and give one to Bob. Otherwise, Bob also split his assets and give one to Alice. The newly exchanging notes should have the same information in the homomorphicall hidden commitment $H$. ### Alice's SNARK ``` public inputs: Note A1's nullifier Note A2's hash Note A3's hash H<=(sell, buy, pk_a, pk_b, salt_a, salt_b, order) private inputs: Note A1's details [3, 500, Alice] Note A1's spending signature from Alice's key Note A2's details [2, 500, Alice] Note A3's details [1, 0, Bob] Bob's public key [pk_b] Bob's public salt S_b<=(buy, pk_a, salt_b) order [1, 100] = (order_x, order_y) salt_a Constraints: 1. The hash of Note A1, A2, and A3 are all valid. 2. Note A1's included in the shielded pool. 3. Note A1's nullifier is correctly derived from the A1's details. 4. output notes - input notes == 0 (A2 + A3 - A1 == 0) 5. A3's owner == pk_b 6. A3's value == if 'sell' then (order_x, 0) else (0, order_y) 7. if sell: H == sell * pk_b * salt_a * order * S_b elif buy: H == buy * pk_b * salt_a * order * S_b ``` ### Bob's SNARK ``` public inputs: Note B1's nullifier Note B2's hash Note B3's hash H<=(sell, buy, pk_a, pk_b, salt_a, salt_b, order) private inputs: Note B1's details [8, 900, Bob] Note B1's spending signature from Alice's key Note B2's details [8, 800, Bob] Note B3's details [8, 100, Alice] Alice's public key [pk_a] Alice's public salt S_a<=(buy, pk_b, salt_a) order [1, 100] = (order_x, order_y) salt_a Constraints: 1. The hash of Note B1, B2, and B3 are all valid. 2. Note B1's included in the shielded pool. 3. Note B1's nullifier is correctly derived from the B1's details. 4. output notes - input notes == 0 (B2 + B3 - B1 == 0) 5. B3's owner == pk_a 6. B3's value == if 'sell' then (order_x, 0) else (0, order_y) 7. if sell: H == sell * pk_a * salt_b * order * S_a elif buy: H == buy * pk_a * salt_b * order * S_a ``` With this construction, we can let Alice and Bob match their order privately and also execute the zk atomic swap.

    Import from clipboard

    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 lost their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template is not available.


    Upgrade

    All
    • All
    • Team
    No template found.

    Create custom template


    Upgrade

    Delete template

    Do you really want to delete this template?

    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 via Google

    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

    Tutorials

    Book Mode Tutorial

    Slide Mode Tutorial

    YAML Metadata

    Contacts

    Facebook

    Twitter

    Feedback

    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

    Versions and GitHub Sync

    Sign in to link this note to GitHub Learn more
    This note is not linked with GitHub Learn more
     
    Add badge Pull Push GitHub Link Settings
    Upgrade now

    Version named by    

    More Less
    • Edit
    • Delete

    Note content is identical to the latest version.
    Compare with
      Choose a version
      No search result
      Version not found

    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. Learn more

         Sign in to GitHub

        HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.

        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
        Available push count

        Upgrade

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Upgrade

        Danger Zone

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

        Syncing

        Push failed

        Push successfully