Skip to content

Commit 383b791

Browse files
committed
schnorrsig: remove noncefp args from sign, add sign_custom function
This makes the default sign function easier to use while still allowing more granular control through sign_custom.
1 parent b8692c9 commit 383b791

File tree

5 files changed

+36
-14
lines changed

5 files changed

+36
-14
lines changed

include/secp256k1_schnorrsig.h

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -64,22 +64,38 @@ SECP256K1_API extern const secp256k1_nonce_function_hardened secp256k1_nonce_fun
6464
* signature. Instead, you can manually use secp256k1_schnorrsig_verify and
6565
* abort if it fails.
6666
*
67-
* Otherwise BIP-340 compliant if the noncefp argument is NULL or
68-
* secp256k1_nonce_function_bip340 and the ndata argument is 32-byte auxiliary
69-
* randomness.
70-
*
7167
* Returns 1 on success, 0 on failure.
7268
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
7369
* Out: sig64: pointer to a 64-byte array to store the serialized signature (cannot be NULL)
7470
* In: msg32: the 32-byte message being signed (cannot be NULL)
7571
* keypair: pointer to an initialized keypair (cannot be NULL)
76-
* noncefp: pointer to a nonce generation function. If NULL, secp256k1_nonce_function_bip340 is used
77-
* ndata: pointer to arbitrary data used by the nonce generation
78-
* function (can be NULL). If it is non-NULL and
79-
* secp256k1_nonce_function_bip340 is used, then ndata must be a
80-
* pointer to 32-byte auxiliary randomness as per BIP-340.
72+
* aux_rand32: 32 bytes of fresh randomness. While recommended to provide
73+
* this, it is only supplemental to security and can be NULL. See
74+
* BIP-340 for a full explanation of this argument and for
75+
* guidance if randomness is expensive.
8176
*/
8277
SECP256K1_API int secp256k1_schnorrsig_sign(
78+
const secp256k1_context* ctx,
79+
unsigned char *sig64,
80+
const unsigned char *msg32,
81+
const secp256k1_keypair *keypair,
82+
unsigned char *aux_rand32
83+
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
84+
85+
/** Create a Schnorr signature with a more flexible API.
86+
*
87+
* Same arguments as secp256k1_schnorrsig_sign except that it misses aux_rand32
88+
* and instead allows allows providing a different nonce derivation function
89+
* with its own data argument.
90+
*
91+
* In: noncefp: pointer to a nonce generation function. If NULL,
92+
* secp256k1_nonce_function_bip340 is used
93+
* ndata: pointer to arbitrary data used by the nonce generation function
94+
* (can be NULL). If it is non-NULL and
95+
* secp256k1_nonce_function_bip340 is used, then ndata must be a
96+
* pointer to 32-byte auxiliary randomness as per BIP-340.
97+
*/
98+
SECP256K1_API int secp256k1_schnorrsig_sign_custom(
8399
const secp256k1_context* ctx,
84100
unsigned char *sig64,
85101
const unsigned char *msg32,
@@ -88,6 +104,7 @@ SECP256K1_API int secp256k1_schnorrsig_sign(
88104
void *ndata
89105
) SECP256K1_ARG_NONNULL(1) SECP256K1_ARG_NONNULL(2) SECP256K1_ARG_NONNULL(3) SECP256K1_ARG_NONNULL(4);
90106

107+
91108
/** Verify a Schnorr signature.
92109
*
93110
* Returns: 1: correct signature

src/bench_schnorrsig.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ void bench_schnorrsig_sign(void* arg, int iters) {
3232
for (i = 0; i < iters; i++) {
3333
msg[0] = i;
3434
msg[1] = i >> 8;
35-
CHECK(secp256k1_schnorrsig_sign(data->ctx, sig, msg, data->keypairs[i], NULL, NULL));
35+
CHECK(secp256k1_schnorrsig_sign(data->ctx, sig, msg, data->keypairs[i], NULL));
3636
}
3737
}
3838

@@ -78,7 +78,7 @@ int main(void) {
7878
data.sigs[i] = sig;
7979

8080
CHECK(secp256k1_keypair_create(data.ctx, keypair, sk));
81-
CHECK(secp256k1_schnorrsig_sign(data.ctx, sig, msg, keypair, NULL, NULL));
81+
CHECK(secp256k1_schnorrsig_sign(data.ctx, sig, msg, keypair, NULL));
8282
CHECK(secp256k1_keypair_xonly_pub(data.ctx, &pk, NULL, keypair));
8383
CHECK(secp256k1_xonly_pubkey_serialize(data.ctx, pk_char, &pk) == 1);
8484
}

src/modules/schnorrsig/main_impl.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,12 @@ static void secp256k1_schnorrsig_challenge(secp256k1_scalar* e, const unsigned c
124124
secp256k1_scalar_set_b32(e, buf, NULL);
125125
}
126126

127-
int secp256k1_schnorrsig_sign(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, secp256k1_nonce_function_hardened noncefp, void *ndata) {
127+
128+
int secp256k1_schnorrsig_sign(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, unsigned char *aux_rand32) {
129+
return secp256k1_schnorrsig_sign_custom(ctx, sig64, msg32, keypair, NULL, aux_rand32);
130+
}
131+
132+
int secp256k1_schnorrsig_sign_custom(const secp256k1_context* ctx, unsigned char *sig64, const unsigned char *msg32, const secp256k1_keypair *keypair, secp256k1_nonce_function_hardened noncefp, void *ndata) {
128133
secp256k1_scalar sk;
129134
secp256k1_scalar e;
130135
secp256k1_scalar k;

src/modules/schnorrsig/tests_exhaustive_impl.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ static void test_exhaustive_schnorrsig_sign(const secp256k1_context *ctx, unsign
161161
unsigned char expected_s_bytes[32];
162162
secp256k1_scalar_get_b32(expected_s_bytes, &expected_s);
163163
/* Invoke the real function to construct a signature. */
164-
CHECK(secp256k1_schnorrsig_sign(ctx, sig64, msg32, &keypairs[d - 1], secp256k1_hardened_nonce_function_smallint, &k));
164+
CHECK(secp256k1_schnorrsig_sign_custom(ctx, sig64, msg32, &keypairs[d - 1], secp256k1_hardened_nonce_function_smallint, &k));
165165
/* The first 32 bytes must match the xonly pubkey for the specified k. */
166166
CHECK(secp256k1_memcmp_var(sig64, xonly_pubkey_bytes[k - 1], 32) == 0);
167167
/* The last 32 bytes must match the expected s value. */

src/valgrind_ctime_test.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ int main(void) {
147147
ret = secp256k1_keypair_create(ctx, &keypair, key);
148148
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
149149
CHECK(ret == 1);
150-
ret = secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL, NULL);
150+
ret = secp256k1_schnorrsig_sign(ctx, sig, msg, &keypair, NULL);
151151
VALGRIND_MAKE_MEM_DEFINED(&ret, sizeof(ret));
152152
CHECK(ret == 1);
153153
#endif

0 commit comments

Comments
 (0)