PSE zkEVM
      • 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

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

    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
    <style>table { white-space: nowrap; }</style> ## Introduction This note aims to explore how EVM circuit handles EOA call (transaction) and internal call. EVM circuit basically iterate over a list of transactions and verify each transaction's update is applied to state trie. Also each transaction could have serveral recursive internal calls with max depth `1024`. Every time when we encounter a internal call, we switch to a new execution environment. And we switch back to caller when encountering a explicit `STOP` and `REVERT`, or any kinds of error. But it's hard for circuit to memorize all caller's execution state like `program_counter`, `stack_pointer`, etc... So we use state circuit to help maintain the consistency of execution state just like the way we maintain stack and memory. So this note proposes 3 extra targets in state circuit: 1. `Tx` - Immutable object of tx information shared between all internal calls within same tx. 2. `Call` - Immutable object of call information (including EOA call's). 3. `CallState` - Mutable state of call. where `Tx` and `Call` might be loaded in evm circuit directly instead of in state circuit becasue it's immutable. ## Structure Each call has `Tx`, `Call` and `CallState`, we seperate different call by a unique identifier `Id`. ### `Tx` | Name | Description | | ---------- | ---------------------------------- | | `Id` | sequence id of tx | | `Origin` | address of tx sender (EOA address) | | `GasPrice` | gas price of tx | ### `Call` | Name | Description | | -------------------- | -------------------------------------------------------------------------------------------------------------------------- | | `Id` | sequence id of call | | `TxId` | tx's id | | `Depth` | depth of call, should `∈ [0,1024]` | | `GlobalCounterBegin` | global counter at the beginning of call | | `CallerId` | caller’s id | | `CallerAddress` | address of caller | | `CalldataOffset` | offset of calldata | | `CalldataSize` | size of calldata | | `CodeAddress` | address of code | | `ReceiverAddress` | address of receiver | | `GasAvailable` | gas given of call | | `Value` | value in wei of call | | `Result` | result of call. A bool success when `*CALL*`, and address when `CREATE*` | | `GlobalCounterEndOfRevert` | global counter in the end of revert section, see [here](https://hackmd.io/G48BKqdPScyoFDHPNzgOYQ) for more | | `IsPersistant` | if call's state write will persist (if call is within red box, see [here](https://hackmd.io/G48BKqdPScyoFDHPNzgOYQ) for more) | | `IsSuccess` | if call is success or not | | `IsStatic` | if call is within a static call | | `IsCreate` | if call is a contract creation | <!-- IsInternal seems to be redundant if only few things need --> <!-- | `IsInternal` | if call is a internal call | --> ### `CallState` | Name | Description | | ------------------- | ------------------------------- | | `ProgramCounter` | program counter | | `StackPointer` | stack pointer | | `MemeorySize` | memory size | | `GasLeft` | gas left | | `StateWriteCounter` | world state write counter | | `CalleeId` | last callee's unique identifier | | `ReturndataOffset` | offset of returndata | | `ReturndataSize` | size of returndata | ## Call Lifecycle EVM circuit tracks a flag `is_initialization` in each slot to specify if it's the beginning of a call. When in the beginning of circuit, any `*CALL` happens, and any transaction ends, it sets next slot's `is_initialization` to `1`, and also set the `id` of the next call. Also EVM circuit tracks call's state like `program_counter` or `stack_pointer` from slot to slot. We can definitely maintain these state in state circuit, but it blows up the size of bus mapping because these state almost change every slot. Only when we encounter a `*CALL`, we write call's state into state circuit to memorize for further resumption. The pseudo code looks like: ```python # THINK: is_initialization could be call_id != prev.call_id? is_executing = 1 - is_initialization is_root = depth == 0 is_internal = not is_root if is_executing and op in (CALL, STATICCALL, ...): # handle op logic... # memorize caller program counter bus_mapping_lookup(gc++, call_id, CallState, ProgramCounter, pc, Write) # memorize caller stack pointer bus_mapping_lookup(gc++, call_id, CallState, StackPointer, sp, Write) # memorize other stuff... # goto next internal call assert next.is_initialization is True assert next.tx_id == tx_id call_lookup(next.call_id, GlobalCounterBegin, gc++) # lookup other call information decided by caller if is_executing and op in (STOP, REVERT): # or any other kinds of error # handle op logic... if is_internal: # resume caller's program counter bus_mapping_lookup(gc++, caller_id, CallState, ProgramCounter, next.pc, Read) # resume caller's stack pointer bus_mapping_lookup(gc++, caller_id, CallState, StackPointer, next.sp, Read) # resume caller's other stuff... # set returndata offset and size for caller bus_mapping_lookup(gc++, caller_id, CallState, CalleeId, call_id, Write) bus_mapping_lookup(gc++, caller_id, CallState, ReturndataOffset, returndata_offset, Write) bus_mapping_lookup(gc++, caller_id, CallState, ReturndataSize, returndata_size, Write) else: # goto next EOA call (transaction) assert next.is_initialization is True assert next.tx_id == tx_id + 1 call_lookup(next.call_id, GlobalCounterBegin, gc++) if is_initialization: if is_root: # verify transaction nonce, balance, signature, etc... # initialize call's state for next slot # if receiver has code and not suicided: dive into the call # else: resume to caller or goto next transaction ``` The rationale to track some call's state slot by slot instead of in state circuit is due to their high frequency of update. To save volume of bus mapping, we track state like `program_counter` and `stack_pointer` which almost change every slot. Then there are other state we only read them out or update them from bus mapping in specific op. For example, we only read `CalleeId`, `ReturndataOffset`, and `ReturndataSize` when `RETURNDATACOPY`, and we only increase `StateWriteCounter` when `SSTORE` or `CALL` with value.

    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