BIP 0141 + 0143
Why does a Bitcoin transaction have two identifiers?
SegWit separates what a transaction does from the proof that it is allowed, and gives the transaction a second name that also covers the proof. Signing and block space changed along the way.
Every transaction is known by a hash of its bytes. Before SegWit, those bytes included the signatures, so a change in how a transaction was signed could change its identifier without changing anything it did.12
BIP 141, a consensus soft fork assigned in 2015 and recorded as deployed, separates the data that decides what a transaction does from the data that proves it is allowed. Each transaction now has two identifiers. BIP 143, its companion, changes what a SegWit signature commits to. This chapter takes a transaction published in BIP 143 apart to see both.345
Source details
This chapter is an independent explanation, not the proposals themselves. Preamble fields are shown as recorded in the pinned files, with author e-mail addresses omitted. Status is the proposal’s own field, not an endorsement or a sign of consensus.
BIP 141: Segregated Witness (Consensus layer)
- BIP
- 141
- Layer
- Consensus (soft fork)
- Title
- Segregated Witness (Consensus layer)
- Authors
- Eric Lombrozo
Johnson Lau
Pieter Wuille - Status
- Deployed
- Type
- Specification
- Assigned
- 2015-12-21
- License
- PD
BIP 143: Transaction Signature Verification for Version 0 Witness Program
- BIP
- 143
- Layer
- Consensus (soft fork)
- Title
- Transaction Signature Verification for Version 0 Witness Program
- Authors
- Johnson Lau
Pieter Wuille - Status
- Deployed
- Type
- Specification
- Assigned
- 2016-01-03
- License
- PD
txiddouble SHA-256 of 233 bytes
09462d604a3102fae083…2a1a15e8wtxiddouble SHA-256 of 343 bytes
62b709c9126ae7782ea8…37386cc3What a transaction does, and what proves it
BIP 141 starts from a distinction. A transaction’s effects are fully determined by which outputs it spends and which new outputs it creates. Signatures, and the other data used to check that a spend is allowed, are needed to validate the chain, not to determine its resulting state.1
SegWit moves that validation data, in particular scripts and signatures, into a new structure called the witness. Blocks commit to it separately from the transaction tree, through a commitment placed in the coinbase transaction.48
Two serializations, two hashes
The txid is defined exactly as before: the double SHA-256 of nVersion, the inputs, the outputs and nLockTime. The wtxid is the double SHA-256 of a longer serialization: the same fields plus a marker byte 0x00 and a flag byte 0x01 after the version, and the witness data just before nLockTime.510
Each input gets one witness field: a count of stack items, then each item preceded by its length. An input that does not spend a witness program still gets a field, an empty one written as the single byte 0x00. When no input has witness data, the wtxid equals the txid.1112
Why have a second identifier at all? Because blocks that contain witness data must commit to it too. BIP 141 adds a rule requiring a commitment to the wtxids of a block’s transactions, built like the existing merkle root and recorded in an output of the coinbase transaction. The coinbase’s own wtxid is simply taken to be all zeros.13
The figure below shows the native P2WPKH example from BIP 143, a transaction with one ordinary input and one SegWit input. Switch lenses to see which bytes each identifier covers.714
Static view of the first example through the txid lens: bytes the txid does not cover are marked. With JavaScript you can switch to the wtxid and BIP 143 signing lenses and to the second example.
- nVersion
010000004 Bhashed - marker segwit marker
001 Bnot in txid - flag segwit marker
011 Bnot in txid - input count
021 Bhashed - input 0 · outpoint
fff7f788 1a8099af a6940d42 d1e7f636 2bec3817 1ea3edf4 33541db4 e4ad969f 0000000036 Bhashed - input 0 · scriptSig
49483045 0221008b 9d1dc26b a6a9cb62 127b0274 2fa9d754 cd3bebf3 37f7a55d 114c8e5c dd30be02 2040529b 194ba3f9 281a99f2 b1c0a19c 0489bc22 ede944cc f4ecbab4 cc618ef3 ed0174 Bhashed - input 0 · nSequence
eeffffff4 Bhashed - input 1 · outpoint
ef51e1b8 04cc89d1 82d27965 5c3aa89e 815b1b30 9fe287d9 b2b55d57 b90ec68a 0100000036 Bhashed - input 1 · scriptSig
001 Bhashed - input 1 · nSequence
ffffffff4 Bhashed - output count
021 Bhashed - output 0 · amount
202cb206 000000008 Bhashed - output 0 · scriptPubKey
1976a914 8280b37d f378db99 f66f85c9 5a783a76 ac7a6d59 88ac26 Bhashed - output 1 · amount
9093510d 000000008 Bhashed - output 1 · scriptPubKey
1976a914 3bde42db ee7e4dbe 6a21b2d5 0ce2f016 7faa8159 88ac26 Bhashed - witness 0 · 0 items witness
001 Bnot in txid - witness 1 · 2 items witness
02473044 02203609 e17b84f6 a7d30c80 bfa610b5 b4542f32 a8a0d544 7a12fb13 66d7f01c c44a0220 573a954c 45183315 61406f90 300e8f33 58f51928 d43c212a 8caed02d e67eebee 01210254 76c2e831 88368da1 ff3e292e 7acafcdb 3566bb0a d253f62f c70f07ae ee6357107 Bnot in txid - nLockTime
110000004 Bhashed
txid = double SHA-256 of the 233 marked bytes
09462d604a3102fae083ada369c795855a28db4bdd3d05358a361cf32a1a15e8wtxid: 62b709c9126ae778…37386cc3
- Base size
- 233 bytes
- Total size
- 343 bytes
- Weight
- 3 × 233 + 343 = 1042
- Virtual size
- ⌈1042 ÷ 4⌉ = 261 vbytes
Hashes are shown in the byte order they are computed. Inputs: P2PK (legacy); P2WPKH (witness v0).
Transaction: BIP 143, line 190 (Example: Native P2WPKH). Serialization view only; this page does not check signatures or scripts.
Why a stable name matters
Because the signature data of SegWit inputs is no longer part of the txid, changing how such an input was signed no longer changes the transaction’s identifier. BIP 141 presents this as the end of nonintentional malleability, under conditions: every input must be a witness input signed with at least one CHECKSIG or CHECKMULTISIG, and a multisig spend can still be changed if enough of its key holders agree.2
Stable identifiers matter when one transaction spends another before either is confirmed. BIP 141 names the benefit directly: chains of unconfirmed transactions without counterparty risk, which it calls important for off-chain protocols such as the Lightning Network.16
The mixed example also shows where the protection stops. Its first input is an ordinary pay-to-public-key spend, so that input’s signature is still in the scriptSig and still part of the txid.514
Not every witness item is a signature
For a P2WPKH input, the witness must be exactly two items: a signature, and a public key whose HASH160 matches the 20-byte program. A P2WSH input’s witness is different again: the stack items its script needs, followed by the script itself. BIP 141 says witness data is not script: the items are data, even when one of them is a serialized witness script. Nor are they all signatures.111517
The second example in the figure wraps a witness program inside an older P2SH script. Its scriptSig carries the redeem script that contains the program, so that single input has both a scriptSig and a witness.1418
Weight instead of bytes
Separating the witness had a second purpose. BIP 141’s motivation notes that moving data into a structure older nodes do not know about lets a soft fork discount it when calculating block size.19
When BIP 141 was written, blocks were limited to 1,000,000 bytes. It replaced that rule with block weight: three times the base size plus the total size, at most 4,000,000. Base size counts the bytes a pre-SegWit node would see; total size counts everything, witness included.620
Written another way, every base byte costs four weight units, and every witness-related byte, including the marker and flag, costs one. BIP 141 explains why it chose a single combined limit rather than two separate ones: separate limits would make mining and fee estimation nearly impossible.2122
Weight is therefore not a byte count. Since base bytes count four times, no more than 1,000,000 of them fit in a block, as before. A block can exceed that many bytes in total only through its witness data.623
Legacy + SegWit inputs · P2PK and P2WPKH
3 × 233 + 343 = 1042 weight units · ⌈1042 ÷ 4⌉ = 261 vbytes
SegWit inside P2SH · P2SH-P2WPKH
3 × 142 + 251 = 677 weight units · ⌈677 ÷ 4⌉ = 170 vbytes
BIP 141 defines transaction weight the same way, and virtual size as weight divided by four, rounded up. For the mixed example that is 233 base bytes and 343 bytes in total: 3 × 233 + 343 = 1,042 weight units, or 261 virtual bytes. These per-transaction terms are suggested vocabulary; the consensus limit applies to blocks.62426
Going deeper: what a SegWit signature signs
BIP 143 changes what a signature in a version 0 witness program commits to. Under the original algorithm, each signature hashed data in proportion to the size of the whole transaction, so total hashing grew with the square of the number of signature checks. BIP 143 instead builds a preimage with a fixed structure of ten items, three of which are summary hashes that can be reused across inputs.27282930
For SIGHASH_ALL, the three summary hashes cover every input’s outpoint, every input’s nSequence and every output. They are computed once and reused for each input that is signed, which is where the saving comes from.3031
The sixth item matters most to small signing devices: the amount of the output being spent. That amount is not in the transaction at all. Without it, an offline signer cannot work out how much is being spent or what fee is being paid. BIP 143 makes each signature commit to its own input’s amount, so if a device is told a wrong amount for that input, the signature is invalid and, in the BIP’s words, no funding might be lost. That protects only the input being signed: knowing the fee needs a trustworthy amount for every input.3233
Use the BIP 143 lens in the figure to see where each item comes from. For a P2WPKH input the scriptCode is a standard pay-to-public-key-hash script built from the 20-byte program. The page reproduces the published preimage and digest of both examples byte for byte.72534
All of this applies only to version 0 witness programs. Each signature covers its own input’s amount. In the mixed example, the first input is an ordinary spend signed with the original algorithm, which does not involve the amount at all.14323536
Two names, two kinds of content
Every transaction has two identifiers, equal when it carries no witness data. The txid names what the transaction does, what it spends and what it creates, plus any proof still kept in scriptSigs. The wtxid adds the witness: the rest of the evidence that the spending was allowed.15
Evidence
Each numbered marker in the text points here. Quotes are verbatim from the BIP files at commit
3a10b5b, including their wiki markup; links open
the exact lines; the quoted text sits under each entry. Labels say what kind of statement each is: a rule, the author’s rationale, history,
a test vector, or our own inference.
-
Author’s rationale A transaction's effects are determined by the outputs it spends and creates; signatures are only needed to validate the chain state, not to determine it.
Quoted source text (1)
The entirety of the transaction's effects are determined by output consumption (spends) and new output creation. Other transaction data, and signatures in particular, are only required to validate the blockchain state, not to determine it.
-
Author’s rationale Signature data in the witness no longer affects the txid; BIP141 states this prevents involuntary malleability when all inputs are signed with at least one CHECKSIG or CHECKMULTISIG, while an m-of-n multisig spend remains malleable with agreement of m key holders.
BIP 141 L26 BIP 141 L27 BIP 141 L28
Quoted source text (3)
Since signature data is no longer part of the transaction hash, changes to how the transaction was signed are no longer relevant to transaction identification.
as long as all inputs are signed (with at least one CHECKSIG or CHECKMULTISIG operation)
In the case of an m-of-n CHECKMULTISIG script, a transaction is malleable only with agreement of m private key holders
-
History BIP141 (assigned 2015) and BIP143 (assigned 2016) are consensus soft-fork proposals whose preambles record the status Deployed.
BIP 141 L10 BIP 141 L3 BIP 141 L8 BIP 143 L9 BIP 143 L7
Quoted source text (5)
Assigned: 2015-12-21
Layer: Consensus (soft fork)
Status: Deployed
Assigned: 2016-01-03
Status: Deployed
-
Rule BIP141 defines a witness structure, committed to blocks separately from the transaction merkle tree, holding data needed to check validity but not to determine effects; scripts and signatures move into it.
Quoted source text (1)
This BIP defines a new structure called a "witness" that is committed to blocks separately from the transaction merkle tree. This structure contains data required to check transaction validity but not required to determine transaction effects. In particular, scripts and signatures are moved into this new structure.
-
Rule Each transaction has two IDs: the txid is the double SHA256 of [nVersion][txins][txouts][nLockTime]; the wtxid is the double SHA256 of [nVersion][marker][flag][txins][txouts][witness][nLockTime].
BIP 141 L41 BIP 141 L43 BIP 141 L45 BIP 141 L47 BIP 141 L49
Quoted source text (5)
Each transaction will have 2 IDs.
Definition of <code>txid</code> remains unchanged: the double SHA256 of the traditional serialization format:
[nVersion][txins][txouts][nLockTime]
A new <code>wtxid</code> is defined: the double SHA256 of the new serialization with witness data:
[nVersion][marker][flag][txins][txouts][witness][nLockTime]
-
Rule Block weight is base size × 3 + total size, and must be at most 4,000,000; base size excludes witness-related data as a non-upgraded node sees it.
BIP 141 L117 BIP 141 L119 BIP 141 L123
Quoted source text (3)
''Block weight'' is defined as ''Base size'' * 3 + ''Total size''.
''Base size'' is the block size in bytes with the original transaction serialization without any witness-related data, as seen by a non-upgraded node.
The new rule is ''block weight'' ≤ 4,000,000.
-
Test vector BIP143 publishes complete example transactions with their preimages and sighashes, and asks developers to test against them.
BIP 143 L137 BIP 143 L190 BIP 143 L250
Quoted source text (3)
developers should test their implementations against all the tests below.
The serialized signed transaction is:
The serialized signed transaction is:
-
Rule The witness is committed in a tree nested into the block's merkle root via the coinbase; the commitment is required when a block has witness data and optional otherwise.
BIP 141 L18 BIP 141 L63 BIP 141 L80
Quoted source text (3)
The witness is committed in a tree that is nested into the block's existing merkle root via the coinbase transaction
A new block rule is added which requires a commitment to the <code>wtxid</code>.
If all transactions in a block do not have witness data, the commitment is optional.
-
Author’s rationale Transmitting witness data is needed only for a peer that validates a transaction, not one that only checks its existence.
Quoted source text (1)
It is needed only if a peer is trying to validate a transaction instead of just checking its existence.
-
Rule The marker must be 0x00 and the flag a non-zero byte, currently 0x01.
Quoted source text (2)
The <code>marker</code> MUST be a 1-byte zero value: <code>0x00</code>.
The <code>flag</code> MUST be a 1-byte non-zero value. Currently, <code>0x01</code> MUST be used.
-
Rule Each input has a witness field: a var_int count of stack items, then each item with a var_int length. Witness data is not script.
Quoted source text (1)
Each txin is associated with a witness field. A witness field starts with a <code>var_int</code> to indicate the number of stack items for the txin. It is followed by stack items, with each item starts with a <code>var_int</code> to indicate the length. Witness data is NOT script.
-
Rule A non-witness-program input must have an empty witness field, written 0x00; if no input is a witness program, wtxid equals txid.
Quoted source text (1)
A non-witness program (defined hereinafter) txin MUST be associated with an empty witness field, represented by a <code>0x00</code>. If all txins are not witness program, a transaction's <code>wtxid</code> is equal to its <code>txid</code>.
-
Rule Blocks containing witness data must commit to the wtxids; the witness root is built like the merkle root, recorded in a coinbase output, and the coinbase's own wtxid is taken as all zeros.
BIP 141 L63 BIP 141 L65 BIP 141 L67 BIP 141 L80
Quoted source text (4)
A new block rule is added which requires a commitment to the <code>wtxid</code>. The <code>wtxid</code> of coinbase transaction is assumed to be <code>0x0000....0000</code>.
A <code>witness root hash</code> is calculated with all those <code>wtxid</code> as leaves, in a way similar to the <code>hashMerkleRoot</code> in the block header.
The commitment is recorded in a <code>scriptPubKey</code> of the coinbase transaction.
If all transactions in a block do not have witness data, the commitment is optional.
-
Test vector The native P2WPKH example spends one ordinary P2PK output and one P2WPKH output; the P2SH-P2WPKH example spends one P2SH-wrapped witness program.
BIP 143 L151 BIP 143 L155 BIP 143 L214
Quoted source text (3)
The first input comes from an ordinary P2PK:
The second input comes from a P2WPKH witness program:
The input comes from a P2SH-P2WPKH witness program:
-
Rule A version 0, 20-byte program is P2WPKH: the witness is exactly two items, a signature and a public key whose HASH160 matches the program.
BIP 141 L95 BIP 141 L96 BIP 141 L97
Quoted source text (3)
It is interpreted as a pay-to-witness-public-key-hash (P2WPKH) program.
The witness must consist of exactly 2 items (≤ 520 bytes each). The first one a signature, and the second one a public key.
The HASH160 of the public key must match the 20-byte witness program.
-
Author’s rationale BIP141 says this allows unconfirmed transaction dependency chains without counterparty risk, important for off-chain protocols such as the Lightning Network.
Quoted source text (1)
It allows creation of unconfirmed transaction dependency chains without counterparty risk, an important feature for offchain protocols such as the Lightning Network
-
Rule A version 0, 32-byte program is P2WSH: the witness is an input stack for the script followed by the serialized script itself.
Quoted source text (2)
It is interpreted as a pay-to-witness-script-hash (P2WSH) program.
The witness must consist of an input stack to feed to the script, followed by a serialized script (<code>witnessScript</code>).
-
Rule Witness validation is also triggered when a P2SH redeemScript pushed in the scriptSig is exactly a version byte plus a witness program.
Quoted source text (1)
Triggered when a <code>scriptPubKey</code> is a P2SH script, and the BIP16 <code>redeemScript</code> pushed in the <code>scriptSig</code> is exactly a push of a version byte plus a push of a witness program.
-
Author’s rationale BIP141's motivation lists that moving data into a structure unknown to old nodes lets a soft fork discount witness size when calculating block size.
Quoted source text (1)
'''Some constraints could be bypassed with a soft fork''' by moving part of the transaction data to a structure unknown to current protocol, for example: #* Size of witness could be ignored / discounted when calculating the block size, effectively increasing the block size to some extent
-
History When BIP141 was written, blocks were limited to 1,000,000 bytes total size.
Quoted source text (1)
Blocks are currently limited to 1,000,000 bytes (1MB) total size.
-
Editorial inference Since weight = 3 × base + total = 4 × base + (total − base), each base byte adds 4 weight units and each witness-related byte (marker, flag, witness) adds 1.
Quoted source text (2)
''Block weight'' is defined as ''Base size'' * 3 + ''Total size''.
including base data and witness data.
-
Author’s rationale BIP141 chose one composite limit because two separate limits would make mining and fee estimation nearly impossible.
Quoted source text (1)
Using two separate limits would make mining and fee estimation nearly impossible.
-
Editorial inference Because weight = 4 × base + witness ≤ 4,000,000, a block's base data cannot exceed 1,000,000 bytes; only witness data lets its total size exceed that.
Quoted source text (2)
''Block weight'' is defined as ''Base size'' * 3 + ''Total size''.
The new rule is ''block weight'' ≤ 4,000,000.
-
Rule Transaction weight is defined the same way, and virtual size is weight / 4 rounded up; these are suggested terms, not consensus limits.
BIP 141 L136 BIP 141 L140 BIP 141 L142
Quoted source text (3)
The following definitions are not used for consensus limits, but are suggested to provide language consistent with the terminology introduced above.
''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''
''Virtual transaction size'' is defined as ''Transaction weight'' / 4 (rounded up to the next integer).
-
Test vector The tested model reproduces both published examples' serializations, preimages and sighashes byte for byte, and computes their sizes and weights.
Quoted source text (2)
hash preimage:
sigHash:
-
Test vector Parsed, the native P2WPKH example has 233 base bytes and 343 bytes in total: weight 1,042, virtual size 261.
Quoted source text (2)
The serialized signed transaction is:
''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''
-
Rule BIP143 defines a new digest for signatures in version 0 witness programs, to minimize redundant hashing and to cover the input value.
Quoted source text (1)
This proposal defines a new transaction digest algorithm for signature verification in version 0 witness program, in order to minimize redundant data hashing in verification, and to cover the input value by the signature.
-
Author’s rationale With the original digest, hashing per signature grows with transaction size, so total hashing grows as O(n^2) in the number of sigops.
Quoted source text (1)
For the verification of each signature, the amount of data hashing is proportional to the size of the transaction. Therefore, data hashing grows in O(n<sup>2</sup>) as the number of sigops in a transaction increases.
-
Rule The BIP143 preimage has ten items: nVersion, hashPrevouts, hashSequence, outpoint, scriptCode, amount, nSequence, hashOutputs, nLocktime and sighash type.
BIP 143 L29 BIP 143 L34 BIP 143 L38
Quoted source text (3)
1. nVersion of the transaction (4-byte little endian)
6. value of the output spent by this input (8-byte little endian)
10. sighash type of the signature (4-byte little endian)
-
Rule hashPrevouts, hashSequence and hashOutputs can be reused across inputs, reducing total hashing from O(n^2) to O(n).
Quoted source text (1)
may be reused in other inputs of the same transaction, so that the time complexity of the whole hashing process reduces from O(n<sup>2</sup>) to O(n).
-
Rule Without ANYONECANPAY, hashPrevouts is the double SHA256 of all input outpoints; for SIGHASH_ALL, hashSequence covers all nSequence values and hashOutputs all outputs.
BIP 143 L58 BIP 143 L62 BIP 143 L66
Quoted source text (3)
If the <code>ANYONECANPAY</code> flag is not set, <code>hashPrevouts</code> is the double SHA256 of the serialization of all input outpoints;
If none of the <code>ANYONECANPAY</code>, <code>SINGLE</code>, <code>NONE</code> sighash type is set, <code>hashSequence</code> is the double SHA256 of the serialization of <code>nSequence</code> of all inputs;
If the sighash type is neither <code>SINGLE</code> nor <code>NONE</code>, <code>hashOutputs</code> is the double SHA256 of the serialization of all output amount (8-byte little endian) with <code>scriptPubKey</code> (serialized as scripts inside CTxOuts);
-
Rule Every BIP143 sighash type commits to the amount spent by the signed input; item 6 is that 8-byte amount.
Quoted source text (2)
All sighash types commit to the amount being spent by the signed input;
The item 6 is a 8-byte value of the amount of bitcoin spent in this input.
-
Author’s rationale Without the amount, an offline signer cannot compute the amount spent or the fee; with the input's amount in its digest, a wrong value makes that signature invalid and no funding might be lost.
Quoted source text (2)
For an offline transaction signing device ("cold wallet"), however, the unknowing of input amount makes it impossible to calculate the exact amount being spent and the transaction fee.
In the case that a wrong value is provided and signed, the signature would be invalid and no funding might be lost.
-
Rule For P2WPKH the scriptCode is 0x1976a914{20-byte-pubkey-hash}88ac.
Quoted source text (1)
For <code>P2WPKH</code> witness program, the <code>scriptCode</code> is <code>0x1976a914{20-byte-pubkey-hash}88ac</code>.
-
Rule The BIP143 digest applies only to sigops in version 0 witness programs.
Quoted source text (2)
A new transaction digest algorithm is defined, but only applicable to sigops in version 0 witness program:
a new transaction digest algorithm for signature verification in version 0 witness program.
-
Author’s rationale The original signature digest does not involve the amount being spent by the input.
Quoted source text (1)
The algorithm does not involve the amount of Bitcoin being spent by the input.