1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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 #ifndef BITCOIN_SCRIPT_SCRIPT_H
7 #define BITCOIN_SCRIPT_SCRIPT_H
8 
9 #include <crypto/common.h>
10 #include <prevector.h>
11 #include <serialize.h>
12 
13 #include <assert.h>
14 #include <climits>
15 #include <limits>
16 #include <stdexcept>
17 #include <stdint.h>
18 #include <string.h>
19 #include <string>
20 #include <vector>
21 
22 // Maximum number of bytes pushable to the stack
23 static const unsigned int MAX_SCRIPT_ELEMENT_SIZE = 520;
24 
25 // Maximum number of non-push operations per script
26 static const int MAX_OPS_PER_SCRIPT = 201;
27 
28 // Maximum number of public keys per multisig
29 static const int MAX_PUBKEYS_PER_MULTISIG = 20;
30 
31 // Maximum script length in bytes
32 static const int MAX_SCRIPT_SIZE = 10000;
33 
34 // Maximum number of values on script interpreter stack
35 static const int MAX_STACK_SIZE = 1000;
36 
37 // Threshold for nLockTime: below this value it is interpreted as block number,
38 // otherwise as UNIX timestamp.
39 static const unsigned int LOCKTIME_THRESHOLD = 500000000; // Tue Nov  5 00:53:20 1985 UTC
40 
41 // Maximum nLockTime. Since a lock time indicates the last invalid timestamp, a
42 // transaction with this lock time will never be valid unless lock time
43 // checking is disabled (by setting all input sequence numbers to
44 // SEQUENCE_FINAL).
45 static const uint32_t LOCKTIME_MAX = 0xFFFFFFFFU;
46 
47 template <typename T>
ToByteVector(const T & in)48 std::vector<unsigned char> ToByteVector(const T& in)
49 {
50     return std::vector<unsigned char>(in.begin(), in.end());
51 }
52 
53 /** Script opcodes */
54 enum opcodetype
55 {
56     // push value
57     OP_0 = 0x00,
58     OP_FALSE = OP_0,
59     OP_PUSHDATA1 = 0x4c,
60     OP_PUSHDATA2 = 0x4d,
61     OP_PUSHDATA4 = 0x4e,
62     OP_1NEGATE = 0x4f,
63     OP_RESERVED = 0x50,
64     OP_1 = 0x51,
65     OP_TRUE=OP_1,
66     OP_2 = 0x52,
67     OP_3 = 0x53,
68     OP_4 = 0x54,
69     OP_5 = 0x55,
70     OP_6 = 0x56,
71     OP_7 = 0x57,
72     OP_8 = 0x58,
73     OP_9 = 0x59,
74     OP_10 = 0x5a,
75     OP_11 = 0x5b,
76     OP_12 = 0x5c,
77     OP_13 = 0x5d,
78     OP_14 = 0x5e,
79     OP_15 = 0x5f,
80     OP_16 = 0x60,
81 
82     // control
83     OP_NOP = 0x61,
84     OP_VER = 0x62,
85     OP_IF = 0x63,
86     OP_NOTIF = 0x64,
87     OP_VERIF = 0x65,
88     OP_VERNOTIF = 0x66,
89     OP_ELSE = 0x67,
90     OP_ENDIF = 0x68,
91     OP_VERIFY = 0x69,
92     OP_RETURN = 0x6a,
93 
94     // stack ops
95     OP_TOALTSTACK = 0x6b,
96     OP_FROMALTSTACK = 0x6c,
97     OP_2DROP = 0x6d,
98     OP_2DUP = 0x6e,
99     OP_3DUP = 0x6f,
100     OP_2OVER = 0x70,
101     OP_2ROT = 0x71,
102     OP_2SWAP = 0x72,
103     OP_IFDUP = 0x73,
104     OP_DEPTH = 0x74,
105     OP_DROP = 0x75,
106     OP_DUP = 0x76,
107     OP_NIP = 0x77,
108     OP_OVER = 0x78,
109     OP_PICK = 0x79,
110     OP_ROLL = 0x7a,
111     OP_ROT = 0x7b,
112     OP_SWAP = 0x7c,
113     OP_TUCK = 0x7d,
114 
115     // splice ops
116     OP_CAT = 0x7e,
117     OP_SUBSTR = 0x7f,
118     OP_LEFT = 0x80,
119     OP_RIGHT = 0x81,
120     OP_SIZE = 0x82,
121 
122     // bit logic
123     OP_INVERT = 0x83,
124     OP_AND = 0x84,
125     OP_OR = 0x85,
126     OP_XOR = 0x86,
127     OP_EQUAL = 0x87,
128     OP_EQUALVERIFY = 0x88,
129     OP_RESERVED1 = 0x89,
130     OP_RESERVED2 = 0x8a,
131 
132     // numeric
133     OP_1ADD = 0x8b,
134     OP_1SUB = 0x8c,
135     OP_2MUL = 0x8d,
136     OP_2DIV = 0x8e,
137     OP_NEGATE = 0x8f,
138     OP_ABS = 0x90,
139     OP_NOT = 0x91,
140     OP_0NOTEQUAL = 0x92,
141 
142     OP_ADD = 0x93,
143     OP_SUB = 0x94,
144     OP_MUL = 0x95,
145     OP_DIV = 0x96,
146     OP_MOD = 0x97,
147     OP_LSHIFT = 0x98,
148     OP_RSHIFT = 0x99,
149 
150     OP_BOOLAND = 0x9a,
151     OP_BOOLOR = 0x9b,
152     OP_NUMEQUAL = 0x9c,
153     OP_NUMEQUALVERIFY = 0x9d,
154     OP_NUMNOTEQUAL = 0x9e,
155     OP_LESSTHAN = 0x9f,
156     OP_GREATERTHAN = 0xa0,
157     OP_LESSTHANOREQUAL = 0xa1,
158     OP_GREATERTHANOREQUAL = 0xa2,
159     OP_MIN = 0xa3,
160     OP_MAX = 0xa4,
161 
162     OP_WITHIN = 0xa5,
163 
164     // crypto
165     OP_RIPEMD160 = 0xa6,
166     OP_SHA1 = 0xa7,
167     OP_SHA256 = 0xa8,
168     OP_HASH160 = 0xa9,
169     OP_HASH256 = 0xaa,
170     OP_CODESEPARATOR = 0xab,
171     OP_CHECKSIG = 0xac,
172     OP_CHECKSIGVERIFY = 0xad,
173     OP_CHECKMULTISIG = 0xae,
174     OP_CHECKMULTISIGVERIFY = 0xaf,
175 
176     // expansion
177     OP_NOP1 = 0xb0,
178     OP_CHECKLOCKTIMEVERIFY = 0xb1,
179     OP_NOP2 = OP_CHECKLOCKTIMEVERIFY,
180     OP_CHECKSEQUENCEVERIFY = 0xb2,
181     OP_NOP3 = OP_CHECKSEQUENCEVERIFY,
182     OP_NOP4 = 0xb3,
183     OP_NOP5 = 0xb4,
184     OP_NOP6 = 0xb5,
185     OP_NOP7 = 0xb6,
186     OP_NOP8 = 0xb7,
187     OP_NOP9 = 0xb8,
188     OP_NOP10 = 0xb9,
189 
190     OP_INVALIDOPCODE = 0xff,
191 };
192 
193 // Maximum value that an opcode can be
194 static const unsigned int MAX_OPCODE = OP_NOP10;
195 
196 const char* GetOpName(opcodetype opcode);
197 
198 class scriptnum_error : public std::runtime_error
199 {
200 public:
scriptnum_error(const std::string & str)201     explicit scriptnum_error(const std::string& str) : std::runtime_error(str) {}
202 };
203 
204 class CScriptNum
205 {
206 /**
207  * Numeric opcodes (OP_1ADD, etc) are restricted to operating on 4-byte integers.
208  * The semantics are subtle, though: operands must be in the range [-2^31 +1...2^31 -1],
209  * but results may overflow (and are valid as long as they are not used in a subsequent
210  * numeric operation). CScriptNum enforces those semantics by storing results as
211  * an int64 and allowing out-of-range values to be returned as a vector of bytes but
212  * throwing an exception if arithmetic is done or the result is interpreted as an integer.
213  */
214 public:
215 
CScriptNum(const int64_t & n)216     explicit CScriptNum(const int64_t& n)
217     {
218         m_value = n;
219     }
220 
221     static const size_t nDefaultMaxNumSize = 4;
222 
223     explicit CScriptNum(const std::vector<unsigned char>& vch, bool fRequireMinimal,
224                         const size_t nMaxNumSize = nDefaultMaxNumSize)
225     {
226         if (vch.size() > nMaxNumSize) {
227             throw scriptnum_error("script number overflow");
228         }
229         if (fRequireMinimal && vch.size() > 0) {
230             // Check that the number is encoded with the minimum possible
231             // number of bytes.
232             //
233             // If the most-significant-byte - excluding the sign bit - is zero
234             // then we're not minimal. Note how this test also rejects the
235             // negative-zero encoding, 0x80.
236             if ((vch.back() & 0x7f) == 0) {
237                 // One exception: if there's more than one byte and the most
238                 // significant bit of the second-most-significant-byte is set
239                 // it would conflict with the sign bit. An example of this case
240                 // is +-255, which encode to 0xff00 and 0xff80 respectively.
241                 // (big-endian).
242                 if (vch.size() <= 1 || (vch[vch.size() - 2] & 0x80) == 0) {
243                     throw scriptnum_error("non-minimally encoded script number");
244                 }
245             }
246         }
247         m_value = set_vch(vch);
248     }
249 
250     inline bool operator==(const int64_t& rhs) const    { return m_value == rhs; }
251     inline bool operator!=(const int64_t& rhs) const    { return m_value != rhs; }
252     inline bool operator<=(const int64_t& rhs) const    { return m_value <= rhs; }
253     inline bool operator< (const int64_t& rhs) const    { return m_value <  rhs; }
254     inline bool operator>=(const int64_t& rhs) const    { return m_value >= rhs; }
255     inline bool operator> (const int64_t& rhs) const    { return m_value >  rhs; }
256 
257     inline bool operator==(const CScriptNum& rhs) const { return operator==(rhs.m_value); }
258     inline bool operator!=(const CScriptNum& rhs) const { return operator!=(rhs.m_value); }
259     inline bool operator<=(const CScriptNum& rhs) const { return operator<=(rhs.m_value); }
260     inline bool operator< (const CScriptNum& rhs) const { return operator< (rhs.m_value); }
261     inline bool operator>=(const CScriptNum& rhs) const { return operator>=(rhs.m_value); }
262     inline bool operator> (const CScriptNum& rhs) const { return operator> (rhs.m_value); }
263 
264     inline CScriptNum operator+(   const int64_t& rhs)    const { return CScriptNum(m_value + rhs);}
265     inline CScriptNum operator-(   const int64_t& rhs)    const { return CScriptNum(m_value - rhs);}
266     inline CScriptNum operator+(   const CScriptNum& rhs) const { return operator+(rhs.m_value);   }
267     inline CScriptNum operator-(   const CScriptNum& rhs) const { return operator-(rhs.m_value);   }
268 
269     inline CScriptNum& operator+=( const CScriptNum& rhs)       { return operator+=(rhs.m_value);  }
270     inline CScriptNum& operator-=( const CScriptNum& rhs)       { return operator-=(rhs.m_value);  }
271 
272     inline CScriptNum operator&(   const int64_t& rhs)    const { return CScriptNum(m_value & rhs);}
273     inline CScriptNum operator&(   const CScriptNum& rhs) const { return operator&(rhs.m_value);   }
274 
275     inline CScriptNum& operator&=( const CScriptNum& rhs)       { return operator&=(rhs.m_value);  }
276 
277     inline CScriptNum operator-()                         const
278     {
279         assert(m_value != std::numeric_limits<int64_t>::min());
280         return CScriptNum(-m_value);
281     }
282 
283     inline CScriptNum& operator=( const int64_t& rhs)
284     {
285         m_value = rhs;
286         return *this;
287     }
288 
289     inline CScriptNum& operator+=( const int64_t& rhs)
290     {
291         assert(rhs == 0 || (rhs > 0 && m_value <= std::numeric_limits<int64_t>::max() - rhs) ||
292                            (rhs < 0 && m_value >= std::numeric_limits<int64_t>::min() - rhs));
293         m_value += rhs;
294         return *this;
295     }
296 
297     inline CScriptNum& operator-=( const int64_t& rhs)
298     {
299         assert(rhs == 0 || (rhs > 0 && m_value >= std::numeric_limits<int64_t>::min() + rhs) ||
300                            (rhs < 0 && m_value <= std::numeric_limits<int64_t>::max() + rhs));
301         m_value -= rhs;
302         return *this;
303     }
304 
305     inline CScriptNum& operator&=( const int64_t& rhs)
306     {
307         m_value &= rhs;
308         return *this;
309     }
310 
getint()311     int getint() const
312     {
313         if (m_value > std::numeric_limits<int>::max())
314             return std::numeric_limits<int>::max();
315         else if (m_value < std::numeric_limits<int>::min())
316             return std::numeric_limits<int>::min();
317         return m_value;
318     }
319 
getvch()320     std::vector<unsigned char> getvch() const
321     {
322         return serialize(m_value);
323     }
324 
serialize(const int64_t & value)325     static std::vector<unsigned char> serialize(const int64_t& value)
326     {
327         if(value == 0)
328             return std::vector<unsigned char>();
329 
330         std::vector<unsigned char> result;
331         const bool neg = value < 0;
332         uint64_t absvalue = neg ? -value : value;
333 
334         while(absvalue)
335         {
336             result.push_back(absvalue & 0xff);
337             absvalue >>= 8;
338         }
339 
340 //    - If the most significant byte is >= 0x80 and the value is positive, push a
341 //    new zero-byte to make the significant byte < 0x80 again.
342 
343 //    - If the most significant byte is >= 0x80 and the value is negative, push a
344 //    new 0x80 byte that will be popped off when converting to an integral.
345 
346 //    - If the most significant byte is < 0x80 and the value is negative, add
347 //    0x80 to it, since it will be subtracted and interpreted as a negative when
348 //    converting to an integral.
349 
350         if (result.back() & 0x80)
351             result.push_back(neg ? 0x80 : 0);
352         else if (neg)
353             result.back() |= 0x80;
354 
355         return result;
356     }
357 
358 private:
set_vch(const std::vector<unsigned char> & vch)359     static int64_t set_vch(const std::vector<unsigned char>& vch)
360     {
361       if (vch.empty())
362           return 0;
363 
364       int64_t result = 0;
365       for (size_t i = 0; i != vch.size(); ++i)
366           result |= static_cast<int64_t>(vch[i]) << 8*i;
367 
368       // If the input vector's most significant byte is 0x80, remove it from
369       // the result's msb and return a negative.
370       if (vch.back() & 0x80)
371           return -((int64_t)(result & ~(0x80ULL << (8 * (vch.size() - 1)))));
372 
373       return result;
374     }
375 
376     int64_t m_value;
377 };
378 
379 /**
380  * We use a prevector for the script to reduce the considerable memory overhead
381  *  of vectors in cases where they normally contain a small number of small elements.
382  * Tests in October 2015 showed use of this reduced dbcache memory usage by 23%
383  *  and made an initial sync 13% faster.
384  */
385 typedef prevector<28, unsigned char> CScriptBase;
386 
387 bool GetScriptOp(CScriptBase::const_iterator& pc, CScriptBase::const_iterator end, opcodetype& opcodeRet, std::vector<unsigned char>* pvchRet);
388 
389 /** Serialized script, used inside transaction inputs and outputs */
390 class CScript : public CScriptBase
391 {
392 protected:
push_int64(int64_t n)393     CScript& push_int64(int64_t n)
394     {
395         if (n == -1 || (n >= 1 && n <= 16))
396         {
397             push_back(n + (OP_1 - 1));
398         }
399         else if (n == 0)
400         {
401             push_back(OP_0);
402         }
403         else
404         {
405             *this << CScriptNum::serialize(n);
406         }
407         return *this;
408     }
409 public:
CScript()410     CScript() { }
CScript(const_iterator pbegin,const_iterator pend)411     CScript(const_iterator pbegin, const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(std::vector<unsigned char>::const_iterator pbegin,std::vector<unsigned char>::const_iterator pend)412     CScript(std::vector<unsigned char>::const_iterator pbegin, std::vector<unsigned char>::const_iterator pend) : CScriptBase(pbegin, pend) { }
CScript(const unsigned char * pbegin,const unsigned char * pend)413     CScript(const unsigned char* pbegin, const unsigned char* pend) : CScriptBase(pbegin, pend) { }
414 
415     ADD_SERIALIZE_METHODS;
416 
417     template <typename Stream, typename Operation>
SerializationOp(Stream & s,Operation ser_action)418     inline void SerializationOp(Stream& s, Operation ser_action) {
419         READWRITEAS(CScriptBase, *this);
420     }
421 
422     CScript& operator+=(const CScript& b)
423     {
424         reserve(size() + b.size());
425         insert(end(), b.begin(), b.end());
426         return *this;
427     }
428 
429     friend CScript operator+(const CScript& a, const CScript& b)
430     {
431         CScript ret = a;
432         ret += b;
433         return ret;
434     }
435 
CScript(int64_t b)436     CScript(int64_t b)        { operator<<(b); }
437 
CScript(opcodetype b)438     explicit CScript(opcodetype b)     { operator<<(b); }
CScript(const CScriptNum & b)439     explicit CScript(const CScriptNum& b) { operator<<(b); }
CScript(const std::vector<unsigned char> & b)440     explicit CScript(const std::vector<unsigned char>& b) { operator<<(b); }
441 
442 
443     CScript& operator<<(int64_t b) { return push_int64(b); }
444 
445     CScript& operator<<(opcodetype opcode)
446     {
447         if (opcode < 0 || opcode > 0xff)
448             throw std::runtime_error("CScript::operator<<(): invalid opcode");
449         insert(end(), (unsigned char)opcode);
450         return *this;
451     }
452 
453     CScript& operator<<(const CScriptNum& b)
454     {
455         *this << b.getvch();
456         return *this;
457     }
458 
459     CScript& operator<<(const std::vector<unsigned char>& b)
460     {
461         if (b.size() < OP_PUSHDATA1)
462         {
463             insert(end(), (unsigned char)b.size());
464         }
465         else if (b.size() <= 0xff)
466         {
467             insert(end(), OP_PUSHDATA1);
468             insert(end(), (unsigned char)b.size());
469         }
470         else if (b.size() <= 0xffff)
471         {
472             insert(end(), OP_PUSHDATA2);
473             uint8_t _data[2];
474             WriteLE16(_data, b.size());
475             insert(end(), _data, _data + sizeof(_data));
476         }
477         else
478         {
479             insert(end(), OP_PUSHDATA4);
480             uint8_t _data[4];
481             WriteLE32(_data, b.size());
482             insert(end(), _data, _data + sizeof(_data));
483         }
484         insert(end(), b.begin(), b.end());
485         return *this;
486     }
487 
488     CScript& operator<<(const CScript& b)
489     {
490         // I'm not sure if this should push the script or concatenate scripts.
491         // If there's ever a use for pushing a script onto a script, delete this member fn
492         assert(!"Warning: Pushing a CScript onto a CScript with << is probably not intended, use + to concatenate!");
493         return *this;
494     }
495 
496 
GetOp(const_iterator & pc,opcodetype & opcodeRet,std::vector<unsigned char> & vchRet)497     bool GetOp(const_iterator& pc, opcodetype& opcodeRet, std::vector<unsigned char>& vchRet) const
498     {
499         return GetScriptOp(pc, end(), opcodeRet, &vchRet);
500     }
501 
GetOp(const_iterator & pc,opcodetype & opcodeRet)502     bool GetOp(const_iterator& pc, opcodetype& opcodeRet) const
503     {
504         return GetScriptOp(pc, end(), opcodeRet, nullptr);
505     }
506 
507 
508     /** Encode/decode small integers: */
DecodeOP_N(opcodetype opcode)509     static int DecodeOP_N(opcodetype opcode)
510     {
511         if (opcode == OP_0)
512             return 0;
513         assert(opcode >= OP_1 && opcode <= OP_16);
514         return (int)opcode - (int)(OP_1 - 1);
515     }
EncodeOP_N(int n)516     static opcodetype EncodeOP_N(int n)
517     {
518         assert(n >= 0 && n <= 16);
519         if (n == 0)
520             return OP_0;
521         return (opcodetype)(OP_1+n-1);
522     }
523 
524     /**
525      * Pre-version-0.6, Bitcoin always counted CHECKMULTISIGs
526      * as 20 sigops. With pay-to-script-hash, that changed:
527      * CHECKMULTISIGs serialized in scriptSigs are
528      * counted more accurately, assuming they are of the form
529      *  ... OP_N CHECKMULTISIG ...
530      */
531     unsigned int GetSigOpCount(bool fAccurate) const;
532 
533     /**
534      * Accurately count sigOps, including sigOps in
535      * pay-to-script-hash transactions:
536      */
537     unsigned int GetSigOpCount(const CScript& scriptSig) const;
538 
539     bool IsPayToScriptHash() const;
540     bool IsPayToWitnessScriptHash() const;
541     bool IsWitnessProgram(int& version, std::vector<unsigned char>& program) const;
542 
543     /** Called by IsStandardTx and P2SH/BIP62 VerifyScript (which makes it consensus-critical). */
544     bool IsPushOnly(const_iterator pc) const;
545     bool IsPushOnly() const;
546 
547     /** Check if the script contains valid OP_CODES */
548     bool HasValidOps() const;
549 
550     /**
551      * Returns whether the script is guaranteed to fail at execution,
552      * regardless of the initial stack. This allows outputs to be pruned
553      * instantly when entering the UTXO set.
554      */
IsUnspendable()555     bool IsUnspendable() const
556     {
557         return (size() > 0 && *begin() == OP_RETURN) || (size() > MAX_SCRIPT_SIZE);
558     }
559 
clear()560     void clear()
561     {
562         // The default prevector::clear() does not release memory
563         CScriptBase::clear();
564         shrink_to_fit();
565     }
566 };
567 
568 struct CScriptWitness
569 {
570     // Note that this encodes the data elements being pushed, rather than
571     // encoding them as a CScript that pushes them.
572     std::vector<std::vector<unsigned char> > stack;
573 
574     // Some compilers complain without a default constructor
CScriptWitnessCScriptWitness575     CScriptWitness() { }
576 
IsNullCScriptWitness577     bool IsNull() const { return stack.empty(); }
578 
SetNullCScriptWitness579     void SetNull() { stack.clear(); stack.shrink_to_fit(); }
580 
581     std::string ToString() const;
582 };
583 
584 class CReserveScript
585 {
586 public:
587     CScript reserveScript;
KeepScript()588     virtual void KeepScript() {}
CReserveScript()589     CReserveScript() {}
~CReserveScript()590     virtual ~CReserveScript() {}
591 };
592 
593 #endif // BITCOIN_SCRIPT_SCRIPT_H
594