Repository navigation
Conversation
tapleaf_hash() raised "script is required" for an empty script, so compute_merkle_root() refused any tree with an empty leaf. BIP 341 does not forbid an empty leaf script, its tapleaf hash is well defined (compact size 0), and a tapscript leaf with an empty script is satisfied by an initial stack of [0x01]. Add p2mr_empty_leaf_script to p2mr_construction.json. Its leaf hashes, Merkle root and control blocks were computed from the tagged hashes directly, not with p2mr.py; the other leaf is the 0xc0 leaf of p2mr_different_version_leaves, whose leaf hash matches.
compute_control_block() hashed each sibling with compute_merkle_root() starting at depth 0, so the 128 limit applied to the sibling subtree on its own rather than to its real depth. For a leaf at depth 1 next to a subtree that is 128 deep, it returned a 33-byte control block although compute_merkle_root() refuses that tree, because its deepest leaf sits at depth 129. Pass the sibling's depth to compute_merkle_root() and add the case to the negative structure tests.
Member
Contributor
|
This seems reasonable to me. Do we want to require depth as a required parameter to compute_merkle_root to avoid this bug in the future? What do you think @cryptoquick @notmike-5 ? |
|
I’d lean toward making |
Contributor
|
I'd like for @notmike-5 to weigh in on this. I'll make sure he knows that this is blocked on his review. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Two fixes to the Python reference implementation in
bip-0360/ref-impl/python/p2mr.py.1. Accept an empty leaf script.
tapleaf_hash()raisedscript is requiredfor an empty script, socompute_merkle_root()refused any tree with an empty leaf. BIP 341 does not forbid an empty leaf script, its tapleaf hash is well defined (compact size 0), and a tapscript leaf with an empty script is satisfied by an initial stack of[0x01].Adds
p2mr_empty_leaf_scripttop2mr_construction.json. Its leaf hashes, Merkle root and control blocks were computed from the tagged hashes directly, not withp2mr.py. The other leaf is the 0xc0 leaf ofp2mr_different_version_leaves, and its leaf hash matches the one there. On master,p2mr.pyraisesValueErroron this vector.2. Bound sibling subtrees by their real depth in
compute_control_block(). Each sibling was hashed withcompute_merkle_root()starting at depth 0, so the 128 limit applied to the sibling subtree on its own. For a leaf at depth 1 next to a 128-deep subtree, it returned a 33-byte control block, althoughcompute_merkle_root()refuses that tree because its deepest leaf sits at depth 129. The sibling's depth is now passed through, and the case is added to the negative structure tests. On master, the new test fails.Tested with
python3 p2mr.pyandpython3 -O p2mr.py; each commit passes on its own. This touches neitherBIP360_tests()nor the spec, and merges cleanly with #2380 and #2382.