1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2017 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 explicit 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 explicit 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
&&
210 memcmp(&a
.vchFingerprint
[0], &b
.vchFingerprint
[0], sizeof(vchFingerprint
)) == 0 &&
211 a
.nChild
== b
.nChild
&&
212 a
.chaincode
== b
.chaincode
&&
213 a
.pubkey
== b
.pubkey
;
216 void Encode(unsigned char code
[BIP32_EXTKEY_SIZE
]) const;
217 void Decode(const unsigned char code
[BIP32_EXTKEY_SIZE
]);
218 bool Derive(CExtPubKey
& out
, unsigned int nChild
) const;
220 void Serialize(CSizeComputer
& s
) const
222 // Optimized implementation for ::GetSerializeSize that avoids copying.
223 s
.seek(BIP32_EXTKEY_SIZE
+ 1); // add one byte for the size (compact int)
225 template <typename Stream
>
226 void Serialize(Stream
& s
) const
228 unsigned int len
= BIP32_EXTKEY_SIZE
;
229 ::WriteCompactSize(s
, len
);
230 unsigned char code
[BIP32_EXTKEY_SIZE
];
232 s
.write((const char *)&code
[0], len
);
234 template <typename Stream
>
235 void Unserialize(Stream
& s
)
237 unsigned int len
= ::ReadCompactSize(s
);
238 unsigned char code
[BIP32_EXTKEY_SIZE
];
239 if (len
!= BIP32_EXTKEY_SIZE
)
240 throw std::runtime_error("Invalid extended key size\n");
241 s
.read((char *)&code
[0], len
);
246 /** Users of this module must hold an ECCVerifyHandle. The constructor and
247 * destructor of these are not allowed to run in parallel, though. */
248 class ECCVerifyHandle
257 #endif // BITCOIN_PUBKEY_H