Skip to content

Commit

Permalink
Rename SHA3 Scalar and Simd256 functions (#439)
Browse files Browse the repository at this point in the history
* Rename SHA3 Scalar and Simd256 functions
* Add runtime cpu feature checks in benchmarks/sha3.cc
  • Loading branch information
mamonet authored Dec 6, 2023
1 parent 6b66fae commit 8e7f025
Show file tree
Hide file tree
Showing 14 changed files with 12,809 additions and 109 deletions.
110 changes: 71 additions & 39 deletions benchmarks/sha3.cc
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include "util.h"

#include "Hacl_Hash_SHA3.h"
#include "Hacl_SHA3_Scalar.h"
#include "Hacl_Hash_SHA3_Scalar.h"

#include "util.h"

#ifdef HACL_CAN_COMPILE_VEC256
#include "Hacl_SHA3_Vec256.h"
#include "Hacl_Hash_SHA3_Simd256.h"
#endif

const bytes input(1000, 0x37);
Expand Down Expand Up @@ -59,10 +61,10 @@ Hacl_Sha3_224(benchmark::State& state)
BENCHMARK(Hacl_Sha3_224)->Setup(DoSetup);

static void
Hacl_Scalar_Sha3_224(benchmark::State& state)
Hacl_Sha3_224_Scalar(benchmark::State& state)
{
for (auto _ : state) {
Hacl_SHA3_Scalar_sha3_224(
Hacl_Hash_SHA3_Scalar_sha3_224(
input.size(), (uint8_t*)input.data(), digest224_0.data());
}
if (digest224_0 != expected_digest_sha3_224) {
Expand All @@ -71,14 +73,19 @@ Hacl_Scalar_Sha3_224(benchmark::State& state)
}
}

BENCHMARK(Hacl_Scalar_Sha3_224)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_224_Scalar)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC256
static void
Hacl_Vec256_Sha3_224(benchmark::State& state)
Hacl_Sha3_224_Simd256(benchmark::State& state)
{
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

for (auto _ : state) {
Hacl_SHA3_Vec256_sha3_224_vec256(input.size(),
Hacl_Hash_SHA3_Simd256_sha3_224(input.size(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
Expand All @@ -97,7 +104,7 @@ Hacl_Vec256_Sha3_224(benchmark::State& state)
}
}

BENCHMARK(Hacl_Vec256_Sha3_224)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_224_Simd256)->Setup(DoSetup);
#endif

#ifndef NO_OPENSSL
Expand Down Expand Up @@ -126,10 +133,10 @@ Hacl_Sha3_256(benchmark::State& state)
BENCHMARK(Hacl_Sha3_256)->Setup(DoSetup);

static void
Hacl_Scalar_Sha3_256(benchmark::State& state)
Hacl_Sha3_256_Scalar(benchmark::State& state)
{
for (auto _ : state) {
Hacl_SHA3_Scalar_sha3_256(
Hacl_Hash_SHA3_Scalar_sha3_256(
input.size(), (uint8_t*)input.data(), digest256_0.data());
}
if (digest256_0 != expected_digest_sha3_256) {
Expand All @@ -138,14 +145,19 @@ Hacl_Scalar_Sha3_256(benchmark::State& state)
}
}

BENCHMARK(Hacl_Scalar_Sha3_256)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_256_Scalar)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC256
static void
Hacl_Vec256_Sha3_256(benchmark::State& state)
Hacl_Sha3_256_Simd256(benchmark::State& state)
{
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

for (auto _ : state) {
Hacl_SHA3_Vec256_sha3_256_vec256(input.size(),
Hacl_Hash_SHA3_Simd256_sha3_256(input.size(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
Expand All @@ -164,7 +176,7 @@ Hacl_Vec256_Sha3_256(benchmark::State& state)
}
}

BENCHMARK(Hacl_Vec256_Sha3_256)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_256_Simd256)->Setup(DoSetup);
#endif

#include "sha3.h"
Expand Down Expand Up @@ -220,10 +232,10 @@ Hacl_Sha3_384(benchmark::State& state)
BENCHMARK(Hacl_Sha3_384)->Setup(DoSetup);

static void
Hacl_Scalar_Sha3_384(benchmark::State& state)
Hacl_Sha3_384_Scalar(benchmark::State& state)
{
for (auto _ : state) {
Hacl_SHA3_Scalar_sha3_384(
Hacl_Hash_SHA3_Scalar_sha3_384(
input.size(), (uint8_t*)input.data(), digest384_0.data());
}
if (digest384_0 != expected_digest_sha3_384) {
Expand All @@ -232,14 +244,19 @@ Hacl_Scalar_Sha3_384(benchmark::State& state)
}
}

BENCHMARK(Hacl_Scalar_Sha3_384)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_384_Scalar)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC384
#ifdef HACL_CAN_COMPILE_VEC256
static void
Hacl_Vec384_Sha3_384(benchmark::State& state)
Hacl_Sha3_384_Simd256(benchmark::State& state)
{
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

for (auto _ : state) {
Hacl_SHA3_Vec256_sha3_384_vec256(input.size(),
Hacl_Hash_SHA3_Simd256_sha3_384(input.size(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
Expand All @@ -258,7 +275,7 @@ Hacl_Vec384_Sha3_384(benchmark::State& state)
}
}

BENCHMARK(Hacl_Vec384_Sha3_384)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_384_Simd256)->Setup(DoSetup);
#endif

#ifndef NO_OPENSSL
Expand Down Expand Up @@ -287,10 +304,10 @@ Hacl_Sha3_512(benchmark::State& state)
BENCHMARK(Hacl_Sha3_512)->Setup(DoSetup);

static void
Hacl_Scalar_Sha3_512(benchmark::State& state)
Hacl_Sha3_512_Scalar(benchmark::State& state)
{
for (auto _ : state) {
Hacl_SHA3_Scalar_sha3_512(
Hacl_Hash_SHA3_Scalar_sha3_512(
input.size(), (uint8_t*)input.data(), digest512_0.data());
}
if (digest512_0 != expected_digest_sha3_512) {
Expand All @@ -299,14 +316,19 @@ Hacl_Scalar_Sha3_512(benchmark::State& state)
}
}

BENCHMARK(Hacl_Scalar_Sha3_512)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_512_Scalar)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC256
static void
Hacl_Vec512_Sha3_512(benchmark::State& state)
Hacl_Sha3_512_Simd256(benchmark::State& state)
{
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

for (auto _ : state) {
Hacl_SHA3_Vec256_sha3_512_vec256(input.size(),
Hacl_Hash_SHA3_Simd256_sha3_512(input.size(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
Expand All @@ -325,7 +347,7 @@ Hacl_Vec512_Sha3_512(benchmark::State& state)
}
}

BENCHMARK(Hacl_Vec512_Sha3_512)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_512_Simd256)->Setup(DoSetup);
#endif

static void
Expand Down Expand Up @@ -444,24 +466,29 @@ Hacl_Sha3_shake128(benchmark::State& state)
BENCHMARK(Hacl_Sha3_shake128)->Setup(DoSetup);

static void
Hacl_Scalar_Sha3_shake128(benchmark::State& state)
Hacl_Sha3_shake128_Scalar(benchmark::State& state)
{
for (auto _ : state) {
Hacl_SHA3_Scalar_shake128_hacl(input.size(),
Hacl_Hash_SHA3_Scalar_shake128(input.size(),
(uint8_t*)input.data(),
digest_shake_0.size(),
digest_shake_0.data());
}
}

BENCHMARK(Hacl_Scalar_Sha3_shake128)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_shake128_Scalar)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC256
static void
Hacl_Vec256_Sha3_shake128(benchmark::State& state)
Hacl_Sha3_shake128_Simd256(benchmark::State& state)
{
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

for (auto _ : state) {
Hacl_SHA3_Vec256_shake128_vec256(input.size(),
Hacl_Hash_SHA3_Simd256_shake128(input.size(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
Expand All @@ -474,7 +501,7 @@ Hacl_Vec256_Sha3_shake128(benchmark::State& state)
}
}

BENCHMARK(Hacl_Vec256_Sha3_shake128)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_shake128_Simd256)->Setup(DoSetup);
#endif

static void
Expand All @@ -491,24 +518,29 @@ Hacl_Sha3_shake256(benchmark::State& state)
BENCHMARK(Hacl_Sha3_shake256)->Setup(DoSetup);

static void
Hacl_Scalar_Sha3_shake256(benchmark::State& state)
Hacl_Sha3_shake256_Scalar(benchmark::State& state)
{
for (auto _ : state) {
Hacl_SHA3_Scalar_shake256_hacl(input.size(),
Hacl_Hash_SHA3_Scalar_shake256(input.size(),
(uint8_t*)input.data(),
digest_shake_0.size(),
digest_shake_0.data());
}
}

BENCHMARK(Hacl_Scalar_Sha3_shake256)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_shake256_Scalar)->Setup(DoSetup);

#ifdef HACL_CAN_COMPILE_VEC256
static void
Hacl_Vec256_Sha3_shake256(benchmark::State& state)
Hacl_Sha3_shake256_Simd256(benchmark::State& state)
{
if (!vec256_support()) {
state.SkipWithError("No vec256 support");
return;
}

for (auto _ : state) {
Hacl_SHA3_Vec256_shake256_vec256(input.size(),
Hacl_Hash_SHA3_Simd256_shake256(input.size(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
(uint8_t*)input.data(),
Expand All @@ -521,7 +553,7 @@ Hacl_Vec256_Sha3_shake256(benchmark::State& state)
}
}

BENCHMARK(Hacl_Vec256_Sha3_shake256)->Setup(DoSetup);
BENCHMARK(Hacl_Sha3_shake256_Simd256)->Setup(DoSetup);
#endif

BENCHMARK_MAIN();
4 changes: 2 additions & 2 deletions config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,11 @@
],
"sha3-mb": [
{
"file": "Hacl_SHA3_Scalar.c",
"file": "Hacl_Hash_SHA3_Scalar.c",
"features": "std"
},
{
"file": "Hacl_SHA3_Vec256.c",
"file": "Hacl_Hash_SHA3_Simd256.c",
"features": "vec256"
}
]
Expand Down
14 changes: 7 additions & 7 deletions config/default_config.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ set(SOURCES_std
${PROJECT_SOURCE_DIR}/src/EverCrypt_Chacha20Poly1305.c
${PROJECT_SOURCE_DIR}/src/EverCrypt_Poly1305.c
${PROJECT_SOURCE_DIR}/src/EverCrypt_AEAD.c
${PROJECT_SOURCE_DIR}/src/Hacl_SHA3_Scalar.c
${PROJECT_SOURCE_DIR}/src/Hacl_Hash_SHA3_Scalar.c
)
set(SOURCES_vec256
${PROJECT_SOURCE_DIR}/src/Hacl_Hash_Blake2b_Simd256.c
Expand All @@ -65,7 +65,7 @@ set(SOURCES_vec256
${PROJECT_SOURCE_DIR}/src/Hacl_HPKE_Curve51_CP256_SHA256.c
${PROJECT_SOURCE_DIR}/src/Hacl_HPKE_Curve51_CP256_SHA512.c
${PROJECT_SOURCE_DIR}/src/Hacl_HPKE_P256_CP256_SHA256.c
${PROJECT_SOURCE_DIR}/src/Hacl_SHA3_Vec256.c
${PROJECT_SOURCE_DIR}/src/Hacl_Hash_SHA3_Simd256.c
)
set(SOURCES_vec128
${PROJECT_SOURCE_DIR}/src/Hacl_Hash_Blake2s_Simd128.c
Expand Down Expand Up @@ -241,9 +241,9 @@ set(INCLUDES
${PROJECT_SOURCE_DIR}/include/EverCrypt_Chacha20Poly1305.h
${PROJECT_SOURCE_DIR}/include/EverCrypt_Poly1305.h
${PROJECT_SOURCE_DIR}/include/EverCrypt_AEAD.h
${PROJECT_SOURCE_DIR}/include/internal/Hacl_SHA3_Scalar.h
${PROJECT_SOURCE_DIR}/include/Hacl_SHA3_Scalar.h
${PROJECT_SOURCE_DIR}/include/Hacl_SHA3_Vec256.h
${PROJECT_SOURCE_DIR}/include/internal/Hacl_Hash_SHA3_Scalar.h
${PROJECT_SOURCE_DIR}/include/Hacl_Hash_SHA3_Scalar.h
${PROJECT_SOURCE_DIR}/include/Hacl_Hash_SHA3_Simd256.h
)
set(PUBLIC_INCLUDES
${PROJECT_SOURCE_DIR}/include/Hacl_NaCl.h
Expand Down Expand Up @@ -354,8 +354,8 @@ set(PUBLIC_INCLUDES
${PROJECT_SOURCE_DIR}/include/EverCrypt_Chacha20Poly1305.h
${PROJECT_SOURCE_DIR}/include/EverCrypt_Poly1305.h
${PROJECT_SOURCE_DIR}/include/EverCrypt_AEAD.h
${PROJECT_SOURCE_DIR}/include/Hacl_SHA3_Scalar.h
${PROJECT_SOURCE_DIR}/include/Hacl_SHA3_Vec256.h
${PROJECT_SOURCE_DIR}/include/Hacl_Hash_SHA3_Scalar.h
${PROJECT_SOURCE_DIR}/include/Hacl_Hash_SHA3_Simd256.h
)
set(ALGORITHMS
nacl
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
*/


#ifndef __Hacl_SHA3_Scalar_H
#define __Hacl_SHA3_Scalar_H
#ifndef __Hacl_Hash_SHA3_Scalar_H
#define __Hacl_Hash_SHA3_Scalar_H

#if defined(__cplusplus)
extern "C" {
Expand All @@ -36,32 +36,32 @@ extern "C" {
#include "krml/internal/target.h"

void
Hacl_SHA3_Scalar_shake128_hacl(
Hacl_Hash_SHA3_Scalar_shake128(
uint32_t inputByteLen,
uint8_t *input,
uint32_t outputByteLen,
uint8_t *output
);

void
Hacl_SHA3_Scalar_shake256_hacl(
Hacl_Hash_SHA3_Scalar_shake256(
uint32_t inputByteLen,
uint8_t *input,
uint32_t outputByteLen,
uint8_t *output
);

void Hacl_SHA3_Scalar_sha3_224(uint32_t inputByteLen, uint8_t *input, uint8_t *output);
void Hacl_Hash_SHA3_Scalar_sha3_224(uint32_t inputByteLen, uint8_t *input, uint8_t *output);

void Hacl_SHA3_Scalar_sha3_256(uint32_t inputByteLen, uint8_t *input, uint8_t *output);
void Hacl_Hash_SHA3_Scalar_sha3_256(uint32_t inputByteLen, uint8_t *input, uint8_t *output);

void Hacl_SHA3_Scalar_sha3_384(uint32_t inputByteLen, uint8_t *input, uint8_t *output);
void Hacl_Hash_SHA3_Scalar_sha3_384(uint32_t inputByteLen, uint8_t *input, uint8_t *output);

void Hacl_SHA3_Scalar_sha3_512(uint32_t inputByteLen, uint8_t *input, uint8_t *output);
void Hacl_Hash_SHA3_Scalar_sha3_512(uint32_t inputByteLen, uint8_t *input, uint8_t *output);

#if defined(__cplusplus)
}
#endif

#define __Hacl_SHA3_Scalar_H_DEFINED
#define __Hacl_Hash_SHA3_Scalar_H_DEFINED
#endif
Loading

0 comments on commit 8e7f025

Please sign in to comment.