Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
script.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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
9#include <util/strencodings.h>
10
11#include <algorithm>
12#include <string>
13
14std::string GetOpName(opcodetype opcode) {
15 switch (opcode) {
16 // push value
17 case OP_0:
18 return "0";
19 case OP_PUSHDATA1:
20 return "OP_PUSHDATA1";
21 case OP_PUSHDATA2:
22 return "OP_PUSHDATA2";
23 case OP_PUSHDATA4:
24 return "OP_PUSHDATA4";
25 case OP_1NEGATE:
26 return "-1";
27 case OP_RESERVED:
28 return "OP_RESERVED";
29 case OP_1:
30 return "1";
31 case OP_2:
32 return "2";
33 case OP_3:
34 return "3";
35 case OP_4:
36 return "4";
37 case OP_5:
38 return "5";
39 case OP_6:
40 return "6";
41 case OP_7:
42 return "7";
43 case OP_8:
44 return "8";
45 case OP_9:
46 return "9";
47 case OP_10:
48 return "10";
49 case OP_11:
50 return "11";
51 case OP_12:
52 return "12";
53 case OP_13:
54 return "13";
55 case OP_14:
56 return "14";
57 case OP_15:
58 return "15";
59 case OP_16:
60 return "16";
61
62 // control
63 case OP_NOP:
64 return "OP_NOP";
65 case OP_VER:
66 return "OP_VER";
67 case OP_IF:
68 return "OP_IF";
69 case OP_NOTIF:
70 return "OP_NOTIF";
71 case OP_VERIF:
72 return "OP_VERIF";
73 case OP_VERNOTIF:
74 return "OP_VERNOTIF";
75 case OP_ELSE:
76 return "OP_ELSE";
77 case OP_ENDIF:
78 return "OP_ENDIF";
79 case OP_VERIFY:
80 return "OP_VERIFY";
81 case OP_RETURN:
82 return "OP_RETURN";
83
84 // stack ops
85 case OP_TOALTSTACK:
86 return "OP_TOALTSTACK";
87 case OP_FROMALTSTACK:
88 return "OP_FROMALTSTACK";
89 case OP_2DROP:
90 return "OP_2DROP";
91 case OP_2DUP:
92 return "OP_2DUP";
93 case OP_3DUP:
94 return "OP_3DUP";
95 case OP_2OVER:
96 return "OP_2OVER";
97 case OP_2ROT:
98 return "OP_2ROT";
99 case OP_2SWAP:
100 return "OP_2SWAP";
101 case OP_IFDUP:
102 return "OP_IFDUP";
103 case OP_DEPTH:
104 return "OP_DEPTH";
105 case OP_DROP:
106 return "OP_DROP";
107 case OP_DUP:
108 return "OP_DUP";
109 case OP_NIP:
110 return "OP_NIP";
111 case OP_OVER:
112 return "OP_OVER";
113 case OP_PICK:
114 return "OP_PICK";
115 case OP_ROLL:
116 return "OP_ROLL";
117 case OP_ROT:
118 return "OP_ROT";
119 case OP_SWAP:
120 return "OP_SWAP";
121 case OP_TUCK:
122 return "OP_TUCK";
123
124 // splice ops
125 case OP_CAT:
126 return "OP_CAT";
127 case OP_SPLIT:
128 return "OP_SPLIT";
129 case OP_NUM2BIN:
130 return "OP_NUM2BIN";
131 case OP_BIN2NUM:
132 return "OP_BIN2NUM";
133 case OP_SIZE:
134 return "OP_SIZE";
135
136 // bit logic
137 case OP_INVERT:
138 return "OP_INVERT";
139 case OP_AND:
140 return "OP_AND";
141 case OP_OR:
142 return "OP_OR";
143 case OP_XOR:
144 return "OP_XOR";
145 case OP_EQUAL:
146 return "OP_EQUAL";
147 case OP_EQUALVERIFY:
148 return "OP_EQUALVERIFY";
149 case OP_RESERVED1:
150 return "OP_RESERVED1";
151 case OP_RESERVED2:
152 return "OP_RESERVED2";
153
154 // numeric
155 case OP_1ADD:
156 return "OP_1ADD";
157 case OP_1SUB:
158 return "OP_1SUB";
159 case OP_2MUL:
160 return "OP_2MUL";
161 case OP_2DIV:
162 return "OP_2DIV";
163 case OP_NEGATE:
164 return "OP_NEGATE";
165 case OP_ABS:
166 return "OP_ABS";
167 case OP_NOT:
168 return "OP_NOT";
169 case OP_0NOTEQUAL:
170 return "OP_0NOTEQUAL";
171 case OP_ADD:
172 return "OP_ADD";
173 case OP_SUB:
174 return "OP_SUB";
175 case OP_MUL:
176 return "OP_MUL";
177 case OP_DIV:
178 return "OP_DIV";
179 case OP_MOD:
180 return "OP_MOD";
181 case OP_LSHIFT:
182 return "OP_LSHIFT";
183 case OP_RSHIFT:
184 return "OP_RSHIFT";
185 case OP_BOOLAND:
186 return "OP_BOOLAND";
187 case OP_BOOLOR:
188 return "OP_BOOLOR";
189 case OP_NUMEQUAL:
190 return "OP_NUMEQUAL";
192 return "OP_NUMEQUALVERIFY";
193 case OP_NUMNOTEQUAL:
194 return "OP_NUMNOTEQUAL";
195 case OP_LESSTHAN:
196 return "OP_LESSTHAN";
197 case OP_GREATERTHAN:
198 return "OP_GREATERTHAN";
200 return "OP_LESSTHANOREQUAL";
202 return "OP_GREATERTHANOREQUAL";
203 case OP_MIN:
204 return "OP_MIN";
205 case OP_MAX:
206 return "OP_MAX";
207 case OP_WITHIN:
208 return "OP_WITHIN";
209
210 // crypto
211 case OP_RIPEMD160:
212 return "OP_RIPEMD160";
213 case OP_SHA1:
214 return "OP_SHA1";
215 case OP_SHA256:
216 return "OP_SHA256";
217 case OP_HASH160:
218 return "OP_HASH160";
219 case OP_HASH256:
220 return "OP_HASH256";
221 case OP_CODESEPARATOR:
222 return "OP_CODESEPARATOR";
223 case OP_CHECKSIG:
224 return "OP_CHECKSIG";
226 return "OP_CHECKSIGVERIFY";
227 case OP_CHECKMULTISIG:
228 return "OP_CHECKMULTISIG";
230 return "OP_CHECKMULTISIGVERIFY";
231 case OP_CHECKDATASIG:
232 return "OP_CHECKDATASIG";
234 return "OP_CHECKDATASIGVERIFY";
235 case OP_REVERSEBYTES:
236 return "OP_REVERSEBYTES";
237
238 // expansion
239 case OP_NOP1:
240 return "OP_NOP1";
242 return "OP_CHECKLOCKTIMEVERIFY";
244 return "OP_CHECKSEQUENCEVERIFY";
245 case OP_NOP4:
246 return "OP_NOP4";
247 case OP_NOP5:
248 return "OP_NOP5";
249 case OP_NOP6:
250 return "OP_NOP6";
251 case OP_NOP7:
252 return "OP_NOP7";
253 case OP_NOP8:
254 return "OP_NOP8";
255 case OP_NOP9:
256 return "OP_NOP9";
257 case OP_NOP10:
258 return "OP_NOP10";
259
260 case OP_INVALIDOPCODE:
261 return "OP_INVALIDOPCODE";
262
263 default:
264 return "OP_UNKNOWN";
265 }
266}
267
268bool CheckMinimalPush(const std::vector<uint8_t> &data, opcodetype opcode) {
269 // Excludes OP_1NEGATE, OP_1-16 since they are by definition minimal
270 assert(0 <= opcode && opcode <= OP_PUSHDATA4);
271 if (data.size() == 0) {
272 // Should have used OP_0.
273 return opcode == OP_0;
274 }
275 if (data.size() == 1 && data[0] >= 1 && data[0] <= 16) {
276 // Should have used OP_1 .. OP_16.
277 return false;
278 }
279 if (data.size() == 1 && data[0] == 0x81) {
280 // Should have used OP_1NEGATE.
281 return false;
282 }
283 if (data.size() <= 75) {
284 // Must have used a direct push (opcode indicating number of bytes
285 // pushed + those bytes).
286 return opcode == data.size();
287 }
288 if (data.size() <= 255) {
289 // Must have used OP_PUSHDATA.
290 return opcode == OP_PUSHDATA1;
291 }
292 if (data.size() <= 65535) {
293 // Must have used OP_PUSHDATA2.
294 return opcode == OP_PUSHDATA2;
295 }
296 return true;
297}
298
299bool CScriptNum::IsMinimallyEncoded(const std::vector<uint8_t> &vch,
300 const size_t nMaxNumSize) {
301 if (vch.size() > nMaxNumSize) {
302 return false;
303 }
304
305 if (vch.size() > 0) {
306 // Check that the number is encoded with the minimum possible number
307 // of bytes.
308 //
309 // If the most-significant-byte - excluding the sign bit - is zero
310 // then we're not minimal. Note how this test also rejects the
311 // negative-zero encoding, 0x80.
312 if ((vch.back() & 0x7f) == 0) {
313 // One exception: if there's more than one byte and the most
314 // significant bit of the second-most-significant-byte is set it
315 // would conflict with the sign bit. An example of this case is
316 // +-255, which encode to 0xff00 and 0xff80 respectively.
317 // (big-endian).
318 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
319 return false;
320 }
321 }
322 }
323
324 return true;
325}
326
327bool CScriptNum::MinimallyEncode(std::vector<uint8_t> &data) {
328 if (data.size() == 0) {
329 return false;
330 }
331
332 // If the last byte is not 0x00 or 0x80, we are minimally encoded.
333 uint8_t last = data.back();
334 if (last & 0x7f) {
335 return false;
336 }
337
338 // If the script is one byte long, then we have a zero, which encodes as an
339 // empty array.
340 if (data.size() == 1) {
341 data = {};
342 return true;
343 }
344
345 // If the next byte has it sign bit set, then we are minimaly encoded.
346 if (data[data.size() - 2] & 0x80) {
347 return false;
348 }
349
350 // We are not minimally encoded, we need to figure out how much to trim.
351 for (size_t i = data.size() - 1; i > 0; i--) {
352 // We found a non zero byte, time to encode.
353 if (data[i - 1] != 0) {
354 if (data[i - 1] & 0x80) {
355 // We found a byte with it sign bit set so we need one more
356 // byte.
357 data[i++] = last;
358 } else {
359 // the sign bit is clear, we can use it.
360 data[i - 1] |= last;
361 }
362
363 data.resize(i);
364 return true;
365 }
366 }
367
368 // If we the whole thing is zeros, then we have a zero.
369 data = {};
370 return true;
371}
372
374 // Extra-fast test for pay-to-script-hash CScripts:
375 return (this->size() == 23 && (*this)[0] == OP_HASH160 &&
376 (*this)[1] == 0x14 && (*this)[22] == OP_EQUAL);
377}
378
379// A witness program is any valid CScript that consists of a 1-byte push opcode
380// followed by a data push between 2 and 40 bytes.
382 std::vector<uint8_t> &program) const {
383 if (this->size() < 4 || this->size() > 42) {
384 return false;
385 }
386 if ((*this)[0] != OP_0 && ((*this)[0] < OP_1 || (*this)[0] > OP_16)) {
387 return false;
388 }
389 if (size_t((*this)[1] + 2) == this->size()) {
390 version = DecodeOP_N((opcodetype)(*this)[0]);
391 program = std::vector<uint8_t>(this->begin() + 2, this->end());
392 return true;
393 }
394 return false;
395}
396
397// Wrapper returning only the predicate
399 int version;
400 std::vector<uint8_t> program;
401 return IsWitnessProgram(version, program);
402}
403
405 while (pc < end()) {
406 opcodetype opcode;
407 if (!GetOp(pc, opcode)) {
408 return false;
409 }
410
411 // Note that IsPushOnly() *does* consider OP_RESERVED to be a push-type
412 // opcode, however execution of OP_RESERVED fails, so it's not relevant
413 // to P2SH/BIP62 as the scriptSig would fail prior to the P2SH special
414 // validation code being executed.
415 if (opcode > OP_16) {
416 return false;
417 }
418 }
419 return true;
420}
421
423 return this->IsPushOnly(begin());
424}
425
428 std::vector<uint8_t> *pvchRet) {
430 if (pvchRet) {
431 pvchRet->clear();
432 }
433 if (pc >= end) {
434 return false;
435 }
436
437 // Read instruction
438 if (end - pc < 1) {
439 return false;
440 }
441
442 uint32_t opcode = *pc++;
443
444 // Immediate operand
445 if (opcode <= OP_PUSHDATA4) {
446 uint32_t nSize = 0;
447 if (opcode < OP_PUSHDATA1) {
448 nSize = opcode;
449 } else if (opcode == OP_PUSHDATA1) {
450 if (end - pc < 1) {
451 return false;
452 }
453 nSize = *pc++;
454 } else if (opcode == OP_PUSHDATA2) {
455 if (end - pc < 2) {
456 return false;
457 }
458 nSize = ReadLE16(&pc[0]);
459 pc += 2;
460 } else if (opcode == OP_PUSHDATA4) {
461 if (end - pc < 4) {
462 return false;
463 }
464 nSize = ReadLE32(&pc[0]);
465 pc += 4;
466 }
467 if (end - pc < 0 || uint32_t(end - pc) < nSize) {
468 return false;
469 }
470 if (pvchRet) {
471 pvchRet->assign(pc, pc + nSize);
472 }
473 pc += nSize;
474 }
475
476 opcodeRet = static_cast<opcodetype>(opcode);
477 return true;
478}
479
482 while (it < end()) {
483 opcodetype opcode;
484 std::vector<uint8_t> item;
485 if (!GetOp(it, opcode, item) || opcode > MAX_OPCODE ||
486 item.size() > MAX_SCRIPT_ELEMENT_SIZE) {
487 return false;
488 }
489 }
490 return true;
491}
bool IsPayToScriptHash() const
Definition script.cpp:373
static int DecodeOP_N(opcodetype opcode)
Encode/decode small integers:
Definition script.h:505
bool IsPushOnly() const
Definition script.cpp:422
bool IsWitnessProgram() const
Definition script.cpp:398
bool HasValidOps() const
Check if the script contains valid OP_CODES.
Definition script.cpp:480
bool GetOp(const_iterator &pc, opcodetype &opcodeRet, std::vector< uint8_t > &vchRet) const
Definition script.h:495
static bool MinimallyEncode(std::vector< uint8_t > &data)
Definition script.cpp:327
static bool IsMinimallyEncoded(const std::vector< uint8_t > &vch, const size_t nMaxNumSize=CScriptNum::MAXIMUM_ELEMENT_SIZE)
Definition script.cpp:299
size_type size() const
Definition prevector.h:394
iterator begin()
Definition prevector.h:398
iterator end()
Definition prevector.h:400
static uint16_t ReadLE16(const uint8_t *ptr)
Definition common.h:17
static uint32_t ReadLE32(const uint8_t *ptr)
Definition common.h:23
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
std::string GetOpName(opcodetype opcode)
Definition script.cpp:14
bool CheckMinimalPush(const std::vector< uint8_t > &data, opcodetype opcode)
Check whether the given stack element data would be minimally pushed using the given opcode.
Definition script.cpp:268
bool GetScriptOp(CScriptBase::const_iterator &pc, CScriptBase::const_iterator end, opcodetype &opcodeRet, std::vector< uint8_t > *pvchRet)
Definition script.cpp:426
static const unsigned int MAX_OPCODE
Definition script.h:200
static const unsigned int MAX_SCRIPT_ELEMENT_SIZE
Definition script.h:24
opcodetype
Script opcodes.
Definition script.h:47
@ OP_NUMNOTEQUAL
Definition script.h:146
@ OP_RESERVED1
Definition script.h:121
@ OP_SPLIT
Definition script.h:109
@ OP_2
Definition script.h:58
@ OP_SHA256
Definition script.h:159
@ OP_PUSHDATA4
Definition script.h:53
@ OP_NOP5
Definition script.h:175
@ OP_CHECKDATASIG
Definition script.h:183
@ OP_BOOLAND
Definition script.h:142
@ OP_CHECKMULTISIG
Definition script.h:165
@ OP_NEGATE
Definition script.h:129
@ OP_IF
Definition script.h:77
@ OP_13
Definition script.h:69
@ OP_ROT
Definition script.h:103
@ OP_SWAP
Definition script.h:104
@ OP_1NEGATE
Definition script.h:54
@ OP_VERNOTIF
Definition script.h:80
@ OP_CHECKSIG
Definition script.h:163
@ OP_CHECKLOCKTIMEVERIFY
Definition script.h:170
@ OP_LESSTHAN
Definition script.h:147
@ OP_16
Definition script.h:72
@ OP_14
Definition script.h:70
@ OP_CHECKDATASIGVERIFY
Definition script.h:184
@ OP_NOP10
Definition script.h:180
@ OP_2DIV
Definition script.h:128
@ OP_INVALIDOPCODE
Definition script.h:196
@ OP_NOT
Definition script.h:131
@ OP_EQUAL
Definition script.h:119
@ OP_NUMEQUAL
Definition script.h:144
@ OP_MOD
Definition script.h:138
@ OP_NOTIF
Definition script.h:78
@ OP_4
Definition script.h:60
@ OP_10
Definition script.h:66
@ OP_SIZE
Definition script.h:112
@ OP_3DUP
Definition script.h:91
@ OP_ENDIF
Definition script.h:82
@ OP_NOP1
Definition script.h:169
@ OP_DUP
Definition script.h:98
@ OP_GREATERTHAN
Definition script.h:148
@ OP_NOP
Definition script.h:75
@ OP_NUM2BIN
Definition script.h:110
@ OP_VERIF
Definition script.h:79
@ OP_TOALTSTACK
Definition script.h:87
@ OP_CODESEPARATOR
Definition script.h:162
@ OP_RIPEMD160
Definition script.h:157
@ OP_MIN
Definition script.h:151
@ OP_HASH256
Definition script.h:161
@ OP_MAX
Definition script.h:152
@ OP_1SUB
Definition script.h:126
@ OP_FROMALTSTACK
Definition script.h:88
@ OP_SUB
Definition script.h:135
@ OP_NUMEQUALVERIFY
Definition script.h:145
@ OP_OVER
Definition script.h:100
@ OP_NOP8
Definition script.h:178
@ OP_DIV
Definition script.h:137
@ OP_HASH160
Definition script.h:160
@ OP_2DUP
Definition script.h:90
@ OP_NIP
Definition script.h:99
@ OP_2MUL
Definition script.h:127
@ OP_NOP4
Definition script.h:174
@ OP_1
Definition script.h:56
@ OP_LESSTHANOREQUAL
Definition script.h:149
@ OP_2DROP
Definition script.h:89
@ OP_DEPTH
Definition script.h:96
@ OP_NOP9
Definition script.h:179
@ OP_BIN2NUM
Definition script.h:111
@ OP_VER
Definition script.h:76
@ OP_VERIFY
Definition script.h:83
@ OP_RESERVED2
Definition script.h:122
@ OP_12
Definition script.h:68
@ OP_ADD
Definition script.h:134
@ OP_CHECKMULTISIGVERIFY
Definition script.h:166
@ OP_NOP7
Definition script.h:177
@ OP_8
Definition script.h:64
@ OP_BOOLOR
Definition script.h:143
@ OP_XOR
Definition script.h:118
@ OP_DROP
Definition script.h:97
@ OP_MUL
Definition script.h:136
@ OP_WITHIN
Definition script.h:154
@ OP_ELSE
Definition script.h:81
@ OP_15
Definition script.h:71
@ OP_CHECKSIGVERIFY
Definition script.h:164
@ OP_PUSHDATA1
Definition script.h:51
@ OP_TUCK
Definition script.h:105
@ OP_2OVER
Definition script.h:92
@ OP_0NOTEQUAL
Definition script.h:132
@ OP_9
Definition script.h:65
@ OP_3
Definition script.h:59
@ OP_11
Definition script.h:67
@ OP_SHA1
Definition script.h:158
@ OP_GREATERTHANOREQUAL
Definition script.h:150
@ OP_RSHIFT
Definition script.h:140
@ OP_2SWAP
Definition script.h:94
@ OP_PUSHDATA2
Definition script.h:52
@ OP_2ROT
Definition script.h:93
@ OP_6
Definition script.h:62
@ OP_INVERT
Definition script.h:115
@ OP_0
Definition script.h:49
@ OP_ABS
Definition script.h:130
@ OP_LSHIFT
Definition script.h:139
@ OP_RETURN
Definition script.h:84
@ OP_IFDUP
Definition script.h:95
@ OP_PICK
Definition script.h:101
@ OP_AND
Definition script.h:116
@ OP_EQUALVERIFY
Definition script.h:120
@ OP_CAT
Definition script.h:108
@ OP_REVERSEBYTES
Definition script.h:187
@ OP_RESERVED
Definition script.h:55
@ OP_1ADD
Definition script.h:125
@ OP_7
Definition script.h:63
@ OP_OR
Definition script.h:117
@ OP_ROLL
Definition script.h:102
@ OP_NOP6
Definition script.h:176
@ OP_5
Definition script.h:61
@ OP_CHECKSEQUENCEVERIFY
Definition script.h:172
assert(!tx.IsCoinBase())