Skip to content

Conversation

Copy link

Copilot AI commented Dec 8, 2025

Implementation Plan for ProgressiveMerkleHasher

  • Create a new file progressive_merkle_hasher.rs with the ProgressiveMerkleHasher struct
  • Implement the core ProgressiveMerkleHasher struct that follows similar API design to MerkleHasher
  • Implement the with_leaves constructor
  • Implement the write method to add bytes/chunks to the hasher
  • Implement the finish method to compute the final root using progressive merkleization semantics
  • Nest MerkleHasher instances for handling binary tree sub-portions (via merkle_root)
  • Add the progressive_merkle_hasher module to lib.rs
  • Export ProgressiveMerkleHasher from lib.rs
  • Add unit tests for ProgressiveMerkleHasher to validate correctness
  • Add more comprehensive tests including the example from the problem statement
  • Run tests to ensure implementation is correct
  • Export ProgressiveMerkleHasherError to avoid forcing users to import separately
  • Request code review
  • Run CodeQL security checker - no issues found
  • Refactor for efficiency - hash complete subtrees as chunks stream in
  • Address code review feedback
  • Refactor to use MerkleHasher internally instead of merkle_root function

Final Implementation

The efficient ProgressiveMerkleHasher implementation now:

  • Uses MerkleHasher internally: Each level uses a MerkleHasher instance for computing binary tree roots
  • Hashes as it goes: Completes and stores merkle roots when levels fill (1, 4, 16, 64... leaves)
  • Minimal memory: Stores only completed subtree roots (O(log n) space)
  • Consistent API: Matches MerkleHasher with with_leaves(), write(), finish() methods
  • Well-tested: 12 unit tests covering all edge cases and consistency checks

All 59 tests pass across the package.

Original prompt

Implement ProgressiveMerkleHasher, following a similar API design to MerkleHasher, and reusing code by nesting a MerkleHasher where appropriate (for hashing the sub binary trees).

The aim is to implement the semantics of merkleize_progressive, which is defined by the spec as:

merkleize_progressive(chunks, num_leaves=1): Given ordered BYTES_PER_CHUNK-byte chunks:
The merkleization depends on the number of input chunks and is defined recursively:
If len(chunks) == 0: the root is a zero value, Bytes32().
Otherwise: compute the root using hash(a, b)
a: Recursively merkleize chunks beyond num_leaves using merkleize_progressive(chunks[num_leaves:], num_leaves * 4).
b: Merkleize the first up to num_leaves chunks as a binary tree using merkleize(chunks[:num_leaves], num_leaves).

Visually, progressive merkle trees have this structure:

        root
         /\
        /  \
       /\   1: chunks[0 ..< 1]
      /  \
     /\   4: chunks[1 ..< 5]
    /  \
   /\  16: chunks[5 ..< 21]
  /  \
 0   64: chunks[21 ..< 85]

You can refer to EIP-7916 for further context: https://eips.ethereum.org/EIPS/eip-7916.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 4 commits December 8, 2025 02:55
Co-authored-by: michaelsproul <4452260+michaelsproul@users.noreply.github.com>
Co-authored-by: michaelsproul <4452260+michaelsproul@users.noreply.github.com>
Co-authored-by: michaelsproul <4452260+michaelsproul@users.noreply.github.com>
Co-authored-by: michaelsproul <4452260+michaelsproul@users.noreply.github.com>
Copilot AI changed the title [WIP] Implement ProgressiveMerkleHasher with recursive merkleization Implement ProgressiveMerkleHasher for EIP-7916 Dec 8, 2025
Copilot AI requested a review from michaelsproul December 8, 2025 03:06
@codecov
Copy link

codecov bot commented Dec 8, 2025

Codecov Report

❌ Patch coverage is 80.00000% with 8 lines in your changes missing coverage. Please review.
✅ Project coverage is 88.02%. Comparing base (6f3b323) to head (3e30512).

Files with missing lines Patch % Lines
tree_hash/src/progressive_merkle_hasher.rs 80.00% 8 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main      #42      +/-   ##
==========================================
+ Coverage   87.75%   88.02%   +0.26%     
==========================================
  Files           6        7       +1     
  Lines         294      334      +40     
==========================================
+ Hits          258      294      +36     
- Misses         36       40       +4     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@michaelsproul
Copy link
Member

@copilot This implementation is inefficient, I want the ProgressiveMerkleHasher to hold the minimum number of chunks possible by performing hashing as new chunks are streamed in.

… stream in

Co-authored-by: michaelsproul <4452260+michaelsproul@users.noreply.github.com>
Copy link
Author

Copilot AI commented Dec 8, 2025

@copilot This implementation is inefficient, I want the ProgressiveMerkleHasher to hold the minimum number of chunks possible by performing hashing as new chunks are streamed in.

Refactored to hash chunks as they stream in (commit 0746652). The hasher now:

  • Computes merkle roots for complete levels immediately (1, 4, 16, 64... leaves)
  • Stores only completed subtree roots instead of all chunks
  • Uses O(log n) space instead of O(n)
  • Builds final tree by combining stored roots

This follows the same efficient folding pattern as MerkleHasher.

…er method

Co-authored-by: michaelsproul <4452260+michaelsproul@users.noreply.github.com>
@michaelsproul
Copy link
Member

@copilot This is getting closer to being correct. Can you refactor the ProgressiveMerkleHasher to use a regular MerkleHasher internally for the hashes of the binary tree?

…inary tree hashing

Co-authored-by: michaelsproul <4452260+michaelsproul@users.noreply.github.com>
Copy link
Author

Copilot AI commented Dec 9, 2025

@copilot This is getting closer to being correct. Can you refactor the ProgressiveMerkleHasher to use a regular MerkleHasher internally for the hashes of the binary tree?

Refactored to use MerkleHasher internally (commit 733344a). Changes:

  • Replaced merkle_root() calls with a MerkleHasher instance (current_hasher)
  • Each level now uses its own MerkleHasher for computing binary tree roots
  • Used std::mem::replace to handle ownership when finishing hashers
  • All tests pass with the new implementation

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants