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