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