Bitcoin ABC  0.25.8
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 using SchnorrSig = std::array<uint8_t, CPubKey::SCHNORR_SIZE>;
26 
28 class CKey {
29 public:
33  static const unsigned int SIZE = 279;
34  static const unsigned int COMPRESSED_SIZE = 214;
39  static_assert(SIZE >= COMPRESSED_SIZE,
40  "COMPRESSED_SIZE is larger than SIZE");
41 
42 private:
46  bool fValid;
47 
51 
53  std::vector<uint8_t, secure_allocator<uint8_t>> keydata;
54 
56  static bool Check(const uint8_t *vch);
57 
58 public:
60  CKey() : fValid(false), fCompressed(false) {
61  // Important: vch must be 32 bytes in length to not break serialization
62  keydata.resize(32);
63  }
65  static CKey MakeCompressedKey();
67  static CKey MakeUncompressedKey();
68 
69  friend bool operator==(const CKey &a, const CKey &b) {
70  return a.fCompressed == b.fCompressed && a.size() == b.size() &&
71  memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
72  }
73 
75  template <typename T>
76  void Set(const T pbegin, const T pend, bool fCompressedIn) {
77  if (size_t(pend - pbegin) != keydata.size()) {
78  fValid = false;
79  } else if (Check(&pbegin[0])) {
80  memcpy(keydata.data(), (uint8_t *)&pbegin[0], keydata.size());
81  fValid = true;
82  fCompressed = fCompressedIn;
83  } else {
84  fValid = false;
85  }
86  }
87 
89  unsigned int size() const { return (fValid ? keydata.size() : 0); }
90  const uint8_t *begin() const { return keydata.data(); }
91  const uint8_t *end() const { return keydata.data() + size(); }
92 
94  bool IsValid() const { return fValid; }
95 
98  bool IsCompressed() const { return fCompressed; }
99 
101  void MakeNewKey(bool fCompressed);
102 
104  bool Negate();
105 
111  CPrivKey GetPrivKey() const;
112 
117  CPubKey GetPubKey() const;
118 
123  bool SignECDSA(const uint256 &hash, std::vector<uint8_t> &vchSig,
124  bool grind = true, uint32_t test_case = 0) const;
125 
130  bool SignSchnorr(const uint256 &hash, SchnorrSig &sig,
131  uint32_t test_case = 0) const;
132  bool SignSchnorr(const uint256 &hash, std::vector<uint8_t> &vchSig,
133  uint32_t test_case = 0) const;
134 
146  bool SignCompact(const uint256 &hash, std::vector<uint8_t> &vchSig) const;
147 
149  bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild,
150  const ChainCode &cc) const;
151 
157  bool VerifyPubKey(const CPubKey &vchPubKey) const;
158 
160  bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey,
161  bool fSkipCheck);
162 };
163 
164 struct CExtKey {
165  uint8_t nDepth;
166  uint8_t vchFingerprint[4];
167  unsigned int nChild;
170 
171  friend bool operator==(const CExtKey &a, const CExtKey &b) {
172  return a.nDepth == b.nDepth &&
173  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0],
174  sizeof(vchFingerprint)) == 0 &&
175  a.nChild == b.nChild && a.chaincode == b.chaincode &&
176  a.key == b.key;
177  }
178 
179  void Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const;
180  void Decode(const uint8_t code[BIP32_EXTKEY_SIZE]);
181  bool Derive(CExtKey &out, unsigned int nChild) const;
182  CExtPubKey Neuter() const;
183  void SetSeed(const uint8_t *seed, unsigned int nSeedLen);
184 
185  CExtKey() = default;
186 };
187 
192 void ECC_Start();
193 
198 void ECC_Stop();
199 
201 bool ECC_InitSanityCheck();
202 
203 #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:98
BIP32_EXTKEY_SIZE
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
CKey::SignSchnorr
bool SignSchnorr(const uint256 &hash, SchnorrSig &sig, uint32_t test_case=0) const
Create a Schnorr signature.
Definition: key.cpp:288
CKey::operator==
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:69
CExtKey::Encode
void Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:406
CKey::MakeNewKey
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:183
CKey::SignCompact
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:316
CKey::keydata
std::vector< uint8_t, secure_allocator< uint8_t > > keydata
The actual byte data.
Definition: key.h:53
CKey::begin
const uint8_t * begin() const
Definition: key.h:90
CExtKey::Derive
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:374
CKey::GetPrivKey
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:196
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:76
CKey::fCompressed
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:50
CKey::Derive
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:352
pubkey.h
ECC_InitSanityCheck
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:427
CPrivKey
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
CKey::CKey
CKey()
Construct an invalid private key.
Definition: key.h:60
CExtKey::operator==
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:171
CKey::SIZE
static const unsigned int SIZE
secp256k1:
Definition: key.h:33
sig
SchnorrSig sig
Definition: processor.cpp:410
CKey::MakeUncompressedKey
static CKey MakeUncompressedKey()
Produce a valid uncompressed key.
Definition: key.cpp:470
CExtKey
Definition: key.h:164
CExtKey::CExtKey
CExtKey()=default
CKey::Check
static bool Check(const uint8_t *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:179
CKey::IsValid
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:94
SchnorrSig
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
Definition: key.h:25
CExtKey::SetSeed
void SetSeed(const uint8_t *seed, unsigned int nSeedLen)
Definition: key.cpp:382
uint256
256-bit opaque blob.
Definition: uint256.h:127
CExtKey::nChild
unsigned int nChild
Definition: key.h:167
CKey::GetPubKey
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:210
CExtKey::vchFingerprint
uint8_t vchFingerprint[4]
Definition: key.h:166
CExtKey::nDepth
uint8_t nDepth
Definition: key.h:165
CKey::Negate
bool Negate()
Negate private key.
Definition: key.cpp:191
secure.h
CExtKey::chaincode
ChainCode chaincode
Definition: key.h:168
CPubKey
An encapsulated public key.
Definition: pubkey.h:31
CKey::size
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:89
CKey
An encapsulated secp256k1 private key.
Definition: key.h:28
CKey::VerifyPubKey
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:302
CKey::MakeCompressedKey
static CKey MakeCompressedKey()
Produce a valid compressed key.
Definition: key.cpp:466
CKey::end
const uint8_t * end() const
Definition: key.h:91
CKey::fValid
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition: key.h:40
CKey::Load
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:336
ECC_Stop
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:451
CExtKey::Neuter
CExtPubKey Neuter() const
Definition: key.cpp:396
CKey::COMPRESSED_SIZE
static const unsigned int COMPRESSED_SIZE
Definition: key.h:34
CExtPubKey
Definition: pubkey.h:198
CExtKey::key
CKey key
Definition: key.h:169
ECC_Start
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:434
CKey::SignECDSA
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
CExtKey::Decode
void Decode(const uint8_t code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:419