Bitcoin Core 22.99.0
P2P Digital Currency
key.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2020 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_extrakeys.h>
15#include <secp256k1_recovery.h>
17
19
37int ec_seckey_import_der(const secp256k1_context* ctx, unsigned char *out32, const unsigned char *seckey, size_t seckeylen) {
38 const unsigned char *end = seckey + seckeylen;
39 memset(out32, 0, 32);
40 /* sequence header */
41 if (end - seckey < 1 || *seckey != 0x30u) {
42 return 0;
43 }
44 seckey++;
45 /* sequence length constructor */
46 if (end - seckey < 1 || !(*seckey & 0x80u)) {
47 return 0;
48 }
49 ptrdiff_t lenb = *seckey & ~0x80u; seckey++;
50 if (lenb < 1 || lenb > 2) {
51 return 0;
52 }
53 if (end - seckey < lenb) {
54 return 0;
55 }
56 /* sequence length */
57 ptrdiff_t len = seckey[lenb-1] | (lenb > 1 ? seckey[lenb-2] << 8 : 0u);
58 seckey += lenb;
59 if (end - seckey < len) {
60 return 0;
61 }
62 /* sequence element 0: version number (=1) */
63 if (end - seckey < 3 || seckey[0] != 0x02u || seckey[1] != 0x01u || seckey[2] != 0x01u) {
64 return 0;
65 }
66 seckey += 3;
67 /* sequence element 1: octet string, up to 32 bytes */
68 if (end - seckey < 2 || seckey[0] != 0x04u) {
69 return 0;
70 }
71 ptrdiff_t oslen = seckey[1];
72 seckey += 2;
73 if (oslen > 32 || end - seckey < oslen) {
74 return 0;
75 }
76 memcpy(out32 + (32 - oslen), seckey, oslen);
77 if (!secp256k1_ec_seckey_verify(ctx, out32)) {
78 memset(out32, 0, 32);
79 return 0;
80 }
81 return 1;
82}
83
94int ec_seckey_export_der(const secp256k1_context *ctx, unsigned char *seckey, size_t *seckeylen, const unsigned char *key32, bool compressed) {
95 assert(*seckeylen >= CKey::SIZE);
96 secp256k1_pubkey pubkey;
97 size_t pubkeylen = 0;
98 if (!secp256k1_ec_pubkey_create(ctx, &pubkey, key32)) {
99 *seckeylen = 0;
100 return 0;
101 }
102 if (compressed) {
103 static const unsigned char begin[] = {
104 0x30,0x81,0xD3,0x02,0x01,0x01,0x04,0x20
105 };
106 static const unsigned char middle[] = {
107 0xA0,0x81,0x85,0x30,0x81,0x82,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
108 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
109 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
110 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
111 0x21,0x02,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
112 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
113 0x17,0x98,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
114 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
115 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x24,0x03,0x22,0x00
116 };
117 unsigned char *ptr = seckey;
118 memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
119 memcpy(ptr, key32, 32); ptr += 32;
120 memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
121 pubkeylen = CPubKey::COMPRESSED_SIZE;
123 ptr += pubkeylen;
124 *seckeylen = ptr - seckey;
125 assert(*seckeylen == CKey::COMPRESSED_SIZE);
126 } else {
127 static const unsigned char begin[] = {
128 0x30,0x82,0x01,0x13,0x02,0x01,0x01,0x04,0x20
129 };
130 static const unsigned char middle[] = {
131 0xA0,0x81,0xA5,0x30,0x81,0xA2,0x02,0x01,0x01,0x30,0x2C,0x06,0x07,0x2A,0x86,0x48,
132 0xCE,0x3D,0x01,0x01,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
133 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
134 0xFF,0xFF,0xFE,0xFF,0xFF,0xFC,0x2F,0x30,0x06,0x04,0x01,0x00,0x04,0x01,0x07,0x04,
135 0x41,0x04,0x79,0xBE,0x66,0x7E,0xF9,0xDC,0xBB,0xAC,0x55,0xA0,0x62,0x95,0xCE,0x87,
136 0x0B,0x07,0x02,0x9B,0xFC,0xDB,0x2D,0xCE,0x28,0xD9,0x59,0xF2,0x81,0x5B,0x16,0xF8,
137 0x17,0x98,0x48,0x3A,0xDA,0x77,0x26,0xA3,0xC4,0x65,0x5D,0xA4,0xFB,0xFC,0x0E,0x11,
138 0x08,0xA8,0xFD,0x17,0xB4,0x48,0xA6,0x85,0x54,0x19,0x9C,0x47,0xD0,0x8F,0xFB,0x10,
139 0xD4,0xB8,0x02,0x21,0x00,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
140 0xFF,0xFF,0xFF,0xFF,0xFE,0xBA,0xAE,0xDC,0xE6,0xAF,0x48,0xA0,0x3B,0xBF,0xD2,0x5E,
141 0x8C,0xD0,0x36,0x41,0x41,0x02,0x01,0x01,0xA1,0x44,0x03,0x42,0x00
142 };
143 unsigned char *ptr = seckey;
144 memcpy(ptr, begin, sizeof(begin)); ptr += sizeof(begin);
145 memcpy(ptr, key32, 32); ptr += 32;
146 memcpy(ptr, middle, sizeof(middle)); ptr += sizeof(middle);
147 pubkeylen = CPubKey::SIZE;
149 ptr += pubkeylen;
150 *seckeylen = ptr - seckey;
151 assert(*seckeylen == CKey::SIZE);
152 }
153 return 1;
154}
155
156bool CKey::Check(const unsigned char *vch) {
158}
159
160void CKey::MakeNewKey(bool fCompressedIn) {
161 do {
162 GetStrongRandBytes(keydata.data(), keydata.size());
163 } while (!Check(keydata.data()));
164 fValid = true;
165 fCompressed = fCompressedIn;
166}
167
169{
170 assert(fValid);
172}
173
175 assert(fValid);
176 CPrivKey seckey;
177 int ret;
178 size_t seckeylen;
179 seckey.resize(SIZE);
180 seckeylen = SIZE;
181 ret = ec_seckey_export_der(secp256k1_context_sign, seckey.data(), &seckeylen, begin(), fCompressed);
182 assert(ret);
183 seckey.resize(seckeylen);
184 return seckey;
185}
186
188 assert(fValid);
189 secp256k1_pubkey pubkey;
190 size_t clen = CPubKey::SIZE;
191 CPubKey result;
193 assert(ret);
195 assert(result.size() == clen);
196 assert(result.IsValid());
197 return result;
198}
199
200// Check that the sig has a low R value and will be less than 71 bytes
202{
203 unsigned char compact_sig[64];
205
206 // In DER serialization, all values are interpreted as big-endian, signed integers. The highest bit in the integer indicates
207 // its signed-ness; 0 is positive, 1 is negative. When the value is interpreted as a negative integer, it must be converted
208 // to a positive value by prepending a 0x00 byte so that the highest bit is 0. We can avoid this prepending by ensuring that
209 // our highest bit is always 0, and thus we must check that the first byte is less than 0x80.
210 return compact_sig[0] < 0x80;
211}
212
213bool CKey::Sign(const uint256 &hash, std::vector<unsigned char>& vchSig, bool grind, uint32_t test_case) const {
214 if (!fValid)
215 return false;
216 vchSig.resize(CPubKey::SIGNATURE_SIZE);
217 size_t nSigLen = CPubKey::SIGNATURE_SIZE;
218 unsigned char extra_entropy[32] = {0};
219 WriteLE32(extra_entropy, test_case);
221 uint32_t counter = 0;
222 int ret = secp256k1_ecdsa_sign(secp256k1_context_sign, &sig, hash.begin(), begin(), secp256k1_nonce_function_rfc6979, (!grind && test_case) ? extra_entropy : nullptr);
223
224 // Grind for low R
225 while (ret && !SigHasLowR(&sig) && grind) {
226 WriteLE32(extra_entropy, ++counter);
228 }
229 assert(ret);
231 vchSig.resize(nSigLen);
232 return true;
233}
234
235bool CKey::VerifyPubKey(const CPubKey& pubkey) const {
236 if (pubkey.IsCompressed() != fCompressed) {
237 return false;
238 }
239 unsigned char rnd[8];
240 std::string str = "Bitcoin key verification\n";
241 GetRandBytes(rnd, sizeof(rnd));
242 uint256 hash;
243 CHash256().Write(MakeUCharSpan(str)).Write(rnd).Finalize(hash);
244 std::vector<unsigned char> vchSig;
245 Sign(hash, vchSig);
246 return pubkey.Verify(hash, vchSig);
247}
248
249bool CKey::SignCompact(const uint256 &hash, std::vector<unsigned char>& vchSig) const {
250 if (!fValid)
251 return false;
252 vchSig.resize(CPubKey::COMPACT_SIGNATURE_SIZE);
253 int rec = -1;
256 assert(ret);
258 assert(ret);
259 assert(rec != -1);
260 vchSig[0] = 27 + rec + (fCompressed ? 4 : 0);
261 return true;
262}
263
264bool CKey::SignSchnorr(const uint256& hash, Span<unsigned char> sig, const uint256* merkle_root, const uint256* aux) const
265{
266 assert(sig.size() == 64);
267 secp256k1_keypair keypair;
268 if (!secp256k1_keypair_create(secp256k1_context_sign, &keypair, begin())) return false;
269 if (merkle_root) {
271 if (!secp256k1_keypair_xonly_pub(secp256k1_context_sign, &pubkey, nullptr, &keypair)) return false;
272 unsigned char pubkey_bytes[32];
273 if (!secp256k1_xonly_pubkey_serialize(secp256k1_context_sign, pubkey_bytes, &pubkey)) return false;
274 uint256 tweak = XOnlyPubKey(pubkey_bytes).ComputeTapTweakHash(merkle_root->IsNull() ? nullptr : merkle_root);
275 if (!secp256k1_keypair_xonly_tweak_add(GetVerifyContext(), &keypair, tweak.data())) return false;
276 }
277 bool ret = secp256k1_schnorrsig_sign(secp256k1_context_sign, sig.data(), hash.data(), &keypair, aux ? (unsigned char*)aux->data() : nullptr);
278 memory_cleanse(&keypair, sizeof(keypair));
279 return ret;
280}
281
282bool CKey::Load(const CPrivKey &seckey, const CPubKey &vchPubKey, bool fSkipCheck=false) {
283 if (!ec_seckey_import_der(secp256k1_context_sign, (unsigned char*)begin(), seckey.data(), seckey.size()))
284 return false;
285 fCompressed = vchPubKey.IsCompressed();
286 fValid = true;
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, begin(), vout.data());
305 }
306 memcpy(ccChild.begin(), vout.data()+32, 32);
307 memcpy((unsigned char*)keyChild.begin(), begin(), 32);
308 bool ret = secp256k1_ec_seckey_tweak_add(secp256k1_context_sign, (unsigned char*)keyChild.begin(), vout.data());
309 keyChild.fCompressed = true;
310 keyChild.fValid = ret;
311 return ret;
312}
313
314bool CExtKey::Derive(CExtKey &out, unsigned int _nChild) const {
315 out.nDepth = nDepth + 1;
316 CKeyID id = key.GetPubKey().GetID();
317 memcpy(out.vchFingerprint, &id, 4);
318 out.nChild = _nChild;
319 return key.Derive(out.key, out.chaincode, _nChild, chaincode);
320}
321
323{
324 static const unsigned char hashkey[] = {'B','i','t','c','o','i','n',' ','s','e','e','d'};
325 std::vector<unsigned char, secure_allocator<unsigned char>> vout(64);
326 CHMAC_SHA512{hashkey, sizeof(hashkey)}.Write(seed.data(), seed.size()).Finalize(vout.data());
327 key.Set(vout.data(), vout.data() + 32, true);
328 memcpy(chaincode.begin(), vout.data() + 32, 32);
329 nDepth = 0;
330 nChild = 0;
331 memset(vchFingerprint, 0, sizeof(vchFingerprint));
332}
333
335 CExtPubKey ret;
336 ret.nDepth = nDepth;
337 memcpy(ret.vchFingerprint, vchFingerprint, 4);
338 ret.nChild = nChild;
339 ret.pubkey = key.GetPubKey();
340 ret.chaincode = chaincode;
341 return ret;
342}
343
344void CExtKey::Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const {
345 code[0] = nDepth;
346 memcpy(code+1, vchFingerprint, 4);
347 WriteBE32(code+5, nChild);
348 memcpy(code+9, chaincode.begin(), 32);
349 code[41] = 0;
350 assert(key.size() == 32);
351 memcpy(code+42, key.begin(), 32);
352}
353
354void CExtKey::Decode(const unsigned char code[BIP32_EXTKEY_SIZE]) {
355 nDepth = code[0];
356 memcpy(vchFingerprint, code+1, 4);
357 nChild = ReadBE32(code+5);
358 memcpy(chaincode.begin(), code+9, 32);
359 key.Set(code+42, code+BIP32_EXTKEY_SIZE, true);
360 if ((nDepth == 0 && (nChild != 0 || ReadLE32(vchFingerprint) != 0)) || code[41] != 0) key = CKey();
361}
362
364 CKey key;
365 key.MakeNewKey(true);
366 CPubKey pubkey = key.GetPubKey();
367 return key.VerifyPubKey(pubkey);
368}
369
370void ECC_Start() {
371 assert(secp256k1_context_sign == nullptr);
372
374 assert(ctx != nullptr);
375
376 {
377 // Pass in a random blinding seed to the secp256k1 context.
378 std::vector<unsigned char, secure_allocator<unsigned char>> vseed(32);
379 GetRandBytes(vseed.data(), 32);
380 bool ret = secp256k1_context_randomize(ctx, vseed.data());
381 assert(ret);
382 }
383
385}
386
387void ECC_Stop() {
389 secp256k1_context_sign = nullptr;
390
391 if (ctx) {
393 }
394}
A hasher class for HMAC-SHA-512.
Definition: hmac_sha512.h:15
CHMAC_SHA512 & Write(const unsigned char *data, size_t len)
Definition: hmac_sha512.h:24
A hasher class for Bitcoin's 256-bit hash (double SHA-256).
Definition: hash.h:24
void Finalize(Span< unsigned char > output)
Definition: hash.h:30
CHash256 & Write(Span< const unsigned char > input)
Definition: hash.h:37
An encapsulated private key.
Definition: key.h:27
const unsigned char * begin() const
Definition: key.h:89
bool Negate()
Negate private key.
Definition: key.cpp:168
static const unsigned int SIZE
secp256k1:
Definition: key.h:32
unsigned int size() const
Simple read-only vector-like interface.
Definition: key.h:87
bool IsValid() const
Check whether this private key is valid.
Definition: key.h:93
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:213
bool fValid
see www.keylength.com script supports up to 75 for single byte push
Definition: key.h:40
CPrivKey GetPrivKey() const
Convert the private key to a CPrivKey (serialized OpenSSL private key data).
Definition: key.cpp:174
static const unsigned int COMPRESSED_SIZE
Definition: key.h:33
bool IsCompressed() const
Check whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:96
void MakeNewKey(bool fCompressed)
Generate a new private key using a cryptographic PRNG.
Definition: key.cpp:160
bool fCompressed
Whether the public key corresponding to this private key is (to be) compressed.
Definition: key.h:48
CPubKey GetPubKey() const
Compute the public key from a private key.
Definition: key.cpp:187
void Set(const T pbegin, const T pend, bool fCompressedIn)
Initialize using begin and end iterators to byte data.
Definition: key.h:73
std::vector< unsigned char, secure_allocator< unsigned char > > keydata
The actual byte data.
Definition: key.h:51
bool VerifyPubKey(const CPubKey &vchPubKey) const
Verify thoroughly whether a private key and a public key match.
Definition: key.cpp:235
bool Load(const CPrivKey &privkey, const CPubKey &vchPubKey, bool fSkipCheck)
Load private key and check that public key matches.
Definition: key.cpp:282
static bool Check(const unsigned char *vch)
Check whether the 32-byte array pointed to by vch is valid keydata.
Definition: key.cpp:156
bool SignSchnorr(const uint256 &hash, Span< unsigned char > sig, const uint256 *merkle_root=nullptr, const uint256 *aux=nullptr) const
Create a BIP-340 Schnorr signature, for the xonly-pubkey corresponding to *this, optionally tweaked b...
Definition: key.cpp:264
bool Derive(CKey &keyChild, ChainCode &ccChild, unsigned int nChild, const ChainCode &cc) const
Derive BIP32 child key.
Definition: key.cpp:294
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:249
A reference to a CKey: the Hash160 of its serialized public key.
Definition: pubkey.h:23
An encapsulated public key.
Definition: pubkey.h:33
bool IsCompressed() const
Check whether this is a compressed public key.
Definition: pubkey.h:194
CKeyID GetID() const
Get the KeyID of this public key (hash of its serialization)
Definition: pubkey.h:160
static constexpr unsigned int COMPRESSED_SIZE
Definition: pubkey.h:39
bool IsValid() const
Definition: pubkey.h:185
bool Verify(const uint256 &hash, const std::vector< unsigned char > &vchSig) const
Verify a DER signature (~72 bytes).
Definition: pubkey.cpp:253
static constexpr unsigned int SIZE
secp256k1:
Definition: pubkey.h:38
unsigned int size() const
Simple read-only vector-like interface to the pubkey data.
Definition: pubkey.h:111
const unsigned char * begin() const
Definition: pubkey.h:113
static constexpr unsigned int SIGNATURE_SIZE
Definition: pubkey.h:40
static constexpr unsigned int COMPACT_SIGNATURE_SIZE
Definition: pubkey.h:41
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:93
constexpr std::size_t size() const noexcept
Definition: span.h:182
constexpr C * data() const noexcept
Definition: span.h:169
uint256 ComputeTapTweakHash(const uint256 *merkle_root) const
Compute the Taproot tweak as specified in BIP341, with *this as internal key:
Definition: pubkey.cpp:216
const unsigned char * data() const
Definition: uint256.h:55
unsigned char * begin()
Definition: uint256.h:58
bool IsNull() const
Definition: uint256.h:31
256-bit opaque blob.
Definition: uint256.h:124
void memory_cleanse(void *ptr, size_t len)
Secure overwrite a buffer (possibly containing secret data) with zero-bytes.
Definition: cleanse.cpp:14
static uint32_t ReadLE32(const unsigned char *ptr)
Definition: common.h:24
static void WriteBE32(unsigned char *ptr, uint32_t x)
Definition: common.h:77
static void WriteLE32(unsigned char *ptr, uint32_t x)
Definition: common.h:44
static uint32_t ReadBE32(const unsigned char *ptr)
Definition: common.h:63
void BIP32Hash(const ChainCode &chainCode, unsigned int nChild, unsigned char header, const unsigned char data[32], unsigned char output[64])
Definition: hash.cpp:75
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:94
static secp256k1_context * secp256k1_context_sign
Definition: key.cpp:18
bool SigHasLowR(const secp256k1_ecdsa_signature *sig)
Definition: key.cpp:201
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:37
bool ECC_InitSanityCheck()
Check that required EC support is available at runtime.
Definition: key.cpp:363
void ECC_Start()
Initialize the elliptic curve support.
Definition: key.cpp:370
void ECC_Stop()
Deinitialize the elliptic curve support.
Definition: key.cpp:387
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
CPrivKey is a serialized private key, with all parameters included (SIZE bytes)
Definition: key.h:23
const secp256k1_context * GetVerifyContext()
Access to the internal secp256k1 context used for verification.
Definition: pubkey.cpp:394
const unsigned int BIP32_EXTKEY_SIZE
Definition: pubkey.h:19
void GetRandBytes(unsigned char *buf, int num) noexcept
Overall design of the RNG and entropy sources.
Definition: random.cpp:584
void GetStrongRandBytes(unsigned char *buf, int num) noexcept
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using i...
Definition: random.cpp:585
#define SECP256K1_CONTEXT_SIGN
Definition: secp256k1.h:185
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32) SECP256K1_ARG_NONNULL(1)
Updates the context randomization to protect against side-channel leakage.
Definition: secp256k1.c:761
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2)
Negates a secret key in place.
Definition: secp256k1.c:622
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:302
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 ECDSA secret key.
Definition: secp256k1.c:581
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:158
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:567
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:604
#define SECP256K1_EC_COMPRESSED
Flag to pass to secp256k1_ec_pubkey_serialize.
Definition: secp256k1.h:190
#define SECP256K1_EC_UNCOMPRESSED
Definition: secp256k1.h:191
SECP256K1_API const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
Definition: secp256k1.c:508
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:412
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:668
SECP256K1_API void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object (created in dynamically allocated memory).
Definition: secp256k1.c:202
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:424
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:43
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 secret key.
Definition: main_impl.h:197
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:256
SECP256K1_API SECP256K1_WARN_UNUSED_RESULT 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:235
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 int secp256k1_schnorrsig_sign(const secp256k1_context *ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, 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
constexpr auto MakeUCharSpan(V &&v) -> decltype(UCharSpanCast(MakeSpan(std::forward< V >(v))))
Like MakeSpan, but for (const) unsigned char member types only.
Definition: span.h:249
Definition: key.h:161
unsigned char vchFingerprint[4]
Definition: key.h:163
CExtPubKey Neuter() const
Definition: key.cpp:334
bool Derive(CExtKey &out, unsigned int nChild) const
Definition: key.cpp:314
void SetSeed(Span< const uint8_t > seed)
Definition: key.cpp:322
void Decode(const unsigned char code[BIP32_EXTKEY_SIZE])
Definition: key.cpp:354
CKey key
Definition: key.h:166
void Encode(unsigned char code[BIP32_EXTKEY_SIZE]) const
Definition: key.cpp:344
unsigned char nDepth
Definition: key.h:162
ChainCode chaincode
Definition: key.h:165
unsigned int nChild
Definition: key.h:164
ChainCode chaincode
Definition: pubkey.h:291
unsigned char vchFingerprint[4]
Definition: pubkey.h:289
unsigned char nDepth
Definition: pubkey.h:288
CPubKey pubkey
Definition: pubkey.h:292
unsigned int nChild
Definition: pubkey.h:290
Opaque data structured that holds a parsed ECDSA signature, supporting pubkey recovery.
Opaque data structured that holds a parsed ECDSA signature.
Definition: secp256k1.h:83
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:70
Opaque data structure that holds a parsed and valid "x-only" public key.
static secp256k1_context * ctx
Definition: tests.c:42
assert(!tx.IsCoinBase())