Skip to content

Commit f36afb8

Browse files
Merge #1725: tests: refactor tagged hash verification
5153cf1 tests: refactor tagged hash tests (josibake) Pull request description: Opened in response to #1698 (comment) --- We use tagged hashes in `modules/musig`, `modules/schnorrsig`, `modules/ellswift`, and the proposed `modules/silentpayments`. In looking for inspiration on how to add tagged hash midstate verification for #1698, it seemed like a good opportunity to DRY up the code across all of the modules. I chose the convention used in the ellswift module as this seems the most idiomatic C. Since the tags are normally specified as strings in the BIPs, I also added a comment above each char array for convenience. If its deemed too invasive to refactor the existing modules in this PR, I'm happy to drop the refactor commits for the ellswift and schnorrsig modules. All I need for #1698 is the first commit which moves the utility function out of the musig module to make it available to use in the silent payments module. ACKs for top commit: real-or-random: utACK 5153cf1 assuming CI passes theStack: Code-review ACK 5153cf1 Tree-SHA512: 335ec3ee6a265e13cc379968f8fa1624534bef2389e4e21b85e6a9572ce1bd9dee4eabd2cb6d187ac974db3ab8246c2626d309ccfbee5744c30cf7560d1e261c
2 parents d2dcf52 + 5153cf1 commit f36afb8

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)