Fabcoin Core  0.16.2
P2P Digital Currency
pubkey.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 FABCOIN_PUBKEY_H
7 #define FABCOIN_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  std::vector<unsigned char> getvch() const
102  {
103  return std::vector<unsigned char>(begin(), end());
104  }
105 
107  friend bool operator==(const CPubKey& a, const CPubKey& b)
108  {
109  return a.vch[0] == b.vch[0] &&
110  memcmp(a.vch, b.vch, a.size()) == 0;
111  }
112  friend bool operator!=(const CPubKey& a, const CPubKey& b)
113  {
114  return !(a == b);
115  }
116  friend bool operator<(const CPubKey& a, const CPubKey& b)
117  {
118  return a.vch[0] < b.vch[0] ||
119  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
120  }
121 
123  template <typename Stream>
124  void Serialize(Stream& s) const
125  {
126  unsigned int len = size();
127  ::WriteCompactSize(s, len);
128  s.write((char*)vch, len);
129  }
130  template <typename Stream>
131  void Unserialize(Stream& s)
132  {
133  unsigned int len = ::ReadCompactSize(s);
134  if (len <= 65) {
135  s.read((char*)vch, len);
136  } else {
137  // invalid pubkey, skip available data
138  char dummy;
139  while (len--)
140  s.read(&dummy, 1);
141  Invalidate();
142  }
143  }
144 
146  CKeyID GetID() const
147  {
148  return CKeyID(Hash160(vch, vch + size()));
149  }
150 
152  uint256 GetHash() const
153  {
154  return Hash(vch, vch + size());
155  }
156 
157  /*
158  * Check syntactic correctness.
159  *
160  * Note that this is consensus critical as CheckSig() calls it!
161  */
162  bool IsValid() const
163  {
164  return size() > 0;
165  }
166 
168  bool IsFullyValid() const;
169 
171  bool IsCompressed() const
172  {
173  return size() == 33;
174  }
175 
180  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
181 
185  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
186 
188  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
189 
191  bool Decompress();
192 
194  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
195 };
196 
197 struct CExtPubKey {
198  unsigned char nDepth;
199  unsigned char vchFingerprint[4];
200  unsigned int nChild;
203 
204  friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
205  {
206  return a.nDepth == b.nDepth &&
207  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
208  a.nChild == b.nChild &&
209  a.chaincode == b.chaincode &&
210  a.pubkey == b.pubkey;
211  }
212 
213  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
214  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
215  bool Derive(CExtPubKey& out, unsigned int nChild) const;
216 
217  void Serialize(CSizeComputer& s) const
218  {
219  // Optimized implementation for ::GetSerializeSize that avoids copying.
220  s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
221  }
222  template <typename Stream>
223  void Serialize(Stream& s) 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)
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 
246 {
247  static int refcount;
248 
249 public:
250  ECCVerifyHandle();
251  ~ECCVerifyHandle();
252 };
253 
254 #endif // FABCOIN_PUBKEY_H
void Serialize(Stream &s) const
Definition: pubkey.h:223
unsigned char vchFingerprint[4]
Definition: pubkey.h:199
unsigned static int GetLen(unsigned char chHeader)
Compute the length of a pubkey with a given first byte.
Definition: pubkey.h:50
uint256 ChainCode
Definition: pubkey.h:36
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:116
uint64_t ReadCompactSize(Stream &is)
Definition: serialize.h:273
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:983
#define T(i, x)
CKeyID(const uint160 &in)
Definition: pubkey.h:33
void Invalidate()
Set this key data to be invalid.
Definition: pubkey.h:60
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:97
void Set(const T pbegin, const T pend)
Initialize a public key using begin/end iterators to byte data.
Definition: pubkey.h:74
static int refcount
Definition: pubkey.h:247
unsigned char nDepth
Definition: pubkey.h:198
void Unserialize(Stream &s)
Definition: pubkey.h:131
uint160 Hash160(const T1 pbegin, const T1 pend)
Compute the 160-bit hash an object.
Definition: hash.h:107
ChainCode chaincode
Definition: pubkey.h:201
bytes code
Definition: SmartVM.cpp:45
unsigned char * begin()
Definition: uint256.h:65
unsigned int nChild
Definition: pubkey.h:200
friend bool operator==(const CPubKey &a, const CPubKey &b)
Comparator implementation.
Definition: pubkey.h:107
std::vector< unsigned char > getvch() const
Definition: pubkey.h:101
unsigned char * end()
Definition: uint256.h:70
#define a(i)
void Unserialize(Stream &s)
Definition: pubkey.h:232
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:245
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:204
void Serialize(Stream &s) const
Implement serialization, as if this was a byte vector.
Definition: pubkey.h:124
CPubKey()
Construct an invalid public key.
Definition: pubkey.h:67
An encapsulated public key.
Definition: pubkey.h:39
const unsigned char & operator[](unsigned int pos) const
Definition: pubkey.h:100
uint256 GetHash() const
Get the 256-bit hash of this public key.
Definition: pubkey.h:152
uint256 Hash(const T1 pbegin, const T1 pend)
Compute the 256-bit hash of an object.
Definition: hash.h:70
unsigned char vch[65]
Just store the serialized data.
Definition: pubkey.h:47
const unsigned char * begin() const
Definition: pubkey.h:98
#define b(i, j)
CPubKey(const std::vector< unsigned char > &_vch)
Construct a public key from a byte vector.
Definition: pubkey.h:91
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:171
void seek(size_t _nSize)
Pretend _nSize bytes are written, without specifying them.
Definition: serialize.h:909
256-bit opaque blob.
Definition: uint256.h:132
CKeyID()
Definition: pubkey.h:32
void * memcpy(void *a, const void *b, size_t c)
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
CPubKey(const T pbegin, const T pend)
Construct a public key using begin/end iterators to byte data.
Definition: pubkey.h:85
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:29
bool IsValid() const
Definition: pubkey.h:162
160-bit opaque blob.
Definition: uint256.h:120
CPubKey pubkey
Definition: pubkey.h:202
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:146
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: pubkey.h:112
const unsigned char * end() const
Definition: pubkey.h:99
unsigned int size() const
Definition: uint256.h:85
void Serialize(CSizeComputer &s) const
Definition: pubkey.h:217