mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-14 11:26:09 -05:00
Callback data: Accept pointers to either const or non-const data
This commit is contained in:
parent
1973c7379e
commit
05732c5a5f
6 changed files with 20 additions and 16 deletions
|
@ -94,7 +94,7 @@ typedef int (*secp256k1_nonce_function_t)(
|
||||||
const unsigned char *msg32,
|
const unsigned char *msg32,
|
||||||
const unsigned char *key32,
|
const unsigned char *key32,
|
||||||
const unsigned char *algo16,
|
const unsigned char *algo16,
|
||||||
const void *data,
|
void *data,
|
||||||
unsigned int attempt
|
unsigned int attempt
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -187,7 +187,7 @@ void secp256k1_context_destroy(
|
||||||
void secp256k1_context_set_illegal_callback(
|
void secp256k1_context_set_illegal_callback(
|
||||||
secp256k1_context_t* ctx,
|
secp256k1_context_t* ctx,
|
||||||
void (*fun)(const char* message, void* data),
|
void (*fun)(const char* message, void* data),
|
||||||
void* data
|
const void* data
|
||||||
) SECP256K1_ARG_NONNULL(1);
|
) SECP256K1_ARG_NONNULL(1);
|
||||||
|
|
||||||
/** Set a callback function to be called when an internal consistency check
|
/** Set a callback function to be called when an internal consistency check
|
||||||
|
@ -209,7 +209,7 @@ void secp256k1_context_set_illegal_callback(
|
||||||
void secp256k1_context_set_error_callback(
|
void secp256k1_context_set_error_callback(
|
||||||
secp256k1_context_t* ctx,
|
secp256k1_context_t* ctx,
|
||||||
void (*fun)(const char* message, void* data),
|
void (*fun)(const char* message, void* data),
|
||||||
void* data
|
const void* data
|
||||||
) SECP256K1_ARG_NONNULL(1);
|
) SECP256K1_ARG_NONNULL(1);
|
||||||
|
|
||||||
/** Parse a variable-length public key into the pubkey object.
|
/** Parse a variable-length public key into the pubkey object.
|
||||||
|
|
|
@ -105,7 +105,7 @@ int secp256k1_ecdsa_sign_recoverable(const secp256k1_context_t* ctx, secp256k1_e
|
||||||
secp256k1_scalar_set_b32(&msg, msg32, NULL);
|
secp256k1_scalar_set_b32(&msg, msg32, NULL);
|
||||||
while (1) {
|
while (1) {
|
||||||
unsigned char nonce32[32];
|
unsigned char nonce32[32];
|
||||||
ret = noncefp(nonce32, seckey, msg32, NULL, noncedata, count);
|
ret = noncefp(nonce32, seckey, msg32, NULL, (void*)noncedata, count);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -36,7 +36,7 @@ int secp256k1_schnorr_sign(const secp256k1_context_t* ctx, unsigned char *sig64,
|
||||||
secp256k1_scalar_set_b32(&sec, seckey, NULL);
|
secp256k1_scalar_set_b32(&sec, seckey, NULL);
|
||||||
while (1) {
|
while (1) {
|
||||||
unsigned char nonce32[32];
|
unsigned char nonce32[32];
|
||||||
ret = noncefp(nonce32, msg32, seckey, secp256k1_schnorr_algo16, noncedata, count);
|
ret = noncefp(nonce32, msg32, seckey, secp256k1_schnorr_algo16, (void*)noncedata, count);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -107,7 +107,7 @@ int secp256k1_schnorr_generate_nonce_pair(const secp256k1_context_t* ctx, secp25
|
||||||
|
|
||||||
do {
|
do {
|
||||||
int overflow;
|
int overflow;
|
||||||
ret = noncefp(privnonce32, sec32, msg32, secp256k1_schnorr_algo16, noncedata, count++);
|
ret = noncefp(privnonce32, sec32, msg32, secp256k1_schnorr_algo16, (void*)noncedata, count++);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -22,7 +22,7 @@
|
||||||
|
|
||||||
#define ARG_CHECK(cond) do { \
|
#define ARG_CHECK(cond) do { \
|
||||||
if (EXPECT(!(cond), 0)) { \
|
if (EXPECT(!(cond), 0)) { \
|
||||||
ctx->illegal_callback.fn(#cond, ctx->illegal_callback.data); \
|
secp256k1_callback(&ctx->illegal_callback, #cond); \
|
||||||
return 0; \
|
return 0; \
|
||||||
} \
|
} \
|
||||||
} while(0)
|
} while(0)
|
||||||
|
@ -94,14 +94,14 @@ void secp256k1_context_destroy(secp256k1_context_t* ctx) {
|
||||||
free(ctx);
|
free(ctx);
|
||||||
}
|
}
|
||||||
|
|
||||||
void secp256k1_context_set_illegal_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) {
|
void secp256k1_context_set_illegal_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
||||||
if (!fun)
|
if (!fun)
|
||||||
fun = default_illegal_callback_fn;
|
fun = default_illegal_callback_fn;
|
||||||
ctx->illegal_callback.fn = fun;
|
ctx->illegal_callback.fn = fun;
|
||||||
ctx->illegal_callback.data = data;
|
ctx->illegal_callback.data = data;
|
||||||
}
|
}
|
||||||
|
|
||||||
void secp256k1_context_set_error_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), void* data) {
|
void secp256k1_context_set_error_callback(secp256k1_context_t* ctx, void (*fun)(const char* message, void* data), const void* data) {
|
||||||
if (!fun)
|
if (!fun)
|
||||||
fun = default_error_callback_fn;
|
fun = default_error_callback_fn;
|
||||||
ctx->error_callback.fn = fun;
|
ctx->error_callback.fn = fun;
|
||||||
|
@ -230,7 +230,7 @@ int secp256k1_ecdsa_verify(const secp256k1_context_t* ctx, const secp256k1_ecdsa
|
||||||
secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
|
secp256k1_ecdsa_sig_verify(&ctx->ecmult_ctx, &r, &s, &q, &m));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nonce_function_rfc6979(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) {
|
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) {
|
||||||
unsigned char keydata[112];
|
unsigned char keydata[112];
|
||||||
int keylen = 64;
|
int keylen = 64;
|
||||||
secp256k1_rfc6979_hmac_sha256_t rng;
|
secp256k1_rfc6979_hmac_sha256_t rng;
|
||||||
|
@ -285,7 +285,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context_t* ctx, secp256k1_ecdsa_signatu
|
||||||
secp256k1_scalar_set_b32(&msg, msg32, NULL);
|
secp256k1_scalar_set_b32(&msg, msg32, NULL);
|
||||||
while (1) {
|
while (1) {
|
||||||
unsigned char nonce32[32];
|
unsigned char nonce32[32];
|
||||||
ret = noncefp(nonce32, msg32, seckey, NULL, noncedata, count);
|
ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1787,7 +1787,7 @@ void run_ecdsa_sign_verify(void) {
|
||||||
}
|
}
|
||||||
|
|
||||||
/** Dummy nonce generation function that just uses a precomputed nonce, and fails if it is not accepted. Use only for testing. */
|
/** Dummy nonce generation function that just uses a precomputed nonce, and fails if it is not accepted. Use only for testing. */
|
||||||
static int precomputed_nonce_function(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) {
|
static int precomputed_nonce_function(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
|
||||||
(void)msg32;
|
(void)msg32;
|
||||||
(void)key32;
|
(void)key32;
|
||||||
(void)algo16;
|
(void)algo16;
|
||||||
|
@ -1795,7 +1795,7 @@ static int precomputed_nonce_function(unsigned char *nonce32, const unsigned cha
|
||||||
return (counter == 0);
|
return (counter == 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nonce_function_test_fail(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) {
|
static int nonce_function_test_fail(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
|
||||||
/* Dummy nonce generator that has a fatal error on the first counter value. */
|
/* Dummy nonce generator that has a fatal error on the first counter value. */
|
||||||
if (counter == 0) {
|
if (counter == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -1803,7 +1803,7 @@ static int nonce_function_test_fail(unsigned char *nonce32, const unsigned char
|
||||||
return nonce_function_rfc6979(nonce32, msg32, key32, algo16, data, counter - 1);
|
return nonce_function_rfc6979(nonce32, msg32, key32, algo16, data, counter - 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, const void *data, unsigned int counter) {
|
static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
|
||||||
/* Dummy nonce generator that produces unacceptable nonces for the first several counter values. */
|
/* Dummy nonce generator that produces unacceptable nonces for the first several counter values. */
|
||||||
if (counter < 3) {
|
if (counter < 3) {
|
||||||
memset(nonce32, counter==0 ? 0 : 255, 32);
|
memset(nonce32, counter==0 ? 0 : 255, 32);
|
||||||
|
|
|
@ -17,9 +17,13 @@
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
void (*fn)(const char *text, void* data);
|
void (*fn)(const char *text, void* data);
|
||||||
void* data;
|
const void* data;
|
||||||
} callback_t;
|
} callback_t;
|
||||||
|
|
||||||
|
static SECP256K1_INLINE void secp256k1_callback(const callback_t * const cb, const char * const text) {
|
||||||
|
cb->fn(text, (void*)cb->data);
|
||||||
|
}
|
||||||
|
|
||||||
#ifdef DETERMINISTIC
|
#ifdef DETERMINISTIC
|
||||||
#define TEST_FAILURE(msg) do { \
|
#define TEST_FAILURE(msg) do { \
|
||||||
fprintf(stderr, "%s\n", msg); \
|
fprintf(stderr, "%s\n", msg); \
|
||||||
|
@ -64,7 +68,7 @@ typedef struct {
|
||||||
static SECP256K1_INLINE void *checked_malloc(const callback_t* cb, size_t size) {
|
static SECP256K1_INLINE void *checked_malloc(const callback_t* cb, size_t size) {
|
||||||
void *ret = malloc(size);
|
void *ret = malloc(size);
|
||||||
if (ret == NULL) {
|
if (ret == NULL) {
|
||||||
cb->fn("Out of memory", cb->data);
|
secp256k1_callback(cb, "Out of memory");
|
||||||
}
|
}
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue