7#ifndef BITCOIN_PUBKEY_H
8#define BITCOIN_PUBKEY_H
39 static constexpr unsigned int SIZE = 65;
49 "COMPRESSED_SIZE is larger than SIZE");
60 unsigned int static GetLen(
unsigned char chHeader)
62 if (chHeader == 2 || chHeader == 3)
64 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
89 void Set(
const T pbegin,
const T pend)
91 int len = pend == pbegin ? 0 :
GetLen(pbegin[0]);
92 if (len && len == (pend - pbegin))
93 memcpy(
vch, (
unsigned char*)&pbegin[0], len);
113 const unsigned char*
data()
const {
return vch; }
116 const unsigned char&
operator[](
unsigned int pos)
const {
return vch[pos]; }
121 return a.
vch[0] == b.
vch[0] &&
130 return a.
vch[0] < b.
vch[0] ||
135 return a.
vch[0] > b.
vch[0] ||
140 template <
typename Stream>
143 unsigned int len =
size();
147 template <
typename Stream>
197 return size() > 0 && (
vch[0] == 0x02 ||
vch[0] == 0x03 ||
vch[0] == 0x04);
213 bool Verify(
const uint256& hash,
const std::vector<unsigned char>& vchSig)
const;
218 static bool CheckLowS(
const std::vector<unsigned char>& vchSig);
312 static constexpr size_t SIZE = 64;
A reference to a CKey: the Hash160 of its serialized public key.
CKeyID(const uint160 &in)
An encapsulated public key.
const unsigned char * data() const
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
bool IsCompressed() const
Check whether this is a compressed public key.
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
static constexpr unsigned int COMPRESSED_SIZE
CPubKey()
Construct an invalid public key.
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
friend bool operator>(const CPubKey &a, const CPubKey &b)
bool Decompress()
Turn this public key into an uncompressed public key.
const unsigned char * end() const
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
static constexpr unsigned int SIZE
secp256k1:
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
const unsigned char * begin() const
static bool ValidSize(const std::vector< unsigned char > &vch)
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
unsigned char vch[SIZE]
see www.keylength.com script supports up to 75 for single byte push
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
void Invalidate()
Set this key data to be invalid.
CPubKey(Span< const uint8_t > _vch)
Construct a public key from a byte vector.
void Unserialize(Stream &s)
uint256 GetHash() const
Get the 256-bit hash of this public key.
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
const unsigned char & operator[](unsigned int pos) const
static constexpr unsigned int SIGNATURE_SIZE
bool IsValidNonHybrid() const noexcept
Check if a public key is a syntactically valid compressed or uncompressed key.
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
friend bool operator!=(const CPubKey &a, const CPubKey &b)
friend bool operator<(const CPubKey &a, const CPubKey &b)
A Span is an object that can refer to a contiguous sequence of objects.
constexpr C * begin() const noexcept
constexpr C * end() const noexcept
XOnlyPubKey & operator=(const XOnlyPubKey &)=default
const unsigned char * end() const
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
const unsigned char * begin() const
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
bool CheckTapTweak(const XOnlyPubKey &internal, const uint256 &merkle_root, bool parity) const
Verify that this is a Taproot tweaked output point, against a specified internal key,...
static const XOnlyPubKey NUMS_H
Nothing Up My Sleeve point H Used as an internal key for provably disabling the key path spend see BI...
static constexpr size_t size()
constexpr XOnlyPubKey(std::span< const unsigned char > bytes)
Construct an x-only pubkey from exactly 32 bytes.
const unsigned char * data() const
SERIALIZE_METHODS(XOnlyPubKey, obj)
Implement serialization without length prefixes since it is a fixed length.
CPubKey GetEvenCorrespondingCPubKey() const
bool operator!=(const XOnlyPubKey &other) const
XOnlyPubKey(const XOnlyPubKey &)=default
XOnlyPubKey(const CPubKey &pubkey)
Construct an x-only pubkey from a normal pubkey.
bool operator<(const XOnlyPubKey &other) const
bool IsFullyValid() const
Determine if this pubkey is fully valid.
bool VerifySchnorr(const uint256 &msg, Span< const unsigned char > sigbytes) const
Verify a Schnorr signature against this public key.
std::vector< CKeyID > GetKeyIDs() const
Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
const unsigned char & operator[](int pos) const
XOnlyPubKey()=default
Construct an empty x-only pubkey.
bool operator==(const XOnlyPubKey &other) const
constexpr bool IsNull() const
constexpr unsigned char * end()
constexpr unsigned char * begin()
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE
const unsigned int BIP32_EXTKEY_SIZE
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
bool Derive(CExtPubKey &out, unsigned int nChild) const
void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const
unsigned char vchFingerprint[4]
friend bool operator!=(const CExtPubKey &a, const CExtPubKey &b)
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)
An ElligatorSwift-encoded public key.
bool friend operator==(const EllSwiftPubKey &a, const EllSwiftPubKey &b)
std::array< std::byte, SIZE > m_pubkey
CPubKey Decode() const
Decode to normal compressed CPubKey (for debugging purposes).
static constexpr size_t SIZE
bool friend operator!=(const EllSwiftPubKey &a, const EllSwiftPubKey &b)
static constexpr size_t size()
const std::byte * data() const
EllSwiftPubKey() noexcept=default
Default constructor creates all-zero pubkey (which is valid).