2010-07-14 15:54:31 +00:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2022-12-24 23:49:50 +00:00
|
|
|
// Copyright (c) 2009-2022 The Bitcoin Core developers
|
2017-06-06 19:21:34 +12:00
|
|
|
// Copyright (c) 2017 The Zcash developers
|
2014-10-26 16:33:52 +08:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2012-05-18 22:02:28 +08:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
2013-04-13 00:13:08 -05:00
|
|
|
|
2011-05-15 09:11:04 +02:00
|
|
|
#ifndef BITCOIN_KEY_H
|
|
|
|
#define BITCOIN_KEY_H
|
2010-07-14 15:54:31 +00:00
|
|
|
|
2017-11-10 13:57:53 +13:00
|
|
|
#include <pubkey.h>
|
|
|
|
#include <serialize.h>
|
|
|
|
#include <support/allocators/secure.h>
|
|
|
|
#include <uint256.h>
|
2013-04-13 00:13:08 -05:00
|
|
|
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <vector>
|
2011-06-26 16:11:56 +02:00
|
|
|
|
2014-11-19 10:53:46 +01:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
/**
|
2017-06-06 19:21:34 +12:00
|
|
|
* CPrivKey is a serialized private key, with all parameters included
|
2018-04-12 13:30:04 -07:00
|
|
|
* (SIZE bytes)
|
2014-10-26 16:33:52 +08:00
|
|
|
*/
|
2011-05-15 09:11:04 +02:00
|
|
|
typedef std::vector<unsigned char, secure_allocator<unsigned char> > CPrivKey;
|
2010-07-14 15:54:31 +00:00
|
|
|
|
2023-04-17 14:25:41 -04:00
|
|
|
/** Size of ECDH shared secrets. */
|
|
|
|
constexpr static size_t ECDH_SECRET_SIZE = CSHA256::OUTPUT_SIZE;
|
|
|
|
|
|
|
|
// Used to represent ECDH shared secret (ECDH_SECRET_SIZE bytes)
|
|
|
|
using ECDHSecret = std::array<std::byte, ECDH_SECRET_SIZE>;
|
|
|
|
|
2024-07-16 18:34:10 +02:00
|
|
|
class KeyPair;
|
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
/** An encapsulated private key. */
|
2014-09-19 19:21:46 +02:00
|
|
|
class CKey
|
|
|
|
{
|
2017-10-04 14:41:40 +01:00
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* secp256k1:
|
|
|
|
*/
|
2018-04-12 13:30:04 -07:00
|
|
|
static const unsigned int SIZE = 279;
|
|
|
|
static const unsigned int COMPRESSED_SIZE = 214;
|
2017-10-04 14:41:40 +01:00
|
|
|
/**
|
|
|
|
* see www.keylength.com
|
|
|
|
* script supports up to 75 for single byte push
|
|
|
|
*/
|
|
|
|
static_assert(
|
2018-04-12 13:30:04 -07:00
|
|
|
SIZE >= COMPRESSED_SIZE,
|
|
|
|
"COMPRESSED_SIZE is larger than SIZE");
|
2017-10-04 14:41:40 +01:00
|
|
|
|
2013-05-01 06:52:05 +02:00
|
|
|
private:
|
2023-09-18 12:10:19 -04:00
|
|
|
/** Internal data container for private key material. */
|
|
|
|
using KeyType = std::array<unsigned char, 32>;
|
2011-11-21 02:46:28 +01:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Whether the public key corresponding to this private key is (to be) compressed.
|
2023-01-31 11:50:10 +00:00
|
|
|
bool fCompressed{false};
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2023-09-18 12:10:19 -04:00
|
|
|
//! The actual byte data. nullptr for invalid keys.
|
|
|
|
secure_unique_ptr<KeyType> keydata;
|
2016-07-18 19:39:46 -07:00
|
|
|
|
2017-03-18 18:13:55 -07:00
|
|
|
//! Check whether the 32-byte array pointed to by vch is valid keydata.
|
2014-09-19 19:21:46 +02:00
|
|
|
bool static Check(const unsigned char* vch);
|
2011-11-21 02:46:28 +01:00
|
|
|
|
2023-09-18 12:10:19 -04:00
|
|
|
void MakeKeyData()
|
|
|
|
{
|
|
|
|
if (!keydata) keydata = make_secure_unique<KeyType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearKeyData()
|
|
|
|
{
|
|
|
|
keydata.reset();
|
|
|
|
}
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
public:
|
2023-09-18 12:10:19 -04:00
|
|
|
CKey() noexcept = default;
|
|
|
|
CKey(CKey&&) noexcept = default;
|
|
|
|
CKey& operator=(CKey&&) noexcept = default;
|
|
|
|
|
|
|
|
CKey& operator=(const CKey& other)
|
2014-09-19 19:21:46 +02:00
|
|
|
{
|
2024-06-05 17:42:27 +00:00
|
|
|
if (this != &other) {
|
|
|
|
if (other.keydata) {
|
|
|
|
MakeKeyData();
|
|
|
|
*keydata = *other.keydata;
|
|
|
|
} else {
|
|
|
|
ClearKeyData();
|
|
|
|
}
|
|
|
|
fCompressed = other.fCompressed;
|
2023-09-18 12:10:19 -04:00
|
|
|
}
|
|
|
|
return *this;
|
2013-05-01 06:52:05 +02:00
|
|
|
}
|
2011-11-21 02:46:28 +01:00
|
|
|
|
2023-09-18 12:10:19 -04:00
|
|
|
CKey(const CKey& other) { *this = other; }
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
friend bool operator==(const CKey& a, const CKey& b)
|
|
|
|
{
|
2016-07-18 19:39:46 -07:00
|
|
|
return a.fCompressed == b.fCompressed &&
|
|
|
|
a.size() == b.size() &&
|
2023-09-18 12:10:19 -04:00
|
|
|
memcmp(a.data(), b.data(), a.size()) == 0;
|
2013-07-15 01:05:25 +02:00
|
|
|
}
|
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Initialize using begin and end iterators to byte data.
|
2014-09-19 19:21:46 +02:00
|
|
|
template <typename T>
|
|
|
|
void Set(const T pbegin, const T pend, bool fCompressedIn)
|
|
|
|
{
|
2023-09-18 12:10:19 -04:00
|
|
|
if (size_t(pend - pbegin) != std::tuple_size_v<KeyType>) {
|
|
|
|
ClearKeyData();
|
2023-12-22 16:50:07 +01:00
|
|
|
} else if (Check(UCharCast(&pbegin[0]))) {
|
2023-09-18 12:10:19 -04:00
|
|
|
MakeKeyData();
|
|
|
|
memcpy(keydata->data(), (unsigned char*)&pbegin[0], keydata->size());
|
2013-05-01 06:52:05 +02:00
|
|
|
fCompressed = fCompressedIn;
|
|
|
|
} else {
|
2023-09-18 12:10:19 -04:00
|
|
|
ClearKeyData();
|
2013-05-01 06:52:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Simple read-only vector-like interface.
|
2023-09-18 12:10:19 -04:00
|
|
|
unsigned int size() const { return keydata ? keydata->size() : 0; }
|
|
|
|
const std::byte* data() const { return keydata ? reinterpret_cast<const std::byte*>(keydata->data()) : nullptr; }
|
2023-12-22 16:50:07 +01:00
|
|
|
const std::byte* begin() const { return data(); }
|
|
|
|
const std::byte* end() const { return data() + size(); }
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Check whether this private key is valid.
|
2023-09-18 12:10:19 -04:00
|
|
|
bool IsValid() const { return !!keydata; }
|
2010-07-14 15:54:31 +00:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Check whether the public key corresponding to this private key is (to be) compressed.
|
2013-05-01 06:52:05 +02:00
|
|
|
bool IsCompressed() const { return fCompressed; }
|
2011-06-25 14:57:32 +02:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Generate a new private key using a cryptographic PRNG.
|
2012-05-16 12:36:38 -04:00
|
|
|
void MakeNewKey(bool fCompressed);
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
/**
|
|
|
|
* Convert the private key to a CPrivKey (serialized OpenSSL private key data).
|
2016-07-18 19:39:46 -07:00
|
|
|
* This is expensive.
|
2014-10-26 16:33:52 +08:00
|
|
|
*/
|
2012-05-16 12:36:38 -04:00
|
|
|
CPrivKey GetPrivKey() const;
|
2013-05-01 06:52:05 +02:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
/**
|
|
|
|
* Compute the public key from a private key.
|
|
|
|
* This is expensive.
|
|
|
|
*/
|
2012-05-14 19:07:52 +02:00
|
|
|
CPubKey GetPubKey() const;
|
2011-06-25 14:57:32 +02:00
|
|
|
|
2014-11-06 06:54:50 -08:00
|
|
|
/**
|
|
|
|
* Create a DER-serialized signature.
|
2015-03-27 15:31:44 -07:00
|
|
|
* The test_case parameter tweaks the deterministic nonce.
|
2014-11-06 06:54:50 -08:00
|
|
|
*/
|
2018-07-14 16:03:28 -07:00
|
|
|
bool Sign(const uint256& hash, std::vector<unsigned char>& vchSig, bool grind = true, uint32_t test_case = 0) const;
|
2010-07-14 15:54:31 +00:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
/**
|
|
|
|
* Create a compact signature (65 bytes), which allows reconstructing the used public key.
|
|
|
|
* The format is one header byte, followed by two times 32 bytes for the serialized r and s values.
|
|
|
|
* The header byte: 0x1B = first key with even y, 0x1C = first key with odd y,
|
|
|
|
* 0x1D = second key with even y, 0x1E = second key with odd y,
|
|
|
|
* add 0x04 for compressed keys.
|
|
|
|
*/
|
2014-09-19 19:21:46 +02:00
|
|
|
bool SignCompact(const uint256& hash, std::vector<unsigned char>& vchSig) const;
|
2013-07-15 01:05:25 +02:00
|
|
|
|
2021-02-08 00:15:51 -08:00
|
|
|
/**
|
|
|
|
* Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this,
|
2021-10-28 13:46:52 -04:00
|
|
|
* optionally tweaked by *merkle_root. Additional nonce entropy is provided through
|
2021-02-08 00:15:51 -08:00
|
|
|
* aux.
|
|
|
|
*
|
2021-06-17 16:54:56 -07:00
|
|
|
* merkle_root is used to optionally perform tweaking of the private key, as specified
|
|
|
|
* in BIP341:
|
|
|
|
* - If merkle_root == nullptr: no tweaking is done, sign with key directly (this is
|
|
|
|
* used for signatures in BIP342 script).
|
|
|
|
* - If merkle_root->IsNull(): sign with key + H_TapTweak(pubkey) (this is used for
|
|
|
|
* key path spending when no scripts are present).
|
|
|
|
* - Otherwise: sign with key + H_TapTweak(pubkey || *merkle_root)
|
|
|
|
* (this is used for key path spending, with specific
|
|
|
|
* Merkle root of the script tree).
|
2021-02-08 00:15:51 -08:00
|
|
|
*/
|
2021-10-28 13:46:52 -04:00
|
|
|
bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root, const uint256& aux) const;
|
2021-02-08 00:15:51 -08:00
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Derive BIP32 child key.
|
2022-08-04 11:28:57 +02:00
|
|
|
[[nodiscard]] bool Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const;
|
2013-10-28 11:20:26 +01:00
|
|
|
|
2014-11-06 01:17:48 -08:00
|
|
|
/**
|
|
|
|
* Verify thoroughly whether a private key and a public key match.
|
|
|
|
* This is done using a different mechanism than just regenerating it.
|
|
|
|
*/
|
|
|
|
bool VerifyPubKey(const CPubKey& vchPubKey) const;
|
|
|
|
|
2014-10-26 16:33:52 +08:00
|
|
|
//! Load private key and check that public key matches.
|
2018-01-23 13:16:56 -05:00
|
|
|
bool Load(const CPrivKey& privkey, const CPubKey& vchPubKey, bool fSkipCheck);
|
2023-04-17 14:25:41 -04:00
|
|
|
|
|
|
|
/** Create an ellswift-encoded public key for this key, with specified entropy.
|
|
|
|
*
|
|
|
|
* entropy must be a 32-byte span with additional entropy to use in the encoding. Every
|
|
|
|
* public key has ~2^256 different encodings, and this function will deterministically pick
|
|
|
|
* one of them, based on entropy. Note that even without truly random entropy, the
|
|
|
|
* resulting encoding will be indistinguishable from uniform to any adversary who does not
|
|
|
|
* know the private key (because the private key itself is always used as entropy as well).
|
|
|
|
*/
|
|
|
|
EllSwiftPubKey EllSwiftCreate(Span<const std::byte> entropy) const;
|
|
|
|
|
|
|
|
/** Compute a BIP324-style ECDH shared secret.
|
|
|
|
*
|
|
|
|
* - their_ellswift: EllSwiftPubKey that was received from the other side.
|
|
|
|
* - our_ellswift: EllSwiftPubKey that was sent to the other side (must have been generated
|
|
|
|
* from *this using EllSwiftCreate()).
|
|
|
|
* - initiating: whether we are the initiating party (true) or responding party (false).
|
|
|
|
*/
|
|
|
|
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift,
|
|
|
|
const EllSwiftPubKey& our_ellswift,
|
|
|
|
bool initiating) const;
|
2024-07-16 18:34:10 +02:00
|
|
|
/** Compute a KeyPair
|
|
|
|
*
|
|
|
|
* Wraps a `secp256k1_keypair` type.
|
2024-07-16 18:36:41 +02:00
|
|
|
*
|
|
|
|
* `merkle_root` is used to optionally perform tweaking of
|
|
|
|
* the internal key, as specified in BIP341:
|
|
|
|
*
|
|
|
|
* - If merkle_root == nullptr: no tweaking is done, use the internal key directly (this is
|
|
|
|
* used for signatures in BIP342 script).
|
|
|
|
* - If merkle_root->IsNull(): tweak the internal key with H_TapTweak(pubkey) (this is used for
|
|
|
|
* key path spending when no scripts are present).
|
|
|
|
* - Otherwise: tweak the internal key with H_TapTweak(pubkey || *merkle_root)
|
|
|
|
* (this is used for key path spending with the
|
|
|
|
* Merkle root of the script tree).
|
2024-07-16 18:34:10 +02:00
|
|
|
*/
|
2024-07-16 18:36:41 +02:00
|
|
|
KeyPair ComputeKeyPair(const uint256* merkle_root) const;
|
2013-07-15 01:05:25 +02:00
|
|
|
};
|
|
|
|
|
2023-09-12 03:35:40 +02:00
|
|
|
CKey GenerateRandomKey(bool compressed = true) noexcept;
|
|
|
|
|
2013-07-15 01:05:25 +02:00
|
|
|
struct CExtKey {
|
|
|
|
unsigned char nDepth;
|
|
|
|
unsigned char vchFingerprint[4];
|
|
|
|
unsigned int nChild;
|
2015-04-21 18:09:37 -04:00
|
|
|
ChainCode chaincode;
|
2013-07-15 01:05:25 +02:00
|
|
|
CKey key;
|
|
|
|
|
2014-09-19 19:21:46 +02:00
|
|
|
friend bool operator==(const CExtKey& a, const CExtKey& b)
|
|
|
|
{
|
2016-07-18 19:39:46 -07:00
|
|
|
return a.nDepth == b.nDepth &&
|
2021-04-30 19:52:00 +02:00
|
|
|
memcmp(a.vchFingerprint, b.vchFingerprint, sizeof(vchFingerprint)) == 0 &&
|
2016-07-18 19:39:46 -07:00
|
|
|
a.nChild == b.nChild &&
|
|
|
|
a.chaincode == b.chaincode &&
|
|
|
|
a.key == b.key;
|
2013-07-15 01:05:25 +02:00
|
|
|
}
|
|
|
|
|
2023-12-20 18:11:41 -05:00
|
|
|
CExtKey() = default;
|
|
|
|
CExtKey(const CExtPubKey& xpub, const CKey& key_in) : nDepth(xpub.nDepth), nChild(xpub.nChild), chaincode(xpub.chaincode), key(key_in)
|
|
|
|
{
|
|
|
|
std::copy(xpub.vchFingerprint, xpub.vchFingerprint + sizeof(xpub.vchFingerprint), vchFingerprint);
|
|
|
|
}
|
|
|
|
|
2015-06-01 16:35:19 +02:00
|
|
|
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const;
|
|
|
|
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE]);
|
2022-08-04 11:28:57 +02:00
|
|
|
[[nodiscard]] bool Derive(CExtKey& out, unsigned int nChild) const;
|
2013-07-15 01:05:25 +02:00
|
|
|
CExtPubKey Neuter() const;
|
2021-11-08 14:27:35 +01:00
|
|
|
void SetSeed(Span<const std::byte> seed);
|
2010-07-14 15:54:31 +00:00
|
|
|
};
|
2011-05-15 09:11:04 +02:00
|
|
|
|
2024-07-16 18:34:10 +02:00
|
|
|
/** KeyPair
|
|
|
|
*
|
|
|
|
* Wraps a `secp256k1_keypair` type, an opaque data structure for holding a secret and public key.
|
|
|
|
* This is intended for BIP340 keys and allows us to easily determine if the secret key needs to
|
|
|
|
* be negated by checking the parity of the public key. This class primarily intended for passing
|
|
|
|
* secret keys to libsecp256k1 functions expecting a `secp256k1_keypair`. For all other cases,
|
|
|
|
* CKey should be preferred.
|
2024-07-16 18:36:41 +02:00
|
|
|
*
|
|
|
|
* A KeyPair can be created from a CKey with an optional merkle_root tweak (per BIP342). See
|
|
|
|
* CKey::ComputeKeyPair for more details.
|
2024-07-16 18:34:10 +02:00
|
|
|
*/
|
|
|
|
class KeyPair
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KeyPair() noexcept = default;
|
|
|
|
KeyPair(KeyPair&&) noexcept = default;
|
|
|
|
KeyPair& operator=(KeyPair&&) noexcept = default;
|
|
|
|
KeyPair& operator=(const KeyPair& other)
|
|
|
|
{
|
|
|
|
if (this != &other) {
|
|
|
|
if (other.m_keypair) {
|
|
|
|
MakeKeyPairData();
|
|
|
|
*m_keypair = *other.m_keypair;
|
|
|
|
} else {
|
|
|
|
ClearKeyPairData();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
KeyPair(const KeyPair& other) { *this = other; }
|
|
|
|
|
2024-07-16 18:36:41 +02:00
|
|
|
friend KeyPair CKey::ComputeKeyPair(const uint256* merkle_root) const;
|
2024-07-16 18:34:10 +02:00
|
|
|
[[nodiscard]] bool SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256& aux) const;
|
|
|
|
|
|
|
|
//! Check whether this keypair is valid.
|
|
|
|
bool IsValid() const { return !!m_keypair; }
|
|
|
|
|
|
|
|
private:
|
2024-07-16 18:36:41 +02:00
|
|
|
KeyPair(const CKey& key, const uint256* merkle_root);
|
2024-07-16 18:34:10 +02:00
|
|
|
|
|
|
|
using KeyType = std::array<unsigned char, 96>;
|
|
|
|
secure_unique_ptr<KeyType> m_keypair;
|
|
|
|
|
|
|
|
void MakeKeyPairData()
|
|
|
|
{
|
|
|
|
if (!m_keypair) m_keypair = make_secure_unique<KeyType>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void ClearKeyPairData()
|
|
|
|
{
|
|
|
|
m_keypair.reset();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
Update key.cpp to use new libsecp256k1
libsecp256k1's API changed, so update key.cpp to use it.
Libsecp256k1 now has explicit context objects, which makes it completely thread-safe.
In turn, keep an explicit context object in key.cpp, which is explicitly initialized
destroyed. This is not really pretty now, but it's more efficient than the static
initialized object in key.cpp (which made for example bitcoin-tx slow, as for most of
its calls, libsecp256k1 wasn't actually needed).
This also brings in the new blinding support in libsecp256k1. By passing in a random
seed, temporary variables during the elliptic curve computations are altered, in such
a way that if an attacker does not know the blind, observing the internal operations
leaks less information about the keys used. This was implemented by Greg Maxwell.
2015-04-22 14:28:26 -07:00
|
|
|
/** Check that required EC support is available at runtime. */
|
2018-09-13 10:36:41 -07:00
|
|
|
bool ECC_InitSanityCheck();
|
2014-06-02 16:21:03 -07:00
|
|
|
|
2024-05-07 08:52:55 -04:00
|
|
|
/**
|
|
|
|
* RAII class initializing and deinitializing global state for elliptic curve support.
|
|
|
|
* Only one instance may be initialized at a time.
|
|
|
|
*
|
|
|
|
* In the future global ECC state could be removed, and this class could contain
|
|
|
|
* state and be passed as an argument to ECC key functions.
|
|
|
|
*/
|
|
|
|
class ECC_Context
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
ECC_Context();
|
|
|
|
~ECC_Context();
|
|
|
|
};
|
|
|
|
|
2014-08-28 22:21:03 +02:00
|
|
|
#endif // BITCOIN_KEY_H
|