Separate implementations of CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY.
[novacoin.git] / src / key.h
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 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 <stdexcept>
9 #include <vector>
10
11 #include "allocators.h"
12 #include "serialize.h"
13 #include "uint256.h"
14 #include "hash.h"
15 #include "bignum.h"
16 #include "ies.h"
17
18 #include <openssl/ec.h> // for EC_KEY definition
19
20 // secp160k1
21 // const unsigned int PRIVATE_KEY_SIZE = 192;
22 // const unsigned int PUBLIC_KEY_SIZE  = 41;
23 // const unsigned int SIGNATURE_SIZE   = 48;
24 //
25 // secp192k1
26 // const unsigned int PRIVATE_KEY_SIZE = 222;
27 // const unsigned int PUBLIC_KEY_SIZE  = 49;
28 // const unsigned int SIGNATURE_SIZE   = 57;
29 //
30 // secp224k1
31 // const unsigned int PRIVATE_KEY_SIZE = 250;
32 // const unsigned int PUBLIC_KEY_SIZE  = 57;
33 // const unsigned int SIGNATURE_SIZE   = 66;
34 //
35 // secp256k1:
36 // const unsigned int PRIVATE_KEY_SIZE = 279;
37 // const unsigned int PUBLIC_KEY_SIZE  = 65;
38 // const unsigned int SIGNATURE_SIZE   = 72;
39 //
40 // see www.keylength.com
41 // script supports up to 75 for single byte push
42
43 class key_error : public std::runtime_error
44 {
45 public:
46     explicit key_error(const std::string& str) : std::runtime_error(str) {}
47 };
48
49 /** A reference to a CKey: the Hash160 of its serialized public key */
50 class CKeyID : public uint160
51 {
52 public:
53     CKeyID() : uint160(0) { }
54     CKeyID(const uint160 &in) : uint160(in) { }
55 };
56
57 /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
58 class CScriptID : public uint160
59 {
60 public:
61     CScriptID() : uint160(0) { }
62     CScriptID(const uint160 &in) : uint160(in) { }
63 };
64
65 /** An encapsulated public key. */
66 class CPubKey {
67 private:
68     std::vector<unsigned char> vchPubKey;
69     friend class CKey;
70
71 public:
72     CPubKey() { }
73     CPubKey(const std::vector<unsigned char> &vchPubKeyIn) : vchPubKey(vchPubKeyIn) { }
74     friend bool operator==(const CPubKey &a, const CPubKey &b) { return a.vchPubKey == b.vchPubKey; }
75     friend bool operator!=(const CPubKey &a, const CPubKey &b) { return a.vchPubKey != b.vchPubKey; }
76     friend bool operator<(const CPubKey &a, const CPubKey &b) { return a.vchPubKey < b.vchPubKey; }
77
78     IMPLEMENT_SERIALIZE(
79         READWRITE(vchPubKey);
80     )
81
82     CKeyID GetID() const {
83         return CKeyID(Hash160(vchPubKey));
84     }
85
86     uint256 GetHash() const {
87         return Hash(vchPubKey.begin(), vchPubKey.end());
88     }
89
90     bool IsValid() const {
91         return vchPubKey.size() == 33 || vchPubKey.size() == 65;
92     }
93
94     bool IsCompressed() const {
95         return vchPubKey.size() == 33;
96     }
97
98     std::vector<unsigned char> Raw() const {
99         return vchPubKey;
100     }
101
102     // Encrypt data
103     void EncryptData(const std::vector<unsigned char>& data, std::vector<unsigned char>& encrypted);
104 };
105
106
107 // secure_allocator is defined in allocators.h
108 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
109 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
110 // CSecret is a serialization of just the secret parameter (32 bytes)
111 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
112
113 /** An encapsulated OpenSSL Elliptic Curve key (public and/or private) */
114 class CKey
115 {
116 protected:
117     EC_KEY* pkey;
118     bool fSet;
119     bool fCompressedPubKey;
120
121     void SetCompressedPubKey();
122
123 public:
124
125     void Reset();
126
127     CKey();
128     CKey(const CKey& b);
129
130     CKey& operator=(const CKey& b);
131
132     ~CKey();
133
134     bool IsNull() const;
135     bool IsCompressed() const;
136
137     void MakeNewKey(bool fCompressed);
138     bool SetPrivKey(const CPrivKey& vchPrivKey);
139     bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
140     CSecret GetSecret(bool &fCompressed) const;
141     CPrivKey GetPrivKey() const;
142     bool SetPubKey(const CPubKey& vchPubKey);
143     CPubKey GetPubKey() const;
144
145     bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);
146
147     // create a compact signature (65 bytes), which allows reconstructing the used public key
148     // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
149     // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
150     //                  0x1D = second key with even y, 0x1E = second key with odd y
151     bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
152
153     // reconstruct public key from a compact signature
154     // This is only slightly more CPU intensive than just verifying it.
155     // If this function succeeds, the recovered public key is guaranteed to be valid
156     // (the signature is a valid signature of the given data for that key)
157     bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
158
159     bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
160
161     // Verify a compact signature
162     bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);
163
164     bool IsValid();
165
166     // Check whether an element of a signature (r or s) is valid.
167     static bool CheckSignatureElement(const unsigned char *vch, int len, bool half);
168
169     // Reserialize to DER
170     static bool ReserealizeSignature(std::vector<unsigned char>& vchSig);
171
172     // Encrypt data
173     void EncryptData(const std::vector<unsigned char>& data, std::vector<unsigned char>& encrypted);
174
175     // Decrypt data
176     void DecryptData(const std::vector<unsigned char>& encrypted, std::vector<unsigned char>& data);
177 };
178
179 class CPoint
180 {
181 private:
182     EC_POINT *point;
183     EC_GROUP* group;
184     BN_CTX* ctx;
185
186 public:
187     CPoint();
188     bool operator!=(const CPoint &a);
189     ~CPoint();
190
191     // Initialize from octets stream
192     bool setBytes(const std::vector<unsigned char> &vchBytes);
193
194     // Initialize from pubkey
195     bool setPubKey(const CPubKey &vchPubKey);
196
197     // Serialize to octets stream
198     bool getBytes(std::vector<unsigned char> &vchBytes);
199
200     // ECC multiplication by specified multiplier
201     bool ECMUL(const CBigNum &bnMultiplier);
202
203     // Calculate G*m + q
204     bool ECMULGEN(const CBigNum &bnMultiplier, const CPoint &qPoint);
205
206     bool IsInfinity() { return EC_POINT_is_at_infinity(group, point); }
207 };
208
209 class CMalleablePubKey
210 {
211 private:
212     unsigned char nVersion;
213     CPubKey pubKeyL;
214     CPubKey pubKeyH;
215     friend class CMalleableKey;
216
217     static const unsigned char CURRENT_VERSION = 1;
218
219 public:
220     CMalleablePubKey() { nVersion = CMalleablePubKey::CURRENT_VERSION; }
221     CMalleablePubKey(const CMalleablePubKey& mpk)
222     {
223         nVersion = mpk.nVersion;
224         pubKeyL = mpk.pubKeyL;
225         pubKeyH = mpk.pubKeyH;
226     }
227     CMalleablePubKey(const std::string& strMalleablePubKey) { SetString(strMalleablePubKey); }
228     CMalleablePubKey(const CPubKey &pubKeyInL, const CPubKey &pubKeyInH) : pubKeyL(pubKeyInL), pubKeyH(pubKeyInH) { nVersion = CMalleablePubKey::CURRENT_VERSION; }
229
230     IMPLEMENT_SERIALIZE(
231         READWRITE(this->nVersion);
232         nVersion = this->nVersion;
233         READWRITE(pubKeyL);
234         READWRITE(pubKeyH);
235     )
236
237     bool IsValid() const {
238         return pubKeyL.IsValid() && pubKeyH.IsValid();
239     }
240
241     bool operator==(const CMalleablePubKey &b);
242     bool operator!=(const CMalleablePubKey &b) { return !(*this == b); }
243     CMalleablePubKey& operator=(const CMalleablePubKey& mpk) {
244         nVersion = mpk.nVersion;
245         pubKeyL = mpk.pubKeyL;
246         pubKeyH = mpk.pubKeyH;
247         return *this;
248     }
249
250     std::string ToString() const;
251     bool SetString(const std::string& strMalleablePubKey);
252
253     CKeyID GetID() const {
254         return pubKeyL.GetID();
255     }
256
257     std::vector<unsigned char> Raw() const;
258
259     CPubKey& GetL() { return pubKeyL; }
260     CPubKey& GetH() { return pubKeyH; }
261     void GetVariant(CPubKey &R, CPubKey &vchPubKeyVariant);
262 };
263
264 class CMalleableKey
265 {
266 private:
267     unsigned char nVersion;
268     CSecret vchSecretL;
269     CSecret vchSecretH;
270
271     friend class CMalleableKeyView;
272
273     static const unsigned char CURRENT_VERSION = 1;
274
275 public:
276     CMalleableKey();
277     CMalleableKey(const CMalleableKey &b);
278     CMalleableKey(const CSecret &L, const CSecret &H);
279     ~CMalleableKey();
280
281     IMPLEMENT_SERIALIZE(
282         READWRITE(this->nVersion);
283         nVersion = this->nVersion;
284         READWRITE(vchSecretL);
285         READWRITE(vchSecretH);
286     )
287
288     std::string ToString() const;
289     bool SetString(const std::string& strMalleablePubKey);
290     std::vector<unsigned char> Raw() const;
291     CMalleableKey& operator=(const CMalleableKey& mk) {
292         nVersion = mk.nVersion;
293         vchSecretL = mk.vchSecretL;
294         vchSecretH = mk.vchSecretH;
295         return *this;
296     }
297
298     void Reset();
299     void MakeNewKeys();
300     bool IsNull() const;
301     bool IsValid() const { return !IsNull() && GetMalleablePubKey().IsValid(); }
302     bool SetSecrets(const CSecret &pvchSecretL, const CSecret &pvchSecretH);
303
304     CSecret GetSecretL() const { return vchSecretL; }
305     CSecret GetSecretH() const { return vchSecretH; }
306
307     CKeyID GetID() const {
308         return GetMalleablePubKey().GetID();
309     }
310     CMalleablePubKey GetMalleablePubKey() const;
311     bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const;
312     bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant, CKey &privKeyVariant) const;
313 };
314
315 class CMalleableKeyView
316 {
317 private:
318     unsigned char nVersion;
319     CSecret vchSecretL;
320     CPubKey vchPubKeyH;
321
322     static const unsigned char CURRENT_VERSION = 1;
323
324 public:
325     CMalleableKeyView() { nVersion = 0; };
326     CMalleableKeyView(const CMalleableKey &b);
327
328     CMalleableKeyView(const CMalleableKeyView &b);
329     CMalleableKeyView& operator=(const CMalleableKey &b);
330     ~CMalleableKeyView();
331
332     IMPLEMENT_SERIALIZE(
333         READWRITE(this->nVersion);
334         nVersion = this->nVersion;
335         READWRITE(vchSecretL);
336         READWRITE(vchPubKeyH);
337     )
338
339     bool IsNull() const;
340     bool IsValid() const { return !IsNull() && GetMalleablePubKey().IsValid(); }
341     std::string ToString() const;
342     bool SetString(const std::string& strMalleablePubKey);
343     std::vector<unsigned char> Raw() const;
344     CMalleableKeyView& operator=(const CMalleableKeyView& mkv) {
345         nVersion = mkv.nVersion;
346         vchSecretL = mkv.vchSecretL;
347         vchPubKeyH = mkv.vchPubKeyH;
348         return *this;
349     }
350
351     CKeyID GetID() const {
352         return GetMalleablePubKey().GetID();
353     }
354     CMalleablePubKey GetMalleablePubKey() const;
355     CMalleableKey GetMalleableKey(const CSecret &vchSecretH) const { return CMalleableKey(vchSecretL, vchSecretH); }
356     bool CheckKeyVariant(const CPubKey &R, const CPubKey &vchPubKeyVariant) const;
357
358     bool operator <(const CMalleableKeyView& kv) const { return vchPubKeyH.GetID() < kv.vchPubKeyH.GetID(); }
359 };
360
361 #endif