-
Notifications
You must be signed in to change notification settings - Fork 94
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Access Merkle Proof in LazyIMT #123
Comments
Yes, this is possible to do. I'll answer your numbered questions first.
To answer the original question, yes this is possible. As you noticed some hashes don't exist in the tree, but the hash of the elements necessary to build the proof should always exist. To build a merkle proof the contract will need to iterate up the tree and at each level check if the intermediate root exists. If the root does not exist the intermediate root should be calculated and used when building higher level roots. This implementation doesn't include such a function because we didn't need it at the time. Building something like this shouldn't be too hard, it will be similar to the update and root functions. I'll look into it later today and see if i can write something simple. |
Thanks for your answer @vimwitch.
Interesting. Our use case expects constant modifications of the tree, and it can be accessed at any time. We don't expect the tree to stabilize ever.
I see, makes sense. Now that I think about it, what is the reason for storing intermediate leaves? Isn't it possible to hash all leafs up to the root in a view function every time someone wants to access it? Using my above example, why not store just Edit: Perhaps related, and may answer my question. Calculating the root "on the fly" of a tree with depth 20 as I suggest may hit the limits on any RPC provider? Eg Infura. So I guess the intermediate leaves fix this problem?
|
Working on #162 addressing this feature. |
Thanks @alrevuelta 👍🏽 Tracking this on the ZK-Kit board! |
Question rather than feature, but it might lead to a feature if feasible.
I'm using the LazyIMT and was wondering if it would be possible to get a Merkle Proof directly from the contract. The main benefit I see, is that any app would be able to get said proof to prove things, without having to locally fetch the whole tree and construct it locally. I'm using the
LazyIMT
because it seems to offer the closest I'm looking for, but open for other trees.With a reduced example, given the following tree with 8 leafs, I would like to get a Merkle Proof for a given leaf, provided by the contract. Example:
C
belongs to the tree: [D
,HASH_AB
,HASH_EFGH
]A
belongs to the tree: [B
,HASH_CD
,HASH_EFGH
]The
LazyIMT
seems to store inelements
the whole tree, both leafs at the bottom and intermediate levels. In theory, I could get the Merkle Proof ofC
, by accessingelements
at different levels:D
:_indexForElement(0, 3)
HASH_AB
:_indexForElement(1, 0)
HASH_EFGH
:_indexForElement(2, 1)
The problem is that when the leaf is a left element, it's not hashed. So when the amount of leaves is odd, then upper hashes don't match, since they are not initialized yet. In other words, in the above example, one would expect
element
at_indexForElement(3, 0)
to be the root, but this holds true only when the amount of leafs is even. Based on this, seems like I can't naively makeelements
public and use it for my purpose.Some questions:
LazyIMT
when compared to others? As far as I understandelements
stores both leaves and intermediate ones, hashed up to the root. This means that in my example (8 leafs) a total of 15 elements are stored (8,4,2,1) and updated. Can an insert trigger up toMAX_DEPTH
hashes?tldr: In order to not fall into the x->y, this is the problem I'm trying to solve. I would like to get the Merkle Proof of a given leaf, ideally with a view function and without worsening the gas consumption. Is this feasible? Which tree would you recommend me to use? Any idea on how to implement this?
Thanks.
The text was updated successfully, but these errors were encountered: