Bitcoin Core  22.0.0
P2P Digital Currency
interpreter.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2020 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 <hash.h>
10 #include <script/script_error.h>
11 #include <span.h>
12 #include <primitives/transaction.h>
13 
14 #include <vector>
15 #include <stdint.h>
16 
17 class CPubKey;
18 class XOnlyPubKey;
19 class CScript;
20 class CTransaction;
21 class CTxOut;
22 class uint256;
23 
25 enum
26 {
31 
35 };
36 
42 enum
43 {
45 
46  // Evaluate P2SH subscripts (BIP16).
47  SCRIPT_VERIFY_P2SH = (1U << 0),
48 
49  // Passing a non-strict-DER signature or one with undefined hashtype to a checksig operation causes script failure.
50  // Evaluating a pubkey that is not (0x04 + 64 bytes) or (0x02 or 0x03 + 32 bytes) by checksig causes script failure.
51  // (not used or intended as a consensus rule).
53 
54  // Passing a non-strict-DER signature to a checksig operation causes script failure (BIP62 rule 1)
55  SCRIPT_VERIFY_DERSIG = (1U << 2),
56 
57  // Passing a non-strict-DER signature or one with S > order/2 to a checksig operation causes script failure
58  // (BIP62 rule 5).
59  SCRIPT_VERIFY_LOW_S = (1U << 3),
60 
61  // verify dummy stack item consumed by CHECKMULTISIG is of zero-length (BIP62 rule 7).
63 
64  // Using a non-push operator in the scriptSig causes script failure (BIP62 rule 2).
66 
67  // Require minimal encodings for all push operations (OP_0... OP_16, OP_1NEGATE where possible, direct
68  // pushes up to 75 bytes, OP_PUSHDATA up to 255 bytes, OP_PUSHDATA2 for anything larger). Evaluating
69  // any other push causes the script to fail (BIP62 rule 3).
70  // In addition, whenever a stack element is interpreted as a number, it must be of minimal length (BIP62 rule 4).
72 
73  // Discourage use of NOPs reserved for upgrades (NOP1-10)
74  //
75  // Provided so that nodes can avoid accepting or mining transactions
76  // containing executed NOP's whose meaning may change after a soft-fork,
77  // thus rendering the script invalid; with this flag set executing
78  // discouraged NOPs fails the script. This verification flag will never be
79  // a mandatory flag applied to scripts in a block. NOPs that are not
80  // executed, e.g. within an unexecuted IF ENDIF block, are *not* rejected.
81  // NOPs that have associated forks to give them new meaning (CLTV, CSV)
82  // are not subject to this rule.
84 
85  // Require that only a single stack element remains after evaluation. This changes the success criterion from
86  // "At least one stack element must remain, and when interpreted as a boolean, it must be true" to
87  // "Exactly one stack element must remain, and when interpreted as a boolean, it must be true".
88  // (BIP62 rule 6)
89  // Note: CLEANSTACK should never be used without P2SH or WITNESS.
90  // Note: WITNESS_V0 and TAPSCRIPT script execution have behavior similar to CLEANSTACK as part of their
91  // consensus rules. It is automatic there and does not need this flag.
93 
94  // Verify CHECKLOCKTIMEVERIFY
95  //
96  // See BIP65 for details.
98 
99  // support CHECKSEQUENCEVERIFY opcode
100  //
101  // See BIP112 for details
103 
104  // Support segregated witness
105  //
106  SCRIPT_VERIFY_WITNESS = (1U << 11),
107 
108  // Making v1-v16 witness program non-standard
109  //
111 
112  // Segwit script only: Require the argument of OP_IF/NOTIF to be exactly 0x01 or empty vector
113  //
114  // Note: TAPSCRIPT script execution has behavior similar to MINIMALIF as part of its consensus
115  // rules. It is automatic there and does not depend on this flag.
117 
118  // Signature(s) must be empty vector if a CHECK(MULTI)SIG operation failed
119  //
121 
122  // Public keys in segregated witness scripts must be compressed
123  //
125 
126  // Making OP_CODESEPARATOR and FindAndDelete fail any non-segwit scripts
127  //
129 
130  // Taproot/Tapscript validation (BIPs 341 & 342)
131  //
132  SCRIPT_VERIFY_TAPROOT = (1U << 17),
133 
134  // Making unknown Taproot leaf versions non-standard
135  //
137 
138  // Making unknown OP_SUCCESS non-standard
140 
141  // Making unknown public key versions (in BIP 342 scripts) non-standard
143 };
144 
145 bool CheckSignatureEncoding(const std::vector<unsigned char> &vchSig, unsigned int flags, ScriptError* serror);
146 
148 {
149  // BIP341 precomputed data.
150  // These are single-SHA256, see https://github.com/bitcoin/bips/blob/master/bip-0341.mediawiki#cite_note-15.
158 
159  // BIP143 precomputed data (double-SHA256).
162  bool m_bip143_segwit_ready = false;
163 
164  std::vector<CTxOut> m_spent_outputs;
166  bool m_spent_outputs_ready = false;
167 
168  PrecomputedTransactionData() = default;
169 
170  template <class T>
171  void Init(const T& tx, std::vector<CTxOut>&& spent_outputs, bool force = false);
172 
173  template <class T>
174  explicit PrecomputedTransactionData(const T& tx);
175 };
176 
177 enum class SigVersion
178 {
179  BASE = 0,
180  WITNESS_V0 = 1,
181  TAPROOT = 2,
182  TAPSCRIPT = 3,
183 };
184 
186 {
188  bool m_tapleaf_hash_init = false;
191 
196 
198  bool m_annex_init = false;
203 
208 };
209 
211 static constexpr size_t WITNESS_V0_SCRIPTHASH_SIZE = 32;
212 static constexpr size_t WITNESS_V0_KEYHASH_SIZE = 20;
213 static constexpr size_t WITNESS_V1_TAPROOT_SIZE = 32;
214 
215 static constexpr uint8_t TAPROOT_LEAF_MASK = 0xfe;
216 static constexpr uint8_t TAPROOT_LEAF_TAPSCRIPT = 0xc0;
217 static constexpr size_t TAPROOT_CONTROL_BASE_SIZE = 33;
218 static constexpr size_t TAPROOT_CONTROL_NODE_SIZE = 32;
219 static constexpr size_t TAPROOT_CONTROL_MAX_NODE_COUNT = 128;
221 
222 extern const CHashWriter HASHER_TAPLEAF;
223 extern const CHashWriter HASHER_TAPBRANCH;
224 
225 template <class T>
226 uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache = nullptr);
227 
229 {
230 public:
231  virtual bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const
232  {
233  return false;
234  }
235 
236  virtual bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, const ScriptExecutionData& execdata, ScriptError* serror = nullptr) const
237  {
238  return false;
239  }
240 
241  virtual bool CheckLockTime(const CScriptNum& nLockTime) const
242  {
243  return false;
244  }
245 
246  virtual bool CheckSequence(const CScriptNum& nSequence) const
247  {
248  return false;
249  }
250 
252 };
253 
258 {
259  ASSERT_FAIL,
260  FAIL,
261 };
262 
263 template<typename T>
264 bool SignatureHashSchnorr(uint256& hash_out, const ScriptExecutionData& execdata, const T& tx_to, uint32_t in_pos, uint8_t hash_type, SigVersion sigversion, const PrecomputedTransactionData& cache, MissingDataBehavior mdb);
265 
266 template <class T>
268 {
269 private:
270  const T* txTo;
272  unsigned int nIn;
275 
276 protected:
277  virtual bool VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;
278  virtual bool VerifySchnorrSignature(Span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const;
279 
280 public:
281  GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, MissingDataBehavior mdb) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(nullptr) {}
282  GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, const PrecomputedTransactionData& txdataIn, MissingDataBehavior mdb) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(&txdataIn) {}
283  bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override;
284  bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, const ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override;
285  bool CheckLockTime(const CScriptNum& nLockTime) const override;
286  bool CheckSequence(const CScriptNum& nSequence) const override;
287 };
288 
291 
293 {
294 protected:
296 
297 public:
299 
300  bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override
301  {
302  return m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion);
303  }
304 
305  bool CheckSchnorrSignature(Span<const unsigned char> sig, Span<const unsigned char> pubkey, SigVersion sigversion, const ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override
306  {
307  return m_checker.CheckSchnorrSignature(sig, pubkey, sigversion, execdata, serror);
308  }
309 
310  bool CheckLockTime(const CScriptNum& nLockTime) const override
311  {
312  return m_checker.CheckLockTime(nLockTime);
313  }
314  bool CheckSequence(const CScriptNum& nSequence) const override
315  {
316  return m_checker.CheckSequence(nSequence);
317  }
318 };
319 
321 uint256 ComputeTapleafHash(uint8_t leaf_version, const CScript& script);
325 
326 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* error = nullptr);
327 bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, unsigned int flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* error = nullptr);
328 bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags, const BaseSignatureChecker& checker, ScriptError* serror = nullptr);
329 
330 size_t CountWitnessSigOps(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, unsigned int flags);
331 
332 bool CheckMinimalPush(const std::vector<unsigned char>& data, opcodetype opcode);
333 
334 int FindAndDelete(CScript& script, const CScript& b);
335 
336 #endif // BITCOIN_SCRIPT_INTERPRETER_H
virtual bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const
Definition: interpreter.h:231
Witness v0 (P2WPKH and P2WSH); see BIP 141.
virtual bool CheckLockTime(const CScriptNum &nLockTime) const
Definition: interpreter.h:241
virtual ~BaseSignatureChecker()
Definition: interpreter.h:251
bool CheckLockTime(const CScriptNum &nLockTime) const override
Definition: interpreter.h:310
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, key path spending; see BIP 341...
bool VerifyScript(const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, unsigned int flags, const BaseSignatureChecker &checker, ScriptError *serror=nullptr)
enum ScriptError_t ScriptError
virtual bool VerifySchnorrSignature(Span< const unsigned char > sig, const XOnlyPubKey &pubkey, const uint256 &sighash) const
const MissingDataBehavior m_mdb
Definition: interpreter.h:271
static constexpr size_t WITNESS_V1_TAPROOT_SIZE
Definition: interpreter.h:213
DeferringSignatureChecker(BaseSignatureChecker &checker)
Definition: interpreter.h:298
uint256 ComputeTapleafHash(uint8_t leaf_version, const CScript &script)
Compute the BIP341 tapleaf hash from leaf version & script.
bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, const ScriptExecutionData &execdata, ScriptError *serror=nullptr) const override
bool m_annex_present
Whether an annex is present.
Definition: interpreter.h:200
uint256 SignatureHash(const CScript &scriptCode, const T &txTo, unsigned int nIn, int nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache=nullptr)
int64_t m_validation_weight_left
How much validation weight is left (decremented for every successful non-empty signature check)...
Definition: interpreter.h:207
std::vector< CTxOut > m_spent_outputs
Definition: interpreter.h:164
static constexpr uint8_t TAPROOT_LEAF_TAPSCRIPT
Definition: interpreter.h:216
bool CheckSignatureEncoding(const std::vector< unsigned char > &vchSig, unsigned int flags, ScriptError *serror)
Bare scripts and BIP16 P2SH-wrapped redeemscripts.
Witness v1 with 32-byte program, not BIP16 P2SH-wrapped, script path spending, leaf version 0xc0; see...
bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, const ScriptExecutionData &execdata, ScriptError *serror=nullptr) const override
Definition: interpreter.h:305
static constexpr size_t TAPROOT_CONTROL_BASE_SIZE
Definition: interpreter.h:217
Taproot only; implied when sighash byte is missing, and equivalent to SIGHASH_ALL.
Definition: interpreter.h:32
uint32_t m_codeseparator_pos
Opcode position of the last executed OP_CODESEPARATOR (or 0xFFFFFFFF if none executed).
Definition: interpreter.h:195
bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
static constexpr size_t TAPROOT_CONTROL_NODE_SIZE
Definition: interpreter.h:218
int64_t CAmount
Amount in satoshis (Can be negative)
Definition: amount.h:12
bool m_annex_init
Whether m_annex_present and (when needed) m_annex_hash are initialized.
Definition: interpreter.h:198
uint256 m_tapleaf_hash
The tapleaf hash.
Definition: interpreter.h:190
bool CheckSequence(const CScriptNum &nSequence) const override
Definition: interpreter.h:314
opcodetype
Script opcodes.
Definition: script.h:65
virtual bool VerifyECDSASignature(const std::vector< unsigned char > &vchSig, const CPubKey &vchPubKey, const uint256 &sighash) const
Abort execution through assertion failure (for consensus code)
const PrecomputedTransactionData * txdata
Definition: interpreter.h:274
void Init(const T &tx, std::vector< CTxOut > &&spent_outputs, bool force=false)
static constexpr uint8_t TAPROOT_LEAF_MASK
Definition: interpreter.h:215
An encapsulated public key.
Definition: pubkey.h:32
bool m_bip143_segwit_ready
Whether the 3 fields above are initialized.
Definition: interpreter.h:162
virtual bool CheckSchnorrSignature(Span< const unsigned char > sig, Span< const unsigned char > pubkey, SigVersion sigversion, const ScriptExecutionData &execdata, ScriptError *serror=nullptr) const
Definition: interpreter.h:236
const CHashWriter HASHER_TAPLEAF
Hasher with tag "TapLeaf" pre-fed to it.
uint256 ComputeTaprootMerkleRoot(Span< const unsigned char > control, const uint256 &tapleaf_hash)
Compute the BIP341 taproot script tree Merkle root from control block and leaf hash.
bool CheckLockTime(const CScriptNum &nLockTime) const override
Just act as if the signature was invalid.
bool EvalScript(std::vector< std::vector< unsigned char > > &stack, const CScript &script, unsigned int flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *error=nullptr)
MissingDataBehavior
Enum to specify what *TransactionSignatureChecker&#39;s behavior should be when dealing with missing tran...
Definition: interpreter.h:257
bool CheckECDSASignature(const std::vector< unsigned char > &scriptSig, const std::vector< unsigned char > &vchPubKey, const CScript &scriptCode, SigVersion sigversion) const override
Definition: interpreter.h:300
An output of a transaction.
Definition: transaction.h:128
static constexpr size_t WITNESS_V0_SCRIPTHASH_SIZE
Signature hash sizes.
Definition: interpreter.h:211
bool m_bip341_taproot_ready
Whether the 5 fields above are initialized.
Definition: interpreter.h:157
static constexpr size_t TAPROOT_CONTROL_MAX_SIZE
Definition: interpreter.h:220
const CHashWriter HASHER_TAPBRANCH
Hasher with tag "TapBranch" pre-fed to it.
bool m_codeseparator_pos_init
Whether m_codeseparator_pos is initialized.
Definition: interpreter.h:193
int flags
Definition: bitcoin-tx.cpp:512
256-bit opaque blob.
Definition: uint256.h:124
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const CAmount &amountIn, MissingDataBehavior mdb)
Definition: interpreter.h:281
static constexpr size_t TAPROOT_CONTROL_MAX_NODE_COUNT
Definition: interpreter.h:219
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:404
static constexpr size_t WITNESS_V0_KEYHASH_SIZE
Definition: interpreter.h:212
bool m_spent_outputs_ready
Whether m_spent_outputs is initialized.
Definition: interpreter.h:166
virtual bool CheckSequence(const CScriptNum &nSequence) const
Definition: interpreter.h:246
bool CheckMinimalPush(const std::vector< unsigned char > &data, opcodetype opcode)
bool m_tapleaf_hash_init
Whether m_tapleaf_hash is initialized.
Definition: interpreter.h:188
A writer stream (for serialization) that computes a 256-bit hash.
Definition: hash.h:100
int FindAndDelete(CScript &script, const CScript &b)
GenericTransactionSignatureChecker(const T *txToIn, unsigned int nInIn, const CAmount &amountIn, const PrecomputedTransactionData &txdataIn, MissingDataBehavior mdb)
Definition: interpreter.h:282
PrecomputedTransactionData()=default
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
The basic transaction that is broadcasted on the network and contained in blocks. ...
Definition: transaction.h:259
bool CheckSequence(const CScriptNum &nSequence) const override
bool SignatureHashSchnorr(uint256 &hash_out, const ScriptExecutionData &execdata, const T &tx_to, uint32_t in_pos, uint8_t hash_type, SigVersion sigversion, const PrecomputedTransactionData &cache, MissingDataBehavior mdb)
bool error(const char *fmt, const Args &... args)
Definition: system.h:49
uint256 m_annex_hash
Hash of the annex data.
Definition: interpreter.h:202
BaseSignatureChecker & m_checker
Definition: interpreter.h:295
bool m_validation_weight_left_init
Whether m_validation_weight_left is initialized.
Definition: interpreter.h:205
SigVersion
Definition: interpreter.h:177