Merge branch '0.6.x' of git://gitorious.org/+bitcoin-stable-developers/bitcoin/bitcoi...
[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 "uint256.h"
13
14 #include <openssl/ec.h> // for EC_KEY definition
15
16 // secp160k1
17 // const unsigned int PRIVATE_KEY_SIZE = 192;
18 // const unsigned int PUBLIC_KEY_SIZE  = 41;
19 // const unsigned int SIGNATURE_SIZE   = 48;
20 //
21 // secp192k1
22 // const unsigned int PRIVATE_KEY_SIZE = 222;
23 // const unsigned int PUBLIC_KEY_SIZE  = 49;
24 // const unsigned int SIGNATURE_SIZE   = 57;
25 //
26 // secp224k1
27 // const unsigned int PRIVATE_KEY_SIZE = 250;
28 // const unsigned int PUBLIC_KEY_SIZE  = 57;
29 // const unsigned int SIGNATURE_SIZE   = 66;
30 //
31 // secp256k1:
32 // const unsigned int PRIVATE_KEY_SIZE = 279;
33 // const unsigned int PUBLIC_KEY_SIZE  = 65;
34 // const unsigned int SIGNATURE_SIZE   = 72;
35 //
36 // see www.keylength.com
37 // script supports up to 75 for single byte push
38
39 class key_error : public std::runtime_error
40 {
41 public:
42     explicit key_error(const std::string& str) : std::runtime_error(str) {}
43 };
44
45
46 // secure_allocator is defined in serialize.h
47 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
48 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
49 // CSecret is a serialization of just the secret parameter (32 bytes)
50 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
51
52 /** An encapsulated OpenSSL Elliptic Curve key (public and/or private) */
53 class CKey
54 {
55 protected:
56     EC_KEY* pkey;
57     bool fSet;
58     bool fCompressedPubKey;
59
60     void SetCompressedPubKey();
61
62 public:
63
64     void Reset();
65
66     CKey();
67     CKey(const CKey& b);
68
69     CKey& operator=(const CKey& b);
70
71     ~CKey();
72
73     bool IsNull() const;
74     bool IsCompressed() const;
75
76     void MakeNewKey(bool fCompressed);
77     bool SetPrivKey(const CPrivKey& vchPrivKey);
78     bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
79     CSecret GetSecret(bool &fCompressed) const;
80     CPrivKey GetPrivKey() const;
81     bool SetPubKey(const std::vector<unsigned char>& vchPubKey);
82     std::vector<unsigned char> GetPubKey() const;
83
84     bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);
85
86     // create a compact signature (65 bytes), which allows reconstructing the used public key
87     // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
88     // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
89     //                  0x1D = second key with even y, 0x1E = second key with odd y
90     bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
91
92     // reconstruct public key from a compact signature
93     // This is only slightly more CPU intensive than just verifying it.
94     // If this function succeeds, the recovered public key is guaranteed to be valid
95     // (the signature is a valid signature of the given data for that key)
96     bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
97
98     bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
99
100     // Verify a compact signature
101     bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);
102
103     bool IsValid();
104 };
105
106 #endif