The Witness Stack for P2WSH
The Witness Stack for P2WSH
In a P2WSH spend, the ScriptSig is empty, and the entire logic of the multisig contract is moved to the Witness Stack. This includes the threshold, the keys, and the signatures.
1. The Stack Sequence
For a 2-of-3 multisig spend, the Witness Stack contains the following items in order:
-
OP_0 (The Multisig Dummy Value)
-
Signature 1
-
Signature 2
-
Witness Script (
OP_2 [Key1] [Key2] [Key3] OP_3 OP_CHECKMULTISIG)
2. The Verification Loop
-
Extraction: The node extracts the last item (the Witness Script) and hashes it.
-
Comparison: It compares the hash to the target in the ScriptPubKey.
-
Unwrapping: If they match, it "unwraps" the Witness Script and places the remaining stack items (
Dummy,Sig1,Sig2) into it for execution.
3. The Discount Advantage
Because the Witness Script (containing the bulky public keys) is now in the Witness area, it only costs 1 Weight Unit per byte.
-
In P2SH Multisig, the Witness Script was in the Redeem Script field (Input), costing 4 Weight Units per byte.
-
Moving the keys to the Witness area is the single biggest "Optimization Hack" in Bitcoin's history for organizational security.
4. Size Limits
Even though it is discounted, P2WSH still has limits:
-
The Witness Script cannot exceed 3,600 bytes.
-
The Witness Stack cannot exceed 502 items. These limits ensure that a single multisig spend cannot take up the entire block.
| Stack Position | Data | Cost (WU) |
|---|---|---|
| Bottom | Witness Script | 1 per byte |
| Middle | Signatures | 1 per byte |
| Top | Dummy Value | 1 per byte |
In the next section, we will compare the Fee Efficiency of different multisig types.
TeachMeBitcoin is an ad-free, open-source educational repository curated by a passionate team of Bitcoin researchers and educators for public benefit. If you found our articles helpful, please consider supporting our hosting and ongoing content updates with a clean donation: