Skip to content

bip360: add spend-path test vectors - #2232

Merged
jonatack merged 1 commit into
bitcoin:masterfrom
jeanpablojp:bip360-spend-vectors
Dec 3, 2026
Merged

jonatack merged 1 commit into
bitcoin:masterfrom
jeanpablojp:bip360-spend-vectors

Conversation

@jeanpablojp

@jeanpablojp jeanpablojp commented Aug 1, 2026 •

Copy link
Copy Markdown
Contributor

BIP 360's vectors stop at construction: a script tree goes in, leaf hashes, Merkle root, scriptPubKey, address and control blocks come out. There is nothing to spend, unlike BIP 341's wallet-test-vectors.json, so every implementation ends up writing its own spend coverage. This adds the spend-path vectors offered in the Delving write-up (https://delvingbitcoin.org/t/bip-360-p2mr-implemented-in-bitcoin-core-on-regtest-vector-results-measurements-spec-feedback/2751).

The schema mirrors bip-0341/wallet-test-vectors.json, adapted to an output type whose only spend path is the script path. Three transactions carry the valid cases, per-input given/intermediary/expected with the sighash midstates at group level. invalidSpending holds ten standalone transactions that must fail; error is a spec-level description for implementations to map onto their own codes, and several deliberately collapse onto one code in ours. Inputs spent without a signature have privkey/hashType/annex/sigMsg/sigHash null rather than absent.

Valid:

  • depth-1 leaf, SIGHASH_DEFAULT
  • depth-2 leaf of a three-leaf tree, SIGHASH_ALL (65-byte signature)
  • annex, covered by the signature
  • p2mr_single_leaf_script_tree at depth 0, no signature: the v0.12.0 anyone-can-spend rule
  • p2mr_different_version_leaves through its 0xfa leaf: unknown leaf versions are unencumbered
  • depth-128 leaf, control block at its 4097-byte maximum: the accepting side of the depth limit
  • a second transaction with one input for each of the other five hash types (SIGHASH_NONE, SIGHASH_SINGLE and the three ANYONECANPAY forms), with SINGLE|ANYONECANPAY at input 1 so it signs output 1 and not output 0
  • a third transaction with one input, spent through a 0xfa leaf whose script is OP_RETURN, at depth 1, so it passes only if the leaf is not executed

Invalid: flipped signature, tampered Merkle path, control byte with the last bit 0, single-element witness, two-element witness ending in an annex, no witness at all, a signature with the undefined hash type 0x80, and control blocks of 0, 34 and 4129 bytes, which is one too short for the control byte, one that is not 1 + 32*m, and one at m = 129 on a real tree, where the path reaches the witness program and the signature is valid.

Both named trees come from p2mr_construction.json and are asserted against it when the file is generated, so the two files cross-check each other. Keys and prevout txids are derived deterministically and signing is BIP 340 with an all-zero aux, so the file regenerates from scratch.

One case takes a position the spec leaves open: p2mr_spend_control_byte_low_bit_zero expects failure when the last bit of c[0] is 0. Script Validation says that bit "is unused and must be 1" and the footnote says a faulty deserialization "will cause an immediate error", but that bullet states no failure condition, while the length rule above it does. If non-enforcement is the intended reading I am happy to drop the vector and the footnote could be reworded; either way it would help to have it explicit.

Verified against a Bitcoin Core implementation of the BIP: every valid input passes VerifyScript and every invalid case fails with the expected script error (https://github.com/jeanpablojp/bitcoin/tree/p2mr-regtest, src/test/p2mr_vector_tests.cpp). The generator is test/functional/tool_p2mr_spend_vectors.py on the same branch.

@jonatack jonatack added Proposed BIP modification PR by non-owner to update BIP content Pending acceptance This BIP modification requires sign-off by the champion of the BIP being modified labels Aug 2, 2026
@murchandamus

Copy link
Copy Markdown
Member

cc: @cryptoquick

@jeanpablojp
jeanpablojp force-pushed the bip360-spend-vectors branch from 92cb0b1 to adbc31f Compare August 15, 2026 18:36

@quantova-inc quantova-inc left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I loaded the file and checked it against my own BIP 360 work. The depth coverage is solid. There is a passing control block at the maximum depth of 128 with the matching reject at 129. The small end is covered down to a single leaf. The reject set with stable error ids reads clean.

One gap on the passing side. The six valid inputs sign with SIGHASH_DEFAULT and SIGHASH_ALL only. SIGHASH_SINGLE, SIGHASH_NONE and the ANYONECANPAY variants are the flags that change which sighash midstate is used, so with none of them in a passing case an implementation that selects the wrong midstate for those flags would still pass every vector. Worth adding a few inputs that exercise them. I can send the signed inputs if that helps.

@jeanpablojp

jeanpablojp commented Nov 2, 2026 •

Copy link
Copy Markdown
Contributor Author

Right, nothing on the passing side reached NONE, SINGLE or ANYONECANPAY. I added a second transaction to scriptPathSpending with one input per remaining hash type, derived the same way as the rest of the file.

I changed SignatureHashSchnorr three ways, to ignore ANYONECANPAY, to commit NONE to the outputs and to take output 0 for SINGLE. Each time the old file still passed and the new one failed.

@quantova-inc

quantova-inc commented Nov 4, 2026 •

Copy link
Copy Markdown

That covers the gap. The second transaction exercises NONE SINGLE and ANYONECANPAY and the three changes you made each break the new case while the old file still passes so the vectors now pin the midstate selection. Thank you for adding it. I work on post quantum signatures at Quantova and since this sits under the BIP 360 post quantum effort I am glad to contribute the signed inputs and the post quantum test vectors for the new spend paths. Keep Quantova in mind for that side and the door open and I will help wherever it is useful.

@cryptoquick

Copy link
Copy Markdown
Contributor

I'll get this updated and validated in libbitcoinpqc, thank you for opening this issue, @quantova-inc

cryptoquick/libbitcoinpqc#32

@mossein

mossein commented Nov 28, 2026 •

Copy link
Copy Markdown

tACK 4daec02

Wrote a verifier from the BIP text without looking at the generator (https://gist.github.com/mossein/49fc06bac78d9c942e7cafc0fc649092). All 11 valid inputs pass, all 10 invalid ones fail for the listed reason, and every intermediary value matches.

On the low bit question, I think the vector should stay. That bit isn't covered by the signature, so if it's not enforced anyone relaying the tx can flip it and change the wtxid. I tried it on input 0 and it still validates. Could we add "Fail if c[0] & 1 = 0" to the spec?

@jeanpablojp

Copy link
Copy Markdown
Contributor Author

If the authors accept the Fail if line, I'll update this PR to reflect it also, thanks.

@devdavidejesus

Copy link
Copy Markdown
Contributor

Concept ACK.

I reviewed the test vectors in commit 4daec02d1ed281b6a2fd26b6ea24c2138bd9bff9. All 11 valid cases check out, including hashes, Merkle roots, control blocks, witness layouts, and signatures. All 10 invalid cases are also rejected as expected.

I found three issues worth addressing:

1. p2mr_spend_control_block_too_deep does not isolate the depth limit (m ≤ 128).

Even with the depth limit disabled, the vector is still rejected due to a witness program mismatch. A harness that only checks accept/reject would therefore not catch an implementation missing the depth check.

I suggest using an actual Merkle tree with a leaf at depth 129, with its root as the spent output's witness program, so the case passes when only the depth limit is disabled.

2. p2mr_spend_undefined_hash_type (0x80) has an inaccurate comment.

The signature matches the message built by TaprootSignatureMsg in Bitcoin Core's functional test framework, which omits sha_outputs for this hash type. However, applied literally to 0x80, the SigMsg steps in BIP 341 would include sha_outputs, since 0x80 & 3 is neither SIGHASH_NONE nor SIGHASH_SINGLE.

An implementation following the BIP text but missing the hash type check would still reject the vector due to an invalid signature. I suggest updating the comment to clarify which message construction was used.

3. The unknown leaf version (0xfa) case does not test whether execution is correctly skipped.

The current script only pushes "BIP341" onto the stack, so it would also succeed if incorrectly executed as tapscript.

I suggest adding another 0xfa case with a script that would fail if executed, such as OP_RETURN, while keeping the existing vector.

Overall, the vectors provide good coverage, and addressing these points would make them more effective at detecting implementations that miss the intended rules.

@jonatack jonatack added Tests and removed Proposed BIP modification PR by non-owner to update BIP content labels Dec 3, 2026
@cryptoquick

Copy link
Copy Markdown
Contributor

Thank you for the thorough review, @devdavidejesus

@jeanpablojp Are you able to make those test changes?

Also, I'm wondering if the BIP text alone is insufficient to replicate the intended behavior, should the BIP text be clarified? What do you think, @jonatack ? Happy to help with that also, and verify and integrate the improved tests.

The existing vectors are construction-only, so implementations have to
invent their own spend coverage. This adds a spending file in the shape
of bip-0341/wallet-test-vectors.json, adapted to an output type whose
only spend path is the script path.

Three transactions cover the valid cases, each with the five shared
BIP 341 sighash hashes at the group level and per-input given,
intermediary and expected fields. The first has six inputs and signs
only with SIGHASH_DEFAULT and SIGHASH_ALL, and the second has five, one
for each of the other hash types. The third has a single input, spent
through an unknown leaf version whose script is OP_RETURN. A separate
invalidSpending array has ten self-contained transactions that must fail
validation, with the reason in a short spec-level error string.

Private keys are sha256("p2mr_spending/key/<n>"), prevout txids are
sha256("p2mr_spending/prevout/<n>") in internal byte order, and signing
is BIP 340 with an all-zero aux, so the signatures can be re-derived
rather than trusted. Two of the inputs reuse trees from
p2mr_construction.json, which makes the two files cross-check each
other.
@jeanpablojp

Copy link
Copy Markdown
Contributor Author

Thanks @devdavidejesus

@cryptoquick, all changes are done. I also updated the description.

@cryptoquick cryptoquick left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

ACK. LGTM.

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

Review feedback from @quantova-inc and @devdavidejesus was addressed (thanks!)

Some follow-ups:

  • Agree with Fail if (c[0] & 1) == 0 (or Fail if c[0] & 1 ≠ 1) on the c[0] bullet. As @mossein noted, v = c[0] & 0xfe masks off bit 0, so bit 0 is neither committed to by tapleaf_hash nor by SigMsg. If not consensus-enforced, any third party could flip c[0] & 1 in transit and malleate wtxid (unlike P2TR, where BIP 341 explicitly fails on c[0] & 1 ≠ y(Q) mod 2). Right now BIP 360 says bit 0 "is unused and must be 1" without a Fail if clause (unlike the adjacent bullets), which creates unnecessary ambiguity for implementers.

  • JSON nesting depth = 135 (for Rust / serde_json consumers). Because group 0 input 5 represents the depth-128 scriptTree as 128 nested JSON arrays, the file reaches a maximum JSON nesting depth of 135. Rust's serde_json defaults to a max recursion limit of 128, so any Rust consumer (libbitcoinpqc, rust-bitcoin, etc.) calling serde_json::from_str directly will hit recursion limit exceeded unless they disable/raise the limit via Deserializer::disable_recursion_limit(). Maybe worth noting in the PR or file comment (not a blocker, since the nested-array format matches BIP 341's scriptTree schema).

  • Nit follow-ups:

    • Test Vectors and Reference Code links only to p2mr_construction.json (can be updated to also reference p2mr_spending.json in a follow-up), and bip-0360/ref-impl/python/p2mr.py doesn't yet exercise p2mr_spending.json.

    • Further coverage: (BIP 341/342-inherited rather than P2MR-specific, so fine to omit here): a 65-byte signature with explicit 0x00 hashType and SIGHASH_SINGLE without a matching output are not tested, nor is a 3-element witness with an annex (e.g. depth-0 + annex).

@jonatack
jonatack merged commit 78255e0 into bitcoin:master Dec 3, 2026
4 checks passed
@jonatack jonatack removed the Pending acceptance This BIP modification requires sign-off by the champion of the BIP being modified label Dec 3, 2026
jeanpablojp added a commit to jeanpablojp/bips that referenced this pull request Dec 3, 2026
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.
jeanpablojp added a commit to jeanpablojp/bips that referenced this pull request Dec 3, 2026
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.
jeanpablojp added a commit to jeanpablojp/bips that referenced this pull request Dec 3, 2026
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants