Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
251extern const HashWriter HASHER_TAPBRANCH; //!< Hasher with tag "TapBranch" pre-fed to it.253/** Data structure to cache SHA256 midstates for the ECDSA sighash calculations254* (bare, P2SH, P2WPKH, P2WSH). */255class SigHashCache256{257/** For each sighash mode (ALL, SINGLE, NONE, ALL|ANYONE, SINGLE|ANYONE, NONE|ANYONE),258* optionally store a scriptCode which the hash is for, plus a midstate for the SHA256259* computation just before adding the hash_type itself. */260std::optional<std::pair<CScript, HashWriter>> m_cache_entries[6];262/** Given a hash_type, find which of the 6 cache entries is to be used. */263int CacheIndex(int32_t hash_type) const noexcept;265public:266/** Load into writer the SHA256 midstate if found in this cache. */267[[nodiscard]] bool Load(int32_t hash_type, const CScript& script_code, HashWriter& writer) const noexcept;268/** Store into this cache object the provided SHA256 midstate. */269void Store(int32_t hash_type, const CScript& script_code, const HashWriter& writer) noexcept;270};272template <class T>273uint256 SignatureHash(const CScript& scriptCode, const T& txTo, unsigned int nIn, int32_t nHashType, const CAmount& amount, SigVersion sigversion, const PrecomputedTransactionData* cache = nullptr, SigHashCache* sighash_cache = nullptr);275class BaseSignatureChecker276{277public:278virtual bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const279{280return false;281}283virtual bool CheckSchnorrSignature(std::span<const unsigned char> sig, std::span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const284{285return false;286}288virtual bool CheckLockTime(const CScriptNum& nLockTime) const289{290return false;291}293virtual bool CheckSequence(const CScriptNum& nSequence) const294{295return false;296}298virtual ~BaseSignatureChecker() = default;299};301/** Enum to specify what *TransactionSignatureChecker's behavior should be302* when dealing with missing transaction data.303*/304enum class MissingDataBehavior305{306ASSERT_FAIL, //!< Abort execution through assertion failure (for consensus code)307FAIL, //!< Just act as if the signature was invalid308};310template<typename T>311bool 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);313template <class T>314class GenericTransactionSignatureChecker : public BaseSignatureChecker315{316private:317const T* txTo;318const MissingDataBehavior m_mdb;319unsigned int nIn;320const CAmount amount;321const PrecomputedTransactionData* txdata;322mutable SigHashCache m_sighash_cache;324protected:325virtual bool VerifyECDSASignature(const std::vector<unsigned char>& vchSig, const CPubKey& vchPubKey, const uint256& sighash) const;326virtual bool VerifySchnorrSignature(std::span<const unsigned char> sig, const XOnlyPubKey& pubkey, const uint256& sighash) const;328public:329GenericTransactionSignatureChecker(const T* txToIn, unsigned int nInIn, const CAmount& amountIn, MissingDataBehavior mdb) : txTo(txToIn), m_mdb(mdb), nIn(nInIn), amount(amountIn), txdata(nullptr) {}330GenericTransactionSignatureChecker(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) {}331bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override;332bool CheckSchnorrSignature(std::span<const unsigned char> sig, std::span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override;333bool CheckLockTime(const CScriptNum& nLockTime) const override;334bool CheckSequence(const CScriptNum& nSequence) const override;335};337using TransactionSignatureChecker = GenericTransactionSignatureChecker<CTransaction>;338using MutableTransactionSignatureChecker = GenericTransactionSignatureChecker<CMutableTransaction>;340class DeferringSignatureChecker : public BaseSignatureChecker341{342protected:343const BaseSignatureChecker& m_checker;345public:346DeferringSignatureChecker(const BaseSignatureChecker& checker) : m_checker(checker) {}348bool CheckECDSASignature(const std::vector<unsigned char>& scriptSig, const std::vector<unsigned char>& vchPubKey, const CScript& scriptCode, SigVersion sigversion) const override349{350return m_checker.CheckECDSASignature(scriptSig, vchPubKey, scriptCode, sigversion);351}353bool CheckSchnorrSignature(std::span<const unsigned char> sig, std::span<const unsigned char> pubkey, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* serror = nullptr) const override354{355return m_checker.CheckSchnorrSignature(sig, pubkey, sigversion, execdata, serror);356}358bool CheckLockTime(const CScriptNum& nLockTime) const override359{360return m_checker.CheckLockTime(nLockTime);361}362bool CheckSequence(const CScriptNum& nSequence) const override363{364return m_checker.CheckSequence(nSequence);365}366};368/** Compute the BIP341 tapleaf hash from leaf version & script. */369uint256 ComputeTapleafHash(uint8_t leaf_version, std::span<const unsigned char> script);370/** Compute the BIP341 tapbranch hash from two branches.371* Spans must be 32 bytes each. */372uint256 ComputeTapbranchHash(std::span<const unsigned char> a, std::span<const unsigned char> b);373/** Compute the BIP341 taproot script tree Merkle root from control block and leaf hash.374* Requires control block to have valid length (33 + k*32, with k in {0,1,..,128}). */375uint256 ComputeTaprootMerkleRoot(std::span<const unsigned char> control, const uint256& tapleaf_hash);377bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptExecutionData& execdata, ScriptError* error = nullptr);378bool EvalScript(std::vector<std::vector<unsigned char> >& stack, const CScript& script, script_verify_flags flags, const BaseSignatureChecker& checker, SigVersion sigversion, ScriptError* error = nullptr);379bool VerifyScript(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness* witness, script_verify_flags flags, const BaseSignatureChecker& checker, ScriptError* serror = nullptr);381size_t CountWitnessSigOps(const CScript& scriptSig, const CScript& scriptPubKey, const CScriptWitness& witness, script_verify_flags flags);383int FindAndDelete(CScript& script, const CScript& b);385const std::map<std::string, script_verify_flag_name>& ScriptFlagNamesToEnum();387std::vector<std::string> GetScriptFlagNames(script_verify_flags flags);389#endif // BITCOIN_SCRIPT_INTERPRETER_H
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.