Skip to content

bip360: follow-ups to the spend-path test vectors - #2382

Open
jeanpablojp wants to merge 6 commits into
bitcoin:masterfrom
jeanpablojp:bip360-spend-vectors-followup
Open

jeanpablojp wants to merge 6 commits into
bitcoin:masterfrom
jeanpablojp:bip360-spend-vectors-followup

Conversation

@jeanpablojp

@jeanpablojp jeanpablojp commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Follow-ups to #2232, one commit each.

  • Fail if the last bit of c[0] is 0. Neither the tapleaf hash nor the signature message commits to that bit, and without a failure condition anyone relaying a spend can flip it and change the wtxid. mossein raised it in bip360: add spend-path test vectors #2232 and jonatack agreed. The list of changes from BIP 341 now names the bit too.
  • Point to p2mr_spending.json in Test Vectors and Reference Code.
  • Note in the vector file that the depth-128 scriptTree puts it at a JSON nesting depth of 135, past serde_json's default limit of 128.
  • Add a vector with a 65-byte signature ending in 0x00, whose first 64 bytes are a valid SIGHASH_DEFAULT signature, and one that spends a depth-0 output with an annex, three witness elements in all.
  • Add the 0.12.2 changelog entry and bump the Version header.

The vectors come from the same generator as before, now on the p2mr-spend-vectors-followup branch of my Core fork, where src/test/p2mr_vector_tests.cpp runs every case through VerifyScript. For each new vector I took the rule it covers out of Core, and that vector was the only one to change result.

I left out SIGHASH_SINGLE without a matching output. It needs an invalid transaction with two inputs, and every invalid case in the file has one input, so covering it means a new field in the invalid format.

Running p2mr_spending.json from p2mr.py touches the same function as #2380, so I'll do that once it is in.

The control byte's last bit is masked off for the leaf version, so
neither the tapleaf hash nor the signature message commits to it. The
text says it must be 1 but gives no failure condition, unlike the
length rule above it, and without one anyone relaying a spend can flip
the bit and change its wtxid. BIP 341 fails on the same bit for P2TR,
where it carries the parity of Q.

The list of changes from BIP 341 now names the bit too.
p2mr_spend_control_byte_low_bit_zero in p2mr_spending.json already
expects failure. mossein raised this in bitcoin#2232 and jonatack
agreed there.
p2mr_spending.json was added next to p2mr_construction.json in
bitcoin#2232, but Test Vectors and Reference Code still names only
the construction file.
@jonatack jonatack added the Fixups Minor fixups not worth bothering the BIP author(s) for label Dec 3, 2026
@jeanpablojp

Copy link
Copy Markdown
Contributor Author

I have one open point, i left out SIGHASH_SINGLE without a matching output. It needs an invalid transaction with two inputs, and every invalid case in the file has one input, so covering it means a new field in the invalid format. Happy to add it if that's fine.

@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.

tACK 411a7ed, addresses the follow-ups in #2232

2 non-blocking suggestions

@cryptoquick wdyt?

The depth-128 scriptTree in p2mr_spending.json puts the file at a JSON
nesting depth of 135, past serde_json's default limit of 128. The
top-level comment now says so, as jonatack suggested in
bitcoin#2232. It also says that the prevout txids are in internal
byte order, which until now only the commit message of bitcoin#2232 said.
p2mr_spend_explicit_default_hash_type signs correctly for
SIGHASH_DEFAULT but appends 0x00, which BIP 341 rejects in a 65-byte
signature. A fourth transaction spends a depth-0 output with an annex,
three witness elements, so the annex has to come off before the depth-0
rule applies. Both were among the follow-ups jonatack listed in
bitcoin#2232. The top-level comment now also says that the fourth
group reuses p2mr_single_leaf_script_tree.
Record the new failure condition on the last bit of c[0] and the
spend-path test vectors, which went in with bitcoin#2232 without an
entry, and move the Version header to 0.12.2.
@jeanpablojp
jeanpablojp force-pushed the bip360-spend-vectors-followup branch from 411a7ed to 7e34385 Compare December 3, 2026 21:53
@jeanpablojp

Copy link
Copy Markdown
Contributor Author

Both taken, thanks.

The Security section still tells key path wallets that moving to a
simple OP_CHECKSIG leaf script is enough. A single leaf is a depth-zero
tree, which bitcoin#2198 made anyone-can-spend in 0.12.0, and the
p2mr_single_leaf_script_tree spend vector spends one with no signature.
That PR updated the other places that described depth-zero trees but
left this sentence. Add a line to the 0.12.2 changelog entry.
@josuetrindade

Copy link
Copy Markdown

tACK 605a46e

Verified the spec updates and test vectors with a standalone Python script (no Core build):

  • Recomputed TapLeaf/TapBranch roots, TapSighash messages, and BIP 340 Schnorr signatures across all 4 scriptPathSpending groups (matching fullySignedTx byte-for-byte), including the new depth-0 + annex vector and confirming its prevout matches sha256('p2mr_spending/prevout/12') in internal byte order.
  • Confirmed p2mr_spend_explicit_default_hash_type has a valid Merkle proof, c[0] = 0xc1, and a valid 64-byte SIGHASH_DEFAULT signature before the trailing 0x00, and p2mr_spend_control_byte_low_bit_zero is valid aside from c[0] & 1 == 0.
  • Confirmed p2mr_spending.json reaches JSON nesting depth 135 from the depth-128 scriptTree, matching the top-level comment.

@jonatack

jonatack commented Dec 4, 2026

Copy link
Copy Markdown
Member

re-tACK 605a46e

@jonatack
jonatack requested a review from cryptoquick December 4, 2026 16:51
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants