26#ifdef SECP256K1_NO_BUILD
27# error "secp256k1.h processed without SECP256K1_BUILD defined while building secp256k1.c"
31# include <valgrind/memcheck.h>
34#define ARG_CHECK(cond) do { \
35 if (EXPECT(!(cond), 0)) { \
36 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
41#define ARG_CHECK_NO_RETURN(cond) do { \
42 if (EXPECT(!(cond), 0)) { \
43 secp256k1_callback_call(&ctx->illegal_callback, #cond); \
56 { secp256k1_default_illegal_callback_fn, 0 },
57 { secp256k1_default_error_callback_fn, 0 },
68 secp256k1_callback_call(&default_illegal_callback,
86 if (!secp256k1_selftest()) {
87 secp256k1_callback_call(&default_error_callback,
"self test failed");
91 if (prealloc_size == 0) {
96 ret->illegal_callback = default_illegal_callback;
97 ret->error_callback = default_error_callback;
101 secp256k1_ecmult_gen_context_build(&
ret->ecmult_gen_ctx);
130 size_t prealloc_size;
156 fun = secp256k1_default_illegal_callback_fn;
165 fun = secp256k1_default_error_callback_fn;
202 secp256k1_ge_from_storage(ge, &
s);
206 secp256k1_fe_set_b32(&x,
pubkey->data);
207 secp256k1_fe_set_b32(&y,
pubkey->data + 32);
208 secp256k1_ge_set_xy(ge, &x, &y);
217 secp256k1_ge_to_storage(&
s, ge);
221 secp256k1_fe_normalize_var(&ge->
x);
222 secp256k1_fe_normalize_var(&ge->
y);
223 secp256k1_fe_get_b32(
pubkey->data, &ge->
x);
224 secp256k1_fe_get_b32(
pubkey->data + 32, &ge->
y);
235 if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
238 if (!secp256k1_ge_is_in_correct_subgroup(&Q)) {
241 secp256k1_pubkey_save(
pubkey, &Q);
242 secp256k1_ge_clear(&Q);
260 if (secp256k1_pubkey_load(ctx, &Q,
pubkey)) {
270 unsigned char out[2][33];
275 pk[0] = pubkey0; pk[1] = pubkey1;
276 for (i = 0; i < 2; i++) {
277 size_t out_size =
sizeof(out[i]);
289 memset(out[i], 0,
sizeof(out[i]));
292 return secp256k1_memcmp_var(out[0], out[1],
sizeof(out[0]));
304 secp256k1_scalar_set_b32(
r, &sig->
data[0], NULL);
305 secp256k1_scalar_set_b32(
s, &sig->
data[32], NULL);
314 secp256k1_scalar_get_b32(&sig->
data[0],
r);
315 secp256k1_scalar_get_b32(&sig->
data[32],
s);
326 if (secp256k1_ecdsa_sig_parse(&
r, &
s, input, inputlen)) {
327 secp256k1_ecdsa_signature_save(sig, &
r, &
s);
330 memset(sig, 0,
sizeof(*sig));
344 secp256k1_scalar_set_b32(&
r, &input64[0], &
overflow);
346 secp256k1_scalar_set_b32(&
s, &input64[32], &
overflow);
349 secp256k1_ecdsa_signature_save(sig, &
r, &
s);
351 memset(sig, 0,
sizeof(*sig));
364 secp256k1_ecdsa_signature_load(ctx, &
r, &
s, sig);
365 return secp256k1_ecdsa_sig_serialize(output, outputlen, &
r, &
s);
375 secp256k1_ecdsa_signature_load(ctx, &
r, &
s, sig);
376 secp256k1_scalar_get_b32(&output64[0], &
r);
377 secp256k1_scalar_get_b32(&output64[32], &
s);
388 secp256k1_ecdsa_signature_load(ctx, &
r, &
s, sigin);
389 ret = secp256k1_scalar_is_high(&
s);
390 if (sigout != NULL) {
392 secp256k1_scalar_negate(&
s, &
s);
394 secp256k1_ecdsa_signature_save(sigout, &
r, &
s);
409 secp256k1_scalar_set_b32(&m, msghash32, NULL);
410 secp256k1_ecdsa_signature_load(ctx, &
r, &
s, sig);
411 return (!secp256k1_scalar_is_high(&
s) &&
412 secp256k1_pubkey_load(ctx, &q,
pubkey) &&
413 secp256k1_ecdsa_sig_verify(&
r, &
s, &q, &m));
416static SECP256K1_INLINE void buffer_append(
unsigned char *
buf,
unsigned int *offset,
const void *data,
unsigned int len) {
421static int nonce_function_rfc6979(
unsigned char *nonce32,
const unsigned char *msg32,
const unsigned char *key32,
const unsigned char *algo16,
void *data,
unsigned int counter) {
422 unsigned char keydata[112];
423 unsigned int offset = 0;
427 unsigned char msgmod32[32];
428 secp256k1_scalar_set_b32(&msg, msg32, NULL);
429 secp256k1_scalar_get_b32(msgmod32, &msg);
438 buffer_append(keydata, &offset, key32, 32);
439 buffer_append(keydata, &offset, msgmod32, 32);
441 buffer_append(keydata, &offset, data, 32);
443 if (algo16 != NULL) {
444 buffer_append(keydata, &offset, algo16, 16);
446 secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, offset);
447 memset(keydata, 0,
sizeof(keydata));
448 for (i = 0; i <= counter; i++) {
449 secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
451 secp256k1_rfc6979_hmac_sha256_finalize(&rng);
462 unsigned char nonce32[32];
463 unsigned int count = 0;
465 *
r = secp256k1_scalar_zero;
466 *
s = secp256k1_scalar_zero;
470 if (noncefp == NULL) {
475 is_sec_valid = secp256k1_scalar_set_b32_seckey(&sec, seckey);
476 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_one, !is_sec_valid);
477 secp256k1_scalar_set_b32(&msg, msg32, NULL);
480 ret = !!noncefp(nonce32, msg32, seckey, NULL, (
void*)noncedata,
count);
484 is_nonce_valid = secp256k1_scalar_set_b32_seckey(&non, nonce32);
486 secp256k1_declassify(ctx, &is_nonce_valid,
sizeof(is_nonce_valid));
487 if (is_nonce_valid) {
490 secp256k1_declassify(ctx, &
ret,
sizeof(
ret));
502 secp256k1_scalar_clear(&msg);
503 secp256k1_scalar_clear(&non);
504 secp256k1_scalar_clear(&sec);
505 secp256k1_scalar_cmov(
r, &secp256k1_scalar_zero, !
ret);
506 secp256k1_scalar_cmov(
s, &secp256k1_scalar_zero, !
ret);
509 secp256k1_int_cmov(recid, &zero, !
ret);
523 ret = secp256k1_ecdsa_sign_inner(ctx, &
r, &
s, NULL, msghash32, seckey, noncefp, noncedata);
524 secp256k1_ecdsa_signature_save(signature, &
r, &
s);
534 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
535 secp256k1_scalar_clear(&sec);
543 ret = secp256k1_scalar_set_b32_seckey(seckey_scalar, seckey);
544 secp256k1_scalar_cmov(seckey_scalar, &secp256k1_scalar_one, !
ret);
546 secp256k1_ecmult_gen(ecmult_gen_ctx, &pj, seckey_scalar);
547 secp256k1_ge_set_gej(
p, &pj);
561 ret = secp256k1_ec_pubkey_create_helper(&ctx->
ecmult_gen_ctx, &seckey_scalar, &
p, seckey);
562 secp256k1_pubkey_save(
pubkey, &
p);
565 secp256k1_scalar_clear(&seckey_scalar);
575 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
576 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !
ret);
577 secp256k1_scalar_negate(&sec, &sec);
578 secp256k1_scalar_get_b32(seckey, &sec);
580 secp256k1_scalar_clear(&sec);
597 secp256k1_ge_neg(&
p, &
p);
598 secp256k1_pubkey_save(
pubkey, &
p);
604static int secp256k1_ec_seckey_tweak_add_helper(
secp256k1_scalar *sec,
const unsigned char *tweak32) {
609 secp256k1_scalar_set_b32(&term, tweak32, &
overflow);
610 ret = (!
overflow) & secp256k1_eckey_privkey_tweak_add(sec, &term);
611 secp256k1_scalar_clear(&term);
622 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
623 ret &= secp256k1_ec_seckey_tweak_add_helper(&sec, tweak32);
624 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !
ret);
625 secp256k1_scalar_get_b32(seckey, &sec);
627 secp256k1_scalar_clear(&sec);
635static int secp256k1_ec_pubkey_tweak_add_helper(
secp256k1_ge *
p,
const unsigned char *tweak32) {
638 secp256k1_scalar_set_b32(&term, tweak32, &
overflow);
639 return !
overflow && secp256k1_eckey_pubkey_tweak_add(
p, &term);
651 ret =
ret && secp256k1_ec_pubkey_tweak_add_helper(&
p, tweak32);
653 secp256k1_pubkey_save(
pubkey, &
p);
668 secp256k1_scalar_set_b32(&factor, tweak32, &
overflow);
669 ret = secp256k1_scalar_set_b32_seckey(&sec, seckey);
670 ret &= (!
overflow) & secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
671 secp256k1_scalar_cmov(&sec, &secp256k1_scalar_zero, !
ret);
672 secp256k1_scalar_get_b32(seckey, &sec);
674 secp256k1_scalar_clear(&sec);
675 secp256k1_scalar_clear(&factor);
692 secp256k1_scalar_set_b32(&factor, tweak32, &
overflow);
696 if (secp256k1_eckey_pubkey_tweak_mul(&
p, &factor)) {
697 secp256k1_pubkey_save(
pubkey, &
p);
708 if (secp256k1_ecmult_gen_context_is_built(&ctx->
ecmult_gen_ctx)) {
721 memset(pubnonce, 0,
sizeof(*pubnonce));
725 secp256k1_gej_set_infinity(&Qj);
727 for (i = 0; i < n; i++) {
729 secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
730 secp256k1_gej_add_ge(&Qj, &Qj, &Q);
732 if (secp256k1_gej_is_infinity(&Qj)) {
735 secp256k1_ge_set_gej(&Q, &Qj);
736 secp256k1_pubkey_save(pubnonce, &Q);
747 secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
748 secp256k1_sha256_write(&sha, msg, msglen);
749 secp256k1_sha256_finalize(&sha, hash32);
753#ifdef ENABLE_MODULE_ECDH
757#ifdef ENABLE_MODULE_RECOVERY
761#ifdef ENABLE_MODULE_EXTRAKEYS
765#ifdef ENABLE_MODULE_SCHNORRSIG
#define VERIFY_CHECK(cond)
int secp256k1_ec_privkey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
int secp256k1_ec_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey)
const secp256k1_nonce_function secp256k1_nonce_function_default
secp256k1_context * secp256k1_context_preallocated_clone(const secp256k1_context *ctx, void *prealloc)
const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
int secp256k1_tagged_sha256(const secp256k1_context *ctx, unsigned char *hash32, const unsigned char *tag, size_t taglen, const unsigned char *msg, size_t msglen)
int secp256k1_ec_pubkey_tweak_add(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32)
int secp256k1_ec_pubkey_serialize(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_pubkey *pubkey, unsigned int flags)
int secp256k1_ecdsa_signature_serialize_der(const secp256k1_context *ctx, unsigned char *output, size_t *outputlen, const secp256k1_ecdsa_signature *sig)
int secp256k1_ec_seckey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
int secp256k1_ec_pubkey_parse(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *input, size_t inputlen)
size_t secp256k1_context_preallocated_clone_size(const secp256k1_context *ctx)
int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey)
const secp256k1_context * secp256k1_context_no_precomp
int secp256k1_ec_seckey_tweak_add(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
void secp256k1_context_preallocated_destroy(secp256k1_context *ctx)
int secp256k1_ecdsa_signature_normalize(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sigout, const secp256k1_ecdsa_signature *sigin)
void secp256k1_context_set_error_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data)
int secp256k1_ecdsa_signature_parse_der(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input, size_t inputlen)
secp256k1_context * secp256k1_context_create(unsigned int flags)
int secp256k1_ec_seckey_negate(const secp256k1_context *ctx, unsigned char *seckey)
int secp256k1_ec_pubkey_cmp(const secp256k1_context *ctx, const secp256k1_pubkey *pubkey0, const secp256k1_pubkey *pubkey1)
int secp256k1_ec_pubkey_combine(const secp256k1_context *ctx, secp256k1_pubkey *pubnonce, const secp256k1_pubkey *const *pubnonces, size_t n)
int secp256k1_ecdsa_signature_parse_compact(const secp256k1_context *ctx, secp256k1_ecdsa_signature *sig, const unsigned char *input64)
void secp256k1_context_set_illegal_callback(secp256k1_context *ctx, void(*fun)(const char *message, void *data), const void *data)
size_t secp256k1_context_preallocated_size(unsigned int flags)
#define ARG_CHECK_NO_RETURN(cond)
int secp256k1_context_randomize(secp256k1_context *ctx, const unsigned char *seed32)
secp256k1_scratch_space * secp256k1_scratch_space_create(const secp256k1_context *ctx, size_t max_size)
int secp256k1_ecdsa_verify(const secp256k1_context *ctx, const secp256k1_ecdsa_signature *sig, const unsigned char *msghash32, const secp256k1_pubkey *pubkey)
int secp256k1_ecdsa_signature_serialize_compact(const secp256k1_context *ctx, unsigned char *output64, const secp256k1_ecdsa_signature *sig)
int secp256k1_ec_pubkey_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey)
void secp256k1_context_destroy(secp256k1_context *ctx)
secp256k1_context * secp256k1_context_clone(const secp256k1_context *ctx)
void secp256k1_scratch_space_destroy(const secp256k1_context *ctx, secp256k1_scratch_space *scratch)
int secp256k1_ec_pubkey_tweak_mul(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *tweak32)
secp256k1_context * secp256k1_context_preallocated_create(void *prealloc, unsigned int flags)
int secp256k1_ecdsa_sign(const secp256k1_context *ctx, secp256k1_ecdsa_signature *signature, const unsigned char *msghash32, const unsigned char *seckey, secp256k1_nonce_function noncefp, const void *noncedata)
int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey)
int secp256k1_ec_privkey_tweak_mul(const secp256k1_context *ctx, unsigned char *seckey, const unsigned char *tweak32)
struct secp256k1_context_struct secp256k1_context
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY
int(* secp256k1_nonce_function)(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int attempt)
#define SECP256K1_EC_COMPRESSED
#define SECP256K1_FLAGS_TYPE_MASK
#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
memset(pInfo->slotDescription, ' ', 64)
memcpy((char *) pInfo->slotDescription, s, l)