What changed, and why it matters
This commit is a test-only cleanup. It moves a helper function that checks SHA256 'tagged hash' setup from one test file to a shared test file, renames it, and updates several test modules to use the shared helper. There are no changes to the actual cryptographic library code that users rely on, and no security bug is fixed or introduced.
No security action needed. This is a routine test refactor; standard code review is sufficient.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The commit refactors test code in libsecp256k1. It relocates sha256_tag_test_internal from src/modules/musig/tests_impl.h to src/tests.c, renames it to test_sha256_tag_midstate, changes the tag parameter to const unsigned char *, and updates callers in ellswift, musig, and schnorrsig test modules. It also replaces some char arrays with static const unsigned char arrays and adds string comments for protocol tag readability. No production code is modified.
Changed components
src/modules/ellswift/tests_impl.hsrc/modules/musig/tests_impl.hsrc/modules/schnorrsig/tests_impl.hsrc/tests.cInspect captured patch +36 / −30
diff --git a/src/modules/ellswift/tests_impl.h b/src/modules/ellswift/tests_impl.h
index 3c314c9..b90fd0a 100644
--- a/src/modules/ellswift/tests_impl.h
+++ b/src/modules/ellswift/tests_impl.h
@@ -405,31 +405,31 @@ void run_ellswift_tests(void) {
/* Test hash initializers. */
{
- secp256k1_sha256 sha, sha_optimized;
+ secp256k1_sha256 sha_optimized;
+ /* "secp256k1_ellswift_encode" */
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'};
+ /* "secp256k1_ellswift_create" */
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'};
+ /* "bip324_ellswift_xonly_ecdh" */
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'};
/* Check that hash initialized by
* secp256k1_ellswift_sha256_init_encode has the expected
* state. */
- secp256k1_sha256_initialize_tagged(&sha, encode_tag, sizeof(encode_tag));
secp256k1_ellswift_sha256_init_encode(&sha_optimized);
- test_sha256_eq(&sha, &sha_optimized);
+ test_sha256_tag_midstate(&sha_optimized, encode_tag, sizeof(encode_tag));
/* Check that hash initialized by
* secp256k1_ellswift_sha256_init_create has the expected
* state. */
- secp256k1_sha256_initialize_tagged(&sha, create_tag, sizeof(create_tag));
secp256k1_ellswift_sha256_init_create(&sha_optimized);
- test_sha256_eq(&sha, &sha_optimized);
+ test_sha256_tag_midstate(&sha_optimized, create_tag, sizeof(create_tag));
/* Check that hash initialized by
* secp256k1_ellswift_sha256_init_bip324 has the expected
* state. */
- secp256k1_sha256_initialize_tagged(&sha, bip324_tag, sizeof(bip324_tag));
secp256k1_ellswift_sha256_init_bip324(&sha_optimized);
- test_sha256_eq(&sha, &sha_optimized);
+ test_sha256_tag_midstate(&sha_optimized, bip324_tag, sizeof(bip324_tag));
}
}
diff --git a/src/modules/musig/tests_impl.h b/src/modules/musig/tests_impl.h
index 4694c31..b57b262 100644
--- a/src/modules/musig/tests_impl.h
+++ b/src/modules/musig/tests_impl.h
@@ -548,40 +548,39 @@ static void musig_nonce_test(void) {
}
}
-static void sha256_tag_test_internal(secp256k1_sha256 *sha_tagged, unsigned char *tag, size_t taglen) {
- secp256k1_sha256 sha;
- secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
- test_sha256_eq(&sha, sha_tagged);
-}
-
/* Checks that the initialized tagged hashes have the expected
* state. */
static void sha256_tag_test(void) {
secp256k1_sha256 sha;
{
- char tag[] = "KeyAgg list";
+ /* "KeyAgg list" */
+ static const unsigned char tag[] = {'K', 'e', 'y', 'A', 'g', 'g', ' ', 'l', 'i', 's', 't'};
secp256k1_musig_keyagglist_sha256(&sha);
- sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
+ test_sha256_tag_midstate(&sha, tag, sizeof(tag));
}
{
- char tag[] = "KeyAgg coefficient";
+ /* "KeyAgg coefficient" */
+ static const unsigned char tag[] = {'K', 'e', 'y', 'A', 'g', 'g', ' ', 'c', 'o', 'e', 'f', 'f', 'i', 'c', 'i', 'e', 'n', 't'};
secp256k1_musig_keyaggcoef_sha256(&sha);
- sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
+ test_sha256_tag_midstate(&sha, tag, sizeof(tag));
}
{
- unsigned char tag[] = "MuSig/aux";
+ /* "MuSig/aux" */
+ static const unsigned char tag[] = { 'M', 'u', 'S', 'i', 'g', '/', 'a', 'u', 'x' };
secp256k1_nonce_function_musig_sha256_tagged_aux(&sha);
- sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
+ test_sha256_tag_midstate(&sha, tag, sizeof(tag));
}
{
- unsigned char tag[] = "MuSig/nonce";
+ /* "MuSig/nonce" */
+ static const unsigned char tag[] = { 'M', 'u', 'S', 'i', 'g', '/', 'n', 'o', 'n', 'c', 'e' };
secp256k1_nonce_function_musig_sha256_tagged(&sha);
- sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
+ test_sha256_tag_midstate(&sha, tag, sizeof(tag));
}
{
- unsigned char tag[] = "MuSig/noncecoef";
+ /* "MuSig/noncecoef" */
+ static const unsigned char tag[] = { 'M', 'u', 'S', 'i', 'g', '/', 'n', 'o', 'n', 'c', 'e', 'c', 'o', 'e', 'f' };
secp256k1_musig_compute_noncehash_sha256_tagged(&sha);
- sha256_tag_test_internal(&sha, (unsigned char*)tag, sizeof(tag) - 1);
+ test_sha256_tag_midstate(&sha, tag, sizeof(tag));
}
}
diff --git a/src/modules/schnorrsig/tests_impl.h b/src/modules/schnorrsig/tests_impl.h
index 2d716a0..5abbeef 100644
--- a/src/modules/schnorrsig/tests_impl.h
+++ b/src/modules/schnorrsig/tests_impl.h
@@ -21,11 +21,12 @@ static void nonce_function_bip340_bitflip(unsigned char **args, size_t n_flip, s
}
static void run_nonce_function_bip340_tests(void) {
- unsigned char tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
- unsigned char aux_tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'a', 'u', 'x'};
+ /* "BIP0340/nonce" */
+ static const unsigned char tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
+ /* "BIP0340/aux" */
+ static const unsigned char aux_tag[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'a', 'u', 'x'};
unsigned char algo[] = {'B', 'I', 'P', '0', '3', '4', '0', '/', 'n', 'o', 'n', 'c', 'e'};
size_t algolen = sizeof(algo);
- secp256k1_sha256 sha;
secp256k1_sha256 sha_optimized;
unsigned char nonce[32], nonce_z[32];
unsigned char msg[32];
@@ -39,16 +40,15 @@ static void run_nonce_function_bip340_tests(void) {
/* Check that hash initialized by
* secp256k1_nonce_function_bip340_sha256_tagged has the expected
* state. */
- secp256k1_sha256_initialize_tagged(&sha, tag, sizeof(tag));
secp256k1_nonce_function_bip340_sha256_tagged(&sha_optimized);
- test_sha256_eq(&sha, &sha_optimized);
+ test_sha256_tag_midstate(&sha_optimized, tag, sizeof(tag));
+
/* Check that hash initialized by
* secp256k1_nonce_function_bip340_sha256_tagged_aux has the expected
* state. */
- secp256k1_sha256_initialize_tagged(&sha, aux_tag, sizeof(aux_tag));
secp256k1_nonce_function_bip340_sha256_tagged_aux(&sha_optimized);
- test_sha256_eq(&sha, &sha_optimized);
+ test_sha256_tag_midstate(&sha_optimized, aux_tag, sizeof(aux_tag));
testrand256(msg);
testrand256(key);
diff --git a/src/tests.c b/src/tests.c
index 0f7b0d4..28bec59 100644
--- a/src/tests.c
+++ b/src/tests.c
@@ -609,6 +609,13 @@ static void test_sha256_eq(const secp256k1_sha256 *sha1, const secp256k1_sha256
CHECK(sha1->bytes == sha2->bytes);
CHECK(secp256k1_memcmp_var(sha1->s, sha2->s, sizeof(sha1->s)) == 0);
}
+/* Convenience function for using test_sha256_eq to verify the correctness of a
+ * tagged hash midstate. This function is used by some module tests. */
+static void test_sha256_tag_midstate(secp256k1_sha256 *sha_tagged, const unsigned char *tag, size_t taglen) {
+ secp256k1_sha256 sha;
+ secp256k1_sha256_initialize_tagged(&sha, tag, taglen);
+ test_sha256_eq(&sha, sha_tagged);
+}
static void run_hmac_sha256_tests(void) {
static const char *keys[6] = {
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.