22 #define ARG_CHECK(cond) do { \
23 if (EXPECT(!(cond), 0)) { \
24 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
29 static void default_illegal_callback_fn(
const char* str,
void* data) {
31 fprintf(stderr,
"[libsecp256k1] illegal argument: %s\n", str);
36 default_illegal_callback_fn,
40 static void default_error_callback_fn(
const char* str,
void* data) {
42 fprintf(stderr,
"[libsecp256k1] internal consistency check failed: %s\n", str);
47 default_error_callback_fn,
71 secp256k1_ecmult_context_init(&ret->
ecmult_ctx);
95 secp256k1_ecmult_context_clear(&ctx->
ecmult_ctx);
104 fun = default_illegal_callback_fn;
112 fun = default_error_callback_fn;
124 secp256k1_scratch_destroy(scratch);
134 secp256k1_ge_from_storage(ge, &s);
138 secp256k1_fe_set_b32(&x, pubkey->
data);
139 secp256k1_fe_set_b32(&y, pubkey->
data + 32);
140 secp256k1_ge_set_xy(ge, &x, &y);
149 secp256k1_ge_to_storage(&s, ge);
153 secp256k1_fe_normalize_var(&ge->
x);
154 secp256k1_fe_normalize_var(&ge->
y);
155 secp256k1_fe_get_b32(pubkey->
data, &ge->
x);
156 secp256k1_fe_get_b32(pubkey->
data + 32, &ge->
y);
165 memset(pubkey, 0,
sizeof(*pubkey));
167 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
170 secp256k1_pubkey_save(pubkey, &Q);
171 secp256k1_ge_clear(&Q);
186 memset(output, 0, len);
189 if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
207 secp256k1_scalar_set_b32(r, &sig->
data[0], NULL);
208 secp256k1_scalar_set_b32(s, &sig->
data[32], NULL);
217 secp256k1_scalar_get_b32(&sig->
data[0], r);
218 secp256k1_scalar_get_b32(&sig->
data[32], s);
229 if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
230 secp256k1_ecdsa_signature_save(sig, &r, &s);
233 memset(sig, 0,
sizeof(*sig));
247 secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
249 secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
252 secp256k1_ecdsa_signature_save(sig, &r, &s);
254 memset(sig, 0,
sizeof(*sig));
267 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
268 return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
278 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
279 secp256k1_scalar_get_b32(&output64[0], &r);
280 secp256k1_scalar_get_b32(&output64[32], &s);
291 secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
292 ret = secp256k1_scalar_is_high(&s);
293 if (sigout != NULL) {
295 secp256k1_scalar_negate(&s, &s);
297 secp256k1_ecdsa_signature_save(sigout, &r, &s);
313 secp256k1_scalar_set_b32(&m, msg32, NULL);
314 secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
315 return (!secp256k1_scalar_is_high(&s) &&
316 secp256k1_pubkey_load(ctx, &q, pubkey) &&
317 secp256k1_ecdsa_sig_verify(&ctx->
ecmult_ctx, &r, &s, &q, &m));
320 static SECP256K1_INLINE void buffer_append(
unsigned char *buf,
unsigned int *offset,
const void *data,
unsigned int len) {
321 memcpy(buf + *offset, data, len);
325 static int nonce_function_rfc6979(
unsigned char *nonce32,
const unsigned char *msg32,
const unsigned char *key32,
const unsigned char *algo16,
void *data,
unsigned int counter) {
326 unsigned char keydata[112];
327 unsigned int offset = 0;
338 buffer_append(keydata, &offset, key32, 32);
339 buffer_append(keydata, &offset, msg32, 32);
341 buffer_append(keydata, &offset, data, 32);
343 if (algo16 != NULL) {
344 buffer_append(keydata, &offset, algo16, 16);
346 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
347 memset(keydata, 0,
sizeof(keydata));
348 for (i = 0; i <= counter; i++) {
349 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
351 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
368 if (noncefp == NULL) {
372 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
374 if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
375 unsigned char nonce32[32];
376 unsigned int count = 0;
377 secp256k1_scalar_set_b32(&msg, msg32, NULL);
379 ret = noncefp(nonce32, msg32, seckey, NULL, (
void*)noncedata, count);
383 secp256k1_scalar_set_b32(&non, nonce32, &overflow);
384 if (!overflow && !secp256k1_scalar_is_zero(&non)) {
385 if (secp256k1_ecdsa_sig_sign(&ctx->
ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) {
391 memset(nonce32, 0, 32);
392 secp256k1_scalar_clear(&msg);
393 secp256k1_scalar_clear(&non);
394 secp256k1_scalar_clear(&sec);
397 secp256k1_ecdsa_signature_save(signature, &r, &s);
399 memset(signature, 0,
sizeof(*signature));
411 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
412 ret = !overflow && !secp256k1_scalar_is_zero(&sec);
413 secp256k1_scalar_clear(&sec);
425 memset(pubkey, 0,
sizeof(*pubkey));
429 secp256k1_scalar_set_b32(&sec, seckey, &overflow);
430 ret = (!overflow) & (!secp256k1_scalar_is_zero(&sec));
433 secp256k1_ge_set_gej(&p, &pj);
434 secp256k1_pubkey_save(pubkey, &p);
436 secp256k1_scalar_clear(&sec);
445 secp256k1_scalar_set_b32(&sec, seckey, NULL);
446 secp256k1_scalar_negate(&sec, &sec);
447 secp256k1_scalar_get_b32(seckey, &sec);
458 ret = secp256k1_pubkey_load(ctx, &p, pubkey);
459 memset(pubkey, 0,
sizeof(*pubkey));
461 secp256k1_ge_neg(&p, &p);
462 secp256k1_pubkey_save(pubkey, &p);
476 secp256k1_scalar_set_b32(&term, tweak, &overflow);
477 secp256k1_scalar_set_b32(&sec, seckey, NULL);
479 ret = !overflow && secp256k1_eckey_privkey_tweak_add(&sec, &term);
480 memset(seckey, 0, 32);
482 secp256k1_scalar_get_b32(seckey, &sec);
485 secp256k1_scalar_clear(&sec);
486 secp256k1_scalar_clear(&term);
500 secp256k1_scalar_set_b32(&term, tweak, &overflow);
501 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
502 memset(pubkey, 0,
sizeof(*pubkey));
504 if (secp256k1_eckey_pubkey_tweak_add(&ctx->
ecmult_ctx, &p, &term)) {
505 secp256k1_pubkey_save(pubkey, &p);
523 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
524 secp256k1_scalar_set_b32(&sec, seckey, NULL);
525 ret = !overflow && secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
526 memset(seckey, 0, 32);
528 secp256k1_scalar_get_b32(seckey, &sec);
531 secp256k1_scalar_clear(&sec);
532 secp256k1_scalar_clear(&factor);
546 secp256k1_scalar_set_b32(&factor, tweak, &overflow);
547 ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
548 memset(pubkey, 0,
sizeof(*pubkey));
550 if (secp256k1_eckey_pubkey_tweak_mul(&ctx->
ecmult_ctx, &p, &factor)) {
551 secp256k1_pubkey_save(pubkey, &p);
573 memset(pubnonce, 0,
sizeof(*pubnonce));
577 secp256k1_gej_set_infinity(&Qj);
579 for (i = 0; i < n; i++) {
580 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
581 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
583 if (secp256k1_gej_is_infinity(&Qj)) {
586 secp256k1_ge_set_gej(&Q, &Qj);
587 secp256k1_pubkey_save(pubnonce, &Q);
591 #ifdef ENABLE_MODULE_ECDH
595 #ifdef ENABLE_MODULE_RECOVERY
void * memcpy(void *a, const void *b, size_t c)
int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak)
Tweak a private key by adding tweak to it.
int secp256k1_ec_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Negates a private key in place.
const secp256k1_nonce_function secp256k1_nonce_function_default
A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979).
const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function.
secp256k1_context * secp256k1_context_create(unsigned int flags)
Create a secp256k1 context object.
int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags)
Serialize a pubkey object into a serialized byte sequence.
secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t max_size)
Create a secp256k1 scratch space object.
int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig)
Serialize an ECDSA signature in DER format.
int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen)
Parse a variable-length public key into the pubkey object.
int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey)
Verify an ECDSA secret key.
secp256k1_context * secp256k1_context_clone(const secp256k1_context *ctx)
Copies a secp256k1 context object.
int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak)
Tweak a public key by multiplying it by a tweak value.
int secp256k1_ecdsa_signature_normalize(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin)
Convert a signature to a normalized lower-S form.
void secp256k1_context_set_error_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data)
Set a callback function to be called when an internal consistency check fails.
int secp256k1_ecdsa_signature_parse_der(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input, size_t inputlen)
Parse a DER ECDSA signature.
void secp256k1_scratch_space_destroy(secp256k1_scratch_space *scratch)
Destroy a secp256k1 scratch space.
int secp256k1_ec_pubkey_combine(const secp256k1_context *ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey *const *pubnonces, size_t n)
Add a number of public keys together.
int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input64)
Parse an ECDSA signature in compact (64 bytes) format.
void secp256k1_context_set_illegal_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data)
Set a callback function to be called when an illegal argument is passed to an API call.
int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32)
Updates the context randomization to protect against side-channel leakage.
int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig)
Serialize an ECDSA signature in compact (64 byte) format.
int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey)
Compute the public key for a secret key.
void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object.
int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msg32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *noncedata)
Create an ECDSA signature.
int secp256k1_ec_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak)
Tweak a public key by adding tweak times the generator to it.
int secp256k1_ec_privkey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak)
Tweak a private key by multiplying it by a tweak.
int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msg32, const secp256k1_pubkey *pubkey)
Verify an ECDSA signature.
int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey)
Negates a public key in place.
int(* secp256k1_nonce_function)(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int attempt)
A pointer to a function to deterministically generate a nonce.
#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY
The higher bits contain the actual data.
#define SECP256K1_FLAGS_TYPE_MASK
All flags' lower 8 bits indicate what they're for.
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN
#define SECP256K1_FLAGS_BIT_COMPRESSION
#define SECP256K1_FLAGS_TYPE_CONTEXT
#define SECP256K1_FLAGS_TYPE_COMPRESSION
void(* fn)(const char *text, void *data)
secp256k1_callback illegal_callback
secp256k1_callback error_callback
secp256k1_ecmult_gen_context ecmult_gen_ctx
secp256k1_ecmult_context ecmult_ctx
Opaque data structured that holds a parsed ECDSA signature.
A group element of the secp256k1 curve, in affine coordinates.
A group element of the secp256k1 curve, in jacobian coordinates.
Opaque data structure that holds a parsed and valid public key.
A scalar modulo the group order of the secp256k1 curve.
#define VERIFY_CHECK(cond)