21 #define ARG_CHECK(cond) do { \    22     if (EXPECT(!(cond), 0)) { \    23         secp256k1_callback_call(&ctx->illegal_callback, #cond); \    28 static void default_illegal_callback_fn(
const char* str, 
void* data) {
    30     fprintf(stderr, 
"[libsecp256k1] illegal argument: %s\n", str);
    35     default_illegal_callback_fn,
    39 static void default_error_callback_fn(
const char* str, 
void* data) {
    41     fprintf(stderr, 
"[libsecp256k1] internal consistency check failed: %s\n", str);
    46     default_error_callback_fn,
    60     ret->illegal_callback = default_illegal_callback;
    61     ret->error_callback = default_error_callback;
    64             secp256k1_callback_call(&
ret->illegal_callback,
    70     secp256k1_ecmult_context_init(&
ret->ecmult_ctx);
    71     secp256k1_ecmult_gen_context_init(&
ret->ecmult_gen_ctx);
    74         secp256k1_ecmult_gen_context_build(&
ret->ecmult_gen_ctx, &
ret->error_callback);
    77         secp256k1_ecmult_context_build(&
ret->ecmult_ctx, &
ret->error_callback);
    94         secp256k1_ecmult_context_clear(&ctx->
ecmult_ctx);
   103         fun = default_illegal_callback_fn;
   111         fun = default_error_callback_fn;
   124         secp256k1_ge_from_storage(ge, &s);
   128         secp256k1_fe_set_b32(&x, pubkey->
data);
   129         secp256k1_fe_set_b32(&y, pubkey->
data + 32);
   130         secp256k1_ge_set_xy(ge, &x, &y);
   139         secp256k1_ge_to_storage(&s, ge);
   143         secp256k1_fe_normalize_var(&ge->
x);
   144         secp256k1_fe_normalize_var(&ge->
y);
   145         secp256k1_fe_get_b32(pubkey->
data, &ge->
x);
   146         secp256k1_fe_get_b32(pubkey->
data + 32, &ge->
y);
   155     memset(pubkey, 0, 
sizeof(*pubkey));
   157     if (!secp256k1_eckey_pubkey_parse(&Q, input, inputlen)) {
   160     secp256k1_pubkey_save(pubkey, &Q);
   161     secp256k1_ge_clear(&Q);
   176     memset(output, 0, len);
   179     if (secp256k1_pubkey_load(ctx, &Q, pubkey)) {
   197         secp256k1_scalar_set_b32(r, &sig->
data[0], NULL);
   198         secp256k1_scalar_set_b32(s, &sig->
data[32], NULL);
   207         secp256k1_scalar_get_b32(&sig->
data[0], r);
   208         secp256k1_scalar_get_b32(&sig->
data[32], s);
   219     if (secp256k1_ecdsa_sig_parse(&r, &s, input, inputlen)) {
   220         secp256k1_ecdsa_signature_save(sig, &r, &s);
   223         memset(sig, 0, 
sizeof(*sig));
   237     secp256k1_scalar_set_b32(&r, &input64[0], &overflow);
   239     secp256k1_scalar_set_b32(&s, &input64[32], &overflow);
   242         secp256k1_ecdsa_signature_save(sig, &r, &s);
   244         memset(sig, 0, 
sizeof(*sig));
   257     secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
   258     return secp256k1_ecdsa_sig_serialize(output, outputlen, &r, &s);
   268     secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
   269     secp256k1_scalar_get_b32(&output64[0], &r);
   270     secp256k1_scalar_get_b32(&output64[32], &s);
   281     secp256k1_ecdsa_signature_load(ctx, &r, &s, sigin);
   282     ret = secp256k1_scalar_is_high(&s);
   283     if (sigout != NULL) {
   285             secp256k1_scalar_negate(&s, &s);
   287         secp256k1_ecdsa_signature_save(sigout, &r, &s);
   303     secp256k1_scalar_set_b32(&m, msg32, NULL);
   304     secp256k1_ecdsa_signature_load(ctx, &r, &s, sig);
   305     return (!secp256k1_scalar_is_high(&s) &&
   306             secp256k1_pubkey_load(ctx, &q, pubkey) &&
   307             secp256k1_ecdsa_sig_verify(&ctx->
ecmult_ctx, &r, &s, &q, &m));
   310 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) {
   311    unsigned char keydata[112];
   323    memcpy(keydata, key32, 32);
   324    memcpy(keydata + 32, msg32, 32);
   326        memcpy(keydata + 64, data, 32);
   329    if (algo16 != NULL) {
   330        memcpy(keydata + keylen, algo16, 16);
   333    secp256k1_rfc6979_hmac_sha256_initialize(&rng, keydata, keylen);
   334    memset(keydata, 0, 
sizeof(keydata));
   335    for (i = 0; i <= counter; i++) {
   336        secp256k1_rfc6979_hmac_sha256_generate(&rng, nonce32, 32);
   338    secp256k1_rfc6979_hmac_sha256_finalize(&rng);
   355     if (noncefp == NULL) {
   359     secp256k1_scalar_set_b32(&sec, seckey, &overflow);
   361     if (!overflow && !secp256k1_scalar_is_zero(&sec)) {
   362         unsigned char nonce32[32];
   363         unsigned int count = 0;
   364         secp256k1_scalar_set_b32(&msg, msg32, NULL);
   366             ret = noncefp(nonce32, msg32, seckey, NULL, (
void*)noncedata, count);
   370             secp256k1_scalar_set_b32(&non, nonce32, &overflow);
   371             if (!overflow && !secp256k1_scalar_is_zero(&non)) {
   372                 if (secp256k1_ecdsa_sig_sign(&ctx->
ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) {
   378         memset(nonce32, 0, 32);
   379         secp256k1_scalar_clear(&msg);
   380         secp256k1_scalar_clear(&non);
   381         secp256k1_scalar_clear(&sec);
   384         secp256k1_ecdsa_signature_save(signature, &r, &s);
   386         memset(signature, 0, 
sizeof(*signature));
   398     secp256k1_scalar_set_b32(&sec, seckey, &overflow);
   399     ret = !overflow && !secp256k1_scalar_is_zero(&sec);
   400     secp256k1_scalar_clear(&sec);
   412     memset(pubkey, 0, 
sizeof(*pubkey));
   416     secp256k1_scalar_set_b32(&sec, seckey, &overflow);
   417     ret = (!overflow) & (!secp256k1_scalar_is_zero(&sec));
   420         secp256k1_ge_set_gej(&p, &pj);
   421         secp256k1_pubkey_save(pubkey, &p);
   423     secp256k1_scalar_clear(&sec);
   432     secp256k1_scalar_set_b32(&sec, seckey, NULL);
   433     secp256k1_scalar_negate(&sec, &sec);
   434     secp256k1_scalar_get_b32(seckey, &sec);
   445     ret = secp256k1_pubkey_load(ctx, &p, pubkey);
   446     memset(pubkey, 0, 
sizeof(*pubkey));
   448         secp256k1_ge_neg(&p, &p);
   449         secp256k1_pubkey_save(pubkey, &p);
   463     secp256k1_scalar_set_b32(&term, tweak, &overflow);
   464     secp256k1_scalar_set_b32(&sec, seckey, NULL);
   466     ret = !overflow && secp256k1_eckey_privkey_tweak_add(&sec, &term);
   467     memset(seckey, 0, 32);
   469         secp256k1_scalar_get_b32(seckey, &sec);
   472     secp256k1_scalar_clear(&sec);
   473     secp256k1_scalar_clear(&term);
   487     secp256k1_scalar_set_b32(&term, tweak, &overflow);
   488     ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
   489     memset(pubkey, 0, 
sizeof(*pubkey));
   491         if (secp256k1_eckey_pubkey_tweak_add(&ctx->
ecmult_ctx, &p, &term)) {
   492             secp256k1_pubkey_save(pubkey, &p);
   510     secp256k1_scalar_set_b32(&factor, tweak, &overflow);
   511     secp256k1_scalar_set_b32(&sec, seckey, NULL);
   512     ret = !overflow && secp256k1_eckey_privkey_tweak_mul(&sec, &factor);
   513     memset(seckey, 0, 32);
   515         secp256k1_scalar_get_b32(seckey, &sec);
   518     secp256k1_scalar_clear(&sec);
   519     secp256k1_scalar_clear(&factor);
   533     secp256k1_scalar_set_b32(&factor, tweak, &overflow);
   534     ret = !overflow && secp256k1_pubkey_load(ctx, &p, pubkey);
   535     memset(pubkey, 0, 
sizeof(*pubkey));
   537         if (secp256k1_eckey_pubkey_tweak_mul(&ctx->
ecmult_ctx, &p, &factor)) {
   538             secp256k1_pubkey_save(pubkey, &p);
   560     memset(pubnonce, 0, 
sizeof(*pubnonce));
   564     secp256k1_gej_set_infinity(&Qj);
   566     for (i = 0; i < n; i++) {
   567         secp256k1_pubkey_load(ctx, &Q, pubnonces[i]);
   568         secp256k1_gej_add_ge(&Qj, &Qj, &Q);
   570     if (secp256k1_gej_is_infinity(&Qj)) {
   573     secp256k1_ge_set_gej(&Q, &Qj);
   574     secp256k1_pubkey_save(pubnonce, &Q);
   578 #ifdef ENABLE_MODULE_ECDH   582 #ifdef ENABLE_MODULE_RECOVERY #define VERIFY_CHECK(cond)
 
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_privkey_negate(const secp256k1_context *ctx, unsigned char *seckey)
Negates a private key in place. 
 
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. 
 
UniValue ret(UniValue::VARR)
 
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. 
 
int secp256k1_ec_seckey_verify(const secp256k1_context *ctx, const unsigned char *seckey)
Verify an ECDSA secret key. 
 
void(* fn)(const char *text, void *data)
 
A group element of the secp256k1 curve, in jacobian coordinates. 
 
#define SECP256K1_FLAGS_TYPE_CONTEXT
 
#define SECP256K1_FLAGS_TYPE_MASK
All flags' lower 8 bits indicate what they're for. 
 
const secp256k1_nonce_function secp256k1_nonce_function_rfc6979
An implementation of RFC6979 (using HMAC-SHA256) as nonce generation function. 
 
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. 
 
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_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. 
 
secp256k1_ecmult_gen_context ecmult_gen_ctx
 
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_create(const secp256k1_context *ctx, secp256k1_pubkey *pubkey, const unsigned char *seckey)
Compute the public key for a secret key. 
 
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. 
 
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...
 
secp256k1_ecmult_context ecmult_ctx
 
A group element of the secp256k1 curve, in affine coordinates. 
 
Opaque data structured that holds a parsed ECDSA signature. 
 
const secp256k1_nonce_function secp256k1_nonce_function_default
A default safe nonce generation function (currently equal to secp256k1_nonce_function_rfc6979). 
 
A scalar modulo the group order of the secp256k1 curve. 
 
secp256k1_callback illegal_callback
 
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN
 
int secp256k1_ec_pubkey_negate(const secp256k1_context *ctx, secp256k1_pubkey *pubkey)
Negates a public key in place. 
 
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. 
 
#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY
The higher bits contain the actual data. 
 
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. 
 
void * memcpy(void *a, const void *b, size_t c)
 
void secp256k1_context_destroy(secp256k1_context *ctx)
Destroy a secp256k1 context object. 
 
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. 
 
secp256k1_callback error_callback
 
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_context_randomize(secp256k1_context *ctx, const unsigned char *seed32)
Updates the context randomization to protect against side-channel leakage. 
 
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. 
 
#define SECP256K1_FLAGS_TYPE_COMPRESSION
 
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. 
 
secp256k1_context * secp256k1_context_create(unsigned int flags)
Create a secp256k1 context object. 
 
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. 
 
Opaque data structure that holds a parsed and valid public key. 
 
#define SECP256K1_FLAGS_BIT_COMPRESSION
 
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.