/tmp/bitcoin/src/script/interpreter.cpp
Line | Count | Source |
1 | | // Copyright (c) 2009-2010 Satoshi Nakamoto |
2 | | // Copyright (c) 2009-present The Bitcoin Core developers |
3 | | // Distributed under the MIT software license, see the accompanying |
4 | | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | | |
6 | | #include <script/interpreter.h> |
7 | | |
8 | | #include <crypto/ripemd160.h> |
9 | | #include <crypto/sha1.h> |
10 | | #include <crypto/sha256.h> |
11 | | #include <pubkey.h> |
12 | | #include <script/script.h> |
13 | | #include <tinyformat.h> |
14 | | #include <uint256.h> |
15 | | |
16 | | typedef std::vector<unsigned char> valtype; |
17 | | |
18 | | namespace { |
19 | | |
20 | | inline bool set_success(ScriptError* ret) |
21 | 2.48M | { |
22 | 2.48M | if (ret) |
23 | 1.74M | *ret = SCRIPT_ERR_OK; |
24 | 2.48M | return true; |
25 | 2.48M | } |
26 | | |
27 | | inline bool set_error(ScriptError* ret, const ScriptError serror) |
28 | 3.21M | { |
29 | 3.21M | if (ret) |
30 | 2.29M | *ret = serror; |
31 | 3.21M | return false; |
32 | 3.21M | } |
33 | | |
34 | | } // namespace |
35 | | |
36 | | bool CastToBool(const valtype& vch) |
37 | 1.04M | { |
38 | 1.05M | for (unsigned int i = 0; i < vch.size(); i++) |
39 | 998k | { |
40 | 998k | if (vch[i] != 0) |
41 | 990k | { |
42 | | // Can be negative zero |
43 | 990k | if (i == vch.size()-1 && vch[i] == 0x80) |
44 | 257 | return false; |
45 | 990k | return true; |
46 | 990k | } |
47 | 998k | } |
48 | 54.1k | return false; |
49 | 1.04M | } |
50 | | |
51 | | /** |
52 | | * Script is a stack machine (like Forth) that evaluates a predicate |
53 | | * returning a bool indicating valid or not. There are no loops. |
54 | | */ |
55 | 11.5M | #define stacktop(i) (stack.at(size_t(int64_t(stack.size()) + int64_t{i}))) |
56 | 5.33k | #define altstacktop(i) (altstack.at(size_t(int64_t(altstack.size()) + int64_t{i}))) |
57 | | static inline void popstack(std::vector<valtype>& stack) |
58 | 11.4M | { |
59 | 11.4M | if (stack.empty()) |
60 | 0 | throw std::runtime_error("popstack(): stack empty"); |
61 | 11.4M | stack.pop_back(); |
62 | 11.4M | } |
63 | | |
64 | 71.1k | bool static IsCompressedOrUncompressedPubKey(const valtype &vchPubKey) { |
65 | 71.1k | if (vchPubKey.size() < CPubKey::COMPRESSED_SIZE) { |
66 | | // Non-canonical public key: too short |
67 | 339 | return false; |
68 | 339 | } |
69 | 70.8k | if (vchPubKey[0] == 0x04) { |
70 | 6.04k | if (vchPubKey.size() != CPubKey::SIZE) { |
71 | | // Non-canonical public key: invalid length for uncompressed key |
72 | 259 | return false; |
73 | 259 | } |
74 | 64.7k | } else if (vchPubKey[0] == 0x02 || vchPubKey[0] == 0x03) { |
75 | 63.6k | if (vchPubKey.size() != CPubKey::COMPRESSED_SIZE) { |
76 | | // Non-canonical public key: invalid length for compressed key |
77 | 114 | return false; |
78 | 114 | } |
79 | 63.6k | } else { |
80 | | // Non-canonical public key: neither compressed nor uncompressed |
81 | 1.15k | return false; |
82 | 1.15k | } |
83 | 69.2k | return true; |
84 | 70.8k | } |
85 | | |
86 | 42.6k | bool static IsCompressedPubKey(const valtype &vchPubKey) { |
87 | 42.6k | if (vchPubKey.size() != CPubKey::COMPRESSED_SIZE) { |
88 | | // Non-canonical public key: invalid length for compressed key |
89 | 7.21k | return false; |
90 | 7.21k | } |
91 | 35.4k | if (vchPubKey[0] != 0x02 && vchPubKey[0] != 0x03) { |
92 | | // Non-canonical public key: invalid prefix for compressed key |
93 | 134 | return false; |
94 | 134 | } |
95 | 35.3k | return true; |
96 | 35.4k | } |
97 | | |
98 | | /** |
99 | | * A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <S> <hashtype> |
100 | | * Where R and S are not negative (their first byte has its highest bit not set), and not |
101 | | * excessively padded (do not start with a 0 byte, unless an otherwise negative number follows, |
102 | | * in which case a single 0 byte is necessary and even required). |
103 | | * |
104 | | * See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623 |
105 | | * |
106 | | * This function is consensus-critical since BIP66. |
107 | | */ |
108 | 234k | bool static IsValidSignatureEncoding(const std::vector<unsigned char> &sig) { |
109 | | // Format: 0x30 [total-length] 0x02 [R-length] [R] 0x02 [S-length] [S] [sighash] |
110 | | // * total-length: 1-byte length descriptor of everything that follows, |
111 | | // excluding the sighash byte. |
112 | | // * R-length: 1-byte length descriptor of the R value that follows. |
113 | | // * R: arbitrary-length big-endian encoded R value. It must use the shortest |
114 | | // possible encoding for a positive integer (which means no null bytes at |
115 | | // the start, except a single one when the next byte has its highest bit set). |
116 | | // * S-length: 1-byte length descriptor of the S value that follows. |
117 | | // * S: arbitrary-length big-endian encoded S value. The same rules apply. |
118 | | // * sighash: 1-byte value indicating what data is hashed (not part of the DER |
119 | | // signature) |
120 | | |
121 | | // Minimum and maximum size constraints. |
122 | 234k | if (sig.size() < 9) return false; |
123 | 233k | if (sig.size() > 73) return false; |
124 | | |
125 | | // A signature is of type 0x30 (compound). |
126 | 233k | if (sig[0] != 0x30) return false; |
127 | | |
128 | | // Make sure the length covers the entire signature. |
129 | 232k | if (sig[1] != sig.size() - 3) return false; |
130 | | |
131 | | // Extract the length of the R element. |
132 | 222k | unsigned int lenR = sig[3]; |
133 | | |
134 | | // Make sure the length of the S element is still inside the signature. |
135 | 222k | if (5 + lenR >= sig.size()) return false; |
136 | | |
137 | | // Extract the length of the S element. |
138 | 222k | unsigned int lenS = sig[5 + lenR]; |
139 | | |
140 | | // Verify that the length of the signature matches the sum of the length |
141 | | // of the elements. |
142 | 222k | if ((size_t)(lenR + lenS + 7) != sig.size()) return false; |
143 | | |
144 | | // Check whether the R element is an integer. |
145 | 222k | if (sig[2] != 0x02) return false; |
146 | | |
147 | | // Zero-length integers are not allowed for R. |
148 | 222k | if (lenR == 0) return false; |
149 | | |
150 | | // Negative numbers are not allowed for R. |
151 | 222k | if (sig[4] & 0x80) return false; |
152 | | |
153 | | // Null bytes at the start of R are not allowed, unless R would |
154 | | // otherwise be interpreted as a negative number. |
155 | 219k | if (lenR > 1 && (sig[4] == 0x00) && !(sig[5] & 0x80)) return false; |
156 | | |
157 | | // Check whether the S element is an integer. |
158 | 218k | if (sig[lenR + 4] != 0x02) return false; |
159 | | |
160 | | // Zero-length integers are not allowed for S. |
161 | 218k | if (lenS == 0) return false; |
162 | | |
163 | | // Negative numbers are not allowed for S. |
164 | 218k | if (sig[lenR + 6] & 0x80) return false; |
165 | | |
166 | | // Null bytes at the start of S are not allowed, unless S would otherwise be |
167 | | // interpreted as a negative number. |
168 | 218k | if (lenS > 1 && (sig[lenR + 6] == 0x00) && !(sig[lenR + 7] & 0x80)) return false; |
169 | | |
170 | 218k | return true; |
171 | 218k | } |
172 | | |
173 | 62.6k | bool static IsLowDERSignature(const valtype &vchSig, ScriptError* serror) { |
174 | 62.6k | if (!IsValidSignatureEncoding(vchSig)) { |
175 | 0 | return set_error(serror, SCRIPT_ERR_SIG_DER); |
176 | 0 | } |
177 | | // https://bitcoin.stackexchange.com/a/12556: |
178 | | // Also note that inside transaction signatures, an extra hashtype byte |
179 | | // follows the actual signature data. |
180 | 62.6k | std::vector<unsigned char> vchSigCopy(vchSig.begin(), vchSig.begin() + vchSig.size() - 1); |
181 | | // If the S value is above the order of the curve divided by two, its |
182 | | // complement modulo the order could have been used instead, which is |
183 | | // one byte shorter when encoded correctly. |
184 | 62.6k | if (!CPubKey::CheckLowS(vchSigCopy)) { |
185 | 349 | return set_error(serror, SCRIPT_ERR_SIG_HIGH_S); |
186 | 349 | } |
187 | 62.3k | return true; |
188 | 62.6k | } |
189 | | |
190 | 65.5k | bool static IsDefinedHashtypeSignature(const valtype &vchSig) { |
191 | 65.5k | if (vchSig.size() == 0) { |
192 | 0 | return false; |
193 | 0 | } |
194 | 65.5k | unsigned char nHashType = vchSig[vchSig.size() - 1] & (~(SIGHASH_ANYONECANPAY)); |
195 | 65.5k | if (nHashType < SIGHASH_ALL || nHashType > SIGHASH_SINGLE) |
196 | 544 | return false; |
197 | | |
198 | 65.0k | return true; |
199 | 65.5k | } |
200 | | |
201 | 238k | bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, script_verify_flags flags, ScriptError* serror) { |
202 | | // Empty signature. Not strictly DER encoded, but allowed to provide a |
203 | | // compact way to provide an invalid signature for use with CHECK(MULTI)SIG |
204 | 238k | if (vchSig.size() == 0) { |
205 | 20.9k | return true; |
206 | 20.9k | } |
207 | 218k | if ((flags & (SCRIPT_VERIFY_DERSIG | SCRIPT_VERIFY_LOW_S | SCRIPT_VERIFY_STRICTENC)) != 0 && !IsValidSignatureEncoding(vchSig)) { |
208 | 16.8k | return set_error(serror, SCRIPT_ERR_SIG_DER); |
209 | 201k | } else if ((flags & SCRIPT_VERIFY_LOW_S) != 0 && !IsLowDERSignature(vchSig, serror)) { |
210 | | // serror is set |
211 | 349 | return false; |
212 | 200k | } else if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && !IsDefinedHashtypeSignature(vchSig)) { |
213 | 544 | return set_error(serror, SCRIPT_ERR_SIG_HASHTYPE); |
214 | 544 | } |
215 | 200k | return true; |
216 | 218k | } |
217 | | |
218 | 219k | bool static CheckPubKeyEncoding(const valtype &vchPubKey, script_verify_flags flags, const SigVersion &sigversion, ScriptError* serror) { |
219 | 219k | if ((flags & SCRIPT_VERIFY_STRICTENC) != 0 && !IsCompressedOrUncompressedPubKey(vchPubKey)) { |
220 | 1.86k | return set_error(serror, SCRIPT_ERR_PUBKEYTYPE); |
221 | 1.86k | } |
222 | | // Only compressed keys are accepted in segwit |
223 | 218k | if ((flags & SCRIPT_VERIFY_WITNESS_PUBKEYTYPE) != 0 && sigversion == SigVersion::WITNESS_V0 && !IsCompressedPubKey(vchPubKey)) { |
224 | 7.34k | return set_error(serror, SCRIPT_ERR_WITNESS_PUBKEYTYPE); |
225 | 7.34k | } |
226 | 210k | return true; |
227 | 218k | } |
228 | | |
229 | | int FindAndDelete(CScript& script, const CScript& b) |
230 | 246k | { |
231 | 246k | int nFound = 0; |
232 | 246k | if (b.empty()) |
233 | 1 | return nFound; |
234 | 246k | CScript result; |
235 | 246k | CScript::const_iterator pc = script.begin(), pc2 = script.begin(), end = script.end(); |
236 | 246k | opcodetype opcode; |
237 | 246k | do |
238 | 2.73M | { |
239 | 2.73M | result.insert(result.end(), pc2, pc); |
240 | 2.75M | while (static_cast<size_t>(end - pc) >= b.size() && std::equal(b.begin(), b.end(), pc)) |
241 | 26.4k | { |
242 | 26.4k | pc = pc + b.size(); |
243 | 26.4k | ++nFound; |
244 | 26.4k | } |
245 | 2.73M | pc2 = pc; |
246 | 2.73M | } |
247 | 2.73M | while (script.GetOp(pc, opcode)); |
248 | | |
249 | 246k | if (nFound > 0) { |
250 | 19.8k | result.insert(result.end(), pc2, end); |
251 | 19.8k | script = std::move(result); |
252 | 19.8k | } |
253 | | |
254 | 246k | return nFound; |
255 | 246k | } |
256 | | |
257 | | namespace { |
258 | | /** A data type to abstract out the condition stack during script execution. |
259 | | * |
260 | | * Conceptually it acts like a vector of booleans, one for each level of nested |
261 | | * IF/THEN/ELSE, indicating whether we're in the active or inactive branch of |
262 | | * each. |
263 | | * |
264 | | * The elements on the stack cannot be observed individually; we only need to |
265 | | * expose whether the stack is empty and whether or not any false values are |
266 | | * present at all. To implement OP_ELSE, a toggle_top modifier is added, which |
267 | | * flips the last value without returning it. |
268 | | * |
269 | | * This uses an optimized implementation that does not materialize the |
270 | | * actual stack. Instead, it just stores the size of the would-be stack, |
271 | | * and the position of the first false value in it. |
272 | | */ |
273 | | class ConditionStack { |
274 | | private: |
275 | | //! A constant for m_first_false_pos to indicate there are no falses. |
276 | | static constexpr uint32_t NO_FALSE = std::numeric_limits<uint32_t>::max(); |
277 | | |
278 | | //! The size of the implied stack. |
279 | | uint32_t m_stack_size = 0; |
280 | | //! The position of the first false value on the implied stack, or NO_FALSE if all true. |
281 | | uint32_t m_first_false_pos = NO_FALSE; |
282 | | |
283 | | public: |
284 | 1.97M | bool empty() const { return m_stack_size == 0; } |
285 | 14.5M | bool all_true() const { return m_first_false_pos == NO_FALSE; } |
286 | | void push_back(bool f) |
287 | 77.7k | { |
288 | 77.7k | if (m_first_false_pos == NO_FALSE && !f) { |
289 | | // The stack consists of all true values, and a false is added. |
290 | | // The first false value will appear at the current size. |
291 | 44.8k | m_first_false_pos = m_stack_size; |
292 | 44.8k | } |
293 | 77.7k | ++m_stack_size; |
294 | 77.7k | } |
295 | | void pop_back() |
296 | 59.3k | { |
297 | 59.3k | assert(m_stack_size > 0); |
298 | 59.3k | --m_stack_size; |
299 | 59.3k | if (m_first_false_pos == m_stack_size) { |
300 | | // When popping off the first false value, everything becomes true. |
301 | 23.1k | m_first_false_pos = NO_FALSE; |
302 | 23.1k | } |
303 | 59.3k | } |
304 | | void toggle_top() |
305 | 62.6k | { |
306 | 62.6k | assert(m_stack_size > 0); |
307 | 62.6k | if (m_first_false_pos == NO_FALSE) { |
308 | | // The current stack is all true values; the first false will be the top. |
309 | 20.0k | m_first_false_pos = m_stack_size - 1; |
310 | 42.5k | } else if (m_first_false_pos == m_stack_size - 1) { |
311 | | // The top is the first false value; toggling it will make everything true. |
312 | 38.7k | m_first_false_pos = NO_FALSE; |
313 | 38.7k | } else { |
314 | | // There is a false value, but not on top. No action is needed as toggling |
315 | | // anything but the first false value is unobservable. |
316 | 3.82k | } |
317 | 62.6k | } |
318 | | }; |
319 | | } |
320 | | |
321 | | static bool EvalChecksigPreTapscript(const valtype& vchSig, const valtype& vchPubKey, CScript::const_iterator pbegincodehash, CScript::const_iterator pend, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror, bool& fSuccess) |
322 | 206k | { |
323 | 206k | assert(sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0); |
324 | | |
325 | | // Subset of script starting at the most recent codeseparator |
326 | 206k | CScript scriptCode(pbegincodehash, pend); |
327 | | |
328 | | // Drop the signature in pre-segwit scripts but not segwit scripts |
329 | 206k | if (sigversion == SigVersion::BASE) { |
330 | 137k | int found = FindAndDelete(scriptCode, CScript() << vchSig); |
331 | 137k | if (found > 0 && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE)) |
332 | 105 | return set_error(serror, SCRIPT_ERR_SIG_FINDANDDELETE); |
333 | 137k | } |
334 | | |
335 | 206k | if (!CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) { |
336 | | //serror is set |
337 | 19.5k | return false; |
338 | 19.5k | } |
339 | 186k | fSuccess = checker.CheckECDSASignature(vchSig, vchPubKey, scriptCode, sigversion); |
340 | | |
341 | 186k | if (!fSuccess && (flags & SCRIPT_VERIFY_NULLFAIL) && vchSig.size()) |
342 | 1.58k | return set_error(serror, SCRIPT_ERR_SIG_NULLFAIL); |
343 | | |
344 | 184k | return true; |
345 | 186k | } |
346 | | |
347 | | static bool EvalChecksigTapscript(const valtype& sig, const valtype& pubkey, ScriptExecutionData& execdata, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror, bool& success) |
348 | 498k | { |
349 | 498k | assert(sigversion == SigVersion::TAPSCRIPT); |
350 | | |
351 | | /* |
352 | | * The following validation sequence is consensus critical. Please note how -- |
353 | | * upgradable public key versions precede other rules; |
354 | | * the script execution fails when using empty signature with invalid public key; |
355 | | * the script execution fails when using non-empty invalid signature. |
356 | | */ |
357 | 498k | success = !sig.empty(); |
358 | 498k | if (success) { |
359 | | // Implement the sigops/witnesssize ratio test. |
360 | | // Passing with an upgradable public key version is also counted. |
361 | 400k | assert(execdata.m_validation_weight_left_init); |
362 | 400k | execdata.m_validation_weight_left -= VALIDATION_WEIGHT_PER_SIGOP_PASSED; |
363 | 400k | if (execdata.m_validation_weight_left < 0) { |
364 | 472 | return set_error(serror, SCRIPT_ERR_TAPSCRIPT_VALIDATION_WEIGHT); |
365 | 472 | } |
366 | 400k | } |
367 | 498k | if (pubkey.size() == 0) { |
368 | 303 | return set_error(serror, SCRIPT_ERR_TAPSCRIPT_EMPTY_PUBKEY); |
369 | 498k | } else if (pubkey.size() == 32) { |
370 | 354k | if (success && !checker.CheckSchnorrSignature(sig, pubkey, sigversion, execdata, serror)) { |
371 | 580 | return false; // serror is set |
372 | 580 | } |
373 | 354k | } else { |
374 | | /* |
375 | | * New public key version softforks should be defined before this `else` block. |
376 | | * Generally, the new code should not do anything but failing the script execution. To avoid |
377 | | * consensus bugs, it should not modify any existing values (including `success`). |
378 | | */ |
379 | 143k | if ((flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_PUBKEYTYPE) != 0) { |
380 | 8 | return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_PUBKEYTYPE); |
381 | 8 | } |
382 | 143k | } |
383 | | |
384 | 497k | return true; |
385 | 498k | } |
386 | | |
387 | | /** Helper for OP_CHECKSIG, OP_CHECKSIGVERIFY, and (in Tapscript) OP_CHECKSIGADD. |
388 | | * |
389 | | * A return value of false means the script fails entirely. When true is returned, the |
390 | | * success variable indicates whether the signature check itself succeeded. |
391 | | */ |
392 | | static bool EvalChecksig(const valtype& sig, const valtype& pubkey, CScript::const_iterator pbegincodehash, CScript::const_iterator pend, ScriptExecutionData& execdata, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror, bool& success) |
393 | 705k | { |
394 | 705k | switch (sigversion) { |
395 | 137k | case SigVersion::BASE: |
396 | 206k | case SigVersion::WITNESS_V0: |
397 | 206k | return EvalChecksigPreTapscript(sig, pubkey, pbegincodehash, pend, flags, checker, sigversion, serror, success); |
398 | 498k | case SigVersion::TAPSCRIPT: |
399 | 498k | return EvalChecksigTapscript(sig, pubkey, execdata, flags, checker, sigversion, serror, success); |
400 | 0 | case SigVersion::TAPROOT: |
401 | | // Key path spending in Taproot has no script, so this is unreachable. |
402 | 0 | break; |
403 | 705k | } |
404 | 705k | assert(false); |
405 | 0 | } |
406 | | |
407 | | bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) |
408 | 2.03M | { |
409 | 2.03M | static const CScriptNum bnZero(0); |
410 | 2.03M | static const CScriptNum bnOne(1); |
411 | | // static const CScriptNum bnFalse(0); |
412 | | // static const CScriptNum bnTrue(1); |
413 | 2.03M | static const valtype vchFalse(0); |
414 | | // static const valtype vchZero(0); |
415 | 2.03M | static const valtype vchTrue(1, 1); |
416 | | |
417 | | // sigversion cannot be TAPROOT here, as it admits no script execution. |
418 | 2.03M | assert(sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0 || sigversion == SigVersion::TAPSCRIPT); |
419 | | |
420 | 2.03M | CScript::const_iterator pc = script.begin(); |
421 | 2.03M | CScript::const_iterator pend = script.end(); |
422 | 2.03M | CScript::const_iterator pbegincodehash = script.begin(); |
423 | 2.03M | opcodetype opcode; |
424 | 2.03M | valtype vchPushValue; |
425 | 2.03M | ConditionStack vfExec; |
426 | 2.03M | std::vector<valtype> altstack; |
427 | 2.03M | set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR); |
428 | 2.03M | if ((sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0) && script.size() > MAX_SCRIPT_SIZE) { |
429 | 97 | return set_error(serror, SCRIPT_ERR_SCRIPT_SIZE); |
430 | 97 | } |
431 | 2.03M | int nOpCount = 0; |
432 | 2.03M | bool fRequireMinimal = (flags & SCRIPT_VERIFY_MINIMALDATA) != 0; |
433 | 2.03M | uint32_t opcode_pos = 0; |
434 | 2.03M | execdata.m_codeseparator_pos = 0xFFFFFFFFUL; |
435 | 2.03M | execdata.m_codeseparator_pos_init = true; |
436 | | |
437 | 2.03M | try |
438 | 2.03M | { |
439 | 16.3M | for (; pc < pend; ++opcode_pos) { |
440 | 14.5M | bool fExec = vfExec.all_true(); |
441 | | |
442 | | // |
443 | | // Read instruction |
444 | | // |
445 | 14.5M | if (!script.GetOp(pc, opcode, vchPushValue)) |
446 | 311 | return set_error(serror, SCRIPT_ERR_BAD_OPCODE); |
447 | 14.5M | if (vchPushValue.size() > MAX_SCRIPT_ELEMENT_SIZE) |
448 | 572 | return set_error(serror, SCRIPT_ERR_PUSH_SIZE); |
449 | | |
450 | 14.5M | if (sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0) { |
451 | | // Note how OP_RESERVED does not count towards the opcode limit. |
452 | 4.88M | if (opcode > OP_16 && ++nOpCount > MAX_OPS_PER_SCRIPT) { |
453 | 491 | return set_error(serror, SCRIPT_ERR_OP_COUNT); |
454 | 491 | } |
455 | 4.88M | } |
456 | | |
457 | 14.5M | if (opcode == OP_CAT || |
458 | 14.5M | opcode == OP_SUBSTR || |
459 | 14.5M | opcode == OP_LEFT || |
460 | 14.5M | opcode == OP_RIGHT || |
461 | 14.5M | opcode == OP_INVERT || |
462 | 14.5M | opcode == OP_AND || |
463 | 14.5M | opcode == OP_OR || |
464 | 14.5M | opcode == OP_XOR || |
465 | 14.5M | opcode == OP_2MUL || |
466 | 14.5M | opcode == OP_2DIV || |
467 | 14.5M | opcode == OP_MUL || |
468 | 14.5M | opcode == OP_DIV || |
469 | 14.5M | opcode == OP_MOD || |
470 | 14.5M | opcode == OP_LSHIFT || |
471 | 14.5M | opcode == OP_RSHIFT) |
472 | 2.93k | return set_error(serror, SCRIPT_ERR_DISABLED_OPCODE); // Disabled opcodes (CVE-2010-5137). |
473 | | |
474 | | // With SCRIPT_VERIFY_CONST_SCRIPTCODE, OP_CODESEPARATOR in non-segwit script is rejected even in an unexecuted branch |
475 | 14.5M | if (opcode == OP_CODESEPARATOR && sigversion == SigVersion::BASE && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE)) |
476 | 302 | return set_error(serror, SCRIPT_ERR_OP_CODESEPARATOR); |
477 | | |
478 | 14.5M | if (fExec && 0 <= opcode && opcode <= OP_PUSHDATA4) { |
479 | 3.35M | if (fRequireMinimal && !CheckMinimalPush(vchPushValue, opcode)) { |
480 | 2.79k | return set_error(serror, SCRIPT_ERR_MINIMALDATA); |
481 | 2.79k | } |
482 | 3.35M | stack.push_back(vchPushValue); |
483 | 11.1M | } else if (fExec || (OP_IF <= opcode && opcode <= OP_ENDIF)) |
484 | 10.7M | switch (opcode) |
485 | 10.7M | { |
486 | | // |
487 | | // Push value |
488 | | // |
489 | 6.70k | case OP_1NEGATE: |
490 | 536k | case OP_1: |
491 | 576k | case OP_2: |
492 | 593k | case OP_3: |
493 | 601k | case OP_4: |
494 | 608k | case OP_5: |
495 | 612k | case OP_6: |
496 | 615k | case OP_7: |
497 | 619k | case OP_8: |
498 | 623k | case OP_9: |
499 | 633k | case OP_10: |
500 | 644k | case OP_11: |
501 | 647k | case OP_12: |
502 | 649k | case OP_13: |
503 | 652k | case OP_14: |
504 | 655k | case OP_15: |
505 | 753k | case OP_16: |
506 | 753k | { |
507 | | // ( -- value) |
508 | 753k | CScriptNum bn((int)opcode - (int)(OP_1 - 1)); |
509 | 753k | stack.push_back(bn.getvch()); |
510 | | // The result of these opcodes should always be the minimal way to push the data |
511 | | // they push, so no need for a CheckMinimalPush here. |
512 | 753k | } |
513 | 753k | break; |
514 | | |
515 | | |
516 | | // |
517 | | // Control |
518 | | // |
519 | 137k | case OP_NOP: |
520 | 137k | break; |
521 | | |
522 | 12.6k | case OP_CHECKLOCKTIMEVERIFY: |
523 | 12.6k | { |
524 | 12.6k | if (!(flags & SCRIPT_VERIFY_CHECKLOCKTIMEVERIFY)) { |
525 | | // not enabled; treat as a NOP2 |
526 | 5.95k | break; |
527 | 5.95k | } |
528 | | |
529 | 6.68k | if (stack.size() < 1) |
530 | 76 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
531 | | |
532 | | // Note that elsewhere numeric opcodes are limited to |
533 | | // operands in the range -2**31+1 to 2**31-1, however it is |
534 | | // legal for opcodes to produce results exceeding that |
535 | | // range. This limitation is implemented by CScriptNum's |
536 | | // default 4-byte limit. |
537 | | // |
538 | | // If we kept to that limit we'd have a year 2038 problem, |
539 | | // even though the nLockTime field in transactions |
540 | | // themselves is uint32 which only becomes meaningless |
541 | | // after the year 2106. |
542 | | // |
543 | | // Thus as a special case we tell CScriptNum to accept up |
544 | | // to 5-byte bignums, which are good until 2**39-1, well |
545 | | // beyond the 2**32-1 limit of the nLockTime field itself. |
546 | 6.60k | const CScriptNum nLockTime(stacktop(-1), fRequireMinimal, 5); |
547 | | |
548 | | // In the rare event that the argument may be < 0 due to |
549 | | // some arithmetic being done first, you can always use |
550 | | // 0 MAX CHECKLOCKTIMEVERIFY. |
551 | 6.60k | if (nLockTime < 0) |
552 | 119 | return set_error(serror, SCRIPT_ERR_NEGATIVE_LOCKTIME); |
553 | | |
554 | | // Actually compare the specified lock time with the transaction. |
555 | 6.49k | if (!checker.CheckLockTime(nLockTime)) |
556 | 5.71k | return set_error(serror, SCRIPT_ERR_UNSATISFIED_LOCKTIME); |
557 | | |
558 | 775 | break; |
559 | 6.49k | } |
560 | | |
561 | 20.1k | case OP_CHECKSEQUENCEVERIFY: |
562 | 20.1k | { |
563 | 20.1k | if (!(flags & SCRIPT_VERIFY_CHECKSEQUENCEVERIFY)) { |
564 | | // not enabled; treat as a NOP3 |
565 | 6.40k | break; |
566 | 6.40k | } |
567 | | |
568 | 13.7k | if (stack.size() < 1) |
569 | 174 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
570 | | |
571 | | // nSequence, like nLockTime, is a 32-bit unsigned integer |
572 | | // field. See the comment in CHECKLOCKTIMEVERIFY regarding |
573 | | // 5-byte numeric operands. |
574 | 13.5k | const CScriptNum nSequence(stacktop(-1), fRequireMinimal, 5); |
575 | | |
576 | | // In the rare event that the argument may be < 0 due to |
577 | | // some arithmetic being done first, you can always use |
578 | | // 0 MAX CHECKSEQUENCEVERIFY. |
579 | 13.5k | if (nSequence < 0) |
580 | 210 | return set_error(serror, SCRIPT_ERR_NEGATIVE_LOCKTIME); |
581 | | |
582 | | // To provide for future soft-fork extensibility, if the |
583 | | // operand has the disabled lock-time flag set, |
584 | | // CHECKSEQUENCEVERIFY behaves as a NOP. |
585 | 13.3k | if ((nSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) != 0) |
586 | 691 | break; |
587 | | |
588 | | // Compare the specified sequence number with the input. |
589 | 12.6k | if (!checker.CheckSequence(nSequence)) |
590 | 5.85k | return set_error(serror, SCRIPT_ERR_UNSATISFIED_LOCKTIME); |
591 | | |
592 | 6.81k | break; |
593 | 12.6k | } |
594 | | |
595 | 6.81k | case OP_NOP1: case OP_NOP4: case OP_NOP5: |
596 | 9.97k | case OP_NOP6: case OP_NOP7: case OP_NOP8: case OP_NOP9: case OP_NOP10: |
597 | 9.97k | { |
598 | 9.97k | if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_NOPS) |
599 | 2.15k | return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_NOPS); |
600 | 9.97k | } |
601 | 7.81k | break; |
602 | | |
603 | 71.1k | case OP_IF: |
604 | 84.3k | case OP_NOTIF: |
605 | 84.3k | { |
606 | | // <expression> if [statements] [else [statements]] endif |
607 | 84.3k | bool fValue = false; |
608 | 84.3k | if (fExec) |
609 | 81.0k | { |
610 | 81.0k | if (stack.size() < 1) |
611 | 2.75k | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
612 | 78.2k | valtype& vch = stacktop(-1); |
613 | | // Tapscript requires minimal IF/NOTIF inputs as a consensus rule. |
614 | 78.2k | if (sigversion == SigVersion::TAPSCRIPT) { |
615 | | // The input argument to the OP_IF and OP_NOTIF opcodes must be either |
616 | | // exactly 0 (the empty vector) or exactly 1 (the one-byte vector with value 1). |
617 | 4.70k | if (vch.size() > 1 || (vch.size() == 1 && vch[0] != 1)) { |
618 | 9 | return set_error(serror, SCRIPT_ERR_TAPSCRIPT_MINIMALIF); |
619 | 9 | } |
620 | 4.70k | } |
621 | | // Under witness v0 rules it is only a policy rule, enabled through SCRIPT_VERIFY_MINIMALIF. |
622 | 78.2k | if (sigversion == SigVersion::WITNESS_V0 && (flags & SCRIPT_VERIFY_MINIMALIF)) { |
623 | 6.32k | if (vch.size() > 1) |
624 | 1.27k | return set_error(serror, SCRIPT_ERR_MINIMALIF); |
625 | 5.04k | if (vch.size() == 1 && vch[0] != 1) |
626 | 2.57k | return set_error(serror, SCRIPT_ERR_MINIMALIF); |
627 | 5.04k | } |
628 | 74.3k | fValue = CastToBool(vch); |
629 | 74.3k | if (opcode == OP_NOTIF) |
630 | 9.46k | fValue = !fValue; |
631 | 74.3k | popstack(stack); |
632 | 74.3k | } |
633 | 77.7k | vfExec.push_back(fValue); |
634 | 77.7k | } |
635 | 0 | break; |
636 | | |
637 | 63.2k | case OP_ELSE: |
638 | 63.2k | { |
639 | 63.2k | if (vfExec.empty()) |
640 | 665 | return set_error(serror, SCRIPT_ERR_UNBALANCED_CONDITIONAL); |
641 | 62.6k | vfExec.toggle_top(); |
642 | 62.6k | } |
643 | 0 | break; |
644 | | |
645 | 60.6k | case OP_ENDIF: |
646 | 60.6k | { |
647 | 60.6k | if (vfExec.empty()) |
648 | 1.31k | return set_error(serror, SCRIPT_ERR_UNBALANCED_CONDITIONAL); |
649 | 59.3k | vfExec.pop_back(); |
650 | 59.3k | } |
651 | 0 | break; |
652 | | |
653 | 55.8k | case OP_VERIFY: |
654 | 55.8k | { |
655 | | // (true -- ) or |
656 | | // (false -- false) and return |
657 | 55.8k | if (stack.size() < 1) |
658 | 87 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
659 | 55.7k | bool fValue = CastToBool(stacktop(-1)); |
660 | 55.7k | if (fValue) |
661 | 55.5k | popstack(stack); |
662 | 220 | else |
663 | 220 | return set_error(serror, SCRIPT_ERR_VERIFY); |
664 | 55.7k | } |
665 | 55.5k | break; |
666 | | |
667 | 55.5k | case OP_RETURN: |
668 | 1.54k | { |
669 | 1.54k | return set_error(serror, SCRIPT_ERR_OP_RETURN); |
670 | 55.7k | } |
671 | 0 | break; |
672 | | |
673 | | |
674 | | // |
675 | | // Stack ops |
676 | | // |
677 | 9.54k | case OP_TOALTSTACK: |
678 | 9.54k | { |
679 | 9.54k | if (stack.size() < 1) |
680 | 103 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
681 | 9.44k | altstack.push_back(stacktop(-1)); |
682 | 9.44k | popstack(stack); |
683 | 9.44k | } |
684 | 0 | break; |
685 | | |
686 | 5.60k | case OP_FROMALTSTACK: |
687 | 5.60k | { |
688 | 5.60k | if (altstack.size() < 1) |
689 | 272 | return set_error(serror, SCRIPT_ERR_INVALID_ALTSTACK_OPERATION); |
690 | 5.33k | stack.push_back(altstacktop(-1)); |
691 | 5.33k | popstack(altstack); |
692 | 5.33k | } |
693 | 0 | break; |
694 | | |
695 | 363k | case OP_2DROP: |
696 | 363k | { |
697 | | // (x1 x2 -- ) |
698 | 363k | if (stack.size() < 2) |
699 | 201 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
700 | 363k | popstack(stack); |
701 | 363k | popstack(stack); |
702 | 363k | } |
703 | 0 | break; |
704 | | |
705 | 288k | case OP_2DUP: |
706 | 288k | { |
707 | | // (x1 x2 -- x1 x2 x1 x2) |
708 | 288k | if (stack.size() < 2) |
709 | 483 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
710 | 287k | valtype vch1 = stacktop(-2); |
711 | 287k | valtype vch2 = stacktop(-1); |
712 | 287k | stack.push_back(vch1); |
713 | 287k | stack.push_back(vch2); |
714 | 287k | } |
715 | 0 | break; |
716 | | |
717 | 318k | case OP_3DUP: |
718 | 318k | { |
719 | | // (x1 x2 x3 -- x1 x2 x3 x1 x2 x3) |
720 | 318k | if (stack.size() < 3) |
721 | 670 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
722 | 318k | valtype vch1 = stacktop(-3); |
723 | 318k | valtype vch2 = stacktop(-2); |
724 | 318k | valtype vch3 = stacktop(-1); |
725 | 318k | stack.push_back(vch1); |
726 | 318k | stack.push_back(vch2); |
727 | 318k | stack.push_back(vch3); |
728 | 318k | } |
729 | 0 | break; |
730 | | |
731 | 1.00k | case OP_2OVER: |
732 | 1.00k | { |
733 | | // (x1 x2 x3 x4 -- x1 x2 x3 x4 x1 x2) |
734 | 1.00k | if (stack.size() < 4) |
735 | 493 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
736 | 514 | valtype vch1 = stacktop(-4); |
737 | 514 | valtype vch2 = stacktop(-3); |
738 | 514 | stack.push_back(vch1); |
739 | 514 | stack.push_back(vch2); |
740 | 514 | } |
741 | 0 | break; |
742 | | |
743 | 3.52k | case OP_2ROT: |
744 | 3.52k | { |
745 | | // (x1 x2 x3 x4 x5 x6 -- x3 x4 x5 x6 x1 x2) |
746 | 3.52k | if (stack.size() < 6) |
747 | 188 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
748 | 3.34k | valtype vch1 = stacktop(-6); |
749 | 3.34k | valtype vch2 = stacktop(-5); |
750 | 3.34k | stack.erase(stack.end()-6, stack.end()-4); |
751 | 3.34k | stack.push_back(vch1); |
752 | 3.34k | stack.push_back(vch2); |
753 | 3.34k | } |
754 | 0 | break; |
755 | | |
756 | 1.00k | case OP_2SWAP: |
757 | 1.00k | { |
758 | | // (x1 x2 x3 x4 -- x3 x4 x1 x2) |
759 | 1.00k | if (stack.size() < 4) |
760 | 488 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
761 | 514 | swap(stacktop(-4), stacktop(-2)); |
762 | 514 | swap(stacktop(-3), stacktop(-1)); |
763 | 514 | } |
764 | 0 | break; |
765 | | |
766 | 1.32k | case OP_IFDUP: |
767 | 1.32k | { |
768 | | // (x - 0 | x x) |
769 | 1.32k | if (stack.size() < 1) |
770 | 189 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
771 | 1.13k | valtype vch = stacktop(-1); |
772 | 1.13k | if (CastToBool(vch)) |
773 | 832 | stack.push_back(vch); |
774 | 1.13k | } |
775 | 0 | break; |
776 | | |
777 | 18.6k | case OP_DEPTH: |
778 | 18.6k | { |
779 | | // -- stacksize |
780 | 18.6k | CScriptNum bn(stack.size()); |
781 | 18.6k | stack.push_back(bn.getvch()); |
782 | 18.6k | } |
783 | 18.6k | break; |
784 | | |
785 | 355k | case OP_DROP: |
786 | 355k | { |
787 | | // (x -- ) |
788 | 355k | if (stack.size() < 1) |
789 | 205 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
790 | 355k | popstack(stack); |
791 | 355k | } |
792 | 0 | break; |
793 | | |
794 | 348k | case OP_DUP: |
795 | 348k | { |
796 | | // (x -- x x) |
797 | 348k | if (stack.size() < 1) |
798 | 63.2k | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
799 | 284k | valtype vch = stacktop(-1); |
800 | 284k | stack.push_back(vch); |
801 | 284k | } |
802 | 0 | break; |
803 | | |
804 | 1.17k | case OP_NIP: |
805 | 1.17k | { |
806 | | // (x1 x2 -- x2) |
807 | 1.17k | if (stack.size() < 2) |
808 | 396 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
809 | 780 | stack.erase(stack.end() - 2); |
810 | 780 | } |
811 | 0 | break; |
812 | | |
813 | 1.18k | case OP_OVER: |
814 | 1.18k | { |
815 | | // (x1 x2 -- x1 x2 x1) |
816 | 1.18k | if (stack.size() < 2) |
817 | 478 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
818 | 711 | valtype vch = stacktop(-2); |
819 | 711 | stack.push_back(vch); |
820 | 711 | } |
821 | 0 | break; |
822 | | |
823 | 3.44k | case OP_PICK: |
824 | 6.37k | case OP_ROLL: |
825 | 6.37k | { |
826 | | // (xn ... x2 x1 x0 n - xn ... x2 x1 x0 xn) |
827 | | // (xn ... x2 x1 x0 n - ... x2 x1 x0 xn) |
828 | 6.37k | if (stack.size() < 2) |
829 | 589 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
830 | 5.78k | int n = CScriptNum(stacktop(-1), fRequireMinimal).getint(); |
831 | 5.78k | popstack(stack); |
832 | 5.78k | if (n < 0 || n >= (int)stack.size()) |
833 | 973 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
834 | 4.81k | valtype vch = stacktop(-n-1); |
835 | 4.81k | if (opcode == OP_ROLL) |
836 | 2.11k | stack.erase(stack.end()-n-1); |
837 | 4.81k | stack.push_back(vch); |
838 | 4.81k | } |
839 | 0 | break; |
840 | | |
841 | 3.18k | case OP_ROT: |
842 | 3.18k | { |
843 | | // (x1 x2 x3 -- x2 x3 x1) |
844 | | // x2 x1 x3 after first swap |
845 | | // x2 x3 x1 after second swap |
846 | 3.18k | if (stack.size() < 3) |
847 | 506 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
848 | 2.68k | swap(stacktop(-3), stacktop(-2)); |
849 | 2.68k | swap(stacktop(-2), stacktop(-1)); |
850 | 2.68k | } |
851 | 0 | break; |
852 | | |
853 | 98.2k | case OP_SWAP: |
854 | 98.2k | { |
855 | | // (x1 x2 -- x2 x1) |
856 | 98.2k | if (stack.size() < 2) |
857 | 466 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
858 | 97.8k | swap(stacktop(-2), stacktop(-1)); |
859 | 97.8k | } |
860 | 0 | break; |
861 | | |
862 | 15.2k | case OP_TUCK: |
863 | 15.2k | { |
864 | | // (x1 x2 -- x2 x1 x2) |
865 | 15.2k | if (stack.size() < 2) |
866 | 486 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
867 | 14.7k | valtype vch = stacktop(-1); |
868 | 14.7k | stack.insert(stack.end()-2, vch); |
869 | 14.7k | } |
870 | 0 | break; |
871 | | |
872 | | |
873 | 8.40k | case OP_SIZE: |
874 | 8.40k | { |
875 | | // (in -- in size) |
876 | 8.40k | if (stack.size() < 1) |
877 | 189 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
878 | 8.22k | CScriptNum bn(stacktop(-1).size()); |
879 | 8.22k | stack.push_back(bn.getvch()); |
880 | 8.22k | } |
881 | 0 | break; |
882 | | |
883 | | |
884 | | // |
885 | | // Bitwise logic |
886 | | // |
887 | 140k | case OP_EQUAL: |
888 | 319k | case OP_EQUALVERIFY: |
889 | | //case OP_NOTEQUAL: // use OP_NUMNOTEQUAL |
890 | 319k | { |
891 | | // (x1 x2 - bool) |
892 | 319k | if (stack.size() < 2) |
893 | 780 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
894 | 318k | valtype& vch1 = stacktop(-2); |
895 | 318k | valtype& vch2 = stacktop(-1); |
896 | 318k | bool fEqual = (vch1 == vch2); |
897 | | // OP_NOTEQUAL is disabled because it would be too easy to say |
898 | | // something like n != 1 and have some wiseguy pass in 1 with extra |
899 | | // zero bytes after it (numerically, 0x01 == 0x0001 == 0x000001) |
900 | | //if (opcode == OP_NOTEQUAL) |
901 | | // fEqual = !fEqual; |
902 | 318k | popstack(stack); |
903 | 318k | popstack(stack); |
904 | 318k | stack.push_back(fEqual ? vchTrue : vchFalse); |
905 | 318k | if (opcode == OP_EQUALVERIFY) |
906 | 178k | { |
907 | 178k | if (fEqual) |
908 | 176k | popstack(stack); |
909 | 2.03k | else |
910 | 2.03k | return set_error(serror, SCRIPT_ERR_EQUALVERIFY); |
911 | 178k | } |
912 | 318k | } |
913 | 316k | break; |
914 | | |
915 | | |
916 | | // |
917 | | // Numeric |
918 | | // |
919 | 316k | case OP_1ADD: |
920 | 3.86k | case OP_1SUB: |
921 | 5.29k | case OP_NEGATE: |
922 | 6.82k | case OP_ABS: |
923 | 29.4k | case OP_NOT: |
924 | 6.29M | case OP_0NOTEQUAL: |
925 | 6.29M | { |
926 | | // (in -- out) |
927 | 6.29M | if (stack.size() < 1) |
928 | 594 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
929 | 6.29M | CScriptNum bn(stacktop(-1), fRequireMinimal); |
930 | 6.29M | switch (opcode) |
931 | 6.29M | { |
932 | 1.82k | case OP_1ADD: bn += bnOne; break; |
933 | 814 | case OP_1SUB: bn -= bnOne; break; |
934 | 1.02k | case OP_NEGATE: bn = -bn; break; |
935 | 1.28k | case OP_ABS: if (bn < bnZero) bn = -bn; break; |
936 | 20.7k | case OP_NOT: bn = (bn == bnZero); break; |
937 | 6.26M | case OP_0NOTEQUAL: bn = (bn != bnZero); break; |
938 | 0 | default: assert(!"invalid opcode"); break; |
939 | 6.29M | } |
940 | 6.28M | popstack(stack); |
941 | 6.28M | stack.push_back(bn.getvch()); |
942 | 6.28M | } |
943 | 0 | break; |
944 | | |
945 | 9.57k | case OP_ADD: |
946 | 11.4k | case OP_SUB: |
947 | 19.1k | case OP_BOOLAND: |
948 | 22.5k | case OP_BOOLOR: |
949 | 31.9k | case OP_NUMEQUAL: |
950 | 33.6k | case OP_NUMEQUALVERIFY: |
951 | 35.4k | case OP_NUMNOTEQUAL: |
952 | 37.9k | case OP_LESSTHAN: |
953 | 40.4k | case OP_GREATERTHAN: |
954 | 42.9k | case OP_LESSTHANOREQUAL: |
955 | 45.4k | case OP_GREATERTHANOREQUAL: |
956 | 47.6k | case OP_MIN: |
957 | 49.9k | case OP_MAX: |
958 | 49.9k | { |
959 | | // (x1 x2 -- out) |
960 | 49.9k | if (stack.size() < 2) |
961 | 2.53k | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
962 | 47.4k | CScriptNum bn1(stacktop(-2), fRequireMinimal); |
963 | 47.4k | CScriptNum bn2(stacktop(-1), fRequireMinimal); |
964 | 47.4k | CScriptNum bn(0); |
965 | 47.4k | switch (opcode) |
966 | 47.4k | { |
967 | 8.93k | case OP_ADD: |
968 | 8.93k | bn = bn1 + bn2; |
969 | 8.93k | break; |
970 | | |
971 | 1.41k | case OP_SUB: |
972 | 1.41k | bn = bn1 - bn2; |
973 | 1.41k | break; |
974 | | |
975 | 6.90k | case OP_BOOLAND: bn = (bn1 != bnZero && bn2 != bnZero); break; |
976 | 2.54k | case OP_BOOLOR: bn = (bn1 != bnZero || bn2 != bnZero); break; |
977 | 8.86k | case OP_NUMEQUAL: bn = (bn1 == bn2); break; |
978 | 1.22k | case OP_NUMEQUALVERIFY: bn = (bn1 == bn2); break; |
979 | 1.28k | case OP_NUMNOTEQUAL: bn = (bn1 != bn2); break; |
980 | 2.05k | case OP_LESSTHAN: bn = (bn1 < bn2); break; |
981 | 2.05k | case OP_GREATERTHAN: bn = (bn1 > bn2); break; |
982 | 2.05k | case OP_LESSTHANOREQUAL: bn = (bn1 <= bn2); break; |
983 | 2.05k | case OP_GREATERTHANOREQUAL: bn = (bn1 >= bn2); break; |
984 | 1.79k | case OP_MIN: bn = (bn1 < bn2 ? bn1 : bn2); break; |
985 | 1.79k | case OP_MAX: bn = (bn1 > bn2 ? bn1 : bn2); break; |
986 | 0 | default: assert(!"invalid opcode"); break; |
987 | 47.4k | } |
988 | 42.9k | popstack(stack); |
989 | 42.9k | popstack(stack); |
990 | 42.9k | stack.push_back(bn.getvch()); |
991 | | |
992 | 42.9k | if (opcode == OP_NUMEQUALVERIFY) |
993 | 1.22k | { |
994 | 1.22k | if (CastToBool(stacktop(-1))) |
995 | 1.03k | popstack(stack); |
996 | 190 | else |
997 | 190 | return set_error(serror, SCRIPT_ERR_NUMEQUALVERIFY); |
998 | 1.22k | } |
999 | 42.9k | } |
1000 | 42.7k | break; |
1001 | | |
1002 | 42.7k | case OP_WITHIN: |
1003 | 3.47k | { |
1004 | | // (x min max -- out) |
1005 | 3.47k | if (stack.size() < 3) |
1006 | 192 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1007 | 3.28k | CScriptNum bn1(stacktop(-3), fRequireMinimal); |
1008 | 3.28k | CScriptNum bn2(stacktop(-2), fRequireMinimal); |
1009 | 3.28k | CScriptNum bn3(stacktop(-1), fRequireMinimal); |
1010 | 3.28k | bool fValue = (bn2 <= bn1 && bn1 < bn3); |
1011 | 3.28k | popstack(stack); |
1012 | 3.28k | popstack(stack); |
1013 | 3.28k | popstack(stack); |
1014 | 3.28k | stack.push_back(fValue ? vchTrue : vchFalse); |
1015 | 3.28k | } |
1016 | 0 | break; |
1017 | | |
1018 | | |
1019 | | // |
1020 | | // Crypto |
1021 | | // |
1022 | 1.52k | case OP_RIPEMD160: |
1023 | 13.0k | case OP_SHA1: |
1024 | 15.3k | case OP_SHA256: |
1025 | 175k | case OP_HASH160: |
1026 | 176k | case OP_HASH256: |
1027 | 176k | { |
1028 | | // (in -- hash) |
1029 | 176k | if (stack.size() < 1) |
1030 | 8.56k | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1031 | 168k | valtype& vch = stacktop(-1); |
1032 | 168k | valtype vchHash((opcode == OP_RIPEMD160 || opcode == OP_SHA1 || opcode == OP_HASH160) ? 20 : 32); |
1033 | 168k | if (opcode == OP_RIPEMD160) |
1034 | 1.32k | CRIPEMD160().Write(vch.data(), vch.size()).Finalize(vchHash.data()); |
1035 | 166k | else if (opcode == OP_SHA1) |
1036 | 11.3k | CSHA1().Write(vch.data(), vch.size()).Finalize(vchHash.data()); |
1037 | 155k | else if (opcode == OP_SHA256) |
1038 | 2.04k | CSHA256().Write(vch.data(), vch.size()).Finalize(vchHash.data()); |
1039 | 153k | else if (opcode == OP_HASH160) |
1040 | 152k | CHash160().Write(vch).Finalize(vchHash); |
1041 | 1.43k | else if (opcode == OP_HASH256) |
1042 | 1.43k | CHash256().Write(vch).Finalize(vchHash); |
1043 | 168k | popstack(stack); |
1044 | 168k | stack.push_back(vchHash); |
1045 | 168k | } |
1046 | 0 | break; |
1047 | | |
1048 | 4.96k | case OP_CODESEPARATOR: |
1049 | 4.96k | { |
1050 | | // If SCRIPT_VERIFY_CONST_SCRIPTCODE flag is set, use of OP_CODESEPARATOR is rejected in pre-segwit |
1051 | | // script, even in an unexecuted branch (this is checked above the opcode case statement). |
1052 | | |
1053 | | // Hash starts after the code separator |
1054 | 4.96k | pbegincodehash = pc; |
1055 | 4.96k | execdata.m_codeseparator_pos = opcode_pos; |
1056 | 4.96k | } |
1057 | 4.96k | break; |
1058 | | |
1059 | 240k | case OP_CHECKSIG: |
1060 | 519k | case OP_CHECKSIGVERIFY: |
1061 | 519k | { |
1062 | | // (sig pubkey -- bool) |
1063 | 519k | if (stack.size() < 2) |
1064 | 9.34k | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1065 | | |
1066 | 510k | valtype& vchSig = stacktop(-2); |
1067 | 510k | valtype& vchPubKey = stacktop(-1); |
1068 | | |
1069 | 510k | bool fSuccess = true; |
1070 | 510k | if (!EvalChecksig(vchSig, vchPubKey, pbegincodehash, pend, execdata, flags, checker, sigversion, serror, fSuccess)) return false; |
1071 | 488k | popstack(stack); |
1072 | 488k | popstack(stack); |
1073 | 488k | stack.push_back(fSuccess ? vchTrue : vchFalse); |
1074 | 488k | if (opcode == OP_CHECKSIGVERIFY) |
1075 | 279k | { |
1076 | 279k | if (fSuccess) |
1077 | 279k | popstack(stack); |
1078 | 188 | else |
1079 | 188 | return set_error(serror, SCRIPT_ERR_CHECKSIGVERIFY); |
1080 | 279k | } |
1081 | 488k | } |
1082 | 488k | break; |
1083 | | |
1084 | 488k | case OP_CHECKSIGADD: |
1085 | 195k | { |
1086 | | // OP_CHECKSIGADD is only available in Tapscript |
1087 | 195k | if (sigversion == SigVersion::BASE || sigversion == SigVersion::WITNESS_V0) return set_error(serror, SCRIPT_ERR_BAD_OPCODE); |
1088 | | |
1089 | | // (sig num pubkey -- num) |
1090 | 194k | if (stack.size() < 3) return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1091 | | |
1092 | 194k | const valtype& sig = stacktop(-3); |
1093 | 194k | const CScriptNum num(stacktop(-2), fRequireMinimal); |
1094 | 194k | const valtype& pubkey = stacktop(-1); |
1095 | | |
1096 | 194k | bool success = true; |
1097 | 194k | if (!EvalChecksig(sig, pubkey, pbegincodehash, pend, execdata, flags, checker, sigversion, serror, success)) return false; |
1098 | 193k | popstack(stack); |
1099 | 193k | popstack(stack); |
1100 | 193k | popstack(stack); |
1101 | 193k | stack.push_back((num + (success ? 1 : 0)).getvch()); |
1102 | 193k | } |
1103 | 0 | break; |
1104 | | |
1105 | 102k | case OP_CHECKMULTISIG: |
1106 | 165k | case OP_CHECKMULTISIGVERIFY: |
1107 | 165k | { |
1108 | 165k | if (sigversion == SigVersion::TAPSCRIPT) return set_error(serror, SCRIPT_ERR_TAPSCRIPT_CHECKMULTISIG); |
1109 | | |
1110 | | // ([sig ...] num_of_signatures [pubkey ...] num_of_pubkeys -- bool) |
1111 | | |
1112 | 165k | int i = 1; |
1113 | 165k | if ((int)stack.size() < i) |
1114 | 133 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1115 | | |
1116 | 165k | int nKeysCount = CScriptNum(stacktop(-i), fRequireMinimal).getint(); |
1117 | 165k | if (nKeysCount < 0 || nKeysCount > MAX_PUBKEYS_PER_MULTISIG) |
1118 | 307 | return set_error(serror, SCRIPT_ERR_PUBKEY_COUNT); |
1119 | 165k | nOpCount += nKeysCount; |
1120 | 165k | if (nOpCount > MAX_OPS_PER_SCRIPT) |
1121 | 390 | return set_error(serror, SCRIPT_ERR_OP_COUNT); |
1122 | 164k | int ikey = ++i; |
1123 | | // ikey2 is the position of last non-signature item in the stack. Top stack item = 1. |
1124 | | // With SCRIPT_VERIFY_NULLFAIL, this is used for cleanup if operation fails. |
1125 | 164k | int ikey2 = nKeysCount + 2; |
1126 | 164k | i += nKeysCount; |
1127 | 164k | if ((int)stack.size() < i) |
1128 | 131 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1129 | | |
1130 | 164k | int nSigsCount = CScriptNum(stacktop(-i), fRequireMinimal).getint(); |
1131 | 164k | if (nSigsCount < 0 || nSigsCount > nKeysCount) |
1132 | 311 | return set_error(serror, SCRIPT_ERR_SIG_COUNT); |
1133 | 164k | int isig = ++i; |
1134 | 164k | i += nSigsCount; |
1135 | 164k | if ((int)stack.size() < i) |
1136 | 334 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1137 | | |
1138 | | // Subset of script starting at the most recent codeseparator |
1139 | 164k | CScript scriptCode(pbegincodehash, pend); |
1140 | | |
1141 | | // Drop the signature in pre-segwit scripts but not segwit scripts |
1142 | 229k | for (int k = 0; k < nSigsCount; k++) |
1143 | 65.6k | { |
1144 | 65.6k | valtype& vchSig = stacktop(-isig-k); |
1145 | 65.6k | if (sigversion == SigVersion::BASE) { |
1146 | 58.5k | int found = FindAndDelete(scriptCode, CScript() << vchSig); |
1147 | 58.5k | if (found > 0 && (flags & SCRIPT_VERIFY_CONST_SCRIPTCODE)) |
1148 | 183 | return set_error(serror, SCRIPT_ERR_SIG_FINDANDDELETE); |
1149 | 58.5k | } |
1150 | 65.6k | } |
1151 | | |
1152 | 163k | bool fSuccess = true; |
1153 | 188k | while (fSuccess && nSigsCount > 0) |
1154 | 30.1k | { |
1155 | 30.1k | valtype& vchSig = stacktop(-isig); |
1156 | 30.1k | valtype& vchPubKey = stacktop(-ikey); |
1157 | | |
1158 | | // Note how this makes the exact order of pubkey/signature evaluation |
1159 | | // distinguishable by CHECKMULTISIG NOT if the STRICTENC flag is set. |
1160 | | // See the script_(in)valid tests for details. |
1161 | 30.1k | if (!CheckSignatureEncoding(vchSig, flags, serror) || !CheckPubKeyEncoding(vchPubKey, flags, sigversion, serror)) { |
1162 | | // serror is set |
1163 | 5.91k | return false; |
1164 | 5.91k | } |
1165 | | |
1166 | | // Check signature |
1167 | 24.2k | bool fOk = checker.CheckECDSASignature(vchSig, vchPubKey, scriptCode, sigversion); |
1168 | | |
1169 | 24.2k | if (fOk) { |
1170 | 14.3k | isig++; |
1171 | 14.3k | nSigsCount--; |
1172 | 14.3k | } |
1173 | 24.2k | ikey++; |
1174 | 24.2k | nKeysCount--; |
1175 | | |
1176 | | // If there are more signatures left than keys left, |
1177 | | // then too many signatures have failed. Exit early, |
1178 | | // without checking any further signatures. |
1179 | 24.2k | if (nSigsCount > nKeysCount) |
1180 | 6.63k | fSuccess = false; |
1181 | 24.2k | } |
1182 | | |
1183 | | // Clean up stack of actual arguments |
1184 | 845k | while (i-- > 1) { |
1185 | | // If the operation failed, we require that all signatures must be empty vector |
1186 | 688k | if (!fSuccess && (flags & SCRIPT_VERIFY_NULLFAIL) && !ikey2 && stacktop(-1).size()) |
1187 | 928 | return set_error(serror, SCRIPT_ERR_SIG_NULLFAIL); |
1188 | 687k | if (ikey2 > 0) |
1189 | 636k | ikey2--; |
1190 | 687k | popstack(stack); |
1191 | 687k | } |
1192 | | |
1193 | | // A bug causes CHECKMULTISIG to consume one extra argument |
1194 | | // whose contents were not checked in any way. |
1195 | | // |
1196 | | // Unfortunately this is a potential source of mutability, |
1197 | | // so optionally verify it is exactly equal to zero prior |
1198 | | // to removing it from the stack. |
1199 | 157k | if (stack.size() < 1) |
1200 | 0 | return set_error(serror, SCRIPT_ERR_INVALID_STACK_OPERATION); |
1201 | 157k | if ((flags & SCRIPT_VERIFY_NULLDUMMY) && stacktop(-1).size()) |
1202 | 747 | return set_error(serror, SCRIPT_ERR_SIG_NULLDUMMY); |
1203 | 156k | popstack(stack); |
1204 | | |
1205 | 156k | stack.push_back(fSuccess ? vchTrue : vchFalse); |
1206 | | |
1207 | 156k | if (opcode == OP_CHECKMULTISIGVERIFY) |
1208 | 62.6k | { |
1209 | 62.6k | if (fSuccess) |
1210 | 62.5k | popstack(stack); |
1211 | 48 | else |
1212 | 48 | return set_error(serror, SCRIPT_ERR_CHECKMULTISIGVERIFY); |
1213 | 62.6k | } |
1214 | 156k | } |
1215 | 156k | break; |
1216 | | |
1217 | 156k | default: |
1218 | 16.2k | return set_error(serror, SCRIPT_ERR_BAD_OPCODE); |
1219 | 10.7M | } |
1220 | | |
1221 | | // Size limits |
1222 | 14.3M | if (stack.size() + altstack.size() > MAX_STACK_SIZE) |
1223 | 494 | return set_error(serror, SCRIPT_ERR_STACK_SIZE); |
1224 | 14.3M | } |
1225 | 2.03M | } |
1226 | 2.03M | catch (const scriptnum_error&) |
1227 | 2.03M | { |
1228 | 9.33k | return set_error(serror, SCRIPT_ERR_SCRIPTNUM); |
1229 | 9.33k | } |
1230 | 2.03M | catch (...) |
1231 | 2.03M | { |
1232 | 0 | return set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR); |
1233 | 0 | } |
1234 | | |
1235 | 1.85M | if (!vfExec.empty()) |
1236 | 681 | return set_error(serror, SCRIPT_ERR_UNBALANCED_CONDITIONAL); |
1237 | | |
1238 | 1.85M | return set_success(serror); |
1239 | 1.85M | } |
1240 | | |
1241 | | bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* serror) |
1242 | 1.87M | { |
1243 | 1.87M | ScriptExecutionData execdata; |
1244 | 1.87M | return EvalScript(stack, script, flags, checker, sigversion, execdata, serror); |
1245 | 1.87M | } |
1246 | | |
1247 | | namespace { |
1248 | | |
1249 | | /** |
1250 | | * Wrapper that serializes like CTransaction, but with the modifications |
1251 | | * required for the signature hash done in-place |
1252 | | */ |
1253 | | template <class T> |
1254 | | class CTransactionSignatureSerializer |
1255 | | { |
1256 | | private: |
1257 | | const T& txTo; //!< reference to the spending transaction (the one being serialized) |
1258 | | const CScript& scriptCode; //!< output script being consumed |
1259 | | const unsigned int nIn; //!< input index of txTo being signed |
1260 | | const bool fAnyoneCanPay; //!< whether the hashtype has the SIGHASH_ANYONECANPAY flag set |
1261 | | const bool fHashSingle; //!< whether the hashtype is SIGHASH_SINGLE |
1262 | | const bool fHashNone; //!< whether the hashtype is SIGHASH_NONE |
1263 | | |
1264 | | public: |
1265 | | CTransactionSignatureSerializer(const T& txToIn, const CScript& scriptCodeIn, unsigned int nInIn, int nHashTypeIn) : |
1266 | 122k | txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn), |
1267 | 122k | fAnyoneCanPay(!!(nHashTypeIn & SIGHASH_ANYONECANPAY)), |
1268 | 122k | fHashSingle((nHashTypeIn & 0x1f) == SIGHASH_SINGLE), |
1269 | 122k | fHashNone((nHashTypeIn & 0x1f) == SIGHASH_NONE) {}interpreter.cpp:(anonymous namespace)::CTransactionSignatureSerializer<CTransaction>::CTransactionSignatureSerializer(CTransaction const&, CScript const&, unsigned int, int) Line | Count | Source | 1266 | 40.0k | txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn), | 1267 | 40.0k | fAnyoneCanPay(!!(nHashTypeIn & SIGHASH_ANYONECANPAY)), | 1268 | 40.0k | fHashSingle((nHashTypeIn & 0x1f) == SIGHASH_SINGLE), | 1269 | 40.0k | fHashNone((nHashTypeIn & 0x1f) == SIGHASH_NONE) {} |
interpreter.cpp:(anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>::CTransactionSignatureSerializer(CMutableTransaction const&, CScript const&, unsigned int, int) Line | Count | Source | 1266 | 82.6k | txTo(txToIn), scriptCode(scriptCodeIn), nIn(nInIn), | 1267 | 82.6k | fAnyoneCanPay(!!(nHashTypeIn & SIGHASH_ANYONECANPAY)), | 1268 | 82.6k | fHashSingle((nHashTypeIn & 0x1f) == SIGHASH_SINGLE), | 1269 | 82.6k | fHashNone((nHashTypeIn & 0x1f) == SIGHASH_NONE) {} |
|
1270 | | |
1271 | | /** Serialize the passed scriptCode, skipping OP_CODESEPARATORs */ |
1272 | | template<typename S> |
1273 | 122k | void SerializeScriptCode(S &s) const { |
1274 | 122k | CScript::const_iterator it = scriptCode.begin(); |
1275 | 122k | CScript::const_iterator itBegin = it; |
1276 | 122k | opcodetype opcode; |
1277 | 122k | unsigned int nCodeSeparators = 0; |
1278 | 692k | while (scriptCode.GetOp(it, opcode)) { |
1279 | 569k | if (opcode == OP_CODESEPARATOR) |
1280 | 26.9k | nCodeSeparators++; |
1281 | 569k | } |
1282 | 122k | ::WriteCompactSize(s, scriptCode.size() - nCodeSeparators); |
1283 | 122k | it = itBegin; |
1284 | 692k | while (scriptCode.GetOp(it, opcode)) { |
1285 | 569k | if (opcode == OP_CODESEPARATOR) { |
1286 | 26.9k | s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin - 1)})); |
1287 | 26.9k | itBegin = it; |
1288 | 26.9k | } |
1289 | 569k | } |
1290 | 122k | if (itBegin != scriptCode.end()) |
1291 | 112k | s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin)})); |
1292 | 122k | } interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CTransaction>::SerializeScriptCode<HashWriter>(HashWriter&) const Line | Count | Source | 1273 | 40.0k | void SerializeScriptCode(S &s) const { | 1274 | 40.0k | CScript::const_iterator it = scriptCode.begin(); | 1275 | 40.0k | CScript::const_iterator itBegin = it; | 1276 | 40.0k | opcodetype opcode; | 1277 | 40.0k | unsigned int nCodeSeparators = 0; | 1278 | 248k | while (scriptCode.GetOp(it, opcode)) { | 1279 | 208k | if (opcode == OP_CODESEPARATOR) | 1280 | 1.72k | nCodeSeparators++; | 1281 | 208k | } | 1282 | 40.0k | ::WriteCompactSize(s, scriptCode.size() - nCodeSeparators); | 1283 | 40.0k | it = itBegin; | 1284 | 248k | while (scriptCode.GetOp(it, opcode)) { | 1285 | 208k | if (opcode == OP_CODESEPARATOR) { | 1286 | 1.72k | s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin - 1)})); | 1287 | 1.72k | itBegin = it; | 1288 | 1.72k | } | 1289 | 208k | } | 1290 | 40.0k | if (itBegin != scriptCode.end()) | 1291 | 39.9k | s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin)})); | 1292 | 40.0k | } |
interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>::SerializeScriptCode<HashWriter>(HashWriter&) const Line | Count | Source | 1273 | 82.6k | void SerializeScriptCode(S &s) const { | 1274 | 82.6k | CScript::const_iterator it = scriptCode.begin(); | 1275 | 82.6k | CScript::const_iterator itBegin = it; | 1276 | 82.6k | opcodetype opcode; | 1277 | 82.6k | unsigned int nCodeSeparators = 0; | 1278 | 443k | while (scriptCode.GetOp(it, opcode)) { | 1279 | 360k | if (opcode == OP_CODESEPARATOR) | 1280 | 25.2k | nCodeSeparators++; | 1281 | 360k | } | 1282 | 82.6k | ::WriteCompactSize(s, scriptCode.size() - nCodeSeparators); | 1283 | 82.6k | it = itBegin; | 1284 | 443k | while (scriptCode.GetOp(it, opcode)) { | 1285 | 360k | if (opcode == OP_CODESEPARATOR) { | 1286 | 25.2k | s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin - 1)})); | 1287 | 25.2k | itBegin = it; | 1288 | 25.2k | } | 1289 | 360k | } | 1290 | 82.6k | if (itBegin != scriptCode.end()) | 1291 | 72.6k | s.write(std::as_bytes(std::span{&itBegin[0], size_t(it - itBegin)})); | 1292 | 82.6k | } |
|
1293 | | |
1294 | | /** Serialize an input of txTo */ |
1295 | | template<typename S> |
1296 | 857k | void SerializeInput(S &s, unsigned int nInput) const { |
1297 | | // In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized |
1298 | 857k | if (fAnyoneCanPay) |
1299 | 26.5k | nInput = nIn; |
1300 | | // Serialize the prevout |
1301 | 857k | ::Serialize(s, txTo.vin[nInput].prevout); |
1302 | | // Serialize the script |
1303 | 857k | if (nInput != nIn) |
1304 | | // Blank out other inputs' signatures |
1305 | 735k | ::Serialize(s, CScript()); |
1306 | 122k | else |
1307 | 122k | SerializeScriptCode(s); |
1308 | | // Serialize the nSequence |
1309 | 857k | if (nInput != nIn && (fHashSingle || fHashNone)) |
1310 | | // let the others update at will |
1311 | 2.97k | ::Serialize(s, int32_t{0}); |
1312 | 854k | else |
1313 | 854k | ::Serialize(s, txTo.vin[nInput].nSequence); |
1314 | 857k | } interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CTransaction>::SerializeInput<HashWriter>(HashWriter&, unsigned int) const Line | Count | Source | 1296 | 219k | void SerializeInput(S &s, unsigned int nInput) const { | 1297 | | // In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized | 1298 | 219k | if (fAnyoneCanPay) | 1299 | 1.05k | nInput = nIn; | 1300 | | // Serialize the prevout | 1301 | 219k | ::Serialize(s, txTo.vin[nInput].prevout); | 1302 | | // Serialize the script | 1303 | 219k | if (nInput != nIn) | 1304 | | // Blank out other inputs' signatures | 1305 | 179k | ::Serialize(s, CScript()); | 1306 | 40.0k | else | 1307 | 40.0k | SerializeScriptCode(s); | 1308 | | // Serialize the nSequence | 1309 | 219k | if (nInput != nIn && (fHashSingle || fHashNone)) | 1310 | | // let the others update at will | 1311 | 634 | ::Serialize(s, int32_t{0}); | 1312 | 218k | else | 1313 | 218k | ::Serialize(s, txTo.vin[nInput].nSequence); | 1314 | 219k | } |
interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>::SerializeInput<HashWriter>(HashWriter&, unsigned int) const Line | Count | Source | 1296 | 638k | void SerializeInput(S &s, unsigned int nInput) const { | 1297 | | // In case of SIGHASH_ANYONECANPAY, only the input being signed is serialized | 1298 | 638k | if (fAnyoneCanPay) | 1299 | 25.5k | nInput = nIn; | 1300 | | // Serialize the prevout | 1301 | 638k | ::Serialize(s, txTo.vin[nInput].prevout); | 1302 | | // Serialize the script | 1303 | 638k | if (nInput != nIn) | 1304 | | // Blank out other inputs' signatures | 1305 | 555k | ::Serialize(s, CScript()); | 1306 | 82.6k | else | 1307 | 82.6k | SerializeScriptCode(s); | 1308 | | // Serialize the nSequence | 1309 | 638k | if (nInput != nIn && (fHashSingle || fHashNone)) | 1310 | | // let the others update at will | 1311 | 2.34k | ::Serialize(s, int32_t{0}); | 1312 | 636k | else | 1313 | 636k | ::Serialize(s, txTo.vin[nInput].nSequence); | 1314 | 638k | } |
|
1315 | | |
1316 | | /** Serialize an output of txTo */ |
1317 | | template<typename S> |
1318 | 450k | void SerializeOutput(S &s, unsigned int nOutput) const { |
1319 | 450k | if (fHashSingle && nOutput != nIn) |
1320 | | // Do not lock-in the txout payee at other indices as txin |
1321 | 1.51k | ::Serialize(s, CTxOut()); |
1322 | 448k | else |
1323 | 448k | ::Serialize(s, txTo.vout[nOutput]); |
1324 | 450k | } interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CTransaction>::SerializeOutput<HashWriter>(HashWriter&, unsigned int) const Line | Count | Source | 1318 | 186k | void SerializeOutput(S &s, unsigned int nOutput) const { | 1319 | 186k | if (fHashSingle && nOutput != nIn) | 1320 | | // Do not lock-in the txout payee at other indices as txin | 1321 | 327 | ::Serialize(s, CTxOut()); | 1322 | 186k | else | 1323 | 186k | ::Serialize(s, txTo.vout[nOutput]); | 1324 | 186k | } |
interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>::SerializeOutput<HashWriter>(HashWriter&, unsigned int) const Line | Count | Source | 1318 | 263k | void SerializeOutput(S &s, unsigned int nOutput) const { | 1319 | 263k | if (fHashSingle && nOutput != nIn) | 1320 | | // Do not lock-in the txout payee at other indices as txin | 1321 | 1.18k | ::Serialize(s, CTxOut()); | 1322 | 262k | else | 1323 | 262k | ::Serialize(s, txTo.vout[nOutput]); | 1324 | 263k | } |
|
1325 | | |
1326 | | /** Serialize txTo */ |
1327 | | template<typename S> |
1328 | 122k | void Serialize(S &s) const { |
1329 | | // Serialize version |
1330 | 122k | ::Serialize(s, txTo.version); |
1331 | | // Serialize vin |
1332 | 122k | unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size(); |
1333 | 122k | ::WriteCompactSize(s, nInputs); |
1334 | 980k | for (unsigned int nInput = 0; nInput < nInputs; nInput++) |
1335 | 857k | SerializeInput(s, nInput); |
1336 | | // Serialize vout |
1337 | 122k | unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size()); |
1338 | 122k | ::WriteCompactSize(s, nOutputs); |
1339 | 573k | for (unsigned int nOutput = 0; nOutput < nOutputs; nOutput++) |
1340 | 450k | SerializeOutput(s, nOutput); |
1341 | | // Serialize nLockTime |
1342 | 122k | ::Serialize(s, txTo.nLockTime); |
1343 | 122k | } interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CTransaction>::Serialize<HashWriter>(HashWriter&) const Line | Count | Source | 1328 | 40.0k | void Serialize(S &s) const { | 1329 | | // Serialize version | 1330 | 40.0k | ::Serialize(s, txTo.version); | 1331 | | // Serialize vin | 1332 | 40.0k | unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size(); | 1333 | 40.0k | ::WriteCompactSize(s, nInputs); | 1334 | 259k | for (unsigned int nInput = 0; nInput < nInputs; nInput++) | 1335 | 219k | SerializeInput(s, nInput); | 1336 | | // Serialize vout | 1337 | 40.0k | unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size()); | 1338 | 40.0k | ::WriteCompactSize(s, nOutputs); | 1339 | 226k | for (unsigned int nOutput = 0; nOutput < nOutputs; nOutput++) | 1340 | 186k | SerializeOutput(s, nOutput); | 1341 | | // Serialize nLockTime | 1342 | 40.0k | ::Serialize(s, txTo.nLockTime); | 1343 | 40.0k | } |
interpreter.cpp:void (anonymous namespace)::CTransactionSignatureSerializer<CMutableTransaction>::Serialize<HashWriter>(HashWriter&) const Line | Count | Source | 1328 | 82.6k | void Serialize(S &s) const { | 1329 | | // Serialize version | 1330 | 82.6k | ::Serialize(s, txTo.version); | 1331 | | // Serialize vin | 1332 | 82.6k | unsigned int nInputs = fAnyoneCanPay ? 1 : txTo.vin.size(); | 1333 | 82.6k | ::WriteCompactSize(s, nInputs); | 1334 | 721k | for (unsigned int nInput = 0; nInput < nInputs; nInput++) | 1335 | 638k | SerializeInput(s, nInput); | 1336 | | // Serialize vout | 1337 | 82.6k | unsigned int nOutputs = fHashNone ? 0 : (fHashSingle ? nIn+1 : txTo.vout.size()); | 1338 | 82.6k | ::WriteCompactSize(s, nOutputs); | 1339 | 346k | for (unsigned int nOutput = 0; nOutput < nOutputs; nOutput++) | 1340 | 263k | SerializeOutput(s, nOutput); | 1341 | | // Serialize nLockTime | 1342 | 82.6k | ::Serialize(s, txTo.nLockTime); | 1343 | 82.6k | } |
|
1344 | | }; |
1345 | | |
1346 | | /** Compute the (single) SHA256 of the concatenation of all prevouts of a tx. */ |
1347 | | template <class T> |
1348 | | uint256 GetPrevoutsSHA256(const T& txTo) |
1349 | 73.9k | { |
1350 | 73.9k | HashWriter ss{}; |
1351 | 20.4M | for (const auto& txin : txTo.vin) { |
1352 | 20.4M | ss << txin.prevout; |
1353 | 20.4M | } |
1354 | 73.9k | return ss.GetSHA256(); |
1355 | 73.9k | } interpreter.cpp:uint256 (anonymous namespace)::GetPrevoutsSHA256<CTransaction>(CTransaction const&) Line | Count | Source | 1349 | 58.7k | { | 1350 | 58.7k | HashWriter ss{}; | 1351 | 134k | for (const auto& txin : txTo.vin) { | 1352 | 134k | ss << txin.prevout; | 1353 | 134k | } | 1354 | 58.7k | return ss.GetSHA256(); | 1355 | 58.7k | } |
interpreter.cpp:uint256 (anonymous namespace)::GetPrevoutsSHA256<CMutableTransaction>(CMutableTransaction const&) Line | Count | Source | 1349 | 15.2k | { | 1350 | 15.2k | HashWriter ss{}; | 1351 | 20.3M | for (const auto& txin : txTo.vin) { | 1352 | 20.3M | ss << txin.prevout; | 1353 | 20.3M | } | 1354 | 15.2k | return ss.GetSHA256(); | 1355 | 15.2k | } |
|
1356 | | |
1357 | | /** Compute the (single) SHA256 of the concatenation of all nSequences of a tx. */ |
1358 | | template <class T> |
1359 | | uint256 GetSequencesSHA256(const T& txTo) |
1360 | 70.9k | { |
1361 | 70.9k | HashWriter ss{}; |
1362 | 6.95M | for (const auto& txin : txTo.vin) { |
1363 | 6.95M | ss << txin.nSequence; |
1364 | 6.95M | } |
1365 | 70.9k | return ss.GetSHA256(); |
1366 | 70.9k | } interpreter.cpp:uint256 (anonymous namespace)::GetSequencesSHA256<CTransaction>(CTransaction const&) Line | Count | Source | 1360 | 58.7k | { | 1361 | 58.7k | HashWriter ss{}; | 1362 | 134k | for (const auto& txin : txTo.vin) { | 1363 | 134k | ss << txin.nSequence; | 1364 | 134k | } | 1365 | 58.7k | return ss.GetSHA256(); | 1366 | 58.7k | } |
interpreter.cpp:uint256 (anonymous namespace)::GetSequencesSHA256<CMutableTransaction>(CMutableTransaction const&) Line | Count | Source | 1360 | 12.2k | { | 1361 | 12.2k | HashWriter ss{}; | 1362 | 6.81M | for (const auto& txin : txTo.vin) { | 1363 | 6.81M | ss << txin.nSequence; | 1364 | 6.81M | } | 1365 | 12.2k | return ss.GetSHA256(); | 1366 | 12.2k | } |
|
1367 | | |
1368 | | /** Compute the (single) SHA256 of the concatenation of all txouts of a tx. */ |
1369 | | template <class T> |
1370 | | uint256 GetOutputsSHA256(const T& txTo) |
1371 | 72.5k | { |
1372 | 72.5k | HashWriter ss{}; |
1373 | 13.8M | for (const auto& txout : txTo.vout) { |
1374 | 13.8M | ss << txout; |
1375 | 13.8M | } |
1376 | 72.5k | return ss.GetSHA256(); |
1377 | 72.5k | } interpreter.cpp:uint256 (anonymous namespace)::GetOutputsSHA256<CTransaction>(CTransaction const&) Line | Count | Source | 1371 | 58.7k | { | 1372 | 58.7k | HashWriter ss{}; | 1373 | 333k | for (const auto& txout : txTo.vout) { | 1374 | 333k | ss << txout; | 1375 | 333k | } | 1376 | 58.7k | return ss.GetSHA256(); | 1377 | 58.7k | } |
interpreter.cpp:uint256 (anonymous namespace)::GetOutputsSHA256<CMutableTransaction>(CMutableTransaction const&) Line | Count | Source | 1371 | 13.7k | { | 1372 | 13.7k | HashWriter ss{}; | 1373 | 13.5M | for (const auto& txout : txTo.vout) { | 1374 | 13.5M | ss << txout; | 1375 | 13.5M | } | 1376 | 13.7k | return ss.GetSHA256(); | 1377 | 13.7k | } |
|
1378 | | |
1379 | | /** Compute the (single) SHA256 of the concatenation of all amounts spent by a tx. */ |
1380 | | uint256 GetSpentAmountsSHA256(const std::vector<CTxOut>& outputs_spent) |
1381 | 54.4k | { |
1382 | 54.4k | HashWriter ss{}; |
1383 | 115k | for (const auto& txout : outputs_spent) { |
1384 | 115k | ss << txout.nValue; |
1385 | 115k | } |
1386 | 54.4k | return ss.GetSHA256(); |
1387 | 54.4k | } |
1388 | | |
1389 | | /** Compute the (single) SHA256 of the concatenation of all scriptPubKeys spent by a tx. */ |
1390 | | uint256 GetSpentScriptsSHA256(const std::vector<CTxOut>& outputs_spent) |
1391 | 54.4k | { |
1392 | 54.4k | HashWriter ss{}; |
1393 | 115k | for (const auto& txout : outputs_spent) { |
1394 | 115k | ss << txout.scriptPubKey; |
1395 | 115k | } |
1396 | 54.4k | return ss.GetSHA256(); |
1397 | 54.4k | } |
1398 | | |
1399 | | |
1400 | | } // namespace |
1401 | | |
1402 | | template <class T> |
1403 | | void PrecomputedTransactionData::Init(const T& txTo, std::vector<CTxOut>&& spent_outputs, bool force) |
1404 | 86.1k | { |
1405 | 86.1k | assert(!m_spent_outputs_ready); |
1406 | | |
1407 | 86.1k | m_spent_outputs = std::move(spent_outputs); |
1408 | 86.1k | if (!m_spent_outputs.empty()) { |
1409 | 85.8k | assert(m_spent_outputs.size() == txTo.vin.size()); |
1410 | 85.8k | m_spent_outputs_ready = true; |
1411 | 85.8k | } |
1412 | | |
1413 | | // Determine which precomputation-impacting features this transaction uses. |
1414 | 86.1k | bool uses_bip143_segwit = force; |
1415 | 86.1k | bool uses_bip341_taproot = force; |
1416 | 194k | for (size_t inpos = 0; inpos < txTo.vin.size() && !(uses_bip143_segwit && uses_bip341_taproot); ++inpos) { |
1417 | 109k | if (!txTo.vin[inpos].scriptWitness.IsNull()) { |
1418 | 72.4k | if (m_spent_outputs_ready && m_spent_outputs[inpos].scriptPubKey.size() == 2 + WITNESS_V1_TAPROOT_SIZE && |
1419 | 72.4k | m_spent_outputs[inpos].scriptPubKey[0] == OP_1) { |
1420 | | // Treat every witness-bearing spend with 34-byte scriptPubKey that starts with OP_1 as a Taproot |
1421 | | // spend. This only works if spent_outputs was provided as well, but if it wasn't, actual validation |
1422 | | // will fail anyway. Note that this branch may trigger for scriptPubKeys that aren't actually segwit |
1423 | | // but in that case validation will fail as SCRIPT_ERR_WITNESS_UNEXPECTED anyway. |
1424 | 48.2k | uses_bip341_taproot = true; |
1425 | 48.2k | } else { |
1426 | | // Treat every spend that's not known to native witness v1 as a Witness v0 spend. This branch may |
1427 | | // also be taken for unknown witness versions, but it is harmless, and being precise would require |
1428 | | // P2SH evaluation to find the redeemScript. |
1429 | 24.2k | uses_bip143_segwit = true; |
1430 | 24.2k | } |
1431 | 72.4k | } |
1432 | 109k | if (uses_bip341_taproot && uses_bip143_segwit) break; // No need to scan further if we already need all. |
1433 | 109k | } |
1434 | | |
1435 | 86.1k | if (uses_bip143_segwit || uses_bip341_taproot) { |
1436 | | // Computations shared between both sighash schemes. |
1437 | 60.7k | m_prevouts_single_hash = GetPrevoutsSHA256(txTo); |
1438 | 60.7k | m_sequences_single_hash = GetSequencesSHA256(txTo); |
1439 | 60.7k | m_outputs_single_hash = GetOutputsSHA256(txTo); |
1440 | 60.7k | } |
1441 | 86.1k | if (uses_bip143_segwit) { |
1442 | 27.4k | hashPrevouts = SHA256Uint256(m_prevouts_single_hash); |
1443 | 27.4k | hashSequence = SHA256Uint256(m_sequences_single_hash); |
1444 | 27.4k | hashOutputs = SHA256Uint256(m_outputs_single_hash); |
1445 | 27.4k | m_bip143_segwit_ready = true; |
1446 | 27.4k | } |
1447 | 86.1k | if (uses_bip341_taproot && m_spent_outputs_ready) { |
1448 | 54.4k | m_spent_amounts_single_hash = GetSpentAmountsSHA256(m_spent_outputs); |
1449 | 54.4k | m_spent_scripts_single_hash = GetSpentScriptsSHA256(m_spent_outputs); |
1450 | 54.4k | m_bip341_taproot_ready = true; |
1451 | 54.4k | } |
1452 | 86.1k | } void PrecomputedTransactionData::Init<CTransaction>(CTransaction const&, std::vector<CTxOut, std::allocator<CTxOut>>&&, bool) Line | Count | Source | 1404 | 84.1k | { | 1405 | 84.1k | assert(!m_spent_outputs_ready); | 1406 | | | 1407 | 84.1k | m_spent_outputs = std::move(spent_outputs); | 1408 | 84.1k | if (!m_spent_outputs.empty()) { | 1409 | 83.9k | assert(m_spent_outputs.size() == txTo.vin.size()); | 1410 | 83.9k | m_spent_outputs_ready = true; | 1411 | 83.9k | } | 1412 | | | 1413 | | // Determine which precomputation-impacting features this transaction uses. | 1414 | 84.1k | bool uses_bip143_segwit = force; | 1415 | 84.1k | bool uses_bip341_taproot = force; | 1416 | 192k | for (size_t inpos = 0; inpos < txTo.vin.size() && !(uses_bip143_segwit && uses_bip341_taproot); ++inpos) { | 1417 | 109k | if (!txTo.vin[inpos].scriptWitness.IsNull()) { | 1418 | 72.4k | if (m_spent_outputs_ready && m_spent_outputs[inpos].scriptPubKey.size() == 2 + WITNESS_V1_TAPROOT_SIZE && | 1419 | 72.4k | m_spent_outputs[inpos].scriptPubKey[0] == OP_1) { | 1420 | | // Treat every witness-bearing spend with 34-byte scriptPubKey that starts with OP_1 as a Taproot | 1421 | | // spend. This only works if spent_outputs was provided as well, but if it wasn't, actual validation | 1422 | | // will fail anyway. Note that this branch may trigger for scriptPubKeys that aren't actually segwit | 1423 | | // but in that case validation will fail as SCRIPT_ERR_WITNESS_UNEXPECTED anyway. | 1424 | 48.2k | uses_bip341_taproot = true; | 1425 | 48.2k | } else { | 1426 | | // Treat every spend that's not known to native witness v1 as a Witness v0 spend. This branch may | 1427 | | // also be taken for unknown witness versions, but it is harmless, and being precise would require | 1428 | | // P2SH evaluation to find the redeemScript. | 1429 | 24.2k | uses_bip143_segwit = true; | 1430 | 24.2k | } | 1431 | 72.4k | } | 1432 | 109k | if (uses_bip341_taproot && uses_bip143_segwit) break; // No need to scan further if we already need all. | 1433 | 109k | } | 1434 | | | 1435 | 84.1k | if (uses_bip143_segwit || uses_bip341_taproot) { | 1436 | | // Computations shared between both sighash schemes. | 1437 | 58.7k | m_prevouts_single_hash = GetPrevoutsSHA256(txTo); | 1438 | 58.7k | m_sequences_single_hash = GetSequencesSHA256(txTo); | 1439 | 58.7k | m_outputs_single_hash = GetOutputsSHA256(txTo); | 1440 | 58.7k | } | 1441 | 84.1k | if (uses_bip143_segwit) { | 1442 | 25.4k | hashPrevouts = SHA256Uint256(m_prevouts_single_hash); | 1443 | 25.4k | hashSequence = SHA256Uint256(m_sequences_single_hash); | 1444 | 25.4k | hashOutputs = SHA256Uint256(m_outputs_single_hash); | 1445 | 25.4k | m_bip143_segwit_ready = true; | 1446 | 25.4k | } | 1447 | 84.1k | if (uses_bip341_taproot && m_spent_outputs_ready) { | 1448 | 52.4k | m_spent_amounts_single_hash = GetSpentAmountsSHA256(m_spent_outputs); | 1449 | 52.4k | m_spent_scripts_single_hash = GetSpentScriptsSHA256(m_spent_outputs); | 1450 | 52.4k | m_bip341_taproot_ready = true; | 1451 | 52.4k | } | 1452 | 84.1k | } |
void PrecomputedTransactionData::Init<CMutableTransaction>(CMutableTransaction const&, std::vector<CTxOut, std::allocator<CTxOut>>&&, bool) Line | Count | Source | 1404 | 2.00k | { | 1405 | 2.00k | assert(!m_spent_outputs_ready); | 1406 | | | 1407 | 2.00k | m_spent_outputs = std::move(spent_outputs); | 1408 | 2.00k | if (!m_spent_outputs.empty()) { | 1409 | 1.96k | assert(m_spent_outputs.size() == txTo.vin.size()); | 1410 | 1.96k | m_spent_outputs_ready = true; | 1411 | 1.96k | } | 1412 | | | 1413 | | // Determine which precomputation-impacting features this transaction uses. | 1414 | 2.00k | bool uses_bip143_segwit = force; | 1415 | 2.00k | bool uses_bip341_taproot = force; | 1416 | 2.01k | for (size_t inpos = 0; inpos < txTo.vin.size() && !(uses_bip143_segwit && uses_bip341_taproot); ++inpos) { | 1417 | 8 | if (!txTo.vin[inpos].scriptWitness.IsNull()) { | 1418 | 0 | if (m_spent_outputs_ready && m_spent_outputs[inpos].scriptPubKey.size() == 2 + WITNESS_V1_TAPROOT_SIZE && | 1419 | 0 | m_spent_outputs[inpos].scriptPubKey[0] == OP_1) { | 1420 | | // Treat every witness-bearing spend with 34-byte scriptPubKey that starts with OP_1 as a Taproot | 1421 | | // spend. This only works if spent_outputs was provided as well, but if it wasn't, actual validation | 1422 | | // will fail anyway. Note that this branch may trigger for scriptPubKeys that aren't actually segwit | 1423 | | // but in that case validation will fail as SCRIPT_ERR_WITNESS_UNEXPECTED anyway. | 1424 | 0 | uses_bip341_taproot = true; | 1425 | 0 | } else { | 1426 | | // Treat every spend that's not known to native witness v1 as a Witness v0 spend. This branch may | 1427 | | // also be taken for unknown witness versions, but it is harmless, and being precise would require | 1428 | | // P2SH evaluation to find the redeemScript. | 1429 | 0 | uses_bip143_segwit = true; | 1430 | 0 | } | 1431 | 0 | } | 1432 | 8 | if (uses_bip341_taproot && uses_bip143_segwit) break; // No need to scan further if we already need all. | 1433 | 8 | } | 1434 | | | 1435 | 2.00k | if (uses_bip143_segwit || uses_bip341_taproot) { | 1436 | | // Computations shared between both sighash schemes. | 1437 | 1.99k | m_prevouts_single_hash = GetPrevoutsSHA256(txTo); | 1438 | 1.99k | m_sequences_single_hash = GetSequencesSHA256(txTo); | 1439 | 1.99k | m_outputs_single_hash = GetOutputsSHA256(txTo); | 1440 | 1.99k | } | 1441 | 2.00k | if (uses_bip143_segwit) { | 1442 | 1.99k | hashPrevouts = SHA256Uint256(m_prevouts_single_hash); | 1443 | 1.99k | hashSequence = SHA256Uint256(m_sequences_single_hash); | 1444 | 1.99k | hashOutputs = SHA256Uint256(m_outputs_single_hash); | 1445 | 1.99k | m_bip143_segwit_ready = true; | 1446 | 1.99k | } | 1447 | 2.00k | if (uses_bip341_taproot && m_spent_outputs_ready) { | 1448 | 1.96k | m_spent_amounts_single_hash = GetSpentAmountsSHA256(m_spent_outputs); | 1449 | 1.96k | m_spent_scripts_single_hash = GetSpentScriptsSHA256(m_spent_outputs); | 1450 | 1.96k | m_bip341_taproot_ready = true; | 1451 | 1.96k | } | 1452 | 2.00k | } |
|
1453 | | |
1454 | | template <class T> |
1455 | | PrecomputedTransactionData::PrecomputedTransactionData(const T& txTo) |
1456 | 214 | { |
1457 | 214 | Init(txTo, {}); |
1458 | 214 | } PrecomputedTransactionData::PrecomputedTransactionData<CTransaction>(CTransaction const&) Line | Count | Source | 1456 | 206 | { | 1457 | 206 | Init(txTo, {}); | 1458 | 206 | } |
PrecomputedTransactionData::PrecomputedTransactionData<CMutableTransaction>(CMutableTransaction const&) Line | Count | Source | 1456 | 8 | { | 1457 | 8 | Init(txTo, {}); | 1458 | 8 | } |
|
1459 | | |
1460 | | // explicit instantiation |
1461 | | template void PrecomputedTransactionData::Init(const CTransaction& txTo, std::vector<CTxOut>&& spent_outputs, bool force); |
1462 | | template void PrecomputedTransactionData::Init(const CMutableTransaction& txTo, std::vector<CTxOut>&& spent_outputs, bool force); |
1463 | | template PrecomputedTransactionData::PrecomputedTransactionData(const CTransaction& txTo); |
1464 | | template PrecomputedTransactionData::PrecomputedTransactionData(const CMutableTransaction& txTo); |
1465 | | |
1466 | | const HashWriter HASHER_TAPSIGHASH{TaggedHash("TapSighash")}; |
1467 | | const HashWriter HASHER_TAPLEAF{TaggedHash("TapLeaf")}; |
1468 | | const HashWriter HASHER_TAPBRANCH{TaggedHash("TapBranch")}; |
1469 | | |
1470 | | static bool HandleMissingData(MissingDataBehavior mdb) |
1471 | 30 | { |
1472 | 30 | switch (mdb) { |
1473 | 0 | case MissingDataBehavior::ASSERT_FAIL: |
1474 | 0 | assert(!"Missing data"); |
1475 | 0 | break; |
1476 | 30 | case MissingDataBehavior::FAIL: |
1477 | 30 | return false; |
1478 | 30 | } |
1479 | 30 | assert(!"Unknown MissingDataBehavior value"); |
1480 | 0 | } |
1481 | | |
1482 | | template<typename T> |
1483 | | bool SignatureHashSchnorr(uint256& hash_out, ScriptExecutionData& execdata, const T& tx_to, uint32_t in_pos, uint8_t hash_type, SigVersion sigversion, const PrecomputedTransactionData& cache, MissingDataBehavior mdb) |
1484 | 263k | { |
1485 | 263k | uint8_t ext_flag, key_version; |
1486 | 263k | switch (sigversion) { |
1487 | 6.03k | case SigVersion::TAPROOT: |
1488 | 6.03k | ext_flag = 0; |
1489 | | // key_version is not used and left uninitialized. |
1490 | 6.03k | break; |
1491 | 257k | case SigVersion::TAPSCRIPT: |
1492 | 257k | ext_flag = 1; |
1493 | | // key_version must be 0 for now, representing the current version of |
1494 | | // 32-byte public keys in the tapscript signature opcode execution. |
1495 | | // An upgradable public key version (with a size not 32-byte) may |
1496 | | // request a different key_version with a new sigversion. |
1497 | 257k | key_version = 0; |
1498 | 257k | break; |
1499 | 0 | default: |
1500 | 0 | assert(false); |
1501 | 263k | } |
1502 | 263k | assert(in_pos < tx_to.vin.size()); |
1503 | 263k | if (!(cache.m_bip341_taproot_ready && cache.m_spent_outputs_ready)) { |
1504 | 0 | return HandleMissingData(mdb); |
1505 | 0 | } |
1506 | | |
1507 | 263k | HashWriter ss{HASHER_TAPSIGHASH}; |
1508 | | |
1509 | | // Epoch |
1510 | 263k | static constexpr uint8_t EPOCH = 0; |
1511 | 263k | ss << EPOCH; |
1512 | | |
1513 | | // Hash type |
1514 | 263k | const uint8_t output_type = (hash_type == SIGHASH_DEFAULT) ? SIGHASH_ALL : (hash_type & SIGHASH_OUTPUT_MASK); // Default (no sighash byte) is equivalent to SIGHASH_ALL |
1515 | 263k | const uint8_t input_type = hash_type & SIGHASH_INPUT_MASK; |
1516 | 263k | if (!(hash_type <= 0x03 || (hash_type >= 0x81 && hash_type <= 0x83))) return false; |
1517 | 263k | ss << hash_type; |
1518 | | |
1519 | | // Transaction level data |
1520 | 263k | ss << tx_to.version; |
1521 | 263k | ss << tx_to.nLockTime; |
1522 | 263k | if (input_type != SIGHASH_ANYONECANPAY) { |
1523 | 199k | ss << cache.m_prevouts_single_hash; |
1524 | 199k | ss << cache.m_spent_amounts_single_hash; |
1525 | 199k | ss << cache.m_spent_scripts_single_hash; |
1526 | 199k | ss << cache.m_sequences_single_hash; |
1527 | 199k | } |
1528 | 263k | if (output_type == SIGHASH_ALL) { |
1529 | 176k | ss << cache.m_outputs_single_hash; |
1530 | 176k | } |
1531 | | |
1532 | | // Data about the input/prevout being spent |
1533 | 263k | assert(execdata.m_annex_init); |
1534 | 263k | const bool have_annex = execdata.m_annex_present; |
1535 | 263k | const uint8_t spend_type = (ext_flag << 1) + (have_annex ? 1 : 0); // The low bit indicates whether an annex is present. |
1536 | 263k | ss << spend_type; |
1537 | 263k | if (input_type == SIGHASH_ANYONECANPAY) { |
1538 | 63.1k | ss << tx_to.vin[in_pos].prevout; |
1539 | 63.1k | ss << cache.m_spent_outputs[in_pos]; |
1540 | 63.1k | ss << tx_to.vin[in_pos].nSequence; |
1541 | 199k | } else { |
1542 | 199k | ss << in_pos; |
1543 | 199k | } |
1544 | 263k | if (have_annex) { |
1545 | 76.3k | ss << execdata.m_annex_hash; |
1546 | 76.3k | } |
1547 | | |
1548 | | // Data about the output (if only one). |
1549 | 263k | if (output_type == SIGHASH_SINGLE) { |
1550 | 37.5k | if (in_pos >= tx_to.vout.size()) return false; |
1551 | 37.5k | if (!execdata.m_output_hash) { |
1552 | 1.76k | HashWriter sha_single_output{}; |
1553 | 1.76k | sha_single_output << tx_to.vout[in_pos]; |
1554 | 1.76k | execdata.m_output_hash = sha_single_output.GetSHA256(); |
1555 | 1.76k | } |
1556 | 37.5k | ss << execdata.m_output_hash.value(); |
1557 | 37.5k | } |
1558 | | |
1559 | | // Additional data for BIP 342 signatures |
1560 | 263k | if (sigversion == SigVersion::TAPSCRIPT) { |
1561 | 257k | assert(execdata.m_tapleaf_hash_init); |
1562 | 257k | ss << execdata.m_tapleaf_hash; |
1563 | 257k | ss << key_version; |
1564 | 257k | assert(execdata.m_codeseparator_pos_init); |
1565 | 257k | ss << execdata.m_codeseparator_pos; |
1566 | 257k | } |
1567 | | |
1568 | 263k | hash_out = ss.GetSHA256(); |
1569 | 263k | return true; |
1570 | 263k | } bool SignatureHashSchnorr<CTransaction>(uint256&, ScriptExecutionData&, CTransaction const&, unsigned int, unsigned char, SigVersion, PrecomputedTransactionData const&, MissingDataBehavior) Line | Count | Source | 1484 | 260k | { | 1485 | 260k | uint8_t ext_flag, key_version; | 1486 | 260k | switch (sigversion) { | 1487 | 4.47k | case SigVersion::TAPROOT: | 1488 | 4.47k | ext_flag = 0; | 1489 | | // key_version is not used and left uninitialized. | 1490 | 4.47k | break; | 1491 | 256k | case SigVersion::TAPSCRIPT: | 1492 | 256k | ext_flag = 1; | 1493 | | // key_version must be 0 for now, representing the current version of | 1494 | | // 32-byte public keys in the tapscript signature opcode execution. | 1495 | | // An upgradable public key version (with a size not 32-byte) may | 1496 | | // request a different key_version with a new sigversion. | 1497 | 256k | key_version = 0; | 1498 | 256k | break; | 1499 | 0 | default: | 1500 | 0 | assert(false); | 1501 | 260k | } | 1502 | 260k | assert(in_pos < tx_to.vin.size()); | 1503 | 260k | if (!(cache.m_bip341_taproot_ready && cache.m_spent_outputs_ready)) { | 1504 | 0 | return HandleMissingData(mdb); | 1505 | 0 | } | 1506 | | | 1507 | 260k | HashWriter ss{HASHER_TAPSIGHASH}; | 1508 | | | 1509 | | // Epoch | 1510 | 260k | static constexpr uint8_t EPOCH = 0; | 1511 | 260k | ss << EPOCH; | 1512 | | | 1513 | | // Hash type | 1514 | 260k | const uint8_t output_type = (hash_type == SIGHASH_DEFAULT) ? SIGHASH_ALL : (hash_type & SIGHASH_OUTPUT_MASK); // Default (no sighash byte) is equivalent to SIGHASH_ALL | 1515 | 260k | const uint8_t input_type = hash_type & SIGHASH_INPUT_MASK; | 1516 | 260k | if (!(hash_type <= 0x03 || (hash_type >= 0x81 && hash_type <= 0x83))) return false; | 1517 | 260k | ss << hash_type; | 1518 | | | 1519 | | // Transaction level data | 1520 | 260k | ss << tx_to.version; | 1521 | 260k | ss << tx_to.nLockTime; | 1522 | 260k | if (input_type != SIGHASH_ANYONECANPAY) { | 1523 | 196k | ss << cache.m_prevouts_single_hash; | 1524 | 196k | ss << cache.m_spent_amounts_single_hash; | 1525 | 196k | ss << cache.m_spent_scripts_single_hash; | 1526 | 196k | ss << cache.m_sequences_single_hash; | 1527 | 196k | } | 1528 | 260k | if (output_type == SIGHASH_ALL) { | 1529 | 173k | ss << cache.m_outputs_single_hash; | 1530 | 173k | } | 1531 | | | 1532 | | // Data about the input/prevout being spent | 1533 | 260k | assert(execdata.m_annex_init); | 1534 | 260k | const bool have_annex = execdata.m_annex_present; | 1535 | 260k | const uint8_t spend_type = (ext_flag << 1) + (have_annex ? 1 : 0); // The low bit indicates whether an annex is present. | 1536 | 260k | ss << spend_type; | 1537 | 260k | if (input_type == SIGHASH_ANYONECANPAY) { | 1538 | 63.0k | ss << tx_to.vin[in_pos].prevout; | 1539 | 63.0k | ss << cache.m_spent_outputs[in_pos]; | 1540 | 63.0k | ss << tx_to.vin[in_pos].nSequence; | 1541 | 196k | } else { | 1542 | 196k | ss << in_pos; | 1543 | 196k | } | 1544 | 260k | if (have_annex) { | 1545 | 76.3k | ss << execdata.m_annex_hash; | 1546 | 76.3k | } | 1547 | | | 1548 | | // Data about the output (if only one). | 1549 | 260k | if (output_type == SIGHASH_SINGLE) { | 1550 | 37.5k | if (in_pos >= tx_to.vout.size()) return false; | 1551 | 37.5k | if (!execdata.m_output_hash) { | 1552 | 1.76k | HashWriter sha_single_output{}; | 1553 | 1.76k | sha_single_output << tx_to.vout[in_pos]; | 1554 | 1.76k | execdata.m_output_hash = sha_single_output.GetSHA256(); | 1555 | 1.76k | } | 1556 | 37.5k | ss << execdata.m_output_hash.value(); | 1557 | 37.5k | } | 1558 | | | 1559 | | // Additional data for BIP 342 signatures | 1560 | 260k | if (sigversion == SigVersion::TAPSCRIPT) { | 1561 | 255k | assert(execdata.m_tapleaf_hash_init); | 1562 | 255k | ss << execdata.m_tapleaf_hash; | 1563 | 255k | ss << key_version; | 1564 | 255k | assert(execdata.m_codeseparator_pos_init); | 1565 | 255k | ss << execdata.m_codeseparator_pos; | 1566 | 255k | } | 1567 | | | 1568 | 260k | hash_out = ss.GetSHA256(); | 1569 | 260k | return true; | 1570 | 260k | } |
bool SignatureHashSchnorr<CMutableTransaction>(uint256&, ScriptExecutionData&, CMutableTransaction const&, unsigned int, unsigned char, SigVersion, PrecomputedTransactionData const&, MissingDataBehavior) Line | Count | Source | 1484 | 2.97k | { | 1485 | 2.97k | uint8_t ext_flag, key_version; | 1486 | 2.97k | switch (sigversion) { | 1487 | 1.56k | case SigVersion::TAPROOT: | 1488 | 1.56k | ext_flag = 0; | 1489 | | // key_version is not used and left uninitialized. | 1490 | 1.56k | break; | 1491 | 1.40k | case SigVersion::TAPSCRIPT: | 1492 | 1.40k | ext_flag = 1; | 1493 | | // key_version must be 0 for now, representing the current version of | 1494 | | // 32-byte public keys in the tapscript signature opcode execution. | 1495 | | // An upgradable public key version (with a size not 32-byte) may | 1496 | | // request a different key_version with a new sigversion. | 1497 | 1.40k | key_version = 0; | 1498 | 1.40k | break; | 1499 | 0 | default: | 1500 | 0 | assert(false); | 1501 | 2.97k | } | 1502 | 2.97k | assert(in_pos < tx_to.vin.size()); | 1503 | 2.97k | if (!(cache.m_bip341_taproot_ready && cache.m_spent_outputs_ready)) { | 1504 | 0 | return HandleMissingData(mdb); | 1505 | 0 | } | 1506 | | | 1507 | 2.97k | HashWriter ss{HASHER_TAPSIGHASH}; | 1508 | | | 1509 | | // Epoch | 1510 | 2.97k | static constexpr uint8_t EPOCH = 0; | 1511 | 2.97k | ss << EPOCH; | 1512 | | | 1513 | | // Hash type | 1514 | 2.97k | const uint8_t output_type = (hash_type == SIGHASH_DEFAULT) ? SIGHASH_ALL : (hash_type & SIGHASH_OUTPUT_MASK); // Default (no sighash byte) is equivalent to SIGHASH_ALL | 1515 | 2.97k | const uint8_t input_type = hash_type & SIGHASH_INPUT_MASK; | 1516 | 2.97k | if (!(hash_type <= 0x03 || (hash_type >= 0x81 && hash_type <= 0x83))) return false; | 1517 | 2.97k | ss << hash_type; | 1518 | | | 1519 | | // Transaction level data | 1520 | 2.97k | ss << tx_to.version; | 1521 | 2.97k | ss << tx_to.nLockTime; | 1522 | 2.97k | if (input_type != SIGHASH_ANYONECANPAY) { | 1523 | 2.94k | ss << cache.m_prevouts_single_hash; | 1524 | 2.94k | ss << cache.m_spent_amounts_single_hash; | 1525 | 2.94k | ss << cache.m_spent_scripts_single_hash; | 1526 | 2.94k | ss << cache.m_sequences_single_hash; | 1527 | 2.94k | } | 1528 | 2.97k | if (output_type == SIGHASH_ALL) { | 1529 | 2.96k | ss << cache.m_outputs_single_hash; | 1530 | 2.96k | } | 1531 | | | 1532 | | // Data about the input/prevout being spent | 1533 | 2.97k | assert(execdata.m_annex_init); | 1534 | 2.97k | const bool have_annex = execdata.m_annex_present; | 1535 | 2.97k | const uint8_t spend_type = (ext_flag << 1) + (have_annex ? 1 : 0); // The low bit indicates whether an annex is present. | 1536 | 2.97k | ss << spend_type; | 1537 | 2.97k | if (input_type == SIGHASH_ANYONECANPAY) { | 1538 | 26 | ss << tx_to.vin[in_pos].prevout; | 1539 | 26 | ss << cache.m_spent_outputs[in_pos]; | 1540 | 26 | ss << tx_to.vin[in_pos].nSequence; | 1541 | 2.94k | } else { | 1542 | 2.94k | ss << in_pos; | 1543 | 2.94k | } | 1544 | 2.97k | if (have_annex) { | 1545 | 0 | ss << execdata.m_annex_hash; | 1546 | 0 | } | 1547 | | | 1548 | | // Data about the output (if only one). | 1549 | 2.97k | if (output_type == SIGHASH_SINGLE) { | 1550 | 4 | if (in_pos >= tx_to.vout.size()) return false; | 1551 | 4 | if (!execdata.m_output_hash) { | 1552 | 4 | HashWriter sha_single_output{}; | 1553 | 4 | sha_single_output << tx_to.vout[in_pos]; | 1554 | 4 | execdata.m_output_hash = sha_single_output.GetSHA256(); | 1555 | 4 | } | 1556 | 4 | ss << execdata.m_output_hash.value(); | 1557 | 4 | } | 1558 | | | 1559 | | // Additional data for BIP 342 signatures | 1560 | 2.97k | if (sigversion == SigVersion::TAPSCRIPT) { | 1561 | 1.40k | assert(execdata.m_tapleaf_hash_init); | 1562 | 1.40k | ss << execdata.m_tapleaf_hash; | 1563 | 1.40k | ss << key_version; | 1564 | 1.40k | assert(execdata.m_codeseparator_pos_init); | 1565 | 1.40k | ss << execdata.m_codeseparator_pos; | 1566 | 1.40k | } | 1567 | | | 1568 | 2.97k | hash_out = ss.GetSHA256(); | 1569 | 2.97k | return true; | 1570 | 2.97k | } |
|
1571 | | |
1572 | | int SigHashCache::CacheIndex(int32_t hash_type) const noexcept |
1573 | 318k | { |
1574 | | // Note that we do not distinguish between BASE and WITNESS_V0 to determine the cache index, |
1575 | | // because no input can simultaneously use both. |
1576 | 318k | return 3 * !!(hash_type & SIGHASH_ANYONECANPAY) + |
1577 | 318k | 2 * ((hash_type & 0x1f) == SIGHASH_SINGLE) + |
1578 | 318k | 1 * ((hash_type & 0x1f) == SIGHASH_NONE); |
1579 | 318k | } |
1580 | | |
1581 | | bool SigHashCache::Load(int32_t hash_type, const CScript& script_code, HashWriter& writer) const noexcept |
1582 | 187k | { |
1583 | 187k | auto& entry = m_cache_entries[CacheIndex(hash_type)]; |
1584 | 187k | if (entry.has_value()) { |
1585 | 57.9k | if (script_code == entry->first) { |
1586 | 57.0k | writer = HashWriter(entry->second); |
1587 | 57.0k | return true; |
1588 | 57.0k | } |
1589 | 57.9k | } |
1590 | 130k | return false; |
1591 | 187k | } |
1592 | | |
1593 | | void SigHashCache::Store(int32_t hash_type, const CScript& script_code, const HashWriter& writer) noexcept |
1594 | 130k | { |
1595 | 130k | auto& entry = m_cache_entries[CacheIndex(hash_type)]; |
1596 | 130k | entry.emplace(script_code, writer); |
1597 | 130k | } |
1598 | | |
1599 | | template <class T> |
1600 | | uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int32_t nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache, SigHashCache* sighash_cache) |
1601 | 255k | { |
1602 | 255k | assert(nIn < txTo.vin.size()); |
1603 | | |
1604 | 255k | if (sigversion != SigVersion::WITNESS_V0) { |
1605 | | // Check for invalid use of SIGHASH_SINGLE |
1606 | 175k | if ((nHashType & 0x1f) == SIGHASH_SINGLE) { |
1607 | 5.23k | if (nIn >= txTo.vout.size()) { |
1608 | | // nOut out of range |
1609 | 759 | return uint256::ONE; |
1610 | 759 | } |
1611 | 5.23k | } |
1612 | 175k | } |
1613 | | |
1614 | 254k | HashWriter ss{}; |
1615 | | |
1616 | | // Try to compute using cached SHA256 midstate. |
1617 | 254k | if (sighash_cache && sighash_cache->Load(nHashType, scriptCode, ss)) { |
1618 | | // Add sighash type and hash. |
1619 | 56.9k | ss << nHashType; |
1620 | 56.9k | return ss.GetHash(); |
1621 | 56.9k | } |
1622 | | |
1623 | 197k | if (sigversion == SigVersion::WITNESS_V0) { |
1624 | 75.2k | uint256 hashPrevouts; |
1625 | 75.2k | uint256 hashSequence; |
1626 | 75.2k | uint256 hashOutputs; |
1627 | 75.2k | const bool cacheready = cache && cache->m_bip143_segwit_ready; |
1628 | | |
1629 | 75.2k | if (!(nHashType & SIGHASH_ANYONECANPAY)) { |
1630 | 63.2k | hashPrevouts = cacheready ? cache->hashPrevouts : SHA256Uint256(GetPrevoutsSHA256(txTo)); |
1631 | 63.2k | } |
1632 | | |
1633 | 75.2k | if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { |
1634 | 56.3k | hashSequence = cacheready ? cache->hashSequence : SHA256Uint256(GetSequencesSHA256(txTo)); |
1635 | 56.3k | } |
1636 | | |
1637 | 75.2k | if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { |
1638 | 61.3k | hashOutputs = cacheready ? cache->hashOutputs : SHA256Uint256(GetOutputsSHA256(txTo)); |
1639 | 61.3k | } else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) { |
1640 | 6.45k | HashWriter inner_ss{}; |
1641 | 6.45k | inner_ss << txTo.vout[nIn]; |
1642 | 6.45k | hashOutputs = inner_ss.GetHash(); |
1643 | 6.45k | } |
1644 | | |
1645 | | // Version |
1646 | 75.2k | ss << txTo.version; |
1647 | | // Input prevouts/nSequence (none/all, depending on flags) |
1648 | 75.2k | ss << hashPrevouts; |
1649 | 75.2k | ss << hashSequence; |
1650 | | // The input being signed (replacing the scriptSig with scriptCode + amount) |
1651 | | // The prevout may already be contained in hashPrevout, and the nSequence |
1652 | | // may already be contain in hashSequence. |
1653 | 75.2k | ss << txTo.vin[nIn].prevout; |
1654 | 75.2k | ss << scriptCode; |
1655 | 75.2k | ss << amount; |
1656 | 75.2k | ss << txTo.vin[nIn].nSequence; |
1657 | | // Outputs (none/one/all, depending on flags) |
1658 | 75.2k | ss << hashOutputs; |
1659 | | // Locktime |
1660 | 75.2k | ss << txTo.nLockTime; |
1661 | 122k | } else { |
1662 | | // Wrapper to serialize only the necessary parts of the transaction being signed |
1663 | 122k | CTransactionSignatureSerializer<T> txTmp(txTo, scriptCode, nIn, nHashType); |
1664 | | |
1665 | | // Serialize |
1666 | 122k | ss << txTmp; |
1667 | 122k | } |
1668 | | |
1669 | | // If a cache object was provided, store the midstate there. |
1670 | 197k | if (sighash_cache != nullptr) { |
1671 | 130k | sighash_cache->Store(nHashType, scriptCode, ss); |
1672 | 130k | } |
1673 | | |
1674 | | // Add sighash type and hash. |
1675 | 197k | ss << nHashType; |
1676 | 197k | return ss.GetHash(); |
1677 | 254k | } uint256 SignatureHash<CTransaction>(CScript const&, CTransaction const&, unsigned int, int, long const&, SigVersion, PrecomputedTransactionData const*, SigHashCache*) Line | Count | Source | 1601 | 135k | { | 1602 | 135k | assert(nIn < txTo.vin.size()); | 1603 | | | 1604 | 135k | if (sigversion != SigVersion::WITNESS_V0) { | 1605 | | // Check for invalid use of SIGHASH_SINGLE | 1606 | 87.9k | if ((nHashType & 0x1f) == SIGHASH_SINGLE) { | 1607 | 3.66k | if (nIn >= txTo.vout.size()) { | 1608 | | // nOut out of range | 1609 | 759 | return uint256::ONE; | 1610 | 759 | } | 1611 | 3.66k | } | 1612 | 87.9k | } | 1613 | | | 1614 | 134k | HashWriter ss{}; | 1615 | | | 1616 | | // Try to compute using cached SHA256 midstate. | 1617 | 134k | if (sighash_cache && sighash_cache->Load(nHashType, scriptCode, ss)) { | 1618 | | // Add sighash type and hash. | 1619 | 50.5k | ss << nHashType; | 1620 | 50.5k | return ss.GetHash(); | 1621 | 50.5k | } | 1622 | | | 1623 | 83.9k | if (sigversion == SigVersion::WITNESS_V0) { | 1624 | 43.8k | uint256 hashPrevouts; | 1625 | 43.8k | uint256 hashSequence; | 1626 | 43.8k | uint256 hashOutputs; | 1627 | 43.8k | const bool cacheready = cache && cache->m_bip143_segwit_ready; | 1628 | | | 1629 | 43.8k | if (!(nHashType & SIGHASH_ANYONECANPAY)) { | 1630 | 36.4k | hashPrevouts = cacheready ? cache->hashPrevouts : SHA256Uint256(GetPrevoutsSHA256(txTo)); | 1631 | 36.4k | } | 1632 | | | 1633 | 43.8k | if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { | 1634 | 32.4k | hashSequence = cacheready ? cache->hashSequence : SHA256Uint256(GetSequencesSHA256(txTo)); | 1635 | 32.4k | } | 1636 | | | 1637 | 43.8k | if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { | 1638 | 35.9k | hashOutputs = cacheready ? cache->hashOutputs : SHA256Uint256(GetOutputsSHA256(txTo)); | 1639 | 35.9k | } else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) { | 1640 | 3.44k | HashWriter inner_ss{}; | 1641 | 3.44k | inner_ss << txTo.vout[nIn]; | 1642 | 3.44k | hashOutputs = inner_ss.GetHash(); | 1643 | 3.44k | } | 1644 | | | 1645 | | // Version | 1646 | 43.8k | ss << txTo.version; | 1647 | | // Input prevouts/nSequence (none/all, depending on flags) | 1648 | 43.8k | ss << hashPrevouts; | 1649 | 43.8k | ss << hashSequence; | 1650 | | // The input being signed (replacing the scriptSig with scriptCode + amount) | 1651 | | // The prevout may already be contained in hashPrevout, and the nSequence | 1652 | | // may already be contain in hashSequence. | 1653 | 43.8k | ss << txTo.vin[nIn].prevout; | 1654 | 43.8k | ss << scriptCode; | 1655 | 43.8k | ss << amount; | 1656 | 43.8k | ss << txTo.vin[nIn].nSequence; | 1657 | | // Outputs (none/one/all, depending on flags) | 1658 | 43.8k | ss << hashOutputs; | 1659 | | // Locktime | 1660 | 43.8k | ss << txTo.nLockTime; | 1661 | 43.8k | } else { | 1662 | | // Wrapper to serialize only the necessary parts of the transaction being signed | 1663 | 40.0k | CTransactionSignatureSerializer<T> txTmp(txTo, scriptCode, nIn, nHashType); | 1664 | | | 1665 | | // Serialize | 1666 | 40.0k | ss << txTmp; | 1667 | 40.0k | } | 1668 | | | 1669 | | // If a cache object was provided, store the midstate there. | 1670 | 83.9k | if (sighash_cache != nullptr) { | 1671 | 83.3k | sighash_cache->Store(nHashType, scriptCode, ss); | 1672 | 83.3k | } | 1673 | | | 1674 | | // Add sighash type and hash. | 1675 | 83.9k | ss << nHashType; | 1676 | 83.9k | return ss.GetHash(); | 1677 | 134k | } |
uint256 SignatureHash<CMutableTransaction>(CScript const&, CMutableTransaction const&, unsigned int, int, long const&, SigVersion, PrecomputedTransactionData const*, SigHashCache*) Line | Count | Source | 1601 | 120k | { | 1602 | 120k | assert(nIn < txTo.vin.size()); | 1603 | | | 1604 | 120k | if (sigversion != SigVersion::WITNESS_V0) { | 1605 | | // Check for invalid use of SIGHASH_SINGLE | 1606 | 87.3k | if ((nHashType & 0x1f) == SIGHASH_SINGLE) { | 1607 | 1.57k | if (nIn >= txTo.vout.size()) { | 1608 | | // nOut out of range | 1609 | 0 | return uint256::ONE; | 1610 | 0 | } | 1611 | 1.57k | } | 1612 | 87.3k | } | 1613 | | | 1614 | 120k | HashWriter ss{}; | 1615 | | | 1616 | | // Try to compute using cached SHA256 midstate. | 1617 | 120k | if (sighash_cache && sighash_cache->Load(nHashType, scriptCode, ss)) { | 1618 | | // Add sighash type and hash. | 1619 | 6.36k | ss << nHashType; | 1620 | 6.36k | return ss.GetHash(); | 1621 | 6.36k | } | 1622 | | | 1623 | 114k | if (sigversion == SigVersion::WITNESS_V0) { | 1624 | 31.4k | uint256 hashPrevouts; | 1625 | 31.4k | uint256 hashSequence; | 1626 | 31.4k | uint256 hashOutputs; | 1627 | 31.4k | const bool cacheready = cache && cache->m_bip143_segwit_ready; | 1628 | | | 1629 | 31.4k | if (!(nHashType & SIGHASH_ANYONECANPAY)) { | 1630 | 26.8k | hashPrevouts = cacheready ? cache->hashPrevouts : SHA256Uint256(GetPrevoutsSHA256(txTo)); | 1631 | 26.8k | } | 1632 | | | 1633 | 31.4k | if (!(nHashType & SIGHASH_ANYONECANPAY) && (nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { | 1634 | 23.8k | hashSequence = cacheready ? cache->hashSequence : SHA256Uint256(GetSequencesSHA256(txTo)); | 1635 | 23.8k | } | 1636 | | | 1637 | 31.4k | if ((nHashType & 0x1f) != SIGHASH_SINGLE && (nHashType & 0x1f) != SIGHASH_NONE) { | 1638 | 25.4k | hashOutputs = cacheready ? cache->hashOutputs : SHA256Uint256(GetOutputsSHA256(txTo)); | 1639 | 25.4k | } else if ((nHashType & 0x1f) == SIGHASH_SINGLE && nIn < txTo.vout.size()) { | 1640 | 3.01k | HashWriter inner_ss{}; | 1641 | 3.01k | inner_ss << txTo.vout[nIn]; | 1642 | 3.01k | hashOutputs = inner_ss.GetHash(); | 1643 | 3.01k | } | 1644 | | | 1645 | | // Version | 1646 | 31.4k | ss << txTo.version; | 1647 | | // Input prevouts/nSequence (none/all, depending on flags) | 1648 | 31.4k | ss << hashPrevouts; | 1649 | 31.4k | ss << hashSequence; | 1650 | | // The input being signed (replacing the scriptSig with scriptCode + amount) | 1651 | | // The prevout may already be contained in hashPrevout, and the nSequence | 1652 | | // may already be contain in hashSequence. | 1653 | 31.4k | ss << txTo.vin[nIn].prevout; | 1654 | 31.4k | ss << scriptCode; | 1655 | 31.4k | ss << amount; | 1656 | 31.4k | ss << txTo.vin[nIn].nSequence; | 1657 | | // Outputs (none/one/all, depending on flags) | 1658 | 31.4k | ss << hashOutputs; | 1659 | | // Locktime | 1660 | 31.4k | ss << txTo.nLockTime; | 1661 | 82.6k | } else { | 1662 | | // Wrapper to serialize only the necessary parts of the transaction being signed | 1663 | 82.6k | CTransactionSignatureSerializer<T> txTmp(txTo, scriptCode, nIn, nHashType); | 1664 | | | 1665 | | // Serialize | 1666 | 82.6k | ss << txTmp; | 1667 | 82.6k | } | 1668 | | | 1669 | | // If a cache object was provided, store the midstate there. | 1670 | 114k | if (sighash_cache != nullptr) { | 1671 | 47.4k | sighash_cache->Store(nHashType, scriptCode, ss); | 1672 | 47.4k | } | 1673 | | | 1674 | | // Add sighash type and hash. | 1675 | 114k | ss << nHashType; | 1676 | 114k | return ss.GetHash(); | 1677 | 120k | } |
|
1678 | | |
1679 | | template <class T> |
1680 | | bool GenericTransactionSignatureChecker<T>::VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& pubkey, const uint256& sighash) const |
1681 | 125k | { |
1682 | 125k | return pubkey.Verify(sighash, vchSig); |
1683 | 125k | } GenericTransactionSignatureChecker<CTransaction>::VerifyECDSASignature(std::vector<unsigned char, std::allocator<unsigned char>> const&, CPubKey const&, uint256 const&) const Line | Count | Source | 1681 | 71.9k | { | 1682 | 71.9k | return pubkey.Verify(sighash, vchSig); | 1683 | 71.9k | } |
GenericTransactionSignatureChecker<CMutableTransaction>::VerifyECDSASignature(std::vector<unsigned char, std::allocator<unsigned char>> const&, CPubKey const&, uint256 const&) const Line | Count | Source | 1681 | 53.5k | { | 1682 | 53.5k | return pubkey.Verify(sighash, vchSig); | 1683 | 53.5k | } |
|
1684 | | |
1685 | | template <class T> |
1686 | | bool GenericTransactionSignatureChecker<T>::VerifySchnorrSignature(std::span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const |
1687 | 20.0k | { |
1688 | 20.0k | return pubkey.VerifySchnorr(sighash, sig); |
1689 | 20.0k | } GenericTransactionSignatureChecker<CTransaction>::VerifySchnorrSignature(std::span<unsigned char const, 18446744073709551615ul>, XOnlyPubKey const&, uint256 const&) const Line | Count | Source | 1687 | 18.3k | { | 1688 | 18.3k | return pubkey.VerifySchnorr(sighash, sig); | 1689 | 18.3k | } |
GenericTransactionSignatureChecker<CMutableTransaction>::VerifySchnorrSignature(std::span<unsigned char const, 18446744073709551615ul>, XOnlyPubKey const&, uint256 const&) const Line | Count | Source | 1687 | 1.67k | { | 1688 | 1.67k | return pubkey.VerifySchnorr(sighash, sig); | 1689 | 1.67k | } |
|
1690 | | |
1691 | | template <class T> |
1692 | | bool GenericTransactionSignatureChecker<T>::CheckECDSASignature(const std::vector<unsigned char>& vchSigIn, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const |
1693 | 211k | { |
1694 | 211k | CPubKey pubkey(vchPubKey); |
1695 | 211k | if (!pubkey.IsValid()) |
1696 | 3.61k | return false; |
1697 | | |
1698 | | // Hash type is one byte tacked on to the end of the signature |
1699 | 207k | std::vector<unsigned char> vchSig(vchSigIn); |
1700 | 207k | if (vchSig.empty()) |
1701 | 19.4k | return false; |
1702 | 188k | int nHashType = vchSig.back(); |
1703 | 188k | vchSig.pop_back(); |
1704 | | |
1705 | | // Witness sighashes need the amount. |
1706 | 188k | if (sigversion == SigVersion::WITNESS_V0 && amount < 0) return HandleMissingData(m_mdb); |
1707 | | |
1708 | 188k | uint256 sighash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, this->txdata, &m_sighash_cache); |
1709 | | |
1710 | 188k | if (!VerifyECDSASignature(vchSig, pubkey, sighash)) |
1711 | 10.7k | return false; |
1712 | | |
1713 | 177k | return true; |
1714 | 188k | } GenericTransactionSignatureChecker<CTransaction>::CheckECDSASignature(std::vector<unsigned char, std::allocator<unsigned char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, CScript const&, SigVersion) const Line | Count | Source | 1693 | 147k | { | 1694 | 147k | CPubKey pubkey(vchPubKey); | 1695 | 147k | if (!pubkey.IsValid()) | 1696 | 88 | return false; | 1697 | | | 1698 | | // Hash type is one byte tacked on to the end of the signature | 1699 | 147k | std::vector<unsigned char> vchSig(vchSigIn); | 1700 | 147k | if (vchSig.empty()) | 1701 | 12.9k | return false; | 1702 | 134k | int nHashType = vchSig.back(); | 1703 | 134k | vchSig.pop_back(); | 1704 | | | 1705 | | // Witness sighashes need the amount. | 1706 | 134k | if (sigversion == SigVersion::WITNESS_V0 && amount < 0) return HandleMissingData(m_mdb); | 1707 | | | 1708 | 134k | uint256 sighash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, this->txdata, &m_sighash_cache); | 1709 | | | 1710 | 134k | if (!VerifyECDSASignature(vchSig, pubkey, sighash)) | 1711 | 2.02k | return false; | 1712 | | | 1713 | 132k | return true; | 1714 | 134k | } |
GenericTransactionSignatureChecker<CMutableTransaction>::CheckECDSASignature(std::vector<unsigned char, std::allocator<unsigned char>> const&, std::vector<unsigned char, std::allocator<unsigned char>> const&, CScript const&, SigVersion) const Line | Count | Source | 1693 | 63.6k | { | 1694 | 63.6k | CPubKey pubkey(vchPubKey); | 1695 | 63.6k | if (!pubkey.IsValid()) | 1696 | 3.52k | return false; | 1697 | | | 1698 | | // Hash type is one byte tacked on to the end of the signature | 1699 | 60.0k | std::vector<unsigned char> vchSig(vchSigIn); | 1700 | 60.0k | if (vchSig.empty()) | 1701 | 6.58k | return false; | 1702 | 53.5k | int nHashType = vchSig.back(); | 1703 | 53.5k | vchSig.pop_back(); | 1704 | | | 1705 | | // Witness sighashes need the amount. | 1706 | 53.5k | if (sigversion == SigVersion::WITNESS_V0 && amount < 0) return HandleMissingData(m_mdb); | 1707 | | | 1708 | 53.5k | uint256 sighash = SignatureHash(scriptCode, *txTo, nIn, nHashType, amount, sigversion, this->txdata, &m_sighash_cache); | 1709 | | | 1710 | 53.5k | if (!VerifyECDSASignature(vchSig, pubkey, sighash)) | 1711 | 8.76k | return false; | 1712 | | | 1713 | 44.7k | return true; | 1714 | 53.5k | } |
|
1715 | | |
1716 | | template <class T> |
1717 | | bool GenericTransactionSignatureChecker<T>::CheckSchnorrSignature(std::span<const unsigned char> sig, std::span<const unsigned char> pubkey_in, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror) const |
1718 | 262k | { |
1719 | 262k | assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); |
1720 | | // Schnorr signatures have 32-byte public keys. The caller is responsible for enforcing this. |
1721 | 262k | assert(pubkey_in.size() == 32); |
1722 | | // Note that in Tapscript evaluation, empty signatures are treated specially (invalid signature that does not |
1723 | | // abort script execution). This is implemented in EvalChecksigTapscript, which won't invoke |
1724 | | // CheckSchnorrSignature in that case. In other contexts, they are invalid like every other signature with |
1725 | | // size different from 64 or 65. |
1726 | 262k | if (sig.size() != 64 && sig.size() != 65) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_SIZE); |
1727 | | |
1728 | 262k | XOnlyPubKey pubkey{pubkey_in}; |
1729 | | |
1730 | 262k | uint8_t hashtype = SIGHASH_DEFAULT; |
1731 | 262k | if (sig.size() == 65) { |
1732 | 185k | hashtype = SpanPopBack(sig); |
1733 | 185k | if (hashtype == SIGHASH_DEFAULT) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_HASHTYPE); |
1734 | 185k | } |
1735 | 262k | uint256 sighash; |
1736 | 262k | if (!this->txdata) return HandleMissingData(m_mdb); |
1737 | 262k | if (!SignatureHashSchnorr(sighash, execdata, *txTo, nIn, hashtype, sigversion, *this->txdata, m_mdb)) { |
1738 | 722 | return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_HASHTYPE); |
1739 | 722 | } |
1740 | 261k | if (!VerifySchnorrSignature(sig, pubkey, sighash)) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG); |
1741 | 261k | return true; |
1742 | 261k | } GenericTransactionSignatureChecker<CTransaction>::CheckSchnorrSignature(std::span<unsigned char const, 18446744073709551615ul>, std::span<unsigned char const, 18446744073709551615ul>, SigVersion, ScriptExecutionData&, ScriptError_t*) const Line | Count | Source | 1718 | 260k | { | 1719 | 260k | assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); | 1720 | | // Schnorr signatures have 32-byte public keys. The caller is responsible for enforcing this. | 1721 | 260k | assert(pubkey_in.size() == 32); | 1722 | | // Note that in Tapscript evaluation, empty signatures are treated specially (invalid signature that does not | 1723 | | // abort script execution). This is implemented in EvalChecksigTapscript, which won't invoke | 1724 | | // CheckSchnorrSignature in that case. In other contexts, they are invalid like every other signature with | 1725 | | // size different from 64 or 65. | 1726 | 260k | if (sig.size() != 64 && sig.size() != 65) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_SIZE); | 1727 | | | 1728 | 260k | XOnlyPubKey pubkey{pubkey_in}; | 1729 | | | 1730 | 260k | uint8_t hashtype = SIGHASH_DEFAULT; | 1731 | 260k | if (sig.size() == 65) { | 1732 | 185k | hashtype = SpanPopBack(sig); | 1733 | 185k | if (hashtype == SIGHASH_DEFAULT) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_HASHTYPE); | 1734 | 185k | } | 1735 | 260k | uint256 sighash; | 1736 | 260k | if (!this->txdata) return HandleMissingData(m_mdb); | 1737 | 260k | if (!SignatureHashSchnorr(sighash, execdata, *txTo, nIn, hashtype, sigversion, *this->txdata, m_mdb)) { | 1738 | 722 | return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_HASHTYPE); | 1739 | 722 | } | 1740 | 260k | if (!VerifySchnorrSignature(sig, pubkey, sighash)) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG); | 1741 | 259k | return true; | 1742 | 260k | } |
GenericTransactionSignatureChecker<CMutableTransaction>::CheckSchnorrSignature(std::span<unsigned char const, 18446744073709551615ul>, std::span<unsigned char const, 18446744073709551615ul>, SigVersion, ScriptExecutionData&, ScriptError_t*) const Line | Count | Source | 1718 | 1.70k | { | 1719 | 1.70k | assert(sigversion == SigVersion::TAPROOT || sigversion == SigVersion::TAPSCRIPT); | 1720 | | // Schnorr signatures have 32-byte public keys. The caller is responsible for enforcing this. | 1721 | 1.70k | assert(pubkey_in.size() == 32); | 1722 | | // Note that in Tapscript evaluation, empty signatures are treated specially (invalid signature that does not | 1723 | | // abort script execution). This is implemented in EvalChecksigTapscript, which won't invoke | 1724 | | // CheckSchnorrSignature in that case. In other contexts, they are invalid like every other signature with | 1725 | | // size different from 64 or 65. | 1726 | 1.70k | if (sig.size() != 64 && sig.size() != 65) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_SIZE); | 1727 | | | 1728 | 1.70k | XOnlyPubKey pubkey{pubkey_in}; | 1729 | | | 1730 | 1.70k | uint8_t hashtype = SIGHASH_DEFAULT; | 1731 | 1.70k | if (sig.size() == 65) { | 1732 | 23 | hashtype = SpanPopBack(sig); | 1733 | 23 | if (hashtype == SIGHASH_DEFAULT) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_HASHTYPE); | 1734 | 23 | } | 1735 | 1.70k | uint256 sighash; | 1736 | 1.70k | if (!this->txdata) return HandleMissingData(m_mdb); | 1737 | 1.67k | if (!SignatureHashSchnorr(sighash, execdata, *txTo, nIn, hashtype, sigversion, *this->txdata, m_mdb)) { | 1738 | 0 | return set_error(serror, SCRIPT_ERR_SCHNORR_SIG_HASHTYPE); | 1739 | 0 | } | 1740 | 1.67k | if (!VerifySchnorrSignature(sig, pubkey, sighash)) return set_error(serror, SCRIPT_ERR_SCHNORR_SIG); | 1741 | 1.67k | return true; | 1742 | 1.67k | } |
|
1743 | | |
1744 | | template <class T> |
1745 | | bool GenericTransactionSignatureChecker<T>::CheckLockTime(const CScriptNum& nLockTime) const |
1746 | 6.80k | { |
1747 | | // There are two kinds of nLockTime: lock-by-blockheight |
1748 | | // and lock-by-blocktime, distinguished by whether |
1749 | | // nLockTime < LOCKTIME_THRESHOLD. |
1750 | | // |
1751 | | // We want to compare apples to apples, so fail the script |
1752 | | // unless the type of nLockTime being tested is the same as |
1753 | | // the nLockTime in the transaction. |
1754 | 6.80k | if (!( |
1755 | 6.80k | (txTo->nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) || |
1756 | 6.80k | (txTo->nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD) |
1757 | 6.80k | )) |
1758 | 178 | return false; |
1759 | | |
1760 | | // Now that we know we're comparing apples-to-apples, the |
1761 | | // comparison is a simple numeric one. |
1762 | 6.62k | if (nLockTime > (int64_t)txTo->nLockTime) |
1763 | 5.59k | return false; |
1764 | | |
1765 | | // Finally the nLockTime feature can be disabled in IsFinalTx() |
1766 | | // and thus CHECKLOCKTIMEVERIFY bypassed if every txin has |
1767 | | // been finalized by setting nSequence to maxint. The |
1768 | | // transaction would be allowed into the blockchain, making |
1769 | | // the opcode ineffective. |
1770 | | // |
1771 | | // Testing if this vin is not final is sufficient to |
1772 | | // prevent this condition. Alternatively we could test all |
1773 | | // inputs, but testing just this input minimizes the data |
1774 | | // required to prove correct CHECKLOCKTIMEVERIFY execution. |
1775 | 1.03k | if (CTxIn::SEQUENCE_FINAL == txTo->vin[nIn].nSequence) |
1776 | 94 | return false; |
1777 | | |
1778 | 939 | return true; |
1779 | 1.03k | } GenericTransactionSignatureChecker<CTransaction>::CheckLockTime(CScriptNum const&) const Line | Count | Source | 1746 | 6.07k | { | 1747 | | // There are two kinds of nLockTime: lock-by-blockheight | 1748 | | // and lock-by-blocktime, distinguished by whether | 1749 | | // nLockTime < LOCKTIME_THRESHOLD. | 1750 | | // | 1751 | | // We want to compare apples to apples, so fail the script | 1752 | | // unless the type of nLockTime being tested is the same as | 1753 | | // the nLockTime in the transaction. | 1754 | 6.07k | if (!( | 1755 | 6.07k | (txTo->nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) || | 1756 | 6.07k | (txTo->nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD) | 1757 | 6.07k | )) | 1758 | 178 | return false; | 1759 | | | 1760 | | // Now that we know we're comparing apples-to-apples, the | 1761 | | // comparison is a simple numeric one. | 1762 | 5.90k | if (nLockTime > (int64_t)txTo->nLockTime) | 1763 | 5.38k | return false; | 1764 | | | 1765 | | // Finally the nLockTime feature can be disabled in IsFinalTx() | 1766 | | // and thus CHECKLOCKTIMEVERIFY bypassed if every txin has | 1767 | | // been finalized by setting nSequence to maxint. The | 1768 | | // transaction would be allowed into the blockchain, making | 1769 | | // the opcode ineffective. | 1770 | | // | 1771 | | // Testing if this vin is not final is sufficient to | 1772 | | // prevent this condition. Alternatively we could test all | 1773 | | // inputs, but testing just this input minimizes the data | 1774 | | // required to prove correct CHECKLOCKTIMEVERIFY execution. | 1775 | 516 | if (CTxIn::SEQUENCE_FINAL == txTo->vin[nIn].nSequence) | 1776 | 94 | return false; | 1777 | | | 1778 | 422 | return true; | 1779 | 516 | } |
GenericTransactionSignatureChecker<CMutableTransaction>::CheckLockTime(CScriptNum const&) const Line | Count | Source | 1746 | 727 | { | 1747 | | // There are two kinds of nLockTime: lock-by-blockheight | 1748 | | // and lock-by-blocktime, distinguished by whether | 1749 | | // nLockTime < LOCKTIME_THRESHOLD. | 1750 | | // | 1751 | | // We want to compare apples to apples, so fail the script | 1752 | | // unless the type of nLockTime being tested is the same as | 1753 | | // the nLockTime in the transaction. | 1754 | 727 | if (!( | 1755 | 727 | (txTo->nLockTime < LOCKTIME_THRESHOLD && nLockTime < LOCKTIME_THRESHOLD) || | 1756 | 727 | (txTo->nLockTime >= LOCKTIME_THRESHOLD && nLockTime >= LOCKTIME_THRESHOLD) | 1757 | 727 | )) | 1758 | 0 | return false; | 1759 | | | 1760 | | // Now that we know we're comparing apples-to-apples, the | 1761 | | // comparison is a simple numeric one. | 1762 | 727 | if (nLockTime > (int64_t)txTo->nLockTime) | 1763 | 210 | return false; | 1764 | | | 1765 | | // Finally the nLockTime feature can be disabled in IsFinalTx() | 1766 | | // and thus CHECKLOCKTIMEVERIFY bypassed if every txin has | 1767 | | // been finalized by setting nSequence to maxint. The | 1768 | | // transaction would be allowed into the blockchain, making | 1769 | | // the opcode ineffective. | 1770 | | // | 1771 | | // Testing if this vin is not final is sufficient to | 1772 | | // prevent this condition. Alternatively we could test all | 1773 | | // inputs, but testing just this input minimizes the data | 1774 | | // required to prove correct CHECKLOCKTIMEVERIFY execution. | 1775 | 517 | if (CTxIn::SEQUENCE_FINAL == txTo->vin[nIn].nSequence) | 1776 | 0 | return false; | 1777 | | | 1778 | 517 | return true; | 1779 | 517 | } |
|
1780 | | |
1781 | | template <class T> |
1782 | | bool GenericTransactionSignatureChecker<T>::CheckSequence(const CScriptNum& nSequence) const |
1783 | 6.48k | { |
1784 | | // Relative lock times are supported by comparing the passed |
1785 | | // in operand to the sequence number of the input. |
1786 | 6.48k | const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence; |
1787 | | |
1788 | | // Fail if the transaction's version number is not set high |
1789 | | // enough to trigger BIP 68 rules. |
1790 | 6.48k | if (txTo->version < 2) |
1791 | 397 | return false; |
1792 | | |
1793 | | // Sequence numbers with their most significant bit set are not |
1794 | | // consensus constrained. Testing that the transaction's sequence |
1795 | | // number do not have this bit set prevents using this property |
1796 | | // to get around a CHECKSEQUENCEVERIFY check. |
1797 | 6.08k | if (txToSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) |
1798 | 36 | return false; |
1799 | | |
1800 | | // Mask off any bits that do not have consensus-enforced meaning |
1801 | | // before doing the integer comparisons |
1802 | 6.04k | const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK; |
1803 | 6.04k | const int64_t txToSequenceMasked = txToSequence & nLockTimeMask; |
1804 | 6.04k | const CScriptNum nSequenceMasked = nSequence & nLockTimeMask; |
1805 | | |
1806 | | // There are two kinds of nSequence: lock-by-blockheight |
1807 | | // and lock-by-blocktime, distinguished by whether |
1808 | | // nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG. |
1809 | | // |
1810 | | // We want to compare apples to apples, so fail the script |
1811 | | // unless the type of nSequenceMasked being tested is the same as |
1812 | | // the nSequenceMasked in the transaction. |
1813 | 6.04k | if (!( |
1814 | 6.04k | (txToSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) || |
1815 | 6.04k | (txToSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) |
1816 | 6.04k | )) { |
1817 | 188 | return false; |
1818 | 188 | } |
1819 | | |
1820 | | // Now that we know we're comparing apples-to-apples, the |
1821 | | // comparison is a simple numeric one. |
1822 | 5.86k | if (nSequenceMasked > txToSequenceMasked) |
1823 | 5.26k | return false; |
1824 | | |
1825 | 598 | return true; |
1826 | 5.86k | } GenericTransactionSignatureChecker<CTransaction>::CheckSequence(CScriptNum const&) const Line | Count | Source | 1783 | 6.05k | { | 1784 | | // Relative lock times are supported by comparing the passed | 1785 | | // in operand to the sequence number of the input. | 1786 | 6.05k | const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence; | 1787 | | | 1788 | | // Fail if the transaction's version number is not set high | 1789 | | // enough to trigger BIP 68 rules. | 1790 | 6.05k | if (txTo->version < 2) | 1791 | 134 | return false; | 1792 | | | 1793 | | // Sequence numbers with their most significant bit set are not | 1794 | | // consensus constrained. Testing that the transaction's sequence | 1795 | | // number do not have this bit set prevents using this property | 1796 | | // to get around a CHECKSEQUENCEVERIFY check. | 1797 | 5.91k | if (txToSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) | 1798 | 16 | return false; | 1799 | | | 1800 | | // Mask off any bits that do not have consensus-enforced meaning | 1801 | | // before doing the integer comparisons | 1802 | 5.90k | const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK; | 1803 | 5.90k | const int64_t txToSequenceMasked = txToSequence & nLockTimeMask; | 1804 | 5.90k | const CScriptNum nSequenceMasked = nSequence & nLockTimeMask; | 1805 | | | 1806 | | // There are two kinds of nSequence: lock-by-blockheight | 1807 | | // and lock-by-blocktime, distinguished by whether | 1808 | | // nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG. | 1809 | | // | 1810 | | // We want to compare apples to apples, so fail the script | 1811 | | // unless the type of nSequenceMasked being tested is the same as | 1812 | | // the nSequenceMasked in the transaction. | 1813 | 5.90k | if (!( | 1814 | 5.90k | (txToSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) || | 1815 | 5.90k | (txToSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) | 1816 | 5.90k | )) { | 1817 | 188 | return false; | 1818 | 188 | } | 1819 | | | 1820 | | // Now that we know we're comparing apples-to-apples, the | 1821 | | // comparison is a simple numeric one. | 1822 | 5.71k | if (nSequenceMasked > txToSequenceMasked) | 1823 | 5.25k | return false; | 1824 | | | 1825 | 458 | return true; | 1826 | 5.71k | } |
GenericTransactionSignatureChecker<CMutableTransaction>::CheckSequence(CScriptNum const&) const Line | Count | Source | 1783 | 429 | { | 1784 | | // Relative lock times are supported by comparing the passed | 1785 | | // in operand to the sequence number of the input. | 1786 | 429 | const int64_t txToSequence = (int64_t)txTo->vin[nIn].nSequence; | 1787 | | | 1788 | | // Fail if the transaction's version number is not set high | 1789 | | // enough to trigger BIP 68 rules. | 1790 | 429 | if (txTo->version < 2) | 1791 | 263 | return false; | 1792 | | | 1793 | | // Sequence numbers with their most significant bit set are not | 1794 | | // consensus constrained. Testing that the transaction's sequence | 1795 | | // number do not have this bit set prevents using this property | 1796 | | // to get around a CHECKSEQUENCEVERIFY check. | 1797 | 166 | if (txToSequence & CTxIn::SEQUENCE_LOCKTIME_DISABLE_FLAG) | 1798 | 20 | return false; | 1799 | | | 1800 | | // Mask off any bits that do not have consensus-enforced meaning | 1801 | | // before doing the integer comparisons | 1802 | 146 | const uint32_t nLockTimeMask = CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG | CTxIn::SEQUENCE_LOCKTIME_MASK; | 1803 | 146 | const int64_t txToSequenceMasked = txToSequence & nLockTimeMask; | 1804 | 146 | const CScriptNum nSequenceMasked = nSequence & nLockTimeMask; | 1805 | | | 1806 | | // There are two kinds of nSequence: lock-by-blockheight | 1807 | | // and lock-by-blocktime, distinguished by whether | 1808 | | // nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG. | 1809 | | // | 1810 | | // We want to compare apples to apples, so fail the script | 1811 | | // unless the type of nSequenceMasked being tested is the same as | 1812 | | // the nSequenceMasked in the transaction. | 1813 | 146 | if (!( | 1814 | 146 | (txToSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked < CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) || | 1815 | 146 | (txToSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG && nSequenceMasked >= CTxIn::SEQUENCE_LOCKTIME_TYPE_FLAG) | 1816 | 146 | )) { | 1817 | 0 | return false; | 1818 | 0 | } | 1819 | | | 1820 | | // Now that we know we're comparing apples-to-apples, the | 1821 | | // comparison is a simple numeric one. | 1822 | 146 | if (nSequenceMasked > txToSequenceMasked) | 1823 | 6 | return false; | 1824 | | | 1825 | 140 | return true; | 1826 | 146 | } |
|
1827 | | |
1828 | | // explicit instantiation |
1829 | | template class GenericTransactionSignatureChecker<CTransaction>; |
1830 | | template class GenericTransactionSignatureChecker<CMutableTransaction>; |
1831 | | |
1832 | | static bool ExecuteWitnessScript(const std::span<const valtype>& stack_span, const CScript& exec_script, script_verify_flags flags, SigVersion sigversion, const BaseSignatureChecker& checker, ScriptExecutionData& execdata, ScriptError* serror) |
1833 | 171k | { |
1834 | 171k | std::vector<valtype> stack{stack_span.begin(), stack_span.end()}; |
1835 | | |
1836 | 171k | if (sigversion == SigVersion::TAPSCRIPT) { |
1837 | | // OP_SUCCESSx processing overrides everything, including stack element size limits |
1838 | 86.5k | CScript::const_iterator pc = exec_script.begin(); |
1839 | 10.0M | while (pc < exec_script.end()) { |
1840 | 9.96M | opcodetype opcode; |
1841 | 9.96M | if (!exec_script.GetOp(pc, opcode)) { |
1842 | | // Note how this condition would not be reached if an unknown OP_SUCCESSx was found |
1843 | 424 | return set_error(serror, SCRIPT_ERR_BAD_OPCODE); |
1844 | 424 | } |
1845 | | // New opcodes will be listed here. May use a different sigversion to modify existing opcodes. |
1846 | 9.96M | if (IsOpSuccess(opcode)) { |
1847 | 4.41k | if (flags & SCRIPT_VERIFY_DISCOURAGE_OP_SUCCESS) { |
1848 | 434 | return set_error(serror, SCRIPT_ERR_DISCOURAGE_OP_SUCCESS); |
1849 | 434 | } |
1850 | 3.98k | return set_success(serror); |
1851 | 4.41k | } |
1852 | 9.96M | } |
1853 | | |
1854 | | // Tapscript enforces initial stack size limits (altstack is empty here) |
1855 | 81.6k | if (stack.size() > MAX_STACK_SIZE) return set_error(serror, SCRIPT_ERR_STACK_SIZE); |
1856 | 81.6k | } |
1857 | | |
1858 | | // Disallow stack item size > MAX_SCRIPT_ELEMENT_SIZE in witness stack |
1859 | 574k | for (const valtype& elem : stack) { |
1860 | 574k | if (elem.size() > MAX_SCRIPT_ELEMENT_SIZE) return set_error(serror, SCRIPT_ERR_PUSH_SIZE); |
1861 | 574k | } |
1862 | | |
1863 | | // Run the script interpreter. |
1864 | 166k | if (!EvalScript(stack, exec_script, flags, checker, sigversion, execdata, serror)) return false; |
1865 | | |
1866 | | // Scripts inside witness implicitly require cleanstack behaviour |
1867 | 149k | if (stack.size() != 1) return set_error(serror, SCRIPT_ERR_CLEANSTACK); |
1868 | 145k | if (!CastToBool(stack.back())) return set_error(serror, SCRIPT_ERR_EVAL_FALSE); |
1869 | 143k | return true; |
1870 | 145k | } |
1871 | | |
1872 | | uint256 ComputeTapleafHash(uint8_t leaf_version, std::span<const unsigned char> script) |
1873 | 135k | { |
1874 | 135k | return (HashWriter{HASHER_TAPLEAF} << leaf_version << CompactSizeWriter(script.size()) << script).GetSHA256(); |
1875 | 135k | } |
1876 | | |
1877 | | uint256 ComputeTapbranchHash(std::span<const unsigned char> a, std::span<const unsigned char> b) |
1878 | 383k | { |
1879 | 383k | HashWriter ss_branch{HASHER_TAPBRANCH}; |
1880 | 383k | if (std::lexicographical_compare(a.begin(), a.end(), b.begin(), b.end())) { |
1881 | 180k | ss_branch << a << b; |
1882 | 202k | } else { |
1883 | 202k | ss_branch << b << a; |
1884 | 202k | } |
1885 | 383k | return ss_branch.GetSHA256(); |
1886 | 383k | } |
1887 | | |
1888 | | uint256 ComputeTaprootMerkleRoot(std::span<const unsigned char> control, const uint256& tapleaf_hash) |
1889 | 93.5k | { |
1890 | 93.5k | assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE); |
1891 | 93.5k | assert(control.size() <= TAPROOT_CONTROL_MAX_SIZE); |
1892 | 93.5k | assert((control.size() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE == 0); |
1893 | | |
1894 | 93.5k | const int path_len = (control.size() - TAPROOT_CONTROL_BASE_SIZE) / TAPROOT_CONTROL_NODE_SIZE; |
1895 | 93.5k | uint256 k = tapleaf_hash; |
1896 | 454k | for (int i = 0; i < path_len; ++i) { |
1897 | 360k | std::span node{std::span{control}.subspan(TAPROOT_CONTROL_BASE_SIZE + TAPROOT_CONTROL_NODE_SIZE * i, TAPROOT_CONTROL_NODE_SIZE)}; |
1898 | 360k | k = ComputeTapbranchHash(k, node); |
1899 | 360k | } |
1900 | 93.5k | return k; |
1901 | 93.5k | } |
1902 | | |
1903 | | static bool VerifyTaprootCommitment(const std::vector<unsigned char>& control, const std::vector<unsigned char>& program, const uint256& tapleaf_hash) |
1904 | 89.9k | { |
1905 | 89.9k | assert(control.size() >= TAPROOT_CONTROL_BASE_SIZE); |
1906 | 89.9k | assert(program.size() >= uint256::size()); |
1907 | | //! The internal pubkey (x-only, so no Y coordinate parity). |
1908 | 89.9k | const XOnlyPubKey p{std::span{control}.subspan(1, TAPROOT_CONTROL_BASE_SIZE - 1)}; |
1909 | | //! The output pubkey (taken from the scriptPubKey). |
1910 | 89.9k | const XOnlyPubKey q{program}; |
1911 | | // Compute the Merkle root from the leaf and the provided path. |
1912 | 89.9k | const uint256 merkle_root = ComputeTaprootMerkleRoot(control, tapleaf_hash); |
1913 | | // Verify that the output pubkey matches the tweaked internal pubkey, after correcting for parity. |
1914 | 89.9k | return q.CheckTapTweak(p, merkle_root, control[0] & 1); |
1915 | 89.9k | } |
1916 | | |
1917 | | static bool VerifyWitnessProgram(const CScriptWitness& witness, int witversion, const std::vector<unsigned char>& program, script_verify_flags flags, const BaseSignatureChecker& checker, ScriptError* serror, bool is_p2sh) |
1918 | 283k | { |
1919 | 283k | CScript exec_script; //!< Actually executed script (last stack item in P2WSH; implied P2PKH script in P2WPKH; leaf script in P2TR) |
1920 | 283k | std::span stack{witness.stack}; |
1921 | 283k | ScriptExecutionData execdata; |
1922 | | |
1923 | 283k | if (witversion == 0) { |
1924 | 140k | if (program.size() == WITNESS_V0_SCRIPTHASH_SIZE) { |
1925 | | // BIP141 P2WSH: 32-byte witness v0 program (which encodes SHA256(script)) |
1926 | 31.5k | if (stack.size() == 0) { |
1927 | 1.15k | return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY); |
1928 | 1.15k | } |
1929 | 30.3k | const valtype& script_bytes = SpanPopBack(stack); |
1930 | 30.3k | exec_script = CScript(script_bytes.begin(), script_bytes.end()); |
1931 | 30.3k | uint256 hash_exec_script; |
1932 | 30.3k | CSHA256().Write(exec_script.data(), exec_script.size()).Finalize(hash_exec_script.begin()); |
1933 | 30.3k | if (memcmp(hash_exec_script.begin(), program.data(), 32)) { |
1934 | 771 | return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH); |
1935 | 771 | } |
1936 | 29.5k | return ExecuteWitnessScript(stack, exec_script, flags, SigVersion::WITNESS_V0, checker, execdata, serror); |
1937 | 109k | } else if (program.size() == WITNESS_V0_KEYHASH_SIZE) { |
1938 | | // BIP141 P2WPKH: 20-byte witness v0 program (which encodes Hash160(pubkey)) |
1939 | 108k | if (stack.size() != 2) { |
1940 | 53.2k | return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH); // 2 items in witness |
1941 | 53.2k | } |
1942 | 55.5k | exec_script << OP_DUP << OP_HASH160 << program << OP_EQUALVERIFY << OP_CHECKSIG; |
1943 | 55.5k | return ExecuteWitnessScript(stack, exec_script, flags, SigVersion::WITNESS_V0, checker, execdata, serror); |
1944 | 108k | } else { |
1945 | 598 | return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_WRONG_LENGTH); |
1946 | 598 | } |
1947 | 142k | } else if (witversion == 1 && program.size() == WITNESS_V1_TAPROOT_SIZE && !is_p2sh) { |
1948 | | // BIP341 Taproot: 32-byte non-P2SH witness v1 program (which encodes a P2C-tweaked pubkey) |
1949 | 128k | if (!(flags & SCRIPT_VERIFY_TAPROOT)) return set_success(serror); |
1950 | 104k | if (stack.size() == 0) return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_WITNESS_EMPTY); |
1951 | 95.3k | if (stack.size() >= 2 && !stack.back().empty() && stack.back()[0] == ANNEX_TAG) { |
1952 | | // Drop annex (this is non-standard; see IsWitnessStandard) |
1953 | 4.37k | const valtype& annex = SpanPopBack(stack); |
1954 | 4.37k | execdata.m_annex_hash = (HashWriter{} << annex).GetSHA256(); |
1955 | 4.37k | execdata.m_annex_present = true; |
1956 | 91.0k | } else { |
1957 | 91.0k | execdata.m_annex_present = false; |
1958 | 91.0k | } |
1959 | 95.3k | execdata.m_annex_init = true; |
1960 | 95.3k | if (stack.size() == 1) { |
1961 | | // Key path spending (stack size is 1 after removing optional annex) |
1962 | 5.40k | if (!checker.CheckSchnorrSignature(stack.front(), program, SigVersion::TAPROOT, execdata, serror)) { |
1963 | 540 | return false; // serror is set |
1964 | 540 | } |
1965 | 4.86k | return set_success(serror); |
1966 | 89.9k | } else { |
1967 | | // Script path spending (stack size is >1 after removing optional annex) |
1968 | 89.9k | const valtype& control = SpanPopBack(stack); |
1969 | 89.9k | const valtype& script = SpanPopBack(stack); |
1970 | 89.9k | if (control.size() < TAPROOT_CONTROL_BASE_SIZE || control.size() > TAPROOT_CONTROL_MAX_SIZE || ((control.size() - TAPROOT_CONTROL_BASE_SIZE) % TAPROOT_CONTROL_NODE_SIZE) != 0) { |
1971 | 18 | return set_error(serror, SCRIPT_ERR_TAPROOT_WRONG_CONTROL_SIZE); |
1972 | 18 | } |
1973 | 89.9k | execdata.m_tapleaf_hash = ComputeTapleafHash(control[0] & TAPROOT_LEAF_MASK, script); |
1974 | 89.9k | if (!VerifyTaprootCommitment(control, program, execdata.m_tapleaf_hash)) { |
1975 | 20 | return set_error(serror, SCRIPT_ERR_WITNESS_PROGRAM_MISMATCH); |
1976 | 20 | } |
1977 | 89.9k | execdata.m_tapleaf_hash_init = true; |
1978 | 89.9k | if ((control[0] & TAPROOT_LEAF_MASK) == TAPROOT_LEAF_TAPSCRIPT) { |
1979 | | // Tapscript (leaf version 0xc0) |
1980 | 86.5k | exec_script = CScript(script.begin(), script.end()); |
1981 | 86.5k | execdata.m_validation_weight_left = ::GetSerializeSize(witness.stack) + VALIDATION_WEIGHT_OFFSET; |
1982 | 86.5k | execdata.m_validation_weight_left_init = true; |
1983 | 86.5k | return ExecuteWitnessScript(stack, exec_script, flags, SigVersion::TAPSCRIPT, checker, execdata, serror); |
1984 | 86.5k | } |
1985 | 3.41k | if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION) { |
1986 | 376 | return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_TAPROOT_VERSION); |
1987 | 376 | } |
1988 | 3.03k | return set_success(serror); |
1989 | 3.41k | } |
1990 | 95.3k | } else if (!is_p2sh && CScript::IsPayToAnchor(witversion, program)) { |
1991 | 10 | return true; |
1992 | 13.7k | } else { |
1993 | 13.7k | if (flags & SCRIPT_VERIFY_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM) { |
1994 | 677 | return set_error(serror, SCRIPT_ERR_DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM); |
1995 | 677 | } |
1996 | | // Other version/size/p2sh combinations return true for future softfork compatibility |
1997 | 13.0k | return true; |
1998 | 13.7k | } |
1999 | | // There is intentionally no return statement here, to be able to use "control reaches end of non-void function" warnings to detect gaps in the logic above. |
2000 | 283k | } |
2001 | | |
2002 | | bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, script_verify_flags flags, const BaseSignatureChecker& checker, ScriptError* serror) |
2003 | 885k | { |
2004 | 885k | static const CScriptWitness emptyWitness; |
2005 | 885k | if (witness == nullptr) { |
2006 | 67.5k | witness = &emptyWitness; |
2007 | 67.5k | } |
2008 | 885k | bool hadWitness = false; |
2009 | | |
2010 | 885k | set_error(serror, SCRIPT_ERR_UNKNOWN_ERROR); |
2011 | | |
2012 | 885k | if ((flags & SCRIPT_VERIFY_SIGPUSHONLY) != 0 && !scriptSig.IsPushOnly()) { |
2013 | 9.73k | return set_error(serror, SCRIPT_ERR_SIG_PUSHONLY); |
2014 | 9.73k | } |
2015 | | |
2016 | | // scriptSig and scriptPubKey must be evaluated sequentially on the same stack |
2017 | | // rather than being simply concatenated (see CVE-2010-5141) |
2018 | 875k | std::vector<std::vector<unsigned char> > stack, stackCopy; |
2019 | 875k | if (!EvalScript(stack, scriptSig, flags, checker, SigVersion::BASE, serror)) |
2020 | | // serror is set |
2021 | 6.80k | return false; |
2022 | 868k | if (flags & SCRIPT_VERIFY_P2SH) |
2023 | 632k | stackCopy = stack; |
2024 | 868k | if (!EvalScript(stack, scriptPubKey, flags, checker, SigVersion::BASE, serror)) |
2025 | | // serror is set |
2026 | 151k | return false; |
2027 | 717k | if (stack.empty()) |
2028 | 2.67k | return set_error(serror, SCRIPT_ERR_EVAL_FALSE); |
2029 | 714k | if (CastToBool(stack.back()) == false) |
2030 | 8.10k | return set_error(serror, SCRIPT_ERR_EVAL_FALSE); |
2031 | | |
2032 | | // Bare witness programs |
2033 | 706k | int witnessversion; |
2034 | 706k | std::vector<unsigned char> witnessprogram; |
2035 | 706k | if (flags & SCRIPT_VERIFY_WITNESS) { |
2036 | 367k | if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) { |
2037 | 261k | hadWitness = true; |
2038 | 261k | if (scriptSig.size() != 0) { |
2039 | | // The scriptSig must be _exactly_ CScript(), otherwise we reintroduce malleability. |
2040 | 1.07k | return set_error(serror, SCRIPT_ERR_WITNESS_MALLEATED); |
2041 | 1.07k | } |
2042 | 260k | if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror, /*is_p2sh=*/false)) { |
2043 | 81.6k | return false; |
2044 | 81.6k | } |
2045 | | // Bypass the cleanstack check at the end. The actual stack is obviously not clean |
2046 | | // for witness programs. |
2047 | 179k | stack.resize(1); |
2048 | 179k | } |
2049 | 367k | } |
2050 | | |
2051 | | // Additional validation for spend-to-script-hash transactions: |
2052 | 623k | if ((flags & SCRIPT_VERIFY_P2SH) && scriptPubKey.IsPayToScriptHash()) |
2053 | 64.0k | { |
2054 | | // scriptSig must be literals-only or validation fails |
2055 | 64.0k | if (!scriptSig.IsPushOnly()) |
2056 | 236 | return set_error(serror, SCRIPT_ERR_SIG_PUSHONLY); |
2057 | | |
2058 | | // Restore stack. |
2059 | 63.8k | swap(stack, stackCopy); |
2060 | | |
2061 | | // stack cannot be empty here, because if it was the |
2062 | | // P2SH HASH <> EQUAL scriptPubKey would be evaluated with |
2063 | | // an empty stack and the EvalScript above would return false. |
2064 | 63.8k | assert(!stack.empty()); |
2065 | | |
2066 | 63.8k | const valtype& pubKeySerialized = stack.back(); |
2067 | 63.8k | CScript pubKey2(pubKeySerialized.begin(), pubKeySerialized.end()); |
2068 | 63.8k | popstack(stack); |
2069 | | |
2070 | 63.8k | if (!EvalScript(stack, pubKey2, flags, checker, SigVersion::BASE, serror)) |
2071 | | // serror is set |
2072 | 10.7k | return false; |
2073 | 53.0k | if (stack.empty()) |
2074 | 1.28k | return set_error(serror, SCRIPT_ERR_EVAL_FALSE); |
2075 | 51.7k | if (!CastToBool(stack.back())) |
2076 | 378 | return set_error(serror, SCRIPT_ERR_EVAL_FALSE); |
2077 | | |
2078 | | // P2SH witness program |
2079 | 51.3k | if (flags & SCRIPT_VERIFY_WITNESS) { |
2080 | 41.2k | if (pubKey2.IsWitnessProgram(witnessversion, witnessprogram)) { |
2081 | 23.0k | hadWitness = true; |
2082 | 23.0k | if (scriptSig != CScript() << std::vector<unsigned char>(pubKey2.begin(), pubKey2.end())) { |
2083 | | // The scriptSig must be _exactly_ a single push of the redeemScript. Otherwise we |
2084 | | // reintroduce malleability. |
2085 | 514 | return set_error(serror, SCRIPT_ERR_WITNESS_MALLEATED_P2SH); |
2086 | 514 | } |
2087 | 22.4k | if (!VerifyWitnessProgram(*witness, witnessversion, witnessprogram, flags, checker, serror, /*is_p2sh=*/true)) { |
2088 | 9.23k | return false; |
2089 | 9.23k | } |
2090 | | // Bypass the cleanstack check at the end. The actual stack is obviously not clean |
2091 | | // for witness programs. |
2092 | 13.2k | stack.resize(1); |
2093 | 13.2k | } |
2094 | 41.2k | } |
2095 | 51.3k | } |
2096 | | |
2097 | | // The CLEANSTACK check is only performed after potential P2SH evaluation, |
2098 | | // as the non-P2SH evaluation of a P2SH script will obviously not result in |
2099 | | // a clean stack (the P2SH inputs remain). The same holds for witness evaluation. |
2100 | 601k | if ((flags & SCRIPT_VERIFY_CLEANSTACK) != 0) { |
2101 | | // Disallow CLEANSTACK without P2SH, as otherwise a switch CLEANSTACK->P2SH+CLEANSTACK |
2102 | | // would be possible, which is not a softfork (and P2SH should be one). |
2103 | 97.6k | assert((flags & SCRIPT_VERIFY_P2SH) != 0); |
2104 | 97.6k | assert((flags & SCRIPT_VERIFY_WITNESS) != 0); |
2105 | 97.6k | if (stack.size() != 1) { |
2106 | 1.16k | return set_error(serror, SCRIPT_ERR_CLEANSTACK); |
2107 | 1.16k | } |
2108 | 97.6k | } |
2109 | | |
2110 | 600k | if (flags & SCRIPT_VERIFY_WITNESS) { |
2111 | | // We can't check for correct unexpected witness data if P2SH was off, so require |
2112 | | // that WITNESS implies P2SH. Otherwise, going from WITNESS->P2SH+WITNESS would be |
2113 | | // possible, which is not a softfork. |
2114 | 263k | assert((flags & SCRIPT_VERIFY_P2SH) != 0); |
2115 | 263k | if (!hadWitness && !witness->IsNull()) { |
2116 | 916 | return set_error(serror, SCRIPT_ERR_WITNESS_UNEXPECTED); |
2117 | 916 | } |
2118 | 263k | } |
2119 | | |
2120 | 599k | return set_success(serror); |
2121 | 600k | } |
2122 | | |
2123 | | size_t static WitnessSigOps(int witversion, const std::vector<unsigned char>& witprogram, const CScriptWitness& witness) |
2124 | 116k | { |
2125 | 116k | if (witversion == 0) { |
2126 | 26.3k | if (witprogram.size() == WITNESS_V0_KEYHASH_SIZE) |
2127 | 16.0k | return 1; |
2128 | | |
2129 | 10.3k | if (witprogram.size() == WITNESS_V0_SCRIPTHASH_SIZE && witness.stack.size() > 0) { |
2130 | 10.3k | CScript subscript(witness.stack.back().begin(), witness.stack.back().end()); |
2131 | 10.3k | return subscript.GetSigOpCount(true); |
2132 | 10.3k | } |
2133 | 10.3k | } |
2134 | | |
2135 | | // Future flags may be implemented here. |
2136 | 90.2k | return 0; |
2137 | 116k | } |
2138 | | |
2139 | | size_t CountWitnessSigOps(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness& witness, script_verify_flags flags) |
2140 | 154k | { |
2141 | 154k | if ((flags & SCRIPT_VERIFY_WITNESS) == 0) { |
2142 | 3 | return 0; |
2143 | 3 | } |
2144 | 154k | assert((flags & SCRIPT_VERIFY_P2SH) != 0); |
2145 | | |
2146 | 154k | int witnessversion; |
2147 | 154k | std::vector<unsigned char> witnessprogram; |
2148 | 154k | if (scriptPubKey.IsWitnessProgram(witnessversion, witnessprogram)) { |
2149 | 113k | return WitnessSigOps(witnessversion, witnessprogram, witness); |
2150 | 113k | } |
2151 | | |
2152 | 40.7k | if (scriptPubKey.IsPayToScriptHash() && scriptSig.IsPushOnly()) { |
2153 | 10.7k | CScript::const_iterator pc = scriptSig.begin(); |
2154 | 10.7k | std::vector<unsigned char> data; |
2155 | 29.9k | while (pc < scriptSig.end()) { |
2156 | 19.1k | opcodetype opcode; |
2157 | 19.1k | scriptSig.GetOp(pc, opcode, data); |
2158 | 19.1k | } |
2159 | 10.7k | CScript subscript(data.begin(), data.end()); |
2160 | 10.7k | if (subscript.IsWitnessProgram(witnessversion, witnessprogram)) { |
2161 | 3.17k | return WitnessSigOps(witnessversion, witnessprogram, witness); |
2162 | 3.17k | } |
2163 | 10.7k | } |
2164 | | |
2165 | 37.5k | return 0; |
2166 | 40.7k | } |
2167 | | |
2168 | | const std::map<std::string, script_verify_flag_name>& ScriptFlagNamesToEnum() |
2169 | 193 | { |
2170 | 4.05k | #define FLAG_NAME(flag) {std::string(#flag), SCRIPT_VERIFY_##flag} |
2171 | 193 | static const std::map<std::string, script_verify_flag_name> g_names_to_enum{ |
2172 | 193 | FLAG_NAME(P2SH), |
2173 | 193 | FLAG_NAME(STRICTENC), |
2174 | 193 | FLAG_NAME(DERSIG), |
2175 | 193 | FLAG_NAME(LOW_S), |
2176 | 193 | FLAG_NAME(SIGPUSHONLY), |
2177 | 193 | FLAG_NAME(MINIMALDATA), |
2178 | 193 | FLAG_NAME(NULLDUMMY), |
2179 | 193 | FLAG_NAME(DISCOURAGE_UPGRADABLE_NOPS), |
2180 | 193 | FLAG_NAME(CLEANSTACK), |
2181 | 193 | FLAG_NAME(MINIMALIF), |
2182 | 193 | FLAG_NAME(NULLFAIL), |
2183 | 193 | FLAG_NAME(CHECKLOCKTIMEVERIFY), |
2184 | 193 | FLAG_NAME(CHECKSEQUENCEVERIFY), |
2185 | 193 | FLAG_NAME(WITNESS), |
2186 | 193 | FLAG_NAME(DISCOURAGE_UPGRADABLE_WITNESS_PROGRAM), |
2187 | 193 | FLAG_NAME(WITNESS_PUBKEYTYPE), |
2188 | 193 | FLAG_NAME(CONST_SCRIPTCODE), |
2189 | 193 | FLAG_NAME(TAPROOT), |
2190 | 193 | FLAG_NAME(DISCOURAGE_UPGRADABLE_PUBKEYTYPE), |
2191 | 193 | FLAG_NAME(DISCOURAGE_OP_SUCCESS), |
2192 | 193 | FLAG_NAME(DISCOURAGE_UPGRADABLE_TAPROOT_VERSION), |
2193 | 193 | }; |
2194 | 193 | #undef FLAG_NAME |
2195 | 193 | return g_names_to_enum; |
2196 | 193 | } |
2197 | | |
2198 | | std::vector<std::string> GetScriptFlagNames(script_verify_flags flags) |
2199 | 231 | { |
2200 | 231 | std::vector<std::string> res; |
2201 | 231 | if (flags == SCRIPT_VERIFY_NONE) { |
2202 | 38 | return res; |
2203 | 38 | } |
2204 | 193 | script_verify_flags leftover = flags; |
2205 | 4.05k | for (const auto& [name, flag] : ScriptFlagNamesToEnum()) { |
2206 | 4.05k | if ((flags & flag) != 0) { |
2207 | 766 | res.push_back(name); |
2208 | 766 | leftover &= ~flag; |
2209 | 766 | } |
2210 | 4.05k | } |
2211 | 193 | if (leftover != 0) { |
2212 | 4 | res.push_back(strprintf("0x%08x", leftover.as_int())); |
2213 | 4 | } |
2214 | 193 | return res; |
2215 | 231 | } |