Help needed regarding merkle implementation in noir

let is_member = std::merkle::check_membership(root, leaf, index, hashpath);
```|

In the above , what is index? In the examples, we are using index = 0 . Why is that the case?

is index used to denote the position of the leaf, left or right?
50 Likes

So the index is the position of the leaf in the tree. In a binary merkle tree it is broken down into a bit array where each bit is used to determine whether the current leaf we are hashing is on the left or right. In our stdlib you can see this in more detail: noir/noir_stdlib/src/merkle.nr at bd8379e2e847ab5196ff01e23add5a277fc6eb1c ยท noir-lang/noir ยท GitHub. We are going to be deprecating the check_membership method in favor of the method linked in the next version of Noir.

19 Likes

Just to supplement the other answer, I think the โ€œposition of the leaf in the treeโ€ means that leftmost โ€œleaf indexโ€ is 0 and the rightmost is ( 2^height)-1 (if height does not include root).

                                  โ”โ”โ”โ”โ”“
root:               โ•ญโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”จ r โ” โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ•ฎ
                    โ”‚             โ”—โ”โ”โ”โ”›             โ”‚
                  โ”โ”โ”ทโ”โ”“                           โ”โ”โ”ทโ”โ”“
lyr2:       โ•ญโ”€โ”€โ”€โ”€โ”€โ”จ x โ” โ”€โ”€โ”€โ”€โ”€โ•ฎ               โ•ญโ”€โ”€โ”€โ”€โ”€โ”จ x โ” โ”€โ”€โ”€โ”€โ”€โ•ฎ
            โ”‚     โ”—โ”โ”โ”โ”›     โ”‚               โ”‚     โ”—โ”โ”โ”โ”›     โ”‚
          โ”โ”โ”ทโ”โ”“           โ”โ”โ”ทโ”โ”“           โ”โ”โ”ทโ”โ”“           โ”โ”โ”ทโ”โ”“
lyr1:   โ•ญโ”€โ”จ x โ” โ”€โ•ฎ       โ•ญโ”€โ”จ x โ” โ”€โ•ฎ       โ•ญโ”€โ”จ x โ” โ”€โ•ฎ       โ•ญโ”€โ”จ x โ” โ”€โ•ฎ
        โ”‚ โ”—โ”โ”โ”โ”› โ”‚       โ”‚ โ”—โ”โ”โ”โ”› โ”‚       โ”‚ โ”—โ”โ”โ”โ”› โ”‚       โ”‚ โ”—โ”โ”โ”โ”› โ”‚
      โ”โ”โ”ทโ”โ”“   โ”โ”โ”ทโ”โ”“   โ”โ”โ”ทโ”โ”“   โ”โ”โ”ทโ”โ”“   โ”โ”โ”ทโ”โ”“   โ”โ”โ”ทโ”โ”“   โ”โ”โ”ทโ”โ”“   โ”โ”โ”ทโ”โ”“
lry0: โ”ƒ 0 โ”ƒ   โ”ƒ 1 โ”ƒ   โ”ƒ 2 โ”ƒ   โ”ƒ 3 โ”ƒ   โ”ƒ 4 โ”ƒ   โ”ƒ 5 โ”ƒ   โ”ƒ 6 โ”ƒ   โ”ƒ 7 โ”ƒ
      โ”—โ”โ”โ”โ”›   โ”—โ”โ”โ”โ”›   โ”—โ”โ”โ”โ”›   โ”—โ”โ”โ”โ”›   โ”—โ”โ”โ”โ”›   โ”—โ”โ”โ”โ”›   โ”—โ”โ”โ”โ”›   โ”—โ”โ”โ”โ”›

height = 3
num_leaves = (2^height) = 2^3 = 8
59 Likes

Thanks for the explanation

25 Likes