PIVX Core  5.6.99
P2P Digital Currency
pubkey.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) 2016-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_PUBKEY_H
8 #define PIVX_PUBKEY_H
9 
10 #include "hash.h"
11 #include "serialize.h"
12 #include "uint256.h"
13 
14 #include <stdexcept>
15 #include <vector>
16 
17 const unsigned int BIP32_EXTKEY_SIZE = 74;
18 
20 class CKeyID : public uint160
21 {
22 public:
23  CKeyID() : uint160() {}
24  explicit CKeyID(const uint160& in) : uint160(in) {}
25 };
26 
29 class CExchangeKeyID : public uint160
30 {
31 public:
33  explicit CExchangeKeyID(const uint160& in) : uint160(in) {}
34 
35  operator CKeyID() const {
36  return CKeyID(static_cast<const uint160&>(*this));
37  }
38 };
39 
41 
43 class CPubKey
44 {
45 public:
49  static constexpr unsigned int PUBLIC_KEY_SIZE = 65;
50  static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
51  static constexpr unsigned int SIGNATURE_SIZE = 72;
52  static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
57  static_assert(
59  "COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
60 
61 private:
66  unsigned char vch[PUBLIC_KEY_SIZE];
67 
69  unsigned int static GetLen(unsigned char chHeader)
70  {
71  if (chHeader == 2 || chHeader == 3)
73  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
74  return PUBLIC_KEY_SIZE;
75  return 0;
76  }
77 
79  void Invalidate()
80  {
81  vch[0] = 0xFF;
82  }
83 
84 public:
85 
86  bool static ValidSize(const std::vector<unsigned char> &vch) {
87  return vch.size() > 0 && GetLen(vch[0]) == vch.size();
88  }
89 
92  {
93  Invalidate();
94  }
95 
97  template <typename T>
98  void Set(const T pbegin, const T pend)
99  {
100  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
101  if (len && len == (pend - pbegin))
102  memcpy(vch, (unsigned char*)&pbegin[0], len);
103  else
104  Invalidate();
105  }
106 
108  template <typename T>
109  CPubKey(const T pbegin, const T pend)
110  {
111  Set(pbegin, pend);
112  }
113 
115  explicit CPubKey(const std::vector<unsigned char>& _vch)
116  {
117  Set(_vch.begin(), _vch.end());
118  }
119 
121  unsigned int size() const { return GetLen(vch[0]); }
122  const unsigned char* data() const { return vch; }
123  const unsigned char* begin() const { return vch; }
124  const unsigned char* end() const { return vch + size(); }
125  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
126 
128  friend bool operator==(const CPubKey& a, const CPubKey& b)
129  {
130  return a.vch[0] == b.vch[0] &&
131  memcmp(a.vch, b.vch, a.size()) == 0;
132  }
133  friend bool operator!=(const CPubKey& a, const CPubKey& b)
134  {
135  return !(a == b);
136  }
137  friend bool operator<(const CPubKey& a, const CPubKey& b)
138  {
139  return a.vch[0] < b.vch[0] ||
140  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
141  }
142 
144  template <typename Stream>
145  void Serialize(Stream& s) const
146  {
147  unsigned int len = size();
148  ::WriteCompactSize(s, len);
149  s.write((char*)vch, len);
150  }
151  template <typename Stream>
152  void Unserialize(Stream& s)
153  {
154  unsigned int len = ::ReadCompactSize(s);
155  if (len <= PUBLIC_KEY_SIZE) {
156  s.read((char*)vch, len);
157  } else {
158  // invalid pubkey, skip available data
159  char dummy;
160  while (len--)
161  s.read(&dummy, 1);
162  Invalidate();
163  }
164  }
165 
167  CKeyID GetID() const
168  {
169  return CKeyID(Hash160(vch, vch + size()));
170  }
171 
173  uint256 GetHash() const
174  {
175  return Hash(vch, vch + size());
176  }
177 
178  /*
179  * Check syntactic correctness.
180  *
181  * Note that this is consensus critical as CheckSig() calls it!
182  */
183  bool IsValid() const
184  {
185  return size() > 0;
186  }
187 
189  bool IsFullyValid() const;
190 
192  bool IsCompressed() const
193  {
194  return size() == COMPRESSED_PUBLIC_KEY_SIZE;
195  }
196 
201  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
202 
206  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
207 
209  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
210 
212  bool Decompress();
213 
215  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
216 
217  std::vector<unsigned char> Raw() const
218  {
219  return std::vector<unsigned char>(vch, vch + size());
220  }
221 
222 };
223 
224 struct CExtPubKey {
225  unsigned char nDepth;
226  unsigned char vchFingerprint[4];
227  unsigned int nChild;
230 
231  friend bool operator==(const CExtPubKey& a, const CExtPubKey& b)
232  {
233  return a.nDepth == b.nDepth &&
234  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
235  a.nChild == b.nChild &&
236  a.chaincode == b.chaincode &&
237  a.pubkey == b.pubkey;
238  }
239 
240  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
241  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
242  bool Derive(CExtPubKey& out, unsigned int nChild) const;
243 
244  void Serialize(CSizeComputer& s) const
245  {
246  // Optimized implementation for ::GetSerializeSize that avoids copying.
247  s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
248  }
249  template <typename Stream>
250  void Serialize(Stream& s) const
251  {
252  unsigned int len = BIP32_EXTKEY_SIZE;
253  ::WriteCompactSize(s, len);
254  unsigned char code[BIP32_EXTKEY_SIZE];
255  Encode(code);
256  s.write((const char *)&code[0], len);
257  }
258  template <typename Stream>
259  void Unserialize(Stream& s)
260  {
261  unsigned int len = ::ReadCompactSize(s);
262  unsigned char code[BIP32_EXTKEY_SIZE];
263  if (len != BIP32_EXTKEY_SIZE)
264  throw std::runtime_error("Invalid extended key size\n");
265  s.read((char *)&code[0], len);
266  Decode(code);
267  }
268 };
269 
273 {
274  static int refcount;
275 
276 public:
277  ECCVerifyHandle();
279 };
280 
281 #endif // PIVX_PUBKEY_H
A reference to a CKey: the Hash160 of its serialized public key, special case for exchange key.
Definition: pubkey.h:30
CExchangeKeyID(const uint160 &in)
Definition: pubkey.h:33
CExchangeKeyID()
Definition: pubkey.h:32
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
CKeyID()
Definition: pubkey.h:23
CKeyID(const uint160 &in)
Definition: pubkey.h:24
An encapsulated public key.
Definition: pubkey.h:44
std::vector< unsigned char > Raw() const
Definition: pubkey.h:217
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:188
const unsigned char * end() const
Definition: pubkey.h:124
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:192
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:167
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:91
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
Definition: pubkey.cpp:287
bool IsValid() const
Definition: pubkey.h:183
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:219
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:169
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:210
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:121
static bool ValidSize(const std::vector< unsigned char > &vch)
Definition: pubkey.h:86
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:69
const unsigned char * begin() const
Definition: pubkey.h:123
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:128
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:145
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:109
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:79
unsigned char vch[PUBLIC_KEY_SIZE]
see www.keylength.com script supports up to 75 for single byte push
Definition: pubkey.h:59
void Unserialize(Stream &s)
Definition: pubkey.h:152
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:173
static constexpr unsigned int PUBLIC_KEY_SIZE
secp256k1:
Definition: pubkey.h:49
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:125
const unsigned char * data() const
Definition: pubkey.h:122
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
Definition: pubkey.cpp:235
CPubKey(const std::vector< unsigned char > &_vch)
Construct a public key from a byte vector.
Definition: pubkey.h:115
static constexpr unsigned int SIGNATURE_SIZE
Definition: pubkey.h:51
static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE
Definition: pubkey.h:50
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:52
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:98
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:133
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:137
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:1351
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:273
static int refcount
Definition: pubkey.h:274
160-bit opaque blob.
Definition: uint256.h:127
256-bit opaque blob.
Definition: uint256.h:138
void * memcpy(void *a, const void *b, size_t c)
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:193
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:173
#define T(expected, seed, data)
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:17
uint256 ChainCode
Definition: pubkey.h:40
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:359
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1435
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:231
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: pubkey.cpp:258
ChainCode chaincode
Definition: pubkey.h:228
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:278
unsigned char vchFingerprint[4]
Definition: pubkey.h:226
unsigned char nDepth
Definition: pubkey.h:225
void Serialize(CSizeComputer &s) const
Definition: pubkey.h:244
void Serialize(Stream &s) const
Definition: pubkey.h:250
void Unserialize(Stream &s)
Definition: pubkey.h:259
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: pubkey.cpp:269
CPubKey pubkey
Definition: pubkey.h:229
unsigned int nChild
Definition: pubkey.h:227