Skip to content

Commit 5160fc7

Browse files
committed
Merge pull request bitcoin#372 from jl2012/patch-16
BIP143 clarifying semantics of ACP|SINGLE
2 parents 5d0b400 + c1ef3a0 commit 5160fc7

File tree

1 file changed

+15
-11
lines changed

1 file changed

+15
-11
lines changed

bip-0143.mediawiki

Lines changed: 15 additions & 11 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]>
@@ -28,37 +28,41 @@ A new transaction digest algorithm is defined, but only applicable to sigops in
2828
2. hashPrevouts (32-byte hash)
2929
3. hashSequence (32-byte hash)
3030
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)
3232
6. value of the output spent by this input (8-byte little endian)
3333
7. nSequence of the input (4-byte little endian)
3434
8. hashOutputs (32-byte hash)
3535
9. nLocktime of the transaction (4-byte little endian)
3636
10. sighash type of the signature (4-byte little endian)
3737

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.
3943
4044
The items 1, 4, 7, 9, 10 have the same meaning as the original algorithm. <ref name=wiki></ref>
4145

4246
The item 5:
4347
*For P2WPKH witness program, the scriptCode is <code>0x1976a914{20-byte-pubkey-hash}88ac</code>.
4448
*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.
4751
4852
The item 6 is a 8-byte value of the amount of bitcoin spent in this input.
4953

5054
<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;
5256
*Otherwise, <code>hashPrevouts</code> is a <code>uint256</code> of <code>0x0000......0000</code>.
5357
5458
<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;
5660
*Otherwise, <code>hashSequence</code> is a <code>uint256</code> of <code>0x0000......0000</code>.
5761
5862
<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>
6266
6367
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).
6468

@@ -195,7 +199,7 @@ As a soft fork, older software will continue to operate without modification. No
195199

196200
== Reference Implementation ==
197201

198-
https://github.com/sipa/bitcoin/commits/segwit
202+
https://github.com/bitcoin/bitcoin/pull/7910
199203

200204
== References ==
201205

0 commit comments

Comments
 (0)