Coverage Report

Created: 2026-04-29 19:21

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/tmp/bitcoin/src/pubkey.h
Line
Count
Source
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
19
const unsigned int BIP32_EXTKEY_SIZE = 74;
20
const unsigned int BIP32_EXTKEY_WITH_VERSION_SIZE = 78;
21
22
/** A reference to a CKey: the Hash160 of its serialized public key */
23
class CKeyID : public uint160
24
{
25
public:
26
489k
    CKeyID() : uint160() {}
27
6.06M
    explicit CKeyID(const uint160& in) : uint160(in) {}
28
};
29
30
typedef uint256 ChainCode;
31
32
/** An encapsulated public key. */
33
class CPubKey
34
{
35
public:
36
    /**
37
     * secp256k1:
38
     */
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;
43
    /**
44
     * see www.keylength.com
45
     * script supports up to 75 for single byte push
46
     */
47
    static_assert(
48
        SIZE >= COMPRESSED_SIZE,
49
        "COMPRESSED_SIZE is larger than SIZE");
50
51
private:
52
53
    /**
54
     * Just store the serialized data.
55
     * Its length can very cheaply be computed from the first byte.
56
     */
57
    unsigned char vch[SIZE];
58
59
    //! Compute the length of a pubkey with a given first byte.
60
    unsigned int static GetLen(unsigned char chHeader)
61
24.1M
    {
62
24.1M
        if (chHeader == 2 || chHeader == 3)
63
24.0M
            return COMPRESSED_SIZE;
64
122k
        if (chHeader == 4 || chHeader == 6 || chHeader == 7)
65
59.7k
            return SIZE;
66
63.1k
        return 0;
67
122k
    }
68
69
    //! Set this key data to be invalid
70
    void Invalidate()
71
2.93M
    {
72
2.93M
        vch[0] = 0xFF;
73
2.93M
    }
74
75
public:
76
77
28.2k
    bool static ValidSize(const std::vector<unsigned char> &vch) {
78
28.2k
      return vch.size() > 0 && GetLen(vch[0]) == vch.size();
79
28.2k
    }
80
81
    //! Construct an invalid public key.
82
    CPubKey()
83
2.91M
    {
84
2.91M
        Invalidate();
85
2.91M
    }
86
87
    //! Initialize a public key using begin/end iterators to byte data.
88
    template <typename T>
89
    void Set(const T pbegin, const T pend)
90
1.93M
    {
91
1.93M
        int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
92
1.93M
        if (len && len == (pend - pbegin))
93
1.90M
            memcpy(vch, (unsigned char*)&pbegin[0], len);
94
22.2k
        else
95
22.2k
            Invalidate();
96
1.93M
    }
void CPubKey::Set<__gnu_cxx::__normal_iterator<unsigned char const*, std::span<unsigned char const, 18446744073709551615ul>::__iter_tag>>(__gnu_cxx::__normal_iterator<unsigned char const*, std::span<unsigned char const, 18446744073709551615ul>::__iter_tag>, __gnu_cxx::__normal_iterator<unsigned char const*, std::span<unsigned char const, 18446744073709551615ul>::__iter_tag>)
Line
Count
Source
90
410k
    {
91
410k
        int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
92
410k
        if (len && len == (pend - pbegin))
93
387k
            memcpy(vch, (unsigned char*)&pbegin[0], len);
94
22.1k
        else
95
22.1k
            Invalidate();
96
410k
    }
void CPubKey::Set<__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char>>>>(__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char>>>, __gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char>>>)
Line
Count
Source
90
2.49k
    {
91
2.49k
        int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
92
2.49k
        if (len && len == (pend - pbegin))
93
2.49k
            memcpy(vch, (unsigned char*)&pbegin[0], len);
94
4
        else
95
4
            Invalidate();
96
2.49k
    }
void CPubKey::Set<__gnu_cxx::__normal_iterator<unsigned char const*, std::vector<unsigned char, std::allocator<unsigned char>>>>(__gnu_cxx::__normal_iterator<unsigned char const*, std::vector<unsigned char, std::allocator<unsigned char>>>, __gnu_cxx::__normal_iterator<unsigned char const*, std::vector<unsigned char, std::allocator<unsigned char>>>)
Line
Count
Source
90
769
    {
91
769
        int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
92
769
        if (len && len == (pend - pbegin))
93
769
            memcpy(vch, (unsigned char*)&pbegin[0], len);
94
0
        else
95
0
            Invalidate();
96
769
    }
void CPubKey::Set<unsigned char*>(unsigned char*, unsigned char*)
Line
Count
Source
90
1.49M
    {
91
1.49M
        int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
92
1.49M
        if (len && len == (pend - pbegin))
93
1.49M
            memcpy(vch, (unsigned char*)&pbegin[0], len);
94
4
        else
95
4
            Invalidate();
96
1.49M
    }
void CPubKey::Set<unsigned char const*>(unsigned char const*, unsigned char const*)
Line
Count
Source
90
24.6k
    {
91
24.6k
        int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
92
24.6k
        if (len && len == (pend - pbegin))
93
24.6k
            memcpy(vch, (unsigned char*)&pbegin[0], len);
94
3
        else
95
3
            Invalidate();
96
24.6k
    }
97
98
    //! Construct a public key using begin/end iterators to byte data.
99
    template <typename T>
100
    CPubKey(const T pbegin, const T pend)
101
3.96k
    {
102
3.96k
        Set(pbegin, pend);
103
3.96k
    }
CPubKey::CPubKey<__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char>>>>(__gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char>>>, __gnu_cxx::__normal_iterator<unsigned char*, std::vector<unsigned char, std::allocator<unsigned char>>>)
Line
Count
Source
101
2.49k
    {
102
2.49k
        Set(pbegin, pend);
103
2.49k
    }
CPubKey::CPubKey<__gnu_cxx::__normal_iterator<unsigned char const*, std::vector<unsigned char, std::allocator<unsigned char>>>>(__gnu_cxx::__normal_iterator<unsigned char const*, std::vector<unsigned char, std::allocator<unsigned char>>>, __gnu_cxx::__normal_iterator<unsigned char const*, std::vector<unsigned char, std::allocator<unsigned char>>>)
Line
Count
Source
101
769
    {
102
769
        Set(pbegin, pend);
103
769
    }
CPubKey::CPubKey<unsigned char*>(unsigned char*, unsigned char*)
Line
Count
Source
101
700
    {
102
700
        Set(pbegin, pend);
103
700
    }
104
105
    //! Construct a public key from a byte vector.
106
    explicit CPubKey(std::span<const uint8_t> _vch)
107
410k
    {
108
410k
        Set(_vch.begin(), _vch.end());
109
410k
    }
110
111
    //! Simple read-only vector-like interface to the pubkey data.
112
22.1M
    unsigned int size() const { return GetLen(vch[0]); }
113
2.18M
    const unsigned char* data() const { return vch; }
114
1.95M
    const unsigned char* begin() const { return vch; }
115
158k
    const unsigned char* end() const { return vch + size(); }
116
39.8k
    const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
117
118
    //! Comparator implementation.
119
    friend bool operator==(const CPubKey& a, const CPubKey& b)
120
70.3k
    {
121
70.3k
        return a.vch[0] == b.vch[0] &&
122
70.3k
               memcmp(a.vch, b.vch, a.size()) == 0;
123
70.3k
    }
124
    friend bool operator<(const CPubKey& a, const CPubKey& b)
125
11.2M
    {
126
11.2M
        return a.vch[0] < b.vch[0] ||
127
11.2M
               (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
128
11.2M
    }
129
    friend bool operator>(const CPubKey& a, const CPubKey& b)
130
594
    {
131
594
        return a.vch[0] > b.vch[0] ||
132
594
               (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) > 0);
133
594
    }
134
135
    //! Implement serialization, as if this was a byte vector.
136
    template <typename Stream>
137
    void Serialize(Stream& s) const
138
5.19k
    {
139
5.19k
        unsigned int len = size();
140
5.19k
        ::WriteCompactSize(s, len);
141
5.19k
        s << std::span{vch, len};
142
5.19k
    }
void CPubKey::Serialize<DataStream>(DataStream&) const
Line
Count
Source
138
4.78k
    {
139
4.78k
        unsigned int len = size();
140
4.78k
        ::WriteCompactSize(s, len);
141
4.78k
        s << std::span{vch, len};
142
4.78k
    }
void CPubKey::Serialize<HashWriter>(HashWriter&) const
Line
Count
Source
138
410
    {
139
410
        unsigned int len = size();
140
410
        ::WriteCompactSize(s, len);
141
410
        s << std::span{vch, len};
142
410
    }
143
    template <typename Stream>
144
    void Unserialize(Stream& s)
145
2.87k
    {
146
2.87k
        const unsigned int len(::ReadCompactSize(s));
147
2.87k
        if (len <= SIZE) {
148
2.87k
            s >> std::span{vch, len};
149
2.87k
            if (len != size()) {
150
6
                Invalidate();
151
6
            }
152
2.87k
        } else {
153
            // invalid pubkey, skip available data
154
0
            s.ignore(len);
155
0
            Invalidate();
156
0
        }
157
2.87k
    }
158
159
    //! Get the KeyID of this public key (hash of its serialization)
160
    CKeyID GetID() const
161
5.66M
    {
162
5.66M
        return CKeyID(Hash160(std::span{vch}.first(size())));
163
5.66M
    }
164
165
    //! Get the 256-bit hash of this public key.
166
    uint256 GetHash() const
167
3.27k
    {
168
3.27k
        return Hash(std::span{vch}.first(size()));
169
3.27k
    }
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
1.40M
    {
187
1.40M
        return size() > 0;
188
1.40M
    }
189
190
    /** Check if a public key is a syntactically valid compressed or uncompressed key. */
191
    bool IsValidNonHybrid() const noexcept
192
326k
    {
193
326k
        return size() > 0 && (vch[0] == 0x02 || vch[0] == 0x03 || vch[0] == 0x04);
194
326k
    }
195
196
    //! fully validate whether this is a valid public key (more expensive than IsValid())
197
    bool IsFullyValid() const;
198
199
    //! Check whether this is a compressed public key.
200
    bool IsCompressed() const
201
234k
    {
202
234k
        return size() == COMPRESSED_SIZE;
203
234k
    }
204
205
    /**
206
     * Verify a DER signature (~72 bytes).
207
     * If this public key is not fully valid, the return value will be false.
208
     */
209
    bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
210
211
    /**
212
     * Check whether a signature is normalized (lower-S).
213
     */
214
    static bool CheckLowS(const std::vector<unsigned char>& vchSig);
215
216
    //! Recover a public key from a compact signature.
217
    bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
218
219
    //! Turn this public key into an uncompressed public key.
220
    bool Decompress();
221
222
    //! Derive BIP32 child pubkey.
223
    [[nodiscard]] bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc, uint256* bip32_tweak_out = nullptr) const;
224
};
225
226
class XOnlyPubKey
227
{
228
private:
229
    uint256 m_keydata;
230
231
public:
232
    /** Nothing Up My Sleeve point H
233
     *  Used as an internal key for provably disabling the key path spend
234
     *  see BIP341 for more details */
235
    static const XOnlyPubKey NUMS_H;
236
237
    /** Construct an empty x-only pubkey. */
238
1.49M
    XOnlyPubKey() = default;
239
240
    XOnlyPubKey(const XOnlyPubKey&) = default;
241
    XOnlyPubKey& operator=(const XOnlyPubKey&) = default;
242
243
    /** Determine if this pubkey is fully valid. This is true for approximately 50% of all
244
     *  possible 32-byte arrays. If false, VerifySchnorr, CheckTapTweak and CreateTapTweak
245
     *  will always fail. */
246
    bool IsFullyValid() const;
247
248
    /** Test whether this is the 0 key (the result of default construction). This implies
249
     *  !IsFullyValid(). */
250
53.1k
    bool IsNull() const { return m_keydata.IsNull(); }
251
252
    /** Construct an x-only pubkey from exactly 32 bytes. */
253
1.96M
    constexpr explicit XOnlyPubKey(std::span<const unsigned char> bytes) : m_keydata{bytes} {}
254
255
    /** Construct an x-only pubkey from a normal pubkey. */
256
1.39M
    explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(std::span{pubkey}.subspan(1, 32)) {}
257
258
    /** Verify a Schnorr signature against this public key.
259
     *
260
     * sigbytes must be exactly 64 bytes.
261
     */
262
    bool VerifySchnorr(const uint256& msg, std::span<const unsigned char> sigbytes) const;
263
264
    /** Compute the Taproot tweak as specified in BIP341, with *this as internal
265
     * key:
266
     *  - if merkle_root == nullptr: H_TapTweak(xonly_pubkey)
267
     *  - otherwise:                 H_TapTweak(xonly_pubkey || *merkle_root)
268
     *
269
     * Note that the behavior of this function with merkle_root != nullptr is
270
     * consensus critical.
271
     */
272
    uint256 ComputeTapTweakHash(const uint256* merkle_root) const;
273
274
    /** Verify that this is a Taproot tweaked output point, against a specified internal key,
275
     *  Merkle root, and parity. */
276
    bool CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const;
277
278
    /** Construct a Taproot tweaked output point with this point as internal key. */
279
    std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;
280
281
    /** Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
282
     * As the CKeyID is the Hash160(full pubkey), the produced CKeyIDs are for the versions of this
283
     * XOnlyPubKey with 0x02 and 0x03 prefixes.
284
     * This is needed for key lookups since keys are indexed by CKeyID.
285
     */
286
    std::vector<CKeyID> GetKeyIDs() const;
287
    /** Returns this XOnlyPubKey with 0x02 and 0x03 prefixes */
288
    std::vector<CPubKey> GetCPubKeys() const;
289
290
    CPubKey GetEvenCorrespondingCPubKey() const;
291
292
0
    const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
293
275k
    static constexpr size_t size() { return decltype(m_keydata)::size(); }
294
496k
    const unsigned char* data() const { return m_keydata.begin(); }
295
1.33M
    const unsigned char* begin() const { return m_keydata.begin(); }
296
1.33M
    const unsigned char* end() const { return m_keydata.end(); }
297
7
    unsigned char* data() { return m_keydata.begin(); }
298
266k
    unsigned char* begin() { return m_keydata.begin(); }
299
6
    unsigned char* end() { return m_keydata.end(); }
300
14.1k
    bool operator==(const XOnlyPubKey& other) const { return m_keydata == other.m_keydata; }
301
2.85M
    bool operator<(const XOnlyPubKey& other) const { return m_keydata < other.m_keydata; }
302
303
    //! Implement serialization without length prefixes since it is a fixed length
304
13.7k
    SERIALIZE_METHODS(XOnlyPubKey, obj) { READWRITE(obj.m_keydata); }
void XOnlyPubKey::SerializationOps<SpanReader, XOnlyPubKey, ActionUnserialize>(XOnlyPubKey&, SpanReader&, ActionUnserialize)
Line
Count
Source
304
5.68k
    SERIALIZE_METHODS(XOnlyPubKey, obj) { READWRITE(obj.m_keydata); }
Unexecuted instantiation: void XOnlyPubKey::SerializationOps<DataStream, XOnlyPubKey, ActionUnserialize>(XOnlyPubKey&, DataStream&, ActionUnserialize)
void XOnlyPubKey::SerializationOps<SizeComputer, XOnlyPubKey const, ActionSerialize>(XOnlyPubKey const&, SizeComputer&, ActionSerialize)
Line
Count
Source
304
4.03k
    SERIALIZE_METHODS(XOnlyPubKey, obj) { READWRITE(obj.m_keydata); }
void XOnlyPubKey::SerializationOps<DataStream, XOnlyPubKey const, ActionSerialize>(XOnlyPubKey const&, DataStream&, ActionSerialize)
Line
Count
Source
304
4.03k
    SERIALIZE_METHODS(XOnlyPubKey, obj) { READWRITE(obj.m_keydata); }
305
};
306
307
/** An ElligatorSwift-encoded public key. */
308
struct EllSwiftPubKey
309
{
310
private:
311
    static constexpr size_t SIZE = 64;
312
    std::array<std::byte, SIZE> m_pubkey;
313
314
public:
315
    /** Default constructor creates all-zero pubkey (which is valid). */
316
    EllSwiftPubKey() noexcept = default;
317
318
    /** Construct a new ellswift public key from a given serialization. */
319
    EllSwiftPubKey(std::span<const std::byte> ellswift) noexcept;
320
321
    /** Decode to normal compressed CPubKey (for debugging purposes). */
322
    CPubKey Decode() const;
323
324
    // Read-only access for serialization.
325
897
    const std::byte* data() const { return m_pubkey.data(); }
326
2.05k
    static constexpr size_t size() { return SIZE; }
327
246
    auto begin() const { return m_pubkey.cbegin(); }
328
246
    auto end() const { return m_pubkey.cend(); }
329
330
    bool friend operator==(const EllSwiftPubKey& a, const EllSwiftPubKey& b)
331
98
    {
332
98
        return a.m_pubkey == b.m_pubkey;
333
98
    }
334
};
335
336
struct CExtPubKey {
337
    unsigned char version[4];
338
    unsigned char nDepth;
339
    unsigned char vchFingerprint[4];
340
    unsigned int nChild;
341
    ChainCode chaincode;
342
    CPubKey pubkey;
343
344
    friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
345
33
    {
346
33
        return a.nDepth == b.nDepth &&
347
33
            memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
348
33
            a.nChild == b.nChild &&
349
33
            a.chaincode == b.chaincode &&
350
33
            a.pubkey == b.pubkey;
351
33
    }
352
353
    friend bool operator<(const CExtPubKey &a, const CExtPubKey &b)
354
616
    {
355
616
        if (a.pubkey < b.pubkey) {
356
22
            return true;
357
594
        } else if (a.pubkey > b.pubkey) {
358
32
            return false;
359
32
        }
360
562
        return a.chaincode < b.chaincode;
361
616
    }
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