Bitcoin Core 30.99.0
P2P Digital Currency
Macros | Typedefs | Functions | Variables
interpreter.cpp File Reference
#include <script/interpreter.h>
#include <crypto/ripemd160.h>
#include <crypto/sha1.h>
#include <crypto/sha256.h>
#include <pubkey.h>
#include <script/script.h>
#include <tinyformat.h>
#include <uint256.h>
Include dependency graph for interpreter.cpp:

Go to the source code of this file.

Macros

#define stacktop(i)   (stack.at(size_t(int64_t(stack.size()) + int64_t{i})))
 Script is a stack machine (like Forth) that evaluates a predicate returning a bool indicating valid or not. More...
 
#define altstacktop(i)   (altstack.at(size_t(int64_t(altstack.size()) + int64_t{i})))
 
#define FLAG_NAME(flag)   {std::string(#flag), SCRIPT_VERIFY_##flag}
 

Typedefs

typedef std::vector< unsigned char > valtype
 

Functions

bool CastToBool (const valtype &vch)
 
static void popstack (std::vector< valtype > &stack)
 
static bool IsCompressedOrUncompressedPubKey (const valtype &vchPubKey)
 
static bool IsCompressedPubKey (const valtype &vchPubKey)
 
static bool IsValidSignatureEncoding (const std::vector< unsigned char > &sig)
 A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <hashtype> Where R and S are not negative (their first byte has its highest bit not set), and not excessively padded (do not start with a 0 byte, unless an otherwise negative number follows, in which case a single 0 byte is necessary and even required). More...
 
static bool IsLowDERSignature (const valtype &vchSig, ScriptError *serror)
 
static bool IsDefinedHashtypeSignature (const valtype &vchSig)
 
bool CheckSignatureEncoding (const std::vector< unsigned char > &vchSig, script_verify_flags flags, ScriptError *serror)
 
static bool CheckPubKeyEncoding (const valtype &vchPubKey, script_verify_flags flags, const SigVersion &sigversion, ScriptError *serror)
 
int FindAndDelete (CScript &script, const CScript &b)
 
static bool EvalChecksigPreTapscript (const valtype &vchSig, const valtype &vchPubKey, CScript::const_iterator pbegincodehash, CScript::const_iterator pend, script_verify_flags flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror, bool &fSuccess)
 
static bool EvalChecksigTapscript (const valtype &sig, const valtype &pubkey, ScriptExecutionData &execdata, script_verify_flags flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror, bool &success)
 
static bool EvalChecksig (const valtype &sig, const valtype &pubkey, CScript::const_iterator pbegincodehash, CScript::const_iterator pend, ScriptExecutionData &execdata, script_verify_flags flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror, bool &success)
 Helper for OP_CHECKSIG, OP_CHECKSIGVERIFY, and (in Tapscript) OP_CHECKSIGADD. More...
 
bool EvalScript (std::vector< std::vector< unsigned char > > &stack, const CScript &script, script_verify_flags flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptExecutionData &execdata, ScriptError *serror)
 
bool EvalScript (std::vector< std::vector< unsigned char > > &stack, const CScript &script, script_verify_flags flags, const BaseSignatureChecker &checker, SigVersion sigversion, ScriptError *serror)
 
static bool HandleMissingData (MissingDataBehavior mdb)
 
template<typename T >
bool SignatureHashSchnorr (uint256 &hash_out, ScriptExecutionData &execdata, const T &tx_to, uint32_t in_pos, uint8_t hash_type, SigVersion sigversion, const PrecomputedTransactionData &cache, MissingDataBehavior mdb)
 
template<class T >
uint256 SignatureHash (const CScript &scriptCode, const T &txTo, unsigned int nIn, int32_t nHashType, const CAmount &amount, SigVersion sigversion, const PrecomputedTransactionData *cache, SigHashCache *sighash_cache)
 
static bool ExecuteWitnessScript (const std::span< const valtype > &stack_span, const CScript &exec_script, script_verify_flags flags, SigVersion sigversion, const BaseSignatureChecker &checker, ScriptExecutionData &execdata, ScriptError *serror)
 
uint256 ComputeTapleafHash (uint8_t leaf_version, std::span< const unsigned char > script)
 Compute the BIP341 tapleaf hash from leaf version & script. More...
 
uint256 ComputeTapbranchHash (std::span< const unsigned char > a, std::span< const unsigned char > b)
 Compute the BIP341 tapbranch hash from two branches. More...
 
uint256 ComputeTaprootMerkleRoot (std::span< const unsigned char > control, const uint256 &tapleaf_hash)
 Compute the BIP341 taproot script tree Merkle root from control block and leaf hash. More...
 
static bool VerifyTaprootCommitment (const std::vector< unsigned char > &control, const std::vector< unsigned char > &program, const uint256 &tapleaf_hash)
 
static bool VerifyWitnessProgram (const CScriptWitness &witness, int witversion, const std::vector< unsigned char > &program, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror, bool is_p2sh)
 
bool VerifyScript (const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags, const BaseSignatureChecker &checker, ScriptError *serror)
 
static size_t WitnessSigOps (int witversion, const std::vector< unsigned char > &witprogram, const CScriptWitness &witness)
 
size_t CountWitnessSigOps (const CScript &scriptSig, const CScript &scriptPubKey, const CScriptWitness *witness, script_verify_flags flags)
 
const std::map< std::string, script_verify_flag_name > & ScriptFlagNamesToEnum ()
 
std::vector< std::string > GetScriptFlagNames (script_verify_flags flags)
 

Variables

const HashWriter HASHER_TAPSIGHASH {TaggedHash("TapSighash")}
 Hasher with tag "TapSighash" pre-fed to it. More...
 
const HashWriter HASHER_TAPLEAF {TaggedHash("TapLeaf")}
 Hasher with tag "TapLeaf" pre-fed to it. More...
 
const HashWriter HASHER_TAPBRANCH {TaggedHash("TapBranch")}
 Hasher with tag "TapBranch" pre-fed to it. More...
 

Macro Definition Documentation

◆ altstacktop

#define altstacktop (   i)    (altstack.at(size_t(int64_t(altstack.size()) + int64_t{i})))

Definition at line 56 of file interpreter.cpp.

◆ FLAG_NAME

#define FLAG_NAME (   flag)    {std::string(#flag), SCRIPT_VERIFY_##flag}

◆ stacktop

#define stacktop (   i)    (stack.at(size_t(int64_t(stack.size()) + int64_t{i})))

Script is a stack machine (like Forth) that evaluates a predicate returning a bool indicating valid or not.

There are no loops.

Definition at line 55 of file interpreter.cpp.

Typedef Documentation

◆ valtype

typedef std::vector<unsigned char> valtype

Definition at line 16 of file interpreter.cpp.

Function Documentation

◆ CastToBool()

bool CastToBool ( const valtype vch)

Definition at line 36 of file interpreter.cpp.

Here is the caller graph for this function:

◆ CheckPubKeyEncoding()

static bool CheckPubKeyEncoding ( const valtype vchPubKey,
script_verify_flags  flags,
const SigVersion sigversion,
ScriptError serror 
)
static

Definition at line 218 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CheckSignatureEncoding()

bool CheckSignatureEncoding ( const std::vector< unsigned char > &  vchSig,
script_verify_flags  flags,
ScriptError serror 
)

Definition at line 201 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeTapbranchHash()

uint256 ComputeTapbranchHash ( std::span< const unsigned char >  a,
std::span< const unsigned char >  b 
)

Compute the BIP341 tapbranch hash from two branches.

Spans must be 32 bytes each.

Definition at line 1873 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ComputeTapleafHash()

uint256 ComputeTapleafHash ( uint8_t  leaf_version,
std::span< const unsigned char >  script 
)

Compute the BIP341 tapleaf hash from leaf version & script.

Definition at line 1868 of file interpreter.cpp.

Here is the caller graph for this function:

◆ ComputeTaprootMerkleRoot()

uint256 ComputeTaprootMerkleRoot ( std::span< const unsigned char >  control,
const uint256 tapleaf_hash 
)

Compute the BIP341 taproot script tree Merkle root from control block and leaf hash.

Requires control block to have valid length (33 + k*32, with k in {0,1,..,128}).

Definition at line 1884 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ CountWitnessSigOps()

size_t CountWitnessSigOps ( const CScript scriptSig,
const CScript scriptPubKey,
const CScriptWitness witness,
script_verify_flags  flags 
)

Definition at line 2135 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EvalChecksig()

static bool EvalChecksig ( const valtype sig,
const valtype pubkey,
CScript::const_iterator  pbegincodehash,
CScript::const_iterator  pend,
ScriptExecutionData execdata,
script_verify_flags  flags,
const BaseSignatureChecker checker,
SigVersion  sigversion,
ScriptError serror,
bool &  success 
)
static

Helper for OP_CHECKSIG, OP_CHECKSIGVERIFY, and (in Tapscript) OP_CHECKSIGADD.

A return value of false means the script fails entirely. When true is returned, the success variable indicates whether the signature check itself succeeded.

Definition at line 392 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EvalChecksigPreTapscript()

static bool EvalChecksigPreTapscript ( const valtype vchSig,
const valtype vchPubKey,
CScript::const_iterator  pbegincodehash,
CScript::const_iterator  pend,
script_verify_flags  flags,
const BaseSignatureChecker checker,
SigVersion  sigversion,
ScriptError serror,
bool &  fSuccess 
)
static

Definition at line 321 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EvalChecksigTapscript()

static bool EvalChecksigTapscript ( const valtype sig,
const valtype pubkey,
ScriptExecutionData execdata,
script_verify_flags  flags,
const BaseSignatureChecker checker,
SigVersion  sigversion,
ScriptError serror,
bool &  success 
)
static

Definition at line 347 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ EvalScript() [1/2]

bool EvalScript ( std::vector< std::vector< unsigned char > > &  stack,
const CScript script,
script_verify_flags  flags,
const BaseSignatureChecker checker,
SigVersion  sigversion,
ScriptError serror 
)

Definition at line 1237 of file interpreter.cpp.

Here is the call graph for this function:

◆ EvalScript() [2/2]

bool EvalScript ( std::vector< std::vector< unsigned char > > &  stack,
const CScript script,
script_verify_flags  flags,
const BaseSignatureChecker checker,
SigVersion  sigversion,
ScriptExecutionData execdata,
ScriptError serror 
)

Definition at line 407 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ ExecuteWitnessScript()

static bool ExecuteWitnessScript ( const std::span< const valtype > &  stack_span,
const CScript exec_script,
script_verify_flags  flags,
SigVersion  sigversion,
const BaseSignatureChecker checker,
ScriptExecutionData execdata,
ScriptError serror 
)
static

Definition at line 1828 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ FindAndDelete()

int FindAndDelete ( CScript script,
const CScript b 
)

Definition at line 229 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetScriptFlagNames()

std::vector< std::string > GetScriptFlagNames ( script_verify_flags  flags)

Definition at line 2196 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ HandleMissingData()

static bool HandleMissingData ( MissingDataBehavior  mdb)
static

Definition at line 1466 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsCompressedOrUncompressedPubKey()

static bool IsCompressedOrUncompressedPubKey ( const valtype vchPubKey)
static

Definition at line 64 of file interpreter.cpp.

Here is the caller graph for this function:

◆ IsCompressedPubKey()

static bool IsCompressedPubKey ( const valtype vchPubKey)
static

Definition at line 86 of file interpreter.cpp.

Here is the caller graph for this function:

◆ IsDefinedHashtypeSignature()

static bool IsDefinedHashtypeSignature ( const valtype vchSig)
static

Definition at line 190 of file interpreter.cpp.

Here is the caller graph for this function:

◆ IsLowDERSignature()

static bool IsLowDERSignature ( const valtype vchSig,
ScriptError serror 
)
static

Definition at line 173 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsValidSignatureEncoding()

static bool IsValidSignatureEncoding ( const std::vector< unsigned char > &  sig)
static

A canonical signature exists of: <30> <total len> <02> <len R> <R> <02> <len S> <hashtype> Where R and S are not negative (their first byte has its highest bit not set), and not excessively padded (do not start with a 0 byte, unless an otherwise negative number follows, in which case a single 0 byte is necessary and even required).

See https://bitcointalk.org/index.php?topic=8392.msg127623#msg127623

This function is consensus-critical since BIP66.

Definition at line 108 of file interpreter.cpp.

Here is the caller graph for this function:

◆ popstack()

static void popstack ( std::vector< valtype > &  stack)
inlinestatic

Definition at line 57 of file interpreter.cpp.

Here is the caller graph for this function:

◆ ScriptFlagNamesToEnum()

const std::map< std::string, script_verify_flag_name > & ScriptFlagNamesToEnum ( )

Definition at line 2166 of file interpreter.cpp.

Here is the caller graph for this function:

◆ SignatureHash()

template<class T >
uint256 SignatureHash ( const CScript scriptCode,
const T &  txTo,
unsigned int  nIn,
int32_t  nHashType,
const CAmount amount,
SigVersion  sigversion,
const PrecomputedTransactionData cache,
SigHashCache sighash_cache 
)

Definition at line 1596 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ SignatureHashSchnorr()

template<typename T >
bool SignatureHashSchnorr ( uint256 hash_out,
ScriptExecutionData execdata,
const T &  tx_to,
uint32_t  in_pos,
uint8_t  hash_type,
SigVersion  sigversion,
const PrecomputedTransactionData cache,
MissingDataBehavior  mdb 
)

Definition at line 1479 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VerifyScript()

bool VerifyScript ( const CScript scriptSig,
const CScript scriptPubKey,
const CScriptWitness witness,
script_verify_flags  flags,
const BaseSignatureChecker checker,
ScriptError serror 
)

Definition at line 1998 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VerifyTaprootCommitment()

static bool VerifyTaprootCommitment ( const std::vector< unsigned char > &  control,
const std::vector< unsigned char > &  program,
const uint256 tapleaf_hash 
)
static

The internal pubkey (x-only, so no Y coordinate parity).

The output pubkey (taken from the scriptPubKey).

Definition at line 1899 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ VerifyWitnessProgram()

static bool VerifyWitnessProgram ( const CScriptWitness witness,
int  witversion,
const std::vector< unsigned char > &  program,
script_verify_flags  flags,
const BaseSignatureChecker checker,
ScriptError serror,
bool  is_p2sh 
)
static

< Actually executed script (last stack item in P2WSH; implied P2PKH script in P2WPKH; leaf script in P2TR)

Definition at line 1913 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ WitnessSigOps()

static size_t WitnessSigOps ( int  witversion,
const std::vector< unsigned char > &  witprogram,
const CScriptWitness witness 
)
static

Definition at line 2119 of file interpreter.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ HASHER_TAPBRANCH

const HashWriter HASHER_TAPBRANCH {TaggedHash("TapBranch")}

Hasher with tag "TapBranch" pre-fed to it.

Definition at line 1464 of file interpreter.cpp.

◆ HASHER_TAPLEAF

const HashWriter HASHER_TAPLEAF {TaggedHash("TapLeaf")}

Hasher with tag "TapLeaf" pre-fed to it.

Definition at line 1463 of file interpreter.cpp.

◆ HASHER_TAPSIGHASH

const HashWriter HASHER_TAPSIGHASH {TaggedHash("TapSighash")}

Hasher with tag "TapSighash" pre-fed to it.

Definition at line 1462 of file interpreter.cpp.