Skip to content

Commit

Permalink
runtime detection in glue
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Mar 19, 2024
1 parent efbf0ae commit 4f80d73
Showing 1 changed file with 54 additions and 12 deletions.
66 changes: 54 additions & 12 deletions libcrux/src/libcrux_hacl_glue.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
#include "libcrux_hacl_glue.h"
#include "Hacl_Hash_SHA3.h"
#include "libcrux_digest.h"
#include "libcrux_kyber.h"
#include "libcrux_platform.h"
#include "libcrux_digest.h"

#ifdef HACL_CAN_COMPILE_VEC256
#include "EverCrypt_AutoConfig2.h"
#include "Hacl_Hash_SHA3_Simd256.h"
#else
#include "Hacl_Hash_SHA3_Scalar.h"
Expand All @@ -14,6 +15,12 @@ bool
libcrux_platform_simd256_support(void)
{
// TODO: Replace this with HACL platform support.
#ifdef HACL_CAN_COMPILE_VEC256
EverCrypt_AutoConfig2_init();
if (EverCrypt_AutoConfig2_has_avx2()) {
return true;
}
#endif
return false;
}

Expand Down Expand Up @@ -46,10 +53,20 @@ libcrux_digest_incremental_x4__libcrux__digest__incremental_x4__Shake128StateX4_
void)
{
#ifdef HACL_CAN_COMPILE_VEC256
return (libcrux_digest_incremental_x4_Shake128StateX4){
.x4 =
(Lib_IntVector_Intrinsics_vec256*)Hacl_Hash_SHA3_Simd256_state_malloc()
};
if (libcrux_platform_simd256_support()) {
return (libcrux_digest_incremental_x4_Shake128StateX4){
.x4 =
(Lib_IntVector_Intrinsics_vec256*)Hacl_Hash_SHA3_Simd256_state_malloc()
};
} else {
uint64_t* st0 = Hacl_Hash_SHA3_Scalar_state_malloc();
uint64_t* st1 = Hacl_Hash_SHA3_Scalar_state_malloc();
uint64_t* st2 = Hacl_Hash_SHA3_Scalar_state_malloc();
uint64_t* st3 = Hacl_Hash_SHA3_Scalar_state_malloc();
return (libcrux_digest_incremental_x4_Shake128StateX4){
.st0 = st0, .st1 = st1, .st2 = st2, .st3 = st3
};
}
#else
uint64_t* st0 = Hacl_Hash_SHA3_Scalar_state_malloc();
uint64_t* st1 = Hacl_Hash_SHA3_Scalar_state_malloc();
Expand All @@ -69,8 +86,14 @@ libcrux_digest_incremental_x4__libcrux__digest__incremental_x4__Shake128StateX4_
{
(void)k;
#ifdef HACL_CAN_COMPILE_VEC256
Hacl_Hash_SHA3_Simd256_shake128_absorb_final(
x0->x4, x1[0].ptr, x1[1].ptr, x1[2].ptr, x1[0].ptr, x1[0].len);
if (libcrux_platform_simd256_support()) {
Hacl_Hash_SHA3_Simd256_shake128_absorb_final(
x0->x4, x1[0].ptr, x1[1].ptr, x1[2].ptr, x1[0].ptr, x1[0].len);
} else {
Hacl_Hash_SHA3_Scalar_shake128_absorb_final(x0->st0, x1[0].ptr, x1[0].len);
Hacl_Hash_SHA3_Scalar_shake128_absorb_final(x0->st1, x1[1].ptr, x1[1].len);
Hacl_Hash_SHA3_Scalar_shake128_absorb_final(x0->st2, x1[2].ptr, x1[2].len);
}
#else
Hacl_Hash_SHA3_Scalar_shake128_absorb_final(x0->st0, x1[0].ptr, x1[0].len);
Hacl_Hash_SHA3_Scalar_shake128_absorb_final(x0->st1, x1[1].ptr, x1[1].len);
Expand All @@ -85,10 +108,22 @@ libcrux_digest_incremental_x4__libcrux__digest__incremental_x4__Shake128StateX4_
uint8_t* output)
{
#ifdef HACL_CAN_COMPILE_VEC256
uint8_t* tmp = aligned_alloc(32, block_len);
Hacl_Hash_SHA3_Simd256_shake128_squeeze_nblocks(
x1->x4, output, output + block_len, output + 2 * block_len, tmp, block_len);
free(tmp);
if (libcrux_platform_simd256_support()) {
uint8_t* tmp = aligned_alloc(32, block_len);
Hacl_Hash_SHA3_Simd256_shake128_squeeze_nblocks(x1->x4,
output,
output + block_len,
output + 2 * block_len,
tmp,
block_len);
free(tmp);
} else {
Hacl_Hash_SHA3_Scalar_shake128_squeeze_nblocks(x1->st0, output, block_len);
Hacl_Hash_SHA3_Scalar_shake128_squeeze_nblocks(
x1->st1, output + block_len, block_len);
Hacl_Hash_SHA3_Scalar_shake128_squeeze_nblocks(
x1->st2, output + 2 * block_len, block_len);
}
#else
Hacl_Hash_SHA3_Scalar_shake128_squeeze_nblocks(x1->st0, output, block_len);
Hacl_Hash_SHA3_Scalar_shake128_squeeze_nblocks(
Expand All @@ -103,7 +138,14 @@ libcrux_digest_incremental_x4__libcrux__digest__incremental_x4__Shake128StateX4_
libcrux_digest_incremental_x4_Shake128StateX4 x0)
{
#ifdef HACL_CAN_COMPILE_VEC256
Hacl_Hash_SHA3_Simd256_state_free(x0.x4);
if (libcrux_platform_simd256_support()) {
Hacl_Hash_SHA3_Simd256_state_free(x0.x4);
} else {
Hacl_Hash_SHA3_Scalar_state_free(x0.st0);
Hacl_Hash_SHA3_Scalar_state_free(x0.st1);
Hacl_Hash_SHA3_Scalar_state_free(x0.st2);
Hacl_Hash_SHA3_Scalar_state_free(x0.st3);
}
#else
Hacl_Hash_SHA3_Scalar_state_free(x0.st0);
Hacl_Hash_SHA3_Scalar_state_free(x0.st1);
Expand Down

0 comments on commit 4f80d73

Please sign in to comment.