Changeset View
Changeset View
Standalone View
Standalone View
src/secp256k1/src/secp256k1.c
Show First 20 Lines • Show All 344 Lines • ▼ Show 20 Lines | |||||
const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979; | const secp256k1_nonce_function secp256k1_nonce_function_rfc6979 = nonce_function_rfc6979; | ||||
const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979; | const secp256k1_nonce_function secp256k1_nonce_function_default = nonce_function_rfc6979; | ||||
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) { | 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) { | ||||
secp256k1_scalar r, s; | secp256k1_scalar r, s; | ||||
secp256k1_scalar sec, non, msg; | secp256k1_scalar sec, non, msg; | ||||
int ret = 0; | int ret = 0; | ||||
int overflow = 0; | int overflow = 0; | ||||
const unsigned char secp256k1_ecdsa_der_algo16[17] = "ECDSA+DER "; | |||||
VERIFY_CHECK(ctx != NULL); | VERIFY_CHECK(ctx != NULL); | ||||
ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); | ARG_CHECK(secp256k1_ecmult_gen_context_is_built(&ctx->ecmult_gen_ctx)); | ||||
ARG_CHECK(msg32 != NULL); | ARG_CHECK(msg32 != NULL); | ||||
ARG_CHECK(signature != NULL); | ARG_CHECK(signature != NULL); | ||||
ARG_CHECK(seckey != NULL); | ARG_CHECK(seckey != NULL); | ||||
if (noncefp == NULL) { | if (noncefp == NULL) { | ||||
noncefp = secp256k1_nonce_function_default; | noncefp = secp256k1_nonce_function_default; | ||||
} | } | ||||
secp256k1_scalar_set_b32(&sec, seckey, &overflow); | secp256k1_scalar_set_b32(&sec, seckey, &overflow); | ||||
/* Fail if the secret key is invalid. */ | /* Fail if the secret key is invalid. */ | ||||
if (!overflow && !secp256k1_scalar_is_zero(&sec)) { | if (!overflow && !secp256k1_scalar_is_zero(&sec)) { | ||||
unsigned char nonce32[32]; | unsigned char nonce32[32]; | ||||
unsigned int count = 0; | unsigned int count = 0; | ||||
secp256k1_scalar_set_b32(&msg, msg32, NULL); | secp256k1_scalar_set_b32(&msg, msg32, NULL); | ||||
while (1) { | while (1) { | ||||
ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count); | ret = noncefp(nonce32, msg32, seckey, secp256k1_ecdsa_der_algo16, (void*)noncedata, count); | ||||
if (!ret) { | if (!ret) { | ||||
break; | break; | ||||
} | } | ||||
secp256k1_scalar_set_b32(&non, nonce32, &overflow); | secp256k1_scalar_set_b32(&non, nonce32, &overflow); | ||||
if (!overflow && !secp256k1_scalar_is_zero(&non)) { | if (!overflow && !secp256k1_scalar_is_zero(&non)) { | ||||
if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) { | if (secp256k1_ecdsa_sig_sign(&ctx->ecmult_gen_ctx, &r, &s, &sec, &msg, &non, NULL)) { | ||||
break; | break; | ||||
} | } | ||||
▲ Show 20 Lines • Show All 214 Lines • Show Last 20 Lines |