1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2014 The Bitcoin Core developers
3 // Copyright (c) 2017 The Zcash developers
4 // Distributed under the MIT software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
7 #ifndef BITCOIN_PUBKEY_H
8 #define BITCOIN_PUBKEY_H
11 #include "serialize.h"
17 const unsigned int BIP32_EXTKEY_SIZE = 74;
19 /** A reference to a CKey: the Hash160 of its serialized public key */
20 class CKeyID : public uint160
23 CKeyID() : uint160() {}
24 CKeyID(const uint160& in) : uint160(in) {}
27 typedef uint256 ChainCode;
29 /** An encapsulated public key. */
36 static const unsigned int PUBLIC_KEY_SIZE = 65;
37 static const unsigned int COMPRESSED_PUBLIC_KEY_SIZE = 33;
38 static const unsigned int SIGNATURE_SIZE = 72;
39 static const unsigned int COMPACT_SIGNATURE_SIZE = 65;
41 * see www.keylength.com
42 * script supports up to 75 for single byte push
45 PUBLIC_KEY_SIZE >= COMPRESSED_PUBLIC_KEY_SIZE,
46 "COMPRESSED_PUBLIC_KEY_SIZE is larger than PUBLIC_KEY_SIZE");
51 * Just store the serialized data.
52 * Its length can very cheaply be computed from the first byte.
54 unsigned char vch[PUBLIC_KEY_SIZE];
56 //! Compute the length of a pubkey with a given first byte.
57 unsigned int static GetLen(unsigned char chHeader)
59 if (chHeader == 2 || chHeader == 3)
60 return COMPRESSED_PUBLIC_KEY_SIZE;
61 if (chHeader == 4 || chHeader == 6 || chHeader == 7)
62 return PUBLIC_KEY_SIZE;
66 //! Set this key data to be invalid
73 //! Construct an invalid public key.
79 //! Initialize a public key using begin/end iterators to byte data.
81 void Set(const T pbegin, const T pend)
83 int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
84 if (len && len == (pend - pbegin))
85 memcpy(vch, (unsigned char*)&pbegin[0], len);
90 //! Construct a public key using begin/end iterators to byte data.
92 CPubKey(const T pbegin, const T pend)
97 //! Construct a public key from a byte vector.
98 CPubKey(const std::vector<unsigned char>& vch)
100 Set(vch.begin(), vch.end());
103 //! Simple read-only vector-like interface to the pubkey data.
104 unsigned int size() const { return GetLen(vch[0]); }
105 const unsigned char* begin() const { return vch; }
106 const unsigned char* end() const { return vch + size(); }
107 const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
109 //! Comparator implementation.
110 friend bool operator==(const CPubKey& a, const CPubKey& b)
112 return a.vch[0] == b.vch[0] &&
113 memcmp(a.vch, b.vch, a.size()) == 0;
115 friend bool operator!=(const CPubKey& a, const CPubKey& b)
119 friend bool operator<(const CPubKey& a, const CPubKey& b)
121 return a.vch[0] < b.vch[0] ||
122 (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
125 //! Implement serialization, as if this was a byte vector.
126 template <typename Stream>
127 void Serialize(Stream& s) const
129 unsigned int len = size();
130 ::WriteCompactSize(s, len);
131 s.write((char*)vch, len);
133 template <typename Stream>
134 void Unserialize(Stream& s)
136 unsigned int len = ::ReadCompactSize(s);
137 if (len <= PUBLIC_KEY_SIZE) {
138 s.read((char*)vch, len);
140 // invalid pubkey, skip available data
148 //! Get the KeyID of this public key (hash of its serialization)
151 return CKeyID(Hash160(vch, vch + size()));
154 //! Get the 256-bit hash of this public key.
155 uint256 GetHash() const
157 return Hash(vch, vch + size());
161 * Check syntactic correctness.
163 * Note that this is consensus critical as CheckSig() calls it!
170 //! fully validate whether this is a valid public key (more expensive than IsValid())
171 bool IsFullyValid() const;
173 //! Check whether this is a compressed public key.
174 bool IsCompressed() const
176 return size() == COMPRESSED_PUBLIC_KEY_SIZE;
180 * Verify a DER signature (~72 bytes).
181 * If this public key is not fully valid, the return value will be false.
183 bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
186 * Check whether a signature is normalized (lower-S).
188 static bool CheckLowS(const std::vector<unsigned char>& vchSig);
190 //! Recover a public key from a compact signature.
191 bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
193 //! Turn this public key into an uncompressed public key.
196 //! Derive BIP32 child pubkey.
197 bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
201 unsigned char nDepth;
202 unsigned char vchFingerprint[4];
207 friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
209 return a.nDepth == b.nDepth && memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], 4) == 0 && a.nChild == b.nChild &&
210 a.chaincode == b.chaincode && a.pubkey == b.pubkey;
213 void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
214 void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
215 bool Derive(CExtPubKey& out, unsigned int nChild) const;
217 void Serialize(CSizeComputer& s) const
219 // Optimized implementation for ::GetSerializeSize that avoids copying.
220 s.seek(BIP32_EXTKEY_SIZE + 1); // add one byte for the size (compact int)
222 template <typename Stream>
223 void Serialize(Stream& s) const
225 unsigned int len = BIP32_EXTKEY_SIZE;
226 ::WriteCompactSize(s, len);
227 unsigned char code[BIP32_EXTKEY_SIZE];
229 s.write((const char *)&code[0], len);
231 template <typename Stream>
232 void Unserialize(Stream& s)
234 unsigned int len = ::ReadCompactSize(s);
235 unsigned char code[BIP32_EXTKEY_SIZE];
236 if (len != BIP32_EXTKEY_SIZE)
237 throw std::runtime_error("Invalid extended key size\n");
238 s.read((char *)&code[0], len);
243 /** Users of this module must hold an ECCVerifyHandle. The constructor and
244 * destructor of these are not allowed to run in parallel, though. */
245 class ECCVerifyHandle
254 #endif // BITCOIN_PUBKEY_H