Coverage Report

Created: 2026-07-14 18:13

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