Skip to content

Commit ef787c3

Browse files
authored
Merge pull request bitcoin#441 from jl2012/bip146nullfail
BIP146: title and content changed
2 parents 40eadef + 40ba92a commit ef787c3

File tree

2 files changed

+72
-16
lines changed

2 files changed

+72
-16
lines changed

README.mediawiki

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,8 +513,8 @@ Those proposing changes should consider that ultimately consent may rest with th
513513
| Draft
514514
|-
515515
| [[bip-0146.mediawiki|146]]
516-
| Dealing with signature malleability
517-
| Pieter Wuille, Johnson Lau
516+
| Dealing with signature encoding malleability
517+
| Johnson Lau, Pieter Wuille
518518
| Standard
519519
| Draft
520520
|-

bip-0146.mediawiki

Lines changed: 70 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,123 @@
11
<pre>
22
BIP: 146
3-
Title: Dealing with signature malleability
4-
Author: Pieter Wuille <[email protected]>
5-
Johnson Lau <[email protected]>
3+
Title: Dealing with signature encoding malleability
4+
Author: Johnson Lau <[email protected]>
5+
Pieter Wuille <[email protected]>
66
Status: Draft
77
Type: Standards Track
88
Created: 2016-08-16
99
</pre>
1010

1111
==Abstract==
1212

13-
This document specifies proposed changes to the Bitcoin transaction validity rules to fix signature malleability for common transaction types.
13+
This document specifies proposed changes to the Bitcoin transaction validity rules to fix signature malleability related to ECDSA signature encoding.
1414

1515

1616
==Motivation==
1717

1818
Signature malleability refers to the ability of any relay node on the network to transform the signature in transactions, with no access to the relevant private keys required. For non-segregated witness transactions, signature malleability will change the <code>txid</code> and invalidate any unconfirmed child transactions. Although the <code>txid</code> of segregated witness ([https://github.com/bitcoin/bips/blob/master/bip-0141.mediawiki BIP141]) transactions is not third party malleable, this malleability vector will change the <code>wtxid</code> and may reduce the efficiency of compact block relay ([https://github.com/bitcoin/bips/blob/master/bip-0152.mediawiki BIP152]).
1919

20-
Since the enforcement of Strict DER signatures ([https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki BIP66]), there are 2 remaining known sources of malleability in the signature passed to ECDSA verification opcodes:
20+
Since the enforcement of Strict DER signatures ([https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki BIP66]), there are 2 remaining known sources of malleability in ECDSA signatures:
2121

2222
# '''Inherent ECDSA signature malleability''': ECDSA signatures are inherently malleable as taking the negative of the number S inside (modulo the curve order) does not invalidate it.
2323
24-
# '''Inputs ignored by scripts''': The (unnecessary) extra stack element consumed by <code>OP_CHECKMULTISIG</code> and <code>OP_CHECKMULTISIGVERIFY</code> is not inspected in any manner, and could be replaced with any value.
24+
# '''Malleability of failing signature''': If a signature failed to validate in <code>OP_CHECKSIG</code> or <code>OP_CHECKMULTISIG</code>, a <code>FALSE</code> would be returned to the stack and the script evaluation would continue. The failing signature may take any value, as long as it follows all the rules described in BIP66.
2525
2626
This document specifies new rules to fix the aforesaid signature malleability.
2727

2828

2929
==Specification==
3030

31-
To fix signature malleability, the following new rules are applied:
31+
To fix signature encoding malleability, the following new rules are applied to pre-segregated witness and segregated witness scripts:
3232

3333

3434
===LOW_S===
3535

3636
We require that the S value inside ECDSA signatures is at most the curve order divided by 2 (essentially restricting this value to its lower half range). Every signature passed to <code>OP_CHECKSIG</code><ref>Including pay-to-witness-public-key-hash (P2WPKH) described in BIP141</ref>, <code>OP_CHECKSIGVERIFY</code>, <code>OP_CHECKMULTISIG</code>, or <code>OP_CHECKMULTISIGVERIFY</code>, to which ECDSA verification is applied, MUST use a S value between <code>0x1</code> and <code>0x7FFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 5D576E73 57A4501D DFE92F46 681B20A0</code> (inclusive) with strict DER encoding (see [https://github.com/bitcoin/bips/blob/master/bip-0066.mediawiki BIP66]).
3737

38-
These operators all perform ECDSA verifications on pubkey/signature pairs, iterating from the top of the stack backwards. For each such verification, if the signature does not pass the Low S value check, the entire script evaluates to false immediately. If the signature is valid DER with low S value, but does not pass ECDSA verification, opcode execution continues as it used to, causing opcode execution to stop and push false on the stack (but not immediately fail the script) in some cases, which potentially skips further signatures (and thus does not subject them to Low S value check).
38+
If a signature passing to ECDSA verification does not pass the Low S value check and is not an empty byte array, the entire script evaluates to false immediately.
3939

4040
A high S value in signature could be trivially replaced by <code>S' = 0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141 - S</code>.
4141

4242

43-
===NULLDUMMY===
43+
===NULLFAIL===
4444

45-
The extra stack element consumed by <code>OP_CHECKMULTISIG</code> and <code>OP_CHECKMULTISIGVERIFY</code> MUST be the empty byte array (the result of <code>OP_0</code>). Anything else makes the script evaluate to false immediately.
45+
If an <code>OP_CHECKSIG</code> is trying to return a <code>FALSE</code> value to the stack, we require that the relevant signature must be an empty byte array.
46+
47+
If an <code>OP_CHECKMULTISIG</code> is trying to return a <code>FALSE</code> value to the stack, we require that all signatures passing to this <code>OP_CHECKMULTISIG</code> must be empty byte arrays, even the processing of some signatures might have been skipped due to early termination of the signature verification.
48+
49+
Otherwise, the entire script evaluates to false immediately.
50+
51+
52+
==Examples==
53+
54+
The following examples are the combined results of the LOW_S and NULLFAIL rules.<ref>Please note that due to implementation details in reference client v0.13.1, some signatures with S value higher than the half curve order might pass the LOW_S test. However, such signatures are certainly invalid, and will fail later due to NULLFAIL test.</ref>
55+
56+
Notation:
57+
58+
CO : curve order = 0xFFFFFFFF FFFFFFFF FFFFFFFF FFFFFFFE BAAEDCE6 AF48A03B BFD25E8C D0364141
59+
HCO : half curve order = CO / 2 = 0x7FFFFFFF FFFFFFFF FFFFFFFF FFFFFFFF 5D576E73 57A4501D DFE92F46 681B20A0
60+
P1, P2 : valid, serialized, public keys
61+
S1L, S2L : valid low S value signatures using respective keys P1 and P2 (1 ≤ S ≤ HCO)
62+
S1H, S2H : signatures with high S value (otherwise valid) using respective keys P1 and P2 (HCO < S < CO)
63+
F : any BIP66-compliant non-empty byte array but not a valid signature
64+
65+
These scripts will return a <code>TRUE</code> to the stack as before:
66+
67+
S1L P1 CHECKSIG
68+
0 S1L S2L 2 P1 P2 2 CHECKMULTISIG
69+
70+
These scripts will return a <code>FALSE</code> to the stack as before:
71+
72+
0 P1 CHECKSIG
73+
0 0 0 2 P1 P2 2 CHECKMULTISIG
74+
75+
These previously <code>TRUE</code> scripts will fail immediately under the new rules:
76+
77+
S1H P1 CHECKSIG
78+
0 S1H S2L 2 P1 P2 2 CHECKMULTISIG
79+
0 S1L S2H 2 P1 P2 2 CHECKMULTISIG
80+
0 S1H S2H 2 P1 P2 2 CHECKMULTISIG
81+
82+
These previously <code>FALSE</code> scripts will fail immediately under the new rules:
83+
84+
F P1 CHECKSIG
85+
0 S2L S1L 2 P1 P2 2 CHECKMULTISIG
86+
0 S1L F 2 P1 P2 2 CHECKMULTISIG
87+
0 F S2L 2 P1 P2 2 CHECKMULTISIG
88+
0 S1L 0 2 P1 P2 2 CHECKMULTISIG
89+
0 0 S2L 2 P1 P2 2 CHECKMULTISIG
90+
0 F 0 2 P1 P2 2 CHECKMULTISIG
91+
0 0 F 2 P1 P2 2 CHECKMULTISIG
4692
4793

4894
==Deployment==
4995

50-
This BIP will be deployed by "version bits" [https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki BIP9] using the same parameters for BIP141 and BIP143, with the name "segwit" and using bit 1.
96+
This BIP will be deployed by "version bits" [https://github.com/bitcoin/bips/blob/master/bip-0009.mediawiki BIP9]. Details TBD.
5197

5298
For Bitcoin mainnet, the BIP9 starttime will be midnight TBD UTC (Epoch timestamp TBD) and BIP9 timeout will be midnight TBD UTC (Epoch timestamp TBD).
5399

54-
For Bitcoin testnet, the BIP9 starttime will be midnight 1 May 2016 UTC (Epoch timestamp 1462060800) and BIP9 timeout will be midnight 1 May 2017 UTC (Epoch timestamp 1493596800).
100+
For Bitcoin testnet, the BIP9 starttime will be midnight TBD UTC (Epoch timestamp TBD) and BIP9 timeout will be midnight TBD UTC (Epoch timestamp TBD).
55101

56102

57103
==Compatibility==
58104

59-
The reference client has produced compatible signatures since v0.9.0, and NULLDUMMY and LOW_S have been enforced as relay policy by the reference client since v0.10.0 and v0.11.1 respectively. As of August 2016, very few transactions violating the requirement are being added to the chain. In addition, every non-compliant signature can trivially be converted into a compliant one, so there is no loss of functionality by this requirement.
105+
The reference client has produced LOW_S compatible signatures since v0.9.0, and the LOW_S rule has been enforced as relay policy by the reference client since v0.11.1. As of August 2016, very few transactions violating the requirement are being added to the chain. For all scriptPubKey types in actual use, non-compliant signatures can trivially be converted into compliant ones, so there is no loss of functionality by these requirements.
106+
107+
Scripts with failing <code>OP_CHECKSIG</code> or <code>OP_CHECKMULTISIG</code> rarely happen on the chain. The NULLFAIL rule has been enforced as relay policy by the reference client since v0.13.1.
108+
109+
Users MUST pay extra attention to these new rules when designing exotic scripts.
60110

61111

62112
==Implementation==
63113

64-
An implementation for the reference client is available at https://github.com/bitcoin/bitcoin/pull/8533
114+
Implementations for the reference client is available at:
115+
116+
https://github.com/bitcoin/bitcoin/blob/35fe0393f216aa6020fc929272118eade5628636/src/script/interpreter.cpp#L185
117+
118+
and
119+
120+
https://github.com/bitcoin/bitcoin/pull/8634
65121

66122

67123
==Footnotes==

0 commit comments

Comments
 (0)