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.
251* will always fail. */252bool IsFullyValid() const;254/** Test whether this is the 0 key (the result of default construction). This implies255* !IsFullyValid(). */256bool IsNull() const { return m_keydata.IsNull(); }258/** Construct an x-only pubkey from exactly 32 bytes. */259constexpr explicit XOnlyPubKey(std::span<const unsigned char> bytes) : m_keydata{bytes} {}261/** Construct an x-only pubkey from a normal pubkey. */262explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(std::span{pubkey}.subspan(1, 32)) {}264/** Verify a Schnorr signature against this public key.265*266* sigbytes must be exactly 64 bytes.267*/268bool VerifySchnorr(const uint256& msg, std::span<const unsigned char> sigbytes) const;270/** Compute the Taproot tweak as specified in BIP341, with *this as internal271* key:272* - if merkle_root == nullptr: H_TapTweak(xonly_pubkey)273* - otherwise: H_TapTweak(xonly_pubkey || *merkle_root)274*275* Note that the behavior of this function with merkle_root != nullptr is276* consensus critical.277*/278uint256 ComputeTapTweakHash(const uint256* merkle_root) const;280/** Verify that this is a Taproot tweaked output point, against a specified internal key,281* Merkle root, and parity. */282bool CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const;284/** Construct a Taproot tweaked output point with this point as internal key. */285std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;287/** Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.288* As the CKeyID is the Hash160(full pubkey), the produced CKeyIDs are for the versions of this289* XOnlyPubKey with 0x02 and 0x03 prefixes.290* This is needed for key lookups since keys are indexed by CKeyID.291*/292std::vector<CKeyID> GetKeyIDs() const;293/** Returns this XOnlyPubKey with 0x02 and 0x03 prefixes */294std::vector<CPubKey> GetCPubKeys() const;296CPubKey GetEvenCorrespondingCPubKey() const;298const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }299static constexpr size_t size() { return decltype(m_keydata)::size(); }300const unsigned char* data() const { return m_keydata.begin(); }301const unsigned char* begin() const { return m_keydata.begin(); }302const unsigned char* end() const { return m_keydata.end(); }303unsigned char* data() { return m_keydata.begin(); }304unsigned char* begin() { return m_keydata.begin(); }305unsigned char* end() { return m_keydata.end(); }306bool operator==(const XOnlyPubKey& other) const { return m_keydata == other.m_keydata; }307bool operator<(const XOnlyPubKey& other) const { return m_keydata < other.m_keydata; }309//! Implement serialization without length prefixes since it is a fixed length310SERIALIZE_METHODS(XOnlyPubKey, obj) { READWRITE(obj.m_keydata); }311};313/** An ElligatorSwift-encoded public key. */314struct EllSwiftPubKey315{316private:317static constexpr size_t SIZE = 64;318std::array<std::byte, SIZE> m_pubkey;320public:321/** Default constructor creates all-zero pubkey (which is valid). */322EllSwiftPubKey() noexcept = default;324/** Construct a new ellswift public key from a given serialization. */325EllSwiftPubKey(std::span<const std::byte> ellswift) noexcept;327/** Decode to normal compressed CPubKey (for debugging purposes). */328CPubKey Decode() const;330// Read-only access for serialization.331const std::byte* data() const { return m_pubkey.data(); }332static constexpr size_t size() { return SIZE; }333auto begin() const { return m_pubkey.cbegin(); }334auto end() const { return m_pubkey.cend(); }336bool friend operator==(const EllSwiftPubKey& a, const EllSwiftPubKey& b)337{338return a.m_pubkey == b.m_pubkey;339}340};342struct CExtPubKey {343unsigned char version[4];344unsigned char nDepth;345KeyFingerprint fingerprint;346unsigned int nChild;347ChainCode chaincode;348CPubKey pubkey;350friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)351{352return a.nDepth == b.nDepth &&353a.fingerprint == b.fingerprint &&354a.nChild == b.nChild &&355a.chaincode == b.chaincode &&356a.pubkey == b.pubkey;357}359friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)360{361if (a.pubkey < b.pubkey) {362return true;363} else if (a.pubkey > b.pubkey) {364return false;365}366return a.chaincode < b.chaincode;367}369KeyFingerprint id_key_fingerprint() const370{371return pubkey.GetID().fingerprint();372}374void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;375void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);376void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const;377void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]);378[[nodiscard]] bool Derive(CExtPubKey& out, unsigned int nChild, uint256* bip32_tweak_out = nullptr) const;379};381#endif // BITCOIN_PUBKEY_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.