PRCYCoin  2.0.0.7rc1
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 // 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(0) {}
33  CKeyID(const uint160& in) : uint160(in) {}
34 };
35 
37 class CPubKey
38 {
39 private:
44  unsigned char vch[65];
45 
47  unsigned int static GetLen(unsigned char chHeader)
48  {
49  if (chHeader == 2 || chHeader == 3)
50  return 33;
51  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
52  return 65;
53  return 0;
54  }
55 
57  void Invalidate()
58  {
59  vch[0] = 0xFF;
60  }
61 
62 public:
65  {
66  Invalidate();
67  }
68 
70  template <typename T>
71  void Set(const T pbegin, const T pend)
72  {
73  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
74  if (len && len == (pend - pbegin))
75  memcpy(vch, (unsigned char*)&pbegin[0], len);
76  else
77  Invalidate();
78  }
79 
81  template <typename T>
82  CPubKey(const T pbegin, const T pend)
83  {
84  Set(pbegin, pend);
85  }
86 
88  CPubKey(const std::vector<unsigned char>& vch)
89  {
90  Set(vch.begin(), vch.end());
91  }
92 
94  unsigned int size() const { return GetLen(vch[0]); }
95  const unsigned char* begin() const { return vch; }
96  const unsigned char* end() const { return vch + size(); }
97  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
98 
100  friend bool operator==(const CPubKey& a, const CPubKey& b)
101  {
102  return a.vch[0] == b.vch[0] &&
103  memcmp(a.vch, b.vch, a.size()) == 0;
104  }
105  friend bool operator!=(const CPubKey& a, const CPubKey& b)
106  {
107  return !(a == b);
108  }
109  friend bool operator<(const CPubKey& a, const CPubKey& b)
110  {
111  return a.vch[0] < b.vch[0] ||
112  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
113  }
114 
116  unsigned int GetSerializeSize(int nType, int nVersion) const
117  {
118  return size() + 1;
119  }
120  template <typename Stream>
121  void Serialize(Stream& s, int nType, int nVersion) const
122  {
123  unsigned int len = size();
124  ::WriteCompactSize(s, len);
125  s.write((char*)vch, len);
126  }
127  template <typename Stream>
128  void Unserialize(Stream& s, int nType, int nVersion)
129  {
130  unsigned int len = ::ReadCompactSize(s);
131  if (len <= 65) {
132  s.read((char*)vch, len);
133  } else {
134  // invalid pubkey, skip available data
135  char dummy;
136  while (len--)
137  s.read(&dummy, 1);
138  Invalidate();
139  }
140  }
141 
143  CKeyID GetID() const
144  {
145  return CKeyID(Hash160(vch, vch + size()));
146  }
147 
149  uint256 GetHash() const
150  {
151  return Hash(vch, vch + size());
152  }
153 
154  /*
155  * Check syntactic correctness.
156  *
157  * Note that this is consensus critical as CheckSig() calls it!
158  */
159  bool IsValid() const
160  {
161  return size() > 0;
162  }
163 
165  bool IsFullyValid() const;
166 
168  bool IsCompressed() const
169  {
170  return size() == 33;
171  }
172 
177  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
178 
180  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
181 
183  bool Decompress();
184 
186  bool Derive(CPubKey& pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const;
187 
188  std::vector<unsigned char> Raw() const
189  {
190  return std::vector<unsigned char>(vch, vch + size());
191  }
192 
193  std::string GetHex() const
194  {
195  unsigned int sz = size();
196  char psz[sz * 2 + 1];
197  for (unsigned int i = 0; i < sz; i++)
198  sprintf(psz + i * 2, "%02x", vch[sz - i - 1]);
199  return std::string(psz, psz + sz * 2);
200  }
201 };
202 
204 
205 struct CExtPubKey {
206  unsigned char nDepth;
207  unsigned char vchFingerprint[4];
208  unsigned int nChild;
209  unsigned char vchChainCode[32];
211 
212  friend bool operator==(const CExtPubKey& a, const CExtPubKey& b)
213  {
214  return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
215  memcmp(&a.vchChainCode[0], &b.vchChainCode[0], 32) == 0 && a.pubkey == b.pubkey;
216  }
217 
218  void Encode(unsigned char code[74]) const;
219  void Decode(const unsigned char code[74]);
220  bool Derive(CExtPubKey& out, unsigned int nChild) const;
221 
222  template <typename Stream>
223  void Serialize(Stream& s, int nType, int nVersion) const
224  {
225  unsigned int len = BIP32_EXTKEY_SIZE;
226  ::WriteCompactSize(s, len);
227  unsigned char code[BIP32_EXTKEY_SIZE];
228  Encode(code);
229  s.write((const char *)&code[0], len);
230  }
231  template <typename Stream>
232  void Unserialize(Stream& s, int nType, int nVersion)
233  {
234  unsigned int len = ::ReadCompactSize(s);
235  unsigned char code[BIP32_EXTKEY_SIZE];
236  if (len != BIP32_EXTKEY_SIZE)
237  throw std::runtime_error("Invalid extended key size\n");
238  s.read((char *)&code[0], len);
239  Decode(code);
240  }
241 };
242 
243 #endif // BITCOIN_PUBKEY_H
BIP32_EXTKEY_SIZE
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
CKeyID::CKeyID
CKeyID()
Definition: pubkey.h:32
ReadCompactSize
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:273
b
void const uint64_t * b
Definition: field_5x52_asm_impl.h:10
CExtPubKey::Unserialize
void Unserialize(Stream &s, int nType, int nVersion)
Definition: pubkey.h:232
CPubKey::Invalidate
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:57
uint256.h
CPubKey::Set
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:71
Hash160
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:237
memcpy
void * memcpy(void *a, const void *b, size_t c)
Definition: glibc_compat.cpp:15
CPubKey::Decompress
bool Decompress()
Turn this public key into an uncompressed public key.
Definition: pubkey.cpp:69
WriteCompactSize
void WriteCompactSize(Stream &os, uint64_t nSize)
Definition: serialize.h:255
CExtPubKey::Serialize
void Serialize(Stream &s, int nType, int nVersion) const
Definition: pubkey.h:223
CKeyID
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
CExtPubKey::nDepth
unsigned char nDepth
Definition: pubkey.h:206
CExtPubKey::Derive
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:133
CPubKey::CPubKey
CPubKey(const std::vector< unsigned char > &vch)
Construct a public key from a byte vector.
Definition: pubkey.h:88
CPubKey::GetHex
std::string GetHex() const
Definition: pubkey.h:193
CExtPubKey::nChild
unsigned int nChild
Definition: pubkey.h:208
CPubKey::begin
const unsigned char * begin() const
Definition: pubkey.h:95
CExtPubKey::operator==
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:212
CPubKey::GetLen
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:47
CExtPubKey::vchFingerprint
unsigned char vchFingerprint[4]
Definition: pubkey.h:207
CPubKey::Verify
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:15
CPubKey::end
const unsigned char * end() const
Definition: pubkey.h:96
CExtPubKey::vchChainCode
unsigned char vchChainCode[32]
Definition: pubkey.h:209
CPubKey::GetHash
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:149
CPubKey::Serialize
void Serialize(Stream &s, int nType, int nVersion) const
Definition: pubkey.h:121
CPubKey::CPubKey
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:64
CPubKey::size
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:94
uint256
256-bit unsigned big integer.
Definition: uint256.h:38
CPubKey::Raw
std::vector< unsigned char > Raw() const
Definition: pubkey.h:188
CPubKey::GetSerializeSize
unsigned int GetSerializeSize(int nType, int nVersion) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:116
CPubKey::RecoverCompact
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Recover a public key from a compact signature.
Definition: pubkey.cpp:32
CPubKey::Derive
bool Derive(CPubKey &pubkeyChild, unsigned char ccChild[32], unsigned int nChild, const unsigned char cc[32]) const
Derive BIP32 child pubkey.
Definition: pubkey.cpp:89
CExtPubKey::Decode
void Decode(const unsigned char code[74])
Definition: pubkey.cpp:124
CPubKey::IsCompressed
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:168
CPubKey::operator==
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:100
uint160
160-bit unsigned big integer.
Definition: uint256.h:27
CPubKey
An encapsulated public key.
Definition: pubkey.h:37
CPubKey::operator<
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:109
CKeyImage
CPubKey CKeyImage
Definition: pubkey.h:203
CPubKey::Unserialize
void Unserialize(Stream &s, int nType, int nVersion)
Definition: pubkey.h:128
CPubKey::IsFullyValid
bool IsFullyValid() const
fully validate whether this is a valid public key (more expensive than IsValid())
Definition: pubkey.cpp:54
CPubKey::CPubKey
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:82
hash.h
serialize.h
CKeyID::CKeyID
CKeyID(const uint160 &in)
Definition: pubkey.h:33
CPubKey::IsValid
bool IsValid() const
Definition: pubkey.h:159
CExtPubKey::pubkey
CPubKey pubkey
Definition: pubkey.h:210
Hash
std::string Hash(std::string input)
Compute the 256-bit hash of a std::string.
Definition: hash.h:122
CPubKey::vch
unsigned char vch[65]
Just store the serialized data.
Definition: pubkey.h:44
CPubKey::operator[]
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:97
CExtPubKey
Definition: pubkey.h:205
CPubKey::GetID
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:143
CPubKey::operator!=
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:105
CExtPubKey::Encode
void Encode(unsigned char code[74]) const
Definition: pubkey.cpp:111