Repository navigation
Description
Problem Statement
P2MR leaf/branch/root commitments are shared by witness validation, CHECKSIGPQC/CHECKDATASIGPQC signing, descriptors, RPC prevout/proof parsing, PSBTs, and address generation. Current tests cover many behaviors, but most expected P2MR roots/control blocks are recomputed with the same production helpers being validated. Add independent byte-level vectors that pin the P2MR commitment format and failure modes.
Evidence
- Core helpers live in
src/script/interpreter.cpp:2195:ComputeP2MRLeafHash,ComputeP2MRBranchHash, andComputeP2MRMerkleRoot. - Witness validation enforces control size, bit-0 marker, leaf-version masking, and direct
program == merkle_rootcommitment insrc/script/interpreter.cpp:2369. - Signing, descriptors, RPC prevouts, RPC proofs, and wallet data proofs all validate control blocks through the same helper path:
src/script/sign.cpp:604,src/script/descriptor.cpp:3263,src/rpc/rawtransaction_util.cpp:93,src/rpc/signmessage.cpp:242,src/wallet/scriptpubkeyman.cpp:2402. - Existing tests cover valid/invalid control sizes, missing bit 0, commitment mismatch, branch paths, and address vectors, but often compute expected values through production helpers in
src/test/script_p2mr_tests.cpp:54andsrc/test/descriptor_p2mr_tests.cpp:394.
Acceptance Criteria
- Add independently generated P2MR vectors. Expected values must be fixed constants, not derived in-test through
ComputeP2MRLeafHash,ComputeP2MRBranchHash,ComputeP2MRMerkleRoot, orTaprootBuilder. - Validate exact
leaf_hash, branch/root hash,control_block, finalmerkle_root, witness v2scriptPubKey, and mainnet/regtest address. - Cover single-leaf, two-leaf, and at least one multi-level branch. Include a branch-ordering case where the spent leaf hash sorts after the sibling.
- Add negative vectors for wrong leaf version, wrong CompactSize/script length encoding, swapped/unsorted branch preimage, mutated sibling, malformed control-block size, missing bit-0 marker, and wrong root/address commitment.
- Keep current helper-based tests; they remain useful for implementation self-consistency and behavioral coverage.
- Audit existing P2MR tests for accidental
ComputeTapleafHashuse whereComputeP2MRLeafHashis intended. Clarify intentional malformed cases with naming/comments, or fix unintended cases.
Suggested Vector Fields
name, leaf_script, leaf_version, leaf_preimage, leaf_hash, siblings, branch_ordering, branch_preimages, control_block, merkle_root, scriptPubKey, mainnet_address, regtest_address, expected_valid, expected_error.
Recommended Test Location
Use src/test/data/p2mr_vectors.json for canonical positive vectors and reusable negative vectors, following the existing BIP341 JSON-vector pattern. Add focused C++ unit coverage in src/test/script_p2mr_tests.cpp and src/test/descriptor_p2mr_tests.cpp.
Functional tests should not need to consume the JSON unless they validate an RPC serialization surface. Add only lightweight functional/RPC coverage if it proves verifydatapqchash or signrawtransactionwithkey accepts/rejects a pinned vector end-to-end.
Implementation Notes
Generate the vector values with a small standalone script that implements only tagged SHA256, CompactSize encoding, and lexicographic branch ordering from primitives. Do not import qbit production or test-framework P2MR helpers. Commit the JSON vectors; commit the generator only if maintainers want reproducibility in-tree.
Activity