-
Notifications
You must be signed in to change notification settings - Fork 35
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
Mst
efficiency improvement
#188
Mst
efficiency improvement
#188
Conversation
…reimage` methods
.try_into() | ||
.unwrap(); | ||
|
||
let sibling_leaf_node_hash = poseidon_entry::<N_ASSETS>( |
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.
poseidon_entry as well as poseidon_node are specific to Node creation and are always used in that context, so their implementations can be placed directly in the Node (no need to have them inside a separate file in utils). For example, here you first create the hash of sibling_leaf_node and then create the sibling_leaf_node Node using this hash, not using the hash for anything else. Meanwhile, Node already has the ::leaf constructor that takes username and balances as args. Moving both hashing operations into the Node would remove confusion over how to create the node.
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.
Thanks for the suggestion! Changes applied in fc44e01. I hope that now it looks cleaner
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 is a good improvement, especially for large entry sets.
Also, thank you for the minor updates in the backend.
I updated the code base according to your comments. A further thing that I noticed while reviewing the codebase is that there was an API |
/// Builds a leaf-level node of the MST | ||
/// The leaf node hash is equal to `H(username, balance[0], balance[1], ... balance[N_ASSETS - 1])` | ||
/// The balances are equal to `balance[0], balance[1], ... balance[N_ASSETS - 1]` | ||
pub fn leaf(username: &BigUint, balances: &[BigUint; N_ASSETS]) -> Node<N_ASSETS> |
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.
In the true spirit of DRY this could just call the Node::leaf_node_from_preimage()
after converting the args :) Same with the middle calling Node::middle_from_preimage inside itself. Won't request changes, we can do this minor enhancement later.
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.
Let's DRY!
Gonna apply the changes now (:
|
||
hash_preimage[N_ASSETS] = tree[level - 1][index].hash; | ||
hash_preimage[N_ASSETS + 1] = tree[level - 1][index + 1].hash; | ||
Node::middle_node_from_preimage(&hash_preimage) |
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.
Actually that's why we could have both constructors. I just meant that one could call the other inside itself.
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.
Oh I see, I misunderstood your point
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.
Applied changes in last commit!
This reverts commit ae539a2.
a734d94
into
v1-improvements-and-consolidation
* feat: tree building rules * feat: add `get_middle_node_hash_preimage` and `get_middle_node_hash_preimage` methods * feat: update `generate_proof` * fix: `generate_proof` * fix: modify middle nodes hashing logic in circuit * feat: update `MerkleProof` * feat: add further constraint on `MstInclusionCircuit` * feat: rm `test_balance_not_in_range` * refactor: update `MerkleProof` * chore: fix `backend` * test: fix `backend` * fix: testing * fix: modify `MstInclusionCircuit` struct * Revert "fix: modify `MstInclusionCircuit` struct" This reverts commit e3ba8e6. * chore: update `benches` * fix: move poseidon hasher APIs to `Node` * chore: add guide on `MerkleProof ` * chore: update `where` clause in backend * chore: update comments * chore: move `generate_leaf_hash` outside of `zk_prover` * chore: add `ethers` legacy feature * chore: update verifier contract * fix: removed minting erc20 on the backend test * chore: rm unused imports * feat: remove `Node::leaf()` * feat: rm `Node::middle()` * Revert "feat: remove `Node::leaf()`" This reverts commit ae539a2. * chore: DRY code --------- Co-authored-by: sifnoc <[email protected]>
* feat: tree building rules * feat: add `get_middle_node_hash_preimage` and `get_middle_node_hash_preimage` methods * feat: update `generate_proof` * fix: `generate_proof` * fix: modify middle nodes hashing logic in circuit * feat: update `MerkleProof` * feat: add further constraint on `MstInclusionCircuit` * feat: rm `test_balance_not_in_range` * refactor: update `MerkleProof` * chore: fix `backend` * test: fix `backend` * fix: testing * fix: modify `MstInclusionCircuit` struct * Revert "fix: modify `MstInclusionCircuit` struct" This reverts commit e3ba8e6. * chore: update `benches` * fix: move poseidon hasher APIs to `Node` * chore: add guide on `MerkleProof ` * chore: update `where` clause in backend * chore: update comments * chore: move `generate_leaf_hash` outside of `zk_prover` * chore: add `ethers` legacy feature * chore: update verifier contract * fix: removed minting erc20 on the backend test * chore: rm unused imports * feat: remove `Node::leaf()` * feat: rm `Node::middle()` * Revert "feat: remove `Node::leaf()`" This reverts commit ae539a2. * chore: DRY code --------- Co-authored-by: sifnoc <[email protected]>
#166
Major breaking changes in this PR:
balance_left, hash_left, balance_right, hash_right
now it isbalance_left + balance_right, hash_left, hash_right
sibling_sums
andsibling_hashes
to be used inside the witness. Now we need to open the hash of each sibling. Because of that theMerkleProof
now has fieldssibling_leaf_node_hash_preimage
andsibling_middle_node_hash_preimages
rather thansibling_hashes
andsibling_sums
. The logic of the circuit is modified to build eachsibling_hash
inside the circuit and constraint that thebalances
associated to a sibling are the same used as input to build the hashTo do:
Tree
trait, it might also have an impact onAggregationMerkleSumTree
Benchmarks:
Given that the focus of V1 is fast commitment time. I think that this is a reasonable tradeoff.