Bitcoin Core  22.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-2019 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 
24 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
25 
27 class CKey
28 {
29 public:
33  static const unsigned int SIZE = 279;
34  static const unsigned int COMPRESSED_SIZE = 214;
39  static_assert(
41  "COMPRESSED_SIZE is larger than SIZE");
42 
43 private:
46  bool fValid;
47 
50 
52  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
53 
55  bool static Check(const unsigned char* vch);
56 
57 public:
59  CKey() : fValid(false), fCompressed(false)
60  {
61  // Important: vch must be 32 bytes in length to not break serialization
62  keydata.resize(32);
63  }
64 
65  friend bool operator==(const CKey& a, const CKey& b)
66  {
67  return a.fCompressed == b.fCompressed &&
68  a.size() == b.size() &&
69  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
70  }
71 
73  template <typename T>
74  void Set(const T pbegin, const T pend, bool fCompressedIn)
75  {
76  if (size_t(pend - pbegin) != keydata.size()) {
77  fValid = false;
78  } else if (Check(&pbegin[0])) {
79  memcpy(keydata.data(), (unsigned char*)&pbegin[0], keydata.size());
80  fValid = true;
81  fCompressed = fCompressedIn;
82  } else {
83  fValid = false;
84  }
85  }
86 
88  unsigned int size() const { return (fValid ? keydata.size() : 0); }
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 
141  bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root = nullptr, const uint256* aux = nullptr) const;
142 
144  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
145 
150  bool VerifyPubKey(const CPubKey& vchPubKey) const;
151 
153  bool Load(const CPrivKey& privkey, const CPubKey& vchPubKey, bool fSkipCheck);
154 };
155 
156 struct CExtKey {
157  unsigned char nDepth;
158  unsigned char vchFingerprint[4];
159  unsigned int nChild;
162 
163  friend bool operator==(const CExtKey& a, const CExtKey& b)
164  {
165  return a.nDepth == b.nDepth &&
166  memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
167  a.nChild == b.nChild &&
168  a.chaincode == b.chaincode &&
169  a.key == b.key;
170  }
171 
172  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
173  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
174  bool Derive(CExtKey& out, unsigned int nChild) const;
175  CExtPubKey Neuter() const;
176  void SetSeed(const unsigned char* seed, unsigned int nSeedLen);
177 };
178 
180 void ECC_Start();
181 
183 void ECC_Stop();
184 
186 bool ECC_InitSanityCheck();
187 
188 #endif // BITCOIN_KEY_H
CKey::IsCompressed
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
BIP32_EXTKEY_SIZE
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
CKey::operator==
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:65
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
CExtKey::Derive
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:314
CKey::GetPrivKey
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:174
CExtKey::vchFingerprint
unsigned char vchFingerprint[4]
Definition: key.h:158
uint256.h
CKey::Set
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:74
CPrivKey
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:24
CKey::fCompressed
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:49
CExtKey::Encode
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:343
CKey::Derive
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:294
pubkey.h
ECC_InitSanityCheck
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:362
CExtKey::nDepth
unsigned char nDepth
Definition: key.h:157
CKey::end
const unsigned char * end() const
Definition: key.h:90
CKey::CKey
CKey()
Construct an invalid private key.
Definition: key.h:59
CExtKey::operator==
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:163
CKey::SignSchnorr
bool SignSchnorr(const uint256 &hash, Span< unsigned char > sig, const uint256 *merkle_root=nullptr, const uint256 *aux=nullptr) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
Definition: key.cpp:264
CKey::SIZE
static const unsigned int SIZE
secp256k1:
Definition: key.h:33
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
CKey::begin
const unsigned char * begin() const
Definition: key.h:89
CExtKey
Definition: key.h:156
CKey::SignCompact
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:249
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:93
CKey::Sign
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
uint256
256-bit opaque blob.
Definition: uint256.h:124
CExtKey::nChild
unsigned int nChild
Definition: key.h:159
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
CKey::Negate
bool Negate()
Negate private key.
Definition: key.cpp:168
secure.h
CExtKey::chaincode
ChainCode chaincode
Definition: key.h:160
CExtKey::SetSeed
void SetSeed(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:322
CPubKey
An encapsulated public key.
Definition: pubkey.h:32
CKey::size
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:88
CKey
An encapsulated private key.
Definition: key.h:27
CKey::VerifyPubKey
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:235
CExtKey::Decode
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:354
serialize.h
CKey::fValid
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition: key.h:41
CKey::Load
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:282
ECC_Stop
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:386
CExtKey::Neuter
CExtPubKey Neuter() const
Definition: key.cpp:333
CKey::COMPRESSED_SIZE
static const unsigned int COMPRESSED_SIZE
Definition: key.h:34
CKey::Check
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:156
CExtPubKey
Definition: pubkey.h:282
CExtKey::key
CKey key
Definition: key.h:161
ECC_Start
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:369
CKey::keydata
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:52