privacy-scaling-explorations / zk-kit.solidity

A monorepo of reusable contracts for zero-knowledge technologies.
MIT License
10 stars 3 forks source link

optimization with nodes to keep track the most recent nodes does not work with duplicated nodes #41

Open 0xDatapunk opened 1 month ago

0xDatapunk commented 1 month ago

https://github.com/privacy-scaling-explorations/zk-kit.solidity/blob/a4e7136d6771456d0118729f3f5fe3dc9f7a4148/packages/imt/contracts/InternalQuinaryIMT.sol#L147-L149

when the updated node is on a path different from the more recently inserted path, which is recorded in self.lastSubtrees, we still might get (nodes[0] == self.lastSubtrees[i][0] || nodes[4] == self.lastSubtrees[i][4])==True, so self.lastSubtrees may be updated in

https://github.com/privacy-scaling-explorations/zk-kit.solidity/blob/a4e7136d6771456d0118729f3f5fe3dc9f7a4148/packages/imt/contracts/InternalQuinaryIMT.sol#L147-L149

However, _insert would be using wrong values in https://github.com/privacy-scaling-explorations/zk-kit.solidity/blob/a4e7136d6771456d0118729f3f5fe3dc9f7a4148/packages/imt/contracts/InternalQuinaryIMT.sol#L93

This optimization with nodes to keep track the most recent nodes does not work even if the tree does not allow for duplicated values, since update/delete may result in duplicated zero values. Also there is no mechanism to enforce always adding unique leaves, if that is the intention.