|
1 |
| -<pre> |
| 1 | +<pre> |
2 | 2 | BIP: 143
|
3 | 3 | Title: Transaction Signature Verification for Version 0 Witness Program
|
4 | 4 | Author: Johnson Lau < [email protected]>
|
@@ -28,37 +28,41 @@ A new transaction digest algorithm is defined, but only applicable to sigops in
|
28 | 28 | 2. hashPrevouts (32-byte hash)
|
29 | 29 | 3. hashSequence (32-byte hash)
|
30 | 30 | 4. outpoint (32-byte hash + 4-byte little endian)
|
31 |
| - 5. scriptCode of the input (varInt for the length + script) |
| 31 | + 5. scriptCode of the input (serialized as scripts inside CTxOuts) |
32 | 32 | 6. value of the output spent by this input (8-byte little endian)
|
33 | 33 | 7. nSequence of the input (4-byte little endian)
|
34 | 34 | 8. hashOutputs (32-byte hash)
|
35 | 35 | 9. nLocktime of the transaction (4-byte little endian)
|
36 | 36 | 10. sighash type of the signature (4-byte little endian)
|
37 | 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. |
| 38 | +Semantics of the original sighash types remain unchanged, except the followings: |
| 39 | +# The way of serialization is changed; |
| 40 | +# All sighash types commit to the amount being spent by the signed input; |
| 41 | +# <code>FindAndDelete</code> of the signature is not applied to the <code>scriptCode</code>; |
| 42 | +# <code>SINGLE</code> does not commit to the input index. When <code>ANYONECANPAY</code> is not set, the semantics are unchanged since <code>hashPrevouts</code> and <code>outpoint</code> together implictly commit to the input index. When <code>SINGLE</code> is used with <code>ANYONECANPAY</code>, omission of the index commitment allows permutation of the input-output pairs, as long as each pair is located at an equivalent index. |
39 | 43 |
|
40 | 44 | The items 1, 4, 7, 9, 10 have the same meaning as the original algorithm. <ref name=wiki></ref>
|
41 | 45 |
|
42 | 46 | The item 5:
|
43 | 47 | *For P2WPKH witness program, the scriptCode is <code>0x1976a914{20-byte-pubkey-hash}88ac</code>.
|
44 | 48 | *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. |
| 49 | +**if the <code>witnessScript</code> does not contain any <code>OP_CODESEPERATOR</code>, the <code>scriptCode</code> is the <code>witnessScript</code> serialized as scripts inside CTxOuts. |
| 50 | +**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, serialized as scripts inside CTxOuts. |
47 | 51 |
|
48 | 52 | The item 6 is a 8-byte value of the amount of bitcoin spent in this input.
|
49 | 53 |
|
50 | 54 | <code>hashPrevouts</code>:
|
51 |
| -*If the ANYONECANPAY flag is not set, hashPrevouts is the double SHA256 of the serialization of all input outpoints; |
| 55 | +*If the <code>ANYONECANPAY</code> flag is not set, <code>hashPrevouts</code> is the double SHA256 of the serialization of all input outpoints; |
52 | 56 | *Otherwise, <code>hashPrevouts</code> is a <code>uint256</code> of <code>0x0000......0000</code>.
|
53 | 57 |
|
54 | 58 | <code>hashSequence</code>:
|
55 |
| -*If none of the ANYONECANPAY, SINGLE, NONE sighash type is set, hashSequence is the double SHA256 of the serialization of nSequence of all inputs; |
| 59 | +*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; |
56 | 60 | *Otherwise, <code>hashSequence</code> is a <code>uint256</code> of <code>0x0000......0000</code>.
|
57 | 61 |
|
58 | 62 | <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>. |
| 63 | +*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 value (8-byte little endian) with <code>scriptPubKey</code> (serialized as scripts inside CTxOuts); |
| 64 | +*If sighash type is <code>SINGLE</code> 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; |
| 65 | +*Otherwise, <code>hashOutputs</code> is a <code>uint256</code> of <code>0x0000......0000</code>.<ref>In the original algorithm, a <code>uint256</code> of <code>0x0000......0001</code> is commited if the input index for a <code>SINGLE</code> signature is greater than the number of outputs. In this BIP a <code>0x0000......0000</code> is commited, without changing the semantics.</ref> |
62 | 66 |
|
63 | 67 | 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).
|
64 | 68 |
|
@@ -195,7 +199,7 @@ As a soft fork, older software will continue to operate without modification. No
|
195 | 199 |
|
196 | 200 | == Reference Implementation ==
|
197 | 201 |
|
198 |
| -https://github.com/sipa/bitcoin/commits/segwit |
| 202 | +https://github.com/bitcoin/bitcoin/pull/7910 |
199 | 203 |
|
200 | 204 | == References ==
|
201 | 205 |
|
|
0 commit comments