Bitcoin Core  24.99.0
P2P Digital Currency
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2022 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_KEY_H
8 #define BITCOIN_KEY_H
9 
10 #include <pubkey.h>
11 #include <serialize.h>
13 #include <uint256.h>
14 
15 #include <stdexcept>
16 #include <vector>
17 
18 
23 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
24 
26 class CKey
27 {
28 public:
32  static const unsigned int SIZE = 279;
33  static const unsigned int COMPRESSED_SIZE = 214;
38  static_assert(
40  "COMPRESSED_SIZE is larger than SIZE");
41 
42 private:
45  bool fValid{false};
46 
48  bool fCompressed{false};
49 
51  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
52 
54  bool static Check(const unsigned char* vch);
55 
56 public:
58  CKey()
59  {
60  // Important: vch must be 32 bytes in length to not break serialization
61  keydata.resize(32);
62  }
63 
64  friend bool operator==(const CKey& a, const CKey& b)
65  {
66  return a.fCompressed == b.fCompressed &&
67  a.size() == b.size() &&
68  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
69  }
70 
72  template <typename T>
73  void Set(const T pbegin, const T pend, bool fCompressedIn)
74  {
75  if (size_t(pend - pbegin) != keydata.size()) {
76  fValid = false;
77  } else if (Check(&pbegin[0])) {
78  memcpy(keydata.data(), (unsigned char*)&pbegin[0], keydata.size());
79  fValid = true;
80  fCompressed = fCompressedIn;
81  } else {
82  fValid = false;
83  }
84  }
85 
87  unsigned int size() const { return (fValid ? keydata.size() : 0); }
88  const std::byte* data() const { return reinterpret_cast<const std::byte*>(keydata.data()); }
89  const unsigned char* begin() const { return keydata.data(); }
90  const unsigned char* end() const { return keydata.data() + size(); }
91 
93  bool IsValid() const { return fValid; }
94 
96  bool IsCompressed() const { return fCompressed; }
97 
99  void MakeNewKey(bool fCompressed);
100 
102  bool Negate();
103 
108  CPrivKey GetPrivKey() const;
109 
114  CPubKey GetPubKey() const;
115 
120  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, bool grind = true, uint32_t test_case = 0) const;
121 
129  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
130 
146  bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root, const uint256& aux) const;
147 
149  [[nodiscard]] bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
150 
155  bool VerifyPubKey(const CPubKey& vchPubKey) const;
156 
158  bool Load(const CPrivKey& privkey, const CPubKey& vchPubKey, bool fSkipCheck);
159 };
160 
161 struct CExtKey {
162  unsigned char nDepth;
163  unsigned char vchFingerprint[4];
164  unsigned int nChild;
167 
168  friend bool operator==(const CExtKey& a, const CExtKey& b)
169  {
170  return a.nDepth == b.nDepth &&
171  memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
172  a.nChild == b.nChild &&
173  a.chaincode == b.chaincode &&
174  a.key == b.key;
175  }
176 
177  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
178  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
179  [[nodiscard]] bool Derive(CExtKey& out, unsigned int nChild) const;
180  CExtPubKey Neuter() const;
181  void SetSeed(Span<const std::byte> seed);
182 };
183 
185 void ECC_Start();
186 
188 void ECC_Stop();
189 
191 bool ECC_InitSanityCheck();
192 
193 #endif // BITCOIN_KEY_H
An encapsulated private key.
Definition: key.h:27
bool SignSchnorr(const uint256 &hash, Span< unsigned char > sig, const uint256 *merkle_root, const uint256 &aux) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
Definition: key.cpp:277
bool Negate()
Negate private key.
Definition: key.cpp:168
static const unsigned int SIZE
secp256k1:
Definition: key.h:32
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:64
const unsigned char * end() const
Definition: key.h:90
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:87
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:93
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:213
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition: key.h:45
CKey()
Construct an invalid private key.
Definition: key.h:58
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:174
static const unsigned int COMPRESSED_SIZE
Definition: key.h:33
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:48
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
const std::byte * data() const
Definition: key.h:88
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
const unsigned char * begin() const
Definition: key.h:89
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:51
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:241
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:302
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:156
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:314
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key.
Definition: key.cpp:254
An encapsulated public key.
Definition: pubkey.h:34
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:97
256-bit opaque blob.
Definition: uint256.h:105
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
Definition: key.h:23
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:384
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:391
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:408
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
Definition: key.h:161
unsigned char vchFingerprint[4]
Definition: key.h:163
CExtPubKey Neuter() const
Definition: key.cpp:355
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:334
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:375
CKey key
Definition: key.h:166
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:365
unsigned char nDepth
Definition: key.h:162
ChainCode chaincode
Definition: key.h:165
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:168
unsigned int nChild
Definition: key.h:164
void SetSeed(Span< const std::byte > seed)
Definition: key.cpp:343