Bitcoin ABC 0.30.5
P2P Digital Currency
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:
60 CKey() : fValid(false), fCompressed(false) {
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;
82 fCompressed = fCompressedIn;
83 } else {
84 fValid = false;
85 }
86 }
87
89 unsigned int size() const { return (fValid ? keydata.size() : 0); }
90 const std::byte *data() const {
91 return reinterpret_cast<const std::byte *>(keydata.data());
92 }
93 const uint8_t *begin() const { return keydata.data(); }
94 const uint8_t *end() const { return keydata.data() + size(); }
95
97 bool IsValid() const { return fValid; }
98
101 bool IsCompressed() const { return fCompressed; }
102
104 void MakeNewKey(bool fCompressed);
105
107 bool Negate();
108
114 CPrivKey GetPrivKey() const;
115
120 CPubKey GetPubKey() const;
121
126 bool SignECDSA(const uint256 &hash, std::vector<uint8_t> &vchSig,
127 bool grind = true, uint32_t test_case = 0) const;
128
133 bool SignSchnorr(const uint256 &hash, SchnorrSig &sig,
134 uint32_t test_case = 0) const;
135 bool SignSchnorr(const uint256 &hash, std::vector<uint8_t> &vchSig,
136 uint32_t test_case = 0) const;
137
149 bool SignCompact(const uint256 &hash, std::vector<uint8_t> &vchSig) const;
150
152 bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild,
153 const ChainCode &cc) const;
154
160 bool VerifyPubKey(const CPubKey &vchPubKey) const;
161
163 bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey,
164 bool fSkipCheck);
165};
166
167struct CExtKey {
168 uint8_t nDepth;
169 uint8_t vchFingerprint[4];
170 unsigned int nChild;
173
174 friend bool operator==(const CExtKey &a, const CExtKey &b) {
175 return a.nDepth == b.nDepth &&
176 memcmp(a.vchFingerprint, b.vchFingerprint,
177 sizeof(vchFingerprint)) == 0 &&
178 a.nChild == b.nChild && a.chaincode == b.chaincode &&
179 a.key == b.key;
180 }
181
182 void Encode(uint8_t code[BIP32_EXTKEY_SIZE]) const;
183 void Decode(const uint8_t code[BIP32_EXTKEY_SIZE]);
184 bool Derive(CExtKey &out, unsigned int nChild) const;
185 CExtPubKey Neuter() const;
187
188 CExtKey() = default;
189};
190
195void ECC_Start();
196
201void ECC_Stop();
202
205
206#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:97
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:93
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:101
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:94
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
const std::byte * data() const
Definition: key.h:90
An encapsulated public key.
Definition: pubkey.h:31
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
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
Definition: processor.cpp:498
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
Definition: key.h:167
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:168
CKey key
Definition: key.h:172
void Decode(const uint8_t code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:419
ChainCode chaincode
Definition: key.h:171
friend bool operator==(const CExtKey &a, const CExtKey &b)
Definition: key.h:174
unsigned int nChild
Definition: key.h:170
uint8_t vchFingerprint[4]
Definition: key.h:169
void SetSeed(Span< const std::byte > seed)
Definition: key.cpp:382