Han
    • 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 New
    • Engagement control
    • Make a copy
    • 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 Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    --- tags: zkevm --- <style> table { white-space: nowrap; } table ul { padding-left: 0.75em !important; margin-bottom: 0 !important; } </style> # ZKEVM - Random Thoughts ## EVM Caveat ### Memory Expansion There are several opcodes that expand memory: | `opcode` | `off1` | `len1` | `off2` | `len2` | Pure Memory Gas Cost | | ------------------- | ------------- | ------------- | ------------- | ------------- | -------------------- | | `32 SHA3` | `stack[sp]` | `stack[sp+1]` | | | | | `55 CALLDATACOPY` | `stack[sp]` | `stack[sp+2]` | | | | | `57 CODECOPY` | `stack[sp]` | `stack[sp+2]` | | | | | `60 EXTCODECOPY` | `stack[sp+1]` | `stack[sp+3]` | | | | | `62 RETURNDATACOPY` | `stack[sp]` | `stack[sp+2]` | | | | | `81 MLOAD` | `stack[sp]` | `32` | | | Yes | | `82 MSTORE` | `stack[sp]` | `32` | | | Yes | | `83 MSTORE8` | `stack[sp]` | `1` | | | Yes | | `160 LOG0` | `stack[sp]` | `stack[sp+1]` | | | | | `161 LOG1` | `stack[sp]` | `stack[sp+1]` | | | | | `162 LOG2` | `stack[sp]` | `stack[sp+1]` | | | | | `163 LOG3` | `stack[sp]` | `stack[sp+1]` | | | | | `164 LOG4` | `stack[sp]` | `stack[sp+1]` | | | | | `240 CREATE` | `stack[sp+1]` | `stack[sp+2]` | | | Yes | | `241 CALL` | `stack[sp+3]` | `stack[sp+4]` | `stack[sp+5]` | `stack[sp+6]` | | | `242 CALLCODE` | `stack[sp+3]` | `stack[sp+4]` | `stack[sp+5]` | `stack[sp+6]` | | | `243 RETURN` | `stack[sp]` | `stack[sp+1]` | | | Yes | | `244 DELEGATECALL` | `stack[sp+2]` | `stack[sp+3]` | `stack[sp+4]` | `stack[sp+5]` | | | `245 CREATE2` | `stack[sp+1]` | `stack[sp+2]` | | | | | `250 STATICCALL` | `stack[sp+2]` | `stack[sp+3]` | `stack[sp+4]` | `stack[sp+5]` | | | `253 REVERT` | `stack[sp]` | `stack[sp+1]` | | | Yes | The psuedo code of memory expansion: ```python from math import ceil MEMORY_SIZE_MAX = 32 * (2**32 - 1) # 0x1FFFFFFFE0 # returns (new_memory_size, is_overflow) def expand_memory(memory_size: int, off1: int, len1: int, off2: int, len2: int) -> int, bool: def m(off: int, len: int) -> int, bool: if len is None or len == 0: return 0, False new_memory_size = 32 * ceil(off + len / 32) if new_memory_size > MEMORY_SIZE_MAX: return 0, True return new_memory_size, False new_memory_size1, is_overflow1 = m(off1, len1) new_memory_size2, is_overflow2 = m(off2, len2) if is_overflow1 or is_overflow2: return 0, True return max(memory_size, new_memory_size1, new_memory_size2), False ``` Ideally we can verify the OOG caused only by memory expansion in a single branch, by lookup the offset and length in stack position with the opcode, and verify the new memory size indeed exceeds the max value. ### Access List (EIP2929) To support EIP2929, we add 2 extra revertable targets `tx_access_list_account` and `tx_access_list_storage_slot` in State circuit to maintain, their value is constraint to only be `0` or `1`, and lazily initialied to be `0`. Then in EVM circuit, we will always write it to `1`, and see the different between current and previous value to see if it's a warm or cold access. ### Precompiles | address | name | execution result | gas cost | | ------- | ------------ | ---------------------------------------------------------------------------------------------------------------------------------------- | ------------------------------- | | `0x01` | ECRecover | <ul><li>`ECRECOVER`</li><li>`ERROR_OOG_CONSTANT`</li></ul> | `3000` | | `0x02` | Sha256 | <ul><li>`SHA256`</li><li>`ERROR_OOG_SHA256`</li></ul> | `60 + 12*ceil(len(input)/32)` | | `0x03` | Ripemd160 | <ul><li>`RIPEMD160`</li><li>`ERROR_OOG_RIPEMD160`</li></ul> | `600 + 120*ceil(len(input)/32)` | | `0x04` | DataCopy | <ul><li>`DATA_COPY`</li><li>`ERROR_OOG_DATA_COPY`</li></ul> | `15 + 3*ceil(len(input)/32)` | | `0x05` | BigModExp | <ul><li>`BIG_MOD_EXP`</li><li>`ERROR_OOG_BIG_MOD_EXP`</li></ul> | see [EIP2565](https://eips.ethereum.org/EIPS/eip-2565#specification) | | `0x06` | BN254Add | <ul><li>`BN254_ADD`</li><li>`ERROR_OOG_CONSTANT`</li><li>`ERROR_INVALID_INPUT_BN254_ADD (Invalid G1)`</li></ul> | `150` | | `0x07` | BN254Mul | <ul><li>`BN254_MUL`</li><li>`ERROR_OOG_CONSTANT`</li><li>`ERROR_INVALID_INPUT_BN254_MUL (Invalid G1)`</li></ul> | `6000` | | `0x08` | BN254Pairing | <ul><li>`BN254_PAIRING`</li><li>`ERROR_OOG_BN254_PAIRING`</li><li>`ERROR_INVALID_INPUT_BN254_PAIRING (Invalid length, G1, G2)`</li></ul> | `45000 + 34000*(len(input)/192)` | | `0x09` | Blake2F | <ul><li>`BLAKE2F`</li><li>`ERROR_OOG_BLAKE2F`</li><li>`ERROR_INVALID_INPUT_BLAKE2F (Invalid length)`</li></ul> | `be_to_int(input[:4])` | Note: If we are reading returndata directly from last callee's memory, we need to be careful when handling precompile `0x04` DataCopy since the last callee would be caller itself. We should explicitly copy calldata to precompile's memory and treat it as a call. ## Possible Upgrade of EVM ### `SELFDESTRUCT` Neutralization > The `SELFDESTRUCT` opcode is renamed to `SENDALL`, and now only immediately moves all ETH in the account to the target; it no longer destroys code or storage or alters the nonce > [name=quotes from <a href="https://notes.ethereum.org/-fJSOrnYQl-mqoWKpaTIsQ#Miscellaneous">Statelessness gas cost changes EIP draft #Miscellaneous</a>] If so, we can get rid of a bunch of consideration of the destroy of code and storage. #### Reference - [Statelessness gas cost changes EIP draft](https://notes.ethereum.org/-fJSOrnYQl-mqoWKpaTIsQ) ### EOF - EVM Object Format After [`EIP3670` - EOF - Code Validation](https://eips.ethereum.org/EIPS/eip-3670): - Create transaction, `CREATE*` becomes more complicated, we need to verify creation code and also returned runtime code. - Opcode lookup becomes easier since no any implict behavior (implict `PUSH*` data padding, implicit `STOP` in the end). After [`EIP4200` - Static relative jumps](https://eips.ethereum.org/EIPS/eip-4200): - `JUMP*` becomes easier since no any invalid jump at runtime. #### Reference - [Everything about the EVM Object Format (EOF)](https://notes.ethereum.org/@ipsilon/evm-object-format-overview) - [The future of EVM - EVM Object Format](https://axic.github.io/notes/liscon_eof/#/) ### Statelessness - Merkle (Hexary) Patricia Trie becomes Verkle Trie - Trie-access-aware gas cost calculation - Chunkified contract bytecode #### Reference - [Ethereum statelessness roadmap](https://notes.ethereum.org/Yn_mwNa2SeeQHnKsRgekKg) ### `EIP4488` - Transaction calldata gas cost reduction with total calldata limit [`EIP4488`](https://github.com/ethereum/EIPs/pull/4488) specification: | Parameter | Value | | ----------------------------- | ----------- | | `NEW_CALLDATA_GAS_COST` | `3` | | `BASE_MAX_CALLDATA_PER_BLOCK` | `1,048,576` | | `CALLDATA_PER_TX_STIPEND` | `300` | Reduce the gas cost of transaction calldata to `NEW_CALLDATA_GAS_COST` per byte, regardless of whether the byte is zero or nonzero. Add a rule that a block is only valid if `sum(len(tx.calldata) for tx in block.txs) <= BASE_MAX_CALLDATA_PER_BLOCK + len(block.txs) * CALLDATA_PER_TX_STIPEND`. ## Misc - [Sparse Representation Trick on Keccak](/4gOBP_loQ7GXXWsaCqcovA) - [Multi-Limbs Multiplication](/HL0QhGUeQoSgIBt2el6fHA) - [ZKEVM - Call and Call State](/GT5-oa__SbedW1W2o4YJBw) - [ZKEVM - EVM Circuit Layout and Branching](/QmmGsuVpRUmPDT0Pvr8VIw) - [ZKEVM - EVM Circuit Design Rationale](/N82vVLWjSp6fZewlOTj1WQ) - [ZKEVM - Consideration on Transaction](/MmXpxgW2TbiMa2EUDeRbaQ) - [ZKEVM - RLP Encoding](/yTirDRdATVmB4wjECHMebg) - [ZKEVM - State Circuit Extension - `StateDB`](/G48BKqdPScyoFDHPNzgOYQ)

    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