Bitcoin Core  31.0.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>
9 #include <crypto/hmac_sha512.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_musig.h>
17 #include <secp256k1_recovery.h>
18 #include <secp256k1_schnorrsig.h>
19 
21 
39 int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen) {
40  const unsigned char *end = seckey + seckeylen;
41  memset(out32, 0, 32);
42  /* sequence header */
43  if (end - seckey < 1 || *seckey != 0x30u) {
44  return 0;
45  }
46  seckey++;
47  /* sequence length constructor */
48  if (end - seckey < 1 || !(*seckey & 0x80u)) {
49  return 0;
50  }
51  ptrdiff_t lenb = *seckey & ~0x80u; seckey++;
52  if (lenb < 1 || lenb > 2) {
53  return 0;
54  }
55  if (end - seckey < lenb) {
56  return 0;
57  }
58  /* sequence length */
59  ptrdiff_t len = seckey[lenb-1] | (lenb > 1 ? seckey[lenb-2] << 8 : 0u);
60  seckey += lenb;
61  if (end - seckey < len) {
62  return 0;
63  }
64  /* sequence element 0: version number (=1) */
65  if (end - seckey < 3 || seckey[0] != 0x02u || seckey[1] != 0x01u || seckey[2] != 0x01u) {
66  return 0;
67  }
68  seckey += 3;
69  /* sequence element 1: octet string, up to 32 bytes */
70  if (end - seckey < 2 || seckey[0] != 0x04u) {
71  return 0;
72  }
73  ptrdiff_t oslen = seckey[1];
74  seckey += 2;
75  if (oslen > 32 || end - seckey < oslen) {
76  return 0;
77  }
78  memcpy(out32 + (32 - oslen), seckey, oslen);
79  if (!secp256k1_ec_seckey_verify(ctx, out32)) {
80  memset(out32, 0, 32);
81  return 0;
82  }
83  return 1;
84 }
85 
96 int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, bool compressed) {
97  assert(*seckeylen >= CKey::SIZE);
98  secp256k1_pubkey pubkey;
99  size_t pubkeylen = 0;
100  if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
101  *seckeylen = 0;
102  return 0;
103  }
104  if (compressed) {
105  static const unsigned char begin[] = {
106  0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
107  };
108  static const unsigned char middle[] = {
109  0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
110  0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
111  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
112  0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
113  0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
114  0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
115  0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
116  0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
117  0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
118  };
119  unsigned char *ptr = seckey;
120  memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
121  memcpy(ptr, key32, 32); ptr += 32;
122  memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
123  pubkeylen = CPubKey::COMPRESSED_SIZE;
124  secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_COMPRESSED);
125  ptr += pubkeylen;
126  *seckeylen = ptr - seckey;
127  assert(*seckeylen == CKey::COMPRESSED_SIZE);
128  } else {
129  static const unsigned char begin[] = {
130  0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
131  };
132  static const unsigned char middle[] = {
133  0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
134  0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
135  0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
136  0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
137  0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
138  0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
139  0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
140  0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
141  0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
142  0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
143  0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
144  };
145  unsigned char *ptr = seckey;
146  memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
147  memcpy(ptr, key32, 32); ptr += 32;
148  memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
149  pubkeylen = CPubKey::SIZE;
150  secp256k1_ec_pubkey_serialize(ctx, ptr, &pubkeylen, &pubkey, SECP256K1_EC_UNCOMPRESSED);
151  ptr += pubkeylen;
152  *seckeylen = ptr - seckey;
153  assert(*seckeylen == CKey::SIZE);
154  }
155  return 1;
156 }
157 
158 bool CKey::Check(const unsigned char *vch) {
160 }
161 
162 void CKey::MakeNewKey(bool fCompressedIn) {
163  MakeKeyData();
164  do {
166  } while (!Check(keydata->data()));
167  fCompressed = fCompressedIn;
168 }
169 
171  assert(keydata);
172  CPrivKey seckey;
173  int ret;
174  size_t seckeylen;
175  seckey.resize(SIZE);
176  seckeylen = SIZE;
177  ret = ec_seckey_export_der(secp256k1_context_sign, seckey.data(), &seckeylen, UCharCast(begin()), fCompressed);
178  assert(ret);
179  seckey.resize(seckeylen);
180  return seckey;
181 }
182 
184  assert(keydata);
185  secp256k1_pubkey pubkey;
186  size_t clen = CPubKey::SIZE;
187  CPubKey result;
189  assert(ret);
191  assert(result.size() == clen);
192  assert(result.IsValid());
193  return result;
194 }
195 
196 // Check that the sig has a low R value and will be less than 71 bytes
198 {
199  unsigned char compact_sig[64];
201 
202  // In DER serialization, all values are interpreted as big-endian, signed integers. The highest bit in the integer indicates
203  // its signed-ness; 0 is positive, 1 is negative. When the value is interpreted as a negative integer, it must be converted
204  // to a positive value by prepending a 0x00 byte so that the highest bit is 0. We can avoid this prepending by ensuring that
205  // our highest bit is always 0, and thus we must check that the first byte is less than 0x80.
206  return compact_sig[0] < 0x80;
207 }
208 
209 bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool grind, uint32_t test_case) const {
210  if (!keydata)
211  return false;
212  vchSig.resize(CPubKey::SIGNATURE_SIZE);
213  size_t nSigLen = CPubKey::SIGNATURE_SIZE;
214  unsigned char extra_entropy[32] = {0};
215  WriteLE32(extra_entropy, test_case);
217  uint32_t counter = 0;
218  int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), UCharCast(begin()), secp256k1_nonce_function_rfc6979, (!grind && test_case) ? extra_entropy : nullptr);
219 
220  // Grind for low R
221  while (ret && !SigHasLowR(&sig) && grind) {
222  WriteLE32(extra_entropy, ++counter);
224  }
225  assert(ret);
227  vchSig.resize(nSigLen);
228  // Additional verification step to prevent using a potentially corrupted signature
231  assert(ret);
233  assert(ret);
234  return true;
235 }
236 
237 bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
238  if (pubkey.IsCompressed() != fCompressed) {
239  return false;
240  }
241  unsigned char rnd[8];
242  std::string str = "Bitcoin key verification\n";
243  GetRandBytes(rnd);
244  uint256 hash{Hash(str, rnd)};
245  std::vector<unsigned char> vchSig;
246  Sign(hash, vchSig);
247  return pubkey.Verify(hash, vchSig);
248 }
249 
250 bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
251  if (!keydata)
252  return false;
253  vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE);
254  int rec = -1;
257  assert(ret);
259  assert(ret);
260  assert(rec != -1);
261  vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
262  // Additional verification step to prevent using a potentially corrupted signature
263  secp256k1_pubkey epk, rpk;
265  assert(ret);
267  assert(ret);
269  assert(ret == 0);
270  return true;
271 }
272 
273 bool CKey::SignSchnorr(const uint256& hash, std::span<unsigned char> sig, const uint256* merkle_root, const uint256& aux) const
274 {
275  KeyPair kp = ComputeKeyPair(merkle_root);
276  return kp.SignSchnorr(hash, sig, aux);
277 }
278 
279 bool CKey::Load(const CPrivKey &seckey, const CPubKey &vchPubKey, bool fSkipCheck=false) {
280  MakeKeyData();
281  if (!ec_seckey_import_der(secp256k1_context_static, (unsigned char*)begin(), seckey.data(), seckey.size())) {
282  ClearKeyData();
283  return false;
284  }
285  fCompressed = vchPubKey.IsCompressed();
286 
287  if (fSkipCheck)
288  return true;
289 
290  return VerifyPubKey(vchPubKey);
291 }
292 
293 bool CKey::Derive(CKey& keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode& cc) const {
294  assert(IsValid());
295  assert(IsCompressed());
296  std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
297  if ((nChild >> 31) == 0) {
298  CPubKey pubkey = GetPubKey();
299  assert(pubkey.size() == CPubKey::COMPRESSED_SIZE);
300  BIP32Hash(cc, nChild, *pubkey.begin(), pubkey.begin()+1, vout.data());
301  } else {
302  assert(size() == 32);
303  BIP32Hash(cc, nChild, 0, UCharCast(begin()), vout.data());
304  }
305  memcpy(ccChild.begin(), vout.data()+32, 32);
306  keyChild.Set(begin(), begin() + 32, true);
307  bool ret = secp256k1_ec_seckey_tweak_add(secp256k1_context_static, (unsigned char*)keyChild.begin(), vout.data());
308  if (!ret) keyChild.ClearKeyData();
309  return ret;
310 }
311 
312 EllSwiftPubKey CKey::EllSwiftCreate(std::span<const std::byte> ent32) const
313 {
314  assert(keydata);
315  assert(ent32.size() == 32);
316  std::array<std::byte, EllSwiftPubKey::size()> encoded_pubkey;
317 
319  UCharCast(encoded_pubkey.data()),
320  keydata->data(),
321  UCharCast(ent32.data()));
322 
323  // Should always succeed for valid keys (asserted above).
324  assert(success);
325  return {encoded_pubkey};
326 }
327 
328 ECDHSecret CKey::ComputeBIP324ECDHSecret(const EllSwiftPubKey& their_ellswift, const EllSwiftPubKey& our_ellswift, bool initiating) const
329 {
330  assert(keydata);
331 
332  ECDHSecret output;
333  // BIP324 uses the initiator as party A, and the responder as party B. Remap the inputs
334  // accordingly:
336  UCharCast(output.data()),
337  UCharCast(initiating ? our_ellswift.data() : their_ellswift.data()),
338  UCharCast(initiating ? their_ellswift.data() : our_ellswift.data()),
339  keydata->data(),
340  initiating ? 0 : 1,
342  nullptr);
343  // Should always succeed for valid keys (assert above).
344  assert(success);
345  return output;
346 }
347 
348 KeyPair CKey::ComputeKeyPair(const uint256* merkle_root) const
349 {
350  return KeyPair(*this, merkle_root);
351 }
352 
353 std::vector<uint8_t> CKey::CreateMuSig2Nonce(MuSig2SecNonce& secnonce, const uint256& sighash, const CPubKey& aggregate_pubkey, const std::vector<CPubKey>& pubkeys)
354 {
355  // Get the keyagg cache and aggregate pubkey
356  secp256k1_musig_keyagg_cache keyagg_cache;
357  if (!MuSig2AggregatePubkeys(pubkeys, keyagg_cache, aggregate_pubkey)) return {};
358 
359  // Parse participant pubkey
360  CPubKey our_pubkey = GetPubKey();
361  secp256k1_pubkey pubkey;
362  if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &pubkey, our_pubkey.data(), our_pubkey.size())) {
363  return {};
364  }
365 
366  // Generate randomness for nonce
367  uint256 rand;
368  GetStrongRandBytes(rand);
369 
370  // Generate nonce
371  secp256k1_musig_pubnonce pubnonce;
372  if (!secp256k1_musig_nonce_gen(secp256k1_context_sign, secnonce.Get(), &pubnonce, rand.data(), UCharCast(begin()), &pubkey, sighash.data(), &keyagg_cache, nullptr)) {
373  return {};
374  }
375 
376  // Serialize pubnonce
377  std::vector<uint8_t> out;
378  out.resize(MUSIG2_PUBNONCE_SIZE);
380  return {};
381  }
382 
383  return out;
384 }
385 
386 std::optional<uint256> CKey::CreateMuSig2PartialSig(const uint256& sighash, const CPubKey& aggregate_pubkey, const std::vector<CPubKey>& pubkeys, const std::map<CPubKey, std::vector<uint8_t>>& pubnonces, MuSig2SecNonce& secnonce, const std::vector<std::pair<uint256, bool>>& tweaks)
387 {
388  secp256k1_keypair keypair;
389  if (!secp256k1_keypair_create(secp256k1_context_sign, &keypair, UCharCast(begin()))) return std::nullopt;
390 
391  // Get the keyagg cache and aggregate pubkey
392  secp256k1_musig_keyagg_cache keyagg_cache;
393  if (!MuSig2AggregatePubkeys(pubkeys, keyagg_cache, aggregate_pubkey)) return std::nullopt;
394 
395  // Check that there are enough pubnonces
396  if (pubnonces.size() != pubkeys.size()) return std::nullopt;
397 
398  // Parse the pubnonces
399  std::vector<std::pair<secp256k1_pubkey, secp256k1_musig_pubnonce>> signers_data;
400  std::vector<const secp256k1_musig_pubnonce*> pubnonce_ptrs;
401  std::optional<size_t> our_pubkey_idx;
402  CPubKey our_pubkey = GetPubKey();
403  for (const CPubKey& part_pk : pubkeys) {
404  const auto& pn_it = pubnonces.find(part_pk);
405  if (pn_it == pubnonces.end()) return std::nullopt;
406  const std::vector<uint8_t> pubnonce = pn_it->second;
407  if (pubnonce.size() != MUSIG2_PUBNONCE_SIZE) return std::nullopt;
408  if (part_pk == our_pubkey) {
409  our_pubkey_idx = signers_data.size();
410  }
411 
412  auto& [secp_pk, secp_pn] = signers_data.emplace_back();
413 
414  if (!secp256k1_ec_pubkey_parse(secp256k1_context_static, &secp_pk, part_pk.data(), part_pk.size())) {
415  return std::nullopt;
416  }
417 
418  if (!secp256k1_musig_pubnonce_parse(secp256k1_context_static, &secp_pn, pubnonce.data())) {
419  return std::nullopt;
420  }
421  }
422  if (our_pubkey_idx == std::nullopt) {
423  return std::nullopt;
424  }
425  pubnonce_ptrs.reserve(signers_data.size());
426  for (auto& [_, pn] : signers_data) {
427  pubnonce_ptrs.push_back(&pn);
428  }
429 
430  // Aggregate nonces
431  secp256k1_musig_aggnonce aggnonce;
432  if (!secp256k1_musig_nonce_agg(secp256k1_context_static, &aggnonce, pubnonce_ptrs.data(), pubnonce_ptrs.size())) {
433  return std::nullopt;
434  }
435 
436  // Apply tweaks
437  for (const auto& [tweak, xonly] : tweaks) {
438  if (xonly) {
439  if (!secp256k1_musig_pubkey_xonly_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
440  return std::nullopt;
441  }
442  } else if (!secp256k1_musig_pubkey_ec_tweak_add(secp256k1_context_static, nullptr, &keyagg_cache, tweak.data())) {
443  return std::nullopt;
444  }
445  }
446 
447  // Create musig_session
448  secp256k1_musig_session session;
449  if (!secp256k1_musig_nonce_process(secp256k1_context_static, &session, &aggnonce, sighash.data(), &keyagg_cache)) {
450  return std::nullopt;
451  }
452 
453  // Create partial signature
455  if (!secp256k1_musig_partial_sign(secp256k1_context_static, &psig, secnonce.Get(), &keypair, &keyagg_cache, &session)) {
456  return std::nullopt;
457  }
458  // The secnonce must be deleted after signing to prevent nonce reuse.
459  secnonce.Invalidate();
460 
461  // Verify partial signature
462  if (!secp256k1_musig_partial_sig_verify(secp256k1_context_static, &psig, &(signers_data.at(*our_pubkey_idx).second), &(signers_data.at(*our_pubkey_idx).first), &keyagg_cache, &session)) {
463  return std::nullopt;
464  }
465 
466  // Serialize
467  uint256 sig;
469  return std::nullopt;
470  }
471 
472  return sig;
473 }
474 
475 CKey GenerateRandomKey(bool compressed) noexcept
476 {
477  CKey key;
478  key.MakeNewKey(/*fCompressed=*/compressed);
479  return key;
480 }
481 
482 bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
483  if (nDepth == std::numeric_limits<unsigned char>::max()) return false;
484  out.nDepth = nDepth + 1;
485  CKeyID id = key.GetPubKey().GetID();
486  memcpy(out.vchFingerprint, &id, 4);
487  out.nChild = _nChild;
488  return key.Derive(out.key, out.chaincode, _nChild, chaincode);
489 }
490 
491 void CExtKey::SetSeed(std::span<const std::byte> seed)
492 {
493  static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
494  std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
495  CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(UCharCast(seed.data()), seed.size()).Finalize(vout.data());
496  key.Set(vout.data(), vout.data() + 32, true);
497  memcpy(chaincode.begin(), vout.data() + 32, 32);
498  nDepth = 0;
499  nChild = 0;
500  memset(vchFingerprint, 0, sizeof(vchFingerprint));
501 }
502 
504  CExtPubKey ret;
505  ret.nDepth = nDepth;
506  memcpy(ret.vchFingerprint, vchFingerprint, 4);
507  ret.nChild = nChild;
508  ret.pubkey = key.GetPubKey();
509  ret.chaincode = chaincode;
510  return ret;
511 }
512 
513 void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
514  code[0] = nDepth;
515  memcpy(code+1, vchFingerprint, 4);
516  WriteBE32(code+5, nChild);
517  memcpy(code+9, chaincode.begin(), 32);
518  code[41] = 0;
519  assert(key.size() == 32);
520  memcpy(code+42, key.begin(), 32);
521 }
522 
523 void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
524  nDepth = code[0];
525  memcpy(vchFingerprint, code+1, 4);
526  nChild = ReadBE32(code+5);
527  memcpy(chaincode.begin(), code+9, 32);
528  key.Set(code+42, code+BIP32_EXTKEY_SIZE, true);
529  if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || code[41] != 0) key = CKey();
530 }
531 
532 KeyPair::KeyPair(const CKey& key, const uint256* merkle_root)
533 {
534  static_assert(std::tuple_size<KeyType>() == sizeof(secp256k1_keypair));
535  MakeKeyPairData();
536  auto keypair = reinterpret_cast<secp256k1_keypair*>(m_keypair->data());
537  bool success = secp256k1_keypair_create(secp256k1_context_sign, keypair, UCharCast(key.data()));
538  if (success && merkle_root) {
539  secp256k1_xonly_pubkey pubkey;
540  unsigned char pubkey_bytes[32];
541  assert(secp256k1_keypair_xonly_pub(secp256k1_context_static, &pubkey, nullptr, keypair));
543  uint256 tweak = XOnlyPubKey(pubkey_bytes).ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root);
545  }
546  if (!success) ClearKeyPairData();
547 }
548 
549 bool KeyPair::SignSchnorr(const uint256& hash, std::span<unsigned char> sig, const uint256& aux) const
550 {
551  assert(sig.size() == 64);
552  if (!IsValid()) return false;
553  auto keypair = reinterpret_cast<const secp256k1_keypair*>(m_keypair->data());
554  bool ret = secp256k1_schnorrsig_sign32(secp256k1_context_sign, sig.data(), hash.data(), keypair, aux.data());
555  if (ret) {
556  // Additional verification step to prevent using a potentially corrupted signature
557  secp256k1_xonly_pubkey pubkey_verify;
558  ret = secp256k1_keypair_xonly_pub(secp256k1_context_static, &pubkey_verify, nullptr, keypair);
559  ret &= secp256k1_schnorrsig_verify(secp256k1_context_static, sig.data(), hash.begin(), 32, &pubkey_verify);
560  }
561  if (!ret) memory_cleanse(sig.data(), sig.size());
562  return ret;
563 }
564 
566  CKey key = GenerateRandomKey();
567  CPubKey pubkey = key.GetPubKey();
568  return key.VerifyPubKey(pubkey);
569 }
570 
572 static void ECC_Start() {
573  assert(secp256k1_context_sign == nullptr);
574 
576  assert(ctx != nullptr);
577 
578  {
579  // Pass in a random blinding seed to the secp256k1 context.
580  std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
581  GetRandBytes(vseed);
582  bool ret = secp256k1_context_randomize(ctx, vseed.data());
583  assert(ret);
584  }
585 
587 }
588 
590 static void ECC_Stop() {
592  secp256k1_context_sign = nullptr;
593 
594  if (ctx) {
596  }
597 }
598 
600 {
601  ECC_Start();
602 }
603 
605 {
606  ECC_Stop();
607 }
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:574
unsigned char data[132]
void MakeKeyPairData()
Definition: key.h:304
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha512.h:23
This module implements BIP 327 "MuSig2 for BIP340-compatible Multi-Signatures" (https://github.com/bitcoin/bips/blob/master/bip-0327.mediawiki) v1.0.0.
int ret
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:170
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:39
SECP256K1_API int secp256k1_musig_pubnonce_serialize(const secp256k1_context *ctx, unsigned char *out66, const secp256k1_musig_pubnonce *nonce) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize a signer&#39;s public nonce.
Definition: session_impl.h:208
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:426
SECP256K1_API int secp256k1_musig_partial_sign(const secp256k1_context *ctx, secp256k1_musig_partial_sig *partial_sig, secp256k1_musig_secnonce *secnonce, const secp256k1_keypair *keypair, const secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_musig_session *session) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6)
Produces a partial signature.
Definition: session_impl.h:649
void SetSeed(std::span< const std::byte > seed)
Definition: key.cpp:491
assert(!tx.IsCoinBase())
void ClearKeyPairData()
Definition: key.h:309
uint32_t ReadBE32(const B *ptr)
Definition: common.h:72
CKey key
Definition: key.h:236
Opaque data structure that holds an aggregate public nonce.
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:755
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:482
bool SignSchnorr(const uint256 &hash, std::span< unsigned char > sig, const uint256 &aux) const
Definition: key.cpp:549
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:245
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:196
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:237
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:183
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_musig_nonce_agg(const secp256k1_context *ctx, secp256k1_musig_aggnonce *aggnonce, const secp256k1_musig_pubnonce *const *pubnonces, size_t n_pubnonces) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Aggregates the nonces of all signers into a single nonce.
Definition: session_impl.h:522
Definition: key.h:231
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
Definition: pubkey.cpp:246
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:513
Opaque data structure that holds a parsed ECDSA signature, supporting pubkey recovery.
#define SECP256K1_CONTEXT_NONE
Context flags to pass to secp256k1_context_create, secp256k1_context_preallocated_size, and secp256k1_context_preallocated_create.
Definition: secp256k1.h:214
secure_unique_ptr< KeyType > keydata
The actual byte data. nullptr for invalid keys.
Definition: key.h:59
unsigned char vchFingerprint[4]
Definition: key.h:233
static void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:572
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_partial_sig_verify(const secp256k1_context *ctx, const secp256k1_musig_partial_sig *partial_sig, const secp256k1_musig_pubnonce *pubnonce, const secp256k1_pubkey *pubkey, const secp256k1_musig_keyagg_cache *keyagg_cache, const secp256k1_musig_session *session) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5) SECP256K1_ARG_NONNULL(6)
Verifies an individual signer&#39;s partial signature.
Definition: session_impl.h:719
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubnonce_parse(const secp256k1_context *ctx, secp256k1_musig_pubnonce *nonce, const unsigned char *in66) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a signer&#39;s public nonce.
Definition: session_impl.h:188
MuSig2SecNonce encapsulates a secret nonce in use in a MuSig2 signing session.
Definition: musig.h:39
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:187
constexpr size_t MUSIG2_PUBNONCE_SIZE
Definition: musig.h:17
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:255
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:268
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:39
memcpy(result.begin(), stream.data(), stream.size())
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:672
static const unsigned int SIZE
secp256k1:
Definition: key.h:41
ECDHSecret ComputeBIP324ECDHSecret(const EllSwiftPubKey &their_ellswift, const EllSwiftPubKey &our_ellswift, bool initiating) const
Compute a BIP324-style ECDH shared secret.
Definition: key.cpp:328
consteval auto _(util::TranslatedLiteral str)
Definition: translation.h:79
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
static int tweak(const secp256k1_context *ctx, secp256k1_xonly_pubkey *agg_pk, secp256k1_musig_keyagg_cache *cache)
Definition: musig.c:64
void ClearKeyData()
Definition: key.h:69
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
const unsigned char * begin() const
Definition: pubkey.h:114
Opaque data structure that holds a partial MuSig signature.
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.h:639
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_ec_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Apply plain "EC" tweaking to a public key in a given keyagg_cache by adding the generator multiplied ...
Definition: keyagg_impl.h:279
constexpr unsigned char * begin()
Definition: uint256.h:100
KeyPair.
Definition: key.h:271
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:250
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
CKey GenerateRandomKey(bool compressed) noexcept
Definition: key.cpp:475
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:612
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
Definition: key.h:24
SECP256K1_API int secp256k1_musig_partial_sig_serialize(const secp256k1_context *ctx, unsigned char *out32, const secp256k1_musig_partial_sig *sig) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Serialize a MuSig partial signature.
Definition: session_impl.h:281
uint32_t ReadLE32(const B *ptr)
Definition: common.h:27
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:209
secp256k1_musig_secnonce * Get() const
Definition: musig.cpp:107
#define SECP256K1_EC_UNCOMPRESSED
Definition: secp256k1.h:225
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:224
static void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:590
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_process(const secp256k1_context *ctx, secp256k1_musig_session *session, const secp256k1_musig_aggnonce *aggnonce, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(5)
Takes the aggregate nonce and creates a session that is required for signing and verification of part...
Definition: session_impl.h:603
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
static const unsigned int COMPRESSED_SIZE
Definition: key.h:42
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:40
void WriteLE32(B *ptr, uint32_t x)
Definition: common.h:50
void GetRandBytes(std::span< unsigned char > bytes) noexcept
Generate random data via the internal PRNG.
Definition: random.cpp:601
KeyPair() noexcept=default
KeyPair ComputeKeyPair(const uint256 *merkle_root) const
Compute a KeyPair.
Definition: key.cpp:348
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:588
Opaque data structure that holds a parsed and valid "x-only" public key.
secure_unique_ptr< KeyType > m_keypair
Definition: key.h:302
const std::byte * data() const
Definition: key.h:119
bool SigHasLowR(const secp256k1_ecdsa_signature *sig)
Definition: key.cpp:197
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:544
unsigned char nDepth
Definition: key.h:232
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:71
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:200
An encapsulated public key.
Definition: pubkey.h:33
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:162
unsigned int nChild
Definition: key.h:234
An ElligatorSwift-encoded public key.
Definition: pubkey.h:308
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:443
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:118
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:112
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3)
Parse a variable-length public key into the pubkey object.
Definition: secp256k1.c:250
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:127
Opaque data structure that holds a parsed ECDSA signature.
Definition: secp256k1.h:74
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:96
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
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:42
ChainCode chaincode
Definition: key.h:235
constexpr bool IsNull() const
Definition: uint256.h:48
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:104
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_nonce_gen(const secp256k1_context *ctx, secp256k1_musig_secnonce *secnonce, secp256k1_musig_pubnonce *pubnonce, unsigned char *session_secrand32, const unsigned char *seckey, const secp256k1_pubkey *pubkey, const unsigned char *msg32, const secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *extra_input32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4) SECP256K1_ARG_NONNULL(6)
Starts a signing session by generating a nonce.
Definition: session_impl.h:447
bool IsValid() const
Check whether this keypair is valid.
Definition: key.h:296
Opaque data structure that holds a keypair consisting of a secret and a public key.
std::optional< CPubKey > MuSig2AggregatePubkeys(const std::vector< CPubKey > &pubkeys, secp256k1_musig_keyagg_cache &keyagg_cache, const std::optional< CPubKey > &expected_aggregate)
Compute the full aggregate pubkey from the given participant pubkeys in their current order...
Definition: musig.cpp:54
static secp256k1_context * secp256k1_context_sign
Definition: key.cpp:20
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 Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:523
Opaque data structure that holds a signer&#39;s public nonce.
256-bit opaque blob.
Definition: uint256.h:195
std::array< std::byte, ECDH_SECRET_SIZE > ECDHSecret
Definition: key.h:30
void WriteBE32(B *ptr, uint32_t x)
Definition: common.h:95
Opaque data structure that holds a MuSig session.
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:414
EllSwiftPubKey EllSwiftCreate(std::span< const std::byte > entropy) const
Create an ellswift-encoded public key for this key, with specified entropy.
Definition: key.cpp:312
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_musig_pubkey_xonly_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *output_pubkey, secp256k1_musig_keyagg_cache *keyagg_cache, const unsigned char *tweak32) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4)
Apply x-only tweaking to a public key in a given keyagg_cache by adding the generator multiplied with...
Definition: keyagg_impl.h:283
void Invalidate()
Definition: musig.cpp:112
auto result
Definition: common-types.h:74
constexpr const unsigned char * data() const
Definition: uint256.h:97
CExtPubKey Neuter() const
Definition: key.cpp:503
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:283
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:293
~ECC_Context()
Definition: key.cpp:604
const std::byte * begin() const
Definition: key.h:120
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
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:44
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:56
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:273
const std::byte * data() const
Definition: pubkey.h:325
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:234
unsigned char * UCharCast(char *c)
Definition: span.h:95
const unsigned char * data() const
Definition: pubkey.h:113
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:158
An encapsulated private key.
Definition: key.h:35
uint256 Hash(const T &in1)
Compute the 256-bit hash of an object.
Definition: hash.h:75
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:565
static constexpr unsigned int SIGNATURE_SIZE
Definition: pubkey.h:41
std::optional< uint256 > CreateMuSig2PartialSig(const uint256 &hash, const CPubKey &aggregate_pubkey, const std::vector< CPubKey > &pubkeys, const std::map< CPubKey, std::vector< uint8_t >> &pubnonces, MuSig2SecNonce &secnonce, const std::vector< std::pair< uint256, bool >> &tweaks)
Definition: key.cpp:386
std::vector< uint8_t > CreateMuSig2Nonce(MuSig2SecNonce &secnonce, const uint256 &sighash, const CPubKey &aggregate_pubkey, const std::vector< CPubKey > &pubkeys)
Definition: key.cpp:353
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:141
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:224
void MakeKeyData()
Definition: key.h:64
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:458
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:294
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:279
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:124
ECC_Context()
Definition: key.cpp:599
Opaque data structure that holds a parsed and valid public key.
Definition: secp256k1.h:61
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:200
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:13