hash: add midstate initializer and use it for tagged hashes

Introduce secp256k1_sha256_initialize_midstate() in the hash layer and use it at all tagged-hash midstate call sites across schnorrsig, musig, and ellswift.

Document the byte-counter contract at the declaration site in hash.h and add run_sha256_initialize_midstate_tests() to directly verify helper behavior against initialize_tagged.

Also switch the helper to take const uint32_t state[8] to reduce argument-order risk at call sites.
This commit is contained in:
w0xlt
2026-02-18 15:21:54 -08:00
parent 471e3a130d
commit f48b1bfa5d
7 changed files with 80 additions and 118 deletions

View File

@@ -739,6 +739,19 @@ static void run_tagged_sha256_tests(void) {
CHECK(secp256k1_memcmp_var(hash32, hash_expected, sizeof(hash32)) == 0);
}
static void run_sha256_initialize_midstate_tests(void) {
/* Midstate for the tagged hash with tag "sha256_midstate_test_tag". */
static const unsigned char tag[] = "sha256_midstate_test_tag";
static const uint32_t midstate[8] = {
0xa9ec59eaul, 0x9b4c2ffful, 0x400821e2ul, 0x0dcf3847ul,
0xbe7ea179ul, 0xa5772bdcul, 0x7d29bfe3ul, 0xa486b855ul
};
secp256k1_sha256 sha;
secp256k1_sha256_initialize_midstate(&sha, 64, midstate);
test_sha256_tag_midstate(&sha, tag, sizeof(tag) - 1);
}
/***** MODINV TESTS *****/
/* Compute the modular inverse of (odd) x mod 2^64. */
@@ -7709,6 +7722,7 @@ static const struct tf_test_entry tests_hash[] = {
CASE(hmac_sha256_tests),
CASE(rfc6979_hmac_sha256_tests),
CASE(tagged_sha256_tests),
CASE(sha256_initialize_midstate_tests),
};
static const struct tf_test_entry tests_scalar[] = {
@@ -7848,4 +7862,3 @@ int main(int argc, char **argv) {
if (tf_init(&tf, argc, argv) != 0) return EXIT_FAILURE;
return tf_run(&tf);
}