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

Function IsValidSignatureEncoding

src/script/interpreter.cpp:118–181  ·  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=8

Source from the content-addressed store, hash-verified

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

Callers 2

IsLowDERSignatureFunction · 0.85
CheckSignatureEncodingFunction · 0.85

Calls 1

sizeMethod · 0.45

Tested by

no test coverage detected