Bitcoin Core 30.99.0
P2P Digital Currency
pubkey.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-present The Bitcoin Core developers
3// Copyright (c) 2017 The Zcash developers
4// Distributed under the MIT software license, see the accompanying
5// file COPYING or http://www.opensource.org/licenses/mit-license.php.
6
7#ifndef BITCOIN_PUBKEY_H
8#define BITCOIN_PUBKEY_H
9
10#include <hash.h>
11#include <serialize.h>
12#include <span.h>
13#include <uint256.h>
14
15#include <cstring>
16#include <optional>
17#include <vector>
18
19const unsigned int BIP32_EXTKEY_SIZE = 74;
20const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE = 78;
21
23class CKeyID : public uint160
24{
25public:
26 CKeyID() : uint160() {}
27 explicit CKeyID(const uint160& in) : uint160(in) {}
28};
29
31
34{
35public:
39 static constexpr unsigned int SIZE = 65;
40 static constexpr unsigned int COMPRESSED_SIZE = 33;
41 static constexpr unsigned int SIGNATURE_SIZE = 72;
42 static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
47 static_assert(
49 "COMPRESSED_SIZE is larger than SIZE");
50
51private:
52
57 unsigned char vch[SIZE];
58
60 unsigned int static GetLen(unsigned char chHeader)
61 {
62 if (chHeader == 2 || chHeader == 3)
63 return COMPRESSED_SIZE;
64 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
65 return SIZE;
66 return 0;
67 }
68
71 {
72 vch[0] = 0xFF;
73 }
74
75public:
76
77 bool static ValidSize(const std::vector<unsigned char> &vch) {
78 return vch.size() > 0 && GetLen(vch[0]) == vch.size();
79 }
80
83 {
84 Invalidate();
85 }
86
88 template <typename T>
89 void Set(const T pbegin, const T pend)
90 {
91 int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
92 if (len && len == (pend - pbegin))
93 memcpy(vch, (unsigned char*)&pbegin[0], len);
94 else
95 Invalidate();
96 }
97
99 template <typename T>
100 CPubKey(const T pbegin, const T pend)
101 {
102 Set(pbegin, pend);
103 }
104
106 explicit CPubKey(std::span<const uint8_t> _vch)
107 {
108 Set(_vch.begin(), _vch.end());
109 }
110
112 unsigned int size() const { return GetLen(vch[0]); }
113 const unsigned char* data() const { return vch; }
114 const unsigned char* begin() const { return vch; }
115 const unsigned char* end() const { return vch + size(); }
116 const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
117
119 friend bool operator==(const CPubKey& a, const CPubKey& b)
120 {
121 return a.vch[0] == b.vch[0] &&
122 memcmp(a.vch, b.vch, a.size()) == 0;
123 }
124 friend bool operator<(const CPubKey& a, const CPubKey& b)
125 {
126 return a.vch[0] < b.vch[0] ||
127 (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
128 }
129 friend bool operator>(const CPubKey& a, const CPubKey& b)
130 {
131 return a.vch[0] > b.vch[0] ||
132 (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) > 0);
133 }
134
136 template <typename Stream>
137 void Serialize(Stream& s) const
138 {
139 unsigned int len = size();
140 ::WriteCompactSize(s, len);
141 s << std::span{vch, len};
142 }
143 template <typename Stream>
144 void Unserialize(Stream& s)
145 {
146 const unsigned int len(::ReadCompactSize(s));
147 if (len <= SIZE) {
148 s >> std::span{vch, len};
149 if (len != size()) {
150 Invalidate();
151 }
152 } else {
153 // invalid pubkey, skip available data
154 s.ignore(len);
155 Invalidate();
156 }
157 }
158
160 CKeyID GetID() const
161 {
162 return CKeyID(Hash160(std::span{vch}.first(size())));
163 }
164
167 {
168 return Hash(std::span{vch}.first(size()));
169 }
170
171 /*
172 * Check syntactic correctness.
173 *
174 * When setting a pubkey (Set()) or deserializing fails (its header bytes
175 * don't match the length of the data), the size is set to 0. Thus,
176 * by checking size, one can observe whether Set() or deserialization has
177 * failed.
178 *
179 * This does not check for more than that. In particular, it does not verify
180 * that the coordinates correspond to a point on the curve (see IsFullyValid()
181 * for that instead).
182 *
183 * Note that this is consensus critical as CheckECDSASignature() calls it!
184 */
185 bool IsValid() const
186 {
187 return size() > 0;
188 }
189
191 bool IsValidNonHybrid() const noexcept
192 {
193 return size() > 0 && (vch[0] == 0x02 || vch[0] == 0x03 || vch[0] == 0x04);
194 }
195
197 bool IsFullyValid() const;
198
200 bool IsCompressed() const
201 {
202 return size() == COMPRESSED_SIZE;
203 }
204
209 bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
210
214 static bool CheckLowS(const std::vector<unsigned char>& vchSig);
215
217 bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
218
220 bool Decompress();
221
223 [[nodiscard]] bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc, uint256* bip32_tweak_out = nullptr) const;
224};
225
227{
228private:
230
231public:
235 static const XOnlyPubKey NUMS_H;
236
238 XOnlyPubKey() = default;
239
240 XOnlyPubKey(const XOnlyPubKey&) = default;
242
246 bool IsFullyValid() const;
247
250 bool IsNull() const { return m_keydata.IsNull(); }
251
253 constexpr explicit XOnlyPubKey(std::span<const unsigned char> bytes) : m_keydata{bytes} {}
254
256 explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(std::span{pubkey}.subspan(1, 32)) {}
257
262 bool VerifySchnorr(const uint256& msg, std::span<const unsigned char> sigbytes) const;
263
272 uint256 ComputeTapTweakHash(const uint256* merkle_root) const;
273
276 bool CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const;
277
279 std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;
280
286 std::vector<CKeyID> GetKeyIDs() const;
288 std::vector<CPubKey> GetCPubKeys() const;
289
291
292 const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
293 static constexpr size_t size() { return decltype(m_keydata)::size(); }
294 const unsigned char* data() const { return m_keydata.begin(); }
295 const unsigned char* begin() const { return m_keydata.begin(); }
296 const unsigned char* end() const { return m_keydata.end(); }
297 unsigned char* data() { return m_keydata.begin(); }
298 unsigned char* begin() { return m_keydata.begin(); }
299 unsigned char* end() { return m_keydata.end(); }
300 bool operator==(const XOnlyPubKey& other) const { return m_keydata == other.m_keydata; }
301 bool operator<(const XOnlyPubKey& other) const { return m_keydata < other.m_keydata; }
302
304 SERIALIZE_METHODS(XOnlyPubKey, obj) { READWRITE(obj.m_keydata); }
305};
306
309{
310private:
311 static constexpr size_t SIZE = 64;
312 std::array<std::byte, SIZE> m_pubkey;
313
314public:
316 EllSwiftPubKey() noexcept = default;
317
319 EllSwiftPubKey(std::span<const std::byte> ellswift) noexcept;
320
322 CPubKey Decode() const;
323
324 // Read-only access for serialization.
325 const std::byte* data() const { return m_pubkey.data(); }
326 static constexpr size_t size() { return SIZE; }
327 auto begin() const { return m_pubkey.cbegin(); }
328 auto end() const { return m_pubkey.cend(); }
329
330 bool friend operator==(const EllSwiftPubKey& a, const EllSwiftPubKey& b)
331 {
332 return a.m_pubkey == b.m_pubkey;
333 }
334};
335
337 unsigned char version[4];
338 unsigned char nDepth;
339 unsigned char vchFingerprint[4];
340 unsigned int nChild;
343
344 friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
345 {
346 return a.nDepth == b.nDepth &&
347 memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
348 a.nChild == b.nChild &&
349 a.chaincode == b.chaincode &&
350 a.pubkey == b.pubkey;
351 }
352
353 friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)
354 {
355 if (a.pubkey < b.pubkey) {
356 return true;
357 } else if (a.pubkey > b.pubkey) {
358 return false;
359 }
360 return a.chaincode < b.chaincode;
361 }
362
363 void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
364 void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
365 void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const;
366 void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]);
367 [[nodiscard]] bool Derive(CExtPubKey& out, unsigned int nChild, uint256* bip32_tweak_out = nullptr) const;
368};
369
370#endif // BITCOIN_PUBKEY_H
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:24
CKeyID()
Definition: pubkey.h:26
CKeyID(const uint160 &in)
Definition: pubkey.h:27
An encapsulated public key.
Definition: pubkey.h:34
const unsigned char * data() const
Definition: pubkey.h:113
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:300
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:200
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:40
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:82
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
Definition: pubkey.cpp:424
CPubKey(std::span< const uint8_t > _vch)
Construct a public key from a byte vector.
Definition: pubkey.h:106
bool IsValid() const
Definition: pubkey.h:185
friend bool operator>(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:129
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:327
const unsigned char * end() const
Definition: pubkey.h:115
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:283
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:39
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:320
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:112
const unsigned char * begin() const
Definition: pubkey.h:114
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc, uint256 *bip32_tweak_out=nullptr) const
Derive BIP32 child pubkey.
Definition: pubkey.cpp:341
static bool ValidSize(const std::vector< unsigned char > &vch)
Definition: pubkey.h:77
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:60
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:119
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:137
unsigned char vch[SIZE]
see www.keylength.com script supports up to 75 for single byte push
Definition: pubkey.h:57
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:100
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:70
void Unserialize(Stream &s)
Definition: pubkey.h:144
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:166
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:116
static constexpr unsigned int SIGNATURE_SIZE
Definition: pubkey.h:41
bool IsValidNonHybrid() const noexcept
Check if a public key is a syntactically valid compressed or uncompressed key.
Definition: pubkey.h:191
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:42
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:89
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:124
unsigned char * end()
Definition: pubkey.h:299
unsigned char * begin()
Definition: pubkey.h:298
XOnlyPubKey & operator=(const XOnlyPubKey &)=default
const unsigned char * end() const
Definition: pubkey.h:296
std::vector< CPubKey > GetCPubKeys() const
Returns this XOnlyPubKey with 0x02 and 0x03 prefixes.
Definition: pubkey.cpp:200
bool IsNull() const
Test whether this is the 0 key (the result of default construction).
Definition: pubkey.h:250
unsigned char * data()
Definition: pubkey.h:297
const unsigned char * begin() const
Definition: pubkey.h:295
std::optional< std::pair< XOnlyPubKey, bool > > CreateTapTweak(const uint256 *merkle_root) const
Construct a Taproot tweaked output point with this point as internal key.
Definition: pubkey.cpp:265
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,...
Definition: pubkey.cpp:257
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...
Definition: pubkey.h:235
static constexpr size_t size()
Definition: pubkey.h:293
constexpr XOnlyPubKey(std::span< const unsigned char > bytes)
Construct an x-only pubkey from exactly 32 bytes.
Definition: pubkey.h:253
const unsigned char * data() const
Definition: pubkey.h:294
bool VerifySchnorr(const uint256 &msg, std::span< const unsigned char > sigbytes) const
Verify a Schnorr signature against this public key.
Definition: pubkey.cpp:236
SERIALIZE_METHODS(XOnlyPubKey, obj)
Implement serialization without length prefixes since it is a fixed length.
Definition: pubkey.h:304
CPubKey GetEvenCorrespondingCPubKey() const
Definition: pubkey.cpp:223
uint256 m_keydata
Definition: pubkey.h:229
XOnlyPubKey(const XOnlyPubKey &)=default
XOnlyPubKey(const CPubKey &pubkey)
Construct an x-only pubkey from a normal pubkey.
Definition: pubkey.h:256
bool operator<(const XOnlyPubKey &other) const
Definition: pubkey.h:301
bool IsFullyValid() const
Determine if this pubkey is fully valid.
Definition: pubkey.cpp:230
std::vector< CKeyID > GetKeyIDs() const
Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
Definition: pubkey.cpp:214
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
Definition: pubkey.cpp:246
const unsigned char & operator[](int pos) const
Definition: pubkey.h:292
XOnlyPubKey()=default
Construct an empty x-only pubkey.
bool operator==(const XOnlyPubKey &other) const
Definition: pubkey.h:300
constexpr bool IsNull() const
Definition: uint256.h:48
constexpr unsigned char * end()
Definition: uint256.h:101
constexpr unsigned char * begin()
Definition: uint256.h:100
160-bit opaque blob.
Definition: uint256.h:183
256-bit opaque blob.
Definition: uint256.h:195
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE
Definition: pubkey.h:20
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
uint256 ChainCode
Definition: pubkey.h:30
void WriteCompactSize(SizeComputer &os, uint64_t nSize)
Definition: serialize.h:1089
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:330
#define READWRITE(...)
Definition: serialize.h:145
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:344
unsigned char version[4]
Definition: pubkey.h:337
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: pubkey.cpp:385
ChainCode chaincode
Definition: pubkey.h:341
void DecodeWithVersion(const unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE])
Definition: pubkey.cpp:409
void EncodeWithVersion(unsigned char code[BIP32_EXTKEY_WITH_VERSION_SIZE]) const
Definition: pubkey.cpp:403
unsigned char vchFingerprint[4]
Definition: pubkey.h:339
unsigned char nDepth
Definition: pubkey.h:338
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: pubkey.cpp:394
CPubKey pubkey
Definition: pubkey.h:342
unsigned int nChild
Definition: pubkey.h:340
friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:353
bool Derive(CExtPubKey &out, unsigned int nChild, uint256 *bip32_tweak_out=nullptr) const
Definition: pubkey.cpp:415
An ElligatorSwift-encoded public key.
Definition: pubkey.h:309
auto begin() const
Definition: pubkey.h:327
bool friend operator==(const EllSwiftPubKey &a, const EllSwiftPubKey &b)
Definition: pubkey.h:330
std::array< std::byte, SIZE > m_pubkey
Definition: pubkey.h:312
CPubKey Decode() const
Decode to normal compressed CPubKey (for debugging purposes).
Definition: pubkey.cpp:371
static constexpr size_t SIZE
Definition: pubkey.h:311
static constexpr size_t size()
Definition: pubkey.h:326
const std::byte * data() const
Definition: pubkey.h:325
EllSwiftPubKey() noexcept=default
Default constructor creates all-zero pubkey (which is valid).
auto end() const
Definition: pubkey.h:328