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