Skip to content

Commit e38e909

Browse files
committed
Merge pull request bitcoin#292 from jl2012/bip143example
BIP143 example and clarify
2 parents da0e020 + ec5b1c0 commit e38e909

File tree

4 files changed

+99
-26
lines changed

4 files changed

+99
-26
lines changed

bip-0141.mediawiki

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -254,7 +254,7 @@ Special thanks to Gregory Maxwell for originating many of the ideas in this BIP
254254

255255
== Reference Implementation ==
256256

257-
https://github.com/sipa/bitcoin/commits/segwit3
257+
https://github.com/sipa/bitcoin/commits/segwit
258258

259259
== References ==
260260

bip-0142.mediawiki

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ Using 0x06 as witness version, followed 0x00 as witness version, and a 0x00 padd
140140
141141
== Reference implementation ==
142142

143+
https://github.com/theuni/bitcoin/commit/ede1b57058ac8efdefe61f67395affb48f2c0d80
144+
143145
== References ==
144146

145147
* [[bip-0013.mediawiki|BIP 13: Address Format for pay-to-script-hash]]

bip-0143.mediawiki

Lines changed: 95 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<pre>
1+
<pre>
22
BIP: 143
33
Title: Transaction Signature Verification for Version 0 Witness Program
44
Author: Johnson Lau <[email protected]>
@@ -24,35 +24,43 @@ Deploying the aforementioned fixes in the original script system is not a simple
2424
== Specification ==
2525
A new transaction digest algorithm is defined, but only applicable to sigops in version 0 witness program:
2626
Double SHA256 of the serialization of:
27-
1. nVersion of the transaction
28-
2. hashPrevouts
29-
3. hashSequence
30-
4. transaction id and output index of the output spent by this input
31-
5. subscript of the input
32-
6. value of the output spent by this input
33-
7. nSequence of the input
34-
8. hashOutputs
35-
9. nLocktime of the transaction
36-
10. sighash type of the signature
37-
38-
The items 1, 4, 5, 7, 9, 10 have the same meaning as the original algorithm. <ref name=wiki></ref>
27+
1. nVersion of the transaction (4-byte little endian)
28+
2. hashPrevouts (32-byte hash)
29+
3. hashSequence (32-byte hash)
30+
4. outpoint (32-byte hash + 4-byte little endian)
31+
5. scriptCode of the input (varInt for the length + script)
32+
6. value of the output spent by this input (8-byte little endian)
33+
7. nSequence of the input (4-byte little endian)
34+
8. hashOutputs (32-byte hash)
35+
9. nLocktime of the transaction (4-byte little endian)
36+
10. sighash type of the signature (4-byte little endian)
37+
38+
All components in the original algorithm, including the behavior <code>OP_CODESEPERATOR</code>, remains unchanged. The only difference is the way of serialization and the inclusion of amount being spent.
39+
40+
The items 1, 4, 7, 9, 10 have the same meaning as the original algorithm. <ref name=wiki></ref>
41+
42+
The item 5:
43+
*For P2WPKH witness program, the scriptCode is <code>0x1976a914{20-byte-pubkey-hash}88ac</code>.
44+
*For P2WSH witness program,
45+
**if the <code>witnessScript</code> does not contain any <code>OP_CODESEPERATOR</code>, the <code>scriptCode</code> is a <code>varInt</code> for the length of the <code>witnessScript</code>, followed by the <code>witnessScript</code>.
46+
**if the <code>witnessScript</code> contains any <code>OP_CODESEPERATOR</code>, the <code>scriptCode</code> is the evaluated script, with all <code>OP_CODESEPARATOR</code> and everything up to the last <code>OP_CODESEPARATOR</code> before the signature checking opcode being executed removed, and prepended by a <code>varInt</code> for the length of the truncated script.
3947
4048
The item 6 is a 8-byte value of the amount of bitcoin spent in this input.
4149

42-
hashPrevouts:
43-
*If the ANYONECANPAY flag is not set, hashPrevouts is the double SHA256 of the serialization of all transaction ids and output indexes involved in this transaction;
44-
*Otherwise, hashPrevouts is a uint256 of 0x0000......0000.
50+
<code>hashPrevouts</code>:
51+
*If the ANYONECANPAY flag is not set, hashPrevouts is the double SHA256 of the serialization of all input outpoints;
52+
*Otherwise, <code>hashPrevouts</code> is a <code>uint256</code> of <code>0x0000......0000</code>.
4553
46-
hashSequence:
54+
<code>hashSequence</code>:
4755
*If none of the ANYONECANPAY, SINGLE, NONE sighash type is set, hashSequence is the double SHA256 of the serialization of nSequence of all inputs;
48-
*Otherwise, hashSequence is a uint256 of 0x0000......0000.
56+
*Otherwise, <code>hashSequence</code> is a <code>uint256</code> of <code>0x0000......0000</code>.
4957
50-
hashOutputs:
51-
*If the sighash type is neither SINGLE nor NONE, hashOutputs is the double SHA256 of the serialization of all output scriptPubKey with value;
52-
*If sighash type is SINGLE and the input index is not greater than the number of outputs, hashOutputs is the double SHA256 of the output scriptPubKey with value of the same index as the input;
53-
*Otherwise, hashOutputs is a uint256 of 0x0000......0000.
58+
<code>hashOutputs</code>:
59+
*If the sighash type is neither SINGLE nor NONE, hashOutputs is the double SHA256 of the serialization of all output value (8-byte little endian) with scriptPubKey (<code>varInt</code> for the length + script);
60+
*If sighash type is SINGLE and the input index is not greater than the number of outputs, <code>hashOutputs</code> is the double SHA256 of the output value with <code>scriptPubKey</code> of the same index as the input;
61+
*Otherwise, <code>hashOutputs</code> is a <code>uint256</code> of <code>0x0000......0000</code>.
5462
55-
The hashPrevouts, hashSequence, and hashOutputs calculated in an earlier verification 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).
63+
The <code>hashPrevouts</code>, <code>hashSequence</code>, and <code>hashOutputs</code> calculated in an earlier verification 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).
5664

5765
Refer to the reference implementation, reproduced below, for the precise algorithm:
5866

@@ -112,6 +120,69 @@ Refer to the reference implementation, reproduced below, for the precise algorit
112120
return ss.GetHash();
113121
</source>
114122

123+
== Example ==
124+
125+
126+
The following is an unsigned transaction:
127+
0100000002fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f0000000000eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac11000000
128+
129+
nVersion: 01000000
130+
txin: 02 fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f 00000000 00 eeffffff
131+
ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a 01000000 00 ffffffff
132+
txout: 02 202cb20600000000 1976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac
133+
9093510d00000000 1976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac
134+
nLockTime: 11000000
135+
136+
The first input comes from an ordinary P2PK:
137+
scriptPubKey: 2103c9f4836b9a4f77fc0d81f7bcb01b7f1b35916864b9476c241ce9fc198bd25432ac value: 6.25
138+
139+
The second input comes from a P2WPKH witness program:
140+
scriptPubKey: 00141d0f172a0ecb48aee1be1f2687d2963ae33f71a1, value: 6
141+
142+
To sign it with a nHashType of 1 (SIGHASH_ALL):
143+
144+
hashPrevouts:
145+
dSHA256(fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f00000000ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a01000000)
146+
= 96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37
147+
148+
hashSequence:
149+
dSHA256(eeffffffffffffff)
150+
= 52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b
151+
152+
hashOutputs:
153+
dSHA256(202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac)
154+
= 863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5
155+
156+
hash preimage: 0100000096b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd3752b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3bef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a010000001976a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac0046c32300000000ffffffff863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e51100000001000000
157+
158+
nVersion: 01000000
159+
hashPrevouts: 96b827c8483d4e9b96712b6713a7b68d6e8003a781feba36c31143470b4efd37
160+
hashSequence: 52b0a642eea2fb7ae638c36f6252b6750293dbe574a806984b8e4d8548339a3b
161+
outpoint: ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a01000000
162+
scriptCode: 1976a9141d0f172a0ecb48aee1be1f2687d2963ae33f71a188ac
163+
amount: 0046c32300000000
164+
nSequence: ffffffff
165+
hashOutputs: 863ef3e1a92afbfdb97f31ad0fc7683ee943e9abcf2501590ff8f6551f47e5e5
166+
nLockTime: 11000000
167+
nHashType: 01000000
168+
169+
sigHash: c37af31116d1b27caf68aae9e3ac82f1477929014d5b917657d0eb49478cb670
170+
signature: 304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee
171+
172+
The serialized signed transaction is: 01000000000102fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f00000000494830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01eeffffffef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a0100000000ffffffff02202cb206000000001976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac9093510d000000001976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac000247304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee0121025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee635711000000
173+
174+
nVersion: 01000000
175+
marker: 00
176+
flag: 01
177+
txin: 02 fff7f7881a8099afa6940d42d1e7f6362bec38171ea3edf433541db4e4ad969f 00000000 494830450221008b9d1dc26ba6a9cb62127b02742fa9d754cd3bebf337f7a55d114c8e5cdd30be022040529b194ba3f9281a99f2b1c0a19c0489bc22ede944ccf4ecbab4cc618ef3ed01 eeffffff
178+
ef51e1b804cc89d182d279655c3aa89e815b1b309fe287d9b2b55d57b90ec68a 01000000 00 ffffffff
179+
txout: 02 202cb20600000000 1976a9148280b37df378db99f66f85c95a783a76ac7a6d5988ac
180+
9093510d00000000 1976a9143bde42dbee7e4dbe6a21b2d50ce2f0167faa815988ac
181+
witness 00
182+
02 47304402203609e17b84f6a7d30c80bfa610b5b4542f32a8a0d5447a12fb1366d7f01cc44a0220573a954c4518331561406f90300e8f3358f51928d43c212a8caed02de67eebee01 21025476c2e83188368da1ff3e292e7acafcdb3566bb0ad253f62fc70f07aeee6357
183+
nLockTime: 11000000
184+
185+
The new serialization format is described in BIP144 <ref>[[bip-0144.mediawiki|BIP144: Segregated Witness (Peer Services)]]</ref>
115186
== Deployment ==
116187

117188
This proposal is deployed with Segregated Witness softfork (BIP 141)
@@ -122,7 +193,7 @@ As a soft fork, older software will continue to operate without modification. No
122193

123194
== Reference Implementation ==
124195

125-
https://github.com/sipa/bitcoin/commits/segwit3
196+
https://github.com/sipa/bitcoin/commits/segwit
126197

127198
== References ==
128199

bip-0144.mediawiki

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ MSG_WITNESS_BLOCK requests will return a block message with transactions that ha
116116
Special thanks to Gregory Maxwell for originating many of the ideas in this BIP and Luke-Jr for figuring out how to deploy this as a soft fork.
117117

118118
== Reference Implementation ==
119-
https://github.com/sipa/bitcoin/commits/segwit3
119+
https://github.com/sipa/bitcoin/commits/segwit
120120

121121
== Copyright ==
122122
This document is placed in the public domain.

0 commit comments

Comments
 (0)