Closed billbaobab closed 11 months ago
For the sake of completeness, this is how I'm creating & rendering the tree:
// (1)
const values = [
["0x1111111111111111111111111111111111111111", "5000000000000000000"],
["0x2222222222222222222222222222222222222222", "2500000000000000000"],
["0x3333333333333333333333333333333333333333", "5000000000000000000"],
["0x4444444444444444444444444444444444444444", "2500000000000000000"],
["0x5555555555555555555555555555555555555555", "5000000000000000000"],
["0x6666666666666666666666666666666666666666", "2500000000000000000"],
["0x7777777777777777777777777777777777777777", "5000000000000000000"],
["0x8888888888888888888888888888888888888888", "2500000000000000000"],
];
// (2)
const tree = StandardMerkleTree.of(values, ["address", "uint256"]);
// (3)
console.log("Merkle Root:", tree.render());
When you hash an inner node you need to sort the two children:
This is probably the reason for what you're observing.
Make sure to look at core.ts
if you are reimplementing the algorithm.
Hi,
Background: I'm currently working on a Merkle Tree construction using a zk domain specific language (Noir). I want it to be compatible with OZ's contracts and JS tooling. For that reason I'm trying to re-implement the OZ implementation. What I'm essentially doing is constructing a Merkle Tree using the JS lib and trying to get to the same outcome in Noir.
During that exercise I got quite confused about the result of the
render()
method. This is the output:However when re-implementing that tree in Noir I noticed that in order to get to the same root value, the tree actually has to look like this (the numbers in my visualization correspond to the "indices" as returned by the
render()
method, so for instance14
corresponds to14) 0730334eb8f8c63981dd1c15ca2dcd670440d3cf0cbbe33687ccb38d499b06bf
):I understand from this issue https://github.com/OpenZeppelin/merkle-tree/issues/26 that it is expected that leafs are stored backwards. So far so good, but what I don't understand is the irregularity in the sorting as highlighted in red in my visualization.
Can you help me understand this? Or is it a bug?
Thanks!