Skip to content
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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

IlyasRidhuan
Copy link
Contributor

@IlyasRidhuan IlyasRidhuan commented Oct 11, 2024

Resolves #9458

@IlyasRidhuan IlyasRidhuan marked this pull request as ready for review October 11, 2024 18:03
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-11-feat_avm_merkle_tree_gadget branch from 1f36cf1 to 76f5435 Compare October 12, 2024 16:40
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-08-feat_avm_full_poseidon2 branch from a421532 to 0959678 Compare October 23, 2024 12:13
Base automatically changed from ir/10-08-feat_avm_full_poseidon2 to master October 23, 2024 14:31
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-11-feat_avm_merkle_tree_gadget branch 5 times, most recently from 3832bb7 to 8f022b7 Compare October 24, 2024 19:03
Copy link
Contributor

@jeanmon jeanmon left a 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/pil/avm/gadgets/merkle_tree.pil Outdated Show resolved Hide resolved
barretenberg/cpp/pil/avm/gadgets/merkle_tree.pil 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;
Copy link
Contributor

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.

Copy link
Contributor

@dbanks12 dbanks12 left a 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.

Comment on lines 15 to 17
// These will be eventually stored somewhere as a "clock speed"
auto entry_id = clk << 6;
Copy link
Contributor

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

Comment on lines +34 to +25
// latch == 1 when the path_len == 0
sel_merkle_tree * (path_len * (latch * (1 - path_len_inv) + path_len_inv) - 1 + latch) = 0;
Copy link
Contributor

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

Copy link
Contributor Author

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

  1. Note that a * a_inverse == 1, but a_inverse only exists if a != 0 (0 is the only element without an inverse in the field).
  2. If latch == 0, then the formula becomes path_len * path_len_inv - 1 = 0 (which can only be true when path_len != 0, since we need a valid path_len_inv.
  3. If latch == 1 then the formula becomes path_len * ( 1 - path_len_inv + path_len_inv) = 0, simplified this is path_len = 0

Copy link
Contributor

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) 🤷

barretenberg/cpp/pil/avm/gadgets/merkle_tree.pil Outdated Show resolved Hide resolved
Comment on lines +40 to +35
// 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;
Copy link
Contributor

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.

barretenberg/cpp/pil/avm/gadgets/merkle_tree.pil Outdated Show resolved Hide resolved
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-25-fix_avm_address_bytecode_hashing_comments branch from 1385a08 to 5ce56b4 Compare October 26, 2024 17:39
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-11-feat_avm_merkle_tree_gadget branch from bfe9c36 to e716c97 Compare October 26, 2024 18:28
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-25-fix_avm_address_bytecode_hashing_comments branch from 5ce56b4 to 54c1e3d Compare October 27, 2024 13:32
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-11-feat_avm_merkle_tree_gadget branch from e716c97 to 88ce7d8 Compare October 27, 2024 13:40
Base automatically changed from ir/10-25-fix_avm_address_bytecode_hashing_comments to master October 27, 2024 15:02
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-11-feat_avm_merkle_tree_gadget branch from 88ce7d8 to 4ae36f3 Compare October 27, 2024 15:33
@IlyasRidhuan IlyasRidhuan marked this pull request as ready for review October 27, 2024 15:33
@IlyasRidhuan IlyasRidhuan force-pushed the ir/10-11-feat_avm_merkle_tree_gadget branch from 4ae36f3 to a9434cd Compare October 27, 2024 20:39
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.

AVM: Merkle gadget
3 participants