Skip to content

Commit 364123a

Browse files
committed
Add secp256k1_context argument to secp256k1_nonce_functions
1 parent 6e0acf8 commit 364123a

File tree

6 files changed

+23
-16
lines changed

6 files changed

+23
-16
lines changed

include/secp256k1.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ typedef struct {
8383
/** A pointer to a function to deterministically generate a nonce.
8484
*
8585
* Returns: 1 if a nonce was successfully generated. 0 will cause signing to fail.
86+
* Args: ctx: an existing context object
8687
* Out: nonce32: pointer to a 32-byte array to be filled by the function.
8788
* In: msg32: the 32-byte message hash being verified (will not be NULL)
8889
* key32: pointer to a 32-byte secret key (will not be NULL)
@@ -97,6 +98,7 @@ typedef struct {
9798
* the message, the algorithm, the key and the attempt.
9899
*/
99100
typedef int (*secp256k1_nonce_function)(
101+
const secp256k1_context *ctx,
100102
unsigned char *nonce32,
101103
const unsigned char *msg32,
102104
const unsigned char *key32,

src/modules/schnorrsig/main_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ int secp256k1_schnorrsig_sign(const secp256k1_context* ctx, secp256k1_schnorrsig
6161
secp256k1_ecmult_gen(&ctx->ecmult_gen_ctx, &pkj, &x);
6262
secp256k1_ge_set_gej(&pk, &pkj);
6363

64-
if (!noncefp(buf, msg32, seckey, NULL, (void*)ndata, 0)) {
64+
if (!noncefp(ctx, buf, msg32, seckey, NULL, (void*)ndata, 0)) {
6565
return 0;
6666
}
6767
secp256k1_scalar_set_b32(&k, buf, NULL);

src/modules/schnorrsig/tests_impl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -554,7 +554,8 @@ void test_schnorrsig_bip_vectors(secp256k1_scratch_space *scratch) {
554554
}
555555

556556
/* Nonce function that returns constant 0 */
557-
static int nonce_function_failing(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
557+
static int nonce_function_failing(const secp256k1_context *context, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
558+
(void) context;
558559
(void) msg32;
559560
(void) key32;
560561
(void) algo16;
@@ -565,7 +566,8 @@ static int nonce_function_failing(unsigned char *nonce32, const unsigned char *m
565566
}
566567

567568
/* Nonce function that sets nonce to 0 */
568-
static int nonce_function_0(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
569+
static int nonce_function_0(const secp256k1_context *context, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
570+
(void) context;
569571
(void) msg32;
570572
(void) key32;
571573
(void) algo16;

src/secp256k1.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ static SECP256K1_INLINE void buffer_append(unsigned char *buf, unsigned int *off
324324

325325
/* This nonce function is described in BIP-schnorr
326326
* (https://github.com/sipa/bips/blob/bip-schnorr/bip-schnorr.mediawiki) */
327-
static int secp256k1_nonce_function_bipschnorr(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
327+
static int secp256k1_nonce_function_bipschnorr(const secp256k1_context *ctx, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
328328
secp256k1_sha256 sha;
329329
(void) data;
330330
(void) counter;
@@ -343,11 +343,12 @@ static int secp256k1_nonce_function_bipschnorr(unsigned char *nonce32, const uns
343343
return 1;
344344
}
345345

346-
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) {
346+
static int nonce_function_rfc6979(const secp256k1_context *ctx, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
347347
unsigned char keydata[112];
348348
unsigned int offset = 0;
349349
secp256k1_rfc6979_hmac_sha256 rng;
350350
unsigned int i;
351+
(void)ctx;
351352
/* We feed a byte array to the PRNG as input, consisting of:
352353
* - the private key (32 bytes) and message (32 bytes), see RFC 6979 3.2d.
353354
* - optionally 32 extra bytes of data, see RFC 6979 3.6 Additional Data.
@@ -397,7 +398,7 @@ int secp256k1_ecdsa_sign(const secp256k1_context* ctx, secp256k1_ecdsa_signature
397398
unsigned int count = 0;
398399
secp256k1_scalar_set_b32(&msg, msg32, NULL);
399400
while (1) {
400-
ret = noncefp(nonce32, msg32, seckey, NULL, (void*)noncedata, count);
401+
ret = noncefp(ctx, nonce32, msg32, seckey, NULL, (void*)noncedata, count);
401402
if (!ret) {
402403
break;
403404
}

src/tests.c

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3987,23 +3987,24 @@ void run_ecdsa_sign_verify(void) {
39873987
}
39883988

39893989
/** Dummy nonce generation function that just uses a precomputed nonce, and fails if it is not accepted. Use only for testing. */
3990-
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) {
3990+
static int precomputed_nonce_function(const secp256k1_context *context, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
3991+
(void)context;
39913992
(void)msg32;
39923993
(void)key32;
39933994
(void)algo16;
39943995
memcpy(nonce32, data, 32);
39953996
return (counter == 0);
39963997
}
39973998

3998-
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) {
3999+
static int nonce_function_test_fail(const secp256k1_context *context, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
39994000
/* Dummy nonce generator that has a fatal error on the first counter value. */
40004001
if (counter == 0) {
40014002
return 0;
40024003
}
4003-
return nonce_function_rfc6979(nonce32, msg32, key32, algo16, data, counter - 1);
4004+
return nonce_function_rfc6979(context, nonce32, msg32, key32, algo16, data, counter - 1);
40044005
}
40054006

4006-
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) {
4007+
static int nonce_function_test_retry(const secp256k1_context *context, unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *algo16, void *data, unsigned int counter) {
40074008
/* Dummy nonce generator that produces unacceptable nonces for the first several counter values. */
40084009
if (counter < 3) {
40094010
memset(nonce32, counter==0 ? 0 : 255, 32);
@@ -4030,7 +4031,7 @@ static int nonce_function_test_retry(unsigned char *nonce32, const unsigned char
40304031
if (counter > 5) {
40314032
return 0;
40324033
}
4033-
return nonce_function_rfc6979(nonce32, msg32, key32, algo16, data, counter - 5);
4034+
return nonce_function_rfc6979(context, nonce32, msg32, key32, algo16, data, counter - 5);
40344035
}
40354036

40364037
int is_empty_signature(const secp256k1_ecdsa_signature *sig) {
@@ -4910,13 +4911,13 @@ void test_ecdsa_edge_cases(void) {
49104911
VG_UNDEF(nonce2,32);
49114912
VG_UNDEF(nonce3,32);
49124913
VG_UNDEF(nonce4,32);
4913-
CHECK(nonce_function_rfc6979(nonce, zeros, zeros, NULL, NULL, 0) == 1);
4914+
CHECK(nonce_function_rfc6979(ctx, nonce, zeros, zeros, NULL, NULL, 0) == 1);
49144915
VG_CHECK(nonce,32);
4915-
CHECK(nonce_function_rfc6979(nonce2, zeros, zeros, zeros, NULL, 0) == 1);
4916+
CHECK(nonce_function_rfc6979(ctx, nonce2, zeros, zeros, zeros, NULL, 0) == 1);
49164917
VG_CHECK(nonce2,32);
4917-
CHECK(nonce_function_rfc6979(nonce3, zeros, zeros, NULL, (void *)zeros, 0) == 1);
4918+
CHECK(nonce_function_rfc6979(ctx, nonce3, zeros, zeros, NULL, (void *)zeros, 0) == 1);
49184919
VG_CHECK(nonce3,32);
4919-
CHECK(nonce_function_rfc6979(nonce4, zeros, zeros, zeros, (void *)zeros, 0) == 1);
4920+
CHECK(nonce_function_rfc6979(ctx, nonce4, zeros, zeros, zeros, (void *)zeros, 0) == 1);
49204921
VG_CHECK(nonce4,32);
49214922
CHECK(memcmp(nonce, nonce2, 32) != 0);
49224923
CHECK(memcmp(nonce, nonce3, 32) != 0);

src/tests_exhaustive.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,12 @@ void random_fe(secp256k1_fe *x) {
6969
}
7070
/** END stolen from tests.c */
7171

72-
int secp256k1_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
72+
int secp256k1_nonce_function_smallint(const secp256k1_context *ctx, unsigned char *nonce32, const unsigned char *msg32,
7373
const unsigned char *key32, const unsigned char *algo16,
7474
void *data, unsigned int attempt) {
7575
secp256k1_scalar s;
7676
int *idata = data;
77+
(void)ctx;
7778
(void)msg32;
7879
(void)key32;
7980
(void)algo16;

0 commit comments

Comments
 (0)