Bitcoin Core  0.18.99
P2P Digital Currency
interpreter.h
Go to the documentation of this file.
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_INTERPRETER_H
7 #define BITCOIN_SCRIPT_INTERPRETER_H
8 
9 #include <script/script_error.h>
10 #include <primitives/transaction.h>
11 
12 #include <vector>
13 #include <stdint.h>
14 #include <string>
15 
16 class CPubKey;
17 class CScript;
18 class CTransaction;
19 class uint256;
20 
22 enum
23 {
28 };
29 
35 enum
36 {
38 
39  // Evaluate P2SH subscripts (BIP16).
40  SCRIPT_VERIFY_P2SH = (1U << 0),
41 
42  // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
43  // Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
44  // (not used or intended as a consensus rule).
46 
47  // Passing a non-strict-DER signature to a checksig operation causes script failure (BIP62 rule 1)
48  SCRIPT_VERIFY_DERSIG = (1U << 2),
49 
50  // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
51  // (BIP62 rule 5).
52  SCRIPT_VERIFY_LOW_S = (1U << 3),
53 
54  // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (BIP62 rule 7).
56 
57  // Using a non-push operator in the scriptSig causes script failure (BIP62 rule 2).
59 
60  // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
61  // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
62  // any other push causes the script to fail (BIP62 rule 3).
63  // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
65 
66  // Discourage use of NOPs reserved for upgrades (NOP1-10)
67  //
68  // Provided so that nodes can avoid accepting or mining transactions
69  // containing executed NOP's whose meaning may change after a soft-fork,
70  // thus rendering the script invalid; with this flag set executing
71  // discouraged NOPs fails the script. This verification flag will never be
72  // a mandatory flag applied to scripts in a block. NOPs that are not
73  // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
74  // NOPs that have associated forks to give them new meaning (CLTV, CSV)
75  // are not subject to this rule.
77 
78  // Require that only a single stack element remains after evaluation. This changes the success criterion from
79  // "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
80  // "Exactly one stack element must remain, and when interpreted as a boolean, it must be true".
81  // (BIP62 rule 6)
82  // Note: CLEANSTACK should never be used without P2SH or WITNESS.
84 
85  // Verify CHECKLOCKTIMEVERIFY
86  //
87  // See BIP65 for details.
89 
90  // support CHECKSEQUENCEVERIFY opcode
91  //
92  // See BIP112 for details
94 
95  // Support segregated witness
96  //
97  SCRIPT_VERIFY_WITNESS = (1U << 11),
98 
99  // Making v1-v16 witness program non-standard
100  //
102 
103  // Segwit script only: Require the argument of OP_IF/NOTIF to be exactly 0x01 or empty vector
104  //
106 
107  // Signature(s) must be empty vector if a CHECK(MULTI)SIG operation failed
108  //
110 
111  // Public keys in segregated witness scripts must be compressed
112  //
114 
115  // Making OP_CODESEPARATOR and FindAndDelete fail any non-segwit scripts
116  //
118 };
119 
120 bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
121 
123 {
125  bool ready = false;
126 
127  template <class T>
128  explicit PrecomputedTransactionData(const T& tx);
129 };
130 
131 enum class SigVersion
132 {
133  BASE = 0,
134  WITNESS_V0 = 1,
135 };
136 
138 static constexpr size_t WITNESS_V0_SCRIPTHASH_SIZE = 32;
139 static constexpr size_t WITNESS_V0_KEYHASH_SIZE = 20;
140 
141 template <class T>
142 uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache = nullptr);
143 
145 {
146 public:
147  virtual bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
148  {
149  return false;
150  }
151 
152  virtual bool CheckLockTime(const CScriptNum& nLockTime) const
153  {
154  return false;
155  }
156 
157  virtual bool CheckSequence(const CScriptNum& nSequence) const
158  {
159  return false;
160  }
161 
163 };
164 
165 template <class T>
167 {
168 private:
169  const T* txTo;
170  unsigned int nIn;
173 
174 protected:
175  virtual bool VerifySignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
176 
177 public:
178  GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
179  GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn) : txTo(txToIn), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
180  bool CheckSig(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override;
181  bool CheckLockTime(const CScriptNum& nLockTime) const override;
182  bool CheckSequence(const CScriptNum& nSequence) const override;
183 };
184 
187 
188 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* error = nullptr);
189 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror = nullptr);
190 
191 size_t CountWitnessSigOps(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags);
192 
193 int FindAndDelete(CScript& script, const CScript& b);
194 
195 #endif // BITCOIN_SCRIPT_INTERPRETER_H
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
Definition: interpreter.h:152
virtual ~BaseSignatureChecker()
Definition: interpreter.h:162
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror=nullptr)
enum ScriptError_t ScriptError
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const CAmount &amountIn, const PrecomputedTransactionData &txdataIn)
Definition: interpreter.h:179
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache=nullptr)
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const CAmount &amountIn)
Definition: interpreter.h:178
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *error=nullptr)
PrecomputedTransactionData(const T &tx)
const PrecomputedTransactionData * txdata
Definition: interpreter.h:172
An encapsulated public key.
Definition: pubkey.h:30
static constexpr size_t WITNESS_V0_SCRIPTHASH_SIZE
Signature hash sizes.
Definition: interpreter.h:138
int flags
Definition: bitcoin-tx.cpp:507
256-bit opaque blob.
Definition: uint256.h:121
size_t CountWitnessSigOps(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags)
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
static constexpr size_t WITNESS_V0_KEYHASH_SIZE
Definition: interpreter.h:139
virtual bool CheckSig(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Definition: interpreter.h:147
virtual bool CheckSequence(const CScriptNum &nSequence) const
Definition: interpreter.h:157
int FindAndDelete(CScript &script, const CScript &b)
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:270
bool error(const char *fmt, const Args &... args)
Definition: system.h:59
SigVersion
Definition: interpreter.h:131