Skip to content

Conversation

@Asim-Shaik
Copy link

Allow initializing MerkleTree with caller-provided precomputed zero hashes
instead of computing them during construction.

closes #20

…ashes

Signed-off-by: Asim Shaik <asimshaik564@gmail.com>

/// Initialize Merkle tree with caller-provided precomputed zero hashes
/// precomputed[i] must be the zero hash at level i (0 = leaf, DEPTH-1 = top)
pub fn with_precomputed_zeros(hasher: H, store: S, precomputed: [Node; DEPTH]) -> Self {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for the contribution, but mind elaborating on the use case of this?
the precomputed zeros depend on the DEPTH and hash function, so not sure why someone would like to call this function. the zeros are already initialized in the constructor. what's your use case for this?

Copy link
Author

@Asim-Shaik Asim-Shaik Nov 6, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to ensure deterministic Merkle roots across different environments like Rust, js, circom, and on-chain programs like solana. In ZK and blockchain setups, even minor differences in poseidon encoding or endianness can lead to mismatched roots, so providing precomputed zero hashes guarantees cross-environment consistency.

it is a very specific use case for a rather general purpose crate XD

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still dont follow. The merkle roots this crate produces are deterministic and compatible with any other merkle tree implementation. As long as you use the same:

  • hash function
  • depth

If you add a with_precomputed_zeros, the precomputed shall match this https://github.com/bilinearlabs/rs-merkle-tree/blob/main/src/tree.rs#L72-L75 to be consistent. So why adding with_precomputed_zeros in the first place?

Can you elaborate on your use case? Whatever it is, I'm not sure this is the way to fix it.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I use a hashing script to precompute the canonical zero hashes namespaced to a specific domain by using strings like "domain-occupied". I’m using this as a way to keep my different environments rust , solana, and circom circuits perfectly deterministic and in sync.

is this a bad way of doing it?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

still dont follow. can you put an example on how you would calculate your zero hashes?

how do you include your "domain-occupied" in the hash? and well, if you include that to hash zeros, dont you have to include it as well to hash the tree?

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.

add support for precomputed zero hashes

2 participants