PIVX Core  5.6.99
P2P Digital Currency
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin developers
3 // Copyright (c) 2015-2021 The PIVX Core 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 PIVX_KEY_H
8 #define PIVX_KEY_H
9 
10 #include "serialize.h"
12 #include "uint256.h"
13 #include "pubkey.h"
14 
15 #include <stdexcept>
16 #include <vector>
17 
18 class CPubKey;
19 
20 struct CExtPubKey;
21 
26 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
27 
29 class CKey
30 {
31 public:
35  static const unsigned int PRIVATE_KEY_SIZE = 279;
36  static const unsigned int COMPRESSED_PRIVATE_KEY_SIZE = 214;
41  static_assert(
43  "COMPRESSED_PRIVATE_KEY_SIZE is larger than PRIVATE_KEY_SIZE");
44 
45 private:
48  bool fValid;
49 
52 
54  std::vector<unsigned char, secure_allocator<unsigned char> > keydata;
55 
57  bool static Check(const unsigned char* vch);
58 
59 public:
62  {
63  // Important: vch must be 32 bytes in length to not break serialization
64  keydata.resize(32);
65  }
66 
67  friend bool operator==(const CKey& a, const CKey& b)
68  {
69  return a.fCompressed == b.fCompressed &&
70  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  {
78  if (size_t(pend - pbegin) != keydata.size()) {
79  fValid = false;
80  } else if (Check(&pbegin[0])) {
81  memcpy(keydata.data(), (unsigned char*)&pbegin[0], keydata.size());
82  fValid = true;
83  fCompressed = fCompressedIn;
84  } else {
85  fValid = false;
86  }
87 }
88 
90  unsigned int size() const { return (fValid ? keydata.size() : 0); }
91  const unsigned char* begin() const { return keydata.data(); }
92  const unsigned char* end() const { return keydata.data() + size(); }
93 
95  bool IsValid() const { return fValid; }
96 
98  bool IsCompressed() const { return fCompressed; }
99 
101  void MakeNewKey(bool fCompressed);
102 
104 
109  CPrivKey GetPrivKey() const;
110 
115  CPubKey GetPubKey() const;
116 
121  bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, uint32_t test_case = 0) const;
122 
130  bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
131 
133  bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
134 
139  bool VerifyPubKey(const CPubKey& vchPubKey) const;
140 
142  bool Load(const CPrivKey& privkey, const CPubKey& vchPubKey, bool fSkipCheck);
143 
145  static bool CheckSignatureElement(const unsigned char* vch, int len, bool half);
146 };
147 
148 struct CExtKey {
149  unsigned char nDepth;
150  unsigned char vchFingerprint[4];
151  unsigned int nChild;
154 
155  friend bool operator==(const CExtKey& a, const CExtKey& b)
156  {
157  return a.nDepth == b.nDepth &&
158  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
159  a.nChild == b.nChild &&
160  a.chaincode == b.chaincode &&
161  a.key == b.key;
162  }
163 
164  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
165  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
166  bool Derive(CExtKey& out, unsigned int nChild) const;
167  CExtPubKey Neuter() const;
168  void SetSeed(const unsigned char* seed, unsigned int nSeedLen);
169 };
170 
172 void ECC_Start(void);
173 
175 void ECC_Stop(void);
176 
178 bool ECC_InitSanityCheck(void);
179 
180 #endif // PIVX_KEY_H
false
Definition: bls_dkg.cpp:151
An encapsulated private key.
Definition: key.h:30
static const unsigned int COMPRESSED_PRIVATE_KEY_SIZE
Definition: key.h:36
uint256 GetPrivKey_256()
Definition: key.cpp:167
friend bool operator==(const CKey &a, const CKey &b)
Definition: key.h:67
const unsigned char * end() const
Definition: key.h:92
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:90
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:95
static bool CheckSignatureElement(const unsigned char *vch, int len, bool half)
Check whether an element of a signature (r or s) is valid.
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition: key.h:43
CKey()
Construct an invalid private key.
Definition: key.h:61
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized secp256k1 private key data).
Definition: key.cpp:173
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:98
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:158
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:51
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:186
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:200
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:76
const unsigned char * begin() const
Definition: key.h:91
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:54
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:216
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:247
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to be vch is valid keydata.
Definition: key.cpp:153
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:260
static const unsigned int PRIVATE_KEY_SIZE
secp256k1:
Definition: key.h:35
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:231
An encapsulated public key.
Definition: pubkey.h:44
256-bit opaque blob.
Definition: uint256.h:138
void * memcpy(void *a, const void *b, size_t c)
#define T(expected, seed, data)
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (PRIVATE_KEY_SIZE bytes)
Definition: key.h:20
bool ECC_InitSanityCheck(void)
Check that required EC support is available at runtime.
Definition: key.cpp:336
void ECC_Start(void)
Initialize the elliptic curve support.
Definition: key.cpp:344
void ECC_Stop(void)
Deinitialize the elliptic curve support.
Definition: key.cpp:361
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:17
Definition: key.h:148
unsigned char vchFingerprint[4]
Definition: key.h:150
CExtPubKey Neuter() const
Definition: key.cpp:302
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:281
void SetSeed(const unsigned char *seed, unsigned int nSeedLen)
Definition: key.cpp:290
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:327
CKey key
Definition: key.h:153
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:313
unsigned char nDepth
Definition: key.h:149
ChainCode chaincode
Definition: key.h:152
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:155
unsigned int nChild
Definition: key.h:151