diff --git a/benchmarks/sha3.cc b/benchmarks/sha3.cc index 63af8ef2..920605e4 100644 --- a/benchmarks/sha3.cc +++ b/benchmarks/sha3.cc @@ -47,7 +47,8 @@ static void Hacl_Sha3_224(benchmark::State& state) { for (auto _ : state) { - Hacl_Hash_SHA3_sha3_224(digest224_0.data(), (uint8_t*)input.data(), input.size()); + Hacl_Hash_SHA3_sha3_224( + digest224_0.data(), (uint8_t*)input.data(), input.size()); } if (digest224_0 != expected_digest_sha3_224) { state.SkipWithError("Incorrect digest."); @@ -61,7 +62,8 @@ static void Hacl_Scalar_Sha3_224(benchmark::State& state) { for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_224(input.size(), (uint8_t*)input.data(), digest224_0.data()); + Hacl_SHA3_Scalar_sha3_224( + input.size(), (uint8_t*)input.data(), digest224_0.data()); } if (digest224_0 != expected_digest_sha3_224) { state.SkipWithError("Incorrect digest."); @@ -77,14 +79,14 @@ Hacl_Vec256_Sha3_224(benchmark::State& state) { for (auto _ : state) { Hacl_SHA3_Vec256_sha3_224_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest224_0.data(), - digest224_1.data(), - digest224_2.data(), - digest224_3.data()); + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + digest224_0.data(), + digest224_1.data(), + digest224_2.data(), + digest224_3.data()); } if (digest224_0 != expected_digest_sha3_224 || digest224_1 != expected_digest_sha3_224 || @@ -112,7 +114,8 @@ static void Hacl_Sha3_256(benchmark::State& state) { for (auto _ : state) { - Hacl_Hash_SHA3_sha3_256(digest256_0.data(), (uint8_t*)input.data(), input.size()); + Hacl_Hash_SHA3_sha3_256( + digest256_0.data(), (uint8_t*)input.data(), input.size()); } if (digest256_0 != expected_digest_sha3_256) { state.SkipWithError("Incorrect digest."); @@ -126,7 +129,8 @@ static void Hacl_Scalar_Sha3_256(benchmark::State& state) { for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_256(input.size(), (uint8_t*)input.data(), digest256_0.data()); + Hacl_SHA3_Scalar_sha3_256( + input.size(), (uint8_t*)input.data(), digest256_0.data()); } if (digest256_0 != expected_digest_sha3_256) { state.SkipWithError("Incorrect digest."); @@ -142,14 +146,14 @@ Hacl_Vec256_Sha3_256(benchmark::State& state) { for (auto _ : state) { Hacl_SHA3_Vec256_sha3_256_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest256_0.data(), - digest256_1.data(), - digest256_2.data(), - digest256_3.data()); + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + digest256_0.data(), + digest256_1.data(), + digest256_2.data(), + digest256_3.data()); } if (digest256_0 != expected_digest_sha3_256 || digest256_1 != expected_digest_sha3_256 || @@ -204,7 +208,8 @@ static void Hacl_Sha3_384(benchmark::State& state) { for (auto _ : state) { - Hacl_Hash_SHA3_sha3_384(digest384_0.data(), (uint8_t*)input.data(), input.size()); + Hacl_Hash_SHA3_sha3_384( + digest384_0.data(), (uint8_t*)input.data(), input.size()); } if (digest384_0 != expected_digest_sha3_384) { state.SkipWithError("Incorrect digest."); @@ -218,7 +223,8 @@ static void Hacl_Scalar_Sha3_384(benchmark::State& state) { for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_384(input.size(), (uint8_t*)input.data(), digest384_0.data()); + Hacl_SHA3_Scalar_sha3_384( + input.size(), (uint8_t*)input.data(), digest384_0.data()); } if (digest384_0 != expected_digest_sha3_384) { state.SkipWithError("Incorrect digest."); @@ -234,14 +240,14 @@ Hacl_Vec384_Sha3_384(benchmark::State& state) { for (auto _ : state) { Hacl_SHA3_Vec256_sha3_384_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest384_0.data(), - digest384_1.data(), - digest384_2.data(), - digest384_3.data()); + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + digest384_0.data(), + digest384_1.data(), + digest384_2.data(), + digest384_3.data()); } if (digest384_0 != expected_digest_sha3_384 || digest384_1 != expected_digest_sha3_384 || @@ -269,7 +275,8 @@ static void Hacl_Sha3_512(benchmark::State& state) { for (auto _ : state) { - Hacl_Hash_SHA3_sha3_512(digest512_0.data(), (uint8_t*)input.data(), input.size()); + Hacl_Hash_SHA3_sha3_512( + digest512_0.data(), (uint8_t*)input.data(), input.size()); } if (digest512_0 != expected_digest_sha3_512) { state.SkipWithError("Incorrect digest."); @@ -283,7 +290,8 @@ static void Hacl_Scalar_Sha3_512(benchmark::State& state) { for (auto _ : state) { - Hacl_SHA3_Scalar_sha3_512(input.size(), (uint8_t*)input.data(), digest512_0.data()); + Hacl_SHA3_Scalar_sha3_512( + input.size(), (uint8_t*)input.data(), digest512_0.data()); } if (digest512_0 != expected_digest_sha3_512) { state.SkipWithError("Incorrect digest."); @@ -299,14 +307,14 @@ Hacl_Vec512_Sha3_512(benchmark::State& state) { for (auto _ : state) { Hacl_SHA3_Vec256_sha3_512_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest512_0.data(), - digest512_1.data(), - digest512_2.data(), - digest512_3.data()); + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + digest512_0.data(), + digest512_1.data(), + digest512_2.data(), + digest512_3.data()); } if (digest512_0 != expected_digest_sha3_512 || digest512_1 != expected_digest_sha3_512 || @@ -426,8 +434,10 @@ static void Hacl_Sha3_shake128(benchmark::State& state) { for (auto _ : state) { - Hacl_Hash_SHA3_shake128_hacl( - input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); + Hacl_Hash_SHA3_shake128_hacl(input.size(), + (uint8_t*)input.data(), + digest_shake_0.size(), + digest_shake_0.data()); } } @@ -437,7 +447,10 @@ static void Hacl_Scalar_Sha3_shake128(benchmark::State& state) { for (auto _ : state) { - Hacl_SHA3_Scalar_shake128_hacl(input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); + Hacl_SHA3_Scalar_shake128_hacl(input.size(), + (uint8_t*)input.data(), + digest_shake_0.size(), + digest_shake_0.data()); } } @@ -449,15 +462,15 @@ Hacl_Vec256_Sha3_shake128(benchmark::State& state) { for (auto _ : state) { Hacl_SHA3_Vec256_shake128_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest_shake_0.size(), - digest_shake_0.data(), - digest_shake_1.data(), - digest_shake_2.data(), - digest_shake_3.data()); + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + digest_shake_0.size(), + digest_shake_0.data(), + digest_shake_1.data(), + digest_shake_2.data(), + digest_shake_3.data()); } } @@ -468,8 +481,10 @@ static void Hacl_Sha3_shake256(benchmark::State& state) { for (auto _ : state) { - Hacl_Hash_SHA3_shake256_hacl( - input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); + Hacl_Hash_SHA3_shake256_hacl(input.size(), + (uint8_t*)input.data(), + digest_shake_0.size(), + digest_shake_0.data()); } } @@ -479,7 +494,10 @@ static void Hacl_Scalar_Sha3_shake256(benchmark::State& state) { for (auto _ : state) { - Hacl_SHA3_Scalar_shake256_hacl(input.size(), (uint8_t*)input.data(), digest_shake_0.size(), digest_shake_0.data()); + Hacl_SHA3_Scalar_shake256_hacl(input.size(), + (uint8_t*)input.data(), + digest_shake_0.size(), + digest_shake_0.data()); } } @@ -491,15 +509,15 @@ Hacl_Vec256_Sha3_shake256(benchmark::State& state) { for (auto _ : state) { Hacl_SHA3_Vec256_shake256_vec256(input.size(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - (uint8_t*)input.data(), - digest_shake_0.size(), - digest_shake_0.data(), - digest_shake_1.data(), - digest_shake_2.data(), - digest_shake_3.data()); + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + (uint8_t*)input.data(), + digest_shake_0.size(), + digest_shake_0.data(), + digest_shake_1.data(), + digest_shake_2.data(), + digest_shake_3.data()); } } diff --git a/tests/sha3.cc b/tests/sha3.cc index 71304ab1..c3534f79 100644 --- a/tests/sha3.cc +++ b/tests/sha3.cc @@ -217,8 +217,7 @@ TEST(ApiSuite, ApiTest) #ifdef HACL_CAN_COMPILE_VEC256 hacl_init_cpu_features(); - if (hacl_vec256_support()) - { + if (hacl_vec256_support()) { // Documentation. // Lines after START and before END are used in documentation. { @@ -234,8 +233,14 @@ TEST(ApiSuite, ApiTest) uint8_t digest3[HACL_HASH_SHA3_256_DIGEST_LENGTH]; Hacl_SHA3_Vec256_sha3_256_vec256(message_size, - (uint8_t*)message, (uint8_t*)message, (uint8_t*)message, (uint8_t*)message, - digest0, digest1, digest2, digest3); + (uint8_t*)message, + (uint8_t*)message, + (uint8_t*)message, + (uint8_t*)message, + digest0, + digest1, + digest2, + digest3); // ANCHOR_END(example vec256_sha3_256) bytes expected_digest = from_hex( @@ -279,31 +284,42 @@ TEST(ApiSuite, ApiTest) uint8_t digest3[42]; Hacl_SHA3_Vec256_shake128_vec256(message_size, - (uint8_t*)message0, (uint8_t*)message1, (uint8_t*)message2, (uint8_t*)message3, - digest_size, digest0, digest1, digest2, digest3); + (uint8_t*)message0, + (uint8_t*)message1, + (uint8_t*)message2, + (uint8_t*)message3, + digest_size, + digest0, + digest1, + digest2, + digest3); // ANCHOR_END(example vec256_shake128) - bytes expected_digest0 = - from_hex("1b82c3db6cb958a09a7ea3dd82b67a9c994422c39616ec373afafcf2fca8bca" - "808881328f9ca03eb119a"); - bytes expected_digest1 = - from_hex("3c8f0ab13109dff341fbe0e7511bd8bdfa8d13335b36acdb391170017c6d45f" - "460964cab081699f6e45d"); - bytes expected_digest2 = - from_hex("86ee9003051369f1d5461b00263e01cac1c65defaf722e6ed648fba99743a14" - "9b39abc52d6fc746f5014"); - bytes expected_digest3 = - from_hex("0b9efd21050944cb5ba5df0cc35a176100201e3fd7c4f2b9f70a9dfd4a7228b" - "5d676451df013d3e22ac9"); + bytes expected_digest0 = from_hex( + "1b82c3db6cb958a09a7ea3dd82b67a9c994422c39616ec373afafcf2fca8bca" + "808881328f9ca03eb119a"); + bytes expected_digest1 = from_hex( + "3c8f0ab13109dff341fbe0e7511bd8bdfa8d13335b36acdb391170017c6d45f" + "460964cab081699f6e45d"); + bytes expected_digest2 = from_hex( + "86ee9003051369f1d5461b00263e01cac1c65defaf722e6ed648fba99743a14" + "9b39abc52d6fc746f5014"); + bytes expected_digest3 = from_hex( + "0b9efd21050944cb5ba5df0cc35a176100201e3fd7c4f2b9f70a9dfd4a7228b" + "5d676451df013d3e22ac9"); EXPECT_EQ( - strncmp((char*)digest0, (char*)expected_digest0.data(), digest_size), 0); + strncmp((char*)digest0, (char*)expected_digest0.data(), digest_size), + 0); EXPECT_EQ( - strncmp((char*)digest1, (char*)expected_digest1.data(), digest_size), 0); + strncmp((char*)digest1, (char*)expected_digest1.data(), digest_size), + 0); EXPECT_EQ( - strncmp((char*)digest2, (char*)expected_digest2.data(), digest_size), 0); + strncmp((char*)digest2, (char*)expected_digest2.data(), digest_size), + 0); EXPECT_EQ( - strncmp((char*)digest3, (char*)expected_digest3.data(), digest_size), 0); + strncmp((char*)digest3, (char*)expected_digest3.data(), digest_size), + 0); } } #endif @@ -358,42 +374,61 @@ TEST_P(Sha3KAT, TryKAT) #ifdef HACL_CAN_COMPILE_VEC256 hacl_init_cpu_features(); - if (hacl_vec256_support()) - { + if (hacl_vec256_support()) { bytes digest0(test_case.md.size(), 0); bytes digest1(test_case.md.size(), 0); bytes digest2(test_case.md.size(), 0); bytes digest3(test_case.md.size(), 0); if (test_case.md.size() == 224 / 8) { - Hacl_SHA3_Vec256_sha3_224_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); + Hacl_SHA3_Vec256_sha3_224_vec256(test_case.msg.size(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + digest0.data(), + digest1.data(), + digest2.data(), + digest3.data()); } else if (test_case.md.size() == 256 / 8) { - Hacl_SHA3_Vec256_sha3_256_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); + Hacl_SHA3_Vec256_sha3_256_vec256(test_case.msg.size(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + digest0.data(), + digest1.data(), + digest2.data(), + digest3.data()); } else if (test_case.md.size() == 384 / 8) { - Hacl_SHA3_Vec256_sha3_384_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); + Hacl_SHA3_Vec256_sha3_384_vec256(test_case.msg.size(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + digest0.data(), + digest1.data(), + digest2.data(), + digest3.data()); } else if (test_case.md.size() == 512 / 8) { - Hacl_SHA3_Vec256_sha3_512_vec256( - test_case.msg.size(), - test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), test_case.msg.data(), - digest0.data(), digest1.data(), digest2.data(), digest3.data()); + Hacl_SHA3_Vec256_sha3_512_vec256(test_case.msg.size(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + digest0.data(), + digest1.data(), + digest2.data(), + digest3.data()); } EXPECT_EQ(test_case.md, digest0) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest0) << std::endl; + << bytes_to_hex(digest0) << std::endl; EXPECT_EQ(test_case.md, digest1) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest1) << std::endl; + << bytes_to_hex(digest1) << std::endl; EXPECT_EQ(test_case.md, digest2) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest2) << std::endl; + << bytes_to_hex(digest2) << std::endl; EXPECT_EQ(test_case.md, digest3) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest3) << std::endl; + << bytes_to_hex(digest3) << std::endl; } #endif } @@ -409,16 +444,20 @@ TEST_P(ShakeKAT, TryKAT) if (test_case.md.size() == 128 / 8) { bytes digest(test_case.md.size(), 128 / 8); - Hacl_Hash_SHA3_shake128_hacl( - test_case.msg.size(), test_case.msg.data(), digest.size(), digest.data()); + Hacl_Hash_SHA3_shake128_hacl(test_case.msg.size(), + test_case.msg.data(), + digest.size(), + digest.data()); EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl << bytes_to_hex(digest) << std::endl; } else if (test_case.md.size() == 256 / 8) { bytes digest(test_case.md.size(), 256 / 8); - Hacl_Hash_SHA3_shake256_hacl( - test_case.msg.size(), test_case.msg.data(), digest.size(), digest.data()); + Hacl_Hash_SHA3_shake256_hacl(test_case.msg.size(), + test_case.msg.data(), + digest.size(), + digest.data()); EXPECT_EQ(test_case.md, digest) << bytes_to_hex(test_case.md) << std::endl << bytes_to_hex(digest) << std::endl; @@ -451,8 +490,7 @@ TEST_P(ShakeKAT, TryKAT) #ifdef HACL_CAN_COMPILE_VEC256 hacl_init_cpu_features(); - if (hacl_vec256_support()) - { + if (hacl_vec256_support()) { if (test_case.md.size() == 128 / 8) { bytes digest0(test_case.md.size(), 128 / 8); bytes digest1(test_case.md.size(), 128 / 8); @@ -460,24 +498,28 @@ TEST_P(ShakeKAT, TryKAT) bytes digest3(test_case.md.size(), 128 / 8); Hacl_SHA3_Vec256_shake128_vec256(test_case.msg.size(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - digest0.size(), - digest0.data(), - digest1.data(), - digest2.data(), - digest3.data()); - - EXPECT_EQ(test_case.md, digest0) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest0) << std::endl; - EXPECT_EQ(test_case.md, digest1) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest1) << std::endl; - EXPECT_EQ(test_case.md, digest2) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest2) << std::endl; - EXPECT_EQ(test_case.md, digest3) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest3) << std::endl; + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + digest0.size(), + digest0.data(), + digest1.data(), + digest2.data(), + digest3.data()); + + EXPECT_EQ(test_case.md, digest0) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest0) << std::endl; + EXPECT_EQ(test_case.md, digest1) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest1) << std::endl; + EXPECT_EQ(test_case.md, digest2) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest2) << std::endl; + EXPECT_EQ(test_case.md, digest3) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest3) << std::endl; } else if (test_case.md.size() == 256 / 8) { bytes digest0(test_case.md.size(), 256 / 8); bytes digest1(test_case.md.size(), 256 / 8); @@ -485,24 +527,28 @@ TEST_P(ShakeKAT, TryKAT) bytes digest3(test_case.md.size(), 256 / 8); Hacl_SHA3_Vec256_shake256_vec256(test_case.msg.size(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - test_case.msg.data(), - digest0.size(), - digest0.data(), - digest1.data(), - digest2.data(), - digest3.data()); - - EXPECT_EQ(test_case.md, digest0) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest0) << std::endl; - EXPECT_EQ(test_case.md, digest1) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest1) << std::endl; - EXPECT_EQ(test_case.md, digest2) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest2) << std::endl; - EXPECT_EQ(test_case.md, digest3) << bytes_to_hex(test_case.md) << std::endl - << bytes_to_hex(digest3) << std::endl; + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + test_case.msg.data(), + digest0.size(), + digest0.data(), + digest1.data(), + digest2.data(), + digest3.data()); + + EXPECT_EQ(test_case.md, digest0) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest0) << std::endl; + EXPECT_EQ(test_case.md, digest1) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest1) << std::endl; + EXPECT_EQ(test_case.md, digest2) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest2) << std::endl; + EXPECT_EQ(test_case.md, digest3) + << bytes_to_hex(test_case.md) << std::endl + << bytes_to_hex(digest3) << std::endl; } } #endif