Samuel Laferriere
    • 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 No publishing access yet

      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.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      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 No publishing access yet

    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.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    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
    # A Walk Down seismic-trie We currently maintain a fork [seismic-trie](https://github.com/SeismicSystems/seismic-trie) of alloy-trie. This document has goal to propose and evaluate getting rid of this fork. Seismic privacy feature is powered by the `FlaggedStorage = (value, is_private): (U256, bool)` replacement for U256 for contract storage slots, where is_private slots are considered shielded and can't be read by `eth_getStorageAt` or `eth_getProof` rpcs, among other things. The big question is how/where we should encode this `is_private` bool in the trie and database layout. ## TODO Decisions we still need to: 1. settle on an rlp encoding implementation for FlaggedStorage 2. decide whether to put storage_root helpers in seismic-alloy or keep a minimal alloy-trie fork ## Current Fork Implementation FlaggedStorage is serialized/encoded differently in the trie and database. ### Trie Currently the value is tucked/hidden into the LeafNode's encodedPath (but see bug section below): - Path header uses the (starting from the left) second bit of the first nibble for is_private: 0x0 for public, 0x4 for private ![image](https://hackmd.io/_uploads/HJVi27IY-l.png) Thus instead of being part of the value, `is_private` is actually treated as trie leaf metadata!! And this is basically the only reason afaiu why we need this fork. This might have been done originally by someone thinking that this trick was saving 1 byte of storage, but given that the LeafNode is actually not stored in the database, and is really only a temporary in-memory data structure used to compute/update the MPT root, this is actually not the case. This structure is really only created to be keccak256 hashed, and keccak256 ingests 136 bytes at a time, which is way larger than the largest possible LeafNode (header is few bytes, max nibbles path is 32 bytes, value is 32 bytes); so adding an extra is_private byte at the end would not affect MPT updating speed. One nice feature of this encoding however is that public LeafNodes have the exact same memory layout as upstream ethereum LeafNodes (which simply don't use that 2nd bit). This means we can run the EELS tests without any modifications and get the same state roots. However.. this is currently not true because there's a bug in our code. #### Inconsistent Encoding Bug I just realized that we encode `is_private` differently in different codepaths. Our current state root thus doesn't match with that of ethereum [even when we only store public slots](https://github.com/SeismicSystems/seismic-reth/blob/seismic/crates/trie/db/src/state.rs#L315-L319) in certain tests! Turns out we encode differently in 2 separate code paths: | Code path | Encodes | Value bytes | Root | | ------------------------------------------------------- | -------------------------- | ------------------------ | ------------------------------ | | [seismic-trie::storage_root()](https://github.com/SeismicSystems/seismic-trie/blob/420123bdf0561894c7d8d202ae919283327234ed/src/root.rs#L101) (used in genesis, tests) | `&value.value` (bare U256) | No extra byte | Ethereum-compatible for public | | [seismic-reth::trie.rs](https://github.com/SeismicSystems/seismic-reth/blob/757b2485300a82361e9adee0b5535dcf8d305677/crates/trie/trie/src/trie.rs#L678)thats (used in block processing) | `&value` (FlaggedStorage) | Always appends bool byte | Never Ethereum-compatible | ``` ┌────────────────────────────────────────────┬────────────────────────────────────────┐ │ Encodes bare U256 │ Encodes FlaggedStorage │ ├────────────────────────────────────────────┼────────────────────────────────────────┤ │ seismic-trie::storage_root() (root.rs:101) │ StorageRoot::calculate() (trie.rs:669) │ ├────────────────────────────────────────────┼────────────────────────────────────────┤ │ sparse test (state.rs:1153) │ sparse engine (sparse_trie.rs:194) │ ├────────────────────────────────────────────┼────────────────────────────────────────┤ │ │ proof verify (proofs.rs:776) │ ├────────────────────────────────────────────┼────────────────────────────────────────┤ │ │ test_utils (test_utils.rs:28,104,128) │ └────────────────────────────────────────────┴────────────────────────────────────────┘ ``` ### Database The database encodes `is_private` completely differently and actually puts it in front of the U256 value. ![image](https://hackmd.io/_uploads/SJ0b1JUF-g.png) We could and probably should change this encoding to also use the same encoding that I am proposing below. Probably makes sense to encode both the same actually. ## Proposal Implementation: - https://github.com/SeismicSystems/seismic-reth/pull/328 - https://github.com/SeismicSystems/seismic-alloy/pull/80 - https://github.com/SeismicSystems/seismic-alloy-core/pull/58 My main proposal is twofold: 1. Always encode is_private as part of the value. That way we don't need our seismic-trie fork and can just use stock alloy-trie 2. Proposing the below encoding which retains the benefits of having our public-only state roots match those of ethereum Taking a step back for a second, there are 3 places where `is_private` could be encoded: - key: this would allow storing both public/private values in the same trie at the same address - CSTORE/CLOAD and SSTORE/SLOAD would work on completely separate address spaces, which would however be merkleized together as a single trie. - I personally like this approach a lot because it makes the evm semantics cleaner imo, but its probably too late to make this big change - leaf metadata: what we currently have - value: by doing this we can use stock alloy-trie without forking it ### Custom RLP Encoding for FlaggedStorage If we do this, we have leeway to "rlp" encode FlaggedStorage however we wish. In order to retain compatibility with ethereum's state root, I'm proposing adding a byte with value 1 iif the slot is private. When the slot is public, we only store the U256 encoded value. Note that we could change this to only add a byte for public slots instead if we expect there to be more private than public slots. ```rust! impl Encodable for FlaggedStorage { fn length(&self) -> usize { self.value.length() + if self.is_private { 1 } else { 0 } } fn encode(&self, out: &mut dyn BufMut) { self.value.encode(out); if self.is_private { true.encode(out); } } } ``` This encoding deviates from the RLP schema, where FlaggedStorage would have to be encoded as a list [U256, bool]. The custom encoding that we have works because RLP encoding of FlaggedStorage is only ever done for MPT leaf nodes, so there is never anything encoded after FlaggedStorage (hence the is_private bool can't be confused with another following value). If we were ever to use FlaggedStorage as part of a list that neds to be RLP encoded, then we'd probably want to just derive the Encodable macro. Only downside for this is that we lose ethereum state root compatibility... Claude analysis: ``` FlaggedStorage is never RLP-encoded as part of a list. Every usage is as a standalone trie leaf value: 1. seismic-trie storage_root() — encodes as leaf value passed to hb.add_leaf() 2. seismic-alloy storage_root() — same, encode_fixed_size(&value) as leaf value 3. seismic-foundry trie_storage() — same pattern 4. Proof verification (proofs.rs:771) — encode_fixed_size(&FlaggedStorage::new(...)) compared against a leaf 5. Proof decoding (proofs.rs:488) — FlaggedStorage::decode(&mut &leaf.value[..]) from a leaf No struct containing FlaggedStorage derives RlpEncodable. No Vec<FlaggedStorage> is ever RLP-encoded. No encode_list ever touches it. ``` ### Pros/Cons - pros: - no fork, which means: - less burden for developers - less bug-prone. I noticed that we have revm [specific semantics](https://github.com/SeismicSystems/seismic-revm?tab=readme-ov-file#flagged-storage) for how cstore/cload/sstore/sload interact. This same semantics is not enforced by our trie fork afaiu however, so a bug could creap in and have a public value overwrite a private one... as one example. The current ongoing audits have also found some bugs so far. - more extensible: if we ever decide to extend our schema and store more metadata per leaf (for eg owner address), the metadata fork approach would require a lot of changes to the trie, whereas storing in value would be a trivial small change of rlp encoding in reth - exact same state root as ethereum when state trie only contains public values, so we can run EELS tests (however fork also has this benefit if we fix the bug) - cons (aka pros of fork): - possibly harder to debug, as for example our trie fork currently has specific errors when is_private value doesn't match expected. I feel like we can likely get the same benefits by having the errors caught when reth rlp encodes FlaggedStorage values and queries the API though. Might be a cleaner separation of concerns too. - not a concern in either: - actual number of bytes used. As explained above, the actual amount of bytes taken by the LeafNode is not a concern because it anyways gets padded to 136 bytes before getting keccak256 encoded. ## Benchmark If my reasoning above is correct, then I wouldn't expect any difference in MPT update computation since keccak256 pads its input to 136 bytes anyways. For completeness ran a single sparse serial trie benchmarks, since that is the one used to update storage tries: - HashBuilder (build tree from leaves from scratch) - used for txs + receipts roots - Sparse Parallel Trie (parallelizes across 16 top-level branches) - used for account tries - Sparse Serial Trie - used for each accounts' storage trie - (non-sparsse) Parallel Trie - fallback in case sparse trie fails during validation (start back from scratch as fast as possible) Ran this on my macbook pro m1 while listening to spotify, so take results with a grain of salt... but TLDR is there doesn't seem to be any performance difference, as expected. ### no fork result ```bash! samlaf ~/devel/seismic-workspace/seismic-reth/crates/trie/sparse [feat--no-trie-fork-wip] $ cargo bench -p reth-trie-sparse --bench root -- \ "repeated/sparse trie/init size 10000 \| update size 1000 \| num updates 10" \ --save-baseline no-fork --sample-size 100 --warm-up-time 5 Benchmarking calculate root from leaves repeated/sparse trie/init size 10000 | update size 1000 | num updates 10: Collecting 20 samples in estimated 5.2558 calculate root from leaves repeated/sparse trie/init size 10000 | update size 1000 | num updates 10 time: [16.307 ms 16.569 ms 16.825 ms] ``` and a second benchmark with larger db size, and pinning alloy-trie to =0.9.1 (instead of 0.9.4 which it defaults to atm) ``` samlaf ~/devel/seismic-workspace/seismic-reth/crates/trie/sparse [feat--no-trie-fork] $ cargo bench -p reth-trie-sparse --bench root -- \ "repeated/sparse trie/init size 1000000 \| update size 10000 \| num updates 10" \ --baseline no-fork --sample-size 100 --warm-up-time 5 Benchmarking calculate root from leaves repeated/sparse trie/init size 1000000 | update size 10000 | n calculate root from leaves repeated/sparse trie/init size 1000000 | update size 10000 | num updates ... #4 time: [555.96 ms 565.18 ms 577.79 ms] Found 12 outliers among 100 measurements (12.00%) 11 (11.00%) high mild 1 (1.00%) high severe ``` ### seismic branch (fork) result ```bash! samlaf ~/devel/seismic-workspace/seismic-reth/crates/trie/sparse [feat--no-trie-fork-wip] $ cargo bench -p reth-trie-sparse --bench root -- \ "repeated/sparse trie/init size 10000 \| update size 1000 \| num updates 10" \ --baseline no-fork --sample-size 100 --warm-up-time 5 Benchmarking calculate root from leaves repeated/sparse trie/init size 10000 | update size 1000 | num updates 10: Collecting 20 samples in estimated 5.0522 calculate root from leaves repeated/sparse trie/init size 10000 | update size 1000 | num updates 10 time: [15.867 ms 15.968 ms 16.092 ms] change: [-3.9681% -2.1122% +0.0297%] (p = 0.06 > 0.05) No change in performance detected. Found 2 outliers among 20 measurements (10.00%) 2 (10.00%) high severe ``` ``` samlaf ~/devel/seismic-workspace/seismic-reth/crates/trie/sparse [seismic] $ cargo bench -p reth-trie-sparse --bench root -- \ "repeated/sparse trie/init size 1000000 \| update size 10000 \| num updates 10" \ --baseline no-fork --sample-size 100 --warm-up-time 5 Benchmarking calculate root from leaves repeated/sparse trie/init size 1000000 | update size 10000 | num updates ... #4: Collecting 100 samples in estimate calculate root from leaves repeated/sparse trie/init size 1000000 | update size 10000 | num updates ... #4 time: [553.44 ms 559.25 ms 566.07 ms] change: [-6.9701% -4.7358% -2.6339%] (p = 0.00 < 0.05) Performance has improved. Found 10 outliers among 100 measurements (10.00%) 4 (4.00%) high mild 6 (6.00%) high severe ``` # Claude audit For completeness, here's an "audit" done by Claude of my current proposal's implementation. It raised a few good issues, but all of them are solvable, and some probably apply to our current implementation anyways. 1. The FlaggedStorage::decode() buffer-emptiness heuristic is fragile ```rust! fn decode(buf: &mut &[u8]) -> RlpResult<Self> { let value = U256::decode(buf)?; let is_private = !buf.is_empty(); // ← any trailing byte = private Ok(Self { value, is_private }) } ``` This only works when the caller passes exactly the leaf value bytes with no trailing data. If FlaggedStorage::decode() is ever called on a buffer that contains more data after the value (e.g., concatenated RLP items), it will falsely report is_private = true. Currently this is safe because: - proofs.rs slices exactly: FlaggedStorage::decode(&mut &leaf.value[..]) - encode_fixed_size produces exact-length output Risk: If anyone adds a new decode site that passes a larger buffer, it silently misreads privacy. There's no validation that the trailing byte is actually 0x01. 2. No compile-time enforcement that FlaggedStorage encoding is used The biggest risk: any code path that calls add_leaf(nibbles, encode_fixed_size(&value.value)) (encoding just the U256, not the FlaggedStorage) will silently produce the wrong root. There's no type-level distinction between "already-encoded FlaggedStorage bytes" and "plain U256 bytes" — both are Vec<u8>. In the old design, add_leaf took an explicit is_private: bool parameter, so forgetting it was a compile error. Now the privacy info must be baked into the bytes before calling add_leaf, and the compiler can't check that. Places to audit: Every call to add_leaf() / update_leaf() / storage_root() that touches storage values. Currently these are correct, but any future code adding trie leaves must remember to encode FlaggedStorage, not bare U256. 3. Proof verification re-encodes from split fields ```rust! // proofs.rs — StorageProof::verify() let expected = Some(encode_fixed_size(&FlaggedStorage::new(self.value, self.is_private)).to_vec()); verify_proof(root, self.nibbles, expected, &self.proof) ``` The StorageProof struct carries value: U256 and is_private: bool as separate fields, then re-combines them for verification. If any code path constructs a StorageProof with the wrong is_private value, verification will fail with an opaque error. The old design had is_private extracted directly from the trie leaf node, so it was always consistent. Now the extraction happens in storage_proof() via FlaggedStorage::decode(), which should be correct — but there's a gap between extraction and verification where the fields could be modified independently. Recommendations 1. Add a wrapper type like EncodedFlaggedStorage(Vec<u8>) for pre-encoded values to get compile-time enforcement that the right encoding was used before passing to add_leaf() 2. Validate the trailing byte in FlaggedStorage::decode() — assert it's 0x01 if present, not just "non-empty" 3. Add integration tests that specifically verify: encode FlaggedStorage → insert into trie → compute root → generate proof → decode from proof → compare privacy flag. This replaces the old seismic-trie property tests that explicitly tested privacy roundtrips

    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
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    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