Dogecoin Core  1.14.2
P2P Digital Currency
pubkey.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 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 
6 #ifndef BITCOIN_PUBKEY_H
7 #define BITCOIN_PUBKEY_H
8 
9 #include "hash.h"
10 #include "serialize.h"
11 #include "uint256.h"
12 
13 #include <stdexcept>
14 #include <vector>
15 
26 const unsigned int BIP32_EXTKEY_SIZE = 74;
27 
29 class CKeyID : public uint160
30 {
31 public:
32  CKeyID() : uint160() {}
33  CKeyID(const uint160& in) : uint160(in) {}
34 };
35 
37 
39 class CPubKey
40 {
41 private:
42 
47  unsigned char vch[65];
48 
50  unsigned int static GetLen(unsigned char chHeader)
51  {
52  if (chHeader == 2 || chHeader == 3)
53  return 33;
54  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
55  return 65;
56  return 0;
57  }
58 
60  void Invalidate()
61  {
62  vch[0] = 0xFF;
63  }
64 
65 public:
68  {
69  Invalidate();
70  }
71 
73  template <typename T>
74  void Set(const T pbegin, const T pend)
75  {
76  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
77  if (len && len == (pend - pbegin))
78  memcpy(vch, (unsigned char*)&pbegin[0], len);
79  else
80  Invalidate();
81  }
82 
84  template <typename T>
85  CPubKey(const T pbegin, const T pend)
86  {
87  Set(pbegin, pend);
88  }
89 
91  CPubKey(const std::vector<unsigned char>& _vch)
92  {
93  Set(_vch.begin(), _vch.end());
94  }
95 
97  unsigned int size() const { return GetLen(vch[0]); }
98  const unsigned char* begin() const { return vch; }
99  const unsigned char* end() const { return vch + size(); }
100  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
101 
103  friend bool operator==(const CPubKey& a, const CPubKey& b)
104  {
105  return a.vch[0] == b.vch[0] &&
106  memcmp(a.vch, b.vch, a.size()) == 0;
107  }
108  friend bool operator!=(const CPubKey& a, const CPubKey& b)
109  {
110  return !(a == b);
111  }
112  friend bool operator<(const CPubKey& a, const CPubKey& b)
113  {
114  return a.vch[0] < b.vch[0] ||
115  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
116  }
117 
119  template <typename Stream>
120  void Serialize(Stream& s) const
121  {
122  unsigned int len = size();
123  ::WriteCompactSize(s, len);
124  s.write((char*)vch, len);
125  }
126  template <typename Stream>
127  void Unserialize(Stream& s)
128  {
129  unsigned int len = ::ReadCompactSize(s);
130  if (len <= 65) {
131  s.read((char*)vch, len);
132  } else {
133  // invalid pubkey, skip available data
134  char dummy;
135  while (len--)
136  s.read(&dummy, 1);
137  Invalidate();
138  }
139  }
140 
142  CKeyID GetID() const
143  {
144  return CKeyID(Hash160(vch, vch + size()));
145  }
146 
148  uint256 GetHash() const
149  {
150  return Hash(vch, vch + size());
151  }
152 
153  /*
154  * Check syntactic correctness.
155  *
156  * Note that this is consensus critical as CheckSig() calls it!
157  */
158  bool IsValid() const
159  {
160  return size() > 0;
161  }
162 
164  bool IsFullyValid() const;
165 
167  bool IsCompressed() const
168  {
169  return size() == 33;
170  }
171 
176  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
177 
181  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
182 
184  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
185 
187  bool Compress();
188 
190  bool Decompress();
191 
193  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
194 };
195 
196 struct CExtPubKey {
197  unsigned char nDepth;
198  unsigned char vchFingerprint[4];
199  unsigned int nChild;
202 
203  friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
204  {
205  return a.nDepth == b.nDepth &&
206  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
207  a.nChild == b.nChild &&
208  a.chaincode == b.chaincode &&
209  a.pubkey == b.pubkey;
210  }
211 
212  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
213  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
214  bool Derive(CExtPubKey& out, unsigned int nChild) const;
215 
216  void Serialize(CSizeComputer& s) const
217  {
218  // Optimized implementation for ::GetSerializeSize that avoids copying.
219  s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
220  }
221  template <typename Stream>
222  void Serialize(Stream& s) const
223  {
224  unsigned int len = BIP32_EXTKEY_SIZE;
225  ::WriteCompactSize(s, len);
226  unsigned char code[BIP32_EXTKEY_SIZE];
227  Encode(code);
228  s.write((const char *)&code[0], len);
229  }
230  template <typename Stream>
231  void Unserialize(Stream& s)
232  {
233  unsigned int len = ::ReadCompactSize(s);
234  unsigned char code[BIP32_EXTKEY_SIZE];
235  if (len != BIP32_EXTKEY_SIZE)
236  throw std::runtime_error("Invalid extended key size\n");
237  s.read((char *)&code[0], len);
238  Decode(code);
239  }
240 };
241 
245 {
246  static int refcount;
247 
248 public:
249  ECCVerifyHandle();
251 };
252 
253 #endif // BITCOIN_PUBKEY_H
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:30
CKeyID()
Definition: pubkey.h:32
CKeyID(const uint160 &in)
Definition: pubkey.h:33
An encapsulated public key.
Definition: pubkey.h:40
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:187
const unsigned char * end() const
Definition: pubkey.h:99
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:167
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:142
unsigned char vch[65]
Just store the serialized data.
Definition: pubkey.h:47
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:67
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
Definition: pubkey.cpp:288
bool IsValid() const
Definition: pubkey.h:158
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:227
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:167
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:207
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:97
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:50
const unsigned char * begin() const
Definition: pubkey.h:98
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:103
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:120
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:85
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:60
void Unserialize(Stream &s)
Definition: pubkey.h:127
bool Compress()
Turn this public key into a compressed public key.
Definition: pubkey.cpp:214
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:148
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:100
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
Definition: pubkey.cpp:241
CPubKey(const std::vector< unsigned char > &_vch)
Construct a public key from a byte vector.
Definition: pubkey.h:91
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:74
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:108
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:112
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:868
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:245
static int refcount
Definition: pubkey.h:246
160-bit opaque blob.
Definition: uint256.h:111
256-bit opaque blob.
Definition: uint256.h:123
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:107
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:70
const unsigned int BIP32_EXTKEY_SIZE
secp256k1: const unsigned int PRIVATE_KEY_SIZE = 279; const unsigned int PUBLIC_KEY_SIZE = 65; const ...
Definition: pubkey.h:26
uint256 ChainCode
Definition: pubkey.h:36
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:245
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:942
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:203
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: pubkey.cpp:262
ChainCode chaincode
Definition: pubkey.h:200
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:280
unsigned char vchFingerprint[4]
Definition: pubkey.h:198
unsigned char nDepth
Definition: pubkey.h:197
void Serialize(CSizeComputer &s) const
Definition: pubkey.h:216
void Serialize(Stream &s) const
Definition: pubkey.h:222
void Unserialize(Stream &s)
Definition: pubkey.h:231
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: pubkey.cpp:272
CPubKey pubkey
Definition: pubkey.h:201
unsigned int nChild
Definition: pubkey.h:199