BIP 0016
How can a sender pay without knowing the spending rules?
The recipient chooses the spending conditions. The sender pays to their script hash. The spender later reveals the script and satisfies its rules.
Suppose a recipient wants two signatures to spend a payment. Without P2SH, the sender would have to understand that arrangement and put the complete spending script in the output. BIP 16 lets the recipient supply a script hash instead, so the sender can use the same payment form for different spending conditions.123
BIP 16, assigned in 2012 and recorded as deployed, turned that around. The sender pays to a fixed-length 20-byte hash of a script. The receiver keeps the script and reveals it only when spending. The author’s stated purpose was to move responsibility for the spending conditions from the sender to the redeemer.145
Source details
This chapter is an independent explanation, not the proposal itself. Preamble fields are shown as the pinned files record them, without author e-mail addresses. Status is the proposal’s own field. It is not an endorsement or a sign of consensus.
BIP 16: Pay to Script Hash
- BIP
- 16
- Layer
- Consensus (soft fork)
- Title
- Pay to Script Hash
- Authors
- Gavin Andresen
- Status
- Deployed
- Type
- Specification
- Assigned
- 2012-01-03
Fig. A09.1 Twenty bytes now, the script later
Exact values
- Output script
a9140fb9463421696b82c833af241c78c17ddbde493487- Redeem script
5221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae- HASH160 of the redeem script (RIPEMD-160 of SHA-256)
0fb9463421696b82c833af241c78c17ddbde4934
A fixed shape
Every P2SH output has the same three parts: OP_HASH160, a push of exactly 20 bytes, and OP_EQUAL. That is 23 bytes whether the hidden conditions are one key or fifteen. A spend supplies the signatures or other data the script needs, followed by the script itself, serialized as one more piece of pushed data. BIP 16 calls that last item the redeemScript.238
Fig. A09.2 A script behind a fixed-size output
Exact output hashes
- Legacy P2SH 2-of-2
a9140fb9463421696b82c833af241c78c17ddbde493487
The BIP’s own example is the simplest case: a script that checks one signature against one key. The output holds the 20-byte hash of that script, and the spending scriptSig holds a signature and the script. Nothing in the output says what kind of script it is, or how many keys it has.19
The receiver must keep the full redeem script. Knowing its hash is enough to receive a payment, but spending requires the exact script bytes and data that make the script succeed.1210
Checked twice
BIP 16 gives the rules as three steps. First, the scriptSig may contain nothing but push operations; anything else fails. Second, normal validation runs. The pushed items become the stack and the output’s script hashes the top item. Validation fails at once if that hash differs from the one in the output.10
Third, and only for these outputs, the serialized script is popped off the stack, decoded, and run against whatever is left. The coin moves only if that second run also succeeds. The same pushed bytes are used twice: once as data to be hashed, once as a program.10
Start with the redeem script hidden, then reveal it and step through the spend. First watch the hash check, then watch the revealed script run. The script must match the commitment and satisfy its spending rules.6710
Fig. A09.3 One push, two evaluations
Static view: the published spend at its last stage. With JavaScript you can hide the redeem script and step through each check.
Before the spend: the output holds only the 20-byte hash 0fb94634…; the redeem script is not known yet.
Exact values
- Output script (23 bytes)
a9140fb9463421696b82c833af241c78c17ddbde493487
Published test vectors. Recorded at build time by the tested model; every signature verified (noble) against the digest it computed. Nothing here signs.
Legacy P2SH 2-of-2, stage 1 of 3: The scriptSig only pushes data. 4 pushes; the last is the serialized redeem script.
Exact values
- Output script (23 bytes)
a9140fb9463421696b82c833af241c78c17ddbde493487- Redeem script (71 bytes)
5221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae- HASH160 of the redeem script
0fb9463421696b82c833af241c78c17ddbde4934
BIP 174 line 833. Recorded at build time by the tested model; every signature verified (noble) against the digest it computed. Nothing here signs. BIP 16 sigops for this redeem script: 2.
Legacy P2SH 2-of-2, stage 2 of 3: Its hash matches the output. HASH160 of the serialized script equals the 20 bytes in the output.
Exact values
- Output script (23 bytes)
a9140fb9463421696b82c833af241c78c17ddbde493487- Redeem script (71 bytes)
5221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae- HASH160 of the redeem script
0fb9463421696b82c833af241c78c17ddbde4934
BIP 174 line 833. Recorded at build time by the tested model; every signature verified (noble) against the digest it computed. Nothing here signs. BIP 16 sigops for this redeem script: 2.
Legacy P2SH 2-of-2, stage 3 of 3: The redeem script runs on the remaining stack. Finishes with true on top: the spend is valid.
Exact values
- Output script (23 bytes)
a9140fb9463421696b82c833af241c78c17ddbde493487- Redeem script (71 bytes)
5221029583bf39ae0a609747ad199addd634fa6108559d6c5cd39b4c2183f1ab96e07f2102dab61ff49a14db6a7d02b0cd1fbb78fc4b18312b5b4e54dae4dba2fbfef536d752ae- HASH160 of the redeem script
0fb9463421696b82c833af241c78c17ddbde4934
BIP 174 line 833. Recorded at build time by the tested model; every signature verified (noble) against the digest it computed. Nothing here signs. BIP 16 sigops for this redeem script: 2.
Signature order in this example
Step through the legacy P2SH spend below. Its redeem script requires both keys to sign. Each signature must match a key in order. The tests behind this page swap the two signatures and the spend fails, even though both are valid signatures by the right keys. CHECKMULTISIG also takes one extra item off the stack; in this spend it is empty.711
Each stage can fail on its own. Change one byte of the redeem script inside the scriptSig and the hash no longer matches: the second stage stops the spend before the script ever runs. Put a non-push opcode into the scriptSig and the first stage stops it.1012
Fig. A09.4 Three ways to fail
Change one byte of the redeem script (its last, ae to af) and its hash no longer fits the lock: stage 2 fails.
Put a non-push opcode (OP_DUP, 0x76) in front of the scriptSig and stage 1 rejects it before anything runs.
Swap the two signatures: both are valid, but CHECKMULTISIG walks the keys in order and never goes back, so the script fails at stage 3.
Exact hashes
- HASH160 of the altered script
7497726d053d5706176c8f5905c1ce50b158c242- Hash in the output
0fb9463421696b82c833af241c78c17ddbde4934
A later use: compatibility with SegWit
BIP 141 later reused P2SH so that older wallets could pay SegWit recipients through an address they already understood. This is an application of BIP 16’s fixed payment form. The witness rules and weight savings come from SegWit.131415
Fig. A09.5 Where the signatures live
Later SegWit wrappers: scripts, signatures and weight
When SegWit arrived, BIP 141 reused this envelope. If the redeem script is exactly a witness version and a witness program, the output is a P2SH witness program. The scriptSig must hold nothing but that one push, and the real conditions move into the witness.14
The later comparison below shows two wrapped spends. In the P2SH-wrapped P2WPKH from BIP 143, the 22-byte redeem script wraps a 20-byte key-hash witness program; the witness holds a signature and the key. In the P2SH-wrapped P2WSH from BIP 174, the 34-byte redeem script wraps a 32-byte script-hash witness program, and the witness holds the signatures and the 2-of-2 script itself. Both redeem scripts begin with OP_0 and a one-byte push-length opcode: 14 for 20 bytes, 20 for 32 bytes, in hexadecimal. Their signatures commit to the BIP 143 digest, not the original one.6714
The difference shows in where the bytes sit. The legacy 2-of-2 puts 218 bytes in its scriptSig. The wrapped 2-of-2 needs a 35-byte scriptSig, and its witness serializes to 218 bytes. Since weight counts a byte outside the witness four times and a witness byte once, those bytes come to 872 weight units against 358, so moving the signatures makes the wrapped spend lighter.1516
The wrapper had a practical reason. BIP 141 lists, among the things a wallet that never upgraded can still do, sending bitcoin to upgraded wallets using a P2SH address. The payer sees an ordinary P2SH output; only the spender has to understand the witness.1314
Wrapped SegWit is still not native SegWit. The output is an ordinary 23-byte P2SH output and the scriptSig is not empty. BIP 141 notes that native P2WSH outputs use 34 bytes instead, and its authors chose the longer hash for better resistance to collision attacks.814
What fits inside the wrapper
Because the redeem script travels as pushed data, it obeys the 520-byte limit on any push. BIP 16 works the consequence through for multisig with 33-byte keys: three bytes plus 34 per key allows at most 15 keys, 513 bytes in all.17
Fig. A09.6 Why fifteen keys
Signature operations inside the redeem script still count toward the block’s limit, by scanning rather than running. A CHECKSIG counts as one. A CHECKMULTISIG counts as its key count when preceded by OP_1 to OP_16, and as 20 otherwise. The BIP’s examples come to 3 and 22. The author explains that the counting is meant to be quick, and that the per-block limit protects miners from blocks crafted to be slow to validate.1819
The original rollout and the dates recorded in BIP 16
The rest is history the BIP records. It replaced an earlier proposal, BIP 12’s OP_EVAL, and the author describes recognizing one special output form as ugly, but less so than the alternatives. Its rollout asked miners to mark their blocks with /P2SH/, and its rules were to apply to blocks timestamped from 1 April 2012; some earlier transactions in the chain fail them. Old nodes, the BIP notes, check only that the hash matches, which is why it also discusses a costly one-confirmation attack against them.20212223
The rollout plan in the text is concrete. To avoid a lasting split, more than half of miners had to switch to the new rules at once. On 1 February 2012 the previous week’s blocks would be counted: with 550 or more of roughly 1,000 carrying /P2SH/, full validation would start for blocks after 15 February. The rule section, as recorded, names 1 April 2012 instead; the BIP keeps both dates, and this chapter does not try to reconcile them. To old software, meanwhile, these spends were non-standard: it would typically neither relay nor mine them.212425
Relay policy versus validation
One more distinction matters. BIP 16 says spends are only standard, meaning relayed and mined by default, if the redeem script is itself one of the standard script types. That was relay policy as BIP 16 states it, separate from the three validation rules that decide whether a block containing the spend is valid.26
P2SH gives the sender a fixed payment form while leaving the recipient to choose the spending conditions. Spending later brings those conditions into view and proves they were satisfied.1310
Evidence
Each numbered marker in the text points here, and the ↩ links lead back to the text. Quotes are verbatim
from the BIP files at commit 3a10b5b, wiki markup
included. Links open the exact lines. Labels say what kind of statement each entry is: a rule, the
author’s rationale, history, a test vector, or our own inference.
-
↩ a b c d e Author’s rationale The author's aim is to move the job of supplying spending conditions from the sender to the redeemer, so a sender can fund any transaction, however complex, with a fixed 20-byte hash. The BIP records that some felt P2SH unnecessary, preferring to give the sender the complete script.
BIP 16 L17 BIP 16 L19 BIP 16 L66
Quoted source text (3)
The purpose of pay-to-script-hash is to move the responsibility for supplying the conditions to redeem a transaction from the sender of the funds to the redeemer.
The benefit is allowing a sender to fund any arbitrary transaction, no matter how complicated, using a fixed-length 20-byte hash that is short enough to scan from a QR code or easily copied and pasted.
several people feel that it is unnecessary, and complex/multisignature transaction types should be supported by simply giving the sender the complete {serialized script}.
-
↩ a b c d e f Rule It is redeemed by a scriptSig of signatures followed by the serialized script, also called the redeemScript.
Quoted source text (2)
...signatures... {serialized script}
the ''serialized script'' - also referred to as the ''redeemScript'' -
-
↩ a b c d e f Rule A P2SH output's script is OP_HASH160, a push of exactly 20 bytes, and OP_EQUAL.
Quoted source text (1)
OP_HASH160 [20-byte-hash-value] OP_EQUAL [20-byte-hash-value] shall be the push-20-bytes-onto-the-stack opcode (0x14) followed by exactly 20 bytes.
-
↩ History BIP 16 is a consensus soft-fork proposal by Gavin Andresen, assigned in 2012; its preamble records the status Deployed.
BIP 16 L3 BIP 16 L5 BIP 16 L6 BIP 16 L8
Quoted source text (4)
Layer: Consensus (soft fork)
Authors: Gavin Andresen
Status: Deployed
Assigned: 2012-01-03
-
↩ Rule BIP 16 describes a new standard transaction type and additional validation rules that apply only to it.
Quoted source text (1)
This BIP describes a new "standard" transaction type for the Bitcoin scripting system, and defines additional validation rules that apply only to the new transactions.
-
↩ a b c d Test vector BIP 174's published role trace ends in a transaction whose first input spends a legacy P2SH 2-of-2 multisig and whose second spends a P2SH-wrapped P2WSH 2-of-2; BIP 143 publishes a P2SH-wrapped P2WPKH example.
BIP 174 L833 BIP 143 L203 BIP 143 L215–216
Quoted source text (3)
0200000000010258e87a21b56daf0c23be8e7070456c336f7cbaa5c8757924f545887bb2abdd75
=== P2SH-P2WPKH ===
scriptPubKey : a9144733f37cf4db86fbc2efed2500b4f4e49f31202387, value: 10 redeemScript : 001479091972186c449eb1ded22b78e40d009bdf0089
-
↩ a b c d e f g h Test vector For all three pinned spends, this site's model finds the scriptSig push-only and the redeem script's HASH160 (computed as RIPEMD-160 of SHA-256) equal to the output's, and every signature verifies (with noble) against the digest the model computes: the original digest for the legacy spend, BIP 143's for the wrapped ones.
Quoted source text (1)
# Validation fails if there are any operations other than "push data" operations in the scriptSig.
-
↩ a b Author’s rationale BIP 141 notes that a native P2WSH output takes 34 bytes against BIP 16's 23, a size its authors chose for better collision resistance.
Quoted source text (1)
The scriptPubKey occupies 34 bytes, as opposed to 23 bytes of BIP16 P2SH. The increased size improves security against possible collision attacks, as 280 work is not infeasible anymore
-
↩ Rule The BIP's example is a one-signature script: the scriptSig holds a signature and the serialized {pubkey OP_CHECKSIG}, and the output holds its 20-byte hash.
Quoted source text (1)
scriptSig: [signature] {[pubkey] OP_CHECKSIG} scriptPubKey: OP_HASH160 [20-byte-hash of {[pubkey] OP_CHECKSIG} ] OP_EQUAL
-
↩ a b c d e f g h i Rule Validation fails if the scriptSig has anything but push operations; normal validation then hashes the serialized script and fails at once if it does not match the output; finally the serialized script is popped and the transaction is validated again with the remaining stack and the deserialized script.
Quoted source text (1)
# Validation fails if there are any operations other than "push data" operations in the scriptSig. # Normal validation is done: an initial stack is created from the signatures and {serialized script}, and the hash of the script is computed and validation fails immediately if it does not match the hash in the outpoint. # {serialized script} is popped off the initial stack, and the transaction is validated again using the popped stack and the deserialized script as the scriptPubKey.
-
↩ a b c Test vector In the legacy 2-of-2 spend, signature 1 matches key 1 and signature 2 matches key 2; with the two signatures swapped the redeem script fails, because the model's CHECKMULTISIG, like Bitcoin Core's, walks the keys in order. CHECKMULTISIG also pops one extra item, which here is empty.
Quoted source text (1)
the transaction is validated again using the popped stack and the deserialized script as the scriptPubKey.
-
↩ a b Test vector Changing one byte of the redeem script inside the scriptSig makes its hash differ from the output and validation fails at the second stage; adding a non-push opcode to the scriptSig fails the first.
Quoted source text (1)
the hash of the script is computed and validation fails immediately if it does not match the hash in the outpoint.
-
↩ a b Rule BIP 141 lists sending to upgraded wallets using a P2SH address among the things a non-upgraded wallet can still do.
Quoted source text (2)
What a non-upgraded wallet can do
Sending bitcoin to upgraded wallets using a P2SH address
-
↩ a b c d e f Rule Under BIP 141, if the redeemScript is exactly a version push opcode, a one-byte push-length opcode and the witness-program bytes, it is a P2SH witness program: the scriptSig must be exactly a push of the redeemScript, and the witness carries the rest; a native witness program instead requires an exactly empty scriptSig. Signatures in version 0 witness programs use BIP 143's digest.
BIP 141 L92 BIP 141 L91 BIP 143 L27 BIP 141 L86–88
Quoted source text (4)
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. The <code>scriptSig</code> must be exactly a push of the BIP16 <code>redeemScript</code> or validation fails.
The scriptSig must be exactly empty or validation fails.
A new transaction digest algorithm is defined, but only applicable to sigops in version 0 witness program:
* First, byte 0x00 (<code>OP_0</code>) or any byte between 0x51 (<code>OP_1</code>) and 0x60 (<code>OP_16</code>) inclusive (the version byte). * Then, a byte ''L'' between 0x02 (push of 2 bytes) and 0x28 (push of 40 bytes) inclusive. * Finally, ''L'' arbitrary bytes (the witness program).
-
↩ a b c Rule Transaction weight is base size × 3 plus total size, so a byte outside the witness counts four times and a witness byte once.
Quoted source text (1)
''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''
-
↩ a b Test vector Measured on the pinned transactions: the legacy spend's scriptSig is 218 bytes with an empty witness; the P2SH-P2WSH spend's scriptSig is 35 bytes and its witness serializes to 218 bytes (item count and length prefixes included); the P2SH-P2WPKH spend's are 23 and 107. Counting scriptSig bytes four times and witness bytes once gives 872 against 358 weight units for the two 2-of-2 spends.
Quoted source text (1)
''Transaction weight'' is defined as ''Base transaction size'' * 3 + ''Total transaction size''
-
↩ a b Rule Because the serialized script is pushed like any other data, it cannot exceed 520 bytes, so a P2SH multisig with 33-byte keys can have at most 15 keys: 3 + 15 × 34 = 513 bytes.
Quoted source text (2)
no data greater than 520 bytes may be pushed to the stack. Thus it is not possible to spend a P2SH output if the redemption script it refers to is >520 bytes in length.
it is only possible to spend a P2SH output requiring a maximum of 15 pubkeys to redeem: 3 bytes + 15 pubkeys * 34 bytes/pubkey = 513 bytes
-
↩ Rule Signature operations in the serialized script count toward the block limit: CHECKSIG as 1, CHECKMULTISIG after OP_1–OP_16 as that number, and any other CHECKMULTISIG as 20, whether or not they run; the BIP's examples count 3 and 22.
BIP 16 L48–52 BIP 16 L56–57 BIP 16 L59–60
Quoted source text (3)
Signature operations in the {serialized script} shall contribute to the maximum number allowed per block (20,000) as follows: # OP_CHECKSIG and OP_CHECKSIGVERIFY count as 1 signature operation, whether or not they are evaluated. # OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY immediately preceded by OP_1 through OP_16 are counted as 1 to 16 signature operation, whether or not they are evaluated. # All other OP_CHECKMULTISIG and OP_CHECKMULTISIGVERIFY are counted as 20 signature operations.
+3 signature operations: {2 [pubkey1] [pubkey2] [pubkey3] 3 OP_CHECKMULTISIG}
+22 signature operations: {OP_CHECKSIG OP_IF OP_CHECKSIGVERIFY OP_ELSE OP_CHECKMULTISIGVERIFY OP_ENDIF}
-
↩ Author’s rationale The author explains the counting rule is meant to be quick to apply by scanning the script, and that the block limit guards miners against denial-of-service blocks.
Quoted source text (1)
The signature operation counting rules are intended to be easy and quick to implement by statically scanning the {serialized script}. Bitcoin imposes a maximum-number-of-signature-operations per block to prevent denial-of-service attacks on miners.
-
↩ Author’s rationale BIP 16 replaced BIP 12's proposed OP_EVAL; the author describes recognizing one special output form as ugly but less so than the alternatives, and notes the motivation was controversial.
BIP 16 L64 BIP 16 L66 BIP 16 L68
Quoted source text (3)
This BIP replaces BIP 12, which proposed a new Script opcode ("OP_EVAL") to accomplish everything in this BIP and more.
The Motivation for this BIP (and BIP 13, the pay-to-script-hash address type) is somewhat controversial
Recognizing one 'special' form of scriptPubKey and performing extra validation when it is detected is ugly. However, the consensus is that the alternatives are either uglier, are more complex to implement, and/or expand the power of the expression language in dangerous ways.
-
↩ a b History BIP 16 says its rules should apply only to blocks with timestamps from 1 April 2012, and that earlier transactions in the chain fail them; its rollout plan counted blocks carrying "/P2SH/" in the coinbase.
BIP 16 L41 BIP 16 L41 BIP 16 L94
Quoted source text (3)
These new rules should only be applied when validating transactions in blocks with timestamps >= 1333238400 (Apr 1 2012)
There are transactions earlier than 1333238400 in the block chain that fail these new validation rules.
miners are asked to upgrade their software and put the string "/P2SH/" in the input of the coinbase transaction for blocks that they create.
-
↩ Rule Old implementations check only that the serialized script's hash matches, and do no other validation, when they validate blocks from upgraded software.
Quoted source text (1)
Old implementations will validate that the {serialize script}'s hash value matches when they validate blocks created by software that fully support this BIP, but will do no other validation.
-
↩ Author’s rationale The BIP describes a 1-confirmation attack on old implementations, which the author judges expensive and difficult in practice.
Quoted source text (1)
There is a 1-confirmation attack on old implementations, but it is expensive and difficult in practice.
-
↩ History To avoid a lasting split, BIP 16 says more than 50% of miners had to enforce the new rules at the same time; its plan was to count blocks marked /P2SH/ over the week before 1 February 2012 and, with 550 or more of roughly 1,000, fully validate these spends in blocks after 15 February 2012.
Quoted source text (2)
more than 50% of miners must support full validation of the new transaction type and must switch from the old validation rules to the new rules at the same time.
On February 1, 2012, the block-chain will be examined to determine the number of blocks supporting pay-to-script-hash for the previous 7 days. If 550 or more contain "/P2SH/" in their coinbase, then all blocks with timestamps after 15 Feb 2012, 00:00:00 GMT shall have their pay-to-script-hash transactions fully validated.
-
↩ Rule To old implementations these transactions are non-standard, so they typically neither relay nor mine them.
Quoted source text (1)
These transactions are non-standard to old implementations, which will (typically) not relay them or include them in blocks.
-
↩ History Spends of these outputs are only considered standard if the redeemScript is itself one of the other standard types.
Quoted source text (1)
Transactions that redeem these pay-to-script outpoints are only considered standard if the ''serialized script'' - also referred to as the ''redeemScript'' - is, itself, one of the other standard transaction types.