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