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

IncrementalBinaryMerkleTree.sol: Update can change uninitialized leaves in the tree #32

Closed
twister-dev opened this issue Oct 1, 2022 · 3 comments · Fixed by #33
Closed
Assignees
Labels
bug 🐛 Something isn't working

Comments

@twister-dev
Copy link
Contributor

Although this is a description for IncrementalBinaryMerkleTree.sol, it probably applies to the Quin Tree contract as well.

Describe the bug

Long story short: It is possible to use the update function to change a zero leaf at an index that hasn't been inserted into the tree yet.

For example, suppose that 4 leaves are inserted into the tree. We can update leaf 7 by proving inclusion of the zero value at leaf 7, because technically the tree is never "empty", but it begins completely filled with zero values.

Updating an uninitialized index can cause the tree root to no longer represent the set of leaves accurately, because the lastSubtrees array will be incorrect at some point when updating the root.

To Reproduce
You can see a demonstration of this bug in a unit test I wrote on my fork of the package:

IncrementalBinaryTreeTest.ts#L188

Expected behavior
The contract should reject updates that reach an index greater than or equal to the number of leaves in the tree. I implemented this in the next commit of the same fork and branch:

contract: IncrementalBinaryTree.sol#L124
test: IncrementalBinaryTreeTest.ts#L188

Additional context
I believe I patched the bug in the IncrementalBinaryMerkleTree.sol contract, as shared above, and if it is a satisfiable solution then I can submit a pull request with that change.

However, I'm mainly opening this bug report because I haven't studied Quin Merkle Trees and don't know how to patch that contract (if the bug is even there, but I'm assuming it is).

@twister-dev twister-dev added the bug 🐛 Something isn't working label Oct 1, 2022
@cedoor cedoor added this to ZK-Kit Oct 1, 2022
@cedoor cedoor moved this to 🗒 Todo in ZK-Kit Oct 1, 2022
@cedoor
Copy link
Member

cedoor commented Oct 1, 2022

Hi @twister-dev , thank you very much for the comprehensive description of this bug. I think your solution is right, and if you can also submit a PR, that would be great!

The Quin Merkle tree is exactly like a Binary Merkle tree, but with 5 child nodes per node, so the bug should be the same there. But if you're not sure we can fix it.

@twister-dev
Copy link
Contributor Author

No problem @cedoor. I submitted a pull request with the Binary Merkle tree fix. I'm looking into the Quin Tree next, honestly I'm still trying to figure out how to recover the index from the path indices, but once I figure that out I can submit a fix for that too. If I can't figure it out then I guess I'll follow up with a comment here so you can handle it.

@twister-dev
Copy link
Contributor Author

I addressed the bug in the Quin Tree as well. I can open a pull request on that fix after the first one goes through.

bug fix: IncrementalQuinTree.sol#L119-L146

unit test: IncrementalQuinTreeTest.ts#L188-L228

@cedoor cedoor moved this from 🗒 Todo to 🏗 In Progress in ZK-Kit Oct 4, 2022
@cedoor cedoor linked a pull request Oct 4, 2022 that will close this issue
2 tasks
@cedoor cedoor closed this as completed Oct 4, 2022
Repository owner moved this from 🏗 In Progress to ✔️ Done in ZK-Kit Oct 4, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 Something isn't working
Projects
Status: ✔️ Done
Development

Successfully merging a pull request may close this issue.

2 participants