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.
11 #include "allocators.h"
12 #include "serialize.h"
16 #include <openssl/ec.h> // for EC_KEY definition
19 // const unsigned int PRIVATE_KEY_SIZE = 192;
20 // const unsigned int PUBLIC_KEY_SIZE = 41;
21 // const unsigned int SIGNATURE_SIZE = 48;
24 // const unsigned int PRIVATE_KEY_SIZE = 222;
25 // const unsigned int PUBLIC_KEY_SIZE = 49;
26 // const unsigned int SIGNATURE_SIZE = 57;
29 // const unsigned int PRIVATE_KEY_SIZE = 250;
30 // const unsigned int PUBLIC_KEY_SIZE = 57;
31 // const unsigned int SIGNATURE_SIZE = 66;
34 // const unsigned int PRIVATE_KEY_SIZE = 279;
35 // const unsigned int PUBLIC_KEY_SIZE = 65;
36 // const unsigned int SIGNATURE_SIZE = 72;
38 // see www.keylength.com
39 // script supports up to 75 for single byte push
41 class key_error : public std::runtime_error
44 explicit key_error(const std::string& str) : std::runtime_error(str) {}
47 /** A reference to a CKey: the Hash160 of its serialized public key */
48 class CKeyID : public uint160
51 CKeyID() : uint160(0) { }
52 CKeyID(const uint160 &in) : uint160(in) { }
55 /** A reference to a CScript: the Hash160 of its serialization (see script.h) */
56 class CScriptID : public uint160
59 CScriptID() : uint160(0) { }
60 CScriptID(const uint160 &in) : uint160(in) { }
63 /** An encapsulated public key. */
66 std::vector<unsigned char> vchPubKey;
71 CPubKey(const std::vector<unsigned char> &vchPubKeyIn) : vchPubKey(vchPubKeyIn) { }
72 friend bool operator==(const CPubKey &a, const CPubKey &b) { return a.vchPubKey == b.vchPubKey; }
73 friend bool operator!=(const CPubKey &a, const CPubKey &b) { return a.vchPubKey != b.vchPubKey; }
74 friend bool operator<(const CPubKey &a, const CPubKey &b) { return a.vchPubKey < b.vchPubKey; }
80 CKeyID GetID() const {
81 return CKeyID(Hash160(vchPubKey));
84 uint256 GetHash() const {
85 return Hash(vchPubKey.begin(), vchPubKey.end());
88 bool IsValid() const {
89 return vchPubKey.size() == 33 || vchPubKey.size() == 65;
92 bool IsCompressed() const {
93 return vchPubKey.size() == 33;
96 std::vector<unsigned char> Raw() const {
102 // secure_allocator is defined in serialize.h
103 // CPrivKey is a serialized private key, with all parameters included (279 bytes)
104 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
105 // CSecret is a serialization of just the secret parameter (32 bytes)
106 typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
108 /** An encapsulated OpenSSL Elliptic Curve key (public and/or private) */
114 bool fCompressedPubKey;
116 void SetCompressedPubKey();
125 CKey& operator=(const CKey& b);
130 bool IsCompressed() const;
132 void MakeNewKey(bool fCompressed);
133 bool SetPrivKey(const CPrivKey& vchPrivKey);
134 bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
135 CSecret GetSecret(bool &fCompressed) const;
136 CPrivKey GetPrivKey() const;
137 bool SetPubKey(const CPubKey& vchPubKey);
138 CPubKey GetPubKey() const;
140 bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);
142 // create a compact signature (65 bytes), which allows reconstructing the used public key
143 // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
144 // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
145 // 0x1D = second key with even y, 0x1E = second key with odd y
146 bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
148 // reconstruct public key from a compact signature
149 // This is only slightly more CPU intensive than just verifying it.
150 // If this function succeeds, the recovered public key is guaranteed to be valid
151 // (the signature is a valid signature of the given data for that key)
152 bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
154 bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
156 // Verify a compact signature
157 bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);