Bitcoin Core 31.99.0
P2P Digital Currency
key.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-present The Bitcoin Core developers
2// Copyright (c) 2017 The Zcash developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <key.h>
7
8#include <crypto/common.h>
10#include <hash.h>
11#include <random.h>
12
13#include <secp256k1.h>
14#include <secp256k1_ellswift.h>
15#include <secp256k1_extrakeys.h>
16#include <secp256k1_recovery.h>
18
19#include <algorithm>
20
22
40int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen) {
41 const unsigned char *end = seckey + seckeylen;
42 memset(out32, 0, 32);
43 /* sequence header */
44 if (end - seckey < 1 || *seckey != 0x30u) {
45 return 0;
46 }
47 seckey++;
48 /* sequence length constructor */
49 if (end - seckey < 1 || !(*seckey & 0x80u)) {
50 return 0;
51 }
52 ptrdiff_t lenb = *seckey & ~0x80u; seckey++;
53 if (lenb < 1 || lenb > 2) {
54 return 0;
55 }
56 if (end - seckey < lenb) {
57 return 0;
58 }
59 /* sequence length */
60 ptrdiff_t len = seckey[lenb-1] | (lenb > 1 ? seckey[lenb-2] << 8 : 0u);
61 seckey += lenb;
62 if (end - seckey < len) {
63 return 0;
64 }
65 /* sequence element 0: version number (=1) */
66 if (end - seckey < 3 || seckey[0] != 0x02u || seckey[1] != 0x01u || seckey[2] != 0x01u) {
67 return 0;
68 }
69 seckey += 3;
70 /* sequence element 1: octet string, up to 32 bytes */
71 if (end - seckey < 2 || seckey[0] != 0x04u) {
72 return 0;
73 }
74 ptrdiff_t oslen = seckey[1];
75 seckey += 2;
76 if (oslen > 32 || end - seckey < oslen) {
77 return 0;
78 }
79 memcpy(out32 + (32 - oslen), seckey, oslen);
80 if (!secp256k1_ec_seckey_verify(ctx, out32)) {
81 memset(out32, 0, 32);
82 return 0;
83 }
84 return 1;
85}
86
97int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, bool compressed) {
98 assert(*seckeylen >= CKey::SIZE);
99 secp256k1_pubkey pubkey;
100 size_t pubkeylen = 0;
101 if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
102 *seckeylen = 0;
103 return 0;
104 }
105 if (compressed) {
106 static const unsigned char begin[] = {
107 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
108 };
109 static const unsigned char middle[] = {
110 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
111 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
112 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
113 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
114 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
115 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
116 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
117 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
118 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
119 };
120 unsigned char *ptr = seckey;
121 memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
122 memcpy(ptr, key32, 32); ptr += 32;
123 memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
124 pubkeylen = CPubKey::COMPRESSED_SIZE;
125 secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
126 ptr += pubkeylen;
127 *seckeylen = ptr - seckey;
128 assert(*seckeylen == CKey::COMPRESSED_SIZE);
129 } else {
130 static const unsigned char begin[] = {
131 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
132 };
133 static const unsigned char middle[] = {
134 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
135 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
136 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
137 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
138 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
139 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
140 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
141 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
142 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
143 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
144 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
145 };
146 unsigned char *ptr = seckey;
147 memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
148 memcpy(ptr, key32, 32); ptr += 32;
149 memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
150 pubkeylen = CPubKey::SIZE;
151 secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
152 ptr += pubkeylen;
153 *seckeylen = ptr - seckey;
154 assert(*seckeylen == CKey::SIZE);
155 }
156 return 1;
157}
158
159bool CKey::Check(const unsigned char *vch) {
161}
162
163void CKey::MakeNewKey(bool fCompressedIn) {
164 MakeKeyData();
165 do {
167 } while (!Check(keydata->data()));
168 fCompressed = fCompressedIn;
169}
170
173 CPrivKey seckey;
174 int ret;
175 size_t seckeylen;
176 seckey.resize(SIZE);
177 seckeylen = SIZE;
179 assert(ret);
180 seckey.resize(seckeylen);
181 return seckey;
182}
183
186 secp256k1_pubkey pubkey;
187 size_t clen = CPubKey::SIZE;
188 CPubKey result;
190 assert(ret);
192 assert(result.size() == clen);
193 assert(result.IsValid());
194 return result;
195}
196
197// Check that the sig has a low R value and will be less than 71 bytes
199{
200 unsigned char compact_sig[64];
202
203 // In DER serialization, all values are interpreted as big-endian, signed integers. The highest bit in the integer indicates
204 // its signed-ness; 0 is positive, 1 is negative. When the value is interpreted as a negative integer, it must be converted
205 // to a positive value by prepending a 0x00 byte so that the highest bit is 0. We can avoid this prepending by ensuring that
206 // our highest bit is always 0, and thus we must check that the first byte is less than 0x80.
207 return compact_sig[0] < 0x80;
208}
209
210bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool grind, uint32_t test_case) const {
211 if (!keydata)
212 return false;
213 vchSig.resize(CPubKey::SIGNATURE_SIZE);
214 size_t nSigLen = CPubKey::SIGNATURE_SIZE;
215 unsigned char extra_entropy[32] = {0};
216 WriteLE32(extra_entropy, test_case);
218 uint32_t counter = 0;
219 int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), UCharCast(begin()), secp256k1_nonce_function_rfc6979, (!grind && test_case) ? extra_entropy : nullptr);
220
221 // Grind for low R
222 while (ret && !SigHasLowR(&sig) && grind) {
223 WriteLE32(extra_entropy, ++counter);
225 }
226 assert(ret);
228 vchSig.resize(nSigLen);
229 // Additional verification step to prevent using a potentially corrupted signature
232 assert(ret);
234 assert(ret);
235 return true;
236}
237
238bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
239 if (pubkey.IsCompressed() != fCompressed) {
240 return false;
241 }
242 unsigned char rnd[8];
243 std::string str = "Bitcoin key verification\n";
244 GetRandBytes(rnd);
245 uint256 hash{Hash(str, rnd)};
246 std::vector<unsigned char> vchSig;
247 Sign(hash, vchSig);
248 return pubkey.Verify(hash, vchSig);
249}
250
251bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
252 if (!keydata)
253 return false;
254 vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE);
255 int rec = -1;
258 assert(ret);
260 assert(ret);
261 assert(rec != -1);
262 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
263 // Additional verification step to prevent using a potentially corrupted signature
264 secp256k1_pubkey epk, rpk;
266 assert(ret);
268 assert(ret);
270 assert(ret == 0);
271 return true;
272}
273
274bool CKey::SignSchnorr(const uint256& hash, std::span<unsigned char> sig, const uint256* merkle_root, const uint256& aux) const
275{
276 KeyPair kp = ComputeKeyPair(merkle_root);
277 return kp.SignSchnorr(hash, sig, aux);
278}
279
280bool CKey::Load(const CPrivKey &seckey, const CPubKey &vchPubKey, bool fSkipCheck=false) {
281 MakeKeyData();
282 if (!ec_seckey_import_der(secp256k1_context_static, (unsigned char*)begin(), seckey.data(), seckey.size())) {
283 ClearKeyData();
284 return false;
285 }
286 fCompressed = vchPubKey.IsCompressed();
287
288 if (fSkipCheck)
289 return true;
290
291 return VerifyPubKey(vchPubKey);
292}
293
294bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
295 assert(IsValid());
297 std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
298 if ((nChild >> 31) == 0) {
299 CPubKey pubkey = GetPubKey();
301 BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
302 } else {
303 assert(size() == 32);
304 BIP32Hash(cc, nChild, 0, UCharCast(begin()), vout.data());
305 }
306 memcpy(ccChild.begin(), vout.data()+32, 32);
307 keyChild.Set(begin(), begin() + 32, true);
308 bool ret = secp256k1_ec_seckey_tweak_add(secp256k1_context_static, (unsigned char*)keyChild.begin(), vout.data());
309 if (!ret) keyChild.ClearKeyData();
310 return ret;
311}
312
313EllSwiftPubKey CKey::EllSwiftCreate(std::span<const std::byte> ent32) const
314{
316 assert(ent32.size() == 32);
317 std::array<std::byte, EllSwiftPubKey::size()> encoded_pubkey;
318
320 UCharCast(encoded_pubkey.data()),
321 keydata->data(),
322 UCharCast(ent32.data()));
323
324 // Should always succeed for valid keys (asserted above).
325 assert(success);
326 return {encoded_pubkey};
327}
328
329ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, const EllSwiftPubKey& our_ellswift, bool initiating) const
330{
332
333 ECDHSecret output;
334 // BIP324 uses the initiator as party A, and the responder as party B. Remap the inputs
335 // accordingly:
337 UCharCast(output.data()),
338 UCharCast(initiating ? our_ellswift.data() : their_ellswift.data()),
339 UCharCast(initiating ? their_ellswift.data() : our_ellswift.data()),
340 keydata->data(),
341 initiating ? 0 : 1,
343 nullptr);
344 // Should always succeed for valid keys (assert above).
345 assert(success);
346 return output;
347}
348
349KeyPair CKey::ComputeKeyPair(const uint256* merkle_root) const
350{
351 return KeyPair(*this, merkle_root);
352}
353
354CKey GenerateRandomKey(bool compressed) noexcept
355{
356 CKey key;
357 key.MakeNewKey(/*fCompressed=*/compressed);
358 return key;
359}
360
361bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
362 if (nDepth == std::numeric_limits<unsigned char>::max()) return false;
363 out.nDepth = nDepth + 1;
364 out.fingerprint = id_key_fingerprint();
365 out.nChild = _nChild;
366 return key.Derive(out.key, out.chaincode, _nChild, chaincode);
367}
368
369std::optional<std::pair<CExtKey, KeyOriginInfo>> DeriveExtKey(const CExtKey& ext_key, const std::vector<uint32_t>& path)
370{
371 CExtKey descendant = ext_key;
372 KeyOriginInfo origin;
373 origin.fingerprint = ext_key.id_key_fingerprint();
374 origin.path = path;
375 for (uint32_t i : path) {
376 if (!descendant.Derive(descendant, i)) return std::nullopt;
377 }
378 return std::make_pair(descendant, origin);
379}
380
381void CExtKey::SetSeed(std::span<const std::byte> seed)
382{
383 Assert(16 <= seed.size() && seed.size() <= 64);
384 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
385 std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
386 CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(UCharCast(seed.data()), seed.size()).Finalize(vout.data());
387 key.Set(vout.data(), vout.data() + 32, true);
388 memcpy(chaincode.begin(), vout.data() + 32, 32);
389 nDepth = 0;
390 nChild = 0;
391 fingerprint.fill(0);
392}
393
396 ret.nDepth = nDepth;
397 ret.fingerprint = fingerprint;
398 ret.nChild = nChild;
399 ret.pubkey = key.GetPubKey();
400 ret.chaincode = chaincode;
401 return ret;
402}
403
404void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
405 code[0] = nDepth;
406 std::ranges::copy(fingerprint, code+1);
407 WriteBE32(code+5, nChild);
408 memcpy(code+9, chaincode.begin(), 32);
409 code[41] = 0;
410 assert(key.size() == 32);
411 memcpy(code+42, key.begin(), 32);
412}
413
414void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
415 nDepth = code[0];
416 std::copy_n(code + 1, fingerprint.size(), fingerprint.begin());
417 nChild = ReadBE32(code+5);
418 memcpy(chaincode.begin(), code+9, 32);
419 key.Set(code+42, code+BIP32_EXTKEY_SIZE, true);
420 if ((nDepth == 0 && (nChild != 0 || ReadLE32(fingerprint.data()) != 0)) || code[41] != 0) key = CKey();
421}
422
423KeyPair::KeyPair(const CKey& key, const uint256* merkle_root)
424{
425 static_assert(std::tuple_size<KeyType>() == sizeof(secp256k1_keypair));
427 auto keypair = reinterpret_cast<secp256k1_keypair*>(m_keypair->data());
428 bool success = secp256k1_keypair_create(secp256k1_context_sign, keypair, UCharCast(key.data()));
429 if (success && merkle_root) {
431 unsigned char pubkey_bytes[32];
434 uint256 tweak = XOnlyPubKey(pubkey_bytes).ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root);
436 }
437 if (!success) ClearKeyPairData();
438}
439
440bool KeyPair::SignSchnorr(const uint256& hash, std::span<unsigned char> sig, const uint256& aux) const
441{
442 assert(sig.size() == 64);
443 if (!IsValid()) return false;
444 auto keypair = reinterpret_cast<const secp256k1_keypair*>(m_keypair->data());
445 bool ret = secp256k1_schnorrsig_sign32(secp256k1_context_sign, sig.data(), hash.data(), keypair, aux.data());
446 if (ret) {
447 // Additional verification step to prevent using a potentially corrupted signature
448 secp256k1_xonly_pubkey pubkey_verify;
449 ret = secp256k1_keypair_xonly_pub(secp256k1_context_static, &pubkey_verify, nullptr, keypair);
450 ret &= secp256k1_schnorrsig_verify(secp256k1_context_static, sig.data(), hash.begin(), 32, &pubkey_verify);
451 }
452 if (!ret) memory_cleanse(sig.data(), sig.size());
453 return ret;
454}
455
457 CKey key = GenerateRandomKey();
458 CPubKey pubkey = key.GetPubKey();
459 return key.VerifyPubKey(pubkey);
460}
461
463{
465}
466
468static void ECC_Start() {
469 assert(secp256k1_context_sign == nullptr);
470
472 assert(ctx != nullptr);
473
474 {
475 // Pass in a random blinding seed to the secp256k1 context.
476 std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
477 GetRandBytes(vseed);
478 bool ret = secp256k1_context_randomize(ctx, vseed.data());
479 assert(ret);
480 }
481
483}
484
486static void ECC_Stop() {
488 secp256k1_context_sign = nullptr;
489
490 if (ctx) {
492 }
493}
494
496{
497 ECC_Start();
498}
499
501{
502 ECC_Stop();
503}
int ret
#define Assert(val)
Identity function.
Definition: check.h:116
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:14
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha512.h:23
An encapsulated private key.
Definition: key.h:40
KeyPair ComputeKeyPair(const uint256 *merkle_root) const
Compute a KeyPair.
Definition: key.cpp:349
void MakeKeyData()
Definition: key.h:68
static constexpr unsigned int COMPRESSED_SIZE
Definition: key.h:46
bool SignSchnorr(const uint256 &hash, std::span< unsigned char > sig, const uint256 *merkle_root, const uint256 &aux) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
Definition: key.cpp:274
void ClearKeyData()
Definition: key.h:73
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:122
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:128
bool Sign(const uint256 &hash, std::vector< unsigned char > &vchSig, bool grind=true, uint32_t test_case=0) const
Create a DER-serialized signature.
Definition: key.cpp:210
const std::byte * begin() const
Definition: key.h:124
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey &their_ellswift, const EllSwiftPubKey &our_ellswift, bool initiating) const
Compute a BIP324-style ECDH shared secret.
Definition: key.cpp:329
static constexpr unsigned int SIZE
secp256k1:
Definition: key.h:45
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:171
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:131
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:163
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:60
EllSwiftPubKey EllSwiftCreate(std::span< const std::byte > entropy) const
Create an ellswift-encoded public key for this key, with specified entropy.
Definition: key.cpp:313
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:184
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:108
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:238
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:280
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:159
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:294
secure_unique_ptr< KeyType > keydata
The actual byte data. nullptr for invalid keys.
Definition: key.h:63
bool SignCompact(const uint256 &hash, std::vector< unsigned char > &vchSig) const
Create a compact signature (65 bytes), which allows reconstructing the used public key.
Definition: key.cpp:251
const std::byte * data() const
Definition: key.h:123
An encapsulated public key.
Definition: pubkey.h:40
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:206
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:46
bool IsValid() const
Definition: pubkey.h:191
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:283
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:45
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:118
const unsigned char * begin() const
Definition: pubkey.h:120
static constexpr unsigned int SIGNATURE_SIZE
Definition: pubkey.h:47
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:48
A BIP32 chain code.
Definition: hash.h:23
ECC_Context()
Definition: key.cpp:495
~ECC_Context()
Definition: key.cpp:500
KeyPair.
Definition: key.h:281
KeyPair() noexcept=default
bool SignSchnorr(const uint256 &hash, std::span< unsigned char > sig, const uint256 &aux) const
Definition: key.cpp:440
void MakeKeyPairData()
Definition: key.h:313
bool IsValid() const
Check whether this keypair is valid.
Definition: key.h:305
secure_unique_ptr< KeyType > m_keypair
Definition: key.h:311
void ClearKeyPairData()
Definition: key.h:318
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
Definition: pubkey.cpp:246
constexpr bool IsNull() const
Definition: uint256.h:50
constexpr unsigned char * begin()
Definition: uint256.h:101
constexpr const unsigned char * data() const
Definition: uint256.h:98
256-bit opaque blob.
Definition: uint256.h:196
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
void WriteLE32(B *ptr, uint32_t x)
Definition: common.h:50
uint32_t ReadLE32(const B *ptr)
Definition: common.h:27
void WriteBE32(B *ptr, uint32_t x)
Definition: common.h:95
uint32_t ReadBE32(const B *ptr)
Definition: common.h:72
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:71
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:83
secp256k1_context * GetSecp256k1SignContext()
Access the secp256k1 context used for signing and MuSig2 nonce generation.
Definition: key.cpp:462
static void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:468
std::optional< std::pair< CExtKey, KeyOriginInfo > > DeriveExtKey(const CExtKey &ext_key, const std::vector< uint32_t > &path)
Get extended key and origin info for a given path.
Definition: key.cpp:369
int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, bool compressed)
This serializes to a DER encoding of the ECPrivateKey type from section C.4 of SEC 1 https://www....
Definition: key.cpp:97
static void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:486
static secp256k1_context * secp256k1_context_sign
Definition: key.cpp:21
bool SigHasLowR(const secp256k1_ecdsa_signature *sig)
Definition: key.cpp:198
int ec_seckey_import_der(const secp256k1_context *ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen)
These functions are taken from the libsecp256k1 distribution and are very ugly.
Definition: key.cpp:40
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:456
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:354
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
Definition: key.h:28
std::array< std::byte, ECDH_SECRET_SIZE > ECDHSecret
Definition: key.h:34
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition: musig.c:64
constexpr unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
void GetStrongRandBytes(std::span< unsigned char > bytes) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
Definition: random.cpp:607
void GetRandBytes(std::span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
Definition: random.cpp:601
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx) SECP256K1_ARG_NONNULL(1)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:189
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Randomizes the context to provide enhanced protection against side-channel leakage.
Definition: secp256k1.c:779
SECP256K1_API int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize a pubkey object into a serialized byte sequence.
Definition: secp256k1.c:286
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_pubkey *pubkey1, const secp256k1_pubkey *pubkey2) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compare two public keys using lexicographic (of compressed serialization) order.
Definition: secp256k1.c:312
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Verify an elliptic curve secret key.
Definition: secp256k1.c:615
SECP256K1_API secp256k1_context * secp256k1_context_create(unsigned int flags) SECP256K1_WARN_UNUSED_RESULT
Create a secp256k1 context object (in dynamically allocated memory).
Definition: secp256k1.c:143
SECP256K1_API int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create an ECDSA signature.
Definition: secp256k1.c:601
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size,...
Definition: secp256k1.h:206
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the public key for a secret key.
Definition: secp256k1.c:636
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:216
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Verify an ECDSA signature.
Definition: secp256k1.c:476
SECP256K1_API const secp256k1_context *const secp256k1_context_static
A built-in constant secp256k1 context object with static storage duration, to be used in conjunction ...
Definition: secp256k1.h:237
#define SECP256K1_EC_UNCOMPRESSED
Definition: secp256k1.h:217
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.h:682
SECP256K1_API int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in DER format.
Definition: secp256k1.c:432
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a secret key by adding tweak to it.
Definition: secp256k1.c:696
SECP256K1_API int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an ECDSA signature in compact (64 byte) format.
Definition: secp256k1.c:444
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_create(const secp256k1_context *ctx, unsigned char *ell64, const unsigned char *seckey32, const unsigned char *auxrnd32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute an ElligatorSwift public key for a secret key.
Definition: main_impl.h:431
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ellswift_xdh(const secp256k1_context *ctx, unsigned char *output, const unsigned char *ell_a64, const unsigned char *ell_b64, const unsigned char *seckey32, int party, secp256k1_ellswift_xdh_hash_function hashfp, void *data) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(7)
Given a private key, and ElligatorSwift public keys sent in both directions, compute a shared secret ...
Definition: main_impl.h:536
SECP256K1_API const secp256k1_ellswift_xdh_hash_function secp256k1_ellswift_xdh_hash_function_bip324
An implementation of an secp256k1_ellswift_xdh_hash_function compatible with BIP324.
SECP256K1_API int secp256k1_xonly_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output32, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize an xonly_pubkey object into a 32-byte sequence.
Definition: main_impl.h:51
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_create(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Compute the keypair for a valid secret key.
Definition: main_impl.h:203
SECP256K1_API int secp256k1_keypair_xonly_pub(const secp256k1_context *ctx, secp256k1_xonly_pubkey *pubkey, int *pk_parity, const secp256k1_keypair *keypair) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(4)
Get the x-only public key from a keypair.
Definition: main_impl.h:241
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_keypair_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_keypair *keypair, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Tweak a keypair by adding tweak32 to the secret key and updating the public key accordingly.
Definition: main_impl.h:262
struct secp256k1_keypair secp256k1_keypair
Opaque data structure that holds a keypair consisting of a secret and a public key.
SECP256K1_API int secp256k1_ecdsa_recoverable_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, int *recid, const secp256k1_ecdsa_recoverable_signature *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Serialize an ECDSA signature in compact format (64 bytes + recovery id).
Definition: main_impl.h:60
SECP256K1_API int secp256k1_ecdsa_sign_recoverable(const secp256k1_context *ctx, secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *ndata) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a recoverable ECDSA signature.
Definition: main_impl.h:123
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ecdsa_recover(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const secp256k1_ecdsa_recoverable_signature *sig, const unsigned char *msghash32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Recover an ECDSA public key from a signature.
Definition: main_impl.h:137
SECP256K1_API int secp256k1_schnorrsig_sign32(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, const unsigned char *aux_rand32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Create a Schnorr signature.
Definition: main_impl.h:188
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_schnorrsig_verify(const secp256k1_context *ctx, const unsigned char *sig64, const unsigned char *msg, size_t msglen, const secp256k1_xonly_pubkey *pubkey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(5)
Verify a Schnorr signature.
Definition: main_impl.h:208
unsigned char * UCharCast(char *c)
Definition: span.h:95
Definition: key.h:232
CExtPubKey Neuter() const
Definition: key.cpp:394
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:361
KeyFingerprint id_key_fingerprint() const
Definition: key.h:251
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:414
KeyFingerprint fingerprint
Definition: key.h:234
CKey key
Definition: key.h:237
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:404
unsigned char nDepth
Definition: key.h:233
ChainCode chaincode
Definition: key.h:236
unsigned int nChild
Definition: key.h:235
void SetSeed(std::span< const std::byte > seed)
Definition: key.cpp:381
An ElligatorSwift-encoded public key.
Definition: pubkey.h:315
static constexpr size_t size()
Definition: pubkey.h:332
const std::byte * data() const
Definition: pubkey.h:331
KeyFingerprint fingerprint
First 32 bits of the Hash160 of the public key at the root of the path.
Definition: keyorigin.h:14
std::vector< uint32_t > path
Definition: keyorigin.h:15
Opaque data structure that holds a parsed ECDSA signature, supporting pubkey recovery.
Opaque data structure that holds a parsed ECDSA signature.
Definition: secp256k1.h:75
Opaque data structure that holds a keypair consisting of a secret and a public key.
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:62
Opaque data structure that holds a parsed and valid "x-only" public key.
assert(!tx.IsCoinBase())