2014-10-28 18:47:18 -03:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2020-12-31 05:48:25 -03:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2017-06-06 03:21:34 -04:00
|
|
|
// Copyright (c) 2017 The Zcash developers
|
2014-11-04 10:34:04 -03:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-10-28 18:47:18 -03:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_PUBKEY_H
|
|
|
|
#define BITCOIN_PUBKEY_H
|
|
|
|
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <hash.h>
|
|
|
|
#include <serialize.h>
|
2020-09-11 18:33:30 -03:00
|
|
|
#include <span.h>
|
2017-11-09 21:57:53 -03:00
|
|
|
#include <uint256.h>
|
2014-10-28 18:47:18 -03:00
|
|
|
|
2021-04-21 15:16:13 -04:00
|
|
|
#include <cstring>
|
2021-05-23 21:32:30 -04:00
|
|
|
#include <optional>
|
2014-10-28 18:47:18 -03:00
|
|
|
#include <vector>
|
|
|
|
|
2015-06-01 11:35:19 -03:00
|
|
|
const unsigned int BIP32_EXTKEY_SIZE = 74;
|
|
|
|
|
2014-10-28 18:47:18 -03:00
|
|
|
/** A reference to a CKey: the Hash160 of its serialized public key */
|
|
|
|
class CKeyID : public uint160
|
|
|
|
{
|
|
|
|
public:
|
2014-12-15 05:11:16 -03:00
|
|
|
CKeyID() : uint160() {}
|
2017-08-01 06:22:41 -04:00
|
|
|
explicit CKeyID(const uint160& in) : uint160(in) {}
|
2014-10-28 18:47:18 -03:00
|
|
|
};
|
|
|
|
|
2015-04-21 19:09:37 -03:00
|
|
|
typedef uint256 ChainCode;
|
2014-01-25 16:05:00 -03:00
|
|
|
|
2014-10-28 18:47:18 -03:00
|
|
|
/** An encapsulated public key. */
|
|
|
|
class CPubKey
|
|
|
|
{
|
2017-10-04 10:41:40 -03:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* secp256k1:
|
|
|
|
*/
|
2019-03-04 06:26:31 -03:00
|
|
|
static constexpr unsigned int SIZE = 65;
|
|
|
|
static constexpr unsigned int COMPRESSED_SIZE = 33;
|
|
|
|
static constexpr unsigned int SIGNATURE_SIZE = 72;
|
|
|
|
static constexpr unsigned int COMPACT_SIGNATURE_SIZE = 65;
|
2017-10-04 10:41:40 -03:00
|
|
|
/**
|
|
|
|
* see www.keylength.com
|
|
|
|
* script supports up to 75 for single byte push
|
|
|
|
*/
|
|
|
|
static_assert(
|
2018-04-12 17:30:04 -03:00
|
|
|
SIZE >= COMPRESSED_SIZE,
|
|
|
|
"COMPRESSED_SIZE is larger than SIZE");
|
2017-10-04 10:41:40 -03:00
|
|
|
|
2014-10-28 18:47:18 -03:00
|
|
|
private:
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Just store the serialized data.
|
|
|
|
* Its length can very cheaply be computed from the first byte.
|
|
|
|
*/
|
2018-04-12 17:30:04 -03:00
|
|
|
unsigned char vch[SIZE];
|
2014-10-28 18:47:18 -03:00
|
|
|
|
|
|
|
//! Compute the length of a pubkey with a given first byte.
|
|
|
|
unsigned int static GetLen(unsigned char chHeader)
|
|
|
|
{
|
|
|
|
if (chHeader == 2 || chHeader == 3)
|
2018-04-12 17:30:04 -03:00
|
|
|
return COMPRESSED_SIZE;
|
2014-10-28 18:47:18 -03:00
|
|
|
if (chHeader == 4 || chHeader == 6 || chHeader == 7)
|
2018-04-12 17:30:04 -03:00
|
|
|
return SIZE;
|
2014-10-28 18:47:18 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Set this key data to be invalid
|
|
|
|
void Invalidate()
|
|
|
|
{
|
|
|
|
vch[0] = 0xFF;
|
|
|
|
}
|
|
|
|
|
|
|
|
public:
|
2018-02-16 17:28:03 -03:00
|
|
|
|
|
|
|
bool static ValidSize(const std::vector<unsigned char> &vch) {
|
|
|
|
return vch.size() > 0 && GetLen(vch[0]) == vch.size();
|
|
|
|
}
|
|
|
|
|
2014-10-28 18:47:18 -03:00
|
|
|
//! Construct an invalid public key.
|
|
|
|
CPubKey()
|
|
|
|
{
|
|
|
|
Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Initialize a public key using begin/end iterators to byte data.
|
|
|
|
template <typename T>
|
|
|
|
void Set(const T pbegin, const T pend)
|
|
|
|
{
|
|
|
|
int len = pend == pbegin ? 0 : GetLen(pbegin[0]);
|
|
|
|
if (len && len == (pend - pbegin))
|
|
|
|
memcpy(vch, (unsigned char*)&pbegin[0], len);
|
|
|
|
else
|
|
|
|
Invalidate();
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Construct a public key using begin/end iterators to byte data.
|
|
|
|
template <typename T>
|
|
|
|
CPubKey(const T pbegin, const T pend)
|
|
|
|
{
|
|
|
|
Set(pbegin, pend);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Construct a public key from a byte vector.
|
2021-04-30 09:32:40 -04:00
|
|
|
explicit CPubKey(Span<const uint8_t> _vch)
|
2014-10-28 18:47:18 -03:00
|
|
|
{
|
2016-09-02 13:19:01 -03:00
|
|
|
Set(_vch.begin(), _vch.end());
|
2014-10-28 18:47:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Simple read-only vector-like interface to the pubkey data.
|
|
|
|
unsigned int size() const { return GetLen(vch[0]); }
|
2018-06-27 19:53:48 -04:00
|
|
|
const unsigned char* data() const { return vch; }
|
2014-10-28 18:47:18 -03:00
|
|
|
const unsigned char* begin() const { return vch; }
|
|
|
|
const unsigned char* end() const { return vch + size(); }
|
|
|
|
const unsigned char& operator[](unsigned int pos) const { return vch[pos]; }
|
|
|
|
|
|
|
|
//! Comparator implementation.
|
|
|
|
friend bool operator==(const CPubKey& a, const CPubKey& b)
|
|
|
|
{
|
|
|
|
return a.vch[0] == b.vch[0] &&
|
|
|
|
memcmp(a.vch, b.vch, a.size()) == 0;
|
|
|
|
}
|
|
|
|
friend bool operator!=(const CPubKey& a, const CPubKey& b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
friend bool operator<(const CPubKey& a, const CPubKey& b)
|
|
|
|
{
|
|
|
|
return a.vch[0] < b.vch[0] ||
|
|
|
|
(a.vch[0] == b.vch[0] && memcmp(a.vch, b.vch, a.size()) < 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Implement serialization, as if this was a byte vector.
|
|
|
|
template <typename Stream>
|
2016-10-28 20:29:17 -03:00
|
|
|
void Serialize(Stream& s) const
|
2014-10-28 18:47:18 -03:00
|
|
|
{
|
|
|
|
unsigned int len = size();
|
|
|
|
::WriteCompactSize(s, len);
|
|
|
|
s.write((char*)vch, len);
|
|
|
|
}
|
|
|
|
template <typename Stream>
|
2016-10-28 20:29:17 -03:00
|
|
|
void Unserialize(Stream& s)
|
2014-10-28 18:47:18 -03:00
|
|
|
{
|
2021-11-06 15:08:29 -03:00
|
|
|
const unsigned int len(::ReadCompactSize(s));
|
2018-04-12 17:30:04 -03:00
|
|
|
if (len <= SIZE) {
|
2014-10-28 18:47:18 -03:00
|
|
|
s.read((char*)vch, len);
|
2020-06-10 10:34:28 -04:00
|
|
|
if (len != size()) {
|
|
|
|
Invalidate();
|
|
|
|
}
|
2014-10-28 18:47:18 -03:00
|
|
|
} else {
|
|
|
|
// invalid pubkey, skip available data
|
2021-11-06 15:08:29 -03:00
|
|
|
s.ignore(len);
|
2014-10-28 18:47:18 -03:00
|
|
|
Invalidate();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//! Get the KeyID of this public key (hash of its serialization)
|
|
|
|
CKeyID GetID() const
|
|
|
|
{
|
2020-06-26 16:36:41 -04:00
|
|
|
return CKeyID(Hash160(MakeSpan(vch).first(size())));
|
2014-10-28 18:47:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
//! Get the 256-bit hash of this public key.
|
|
|
|
uint256 GetHash() const
|
|
|
|
{
|
2020-06-26 16:36:41 -04:00
|
|
|
return Hash(MakeSpan(vch).first(size()));
|
2014-10-28 18:47:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check syntactic correctness.
|
2016-07-18 22:39:46 -04:00
|
|
|
*
|
2020-10-20 19:26:29 -03:00
|
|
|
* When setting a pubkey (Set()) or deserializing fails (its header bytes
|
|
|
|
* don't match the length of the data), the size is set to 0. Thus,
|
|
|
|
* by checking size, one can observe whether Set() or deserialization has
|
|
|
|
* failed.
|
|
|
|
*
|
|
|
|
* This does not check for more than that. In particular, it does not verify
|
|
|
|
* that the coordinates correspond to a point on the curve (see IsFullyValid()
|
|
|
|
* for that instead).
|
|
|
|
*
|
2020-09-11 18:33:23 -03:00
|
|
|
* Note that this is consensus critical as CheckECDSASignature() calls it!
|
2014-10-28 18:47:18 -03:00
|
|
|
*/
|
|
|
|
bool IsValid() const
|
|
|
|
{
|
|
|
|
return size() > 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
//! fully validate whether this is a valid public key (more expensive than IsValid())
|
|
|
|
bool IsFullyValid() const;
|
|
|
|
|
|
|
|
//! Check whether this is a compressed public key.
|
|
|
|
bool IsCompressed() const
|
|
|
|
{
|
2018-04-12 17:30:04 -03:00
|
|
|
return size() == COMPRESSED_SIZE;
|
2014-10-28 18:47:18 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Verify a DER signature (~72 bytes).
|
|
|
|
* If this public key is not fully valid, the return value will be false.
|
|
|
|
*/
|
|
|
|
bool Verify(const uint256& hash, const std::vector<unsigned char>& vchSig) const;
|
|
|
|
|
2015-07-28 15:11:20 -03:00
|
|
|
/**
|
|
|
|
* Check whether a signature is normalized (lower-S).
|
|
|
|
*/
|
|
|
|
static bool CheckLowS(const std::vector<unsigned char>& vchSig);
|
|
|
|
|
2014-10-28 18:47:18 -03:00
|
|
|
//! Recover a public key from a compact signature.
|
|
|
|
bool RecoverCompact(const uint256& hash, const std::vector<unsigned char>& vchSig);
|
|
|
|
|
|
|
|
//! Turn this public key into an uncompressed public key.
|
|
|
|
bool Decompress();
|
|
|
|
|
|
|
|
//! Derive BIP32 child pubkey.
|
2015-04-21 19:09:37 -03:00
|
|
|
bool Derive(CPubKey& pubkeyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
|
2014-10-28 18:47:18 -03:00
|
|
|
};
|
|
|
|
|
2020-09-11 18:33:30 -03:00
|
|
|
class XOnlyPubKey
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
uint256 m_keydata;
|
|
|
|
|
|
|
|
public:
|
2021-05-23 20:38:18 -04:00
|
|
|
/** Construct an empty x-only pubkey. */
|
|
|
|
XOnlyPubKey() = default;
|
|
|
|
|
|
|
|
XOnlyPubKey(const XOnlyPubKey&) = default;
|
|
|
|
XOnlyPubKey& operator=(const XOnlyPubKey&) = default;
|
|
|
|
|
2021-02-01 23:53:24 -03:00
|
|
|
/** Determine if this pubkey is fully valid. This is true for approximately 50% of all
|
2021-10-30 22:39:15 -03:00
|
|
|
* possible 32-byte arrays. If false, VerifySchnorr, CheckTapTweak and CreateTapTweak
|
|
|
|
* will always fail. */
|
2021-02-01 23:53:24 -03:00
|
|
|
bool IsFullyValid() const;
|
|
|
|
|
2021-03-03 20:02:56 -03:00
|
|
|
/** Test whether this is the 0 key (the result of default construction). This implies
|
|
|
|
* !IsFullyValid(). */
|
|
|
|
bool IsNull() const { return m_keydata.IsNull(); }
|
|
|
|
|
2020-09-11 18:33:30 -03:00
|
|
|
/** Construct an x-only pubkey from exactly 32 bytes. */
|
2020-11-29 15:33:22 -03:00
|
|
|
explicit XOnlyPubKey(Span<const unsigned char> bytes);
|
2020-09-11 18:33:30 -03:00
|
|
|
|
2021-02-08 00:18:39 -03:00
|
|
|
/** Construct an x-only pubkey from a normal pubkey. */
|
|
|
|
explicit XOnlyPubKey(const CPubKey& pubkey) : XOnlyPubKey(Span<const unsigned char>(pubkey.begin() + 1, pubkey.begin() + 33)) {}
|
|
|
|
|
2020-09-11 18:33:30 -03:00
|
|
|
/** Verify a Schnorr signature against this public key.
|
|
|
|
*
|
|
|
|
* sigbytes must be exactly 64 bytes.
|
|
|
|
*/
|
|
|
|
bool VerifySchnorr(const uint256& msg, Span<const unsigned char> sigbytes) const;
|
2021-02-01 21:21:59 -03:00
|
|
|
|
|
|
|
/** Compute the Taproot tweak as specified in BIP341, with *this as internal
|
|
|
|
* key:
|
|
|
|
* - if merkle_root == nullptr: H_TapTweak(xonly_pubkey)
|
|
|
|
* - otherwise: H_TapTweak(xonly_pubkey || *merkle_root)
|
|
|
|
*
|
|
|
|
* Note that the behavior of this function with merkle_root != nullptr is
|
|
|
|
* consensus critical.
|
|
|
|
*/
|
|
|
|
uint256 ComputeTapTweakHash(const uint256* merkle_root) const;
|
|
|
|
|
|
|
|
/** Verify that this is a Taproot tweaked output point, against a specified internal key,
|
|
|
|
* Merkle root, and parity. */
|
|
|
|
bool CheckTapTweak(const XOnlyPubKey& internal, const uint256& merkle_root, bool parity) const;
|
2020-09-11 18:33:30 -03:00
|
|
|
|
2021-05-23 21:32:30 -04:00
|
|
|
/** Construct a Taproot tweaked output point with this point as internal key. */
|
|
|
|
std::optional<std::pair<XOnlyPubKey, bool>> CreateTapTweak(const uint256* merkle_root) const;
|
|
|
|
|
2021-06-28 15:00:33 -04:00
|
|
|
/** Returns a list of CKeyIDs for the CPubKeys that could have been used to create this XOnlyPubKey.
|
|
|
|
* This is needed for key lookups since keys are indexed by CKeyID.
|
|
|
|
*/
|
|
|
|
std::vector<CKeyID> GetKeyIDs() const;
|
|
|
|
|
2020-09-11 18:33:30 -03:00
|
|
|
const unsigned char& operator[](int pos) const { return *(m_keydata.begin() + pos); }
|
2020-09-11 18:33:45 -03:00
|
|
|
const unsigned char* data() const { return m_keydata.begin(); }
|
2021-05-23 20:38:18 -04:00
|
|
|
static constexpr size_t size() { return decltype(m_keydata)::size(); }
|
|
|
|
const unsigned char* begin() const { return m_keydata.begin(); }
|
|
|
|
const unsigned char* end() const { return m_keydata.end(); }
|
|
|
|
unsigned char* begin() { return m_keydata.begin(); }
|
|
|
|
unsigned char* end() { return m_keydata.end(); }
|
|
|
|
bool operator==(const XOnlyPubKey& other) const { return m_keydata == other.m_keydata; }
|
|
|
|
bool operator!=(const XOnlyPubKey& other) const { return m_keydata != other.m_keydata; }
|
|
|
|
bool operator<(const XOnlyPubKey& other) const { return m_keydata < other.m_keydata; }
|
2020-09-11 18:33:30 -03:00
|
|
|
};
|
|
|
|
|
2014-10-28 18:47:18 -03:00
|
|
|
struct CExtPubKey {
|
|
|
|
unsigned char nDepth;
|
|
|
|
unsigned char vchFingerprint[4];
|
|
|
|
unsigned int nChild;
|
2015-04-21 19:09:37 -03:00
|
|
|
ChainCode chaincode;
|
2014-10-28 18:47:18 -03:00
|
|
|
CPubKey pubkey;
|
|
|
|
|
2014-01-25 16:05:00 -03:00
|
|
|
friend bool operator==(const CExtPubKey &a, const CExtPubKey &b)
|
2014-10-28 18:47:18 -03:00
|
|
|
{
|
2016-07-18 22:39:46 -04:00
|
|
|
return a.nDepth == b.nDepth &&
|
2021-04-30 13:52:00 -04:00
|
|
|
memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
|
2016-07-18 22:39:46 -04:00
|
|
|
a.nChild == b.nChild &&
|
|
|
|
a.chaincode == b.chaincode &&
|
|
|
|
a.pubkey == b.pubkey;
|
2014-10-28 18:47:18 -03:00
|
|
|
}
|
|
|
|
|
2019-07-10 16:38:12 -04:00
|
|
|
friend bool operator!=(const CExtPubKey &a, const CExtPubKey &b)
|
|
|
|
{
|
|
|
|
return !(a == b);
|
|
|
|
}
|
|
|
|
|
2015-06-01 11:35:19 -03:00
|
|
|
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
|
|
|
|
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
|
2014-10-28 18:47:18 -03:00
|
|
|
bool Derive(CExtPubKey& out, unsigned int nChild) const;
|
|
|
|
};
|
|
|
|
|
2015-07-28 15:11:20 -03:00
|
|
|
/** Users of this module must hold an ECCVerifyHandle. The constructor and
|
|
|
|
* destructor of these are not allowed to run in parallel, though. */
|
|
|
|
class ECCVerifyHandle
|
|
|
|
{
|
|
|
|
static int refcount;
|
|
|
|
|
|
|
|
public:
|
|
|
|
ECCVerifyHandle();
|
|
|
|
~ECCVerifyHandle();
|
|
|
|
};
|
|
|
|
|
2021-02-08 05:15:51 -03:00
|
|
|
typedef struct secp256k1_context_struct secp256k1_context;
|
|
|
|
|
|
|
|
/** Access to the internal secp256k1 context used for verification. Only intended to be used
|
|
|
|
* by key.cpp. */
|
|
|
|
const secp256k1_context* GetVerifyContext();
|
|
|
|
|
2014-10-28 18:47:18 -03:00
|
|
|
#endif // BITCOIN_PUBKEY_H
|