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 <string>
15 : :
16 : 218782 : CScriptID::CScriptID(const CScript& in) : BaseHash(Hash160(in)) {}
17 : :
18 : 8200314 : std::string GetOpName(opcodetype opcode)
19 : : {
20 [ + + + + : 8200314 : switch (opcode)
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + + +
+ + + +
+ ]
21 : : {
22 : : // push value
23 : 1259 : case OP_0 : return "0";
24 : 3 : case OP_PUSHDATA1 : return "OP_PUSHDATA1";
25 : 2 : case OP_PUSHDATA2 : return "OP_PUSHDATA2";
26 : 3 : case OP_PUSHDATA4 : return "OP_PUSHDATA4";
27 : 6526 : case OP_1NEGATE : return "-1";
28 : 30130 : case OP_RESERVED : return "OP_RESERVED";
29 : 122688 : case OP_1 : return "1";
30 : 54218 : case OP_2 : return "2";
31 : 65213 : case OP_3 : return "3";
32 : 17854 : case OP_4 : return "4";
33 : 13875 : case OP_5 : return "5";
34 : 7694 : case OP_6 : return "6";
35 : 5386 : case OP_7 : return "7";
36 : 206429 : case OP_8 : return "8";
37 : 16043 : case OP_9 : return "9";
38 : 8586 : case OP_10 : return "10";
39 : 68125 : case OP_11 : return "11";
40 : 16506 : case OP_12 : return "12";
41 : 32298 : case OP_13 : return "13";
42 : 10651 : case OP_14 : return "14";
43 : 18094 : case OP_15 : return "15";
44 : 12203 : case OP_16 : return "16";
45 : :
46 : : // control
47 : 938413 : case OP_NOP : return "OP_NOP";
48 : 10874 : case OP_VER : return "OP_VER";
49 : 16223 : case OP_IF : return "OP_IF";
50 : 14789 : case OP_NOTIF : return "OP_NOTIF";
51 : 14313 : case OP_VERIF : return "OP_VERIF";
52 : 16819 : case OP_VERNOTIF : return "OP_VERNOTIF";
53 : 159360 : case OP_ELSE : return "OP_ELSE";
54 : 22809 : case OP_ENDIF : return "OP_ENDIF";
55 : 33500 : case OP_VERIFY : return "OP_VERIFY";
56 : 49879 : case OP_RETURN : return "OP_RETURN";
57 : :
58 : : // stack ops
59 : 22854 : case OP_TOALTSTACK : return "OP_TOALTSTACK";
60 : 19401 : case OP_FROMALTSTACK : return "OP_FROMALTSTACK";
61 : 12970 : case OP_2DROP : return "OP_2DROP";
62 : 8772 : case OP_2DUP : return "OP_2DUP";
63 : 34001 : case OP_3DUP : return "OP_3DUP";
64 : 12379 : case OP_2OVER : return "OP_2OVER";
65 : 10384 : case OP_2ROT : return "OP_2ROT";
66 : 17520 : case OP_2SWAP : return "OP_2SWAP";
67 : 20925 : case OP_IFDUP : return "OP_IFDUP";
68 : 16859 : case OP_DEPTH : return "OP_DEPTH";
69 : 13883 : case OP_DROP : return "OP_DROP";
70 : 34807 : case OP_DUP : return "OP_DUP";
71 : 80864 : case OP_NIP : return "OP_NIP";
72 : 10270 : case OP_OVER : return "OP_OVER";
73 : 11885 : case OP_PICK : return "OP_PICK";
74 : 14520 : case OP_ROLL : return "OP_ROLL";
75 : 29407 : case OP_ROT : return "OP_ROT";
76 : 307196 : case OP_SWAP : return "OP_SWAP";
77 : 12287 : case OP_TUCK : return "OP_TUCK";
78 : :
79 : : // splice ops
80 : 10706 : case OP_CAT : return "OP_CAT";
81 : 110663 : case OP_SUBSTR : return "OP_SUBSTR";
82 : 40159 : case OP_LEFT : return "OP_LEFT";
83 : 24921 : case OP_RIGHT : return "OP_RIGHT";
84 : 9495 : case OP_SIZE : return "OP_SIZE";
85 : :
86 : : // bit logic
87 : 15906 : case OP_INVERT : return "OP_INVERT";
88 : 32958 : case OP_AND : return "OP_AND";
89 : 13284 : case OP_OR : return "OP_OR";
90 : 10150 : case OP_XOR : return "OP_XOR";
91 : 153278 : case OP_EQUAL : return "OP_EQUAL";
92 : 32774 : case OP_EQUALVERIFY : return "OP_EQUALVERIFY";
93 : 10222 : case OP_RESERVED1 : return "OP_RESERVED1";
94 : 12810 : case OP_RESERVED2 : return "OP_RESERVED2";
95 : :
96 : : // numeric
97 : 10890 : case OP_1ADD : return "OP_1ADD";
98 : 8746 : case OP_1SUB : return "OP_1SUB";
99 : 13023 : case OP_2MUL : return "OP_2MUL";
100 : 7425 : case OP_2DIV : return "OP_2DIV";
101 : 8794 : case OP_NEGATE : return "OP_NEGATE";
102 : 8232 : case OP_ABS : return "OP_ABS";
103 : 10274 : case OP_NOT : return "OP_NOT";
104 : 77763 : case OP_0NOTEQUAL : return "OP_0NOTEQUAL";
105 : 13233 : case OP_ADD : return "OP_ADD";
106 : 10866 : case OP_SUB : return "OP_SUB";
107 : 13614 : case OP_MUL : return "OP_MUL";
108 : 56647 : case OP_DIV : return "OP_DIV";
109 : 13425 : case OP_MOD : return "OP_MOD";
110 : 39353 : case OP_LSHIFT : return "OP_LSHIFT";
111 : 14850 : case OP_RSHIFT : return "OP_RSHIFT";
112 : 48484 : case OP_BOOLAND : return "OP_BOOLAND";
113 : 16276 : case OP_BOOLOR : return "OP_BOOLOR";
114 : 17399 : case OP_NUMEQUAL : return "OP_NUMEQUAL";
115 : 10054 : case OP_NUMEQUALVERIFY : return "OP_NUMEQUALVERIFY";
116 : 11821 : case OP_NUMNOTEQUAL : return "OP_NUMNOTEQUAL";
117 : 19895 : case OP_LESSTHAN : return "OP_LESSTHAN";
118 : 9267 : case OP_GREATERTHAN : return "OP_GREATERTHAN";
119 : 500232 : case OP_LESSTHANOREQUAL : return "OP_LESSTHANOREQUAL";
120 : 10430 : case OP_GREATERTHANOREQUAL : return "OP_GREATERTHANOREQUAL";
121 : 24686 : case OP_MIN : return "OP_MIN";
122 : 72349 : case OP_MAX : return "OP_MAX";
123 : 25367 : case OP_WITHIN : return "OP_WITHIN";
124 : :
125 : : // crypto
126 : 12590 : case OP_RIPEMD160 : return "OP_RIPEMD160";
127 : 20151 : case OP_SHA1 : return "OP_SHA1";
128 : 9601 : case OP_SHA256 : return "OP_SHA256";
129 : 164366 : case OP_HASH160 : return "OP_HASH160";
130 : 12078 : case OP_HASH256 : return "OP_HASH256";
131 : 12131 : case OP_CODESEPARATOR : return "OP_CODESEPARATOR";
132 : 128405 : case OP_CHECKSIG : return "OP_CHECKSIG";
133 : 24375 : case OP_CHECKSIGVERIFY : return "OP_CHECKSIGVERIFY";
134 : 511699 : case OP_CHECKMULTISIG : return "OP_CHECKMULTISIG";
135 : 89685 : case OP_CHECKMULTISIGVERIFY : return "OP_CHECKMULTISIGVERIFY";
136 : :
137 : : // expansion
138 : 20222 : case OP_NOP1 : return "OP_NOP1";
139 : 290490 : case OP_CHECKLOCKTIMEVERIFY : return "OP_CHECKLOCKTIMEVERIFY";
140 : 14918 : case OP_CHECKSEQUENCEVERIFY : return "OP_CHECKSEQUENCEVERIFY";
141 : 12626 : case OP_NOP4 : return "OP_NOP4";
142 : 32956 : case OP_NOP5 : return "OP_NOP5";
143 : 10252 : case OP_NOP6 : return "OP_NOP6";
144 : 9186 : case OP_NOP7 : return "OP_NOP7";
145 : 13859 : case OP_NOP8 : return "OP_NOP8";
146 : 9390 : case OP_NOP9 : return "OP_NOP9";
147 : 19014 : case OP_NOP10 : return "OP_NOP10";
148 : :
149 : : // Opcode added by BIP 342 (Tapscript)
150 : 12620 : case OP_CHECKSIGADD : return "OP_CHECKSIGADD";
151 : :
152 : 87226 : case OP_INVALIDOPCODE : return "OP_INVALIDOPCODE";
153 : :
154 : 2449804 : default:
155 : 2449804 : return "OP_UNKNOWN";
156 : : }
157 : : }
158 : :
159 : 12989759 : unsigned int CScript::GetSigOpCount(bool fAccurate) const
160 : : {
161 : 12989759 : unsigned int n = 0;
162 [ + + ]: 12989759 : const_iterator pc = begin();
163 : 12989759 : opcodetype lastOpcode = OP_INVALIDOPCODE;
164 [ + + ]: 89927403 : while (pc < end())
165 : : {
166 : 77361915 : opcodetype opcode;
167 [ + + ]: 77361915 : if (!GetOp(pc, opcode))
168 : : break;
169 [ + + ]: 76937644 : if (opcode == OP_CHECKSIG || opcode == OP_CHECKSIGVERIFY)
170 : 504545 : n++;
171 [ + + ]: 76433099 : else if (opcode == OP_CHECKMULTISIG || opcode == OP_CHECKMULTISIGVERIFY)
172 : : {
173 [ + + + + ]: 1055433 : if (fAccurate && lastOpcode >= OP_1 && lastOpcode <= OP_16)
174 : 1100 : n += DecodeOP_N(lastOpcode);
175 : : else
176 : 1054333 : n += MAX_PUBKEYS_PER_MULTISIG;
177 : : }
178 : : lastOpcode = opcode;
179 : : }
180 : 12989759 : return n;
181 : : }
182 : :
183 : 2445 : unsigned int CScript::GetSigOpCount(const CScript& scriptSig) const
184 : : {
185 [ + + ]: 2445 : if (!IsPayToScriptHash())
186 : 233 : 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 [ + + ]: 2212 : const_iterator pc = scriptSig.begin();
192 : 2212 : std::vector<unsigned char> vData;
193 [ + + ]: 14035 : while (pc < scriptSig.end())
194 : : {
195 : 13537 : opcodetype opcode;
196 [ + - + + ]: 13537 : if (!scriptSig.GetOp(pc, opcode, vData))
197 : : return 0;
198 [ + + ]: 11933 : if (opcode > OP_16)
199 : : return 0;
200 : : }
201 : :
202 : : /// ... and return its opcount:
203 : 498 : CScript subscript(vData.begin(), vData.end());
204 [ + - ]: 498 : return subscript.GetSigOpCount(true);
205 : 2710 : }
206 : :
207 : 1607118 : bool CScript::IsPayToAnchor() const
208 : : {
209 [ + + ]: 1607118 : return (this->size() == 4 &&
210 [ + + + + ]: 107852 : (*this)[0] == OP_1 &&
211 [ + + + + ]: 11750 : (*this)[1] == 0x02 &&
212 [ + + + + : 1618864 : (*this)[2] == 0x4e &&
+ + ]
213 [ + + + + ]: 5960 : (*this)[3] == 0x73);
214 : : }
215 : :
216 : 5931 : bool CScript::IsPayToAnchor(int version, const std::vector<unsigned char>& program)
217 : : {
218 : 10711 : return version == 1 &&
219 [ + + ]: 4780 : program.size() == 2 &&
220 [ + + + + ]: 9264 : program[0] == 0x4e &&
221 [ + + ]: 2455 : program[1] == 0x73;
222 : : }
223 : :
224 : 16717480 : bool CScript::IsPayToScriptHash() const
225 : : {
226 : : // Extra-fast test for pay-to-script-hash CScripts:
227 [ + + ]: 16717480 : return (this->size() == 23 &&
228 [ + + + + ]: 1747148 : (*this)[0] == OP_HASH160 &&
229 [ + + + + : 18430434 : (*this)[1] == 0x14 &&
+ + ]
230 [ + + + + ]: 1704728 : (*this)[22] == OP_EQUAL);
231 : : }
232 : :
233 : 4865 : bool CScript::IsPayToWitnessScriptHash() const
234 : : {
235 : : // Extra-fast test for pay-to-witness-script-hash CScripts:
236 [ + + + + ]: 4904 : return (this->size() == 34 &&
237 [ + + + + : 4281 : (*this)[0] == OP_0 &&
+ + ]
238 [ + + ]: 9 : (*this)[1] == 0x20);
239 : : }
240 : :
241 : 23452 : bool CScript::IsPayToTaproot() const
242 : : {
243 [ + + + + ]: 26212 : return (this->size() == 34 &&
244 [ + + + + : 7517 : (*this)[0] == OP_1 &&
+ + ]
245 [ + + ]: 1510 : (*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 : 37426411 : bool CScript::IsWitnessProgram(int& version, std::vector<unsigned char>& program) const
251 : : {
252 [ + + + + : 71378004 : if (this->size() < 4 || this->size() > 42) {
+ + + + ]
253 : : return false;
254 : : }
255 [ + + + + : 68640942 : if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
+ + + + ]
256 : : return false;
257 : : }
258 [ + + + + ]: 66947846 : if ((size_t)((*this)[1] + 2) == this->size()) {
259 : 33412004 : version = DecodeOP_N((opcodetype)(*this)[0]);
260 [ + + ]: 100236012 : program = std::vector<unsigned char>(this->begin() + 2, this->end());
261 : 33412004 : return true;
262 : : }
263 : : return false;
264 : : }
265 : :
266 : 3782566 : bool CScript::IsPushOnly(const_iterator pc) const
267 : : {
268 [ + + ]: 8327085 : while (pc < end())
269 : : {
270 : 4588351 : opcodetype opcode;
271 [ + + ]: 4588351 : 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 [ + + ]: 4566048 : if (opcode > OP_16)
278 : : return false;
279 : : }
280 : : return true;
281 : : }
282 : :
283 : 3738658 : bool CScript::IsPushOnly() const
284 : : {
285 [ + + ]: 7477316 : return this->IsPushOnly(begin());
286 : : }
287 : :
288 : 1277803 : std::string CScriptWitness::ToString() const
289 : : {
290 : 1277803 : std::string ret = "CScriptWitness(";
291 [ + + ]: 6006057 : for (unsigned int i = 0; i < stack.size(); i++) {
292 [ + + ]: 4728254 : if (i) {
293 [ + - ]: 4700078 : ret += ", ";
294 : : }
295 [ + - ]: 9456508 : ret += HexStr(stack[i]);
296 : : }
297 [ + - ]: 1277803 : return ret + ")";
298 : 1277803 : }
299 : :
300 : 537301 : bool CScript::HasValidOps() const
301 : : {
302 [ + + ]: 537301 : CScript::const_iterator it = begin();
303 [ + + ]: 10463118 : while (it < end()) {
304 : 9930599 : opcodetype opcode;
305 : 9930599 : std::vector<unsigned char> item;
306 [ + - + + : 9930599 : if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE || item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
+ + + + ]
307 : 4782 : return false;
308 : : }
309 : 9930599 : }
310 : : return true;
311 : : }
312 : :
313 : 321730864 : bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet)
314 : : {
315 : 321730864 : opcodeRet = OP_INVALIDOPCODE;
316 [ + + ]: 321730864 : if (pvchRet)
317 [ + + ]: 62756218 : pvchRet->clear();
318 [ + + ]: 321730864 : if (pc >= end)
319 : : return false;
320 : :
321 : : // Read instruction
322 [ + - ]: 321346556 : if (end - pc < 1)
323 : : return false;
324 [ + + ]: 321346556 : unsigned int opcode = *pc++;
325 : :
326 : : // Immediate operand
327 [ + + ]: 321346556 : if (opcode <= OP_PUSHDATA4)
328 : : {
329 : 87725724 : unsigned int nSize = 0;
330 [ + + ]: 87725724 : if (opcode < OP_PUSHDATA1)
331 : : {
332 : : nSize = opcode;
333 : : }
334 [ + + ]: 16319091 : else if (opcode == OP_PUSHDATA1)
335 : : {
336 [ + + ]: 16057161 : if (end - pc < 1)
337 : : return false;
338 : 16049049 : nSize = *pc++;
339 : : }
340 [ + + ]: 261930 : else if (opcode == OP_PUSHDATA2)
341 : : {
342 [ + + ]: 193990 : if (end - pc < 2)
343 : : return false;
344 : 182161 : nSize = ReadLE16(&pc[0]);
345 : 182161 : pc += 2;
346 : : }
347 [ + - ]: 67940 : else if (opcode == OP_PUSHDATA4)
348 : : {
349 [ + + ]: 67940 : if (end - pc < 4)
350 : : return false;
351 : 61165 : nSize = ReadLE32(&pc[0]);
352 : 61165 : pc += 4;
353 : : }
354 [ + - + + ]: 87699008 : if (end - pc < 0 || (unsigned int)(end - pc) < nSize)
355 : 1261756 : return false;
356 [ + + ]: 86437252 : if (pvchRet)
357 : 23772149 : pvchRet->assign(pc, pc + nSize);
358 : 86437252 : pc += nSize;
359 : : }
360 : :
361 : 320058084 : opcodeRet = static_cast<opcodetype>(opcode);
362 : 320058084 : return true;
363 : : }
364 : :
365 : 787314 : bool IsOpSuccess(const opcodetype& opcode)
366 : : {
367 [ + + + + : 787314 : return opcode == 80 || opcode == 98 || (opcode >= 126 && opcode <= 129) ||
+ + + +
+ ]
368 : : (opcode >= 131 && opcode <= 134) || (opcode >= 137 && opcode <= 138) ||
369 : : (opcode >= 141 && opcode <= 142) || (opcode >= 149 && opcode <= 153) ||
370 : 787314 : (opcode >= 187 && opcode <= 254);
371 : : }
372 : :
373 : 10396038 : bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode) {
374 : : // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
375 [ - + ]: 10396038 : assert(0 <= opcode && opcode <= OP_PUSHDATA4);
376 [ + + ]: 10396038 : if (data.size() == 0) {
377 : : // Should have used OP_0.
378 : 7999782 : return opcode == OP_0;
379 [ + + + + : 2396256 : } else if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
+ + ]
380 : : // Should have used OP_1 .. OP_16.
381 : : return false;
382 [ + + + + ]: 2390651 : } else if (data.size() == 1 && data[0] == 0x81) {
383 : : // Should have used OP_1NEGATE.
384 : : return false;
385 [ + + ]: 2389550 : } else if (data.size() <= 75) {
386 : : // Must have used a direct push (opcode indicating number of bytes pushed + those bytes).
387 : 2332764 : return opcode == data.size();
388 [ + + ]: 56786 : } else if (data.size() <= 255) {
389 : : // Must have used OP_PUSHDATA.
390 : 41589 : return opcode == OP_PUSHDATA1;
391 [ + + ]: 15197 : } else if (data.size() <= 65535) {
392 : : // Must have used OP_PUSHDATA2.
393 : 15170 : return opcode == OP_PUSHDATA2;
394 : : }
395 : : return true;
396 : : }
|