MCPcopy Create free account
hub / github.com/bitcoinxt/bitcoinxt / IsValidSignatureEncoding

Function IsValidSignatureEncoding

src/script/interpreter.cpp:98–161  ·  view source on GitHub ↗

* A canonical signature exists of: <30> <02> <02> * Where R and S are not negative (their first byte has its highest bit not set), and not * excessively padded (do not start with a 0 byte, unless an otherwise negative number follows, * in which case a single 0 byte is necessary and even required). * * See https://bitcointalk.org/index.php?topic=

Source from the content-addressed store, hash-verified

96 * This function is consensus-critical since BIP66.
97 */
98bool static IsValidSignatureEncoding(const std::vector<unsigned char> &sig) {
99 // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] [sighash]
100 // * total-length: 1-byte length descriptor of everything that follows,
101 // excluding the sighash byte.
102 // * R-length: 1-byte length descriptor of the R value that follows.
103 // * R: arbitrary-length big-endian encoded R value. It must use the shortest
104 // possible encoding for a positive integers (which means no null bytes at
105 // the start, except a single one when the next byte has its highest bit set).
106 // * S-length: 1-byte length descriptor of the S value that follows.
107 // * S: arbitrary-length big-endian encoded S value. The same rules apply.
108 // * sighash: 1-byte value indicating what data is hashed (not part of the DER
109 // signature)
110
111 // Minimum and maximum size constraints.
112 if (sig.size() < 9) return false;
113 if (sig.size() > 73) return false;
114
115 // A signature is of type 0x30 (compound).
116 if (sig[0] != 0x30) return false;
117
118 // Make sure the length covers the entire signature.
119 if (sig[1] != sig.size() - 3) return false;
120
121 // Extract the length of the R element.
122 unsigned int lenR = sig[3];
123
124 // Make sure the length of the S element is still inside the signature.
125 if (5 + lenR >= sig.size()) return false;
126
127 // Extract the length of the S element.
128 unsigned int lenS = sig[5 + lenR];
129
130 // Verify that the length of the signature matches the sum of the length
131 // of the elements.
132 if ((size_t)(lenR + lenS + 7) != sig.size()) return false;
133
134 // Check whether the R element is an integer.
135 if (sig[2] != 0x02) return false;
136
137 // Zero-length integers are not allowed for R.
138 if (lenR == 0) return false;
139
140 // Negative numbers are not allowed for R.
141 if (sig[4] & 0x80) return false;
142
143 // Null bytes at the start of R are not allowed, unless R would
144 // otherwise be interpreted as a negative number.
145 if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80)) return false;
146
147 // Check whether the S element is an integer.
148 if (sig[lenR + 4] != 0x02) return false;
149
150 // Zero-length integers are not allowed for S.
151 if (lenS == 0) return false;
152
153 // Negative numbers are not allowed for S.
154 if (sig[lenR + 6] & 0x80) return false;
155

Callers 2

IsLowDERSignatureFunction · 0.85
CheckSignatureEncodingFunction · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected