Skip to content

bip360: accept empty leaf scripts and bound sibling depth in p2mr.py - #2387

Open
Rovart wants to merge 2 commits into
bitcoin:masterfrom
Rovart:bip360-ref-impl-fixes
Open

Rovart wants to merge 2 commits into
bitcoin:masterfrom
Rovart:bip360-ref-impl-fixes

Conversation

@Rovart

@Rovart Rovart commented Dec 4, 2026

Copy link
Copy Markdown

Two fixes to the Python reference implementation in bip-0360/ref-impl/python/p2mr.py.

1. Accept an empty leaf script. 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].

Adds 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, and its leaf hash matches the one there. On master, p2mr.py raises ValueError on this vector.

2. Bound sibling subtrees by their real depth in compute_control_block(). Each sibling was hashed with compute_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, although compute_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.py and python3 -O p2mr.py; each commit passes on its own. This touches neither BIP360_tests() nor the spec, and merges cleanly with #2380 and #2382.

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.
@jonatack jonatack added the Fixups Minor fixups not worth bothering the BIP author(s) for label Dec 4, 2026

@jonatack jonatack left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tested ACK a29e624

Fixes look correct. They pass independently and together. Checked compatibility with #2382 and all spend path vectors.

@jonatack

jonatack commented Dec 4, 2026

Copy link
Copy Markdown
Member

cc @cryptoquick @EthanHeilman

@EthanHeilman

Copy link
Copy Markdown
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 ?

@wijehyeon

Copy link
Copy Markdown

I’d lean toward making depth required. In a local check, the pre-PR tests still passed when I omitted depth from either the right-child recursion or the sibling-subtree call in compute_control_block(). The new depth-129 test caught both omissions. Making depth required and passing 0 at the four root call sites also passed the full Python reference suite.

@cryptoquick

cryptoquick commented Dec 5, 2026 •

Copy link
Copy Markdown
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.

@jonatack jonatack added the Pending acceptance This BIP modification requires sign-off by the champion of the BIP being modified label Dec 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Fixups Minor fixups not worth bothering the BIP author(s) for Pending acceptance This BIP modification requires sign-off by the champion of the BIP being modified

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants