Transaction Malleability
Transaction malleability let anyone change a transaction's TXID without the private keys. The nine vectors listed in BIP-62, what Mt. Gox claimed and what researchers found, what BIP-66 and SegWit fixed, and what is still malleable today.
Contents
Transaction malleability is the property that someone other than the creator of a transaction can change its TXID while leaving the transaction just as valid. The inputs, outputs and amounts stay the same. Only the identifier changes. BIP-62 put it this way: a valid transaction can be modified in-flight, without invalidating it, but without access to the relevant private keys.
Why the TXID Changes
A TXID is the double SHA-256 hash of the whole transaction. Before SegWit, that data included the scriptSig, which holds the signatures.
A signature protects the inputs and outputs of a transaction, but it cannot protect itself, because a signature cannot sign itself. If there is even one other way to write the signature part that is still valid, anyone relaying the transaction can rewrite it that way and produce the same transaction with a different TXID.
Nine Ways to Do It
BIP-62, assigned in March 2014, listed the vectors known at the time.
- ECDSA signatures not strictly DER encoded. OpenSSL, which did the verification then, accepted looser encodings.
- Non-push operations in scriptSig that produce the same result as the intended pushes.
- Pushing the same data with a different push operator (OP_PUSHDATA1, 2 or 4).
- Zero-padding values that are read as numbers.
- ECDSA itself. If (r, s) is a valid signature, so is (r, n-s), where n is the curve order.
- Extra data pushed at the start of scriptSig that the locking script never consumes.
- Changing inputs that the locking script ignores.
- Changing parts that SIGHASH flags left out of the signature.
- The sender producing a new signature for the same inputs and outputs.
The first seven are open to a third party without any private key. Numbers 8 and 9 are available only to someone who can sign, and BIP-62 notes that consensus rules cannot fix those two.
What Goes Wrong
No coins disappear. The modified transaction still sends the same inputs to the same outputs. BIP-62 names three problems.
- The sender may not recognize their own transaction. A wallet or exchange that tracks confirmation by TXID sees the transaction as gone.
- A follow-up transaction that spends the change of an unconfirmed transaction becomes invalid. It refers to the earlier transaction by TXID, and if the modified version is mined, that TXID never exists.
- A modified transaction is effectively a double spend of the original. It can arise without malice and can make other attacks easier.
The second problem matters most for contracts. Suppose two people want to fund a 2-of-2 multisig output, and before funding it they want to exchange signatures on a refund transaction that returns the money. The refund transaction has to refer to the funding transaction's TXID. If that TXID can change after broadcast, the pre-signed refund is worthless.
Mt. Gox
In February 2014 Mt. Gox halted withdrawals and filed for bankruptcy, claiming that attackers had drained its accounts with malleability attacks. The idea is to change the TXID of a withdrawal and then ask for it again as if it had never been paid.
Christian Decker and Roger Wattenhofer of ETH Zurich examined traces of the Bitcoin network covering more than a year before the filing. They concluded that the problem is real, but that there was no widespread use of malleability attacks before Mt. Gox closed.
How It Was Closed Off
Malleability was not fixed in one step. Relay policy came first, some of it was promoted to consensus, and finally the structure was changed.
| When | Measure | Kind | What it covers |
|---|---|---|---|
| Bitcoin Core 0.8.0 | Non-DER signatures no longer relayed | Relay policy | No. 1 |
| March 2014, 0.9 | Wallet creates only low-S signatures | Wallet behaviour | No. 5 (own signatures only) |
| March 2014, BIP-62 | Proposal to make Nos. 1 to 7 consensus rules | Closed without deployment | - |
| July 2015, BIP-66 | Strict DER signatures, from block 363,725 | Consensus | No. 1 |
| October 2015, 0.11.1 | High-S signatures not relayed or mined | Relay policy | No. 5 |
| BIP-146 | Proposal to make low S and NULLFAIL consensus rules | Closed without deployment | - |
| August 2017, BIP-141 | SegWit, from block 481,824 | Consensus | All third-party malleability (SegWit inputs) |
The 0.11.1 release notes introduced the low-S rule with the sentence "Consensus behavior is unchanged." The rule still sits only in Bitcoin Core's standardness checks and is not a consensus rule. The same notes say that miners can still cooperate to break transactions.
The SegWit Fix
SegWit did not close the vectors one by one. It took signatures out of the TXID calculation. Signatures and public keys move to a separate area called the witness, and the TXID is computed over the traditional serialization, which has no witness in it. The hash that does include the witness is defined separately as the wtxid.
BIP-141 gives its reasons for calling this superior to canonical-signature rules such as BIP-62.
- It prevents involuntary malleability for any type of script, as long as all inputs are signed.
- In an m-of-n multisig, a transaction is malleable only with the agreement of m key holders. Under BIP-62 a single key holder was enough.
- It prevents malleability from ECDSA quirks that are not yet known.
The 2-of-2 refund described above now works. The funding transaction's TXID is known before it is signed, and adding the signatures does not change it. BIP-141 calls this a trust-free unconfirmed transaction dependency chain and describes it as a fundamental building block of payment networks such as the Lightning Network.
What Is Still Malleable
SegWit removed third-party malleability for transactions that spend only SegWit inputs. The following remain.
- A transaction with legacy inputs, such as P2PKH addresses starting with 1, still has its scriptSig in the TXID. Variants like high-S signatures are filtered by relay policy but are not forbidden by consensus.
- Whoever signed can always sign again and produce a transaction with a different TXID. A fee bump with RBF changing the TXID belongs in the same category.
- The wtxid can still change. Transactions refer to each other by TXID, so follow-up transactions are not affected.
Schnorr signatures, used by Taproot, are not malleable in themselves. BIP-340 states that while ECDSA signatures are inherently malleable, non-malleability of Schnorr signatures follows from their security proof.
In practice the rule is not to track an unconfirmed transaction by its TXID alone. Credit a deposit after it is in a block, and do not build pre-signed transactions on the outputs of an unconfirmed transaction that has legacy inputs.
Related Concepts
- SegWit - the upgrade that separated signatures from the TXID calculation
- What Is a TXID? - how the transaction identifier is made
- Schnorr Signatures - a signature scheme that is not malleable
- Lightning Network - a payment network built on chains of pre-signed transactions