Feathercoin  0.5.0
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros
key.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2013 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
5 #ifndef BITCOIN_KEY_H
6 #define BITCOIN_KEY_H
7 
8 #include <vector>
9 
10 #include "allocators.h"
11 #include "serialize.h"
12 #include "uint256.h"
13 #include "hash.h"
14 
15 // secp256k1:
16 // const unsigned int PRIVATE_KEY_SIZE = 279;
17 // const unsigned int PUBLIC_KEY_SIZE = 65;
18 // const unsigned int SIGNATURE_SIZE = 72;
19 //
20 // see www.keylength.com
21 // script supports up to 75 for single byte push
22 
24 class CKeyID : public uint160
25 {
26 public:
27  CKeyID() : uint160(0) { }
28  CKeyID(const uint160 &in) : uint160(in) { }
29 };
30 
32 class CScriptID : public uint160
33 {
34 public:
35  CScriptID() : uint160(0) { }
36  CScriptID(const uint160 &in) : uint160(in) { }
37 };
38 
40 class CPubKey {
41 private:
42  // Just store the serialized data.
43  // Its length can very cheaply be computed from the first byte.
44  unsigned char vch[65];
45 
46  // Compute the length of a pubkey with a given first byte.
47  unsigned int static GetLen(unsigned char chHeader) {
48  if (chHeader == 2 || chHeader == 3)
49  return 33;
50  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
51  return 65;
52  return 0;
53  }
54 
55  // Set this key data to be invalid
56  void Invalidate() {
57  vch[0] = 0xFF;
58  }
59 
60 public:
61  // Construct an invalid public key.
62  CPubKey() {
63  Invalidate();
64  }
65 
66  // Initialize a public key using begin/end iterators to byte data.
67  template<typename T>
68  void Set(const T pbegin, const T pend) {
69  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
70  if (len && len == (pend-pbegin))
71  memcpy(vch, (unsigned char*)&pbegin[0], len);
72  else
73  Invalidate();
74  }
75 
76  // Construct a public key using begin/end iterators to byte data.
77  template<typename T>
78  CPubKey(const T pbegin, const T pend) {
79  Set(pbegin, pend);
80  }
81 
82  // Construct a public key from a byte vector.
83  CPubKey(const std::vector<unsigned char> &vch) {
84  Set(vch.begin(), vch.end());
85  }
86 
87  // Simple read-only vector-like interface to the pubkey data.
88  unsigned int size() const { return GetLen(vch[0]); }
89  const unsigned char *begin() const { return vch; }
90  const unsigned char *end() const { return vch+size(); }
91  const unsigned char &operator[](unsigned int pos) const { return vch[pos]; }
92 
93  // Comparator implementation.
94  friend bool operator==(const CPubKey &a, const CPubKey &b) {
95  return a.vch[0] == b.vch[0] &&
96  memcmp(a.vch, b.vch, a.size()) == 0;
97  }
98  friend bool operator!=(const CPubKey &a, const CPubKey &b) {
99  return !(a == b);
100  }
101  friend bool operator<(const CPubKey &a, const CPubKey &b) {
102  return a.vch[0] < b.vch[0] ||
103  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
104  }
105 
106  // Implement serialization, as if this was a byte vector.
107  unsigned int GetSerializeSize(int nType, int nVersion) const {
108  return size() + 1;
109  }
110  template<typename Stream> void Serialize(Stream &s, int nType, int nVersion) const {
111  unsigned int len = size();
112  ::WriteCompactSize(s, len);
113  s.write((char*)vch, len);
114  }
115  template<typename Stream> void Unserialize(Stream &s, int nType, int nVersion) {
116  unsigned int len = ::ReadCompactSize(s);
117  if (len <= 65) {
118  s.read((char*)vch, len);
119  } else {
120  // invalid pubkey, skip available data
121  char dummy;
122  while (len--)
123  s.read(&dummy, 1);
124  Invalidate();
125  }
126  }
127 
128  // Get the KeyID of this public key (hash of its serialization)
129  CKeyID GetID() const {
130  return CKeyID(Hash160(vch, vch+size()));
131  }
132 
133  // Get the 256-bit hash of this public key.
134  uint256 GetHash() const {
135  return Hash(vch, vch+size());
136  }
137 
138  // just check syntactic correctness.
139  bool IsValid() const {
140  return size() > 0;
141  }
142 
143  // fully validate whether this is a valid public key (more expensive than IsValid())
144  bool IsFullyValid() const;
145 
146  // Check whether this is a compressed public key.
147  bool IsCompressed() const {
148  return size() == 33;
149  }
150 
151  // Verify a DER signature (~72 bytes).
152  // If this public key is not fully valid, the return value will be false.
153  bool Verify(const uint256 &hash, const std::vector<unsigned char>& vchSig) const;
154 
155  // Verify a compact signature (~65 bytes).
156  // See CKey::SignCompact.
157  bool VerifyCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig) const;
158 
159  // Recover a public key from a compact signature.
160  bool RecoverCompact(const uint256 &hash, const std::vector<unsigned char>& vchSig);
161 
162  // Turn this public key into an uncompressed public key.
163  bool Decompress();
164 };
165 
166 
167 // secure_allocator is defined in allocators.h
168 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
169 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
170 
172 class CKey {
173 private:
174  // Whether this private key is valid. We check for correctness when modifying the key
175  // data, so fValid should always correspond to the actual state.
176  bool fValid;
177 
178  // Whether the public key corresponding to this private key is (to be) compressed.
180 
181  // The actual byte data
182  unsigned char vch[32];
183 
184  // Check whether the 32-byte array pointed to be vch is valid keydata.
185  bool static Check(const unsigned char *vch);
186 public:
187 
188  // Construct an invalid private key.
189  CKey() : fValid(false) {
190  LockObject(vch);
191  }
192 
193  // Copy constructor. This is necessary because of memlocking.
194  CKey(const CKey &secret) : fValid(secret.fValid), fCompressed(secret.fCompressed) {
195  LockObject(vch);
196  memcpy(vch, secret.vch, sizeof(vch));
197  }
198 
199  // Destructor (again necessary because of memlocking).
200  ~CKey() {
201  UnlockObject(vch);
202  }
203 
204  // Initialize using begin and end iterators to byte data.
205  template<typename T>
206  void Set(const T pbegin, const T pend, bool fCompressedIn) {
207  if (pend - pbegin != 32) {
208  fValid = false;
209  return;
210  }
211  if (Check(&pbegin[0])) {
212  memcpy(vch, (unsigned char*)&pbegin[0], 32);
213  fValid = true;
214  fCompressed = fCompressedIn;
215  } else {
216  fValid = false;
217  }
218  }
219 
220  // Simple read-only vector-like interface.
221  unsigned int size() const { return (fValid ? 32 : 0); }
222  const unsigned char *begin() const { return vch; }
223  const unsigned char *end() const { return vch + size(); }
224 
225  // Check whether this private key is valid.
226  bool IsValid() const { return fValid; }
227 
228  // Check whether the public key corresponding to this private key is (to be) compressed.
229  bool IsCompressed() const { return fCompressed; }
230 
231  // Initialize from a CPrivKey (serialized OpenSSL private key data).
232  bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed);
233 
234  // Generate a new private key using a cryptographic PRNG.
235  void MakeNewKey(bool fCompressed);
236 
237  // Convert the private key to a CPrivKey (serialized OpenSSL private key data).
238  // This is expensive.
239  CPrivKey GetPrivKey() const;
240 
241  // Compute the public key from a private key.
242  // This is expensive.
243  CPubKey GetPubKey() const;
244 
245  // Create a DER-serialized signature.
246  bool Sign(const uint256 &hash, std::vector<unsigned char>& vchSig) const;
247 
248  // Create a compact signature (65 bytes), which allows reconstructing the used public key.
249  // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
250  // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
251  // 0x1D = second key with even y, 0x1E = second key with odd y,
252  // add 0x04 for compressed keys.
253  bool SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const;
254 };
255 
256 #endif
bool VerifyCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Definition: key.cpp:364
void UnlockObject(const T &t)
Definition: allocators.h:187
void Unserialize(Stream &s, int nType, int nVersion)
Definition: key.h:115
unsigned static int GetLen(unsigned char chHeader)
Definition: key.h:47
const unsigned char * begin() const
Definition: key.h:222
friend bool operator<(const CPubKey &a, const CPubKey &b)
Definition: key.h:101
const unsigned char * end() const
Definition: key.h:223
CKeyID(const uint160 &in)
Definition: key.h:28
void Invalidate()
Definition: key.h:56
unsigned int size() const
Definition: key.h:88
void Set(const T pbegin, const T pend)
Definition: key.h:68
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:321
uint160 Hash160(const T1 pbegin, const T1 pend)
Definition: hash.h:109
CScriptID()
Definition: key.h:35
bool fValid
Definition: key.h:176
friend bool operator==(const CPubKey &a, const CPubKey &b)
Definition: key.h:94
bool IsValid() const
Definition: key.h:226
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
Definition: key.h:169
int std::string int dummy
Definition: util.h:164
bool IsCompressed() const
Definition: key.h:229
void LockObject(const T &t)
Definition: allocators.h:183
void Serialize(Stream &s, int nType, int nVersion) const
Definition: key.h:110
CPubKey GetPubKey() const
Definition: key.cpp:312
CPrivKey GetPrivKey() const
Definition: key.cpp:303
bool RecoverCompact(const uint256 &hash, const std::vector< unsigned char > &vchSig)
Definition: key.cpp:354
CPubKey()
Definition: key.h:62
An encapsulated public key.
Definition: key.h:40
void MakeNewKey(bool fCompressed)
Definition: key.cpp:285
const unsigned char & operator[](unsigned int pos) const
Definition: key.h:91
~CKey()
Definition: key.h:200
uint256 GetHash() const
Definition: key.h:134
CPubKey(const std::vector< unsigned char > &vch)
Definition: key.h:83
uint256 Hash(const T1 pbegin, const T1 pend)
Definition: hash.h:16
void Set(const T pbegin, const T pend, bool fCompressedIn)
Definition: key.h:206
unsigned char vch[65]
Definition: key.h:44
const unsigned char * begin() const
Definition: key.h:89
CScriptID(const uint160 &in)
Definition: key.h:36
void WriteCompactSize(Stream &os, uint64 nSize)
Definition: serialize.h:173
bool IsCompressed() const
Definition: key.h:147
256-bit unsigned integer
Definition: uint256.h:537
bool SetPrivKey(const CPrivKey &vchPrivKey, bool fCompressed)
Definition: key.cpp:293
CKeyID()
Definition: key.h:27
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Definition: key.cpp:329
CPubKey(const T pbegin, const T pend)
Definition: key.h:78
A reference to a CKey: the Hash160 of its serialized public key.
Definition: key.h:24
bool fCompressed
Definition: key.h:179
bool IsFullyValid() const
Definition: key.cpp:379
bool IsValid() const
Definition: key.h:139
160-bit unsigned integer
Definition: uint256.h:422
CKey()
Definition: key.h:189
CKey(const CKey &secret)
Definition: key.h:194
A reference to a CScript: the Hash160 of its serialization (see script.h)
Definition: key.h:32
uint64 ReadCompactSize(Stream &is)
Definition: serialize.h:205
static bool Check(const unsigned char *vch)
Definition: key.cpp:261
An encapsulated private key.
Definition: key.h:172
unsigned char vch[32]
Definition: key.h:182
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Definition: key.cpp:343
unsigned int size() const
Definition: key.h:221
CKeyID GetID() const
Definition: key.h:129
uint32_t hash
Definition: cache.cc:34
unsigned int GetSerializeSize(int nType, int nVersion) const
Definition: key.h:107
friend bool operator!=(const CPubKey &a, const CPubKey &b)
Definition: key.h:98
const unsigned char * end() const
Definition: key.h:90
bool Decompress()
Definition: key.cpp:388