AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Low 45 Bitcoin

psbt: Fix tapscript witness signature ordering

Public commit record

What the developer wrote

Authored by nervana21

100/100 · Strong
psbt: Fix tapscript witness signature ordering

BIP-0342 requires that when executing a spend of a P2TR output using the
script path, the witness signatures are ordered in reverse of the order
in which their corresponding public keys appear in the script. For
example, a script with `pubkey_1` CHECKSIG `pubkey_2` CHECKSIGADD ...
`pubkey_n` CHECKSIGADD must have witness signatures ordered as `w_n`,
..., `w_2`, `w_1`, where `w_i` corresponds to `pubkey_i`. This is
necessary because of the LIFO nature of stack execution.

Previously, the example and test code pushed witness signatures onto
the witness stack in sorted key order (from `BTreeMap` iteration), which
may not have matched the order required by the specification. This
commit fixes the logic in the example and test code to match the
specification.

Reference:
- BIP-0342: https://github.com/bitcoin/bips/blob/master/bip-0342.mediawiki
✓ Descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context✓ Explains rationale or failure mode✓ Mentions testing or verification✓ Links an issue, advisory, or supporting reference✓ Names security-relevant behavior explicitly
The short version

What changed, and why it matters

This commit fixes example code and a test helper that builds Bitcoin Taproot script-path witness data. The old code placed digital signatures in the wrong order on the transaction witness stack, following the order of an internal sorted map instead of the reverse order required by the Bitcoin Taproot specification (BIP-0342). A transaction finalized with the old ordering would be rejected by the Bitcoin network, so no funds could be stolen, but users copying the example could produce invalid transactions. The fix reads public keys from the script in their actual script order, then pushes the matching signatures in reverse.

Recommended action

Review any local copies or forks of the taproot-psbt example and psbt-sign-taproot test helper that finalize PSBTs for Taproot script-path spends; ensure signatures are pushed in reverse script-key order. The library itself is not patched here, so downstream projects using rust-bitcoin's PSBT APIs should verify their own finalization logic. No emergency upgrade is required solely because of this commit.

Security signals we found

01

Incorrect witness ordering in Taproot script-path spend construction

02

Violation of BIP-0342 stack ordering requirement

03

Fix located in example/test code rather than production library

04

No cryptographic weakness introduced; change corrects a protocol-formatting bug

Risk score

Why this scored 45/100

Our methodology →
Potential impact 12/30
Exploitability 8/25
Stealth signal 7/15
Affected reach 6/15
Confidence 8/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.