Skip to content

Commit fb25ee6

Browse files
Add test vectors for very long SHA256 messages
One of the new vectors is a message of size 2^33 bits and verifies that we handle the bit counter in the SHA256 correctly. It is however commented out because it takes very long to run (6s on my machine). The vectors have been taken from https://www.di-mgt.com.au/sha_testvectors.html. They can be independently verified using the following Python code. ``` h = hashlib.sha256() for i in range(1_000_000): h.update(b'a') print(h.hexdigest()) h = hashlib.sha256() for i in range(16_777_216): h.update(b'abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno') print(h.hexdigest()) ```
1 parent bb3db0d commit fb25ee6

File tree

1 file changed

+32
-12
lines changed

1 file changed

+32
-12
lines changed

src/tests.c

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -408,35 +408,55 @@ void run_scratch_tests(void) {
408408
/***** HASH TESTS *****/
409409

410410
void run_sha256_tests(void) {
411-
static const char *inputs[8] = {
411+
static const char *inputs[] = {
412412
"", "abc", "message digest", "secure hash algorithm", "SHA256 is considered to be safe",
413413
"abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq",
414414
"For this sample, this 63-byte string will be used as input data",
415-
"This is exactly 64 bytes long, not counting the terminating byte"
415+
"This is exactly 64 bytes long, not counting the terminating byte",
416+
"aaaaa"
417+
/* Uncomment for test with extremely long input message. */
418+
/*, "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmnhijklmno" */
416419
};
417-
static const unsigned char outputs[8][32] = {
420+
static const unsigned int repeat[] = {
421+
1, 1, 1, 1, 1, 1, 1, 1, 1000000/5, 16777216
422+
};
423+
static const unsigned char outputs[][32] = {
418424
{0xe3, 0xb0, 0xc4, 0x42, 0x98, 0xfc, 0x1c, 0x14, 0x9a, 0xfb, 0xf4, 0xc8, 0x99, 0x6f, 0xb9, 0x24, 0x27, 0xae, 0x41, 0xe4, 0x64, 0x9b, 0x93, 0x4c, 0xa4, 0x95, 0x99, 0x1b, 0x78, 0x52, 0xb8, 0x55},
419425
{0xba, 0x78, 0x16, 0xbf, 0x8f, 0x01, 0xcf, 0xea, 0x41, 0x41, 0x40, 0xde, 0x5d, 0xae, 0x22, 0x23, 0xb0, 0x03, 0x61, 0xa3, 0x96, 0x17, 0x7a, 0x9c, 0xb4, 0x10, 0xff, 0x61, 0xf2, 0x00, 0x15, 0xad},
420426
{0xf7, 0x84, 0x6f, 0x55, 0xcf, 0x23, 0xe1, 0x4e, 0xeb, 0xea, 0xb5, 0xb4, 0xe1, 0x55, 0x0c, 0xad, 0x5b, 0x50, 0x9e, 0x33, 0x48, 0xfb, 0xc4, 0xef, 0xa3, 0xa1, 0x41, 0x3d, 0x39, 0x3c, 0xb6, 0x50},
421427
{0xf3, 0x0c, 0xeb, 0x2b, 0xb2, 0x82, 0x9e, 0x79, 0xe4, 0xca, 0x97, 0x53, 0xd3, 0x5a, 0x8e, 0xcc, 0x00, 0x26, 0x2d, 0x16, 0x4c, 0xc0, 0x77, 0x08, 0x02, 0x95, 0x38, 0x1c, 0xbd, 0x64, 0x3f, 0x0d},
422428
{0x68, 0x19, 0xd9, 0x15, 0xc7, 0x3f, 0x4d, 0x1e, 0x77, 0xe4, 0xe1, 0xb5, 0x2d, 0x1f, 0xa0, 0xf9, 0xcf, 0x9b, 0xea, 0xea, 0xd3, 0x93, 0x9f, 0x15, 0x87, 0x4b, 0xd9, 0x88, 0xe2, 0xa2, 0x36, 0x30},
423429
{0x24, 0x8d, 0x6a, 0x61, 0xd2, 0x06, 0x38, 0xb8, 0xe5, 0xc0, 0x26, 0x93, 0x0c, 0x3e, 0x60, 0x39, 0xa3, 0x3c, 0xe4, 0x59, 0x64, 0xff, 0x21, 0x67, 0xf6, 0xec, 0xed, 0xd4, 0x19, 0xdb, 0x06, 0xc1},
424430
{0xf0, 0x8a, 0x78, 0xcb, 0xba, 0xee, 0x08, 0x2b, 0x05, 0x2a, 0xe0, 0x70, 0x8f, 0x32, 0xfa, 0x1e, 0x50, 0xc5, 0xc4, 0x21, 0xaa, 0x77, 0x2b, 0xa5, 0xdb, 0xb4, 0x06, 0xa2, 0xea, 0x6b, 0xe3, 0x42},
425-
{0xab, 0x64, 0xef, 0xf7, 0xe8, 0x8e, 0x2e, 0x46, 0x16, 0x5e, 0x29, 0xf2, 0xbc, 0xe4, 0x18, 0x26, 0xbd, 0x4c, 0x7b, 0x35, 0x52, 0xf6, 0xb3, 0x82, 0xa9, 0xe7, 0xd3, 0xaf, 0x47, 0xc2, 0x45, 0xf8}
431+
{0xab, 0x64, 0xef, 0xf7, 0xe8, 0x8e, 0x2e, 0x46, 0x16, 0x5e, 0x29, 0xf2, 0xbc, 0xe4, 0x18, 0x26, 0xbd, 0x4c, 0x7b, 0x35, 0x52, 0xf6, 0xb3, 0x82, 0xa9, 0xe7, 0xd3, 0xaf, 0x47, 0xc2, 0x45, 0xf8},
432+
{0xcd, 0xc7, 0x6e, 0x5c, 0x99, 0x14, 0xfb, 0x92, 0x81, 0xa1, 0xc7, 0xe2, 0x84, 0xd7, 0x3e, 0x67, 0xf1, 0x80, 0x9a, 0x48, 0xa4, 0x97, 0x20, 0x0e, 0x04, 0x6d, 0x39, 0xcc, 0xc7, 0x11, 0x2c, 0xd0},
433+
{0x50, 0xe7, 0x2a, 0x0e, 0x26, 0x44, 0x2f, 0xe2, 0x55, 0x2d, 0xc3, 0x93, 0x8a, 0xc5, 0x86, 0x58, 0x22, 0x8c, 0x0c, 0xbf ,0xb1, 0xd2, 0xca, 0x87, 0x2a, 0xe4, 0x35, 0x26, 0x6f, 0xcd, 0x05, 0x5e}
426434
};
427-
int i;
428-
for (i = 0; i < 8; i++) {
435+
unsigned int i;
436+
for (i = 0; i < sizeof(inputs)/sizeof(inputs[0]); i++) {
429437
unsigned char out[32];
430438
secp256k1_sha256 hasher;
431-
secp256k1_sha256_initialize(&hasher);
432-
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), strlen(inputs[i]));
433-
secp256k1_sha256_finalize(&hasher, out);
434-
CHECK(memcmp(out, outputs[i], 32) == 0);
439+
unsigned int j;
440+
/* Skip the simple test (and rely only on the "split" test below) for long inputs to save time. */
441+
if (repeat[i] <= 1000000) {
442+
j = repeat[i];
443+
secp256k1_sha256_initialize(&hasher);
444+
while (j > 0) {
445+
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), strlen(inputs[i]));
446+
j--;
447+
}
448+
secp256k1_sha256_finalize(&hasher, out);
449+
CHECK(memcmp(out, outputs[i], 32) == 0);
450+
}
435451
if (strlen(inputs[i]) > 0) {
436452
int split = secp256k1_rand_int(strlen(inputs[i]));
437453
secp256k1_sha256_initialize(&hasher);
438-
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), split);
439-
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i] + split), strlen(inputs[i]) - split);
454+
j = repeat[i];
455+
while (j > 0) {
456+
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i]), split);
457+
secp256k1_sha256_write(&hasher, (const unsigned char*)(inputs[i] + split), strlen(inputs[i]) - split);
458+
j--;
459+
}
440460
secp256k1_sha256_finalize(&hasher, out);
441461
CHECK(memcmp(out, outputs[i], 32) == 0);
442462
}

0 commit comments

Comments
 (0)