Skip to content

Commit 442cee5

Browse files
committed
schnorrsig: add algolen argument to nonce_function_hardened
This avoids having to remove trailing NUL bytes in the nonce function
1 parent df3bfa1 commit 442cee5

File tree

4 files changed

+67
-59
lines changed

4 files changed

+67
-59
lines changed

include/secp256k1_schnorrsig.h

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,15 @@ extern "C" {
2323
*
2424
* Returns: 1 if a nonce was successfully generated. 0 will cause signing to
2525
* return an error.
26-
* Out: nonce32: pointer to a 32-byte array to be filled by the function.
27-
* In: msg32: the 32-byte message hash being verified (will not be NULL)
28-
* key32: pointer to a 32-byte secret key (will not be NULL)
29-
* xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key32
30-
* (will not be NULL)
31-
* algo16: pointer to a 16-byte array describing the signature
32-
* algorithm (will not be NULL).
33-
* data: Arbitrary data pointer that is passed through.
26+
* Out: nonce32: pointer to a 32-byte array to be filled by the function
27+
* In: msg32: the 32-byte message hash being verified (will not be NULL)
28+
* key32: pointer to a 32-byte secret key (will not be NULL)
29+
* xonly_pk32: the 32-byte serialized xonly pubkey corresponding to key32
30+
* (will not be NULL)
31+
* algo: pointer to an array describing the signature
32+
* algorithm (will not be NULL)
33+
* algolen: the length of the algo array
34+
* data: arbitrary data pointer that is passed through
3435
*
3536
* Except for test cases, this function should compute some cryptographic hash of
3637
* the message, the key, the pubkey, the algorithm description, and data.
@@ -40,7 +41,8 @@ typedef int (*secp256k1_nonce_function_hardened)(
4041
const unsigned char *msg32,
4142
const unsigned char *key32,
4243
const unsigned char *xonly_pk32,
43-
const unsigned char *algo16,
44+
const unsigned char *algo,
45+
size_t algolen,
4446
void *data
4547
);
4648

@@ -51,10 +53,10 @@ typedef int (*secp256k1_nonce_function_hardened)(
5153
* If a data pointer is passed, it is assumed to be a pointer to 32 bytes of
5254
* auxiliary random data as defined in BIP-340. If the data pointer is NULL,
5355
* the nonce derivation procedure follows BIP-340 by setting the auxiliary
54-
* random data to zero. The algo16 argument must be non-NULL, otherwise the
55-
* function will fail and return 0. The hash will be tagged with algo16 after
56-
* removing all terminating null bytes. Therefore, to create BIP-340 compliant
57-
* signatures, algo16 must be set to "BIP0340/nonce\0\0\0"
56+
* random data to zero. The algo argument must be non-NULL, otherwise the
57+
* function will fail and return 0. The hash will be tagged with algo.
58+
* Therefore, to create BIP-340 compliant signatures, algo must be set to
59+
* "BIP0340/nonce" and algolen to 13.
5860
*/
5961
SECP256K1_API extern const secp256k1_nonce_function_hardened secp256k1_nonce_function_bip340;
6062

src/modules/schnorrsig/main_impl.h

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ static void secp256k1_nonce_function_bip340_sha256_tagged_aux(secp256k1_sha256 *
4343
sha->bytes = 64;
4444
}
4545

46-
/* algo16 argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340
46+
/* algo argument for nonce_function_bip340 to derive the nonce exactly as stated in BIP-340
4747
* by using the correct tagged hash function. */
48-
static const unsigned char bip340_algo16[16] = "BIP0340/nonce\0\0\0";
48+
static const unsigned char bip340_algo[13] = "BIP0340/nonce";
4949

50-
static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo16, void *data) {
50+
static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) {
5151
secp256k1_sha256 sha;
5252
unsigned char masked_key[32];
5353
int i;
5454

55-
if (algo16 == NULL) {
55+
if (algo == NULL) {
5656
return 0;
5757
}
5858

@@ -65,18 +65,14 @@ static int nonce_function_bip340(unsigned char *nonce32, const unsigned char *ms
6565
}
6666
}
6767

68-
/* Tag the hash with algo16 which is important to avoid nonce reuse across
68+
/* Tag the hash with algo which is important to avoid nonce reuse across
6969
* algorithms. If this nonce function is used in BIP-340 signing as defined
7070
* in the spec, an optimized tagging implementation is used. */
71-
if (secp256k1_memcmp_var(algo16, bip340_algo16, 16) == 0) {
71+
if (algolen == sizeof(bip340_algo)
72+
&& secp256k1_memcmp_var(algo, bip340_algo, algolen) == 0) {
7273
secp256k1_nonce_function_bip340_sha256_tagged(&sha);
7374
} else {
74-
int algo16_len = 16;
75-
/* Remove terminating null bytes */
76-
while (algo16_len > 0 && !algo16[algo16_len - 1]) {
77-
algo16_len--;
78-
}
79-
secp256k1_sha256_initialize_tagged(&sha, algo16, algo16_len);
75+
secp256k1_sha256_initialize_tagged(&sha, algo, algolen);
8076
}
8177

8278
/* Hash (masked-)key||pk||msg using the tagged hash as per the spec */
@@ -156,7 +152,7 @@ int secp256k1_schnorrsig_sign(const secp256k1_context* ctx, unsigned char *sig64
156152

157153
secp256k1_scalar_get_b32(seckey, &sk);
158154
secp256k1_fe_get_b32(pk_buf, &pk.x);
159-
ret &= !!noncefp(buf, msg32, seckey, pk_buf, bip340_algo16, ndata);
155+
ret &= !!noncefp(buf, msg32, seckey, pk_buf, bip340_algo, sizeof(bip340_algo), ndata);
160156
secp256k1_scalar_set_b32(&k, buf, NULL);
161157
ret &= !secp256k1_scalar_is_zero(&k);
162158
secp256k1_scalar_cmov(&k, &secp256k1_scalar_one, !ret);

src/modules/schnorrsig/tests_exhaustive_impl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,15 @@ static const unsigned char invalid_pubkey_bytes[][32] = {
6060

6161
static int secp256k1_hardened_nonce_function_smallint(unsigned char *nonce32, const unsigned char *msg32,
6262
const unsigned char *key32, const unsigned char *xonly_pk32,
63-
const unsigned char *algo16, void* data) {
63+
const unsigned char *algo, size_t algolen,
64+
void* data) {
6465
secp256k1_scalar s;
6566
int *idata = data;
6667
(void)msg32;
6768
(void)key32;
6869
(void)xonly_pk32;
69-
(void)algo16;
70+
(void)algo;
71+
(void)algolen;
7072
secp256k1_scalar_set_int(&s, *idata);
7173
secp256k1_scalar_get_b32(nonce32, &s);
7274
return 1;

src/modules/schnorrsig/tests_impl.h

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@
1212
/* Checks that a bit flip in the n_flip-th argument (that has n_bytes many
1313
* bytes) changes the hash function
1414
*/
15-
void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, size_t n_bytes) {
15+
void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, size_t n_bytes, size_t algolen) {
1616
unsigned char nonces[2][32];
17-
CHECK(nonce_function_bip340(nonces[0], args[0], args[1], args[2], args[3], args[4]) == 1);
17+
CHECK(nonce_function_bip340(nonces[0], args[0], args[1], args[2], args[3], algolen, args[4]) == 1);
1818
secp256k1_testrand_flip(args[n_flip], n_bytes);
19-
CHECK(nonce_function_bip340(nonces[1], args[0], args[1], args[2], args[3], args[4]) == 1);
19+
CHECK(nonce_function_bip340(nonces[1], args[0], args[1], args[2], args[3], algolen, args[4]) == 1);
2020
CHECK(secp256k1_memcmp_var(nonces[0], nonces[1], 32) != 0);
2121
}
2222

@@ -34,7 +34,8 @@ void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256 *sha2)
3434
void run_nonce_function_bip340_tests(void) {
3535
unsigned char tag[13] = "BIP0340/nonce";
3636
unsigned char aux_tag[11] = "BIP0340/aux";
37-
unsigned char algo16[16] = "BIP0340/nonce\0\0\0";
37+
unsigned char algo[13] = "BIP0340/nonce";
38+
size_t algolen = sizeof(algo);
3839
secp256k1_sha256 sha;
3940
secp256k1_sha256 sha_optimized;
4041
unsigned char nonce[32];
@@ -68,33 +69,37 @@ void run_nonce_function_bip340_tests(void) {
6869
args[0] = msg;
6970
args[1] = key;
7071
args[2] = pk;
71-
args[3] = algo16;
72+
args[3] = algo;
7273
args[4] = aux_rand;
7374
for (i = 0; i < count; i++) {
74-
nonce_function_bip340_bitflip(args, 0, 32);
75-
nonce_function_bip340_bitflip(args, 1, 32);
76-
nonce_function_bip340_bitflip(args, 2, 32);
77-
/* Flip algo16 special case "BIP0340/nonce" */
78-
nonce_function_bip340_bitflip(args, 3, 16);
79-
/* Flip algo16 again */
80-
nonce_function_bip340_bitflip(args, 3, 16);
81-
nonce_function_bip340_bitflip(args, 4, 32);
75+
nonce_function_bip340_bitflip(args, 0, 32, algolen);
76+
nonce_function_bip340_bitflip(args, 1, 32, algolen);
77+
nonce_function_bip340_bitflip(args, 2, 32, algolen);
78+
/* Flip algo special case "BIP0340/nonce" */
79+
nonce_function_bip340_bitflip(args, 3, algolen, algolen);
80+
/* Flip algo again */
81+
nonce_function_bip340_bitflip(args, 3, algolen, algolen);
82+
nonce_function_bip340_bitflip(args, 4, 32, algolen);
8283
}
8384

84-
/* NULL algo16 is disallowed */
85-
CHECK(nonce_function_bip340(nonce, msg, key, pk, NULL, NULL) == 0);
86-
/* Empty algo16 is fine */
87-
memset(algo16, 0x00, 16);
88-
CHECK(nonce_function_bip340(nonce, msg, key, pk, algo16, NULL) == 1);
89-
/* algo16 with terminating null bytes is fine */
90-
algo16[1] = 65;
91-
CHECK(nonce_function_bip340(nonce, msg, key, pk, algo16, NULL) == 1);
92-
/* Other algo16 is fine */
93-
memset(algo16, 0xFF, 16);
94-
CHECK(nonce_function_bip340(nonce, msg, key, pk, algo16, NULL) == 1);
85+
/* NULL algo is disallowed */
86+
CHECK(nonce_function_bip340(nonce, msg, key, pk, NULL, 0, NULL) == 0);
87+
CHECK(nonce_function_bip340(nonce, msg, key, pk, algo, algolen, NULL) == 1);
88+
/* Other algo is fine */
89+
secp256k1_rfc6979_hmac_sha256_generate(&secp256k1_test_rng, algo, algolen);
90+
CHECK(nonce_function_bip340(nonce, msg, key, pk, algo, algolen, NULL) == 1);
91+
92+
for (i = 0; i < count; i++) {
93+
unsigned char nonce2[32];
94+
/* Different algolen gives different nonce */
95+
uint32_t offset = secp256k1_testrand_int(algolen - 1);
96+
size_t algolen_tmp = (algolen + offset) % algolen;
97+
CHECK(nonce_function_bip340(nonce2, msg, key, pk, algo, algolen_tmp, NULL) == 1);
98+
CHECK(secp256k1_memcmp_var(nonce, nonce2, 32) != 0);
99+
}
95100

96101
/* NULL aux_rand argument is allowed. */
97-
CHECK(nonce_function_bip340(nonce, msg, key, pk, algo16, NULL) == 1);
102+
CHECK(nonce_function_bip340(nonce, msg, key, pk, algo, algolen, NULL) == 1);
98103
}
99104

100105
void test_schnorrsig_api(void) {
@@ -634,34 +639,37 @@ void test_schnorrsig_bip_vectors(void) {
634639
}
635640

636641
/* Nonce function that returns constant 0 */
637-
static int nonce_function_failing(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo16, void *data) {
642+
static int nonce_function_failing(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) {
638643
(void) msg32;
639644
(void) key32;
640645
(void) xonly_pk32;
641-
(void) algo16;
646+
(void) algo;
647+
(void) algolen;
642648
(void) data;
643649
(void) nonce32;
644650
return 0;
645651
}
646652

647653
/* Nonce function that sets nonce to 0 */
648-
static int nonce_function_0(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo16, void *data) {
654+
static int nonce_function_0(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) {
649655
(void) msg32;
650656
(void) key32;
651657
(void) xonly_pk32;
652-
(void) algo16;
658+
(void) algo;
659+
(void) algolen;
653660
(void) data;
654661

655662
memset(nonce32, 0, 32);
656663
return 1;
657664
}
658665

659666
/* Nonce function that sets nonce to 0xFF...0xFF */
660-
static int nonce_function_overflowing(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo16, void *data) {
667+
static int nonce_function_overflowing(unsigned char *nonce32, const unsigned char *msg32, const unsigned char *key32, const unsigned char *xonly_pk32, const unsigned char *algo, size_t algolen, void *data) {
661668
(void) msg32;
662669
(void) key32;
663670
(void) xonly_pk32;
664-
(void) algo16;
671+
(void) algo;
672+
(void) algolen;
665673
(void) data;
666674

667675
memset(nonce32, 0xFF, 32);

0 commit comments

Comments
 (0)