Bitcoin Core  0.18.99
P2P Digital Currency
pubkey.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2018 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_PUBKEY_H
8 #define BITCOIN_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 
28 
30 class CPubKey
31 {
32 public:
36  static constexpr unsigned int PUBLIC_KEY_SIZE = 65;
37  static constexpr unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
38  static constexpr unsigned int SIGNATURE_SIZE = 72;
39  static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
44  static_assert(
45  PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
46  "COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
47 
48 private:
49 
54  unsigned char vch[PUBLIC_KEY_SIZE];
55 
57  unsigned int static GetLen(unsigned char chHeader)
58  {
59  if (chHeader == 2 || chHeader == 3)
60  return COMPRESSED_PUBLIC_KEY_SIZE;
61  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
62  return PUBLIC_KEY_SIZE;
63  return 0;
64  }
65 
67  void Invalidate()
68  {
69  vch[0] = 0xFF;
70  }
71 
72 public:
73 
74  bool static ValidSize(const std::vector<unsigned char> &vch) {
75  return vch.size() > 0 && GetLen(vch[0]) == vch.size();
76  }
77 
80  {
81  Invalidate();
82  }
83 
85  template <typename T>
86  void Set(const T pbegin, const T pend)
87  {
88  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
89  if (len && len == (pend - pbegin))
90  memcpy(vch, (unsigned char*)&pbegin[0], len);
91  else
92  Invalidate();
93  }
94 
96  template <typename T>
97  CPubKey(const T pbegin, const T pend)
98  {
99  Set(pbegin, pend);
100  }
101 
103  explicit CPubKey(const std::vector<unsigned char>& _vch)
104  {
105  Set(_vch.begin(), _vch.end());
106  }
107 
109  unsigned int size() const { return GetLen(vch[0]); }
110  const unsigned char* data() const { return vch; }
111  const unsigned char* begin() const { return vch; }
112  const unsigned char* end() const { return vch + size(); }
113  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
114 
116  friend bool operator==(const CPubKey& a, const CPubKey& b)
117  {
118  return a.vch[0] == b.vch[0] &&
119  memcmp(a.vch, b.vch, a.size()) == 0;
120  }
121  friend bool operator!=(const CPubKey& a, const CPubKey& b)
122  {
123  return !(a == b);
124  }
125  friend bool operator<(const CPubKey& a, const CPubKey& b)
126  {
127  return a.vch[0] < b.vch[0] ||
128  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
129  }
130 
132  template <typename Stream>
133  void Serialize(Stream& s) const
134  {
135  unsigned int len = size();
136  ::WriteCompactSize(s, len);
137  s.write((char*)vch, len);
138  }
139  template <typename Stream>
140  void Unserialize(Stream& s)
141  {
142  unsigned int len = ::ReadCompactSize(s);
143  if (len <= PUBLIC_KEY_SIZE) {
144  s.read((char*)vch, len);
145  } else {
146  // invalid pubkey, skip available data
147  char dummy;
148  while (len--)
149  s.read(&dummy, 1);
150  Invalidate();
151  }
152  }
153 
155  CKeyID GetID() const
156  {
157  return CKeyID(Hash160(vch, vch + size()));
158  }
159 
161  uint256 GetHash() const
162  {
163  return Hash(vch, vch + size());
164  }
165 
166  /*
167  * Check syntactic correctness.
168  *
169  * Note that this is consensus critical as CheckSig() calls it!
170  */
171  bool IsValid() const
172  {
173  return size() > 0;
174  }
175 
177  bool IsFullyValid() const;
178 
180  bool IsCompressed() const
181  {
182  return size() == COMPRESSED_PUBLIC_KEY_SIZE;
183  }
184 
189  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
190 
194  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
195 
197  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
198 
200  bool Decompress();
201 
203  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
204 };
205 
206 struct CExtPubKey {
207  unsigned char nDepth;
208  unsigned char vchFingerprint[4];
209  unsigned int nChild;
212 
213  friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
214  {
215  return a.nDepth == b.nDepth &&
216  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
217  a.nChild == b.nChild &&
218  a.chaincode == b.chaincode &&
219  a.pubkey == b.pubkey;
220  }
221 
222  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
223  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
224  bool Derive(CExtPubKey& out, unsigned int nChild) const;
225 
226  void Serialize(CSizeComputer& s) const
227  {
228  // Optimized implementation for ::GetSerializeSize that avoids copying.
229  s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
230  }
231  template <typename Stream>
232  void Serialize(Stream& s) const
233  {
234  unsigned int len = BIP32_EXTKEY_SIZE;
235  ::WriteCompactSize(s, len);
236  unsigned char code[BIP32_EXTKEY_SIZE];
237  Encode(code);
238  s.write((const char *)&code[0], len);
239  }
240  template <typename Stream>
241  void Unserialize(Stream& s)
242  {
243  unsigned int len = ::ReadCompactSize(s);
244  unsigned char code[BIP32_EXTKEY_SIZE];
245  if (len != BIP32_EXTKEY_SIZE)
246  throw std::runtime_error("Invalid extended key size\n");
247  s.read((char *)&code[0], len);
248  Decode(code);
249  }
250 };
251 
255 {
256  static int refcount;
257 
258 public:
259  ECCVerifyHandle();
260  ~ECCVerifyHandle();
261 };
262 
263 #endif // BITCOIN_PUBKEY_H
unsigned char vch[PUBLIC_KEY_SIZE]
see www.keylength.com script supports up to 75 for single byte push
Definition: pubkey.h:46
unsigned char vchFingerprint[4]
Definition: pubkey.h:208
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:57
uint256 ChainCode
Definition: pubkey.h:27
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:125
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:289
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:986
CKeyID(const uint160 &in)
Definition: pubkey.h:24
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:67
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:86
static int refcount
Definition: pubkey.h:256
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:161
unsigned char nDepth
Definition: pubkey.h:207
void Serialize(Stream &s) const
Definition: pubkey.h:232
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:113
void Unserialize(Stream &s)
Definition: pubkey.h:140
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:94
ChainCode chaincode
Definition: pubkey.h:210
unsigned int nChild
Definition: pubkey.h:209
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:116
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:155
const unsigned char * begin() const
Definition: pubkey.h:111
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:133
static bool ValidSize(const std::vector< unsigned char > &vch)
Definition: pubkey.h:74
void Unserialize(Stream &s)
Definition: pubkey.h:241
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:254
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:213
const unsigned char * end() const
Definition: pubkey.h:112
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:79
bool IsValid() const
Definition: pubkey.h:171
An encapsulated public key.
Definition: pubkey.h:30
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:71
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:109
unsigned int size() const
Definition: uint256.h:75
std::pair< std::string, data > Decode(const std::string &str)
Decode a Bech32 string.
Definition: bech32.cpp:159
CPubKey(const std::vector< unsigned char > &_vch)
Construct a public key from a byte vector.
Definition: pubkey.h:103
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:925
256-bit opaque blob.
Definition: uint256.h:121
CKeyID()
Definition: pubkey.h:23
void * memcpy(void *a, const void *b, size_t c)
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:17
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:97
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
160-bit opaque blob.
Definition: uint256.h:110
CPubKey pubkey
Definition: pubkey.h:211
void Serialize(CSizeComputer &s) const
Definition: pubkey.h:226
const unsigned char * data() const
Definition: pubkey.h:110
std::string Encode(const std::string &hrp, const data &values)
Encode a Bech32 string.
Definition: bech32.cpp:147
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:121
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:180