Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
key.h
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 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_KEY_H
8#define BITCOIN_KEY_H
9
10#include <pubkey.h>
12#include <uint256.h>
13
14#include <stdexcept>
15#include <vector>
16
22typedef std::vector<uint8_t, secure_allocator<uint8_t>> CPrivKey;
23
25using SchnorrSig = std::array<uint8_t, CPubKey::SCHNORR_SIZE>;
26
28class CKey {
29public:
33 static const unsigned int SIZE = 279;
34 static const unsigned int COMPRESSED_SIZE = 214;
39 static_assert(SIZE >= COMPRESSED_SIZE,
40 "COMPRESSED_SIZE is larger than SIZE");
41
42private:
46 bool fValid;
47
51
53 std::vector<uint8_t, secure_allocator<uint8_t>> keydata;
54
56 static bool Check(const uint8_t *vch);
57
58public:
61 // Important: vch must be 32 bytes in length to not break serialization
62 keydata.resize(32);
63 }
65 static CKey MakeCompressedKey();
68
69 friend bool operator==(const CKey &a, const CKey &b) {
70 return a.fCompressed == b.fCompressed && a.size() == b.size() &&
71 memcmp(a.keydata.data(), b.keydata.data(), a.size()) == 0;
72 }
73
75 template <typename T>
76 void Set(const T pbegin, const T pend, bool fCompressedIn) {
77 if (size_t(pend - pbegin) != keydata.size()) {
78 fValid = false;
79 } else if (Check(&pbegin[0])) {
80 memcpy(keydata.data(), (uint8_t *)&pbegin[0], keydata.size());
81 fValid = true;
83 } else {
84 fValid = false;
85 }
86 }
87
89 unsigned int size() const { return (fValid ? keydata.size() : 0); }
90 const uint8_t *begin() const { return keydata.data(); }
91 const uint8_t *end() const { return keydata.data() + size(); }
92
94 bool IsValid() const { return fValid; }
95
98 bool IsCompressed() const { return fCompressed; }
99
101 void MakeNewKey(bool fCompressed);
102
104 bool Negate();
105
111 CPrivKey GetPrivKey() const;
112
117 CPubKey GetPubKey() const;
118
123 bool SignECDSA(const uint256 &hash, std::vector<uint8_t> &vchSig,
124 bool grind = true, uint32_t test_case = 0) const;
125
130 bool SignSchnorr(const uint256 &hash, SchnorrSig &sig,
131 uint32_t test_case = 0) const;
132 bool SignSchnorr(const uint256 &hash, std::vector<uint8_t> &vchSig,
133 uint32_t test_case = 0) const;
134
146 bool SignCompact(const uint256 &hash, std::vector<uint8_t> &vchSig) const;
147
149 bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild,
150 const ChainCode &cc) const;
151
157 bool VerifyPubKey(const CPubKey &vchPubKey) const;
158
160 bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey,
161 bool fSkipCheck);
162};
163
164struct CExtKey {
167 unsigned int nChild;
170
171 friend bool operator==(const CExtKey &a, const CExtKey &b) {
172 return a.nDepth == b.nDepth &&
173 memcmp(a.vchFingerprint, b.vchFingerprint,
174 sizeof(vchFingerprint)) == 0 &&
175 a.nChild == b.nChild && a.chaincode == b.chaincode &&
176 a.key == b.key;
177 }
178
179 void Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const;
180 void Decode(const uint8_t code[BIP32_EXTKEY_SIZE]);
181 bool Derive(CExtKey &out, unsigned int nChild) const;
182 CExtPubKey Neuter() const;
183 void SetSeed(const uint8_t *seed, unsigned int nSeedLen);
184
185 CExtKey() = default;
186};
187
192void ECC_Start();
193
198void ECC_Stop();
199
202
203#endif // BITCOIN_KEY_H
An encapsulated secp256k1 private key.
Definition key.h:28
static bool Check(const uint8_t *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition key.cpp:179
bool Negate()
Negate private key.
Definition key.cpp:191
static const unsigned int SIZE
secp256k1:
Definition key.h:33
friend bool operator==(const CKey &a, const CKey &b)
Definition key.h:69
bool SignCompact(const uint256 &hash, std::vector< uint8_t > &vchSig) const
Create a compact ECDSA signature (65 bytes), which allows reconstructing the used public key.
Definition key.cpp:316
unsigned int size() const
Simple read-only vector-like interface.
Definition key.h:89
bool IsValid() const
Check whether this private key is valid.
Definition key.h:94
static CKey MakeCompressedKey()
Produce a valid compressed key.
Definition key.cpp:466
bool SignECDSA(const uint256 &hash, std::vector< uint8_t > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized ECDSA signature.
Definition key.cpp:242
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition key.h:46
static CKey MakeUncompressedKey()
Produce a valid uncompressed key.
Definition key.cpp:470
const uint8_t * begin() const
Definition key.h:90
CKey()
Construct an invalid private key.
Definition key.h:60
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition key.cpp:196
static const unsigned int COMPRESSED_SIZE
Definition key.h:34
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition key.h:98
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition key.cpp:183
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition key.h:50
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition key.cpp:210
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition key.h:76
bool SignSchnorr(const uint256 &hash, SchnorrSig &sig, uint32_t test_case=0) const
Create a Schnorr signature.
Definition key.cpp:288
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition key.cpp:302
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition key.cpp:336
const uint8_t * end() const
Definition key.h:91
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition key.cpp:352
std::vector< uint8_t, secure_allocator< uint8_t > > keydata
The actual byte data.
Definition key.h:53
An encapsulated public key.
Definition pubkey.h:31
256-bit opaque blob.
Definition uint256.h:129
std::array< uint8_t, CPubKey::SCHNORR_SIZE > SchnorrSig
a Schnorr signature
Definition key.h:25
std::vector< uint8_t, secure_allocator< uint8_t > > CPrivKey
secure_allocator is defined in allocators.h CPrivKey is a serialized private key, with all parameters...
Definition key.h:22
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition key.cpp:427
void ECC_Start()
Initialize the elliptic curve support.
Definition key.cpp:434
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition key.cpp:451
SchnorrSig sig
const unsigned int BIP32_EXTKEY_SIZE
Definition pubkey.h:19
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
Definition key.h:164
CExtKey()=default
void Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const
Definition key.cpp:406
CExtPubKey Neuter() const
Definition key.cpp:396
bool Derive(CExtKey &out, unsigned int nChild) const
Definition key.cpp:374
uint8_t nDepth
Definition key.h:165
CKey key
Definition key.h:169
void Decode(const uint8_t code[BIP32_EXTKEY_SIZE])
Definition key.cpp:419
ChainCode chaincode
Definition key.h:168
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition key.h:171
void SetSeed(const uint8_t *seed, unsigned int nSeedLen)
Definition key.cpp:382
unsigned int nChild
Definition key.h:167
uint8_t vchFingerprint[4]
Definition key.h:166