]> Git Repo - VerusCoin.git/blame - src/key.h
Merge pull request #2112 from fanquake/typos
[VerusCoin.git] / src / key.h
CommitLineData
8bd66202 1// Copyright (c) 2009-2010 Satoshi Nakamoto
88216419 2// Copyright (c) 2009-2012 The Bitcoin developers
8bd66202 3// Distributed under the MIT/X11 software license, see the accompanying
3a25a2b9 4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
223b6f1b
WL
5#ifndef BITCOIN_KEY_H
6#define BITCOIN_KEY_H
8bd66202 7
fcedd45c
VN
8#include <stdexcept>
9#include <vector>
10
0a83c0fc 11#include "allocators.h"
fd61d6f5 12#include "serialize.h"
fcedd45c 13#include "uint256.h"
fd61d6f5 14#include "util.h"
fcedd45c 15
096e06db
GA
16#include <openssl/ec.h> // for EC_KEY definition
17
8bd66202
GA
18// secp160k1
19// const unsigned int PRIVATE_KEY_SIZE = 192;
20// const unsigned int PUBLIC_KEY_SIZE = 41;
21// const unsigned int SIGNATURE_SIZE = 48;
22//
23// secp192k1
24// const unsigned int PRIVATE_KEY_SIZE = 222;
25// const unsigned int PUBLIC_KEY_SIZE = 49;
26// const unsigned int SIGNATURE_SIZE = 57;
27//
28// secp224k1
29// const unsigned int PRIVATE_KEY_SIZE = 250;
30// const unsigned int PUBLIC_KEY_SIZE = 57;
31// const unsigned int SIGNATURE_SIZE = 66;
32//
33// secp256k1:
34// const unsigned int PRIVATE_KEY_SIZE = 279;
35// const unsigned int PUBLIC_KEY_SIZE = 65;
36// const unsigned int SIGNATURE_SIZE = 72;
37//
38// see www.keylength.com
39// script supports up to 75 for single byte push
40
8bd66202
GA
41class key_error : public std::runtime_error
42{
43public:
44 explicit key_error(const std::string& str) : std::runtime_error(str) {}
45};
46
10254401
PW
47/** A reference to a CKey: the Hash160 of its serialized public key */
48class CKeyID : public uint160
49{
50public:
51 CKeyID() : uint160(0) { }
52 CKeyID(const uint160 &in) : uint160(in) { }
53};
54
55/** A reference to a CScript: the Hash160 of its serialization (see script.h) */
56class CScriptID : public uint160
57{
58public:
59 CScriptID() : uint160(0) { }
60 CScriptID(const uint160 &in) : uint160(in) { }
61};
62
63/** An encapsulated public key. */
fd61d6f5
PW
64class CPubKey {
65private:
66 std::vector<unsigned char> vchPubKey;
67 friend class CKey;
68
69public:
70 CPubKey() { }
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; }
75
76 IMPLEMENT_SERIALIZE(
77 READWRITE(vchPubKey);
78 )
79
10254401
PW
80 CKeyID GetID() const {
81 return CKeyID(Hash160(vchPubKey));
fd61d6f5
PW
82 }
83
84 uint256 GetHash() const {
85 return Hash(vchPubKey.begin(), vchPubKey.end());
86 }
87
88 bool IsValid() const {
89 return vchPubKey.size() == 33 || vchPubKey.size() == 65;
90 }
91
10254401
PW
92 bool IsCompressed() const {
93 return vchPubKey.size() == 33;
94 }
95
fd61d6f5
PW
96 std::vector<unsigned char> Raw() const {
97 return vchPubKey;
98 }
99};
100
8bd66202 101
7fddf121 102// secure_allocator is defined in allocators.h
d825e6a3 103// CPrivKey is a serialized private key, with all parameters included (279 bytes)
223b6f1b 104typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
d825e6a3 105// CSecret is a serialization of just the secret parameter (32 bytes)
acd65016 106typedef std::vector<unsigned char, secure_allocator<unsigned char> > CSecret;
8bd66202 107
6b8de05d 108/** An encapsulated OpenSSL Elliptic Curve key (public and/or private) */
8bd66202
GA
109class CKey
110{
111protected:
112 EC_KEY* pkey;
113 bool fSet;
11529c6e
PW
114 bool fCompressedPubKey;
115
8bd66202 116public:
69fc8047 117 void SetCompressedPubKey(bool fCompressed = true);
11529c6e 118
096e06db 119 void Reset();
8bd66202 120
096e06db
GA
121 CKey();
122 CKey(const CKey& b);
11529c6e 123
096e06db 124 CKey& operator=(const CKey& b);
8bd66202 125
096e06db 126 ~CKey();
8bd66202 127
096e06db
GA
128 bool IsNull() const;
129 bool IsCompressed() const;
acd65016 130
096e06db
GA
131 void MakeNewKey(bool fCompressed);
132 bool SetPrivKey(const CPrivKey& vchPrivKey);
133 bool SetSecret(const CSecret& vchSecret, bool fCompressed = false);
134 CSecret GetSecret(bool &fCompressed) const;
135 CPrivKey GetPrivKey() const;
fd61d6f5
PW
136 bool SetPubKey(const CPubKey& vchPubKey);
137 CPubKey GetPubKey() const;
acd65016 138
096e06db 139 bool Sign(uint256 hash, std::vector<unsigned char>& vchSig);
8bd66202 140
01cc5263 141 // create a compact signature (65 bytes), which allows reconstructing the used public key
d825e6a3
PW
142 // The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
143 // The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
144 // 0x1D = second key with even y, 0x1E = second key with odd y
096e06db 145 bool SignCompact(uint256 hash, std::vector<unsigned char>& vchSig);
01cc5263
PW
146
147 // reconstruct public key from a compact signature
d825e6a3
PW
148 // This is only slightly more CPU intensive than just verifying it.
149 // If this function succeeds, the recovered public key is guaranteed to be valid
150 // (the signature is a valid signature of the given data for that key)
096e06db 151 bool SetCompactSignature(uint256 hash, const std::vector<unsigned char>& vchSig);
01cc5263 152
096e06db 153 bool Verify(uint256 hash, const std::vector<unsigned char>& vchSig);
2ffba736 154
d825e6a3 155 // Verify a compact signature
096e06db 156 bool VerifyCompact(uint256 hash, const std::vector<unsigned char>& vchSig);
91f43a33 157
096e06db 158 bool IsValid();
8bd66202 159};
223b6f1b
WL
160
161#endif
This page took 0.074406 seconds and 4 git commands to generate.