Skip to content

Commit 5153cf1

Browse files
committed
tests: refactor tagged hash tests
Move the sha256_tag_test_internal function out of the musig module into tests.c. This makes it available to other modules wishing to verify tagged hashes without needing to duplicate the function. Change the function signature to expect a const unsigned char and update the tagged hash tests to use static const unsigned char character arrays (where necessary). Add a comment for each tag. This is done as a convenience for checking the strings against the protocol specifications, where the tags are normally specified as strings. Update tests in the ellswift and schnorrsig modules to use the sha256_tag_test_internal helper function.
1 parent d599714 commit 5153cf1

File tree

4 files changed

+36
-30
lines changed

4 files changed

+36
-30
lines changed

src/modules/ellswift/tests_impl.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -405,31 +405,31 @@ void run_ellswift_tests(void) {
405405

406406
/* Test hash initializers. */
407407
{
408-
secp256k1_sha256 sha, sha_optimized;
408+
secp256k1_sha256 sha_optimized;
409+
/* "secp256k1_ellswift_encode" */
409410
static const unsigned char encode_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'e', 'n', 'c', 'o', 'd', 'e'};
411+
/* "secp256k1_ellswift_create" */
410412
static const unsigned char create_tag[] = {'s', 'e', 'c', 'p', '2', '5', '6', 'k', '1', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'c', 'r', 'e', 'a', 't', 'e'};
413+
/* "bip324_ellswift_xonly_ecdh" */
411414
static const unsigned char bip324_tag[] = {'b', 'i', 'p', '3', '2', '4', '_', 'e', 'l', 'l', 's', 'w', 'i', 'f', 't', '_', 'x', 'o', 'n', 'l', 'y', '_', 'e', 'c', 'd', 'h'};
412415

413416
/* Check that hash initialized by
414417
* secp256k1_ellswift_sha256_init_encode has the expected
415418
* state. */
416-
secp256k1_sha256_initialize_tagged(&sha, encode_tag, sizeof(encode_tag));
417419
secp256k1_ellswift_sha256_init_encode(&sha_optimized);
418-
test_sha256_eq(&sha, &sha_optimized);
420+
test_sha256_tag_midstate(&sha_optimized, encode_tag, sizeof(encode_tag));
419421

420422
/* Check that hash initialized by
421423
* secp256k1_ellswift_sha256_init_create has the expected
422424
* state. */
423-
secp256k1_sha256_initialize_tagged(&sha, create_tag, sizeof(create_tag));
424425
secp256k1_ellswift_sha256_init_create(&sha_optimized);
425-
test_sha256_eq(&sha, &sha_optimized);
426+
test_sha256_tag_midstate(&sha_optimized, create_tag, sizeof(create_tag));
426427

427428
/* Check that hash initialized by
428429
* secp256k1_ellswift_sha256_init_bip324 has the expected
429430
* state. */
430-
secp256k1_sha256_initialize_tagged(&sha, bip324_tag, sizeof(bip324_tag));
431431
secp256k1_ellswift_sha256_init_bip324(&sha_optimized);
432-
test_sha256_eq(&sha, &sha_optimized);
432+
test_sha256_tag_midstate(&sha_optimized, bip324_tag, sizeof(bip324_tag));
433433
}
434434
}
435435

src/modules/musig/tests_impl.h

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -548,40 +548,39 @@ static void musig_nonce_test(void) {
548548
}
549549
}
550550

551-
static void sha256_tag_test_internal(secp256k1_sha256 *sha_tagged, unsigned char *tag, size_t taglen) {
552-
secp256k1_sha256 sha;
553-
secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
554-
test_sha256_eq(&sha, sha_tagged);
555-
}
556-
557551
/* Checks that the initialized tagged hashes have the expected
558552
* state. */
559553
static void sha256_tag_test(void) {
560554
secp256k1_sha256 sha;
561555
{
562-
char tag[] = "KeyAgg list";
556+
/* "KeyAgg list" */
557+
static const unsigned char tag[] = {'K', 'e', 'y', 'A', 'g', 'g', ' ', 'l', 'i', 's', 't'};
563558
secp256k1_musig_keyagglist_sha256(&sha);
564-
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
559+
test_sha256_tag_midstate(&sha, tag, sizeof(tag));
565560
}
566561
{
567-
char tag[] = "KeyAgg coefficient";
562+
/* "KeyAgg coefficient" */
563+
static const unsigned char tag[] = {'K', 'e', 'y', 'A', 'g', 'g', ' ', 'c', 'o', 'e', 'f', 'f', 'i', 'c', 'i', 'e', 'n', 't'};
568564
secp256k1_musig_keyaggcoef_sha256(&sha);
569-
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
565+
test_sha256_tag_midstate(&sha, tag, sizeof(tag));
570566
}
571567
{
572-
unsigned char tag[] = "MuSig/aux";
568+
/* "MuSig/aux" */
569+
static const unsigned char tag[] = { 'M', 'u', 'S', 'i', 'g', '/', 'a', 'u', 'x' };
573570
secp256k1_nonce_function_musig_sha256_tagged_aux(&sha);
574-
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
571+
test_sha256_tag_midstate(&sha, tag, sizeof(tag));
575572
}
576573
{
577-
unsigned char tag[] = "MuSig/nonce";
574+
/* "MuSig/nonce" */
575+
static const unsigned char tag[] = { 'M', 'u', 'S', 'i', 'g', '/', 'n', 'o', 'n', 'c', 'e' };
578576
secp256k1_nonce_function_musig_sha256_tagged(&sha);
579-
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
577+
test_sha256_tag_midstate(&sha, tag, sizeof(tag));
580578
}
581579
{
582-
unsigned char tag[] = "MuSig/noncecoef";
580+
/* "MuSig/noncecoef" */
581+
static const unsigned char tag[] = { 'M', 'u', 'S', 'i', 'g', '/', 'n', 'o', 'n', 'c', 'e', 'c', 'o', 'e', 'f' };
583582
secp256k1_musig_compute_noncehash_sha256_tagged(&sha);
584-
sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
583+
test_sha256_tag_midstate(&sha, tag, sizeof(tag));
585584
}
586585
}
587586

src/modules/schnorrsig/tests_impl.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ static void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, s
2121
}
2222

2323
static void run_nonce_function_bip340_tests(void) {
24-
unsigned char tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
25-
unsigned char aux_tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'a', 'u', 'x'};
24+
/* "BIP0340/nonce" */
25+
static const unsigned char tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
26+
/* "BIP0340/aux" */
27+
static const unsigned char aux_tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'a', 'u', 'x'};
2628
unsigned char algo[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
2729
size_t algolen = sizeof(algo);
28-
secp256k1_sha256 sha;
2930
secp256k1_sha256 sha_optimized;
3031
unsigned char nonce[32], nonce_z[32];
3132
unsigned char msg[32];
@@ -39,16 +40,15 @@ static void run_nonce_function_bip340_tests(void) {
3940
/* Check that hash initialized by
4041
* secp256k1_nonce_function_bip340_sha256_tagged has the expected
4142
* state. */
42-
secp256k1_sha256_initialize_tagged(&sha, tag, sizeof(tag));
4343
secp256k1_nonce_function_bip340_sha256_tagged(&sha_optimized);
44-
test_sha256_eq(&sha, &sha_optimized);
44+
test_sha256_tag_midstate(&sha_optimized, tag, sizeof(tag));
45+
4546

4647
/* Check that hash initialized by
4748
* secp256k1_nonce_function_bip340_sha256_tagged_aux has the expected
4849
* state. */
49-
secp256k1_sha256_initialize_tagged(&sha, aux_tag, sizeof(aux_tag));
5050
secp256k1_nonce_function_bip340_sha256_tagged_aux(&sha_optimized);
51-
test_sha256_eq(&sha, &sha_optimized);
51+
test_sha256_tag_midstate(&sha_optimized, aux_tag, sizeof(aux_tag));
5252

5353
testrand256(msg);
5454
testrand256(key);

src/tests.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,6 +609,13 @@ static void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256
609609
CHECK(sha1->bytes == sha2->bytes);
610610
CHECK(secp256k1_memcmp_var(sha1->s, sha2->s, sizeof(sha1->s)) == 0);
611611
}
612+
/* Convenience function for using test_sha256_eq to verify the correctness of a
613+
* tagged hash midstate. This function is used by some module tests. */
614+
static void test_sha256_tag_midstate(secp256k1_sha256 *sha_tagged, const unsigned char *tag, size_t taglen) {
615+
secp256k1_sha256 sha;
616+
secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
617+
test_sha256_eq(&sha, sha_tagged);
618+
}
612619

613620
static void run_hmac_sha256_tests(void) {
614621
static const char *keys[6] = {

0 commit comments

Comments
 (0)