Home › Learn › SegWit explained: Segregated Witness and its BIPs

SegWit explained: Segregated Witness and its BIPs

SegWit (Segregated Witness) is the soft fork that moved signatures and other unlocking data out of the part of a transaction its ID is computed from, into a separate "witness" section. That fixed third-party transaction malleability, replaced the 1 MB block size limit with a limit of 4 million weight units, and added versioned outputs that later made Taproot possible. It is defined mainly in BIP 141 and activated at block 481,824 on 24 August 2017.

Updated · 9 min read

What is SegWit?

Every transaction input carries data that proves the spender is allowed to spend: signatures, public keys and sometimes a script. That data is called the witness. Before SegWit it lived inside each input's scriptSig, so it was part of the bytes hashed into the transaction ID (txid). SegWit moves it into a separate structure that blocks commit to in a different place and that the txid does not cover.

BIP 141, written by Eric Lombrozo, Johnson Lau and Pieter Wuille, defines the consensus rules. It describes the witness as data "required to check transaction validity but not required to determine transaction effects." Who paid whom and how much stays in the main transaction; the proof that the payment was authorized moves out.

SegWit is a soft fork: it only tightens the rules, so nodes that never upgraded still accept the blocks. To an old node a SegWit output looks like a script anyone could spend, and it receives blocks without the witness data. Upgraded nodes enforce the full rules. BIP 141 notes that wallets "should always be wary of anyone-can-spend scripts", one reason node operators were encouraged to upgrade.

The problem SegWit solved: transaction malleability

Transaction malleability means someone other than the signer can change a transaction's bytes, and so its txid, without making it invalid. ECDSA signatures are malleable by nature: as BIP 340 puts it, a third party "can alter an existing valid signature for a given public key and message into another signature that is valid for the same key and message." There were other vectors too, such as the unused "dummy" element that OP_CHECKMULTISIG consumes.

A changing txid matters because a transaction that spends an unconfirmed output refers to it by txid. If the parent's txid changes before it confirms, every pre-signed child becomes invalid. That breaks any protocol that builds chains of pre-signed transactions, such as payment channels. BIP 141 says SegWit "allows creation of unconfirmed transaction dependency chains without counterparty risk, an important feature for offchain protocols such as the Lightning Network."

Earlier work patched individual vectors: BIP 62 (Pieter Wuille) proposed canonical-encoding rules to rule out each known vector, and BIP 66 made strict DER signature encoding a consensus rule in 2015. SegWit took a different route. Since signatures are no longer hashed into the txid at all, changing them cannot change the txid, for any script type "as long as all inputs are signed". BIP 147 by Johnson Lau closed the dummy-element gap in the same deployment by requiring that element to be empty (the NULLDUMMY rule), which keeps the witness txid stable too.

What moved to the witness

Two IDs: txid and wtxid

After SegWit every transaction has two identifiers:

  • txid: double SHA-256 of [nVersion][txins][txouts][nLockTime]. The definition did not change, so old software computes the same IDs.
  • wtxid: double SHA-256 of [nVersion][marker][flag][txins][txouts][witness][nLockTime], which includes the witness. The marker is 0x00 and the flag 0x01.

Blocks commit to every wtxid through a second Merkle tree, whose root goes into an OP_RETURN output of the coinbase transaction starting with 0x6a24aa21a9ed. Putting the commitment in the coinbase, rather than the block header, is what kept SegWit a soft fork.

Witness programs and version numbers

A SegWit output script is a version number (OP_0 to OP_16) followed by a 2 to 40 byte "witness program". BIP 141 gives meaning to version 0 with two program sizes:

Output type Witness program Witness when spending Address
P2WPKH 20-byte hash of a public key signature and public key bc1q, 42 characters
P2WSH 32-byte SHA-256 of a script inputs and the script (up to 10,000 bytes) bc1q, 62 characters

Versions 1 to 16 were left undefined and spendable by anyone under the old rules, so a later soft fork could give them meaning without disturbing old nodes. Version 1 became Taproot in 2021 (BIP 341). See BIP 141 vs BIP 341 for how the two compare.

Block weight, weight units and vbytes

SegWit replaced the 1,000,000-byte block size limit with a limit on block weight:

  • Block weight = base size × 3 + total size, where base size excludes witness data and total size includes it.
  • The rule: block weight must be at most 4,000,000 weight units (WU).

Put another way, each non-witness byte counts as 4 WU and each witness byte as 1 WU. An old node only sees the base size, so to it every valid block is still at most 1 MB. BIP 141 explains why it chose one combined limit instead of two separate ones (1 MB of base data plus 3 MB of witness): two limits would make fee estimation and block building "nearly impossible".

For single transactions BIP 141 defines virtual size: weight divided by 4, rounded up, measured in vbytes (vB). Fee rates are quoted in satoshis per vbyte, so a witness byte costs a quarter of what a non-witness byte costs. Typical sizes for a payment with one input and two outputs:

Paying from Weight Virtual size
Legacy P2PKH (address starts with 1) about 904 WU about 226 vB
Nested SegWit, P2SH-P2WPKH (starts with 3) about 662 WU about 166 vB
Native SegWit, P2WPKH (starts with bc1q) about 562 WU about 141 vB

Exact numbers depend on signature length and output types. The transaction size calculator works them out for any mix of inputs and outputs.

The SegWit BIPs

SegWit is a group of BIPs. BIPs 141, 143 and 147 were the consensus changes, deployed together; the others cover networking, mining, addresses and wallets.

BIP What it defines Authors
141 Witness structure, wtxid, witness commitment, witness programs, block weight Eric Lombrozo, Johnson Lau, Pieter Wuille
143 New signature hash for version 0 witness programs Johnson Lau, Pieter Wuille
144 Peer-to-peer serialization, the NODE_WITNESS service bit and witness inv types Eric Lombrozo, Pieter Wuille
145 getblocktemplate changes so miners can build SegWit blocks Luke Dashjr
147 NULLDUMMY rule against multisig dummy-element malleability Johnson Lau
173 Bech32 addresses (bc1) for native witness outputs Pieter Wuille, Greg Maxwell
49 HD wallet derivation for nested SegWit accounts Daniel Weigl
84 HD wallet derivation for native SegWit accounts Pavol Rusnak

BIP 143: a faster, safer signature hash

BIP 143 changed what a signature commits to, for SegWit version 0 only. It fixed two weaknesses of the original algorithm:

  1. Quadratic hashing. Every signature check used to rehash most of the transaction, so the work grew with the square of the transaction size. BIP 143 notes that a 1 MB block normally took about 2 seconds to verify in 2015, but a 1 MB transaction with 5,569 signature operations could take 25 seconds. The new algorithm hashes the prevouts, sequences and outputs once and reuses them, making the work linear.
  2. Unsigned input amounts. The old signature did not cover the value being spent, so an offline signer such as a hardware wallet could not be sure of the fee without the full previous transactions. BIP 143 signs the amount, so if a wallet is fed a wrong value the signature is simply invalid.

Native SegWit vs nested SegWit

When SegWit launched, many wallets and services could not yet pay to the new bc1 addresses. Nested SegWit wraps a SegWit program inside an ordinary P2SH output, so it gets a "3" address that every wallet could already pay. BIP 141 calls it "fully transparent and backward compatible for all Bitcoin reference client since version 0.6.0", at the cost of a 23-byte larger scriptSig that is not discounted.

Nested SegWit Native SegWit
Script P2SH-P2WPKH P2WPKH
Address starts with 3 bc1q
Address encoding Base58Check Bech32 (BIP 173)
Standard derivation path m/49'/0'/0' (BIP 49) m/84'/0'/0' (BIP 84)
Extended public key prefix ypub zpub
Typical 1-in, 2-out payment about 166 vB about 141 vB

Bech32 addresses are all one case, at most 90 characters long, and their checksum is guaranteed to detect any error affecting up to 4 characters. More detail is in BIP 49 vs BIP 84, the derivation paths guide and the address types guide. The address checker tells you which type an address is.

How SegWit activated

SegWit was deployed with BIP 9 version bits, named "segwit" on bit 1. Its activation was the most contested upgrade so far, and accounts of 2017 still differ on which pressures mattered most. The main events, from the BIP texts:

When What happened
15 November 2016 BIP 9 signaling starts. Lock-in needs 95% of a 2,016-block period (1,916 blocks); timeout 15 November 2017.
March 2017 BIP 148 (Shaolin Fry) proposes a user-activated soft fork: from 1 August 2017, nodes running it would reject blocks not signaling for SegWit.
May 2017 BIP 91 (James Hilliard) proposes "segsignal" on bit 4: once 80% of a 336-block window signals, blocks not signaling bit 1 are rejected.
July 2017 BIP 91 locks in and takes effect, so blocks signal for SegWit.
August 2017 SegWit's BIP 9 deployment locks in (block 479,808).
24 August 2017 SegWit activates at block 481,824.

Signaling had stayed well below 95% for months. Some users and businesses backed BIP 148, some miners and companies backed BIP 91, and some parties tied SegWit to a later block size increase. Separately, on 1 August 2017 a group that preferred larger blocks without SegWit split off through a hard fork, creating Bitcoin Cash. The lessons of 2017 shaped later activation proposals such as BIP 8 and the Speedy Trial method used for Taproot; the soft forks guide and BIP 8 vs BIP 9 cover them.

What SegWit made possible

  • Lightning and other payment channels, which rely on pre-signed transactions whose txids cannot change. See the BIPs behind Lightning.
  • More capacity without a hard fork, through the weight limit.
  • Safer offline signing, since BIP 143 signatures commit to the amounts being spent.
  • Script upgrades by version number, which is how Taproot arrived as witness version 1.
  • Cheaper, easier addresses: bech32 is case-insensitive and fits QR codes better than Base58.

The full list of SegWit-related BIPs is on the SegWit topic page.

Frequently asked questions

Is SegWit a soft fork or a hard fork?

SegWit is a soft fork. It only adds restrictions, so blocks that follow the new rules are still valid to nodes that never upgraded. Old nodes see SegWit outputs as spendable by anyone and receive blocks with the witness data stripped, while upgraded nodes check the signatures in the witness. That is why SegWit could activate without splitting the network between old and new software.

What is the difference between native SegWit and nested SegWit?

Native SegWit (P2WPKH) uses bc1q addresses and puts the witness program directly in the output. Nested SegWit (P2SH-P2WPKH) wraps the same program in a P2SH output with a 3 address, which older wallets could pay before they supported bc1 addresses. Each nested input costs about 23 vbytes more, so native SegWit is cheaper. BIP 84 and BIP 49 define their wallet derivation paths.

Did SegWit increase the block size?

Yes, indirectly. SegWit replaced the 1 MB limit with a limit of 4 million weight units, where witness bytes count as 1 unit and other bytes as 4. Blocks with SegWit transactions can therefore be larger than 1 MB in total, while old nodes, which only see the non-witness part, still see blocks of at most 1 MB.

When was SegWit activated?

SegWit (BIPs 141, 143 and 147) activated at block 481,824 on 24 August 2017. It was signaled with BIP 9 version bits on bit 1, locked in during August 2017 after BIP 91 had made SegWit signaling mandatory for participating miners, and BIP 148 had set a user-activated deadline of 1 August 2017.

What is a vbyte?

A vbyte (virtual byte) is a transaction's weight divided by 4, as defined in BIP 141. Non-witness bytes count as 4 weight units and witness bytes as 1, so a transaction with lots of witness data has fewer vbytes than raw bytes. Fee rates are quoted in satoshis per vbyte, which is why SegWit transactions pay lower fees for the same payment.

BIPs in this article

Learn more

Guides

Free tools

Glossary