Bitcoin ABC  0.22.13
P2P Digital Currency
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2016 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>
12 #include <uint256.h>
13 
14 #include <stdexcept>
15 #include <vector>
16 
22 typedef std::vector<uint8_t, secure_allocator<uint8_t>> CPrivKey;
23 
25 class CKey {
26 public:
30  static const unsigned int SIZE = 279;
31  static const unsigned int COMPRESSED_SIZE = 214;
36  static_assert(SIZE >= COMPRESSED_SIZE,
37  "COMPRESSED_SIZE is larger than SIZE");
38 
39 private:
43  bool fValid;
44 
48 
50  std::vector<uint8_t, secure_allocator<uint8_t>> keydata;
51 
53  static bool Check(const uint8_t *vch);
54 
55 public:
57  CKey() : fValid(false), fCompressed(false) {
58  // Important: vch must be 32 bytes in length to not break serialization
59  keydata.resize(32);
60  }
61 
62  friend bool operator==(const CKey &a, const CKey &b) {
63  return a.fCompressed == b.fCompressed && a.size() == b.size() &&
64  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
65  }
66 
68  template <typename T>
69  void Set(const T pbegin, const T pend, bool fCompressedIn) {
70  if (size_t(pend - pbegin) != keydata.size()) {
71  fValid = false;
72  } else if (Check(&pbegin[0])) {
73  memcpy(keydata.data(), (uint8_t *)&pbegin[0], keydata.size());
74  fValid = true;
75  fCompressed = fCompressedIn;
76  } else {
77  fValid = false;
78  }
79  }
80 
82  unsigned int size() const { return (fValid ? keydata.size() : 0); }
83  const uint8_t *begin() const { return keydata.data(); }
84  const uint8_t *end() const { return keydata.data() + size(); }
85 
87  bool IsValid() const { return fValid; }
88 
91  bool IsCompressed() const { return fCompressed; }
92 
94  void MakeNewKey(bool fCompressed);
95 
97  bool Negate();
98 
104  CPrivKey GetPrivKey() const;
105 
110  CPubKey GetPubKey() const;
111 
116  bool SignECDSA(const uint256 &hash, std::vector<uint8_t> &vchSig,
117  bool grind = true, uint32_t test_case = 0) const;
118 
123  bool SignSchnorr(const uint256 &hash,
124  std::array<uint8_t, CPubKey::SCHNORR_SIZE> &sig,
125  uint32_t test_case = 0) const;
126  bool SignSchnorr(const uint256 &hash, std::vector<uint8_t> &vchSig,
127  uint32_t test_case = 0) const;
128 
140  bool SignCompact(const uint256 &hash, std::vector<uint8_t> &vchSig) const;
141 
143  bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild,
144  const ChainCode &cc) const;
145 
151  bool VerifyPubKey(const CPubKey &vchPubKey) const;
152 
154  bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey,
155  bool fSkipCheck);
156 };
157 
158 struct CExtKey {
159  uint8_t nDepth;
160  uint8_t vchFingerprint[4];
161  unsigned int nChild;
164 
165  friend bool operator==(const CExtKey &a, const CExtKey &b) {
166  return a.nDepth == b.nDepth &&
167  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0],
168  sizeof(vchFingerprint)) == 0 &&
169  a.nChild == b.nChild && a.chaincode == b.chaincode &&
170  a.key == b.key;
171  }
172 
173  void Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const;
174  void Decode(const uint8_t code[BIP32_EXTKEY_SIZE]);
175  bool Derive(CExtKey &out, unsigned int nChild) const;
176  CExtPubKey Neuter() const;
177  void SetSeed(const uint8_t *seed, unsigned int nSeedLen);
178 
179  CExtKey() = default;
180 };
181 
186 void ECC_Start();
187 
192 void ECC_Stop();
193 
195 bool ECC_InitSanityCheck();
196 
197 #endif // BITCOIN_KEY_H
std::vector< uint8_t, secure_allocator< uint8_t > > keydata
The actual byte data.
Definition: key.h:50
bool SignECDSA(const uint256 &hash, std::vector< uint8_t > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized ECDSA signature.
Definition: key.cpp:242
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:196
std::array< uint8_t, 64 > sig
Definition: processor.cpp:250
CKey key
Definition: key.h:163
bool Negate()
Negate private key.
Definition: key.cpp:191
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:455
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:303
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
Definition: key.h:158
static const unsigned int SIZE
secp256k1:
Definition: key.h:30
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition: key.h:37
std::vector< uint8_t, secure_allocator< uint8_t > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition: key.h:22
static const unsigned int COMPRESSED_SIZE
Definition: key.h:31
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:165
An encapsulated public key.
Definition: pubkey.h:31
static bool Check(const uint8_t *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:179
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
unsigned int nChild
Definition: key.h:161
uint8_t vchFingerprint[4]
Definition: key.h:160
bool SignCompact(const uint256 &hash, std::vector< uint8_t > &vchSig) const
Create a compact ECDSA signature (65 bytes), which allows reconstructing the used public key...
Definition: key.cpp:320
std::pair< std::string, data > Decode(const std::string &str, const std::string &default_prefix)
Decode a cashaddr string.
Definition: cashaddr.cpp:214
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:82
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:91
ChainCode chaincode
Definition: key.h:162
const uint8_t * begin() const
Definition: key.h:83
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:69
256-bit opaque blob.
Definition: uint256.h:120
uint8_t nDepth
Definition: key.h:159
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:356
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
std::string Encode(const std::string &prefix, const data &payload)
Encode a cashaddr string.
Definition: cashaddr.cpp:198
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:47
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:431
CKey()
Construct an invalid private key.
Definition: key.h:57
const uint8_t * end() const
Definition: key.h:84
An encapsulated secp256k1 private key.
Definition: key.h:25
bool SignSchnorr(const uint256 &hash, std::array< uint8_t, CPubKey::SCHNORR_SIZE > &sig, uint32_t test_case=0) const
Create a Schnorr signature.
Definition: key.cpp:288
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:340
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:62
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:87
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:438