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