Branch data Line data Source code
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/script.h>
7 : :
8 : : #include <crypto/common.h>
9 : : #include <crypto/hex_base.h>
10 : : #include <hash.h>
11 : : #include <uint256.h>
12 : : #include <util/hash_type.h>
13 : :
14 : : #include <compare>
15 : : #include <string>
16 : :
17 : 703586 : CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
18 : :
19 : 12632862 : std::string GetOpName(opcodetype opcode)
20 : : {
21 : 12632862 : switch (opcode)
[ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ]
22 : : {
23 : : // push value
24 : 1569 : case OP_0 : return "0";
25 : 3 : case OP_PUSHDATA1 : return "OP_PUSHDATA1";
26 : 4 : case OP_PUSHDATA2 : return "OP_PUSHDATA2";
27 : 3 : case OP_PUSHDATA4 : return "OP_PUSHDATA4";
28 : 16295 : case OP_1NEGATE : return "-1";
29 : 85110 : case OP_RESERVED : return "OP_RESERVED";
30 : 245290 : case OP_1 : return "1";
31 : 184471 : case OP_2 : return "2";
32 : 46533 : case OP_3 : return "3";
33 : 24683 : case OP_4 : return "4";
34 : 27349 : case OP_5 : return "5";
35 : 13521 : case OP_6 : return "6";
36 : 15742 : case OP_7 : return "7";
37 : 154422 : case OP_8 : return "8";
38 : 27805 : case OP_9 : return "9";
39 : 19209 : case OP_10 : return "10";
40 : 116132 : case OP_11 : return "11";
41 : 20661 : case OP_12 : return "12";
42 : 64759 : case OP_13 : return "13";
43 : 16192 : case OP_14 : return "14";
44 : 29114 : case OP_15 : return "15";
45 : 31063 : case OP_16 : return "16";
46 : :
47 : : // control
48 : 1631774 : case OP_NOP : return "OP_NOP";
49 : 18037 : case OP_VER : return "OP_VER";
50 : 29273 : case OP_IF : return "OP_IF";
51 : 22326 : case OP_NOTIF : return "OP_NOTIF";
52 : 37686 : case OP_VERIF : return "OP_VERIF";
53 : 41360 : case OP_VERNOTIF : return "OP_VERNOTIF";
54 : 61184 : case OP_ELSE : return "OP_ELSE";
55 : 53841 : case OP_ENDIF : return "OP_ENDIF";
56 : 81025 : case OP_VERIFY : return "OP_VERIFY";
57 : 157508 : case OP_RETURN : return "OP_RETURN";
58 : :
59 : : // stack ops
60 : 30644 : case OP_TOALTSTACK : return "OP_TOALTSTACK";
61 : 22074 : case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
62 : 19835 : case OP_2DROP : return "OP_2DROP";
63 : 14958 : case OP_2DUP : return "OP_2DUP";
64 : 56951 : case OP_3DUP : return "OP_3DUP";
65 : 27555 : case OP_2OVER : return "OP_2OVER";
66 : 63534 : case OP_2ROT : return "OP_2ROT";
67 : 42899 : case OP_2SWAP : return "OP_2SWAP";
68 : 38526 : case OP_IFDUP : return "OP_IFDUP";
69 : 39574 : case OP_DEPTH : return "OP_DEPTH";
70 : 24830 : case OP_DROP : return "OP_DROP";
71 : 49613 : case OP_DUP : return "OP_DUP";
72 : 197289 : case OP_NIP : return "OP_NIP";
73 : 21104 : case OP_OVER : return "OP_OVER";
74 : 21494 : case OP_PICK : return "OP_PICK";
75 : 36102 : case OP_ROLL : return "OP_ROLL";
76 : 58831 : case OP_ROT : return "OP_ROT";
77 : 468561 : case OP_SWAP : return "OP_SWAP";
78 : 20371 : case OP_TUCK : return "OP_TUCK";
79 : :
80 : : // splice ops
81 : 22660 : case OP_CAT : return "OP_CAT";
82 : 268549 : case OP_SUBSTR : return "OP_SUBSTR";
83 : 61949 : case OP_LEFT : return "OP_LEFT";
84 : 25816 : case OP_RIGHT : return "OP_RIGHT";
85 : 16776 : case OP_SIZE : return "OP_SIZE";
86 : :
87 : : // bit logic
88 : 45605 : case OP_INVERT : return "OP_INVERT";
89 : 24694 : case OP_AND : return "OP_AND";
90 : 22493 : case OP_OR : return "OP_OR";
91 : 19198 : case OP_XOR : return "OP_XOR";
92 : 208026 : case OP_EQUAL : return "OP_EQUAL";
93 : 49094 : case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
94 : 22533 : case OP_RESERVED1 : return "OP_RESERVED1";
95 : 22076 : case OP_RESERVED2 : return "OP_RESERVED2";
96 : :
97 : : // numeric
98 : 21632 : case OP_1ADD : return "OP_1ADD";
99 : 17988 : case OP_1SUB : return "OP_1SUB";
100 : 28879 : case OP_2MUL : return "OP_2MUL";
101 : 14666 : case OP_2DIV : return "OP_2DIV";
102 : 34740 : case OP_NEGATE : return "OP_NEGATE";
103 : 19711 : case OP_ABS : return "OP_ABS";
104 : 23380 : case OP_NOT : return "OP_NOT";
105 : 173699 : case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
106 : 18071 : case OP_ADD : return "OP_ADD";
107 : 13880 : case OP_SUB : return "OP_SUB";
108 : 25366 : case OP_MUL : return "OP_MUL";
109 : 93238 : case OP_DIV : return "OP_DIV";
110 : 21031 : case OP_MOD : return "OP_MOD";
111 : 62156 : case OP_LSHIFT : return "OP_LSHIFT";
112 : 48021 : case OP_RSHIFT : return "OP_RSHIFT";
113 : 27470 : case OP_BOOLAND : return "OP_BOOLAND";
114 : 27871 : case OP_BOOLOR : return "OP_BOOLOR";
115 : 54017 : case OP_NUMEQUAL : return "OP_NUMEQUAL";
116 : 18941 : case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
117 : 29146 : case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
118 : 28206 : case OP_LESSTHAN : return "OP_LESSTHAN";
119 : 16752 : case OP_GREATERTHAN : return "OP_GREATERTHAN";
120 : 1101398 : case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
121 : 77852 : case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
122 : 28331 : case OP_MIN : return "OP_MIN";
123 : 37250 : case OP_MAX : return "OP_MAX";
124 : 27121 : case OP_WITHIN : return "OP_WITHIN";
125 : :
126 : : // crypto
127 : 21693 : case OP_RIPEMD160 : return "OP_RIPEMD160";
128 : 30214 : case OP_SHA1 : return "OP_SHA1";
129 : 17695 : case OP_SHA256 : return "OP_SHA256";
130 : 226785 : case OP_HASH160 : return "OP_HASH160";
131 : 44351 : case OP_HASH256 : return "OP_HASH256";
132 : 31774 : case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
133 : 134657 : case OP_CHECKSIG : return "OP_CHECKSIG";
134 : 44910 : case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
135 : 1329458 : case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
136 : 138741 : case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
137 : :
138 : : // expansion
139 : 35198 : case OP_NOP1 : return "OP_NOP1";
140 : 613307 : case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
141 : 104506 : case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
142 : 25083 : case OP_NOP4 : return "OP_NOP4";
143 : 52762 : case OP_NOP5 : return "OP_NOP5";
144 : 18912 : case OP_NOP6 : return "OP_NOP6";
145 : 21535 : case OP_NOP7 : return "OP_NOP7";
146 : 25748 : case OP_NOP8 : return "OP_NOP8";
147 : 17233 : case OP_NOP9 : return "OP_NOP9";
148 : 26258 : case OP_NOP10 : return "OP_NOP10";
149 : :
150 : : // Opcode added by BIP 342 (Tapscript)
151 : 34280 : case OP_CHECKSIGADD : return "OP_CHECKSIGADD";
152 : :
153 : 284482 : case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
154 : :
155 : : } // no default case, so the compiler can warn about missing cases
156 : 1924309 : return "OP_UNKNOWN";
157 : : }
158 : :
159 : 52721732 : unsigned int CScript::GetSigOpCount(bool fAccurate) const
160 : : {
161 : 52721732 : unsigned int n = 0;
162 [ + + ]: 52721732 : const_iterator pc = begin();
163 : 52721732 : opcodetype lastOpcode = OP_INVALIDOPCODE;
164 [ + + + + ]: 510869490 : while (pc < end())
165 : : {
166 : 203529118 : opcodetype opcode;
167 [ + + ]: 203529118 : if (!GetOp(pc, opcode))
168 : : break;
169 [ + + ]: 202713013 : if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
170 : 841871 : n++;
171 [ + + ]: 201871142 : else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
172 : : {
173 [ + + + + ]: 1161135 : if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
174 : 9843 : n += DecodeOP_N(lastOpcode);
175 : : else
176 : 1151292 : n += MAX_PUBKEYS_PER_MULTISIG;
177 : : }
178 : : lastOpcode = opcode;
179 : : }
180 : 52721732 : return n;
181 : : }
182 : :
183 : 3938955 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
184 : : {
185 [ + + ]: 3938955 : if (!IsPayToScriptHash())
186 : 3924259 : return GetSigOpCount(true);
187 : :
188 : : // This is a pay-to-script-hash scriptPubKey;
189 : : // get the last item that the scriptSig
190 : : // pushes onto the stack:
191 [ + + ]: 14696 : const_iterator pc = scriptSig.begin();
192 : 14696 : std::vector<unsigned char> vData;
193 [ + + + + ]: 550458 : while (pc < scriptSig.end())
194 : : {
195 : 271068 : opcodetype opcode;
196 [ + - + + ]: 271068 : if (!scriptSig.GetOp(pc, opcode, vData))
197 : : return 0;
198 [ + + ]: 265037 : if (opcode > OP_16)
199 : : return 0;
200 : : }
201 : :
202 : : /// ... and return its opcount:
203 : 4161 : CScript subscript(vData.begin(), vData.end());
204 [ + - ]: 4161 : return subscript.GetSigOpCount(true);
205 : 18857 : }
206 : :
207 : 3804623 : bool CScript::IsPayToAnchor() const
208 : : {
209 [ + + ]: 3804623 : return (this->size() == 4 &&
210 [ + + + + ]: 255766 : (*this)[0] == OP_1 &&
211 [ + + + + ]: 27920 : (*this)[1] == 0x02 &&
212 [ + + + + : 3832521 : (*this)[2] == 0x4e &&
+ + ]
213 [ + + + + ]: 13738 : (*this)[3] == 0x73);
214 : : }
215 : :
216 : 44482 : bool CScript::IsPayToAnchor(int version, const std::vector<unsigned char>& program)
217 : : {
218 : 55435 : return version == 1 &&
219 [ + + ]: 10953 : program.size() == 2 &&
220 [ + + + + ]: 50863 : program[0] == 0x4e &&
221 [ + + ]: 3185 : program[1] == 0x73;
222 : : }
223 : :
224 : 46006905 : bool CScript::IsPayToScriptHash() const
225 : : {
226 : : // Extra-fast test for pay-to-script-hash CScripts:
227 [ + + ]: 46006905 : return (this->size() == 23 &&
228 [ + + + + ]: 3157516 : (*this)[0] == OP_HASH160 &&
229 [ + + + + : 49086411 : (*this)[1] == 0x14 &&
+ + ]
230 [ + + + + ]: 3056492 : (*this)[22] == OP_EQUAL);
231 : : }
232 : :
233 : 69457 : bool CScript::IsPayToWitnessScriptHash() const
234 : : {
235 : : // Extra-fast test for pay-to-witness-script-hash CScripts:
236 [ + + ]: 69457 : return (this->size() == 34 &&
237 [ + + + + : 94640 : (*this)[0] == OP_0 &&
+ + ]
238 [ + + + + ]: 50168 : (*this)[1] == 0x20);
239 : : }
240 : :
241 : 50874 : bool CScript::IsPayToTaproot() const
242 : : {
243 [ + + ]: 50874 : return (this->size() == 34 &&
244 [ + + + - : 61248 : (*this)[0] == OP_1 &&
+ + ]
245 [ + - + + ]: 19114 : (*this)[1] == 0x20);
246 : : }
247 : :
248 : : // A witness program is any valid CScript that consists of a 1-byte push opcode
249 : : // followed by a data push between 2 and 40 bytes.
250 : 155541493 : bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
251 : : {
252 [ + + + + : 157235024 : if (this->size() < 4 || this->size() > 42) {
+ + + + ]
253 : : return false;
254 : : }
255 [ + + + + : 298664402 : if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
+ + + + ]
256 : : return false;
257 : : }
258 [ + + + + ]: 294889430 : if ((size_t)((*this)[1] + 2) == this->size()) {
259 : 147291215 : version = DecodeOP_N((opcodetype)(*this)[0]);
260 [ + + ]: 441873645 : program = std::vector<unsigned char>(this->begin() + 2, this->end());
261 : 147291215 : return true;
262 : : }
263 : : return false;
264 : : }
265 : :
266 : 11627369 : bool CScript::IsPushOnly(const_iterator pc) const
267 : : {
268 [ + + + + ]: 70538422 : while (pc < end())
269 : : {
270 : 23763410 : opcodetype opcode;
271 [ + + ]: 23763410 : if (!GetOp(pc, opcode))
272 : : return false;
273 : : // Note that IsPushOnly() *does* consider OP_RESERVED to be a
274 : : // push-type opcode, however execution of OP_RESERVED fails, so
275 : : // it's not relevant to P2SH/BIP62 as the scriptSig would fail prior to
276 : : // the P2SH special validation code being executed.
277 [ + + ]: 23703796 : if (opcode > OP_16)
278 : : return false;
279 : : }
280 : : return true;
281 : : }
282 : :
283 : 11524284 : bool CScript::IsPushOnly() const
284 : : {
285 [ + + ]: 23048568 : return this->IsPushOnly(begin());
286 : : }
287 : :
288 : 1473534 : std::string CScriptWitness::ToString() const
289 : : {
290 : 1473534 : std::string ret = "CScriptWitness(";
291 [ - + + + ]: 14142626 : for (unsigned int i = 0; i < stack.size(); i++) {
292 [ + + ]: 12669092 : if (i) {
293 [ + - ]: 12622055 : ret += ", ";
294 : : }
295 [ - + + - ]: 25338184 : ret += HexStr(stack[i]);
296 : : }
297 [ + - ]: 1473534 : return ret + ")";
298 : 1473534 : }
299 : :
300 : 38154 : bool CScript::HasValidOps() const
301 : : {
302 [ + + ]: 38154 : CScript::const_iterator it = begin();
303 [ + + + + ]: 5463582 : while (it < end()) {
304 : 2696284 : opcodetype opcode;
305 : 2696284 : std::vector<unsigned char> item;
306 [ + - + + : 5389969 : if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
+ + + + ]
307 : 2647 : return false;
308 : : }
309 : 2696284 : }
310 : : return true;
311 : : }
312 : :
313 : 808229359 : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
314 : : {
315 : 808229359 : opcodeRet = OP_INVALIDOPCODE;
316 [ + + ]: 808229359 : if (pvchRet)
317 [ + + ]: 123256132 : pvchRet->clear();
318 [ + + + + ]: 1616458718 : if (pc >= end)
319 : : return false;
320 : :
321 : : // Read instruction
322 [ + - ]: 807582855 : if (end - pc < 1)
323 : : return false;
324 [ + + ]: 807582855 : unsigned int opcode = *pc++;
325 : :
326 : : // Immediate operand
327 [ + + ]: 807582855 : if (opcode <= OP_PUSHDATA4)
328 : : {
329 : 314165080 : unsigned int nSize = 0;
330 [ + + ]: 314165080 : if (opcode < OP_PUSHDATA1)
331 : : {
332 : : nSize = opcode;
333 : : }
334 [ + + ]: 86961941 : else if (opcode == OP_PUSHDATA1)
335 : : {
336 [ + + ]: 85458411 : if (end - pc < 1)
337 : : return false;
338 : 85435287 : nSize = *pc++;
339 : : }
340 [ + + ]: 1503530 : else if (opcode == OP_PUSHDATA2)
341 : : {
342 [ + + ]: 874219 : if (end - pc < 2)
343 : : return false;
344 : 843269 : nSize = ReadLE16(&pc[0]);
345 : 843269 : pc += 2;
346 : : }
347 [ + - ]: 629311 : else if (opcode == OP_PUSHDATA4)
348 : : {
349 [ + + ]: 629311 : if (end - pc < 4)
350 : : return false;
351 : 604810 : nSize = ReadLE32(&pc[0]);
352 : 604810 : pc += 4;
353 : : }
354 [ + - + + ]: 314086505 : if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
355 : 2458182 : return false;
356 [ + + ]: 311628323 : if (pvchRet)
357 : 56096966 : pvchRet->assign(pc, pc + nSize);
358 : 311628323 : pc += nSize;
359 : : }
360 : :
361 : 805046098 : opcodeRet = static_cast<opcodetype>(opcode);
362 : 805046098 : return true;
363 : : }
364 : :
365 : 1027193 : bool IsOpSuccess(const opcodetype& opcode)
366 : : {
367 [ + + + + ]: 1027175 : return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
368 [ + + + + ]: 1027147 : (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
369 [ + + + + : 2054297 : (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
+ + ]
370 [ + + ]: 1027080 : (opcode >= 187 && opcode <= 254);
371 : : }
372 : :
373 : 21181254 : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {
374 : : // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
375 [ - + ]: 21181254 : assert(0 <= opcode && opcode <= OP_PUSHDATA4);
376 [ - + + + ]: 21181254 : if (data.size() == 0) {
377 : : // Should have used OP_0.
378 : 16518725 : return opcode == OP_0;
379 [ + + + + : 4662529 : } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
+ + ]
380 : : // Should have used OP_1 .. OP_16.
381 : : return false;
382 [ + + + + ]: 4647318 : } else if (data.size() == 1 && data[0] == 0x81) {
383 : : // Should have used OP_1NEGATE.
384 : : return false;
385 [ + + ]: 4643855 : } else if (data.size() <= 75) {
386 : : // Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
387 : 4581630 : return opcode == data.size();
388 [ + + ]: 62225 : } else if (data.size() <= 255) {
389 : : // Must have used OP_PUSHDATA.
390 : 39780 : return opcode == OP_PUSHDATA1;
391 [ + + ]: 22445 : } else if (data.size() <= 65535) {
392 : : // Must have used OP_PUSHDATA2.
393 : 22378 : return opcode == OP_PUSHDATA2;
394 : : }
395 : : return true;
396 : : }
|