Bitcoin Core  0.21.1
P2P Digital Currency
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules
pubkey.h
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2019 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.
6 
7 #ifndef BITCOIN_PUBKEY_H
8 #define BITCOIN_PUBKEY_H
9 
10 #include <hash.h>
11 #include <serialize.h>
12 #include <span.h>
13 #include <uint256.h>
14 
15 #include <stdexcept>
16 #include <vector>
17 
18 const unsigned int BIP32_EXTKEY_SIZE = 74;
19 
21 class CKeyID : public uint160
22 {
23 public:
24  CKeyID() : uint160() {}
25  explicit CKeyID(const uint160& in) : uint160(in) {}
26 };
27 
29 
31 class CPubKey
32 {
33 public:
37  static constexpr unsigned int SIZE = 65;
38  static constexpr unsigned int COMPRESSED_SIZE = 33;
39  static constexpr unsigned int SIGNATURE_SIZE = 72;
40  static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
45  static_assert(
46  SIZE >= COMPRESSED_SIZE,
47  "COMPRESSED_SIZE is larger than SIZE");
48 
49 private:
50 
55  unsigned char vch[SIZE];
56 
58  unsigned int static GetLen(unsigned char chHeader)
59  {
60  if (chHeader == 2 || chHeader == 3)
61  return COMPRESSED_SIZE;
62  if (chHeader == 4 || chHeader == 6 || chHeader == 7)
63  return SIZE;
64  return 0;
65  }
66 
68  void Invalidate()
69  {
70  vch[0] = 0xFF;
71  }
72 
73 public:
74 
75  bool static ValidSize(const std::vector<unsigned char> &vch) {
76  return vch.size() > 0 && GetLen(vch[0]) == vch.size();
77  }
78 
80  CPubKey()
81  {
82  Invalidate();
83  }
84 
86  template <typename T>
87  void Set(const T pbegin, const T pend)
88  {
89  int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
90  if (len && len == (pend - pbegin))
91  memcpy(vch, (unsigned char*)&pbegin[0], len);
92  else
93  Invalidate();
94  }
95 
97  template <typename T>
98  CPubKey(const T pbegin, const T pend)
99  {
100  Set(pbegin, pend);
101  }
102 
104  explicit CPubKey(const std::vector<unsigned char>& _vch)
105  {
106  Set(_vch.begin(), _vch.end());
107  }
108 
110  unsigned int size() const { return GetLen(vch[0]); }
111  const unsigned char* data() const { return vch; }
112  const unsigned char* begin() const { return vch; }
113  const unsigned char* end() const { return vch + size(); }
114  const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
115 
117  friend bool operator==(const CPubKey& a, const CPubKey& b)
118  {
119  return a.vch[0] == b.vch[0] &&
120  memcmp(a.vch, b.vch, a.size()) == 0;
121  }
122  friend bool operator!=(const CPubKey& a, const CPubKey& b)
123  {
124  return !(a == b);
125  }
126  friend bool operator<(const CPubKey& a, const CPubKey& b)
127  {
128  return a.vch[0] < b.vch[0] ||
129  (a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
130  }
131 
133  template <typename Stream>
134  void Serialize(Stream& s) const
135  {
136  unsigned int len = size();
137  ::WriteCompactSize(s, len);
138  s.write((char*)vch, len);
139  }
140  template <typename Stream>
141  void Unserialize(Stream& s)
142  {
143  unsigned int len = ::ReadCompactSize(s);
144  if (len <= SIZE) {
145  s.read((char*)vch, len);
146  if (len != size()) {
147  Invalidate();
148  }
149  } else {
150  // invalid pubkey, skip available data
151  char dummy;
152  while (len--)
153  s.read(&dummy, 1);
154  Invalidate();
155  }
156  }
157 
159  CKeyID GetID() const
160  {
161  return CKeyID(Hash160(MakeSpan(vch).first(size())));
162  }
163 
165  uint256 GetHash() const
166  {
167  return Hash(MakeSpan(vch).first(size()));
168  }
169 
170  /*
171  * Check syntactic correctness.
172  *
173  * Note that this is consensus critical as CheckECDSASignature() calls it!
174  */
175  bool IsValid() const
176  {
177  return size() > 0;
178  }
179 
181  bool IsFullyValid() const;
182 
184  bool IsCompressed() const
185  {
186  return size() == COMPRESSED_SIZE;
187  }
188 
193  bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
194 
198  static bool CheckLowS(const std::vector<unsigned char>& vchSig);
199 
201  bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
202 
204  bool Decompress();
205 
207  bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
208 };
209 
211 {
212 private:
214 
215 public:
218 
223  bool VerifySchnorr(const uint256& msg, Span<const unsigned char> sigbytes) const;
224  bool CheckPayToContract(const XOnlyPubKey& base, const uint256& hash, bool parity) const;
225 
226  const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
227  const unsigned char* data() const { return m_keydata.begin(); }
228  size_t size() const { return m_keydata.size(); }
229 };
230 
231 struct CExtPubKey {
232  unsigned char nDepth;
233  unsigned char vchFingerprint[4];
234  unsigned int nChild;
237 
238  friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
239  {
240  return a.nDepth == b.nDepth &&
241  memcmp(&a.vchFingerprint[0], &b.vchFingerprint[0], sizeof(vchFingerprint)) == 0 &&
242  a.nChild == b.nChild &&
243  a.chaincode == b.chaincode &&
244  a.pubkey == b.pubkey;
245  }
246 
247  friend bool operator!=(const CExtPubKey &a, const CExtPubKey &b)
248  {
249  return !(a == b);
250  }
251 
252  void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
253  void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
254  bool Derive(CExtPubKey& out, unsigned int nChild) const;
255 };
256 
260 {
261  static int refcount;
262 
263 public:
264  ECCVerifyHandle();
266 };
267 
268 #endif // BITCOIN_PUBKEY_H
bool CheckPayToContract(const XOnlyPubKey &base, const uint256 &hash, bool parity) const
Definition: pubkey.cpp:184
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:37
unsigned char vchFingerprint[4]
Definition: pubkey.h:233
uint256 ChainCode
Definition: pubkey.h:28
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: pubkey.cpp:275
bool operator==(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:585
uint64_t ReadCompactSize(Stream &is, bool range_check=true)
Decode a CompactSize-encoded variable-length integer.
Definition: serialize.h:318
void WriteCompactSize(CSizeComputer &os, uint64_t nSize)
Definition: serialize.h:1110
CKeyID(const uint160 &in)
Definition: pubkey.h:25
static int refcount
Definition: pubkey.h:261
unsigned char nDepth
Definition: pubkey.h:232
ChainCode chaincode
Definition: pubkey.h:235
unsigned char * begin()
Definition: uint256.h:58
unsigned int nChild
Definition: pubkey.h:234
bool Derive(CExtPubKey &out, unsigned int nChild) const
Definition: pubkey.cpp:293
void Serialize(Stream &s, char a)
Definition: serialize.h:227
const unsigned char & operator[](int pos) const
Definition: pubkey.h:226
Users of this module must hold an ECCVerifyHandle.
Definition: pubkey.h:259
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:238
friend bool operator!=(const CExtPubKey &a, const CExtPubKey &b)
Definition: pubkey.h:247
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:38
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: pubkey.cpp:285
An encapsulated public key.
Definition: pubkey.h:31
XOnlyPubKey(Span< const unsigned char > bytes)
Construct an x-only pubkey from exactly 32 bytes.
Definition: pubkey.cpp:170
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:40
uint256 m_keydata
Definition: pubkey.h:213
bool VerifySchnorr(const uint256 &msg, Span< const unsigned char > sigbytes) const
Verify a Schnorr signature against this public key.
Definition: pubkey.cpp:176
uint160 Hash160(const T1 &in1)
Compute the 160-bit hash an object.
Definition: hash.h:92
bool operator<(const CNetAddr &a, const CNetAddr &b)
Definition: netaddress.cpp:590
256-bit opaque blob.
Definition: uint256.h:124
CKeyID()
Definition: pubkey.h:24
void * memcpy(void *a, const void *b, size_t c)
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:18
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:21
160-bit opaque blob.
Definition: uint256.h:113
void Unserialize(Stream &s, char &a)
Definition: serialize.h:245
CPubKey pubkey
Definition: pubkey.h:236
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:82
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
const unsigned char * data() const
Definition: pubkey.h:227
size_t size() const
Definition: pubkey.h:228
static constexpr unsigned int SIGNATURE_SIZE
Definition: pubkey.h:39
unsigned int size() const
Definition: uint256.h:78
Span< A > constexpr MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:193