celestiaorg / nmt

Namespaced Merkle Tree
Apache License 2.0
112 stars 39 forks source link

Verification functions do not check the consistency between the NID of the supplied leaves and the queried NID #183

Closed staheri14 closed 1 year ago

staheri14 commented 1 year ago

Problem

None of the current proof verification functions i.e., VerifyNamespace and VerifyInclusion verify the consistency between the namespace ID of the leaves and the queried name ID (this inconsistency gets caught later in the root calculation). This can be addressed by incorporating a check at the beginning of the functions, which not only makes the code more clear and easier to maintain and debug, but also saves a significant amount of time by preventing unnecessary root calculations from invalid inputs and allowing for early returns from the function call.

staheri14 commented 1 year ago

Update: VerifyNamespace does check the NID of the leaves against the queried NID in this line prior to hashing. Nothing to be done in this method. VerifyInclusion does not have to do any check since the leaves passed as argument to this method are not namespaced, and get namespaced within the method (hence no check needed). The only method that can do the check but does not is the, soon to be exposed, VerifyLeafHashes. However, incorporating this check depends on our decision on the following issue https://github.com/celestiaorg/nmt/issues/110, so going to mark it as blocked until the other one gets concluded.

staheri14 commented 1 year ago

Update: VerifyLeafHashes should be revised to include a check that ensures the namespace of the leafHashes parameter (if it is present) matches the queried namespace. This required behavior should remain consistent regardless of whether the proof is a partial absence proof or a full absence proof (in the case of an absence proof, leafHashes will be empty). I will proceed with implementing this modification.