-
Notifications
You must be signed in to change notification settings - Fork 195
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
feat(avm): merkle tree gadget #9205
base: master
Are you sure you want to change the base?
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. Join @IlyasRidhuan and the rest of your teammates on Graphite |
1f36cf1
to
76f5435
Compare
a421532
to
0959678
Compare
3832bb7
to
8f022b7
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Very good job! Please look at my comments.
barretenberg/cpp/src/barretenberg/vm/avm/trace/gadgets/merkle_tree.hpp
Outdated
Show resolved
Hide resolved
barretenberg/cpp/src/barretenberg/vm/avm/trace/gadgets/merkle_tree.cpp
Outdated
Show resolved
Hide resolved
std::vector<bool> path_bits; | ||
std::vector<FF> path_values; | ||
// These will be eventually stored somewhere as a "clock speed" | ||
auto entry_id = clk << 6; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you please add some explanations? the shift << 6 seems a magic constant.
barretenberg/cpp/src/barretenberg/vm/avm/tests/merkle_tree.test.cpp
Outdated
Show resolved
Hide resolved
8f022b7
to
bfe9c36
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome work! Added some comments. I mostly reviewed to help me understand what you've done and how to conceptualize these things in the circuit.
// These will be eventually stored somewhere as a "clock speed" | ||
auto entry_id = clk << 6; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand what entry_id is here, how can be interpreted as "clock speed", and why we shift left by 6
barretenberg/cpp/src/barretenberg/vm/avm/trace/gadgets/merkle_tree.cpp
Outdated
Show resolved
Hide resolved
// latch == 1 when the path_len == 0 | ||
sel_merkle_tree * (path_len * (latch * (1 - path_len_inv) + path_len_inv) - 1 + latch) = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My brain is having trouble understaning this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks very confusing but is our standard check for "set A = 1 when B = 0, and set A = 0 when B !=0".
It works as follows
- Note that
a * a_inverse == 1
, buta_inverse
only exists ifa != 0
(0 is the only element without an inverse in the field). - If
latch == 0
, then the formula becomespath_len * path_len_inv - 1 = 0
(which can only be true whenpath_len != 0,
since we need a validpath_len_inv
. - If
latch == 1
then the formula becomespath_len * ( 1 - path_len_inv + path_len_inv) = 0
, simplified this ispath_len = 0
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah I see.... Makes sense! I'd through this in comments (either everywhere we use it, or in one place that we can point to) 🤷
// If we are not done, the next leaf index is half the current leaf index; | ||
// We don't need to worry about underflowing the field since (leaf_index - LEAF_INDEX_IS_ODD) | ||
// wil be even (over the integers) and as the field is not of characteristic 2, leaf_index' == leaf_index / 2 over the integers | ||
sel_merkle_tree * (1 - latch) * (leaf_index' * 2 + LEAF_INDEX_IS_ODD - leaf_index) = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: maybe silly, this would be easier to read for me if the + LEAF_INDEX_IS_ODD
was at the end.
1385a08
to
5ce56b4
Compare
bfe9c36
to
e716c97
Compare
5ce56b4
to
54c1e3d
Compare
e716c97
to
88ce7d8
Compare
88ce7d8
to
4ae36f3
Compare
4ae36f3
to
a9434cd
Compare
Resolves #9458