Skip to content

Commit 5c63646

Browse files
committed
f add ARG_CHECK to ensure s2c_opening and s2c_data32 are either both non-NULL or both NULL.
1 parent ee77436 commit 5c63646

File tree

3 files changed

+19
-15
lines changed

3 files changed

+19
-15
lines changed

include/secp256k1_schnorrsig.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ SECP256K1_API int secp256k1_schnorrsig_parse(
6161
* Returns 1 on success, 0 on failure.
6262
* Args: ctx: pointer to a context object, initialized for signing (cannot be NULL)
6363
* Out: sig: pointer to the returned signature (cannot be NULL)
64-
* s2c_data: pointer to an secp256k1_s2c_opening structure which can be
64+
* s2c_opening: pointer to an secp256k1_s2c_opening structure which can be
6565
* NULL but is required to be not NULL if this signature creates
6666
* a sign-to-contract commitment (i.e. the `s2c_data` argument
6767
* is not NULL).

src/modules/schnorrsig/main_impl.h

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ int secp256k1_schnorrsig_sign(const secp256k1_context* ctx, secp256k1_schnorrsig
7676
* because we need to ensure that s2c_data is actually hashed into the nonce and
7777
* not just ignored. */
7878
ARG_CHECK(s2c_data32 == NULL || (noncefp == NULL || noncefp == secp256k1_nonce_function_bipschnorr));
79+
/* s2c_opening and s2c_data32 should be either both non-NULL or both NULL. */
80+
ARG_CHECK((s2c_opening != NULL) == (s2c_data32 != NULL));
7981

8082
if (noncefp == NULL) {
8183
noncefp = secp256k1_nonce_function_bipschnorr;

src/modules/schnorrsig/tests_impl.h

+16-14
Original file line numberDiff line numberDiff line change
@@ -72,17 +72,21 @@ void test_schnorrsig_api(secp256k1_scratch_space *scratch) {
7272
CHECK(ecount == 2);
7373
CHECK(secp256k1_schnorrsig_sign(sign, NULL, &s2c_opening, msg, sk1, s2c_data32, NULL, NULL) == 0);
7474
CHECK(ecount == 3);
75-
CHECK(secp256k1_schnorrsig_sign(sign, &sig, NULL, msg, sk1, s2c_data32, NULL, NULL) == 1);
76-
CHECK(ecount == 3);
77-
CHECK(secp256k1_schnorrsig_sign(sign, &sig, &s2c_opening, NULL, sk1, s2c_data32, NULL, NULL) == 0);
75+
CHECK(secp256k1_schnorrsig_sign(sign, &sig, NULL, msg, sk1, s2c_data32, NULL, NULL) == 0);
7876
CHECK(ecount == 4);
79-
CHECK(secp256k1_schnorrsig_sign(sign, &sig, &s2c_opening, msg, NULL, s2c_data32, NULL, NULL) == 0);
80-
CHECK(ecount == 5);
81-
CHECK(secp256k1_schnorrsig_sign(sign, &sig, &s2c_opening, msg, sk1, NULL, NULL, NULL) == 1);
77+
CHECK(secp256k1_schnorrsig_sign(sign, &sig, &s2c_opening, NULL, sk1, s2c_data32, NULL, NULL) == 0);
8278
CHECK(ecount == 5);
79+
CHECK(secp256k1_schnorrsig_sign(sign, &sig, &s2c_opening, msg, NULL, s2c_data32, NULL, NULL) == 0);
80+
CHECK(ecount == 6);
81+
CHECK(secp256k1_schnorrsig_sign(sign, &sig, &s2c_opening, msg, sk1, NULL, NULL, NULL) == 0);
82+
CHECK(ecount == 7);
83+
/* It's okay if both s2c_opening and s2c_data32 are NULL. It's just not okay if
84+
* only a single one of them is NULL. */
85+
CHECK(secp256k1_schnorrsig_sign(sign, &sig, NULL, msg, sk1, NULL, NULL, NULL) == 1);
86+
CHECK(ecount == 7);
8387
/* s2c commitments with a different nonce function than bipschnorr are not allowed */
8488
CHECK(secp256k1_schnorrsig_sign(sign, &sig, &s2c_opening, msg, sk1, s2c_data32, secp256k1_nonce_function_rfc6979, NULL) == 0);
85-
CHECK(ecount == 6);
89+
CHECK(ecount == 8);
8690

8791
ecount = 0;
8892
CHECK(secp256k1_schnorrsig_serialize(none, sig64, &sig) == 1);
@@ -170,14 +174,12 @@ void test_schnorrsig_api(secp256k1_scratch_space *scratch) {
170174

171175
/* Helper function for schnorrsig_bip_vectors
172176
* Signs the message and checks that it's the same as expected_sig. */
173-
void test_schnorrsig_bip_vectors_check_signing(const unsigned char *sk, const unsigned char *pk_serialized, const unsigned char *msg, const unsigned char *expected_sig, const int expected_nonce_is_negated) {
177+
void test_schnorrsig_bip_vectors_check_signing(const unsigned char *sk, const unsigned char *pk_serialized, const unsigned char *msg, const unsigned char *expected_sig) {
174178
secp256k1_schnorrsig sig;
175179
unsigned char serialized_sig[64];
176180
secp256k1_pubkey pk;
177-
secp256k1_s2c_opening s2c_opening;
178181

179-
CHECK(secp256k1_schnorrsig_sign(ctx, &sig, &s2c_opening, msg, sk, NULL, NULL, NULL));
180-
CHECK(s2c_opening.nonce_is_negated == expected_nonce_is_negated);
182+
CHECK(secp256k1_schnorrsig_sign(ctx, &sig, NULL, msg, sk, NULL, NULL, NULL));
181183
CHECK(secp256k1_schnorrsig_serialize(ctx, serialized_sig, &sig));
182184
CHECK(memcmp(serialized_sig, expected_sig, 64) == 0);
183185

@@ -240,7 +242,7 @@ void test_schnorrsig_bip_vectors(secp256k1_scratch_space *scratch) {
240242
0x2C, 0xCD, 0x00, 0x79, 0xE1, 0xF9, 0x2A, 0xF1,
241243
0x77, 0xF7, 0xF2, 0x2C, 0xC1, 0xDC, 0xED, 0x05
242244
};
243-
test_schnorrsig_bip_vectors_check_signing(sk1, pk1, msg1, sig1, 1);
245+
test_schnorrsig_bip_vectors_check_signing(sk1, pk1, msg1, sig1);
244246
test_schnorrsig_bip_vectors_check_verify(scratch, pk1, msg1, sig1, 1);
245247
}
246248
{
@@ -274,7 +276,7 @@ void test_schnorrsig_bip_vectors(secp256k1_scratch_space *scratch) {
274276
0x5F, 0xFC, 0x2D, 0x03, 0x5A, 0x23, 0x04, 0x34,
275277
0xA1, 0xA6, 0x4D, 0xC5, 0x9F, 0x70, 0x13, 0xFD
276278
};
277-
test_schnorrsig_bip_vectors_check_signing(sk2, pk2, msg2, sig2, 0);
279+
test_schnorrsig_bip_vectors_check_signing(sk2, pk2, msg2, sig2);
278280
test_schnorrsig_bip_vectors_check_verify(scratch, pk2, msg2, sig2, 1);
279281
}
280282
{
@@ -308,7 +310,7 @@ void test_schnorrsig_bip_vectors(secp256k1_scratch_space *scratch) {
308310
0xA5, 0x83, 0x7E, 0xC5, 0x7F, 0xED, 0x76, 0x60,
309311
0x77, 0x3A, 0x05, 0xF0, 0xDE, 0x14, 0x23, 0x80
310312
};
311-
test_schnorrsig_bip_vectors_check_signing(sk3, pk3, msg3, sig3, 0);
313+
test_schnorrsig_bip_vectors_check_signing(sk3, pk3, msg3, sig3);
312314
test_schnorrsig_bip_vectors_check_verify(scratch, pk3, msg3, sig3, 1);
313315
}
314316
{

0 commit comments

Comments
 (0)