[depends] ZeroMQ 4.1.5
[bitcoinplatinum.git] / src / pubkey.h
blobdb5444ea9d745df424ca569695eb9f872c0a2cf6
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2015 The Bitcoin Core developers
3 // Distributed under the MIT software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 #ifndef BITCOIN_PUBKEY_H
7 #define BITCOIN_PUBKEY_H
9 #include "hash.h"
10 #include "serialize.h"
11 #include "uint256.h"
13 #include <stdexcept>
14 #include <vector>
16 /**
17 * secp256k1:
18 * const unsigned int PRIVATE_KEY_SIZE = 279;
19 * const unsigned int PUBLIC_KEY_SIZE = 65;
20 * const unsigned int SIGNATURE_SIZE = 72;
22 * see www.keylength.com
23 * script supports up to 75 for single byte push
26 const unsigned int BIP32_EXTKEY_SIZE = 74;
28 /** A reference to a CKey: the Hash160 of its serialized public key */
29 class CKeyID : public uint160
31 public:
32 CKeyID() : uint160() {}
33 CKeyID(const uint160& in) : uint160(in) {}
36 typedef uint256 ChainCode;
38 /** An encapsulated public key. */
39 class CPubKey
41 private:
43 /**
44 * Just store the serialized data.
45 * Its length can very cheaply be computed from the first byte.
47 unsigned char vch[65];
49 //! Compute the length of a pubkey with a given first byte.
50 unsigned int static GetLen(unsigned char chHeader)
52 if (chHeader == 2 || chHeader == 3)
53 return 33;
54 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
55 return 65;
56 return 0;
59 //! Set this key data to be invalid
60 void Invalidate()
62 vch[0] = 0xFF;
65 public:
66 //! Construct an invalid public key.
67 CPubKey()
69 Invalidate();
72 //! Initialize a public key using begin/end iterators to byte data.
73 template <typename T>
74 void Set(const T pbegin, const T pend)
76 int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
77 if (len && len == (pend - pbegin))
78 memcpy(vch, (unsigned char*)&pbegin[0], len);
79 else
80 Invalidate();
83 //! Construct a public key using begin/end iterators to byte data.
84 template <typename T>
85 CPubKey(const T pbegin, const T pend)
87 Set(pbegin, pend);
90 //! Construct a public key from a byte vector.
91 CPubKey(const std::vector<unsigned char>& vch)
93 Set(vch.begin(), vch.end());
96 //! Simple read-only vector-like interface to the pubkey data.
97 unsigned int size() const { return GetLen(vch[0]); }
98 const unsigned char* begin() const { return vch; }
99 const unsigned char* end() const { return vch + size(); }
100 const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
102 //! Comparator implementation.
103 friend bool operator==(const CPubKey& a, const CPubKey& b)
105 return a.vch[0] == b.vch[0] &&
106 memcmp(a.vch, b.vch, a.size()) == 0;
108 friend bool operator!=(const CPubKey& a, const CPubKey& b)
110 return !(a == b);
112 friend bool operator<(const CPubKey& a, const CPubKey& b)
114 return a.vch[0] < b.vch[0] ||
115 (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
118 //! Implement serialization, as if this was a byte vector.
119 unsigned int GetSerializeSize(int nType, int nVersion) const
121 return size() + 1;
123 template <typename Stream>
124 void Serialize(Stream& s, int nType, int nVersion) const
126 unsigned int len = size();
127 ::WriteCompactSize(s, len);
128 s.write((char*)vch, len);
130 template <typename Stream>
131 void Unserialize(Stream& s, int nType, int nVersion)
133 unsigned int len = ::ReadCompactSize(s);
134 if (len <= 65) {
135 s.read((char*)vch, len);
136 } else {
137 // invalid pubkey, skip available data
138 char dummy;
139 while (len--)
140 s.read(&dummy, 1);
141 Invalidate();
145 //! Get the KeyID of this public key (hash of its serialization)
146 CKeyID GetID() const
148 return CKeyID(Hash160(vch, vch + size()));
151 //! Get the 256-bit hash of this public key.
152 uint256 GetHash() const
154 return Hash(vch, vch + size());
158 * Check syntactic correctness.
160 * Note that this is consensus critical as CheckSig() calls it!
162 bool IsValid() const
164 return size() > 0;
167 //! fully validate whether this is a valid public key (more expensive than IsValid())
168 bool IsFullyValid() const;
170 //! Check whether this is a compressed public key.
171 bool IsCompressed() const
173 return size() == 33;
177 * Verify a DER signature (~72 bytes).
178 * If this public key is not fully valid, the return value will be false.
180 bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
183 * Check whether a signature is normalized (lower-S).
185 static bool CheckLowS(const std::vector<unsigned char>& vchSig);
187 //! Recover a public key from a compact signature.
188 bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
190 //! Turn this public key into an uncompressed public key.
191 bool Decompress();
193 //! Derive BIP32 child pubkey.
194 bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
197 struct CExtPubKey {
198 unsigned char nDepth;
199 unsigned char vchFingerprint[4];
200 unsigned int nChild;
201 ChainCode chaincode;
202 CPubKey pubkey;
204 friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
206 return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
207 a.chaincode == b.chaincode && a.pubkey == b.pubkey;
210 void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
211 void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
212 bool Derive(CExtPubKey& out, unsigned int nChild) const;
214 unsigned int GetSerializeSize(int nType, int nVersion) const
216 return BIP32_EXTKEY_SIZE+1; //add one byte for the size (compact int)
218 template <typename Stream>
219 void Serialize(Stream& s, int nType, int nVersion) const
221 unsigned int len = BIP32_EXTKEY_SIZE;
222 ::WriteCompactSize(s, len);
223 unsigned char code[BIP32_EXTKEY_SIZE];
224 Encode(code);
225 s.write((const char *)&code[0], len);
227 template <typename Stream>
228 void Unserialize(Stream& s, int nType, int nVersion)
230 unsigned int len = ::ReadCompactSize(s);
231 unsigned char code[BIP32_EXTKEY_SIZE];
232 if (len != BIP32_EXTKEY_SIZE)
233 throw std::runtime_error("Invalid extended key size\n");
234 s.read((char *)&code[0], len);
235 Decode(code);
239 /** Users of this module must hold an ECCVerifyHandle. The constructor and
240 * destructor of these are not allowed to run in parallel, though. */
241 class ECCVerifyHandle
243 static int refcount;
245 public:
246 ECCVerifyHandle();
247 ~ECCVerifyHandle();
250 #endif // BITCOIN_PUBKEY_H