krlosMata
    • 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

      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
    • 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 Note Insights 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

    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
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # zkProver debugging [TOC] ## Repositories involved - [zkproverjs](https://github.com/hermeznetwork/zkproverjs): prover reference implementation writen in javascript - [zkproverc](https://github.com/hermeznetwork/zkproverc): prover implementation writen in C - [zkasm](https://github.com/hermeznetwork/zkasm): compiles .zkasm to a json ready for the zkproverjs - [zkpil](https://github.com/hermeznetwork/zkpil): Polynomial Identity Language - [zkvmpil](https://github.com/hermeznetwork/zkvmpil): PIL source code for the zkVM (state-machines) - [zkrom](https://github.com/hermeznetwork/zkrom): zkasm source code of the zkEVM - [zkevmdoc](https://github.com/hermeznetwork/zkevmdoc): docs zkevm ## Setup environment - ideal repository structure: ``` github --> zkrom --> zkvmpil --> zkproverjs ``` - Next steps are required to run the `zkprover:executor`: ``` git clone https://github.com/hermeznetwork/zkrom.git cd zkrom npm i && npm run build cd .. git clone https://github.com/hermeznetwork/zkvmpil.git cd zkvmpil npm i && npm run build git clone https://github.com/hermeznetwork/zkproverjs.git cd zkproverjs npm i ``` - Detailed explanation: - repository `zkrom` - `main/*` : contains assembly code - `build`: compiled assembly. code ready to the executor - repository `zkvmpil` - `src`: state-machines - `build`: compiled state-machines. code ready to the executor - repository `zkproverjs` - `src/main_executor.js`: cli to run executor easily - executor needs files fenerated from `zkrom/build` & `zkvm pil/build` - it also needs an `input.json` - Examples: - [zkrom file](https://github.com/hermeznetwork/zkrom/blob/main/build/rom.json) - [zkvmpil file](https://github.com/hermeznetwork/zkvmpil/blob/main/build/zkevm.pil.json) - [input file](https://github.com/hermeznetwork/zkproverjs/blob/main/testvectors/input.json) - Run executor (in `zkproverjs` repository) >to just test the executor, the output is not needed ``` node src/main_executor.js ./testvectors/input.json -r ../zkrom/build/rom.json -p ../zkvmpil/build/zkevm.pil.json -o ./testvectors/poly.bin ``` ## Executor insights Basically, the executor runs the program that is specified by the ROM. The program can be seen in the `rom.json` file, which includes some debugging information. Let's see an example of `assembly code` builded into the `rom.json`: ``` ASSEMBLY: 1 => B JSON FILE: { "CONST": 1, "neg": 0, "setB": 1, "line": 51, "fileName": "../zkrom/main/main.zkasm" } ``` All operations are defined in the JSON file, plus `line` & `fileName` where the assembly code is. This JSON file is ready to be interpreted by the `executor` ## VSCode debugging In the `zkproverjs` repository you can find an example of `launch.json` to debug the executor code: https://github.com/hermeznetwork/zkproverjs/blob/main/.vscode/launch.json#L8 ## Debugging tips - Main executor code to debug: https://github.com/hermeznetwork/zkproverjs/blob/main/src/executor.js#L12 - variable `l` is the rom.json that is going to be executed: https://github.com/hermeznetwork/zkproverjs/blob/main/src/executor.js#L61 - debug helpers - [print registers](https://github.com/hermeznetwork/zkproverjs/blob/main/src/executor.js#L1030) - By monioring `ctx(context)`, registers and `op` you will see all the states changes made by the executor - `ctx.input` contins all the variables loaded from `input.json` - `storage` makes refrence to the merkle-tree - transactions places at `input.json` are pre-processed and store it on `ctx.pTxs`. Besides, `globalHash` is computed given all the `inputs.json` according to [specification (TO_BE_UPDATED)](https://hackmd.io/tEny6MhSQaqPpu4ltUyC_w#validateBatch) ## Table rom assembly instructions | NAME | DESCRIPTION | EXECUTION | |:---------:|:--------------------:|:--------------------------------------------------------------------------------------------------------------------------------------------------------------------------:| | MLOAD | memory load | op = mem(addr) | | MSTORE | memory storage | mem(addr) = op | | SLOAD | storage load | op = `storage.get(SR, H[A0 , A1 , A2 , B0 , C0 , C1 , C2 , C3, 0...0]))` where `storage.get(root, key) -> value` | | SSTORE | storage store | op = `storage.set(SR, (H[A0 , A1 , A2 , B0 , C0 , C1 , C2 , C3, 0...0], D0 + D1 * 2^64 + D2 * 2^128 + D3 * 2^192 )` where `storage.set(oldRoot, key, newValue) -> newRoot` | | HASHW | hash write bytes | hash[addr].push(op[0..D-1]) | | HASHE | hash end | hash[addr].end() | | HASHR | hash read | op = hash[addr].result | | ARITH | arithmetic operation | AB + C = D OR op | | SHL | shift left | op = A << D | | SHR | shift right | op = A >> D | | ECRECOVER | signature recover | op = ECRECOVER( A: HASH, B: R, C:S, D: V) | | ASSERT | assertion | A = op | ## Examples assembly ### MSTORE - assembly ```javascript= A :MSTORE(sequencerAddr) ``` - rom.json ```json= { "inA": 1, "neg": 0, "offset": 4, "mWR": 1, "line": 9, "offsetLabel": "sequencerAddr", "useCTX": 0, "fileName": ".../zkrom/main/main.zkasm" } ``` - description: load `A` register in `op`, write in memory position 4 (`offset`) the `op` value ### MREAD - assembly: ```javascript= $ => A : MLOAD(pendingTxs) ``` - rom.json ```json= { "freeInTag": { "op": "" }, "inFREE": 1, "neg": 0, "setA": 1, "offset": 1, "mRD": 1, "line": 25, "offsetLabel": "pendingTxs", "useCTX": 0, "fileName": ".../zkrom/main/main.zkasm" } ``` - description: load a memory value from position 1 (`offset`) into `op` (action marked by `inFREE`), set `op` in `A` register ### LOAD FROM STORAGE - assembly ```javascript= $ => A :MLOAD(sequencerAddr) 0 => B,C $ => A :SLOAD ``` - rom.json ```json= { "freeInTag": { "op": "" }, "inFREE": 1, "neg": 0, "setA": 1, "offset": 4, "mRD": 1, "line": 47, "offsetLabel": "sequencerAddr", "useCTX": 0, "fileName": ".../zkrom/main/main.zkasm" }, { "CONST": 0, "neg": 0, "setB": 1, "setC": 1, "line": 48, "fileName": ".../zkrom/main/main.zkasm" }, { "freeInTag": { "op": "" }, "inFREE": 1, "neg": 0, "setA": 1, "sRD": 1, "line": 49, "fileName": ".../zkrom/main/main.zkasm" } ``` - description - load from memory position 5 (`sequencerAccValue`) into `op`, store `op` on `D` register - load from memory position 4 (`sequencerAddr`) into `op`, store `op` on `A` register - load CONST in `op`, store it in registers `B` and `C` - Perform SLOAD (reading from merkle-tree) with the follwing key: `storage.get(SR, H[A0 , A1 , A2 , B0 , C0 , C1 , C2 , C3, 0...0]))` - `SR` is the current state-root saved in register `SR` - `A0, A1, A2` has the sequencer address - `B0` is set to `0` pointing out that the `balance` is going to be read - `C0,C1,C2,C3` are set to 0 since they are not used when reading balance from merkle-tree - merkle-tree value is store in `op` (marked by `inFREE` tag), set `op` to register `A` ### WRITE TO STORAGE - assembly ```javascript= $ => A :MLOAD(sequencerAddr) 0 => B,C $ => SR :SSTORE ``` - rom.json ```json= { "freeInTag": { "op": "" }, "inFREE": 1, "neg": 0, "setA": 1, "offset": 4, "mRD": 1, "line": 56, "offsetLabel": "sequencerAddr", "useCTX": 0, "fileName": ".../zkrom/main/main.zkasm" }, { "CONST": 0, "neg": 0, "setB": 1, "setC": 1, "line": 57, "fileName": ".../zkrom/main/main.zkasm" }, { "freeInTag": { "op": "" }, "inFREE": 1, "neg": 0, "setSR": 1, "sWR": 1, "line": 58, "fileName": ".../zkrom/main/main.zkasm" } ``` - description - read from memory position 4 (`sequencerAddr`) and store it on `op`, set `op` to register A - set CONST to `op`, store `op` in registers `B` and `C` - Perform SWRITE (write to merkle-tree) according: `storage.set(SR, (H[A0 , A1 , A2 , B0 , C0 , C1 , C2 , C3, 0...0], D0 + D1 * 2^64 + D2 * 2^128 + D3 * 2^192 )` - `SR` is the current state-root saved in register `SR` - `A0, A1, A2` has the sequencer address - `B0` is set to `0` pointing out that the `balance` is going to be read - `C0,C1,C2,C3` are set to 0 since they are not used when reading balance from merkle-tree - `D0, D1, D2, D3` is the value writen in the merkle-tree pointed out by `H[A0 , A1 , A2 , B0 , C0 , C1 , C2 , C3, 0...0]` --> in this example register `D` has the balance of the `seqAddr` - write merkle-tree state root in `SR` register

    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