Dash Core Source Documentation (0.16.0.1)

Find detailed information regarding the Dash Core source code.

pubkey.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 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 const unsigned int PUBLIC_KEY_SIZE = 65;
37  static const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
38  static const unsigned int SIGNATURE_SIZE = 72;
39  static const unsigned int COMPACT_SIGNATURE_SIZE = 65;
44  static_assert(
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)
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:
75  {
76  Invalidate();
77  }
78 
80  template <typename T>
81  void Set(const T pbegin, const T pend)
82  {
83  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
84  if (len && len == (pend - pbegin))
85  memcpy(vch, (unsigned char*)&pbegin[0], len);
86  else
87  Invalidate();
88  }
89 
91  template <typename T>
92  CPubKey(const T pbegin, const T pend)
93  {
94  Set(pbegin, pend);
95  }
96 
98  explicit CPubKey(const std::vector<unsigned char>& _vch)
99  {
100  Set(_vch.begin(), _vch.end());
101  }
102 
104  unsigned int size() const { return GetLen(vch[0]); }
105  const unsigned char* begin() const { return vch; }
106  const unsigned char* end() const { return vch + size(); }
107  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
108 
110  friend bool operator==(const CPubKey& a, const CPubKey& b)
111  {
112  return a.vch[0] == b.vch[0] &&
113  memcmp(a.vch, b.vch, a.size()) == 0;
114  }
115  friend bool operator!=(const CPubKey& a, const CPubKey& b)
116  {
117  return !(a == b);
118  }
119  friend bool operator<(const CPubKey& a, const CPubKey& b)
120  {
121  return a.vch[0] < b.vch[0] ||
122  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
123  }
124 
126  template <typename Stream>
127  void Serialize(Stream& s) const
128  {
129  unsigned int len = size();
130  ::WriteCompactSize(s, len);
131  s.write((char*)vch, len);
132  }
133  template <typename Stream>
134  void Unserialize(Stream& s)
135  {
136  unsigned int len = ::ReadCompactSize(s);
137  if (len <= PUBLIC_KEY_SIZE) {
138  s.read((char*)vch, len);
139  } else {
140  // invalid pubkey, skip available data
141  char dummy;
142  while (len--)
143  s.read(&dummy, 1);
144  Invalidate();
145  }
146  }
147 
149  CKeyID GetID() const
150  {
151  return CKeyID(Hash160(vch, vch + size()));
152  }
153 
155  uint256 GetHash() const
156  {
157  return Hash(vch, vch + size());
158  }
159 
160  /*
161  * Check syntactic correctness.
162  *
163  * Note that this is consensus critical as CheckSig() calls it!
164  */
165  bool IsValid() const
166  {
167  return size() > 0;
168  }
169 
171  bool IsFullyValid() const;
172 
174  bool IsCompressed() const
175  {
176  return size() == COMPRESSED_PUBLIC_KEY_SIZE;
177  }
178 
183  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
184 
188  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
189 
191  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
192 
194  bool Decompress();
195 
197  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
198 };
199 
200 struct CExtPubKey {
201  unsigned char nDepth;
202  unsigned char vchFingerprint[4];
203  unsigned int nChild;
206 
207  friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
208  {
209  return a.nDepth == b.nDepth &&
210  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
211  a.nChild == b.nChild &&
212  a.chaincode == b.chaincode &&
213  a.pubkey == b.pubkey;
214  }
215 
216  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
217  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
218  bool Derive(CExtPubKey& out, unsigned int nChild) const;
219 
220  void Serialize(CSizeComputer& s) const
221  {
222  // Optimized implementation for ::GetSerializeSize that avoids copying.
223  s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
224  }
225  template <typename Stream>
226  void Serialize(Stream& s) const
227  {
228  unsigned int len = BIP32_EXTKEY_SIZE;
229  ::WriteCompactSize(s, len);
230  unsigned char code[BIP32_EXTKEY_SIZE];
231  Encode(code);
232  s.write((const char *)&code[0], len);
233  }
234  template <typename Stream>
235  void Unserialize(Stream& s)
236  {
237  unsigned int len = ::ReadCompactSize(s);
238  unsigned char code[BIP32_EXTKEY_SIZE];
239  if (len != BIP32_EXTKEY_SIZE)
240  throw std::runtime_error("Invalid extended key size\n");
241  s.read((char *)&code[0], len);
242  Decode(code);
243  }
244 };
245 
249 {
250  static int refcount;
251 
252 public:
253  ECCVerifyHandle();
255 };
256 
257 #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:202
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:119
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:261
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1289
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:81
static int refcount
Definition: pubkey.h:250
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:155
unsigned char nDepth
Definition: pubkey.h:201
void Serialize(Stream &s) const
Definition: pubkey.h:226
static bool CheckLowS(const std::vector< unsigned char > &vchSig)
Check whether a signature is normalized (lower-S).
Definition: pubkey.cpp:274
bool Derive(CPubKey &pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child pubkey.
Definition: pubkey.cpp:227
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:107
void Unserialize(Stream &s)
Definition: pubkey.h:134
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:161
ChainCode chaincode
Definition: pubkey.h:204
unsigned int nChild
Definition: pubkey.h:203
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:110
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:149
const unsigned char * begin() const
Definition: pubkey.h:105
static const unsigned int PUBLIC_KEY_SIZE
secp256k1:
Definition: pubkey.h:36
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:127
void Unserialize(Stream &s)
Definition: pubkey.h:235
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:248
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:207
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: pubkey.cpp:248
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: pubkey.cpp:258
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid()) ...
Definition: pubkey.cpp:206
const unsigned char * end() const
Definition: pubkey.h:106
static const unsigned int COMPRESSED_PUBLIC_KEY_SIZE
Definition: pubkey.h:37
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:186
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:74
bool IsValid() const
Definition: pubkey.h:165
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:84
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:104
CPubKey(const std::vector< unsigned char > &_vch)
Construct a public key from a byte vector.
Definition: pubkey.h:98
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:1215
256-bit opaque blob.
Definition: uint256.h:123
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:266
CKeyID()
Definition: pubkey.h:23
static const unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:39
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:169
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:92
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:20
160-bit opaque blob.
Definition: uint256.h:112
CPubKey pubkey
Definition: pubkey.h:205
void Serialize(CSizeComputer &s) const
Definition: pubkey.h:220
static const unsigned int SIGNATURE_SIZE
Definition: pubkey.h:38
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:115
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:213
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:174
Released under the MIT license