Skip to content

Commit

Permalink
fixup windows
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Jun 10, 2024
1 parent f6fb486 commit 7829dd1
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 57 deletions.
43 changes: 22 additions & 21 deletions libcrux-ml-kem/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,22 @@ project(libcrux-ml-kem
)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)

# FIXME: Windows?
add_compile_options(
-Wall

# -Wextra
# -pedantic
# -Wconversion
# -Wsign-conversion
$<$<CONFIG:DEBUG>:-g>
$<$<CONFIG:DEBUG>:-Og>
$<$<CONFIG:RELEASE>:-g>
$<$<CONFIG:RELEASE>:-O3>
)
if(NOT MSVC)
# TODO: Clean up
add_compile_options(
-Wall
# -Wextra
# -pedantic
# -Wconversion
# -Wsign-conversion
$<$<CONFIG:DEBUG>:-g>
$<$<CONFIG:DEBUG>:-Og>
$<$<CONFIG:RELEASE>:-g>
$<$<CONFIG:RELEASE>:-O3>
)
endif(NOT MSVC)

set(CMAKE_COLOR_DIAGNOSTICS "ON")
include_directories(
Expand Down Expand Up @@ -78,14 +79,16 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
add_library(ml_kem_vec256 OBJECT ${SOURCES_vec256})
target_sources(ml_kem_static PRIVATE $<TARGET_OBJECTS:ml_kem_vec256>)
target_sources(ml_kem PRIVATE $<TARGET_OBJECTS:ml_kem_vec256>)
target_compile_options(ml_kem_vec256 PRIVATE
-mavx
-mavx2
)
if(NOT MSVC)
target_compile_options(ml_kem_vec256 PRIVATE
-mavx
-mavx2
)
endif(NOT MSVC)
endif()

# This is only for local testing and we assume neon on arm64.
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8")
if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8" AND DEFINED $ENV{LIBCRUX_NEON})
message(STATUS "Detected an arm64 architecture")
add_compile_definitions(LIBCRUX_AARCH64)

Expand Down Expand Up @@ -179,5 +182,3 @@ target_link_libraries(sha3_bench PRIVATE
ml_kem_static
benchmark::benchmark
)
target_compile_definitions(sha3_bench PUBLIC HACL_CAN_COMPILE_VEC256)
target_compile_options(sha3_bench PRIVATE)
56 changes: 55 additions & 1 deletion libcrux-ml-kem/c/benches/mlkem768.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <benchmark/benchmark.h>

#include "libcrux_sha3.h"
#include "libcrux_mlkem768.h"
#include "libcrux_mlkem768_portable.h"
#include "internal/libcrux_core.h"
Expand Down Expand Up @@ -125,4 +124,59 @@ BENCHMARK(kyber768_encapsulation_neon);
BENCHMARK(kyber768_decapsulation_neon);
#endif

#ifdef LIBCRUX_X64
#include "libcrux_mlkem768_avx2.h"

static void
kyber768_key_generation_avx2(benchmark::State &state)
{
uint8_t randomness[64];
generate_random(randomness, 64);
auto key_pair = libcrux_ml_kem_mlkem768_avx2_generate_key_pair(randomness);

for (auto _ : state)
{
key_pair = libcrux_ml_kem_mlkem768_avx2_generate_key_pair(randomness);
}
}

static void
kyber768_encapsulation_avx2(benchmark::State &state)
{
uint8_t randomness[64];
generate_random(randomness, 64);

auto key_pair = libcrux_ml_kem_mlkem768_avx2_generate_key_pair(randomness);
generate_random(randomness, 32);
auto ctxt = libcrux_ml_kem_mlkem768_avx2_encapsulate(&key_pair.pk, randomness);

for (auto _ : state)
{
ctxt = libcrux_ml_kem_mlkem768_avx2_encapsulate(&key_pair.pk, randomness);
}
}

static void
kyber768_decapsulation_avx2(benchmark::State &state)
{
uint8_t randomness[64];
generate_random(randomness, 64);

auto key_pair = libcrux_ml_kem_mlkem768_avx2_generate_key_pair(randomness);
generate_random(randomness, 32);
auto ctxt = libcrux_ml_kem_mlkem768_avx2_encapsulate(&key_pair.pk, randomness);

uint8_t sharedSecret2[LIBCRUX_ML_KEM_CONSTANTS_SHARED_SECRET_SIZE];

for (auto _ : state)
{
libcrux_ml_kem_mlkem768_avx2_decapsulate(&key_pair.sk, &ctxt.fst, sharedSecret2);
}
}

BENCHMARK(kyber768_key_generation_avx2);
BENCHMARK(kyber768_encapsulation_avx2);
BENCHMARK(kyber768_decapsulation_avx2);
#endif

BENCHMARK_MAIN();
1 change: 0 additions & 1 deletion libcrux-ml-kem/c/benches/mlkem768_encaps.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <benchmark/benchmark.h>

#include "libcrux_sha3.h"
#include "libcrux_mlkem768.h"
#include "libcrux_mlkem768_portable.h"
#include "internal/libcrux_core.h"
Expand Down
1 change: 0 additions & 1 deletion libcrux-ml-kem/c/benches/mlkem768_keygen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

#include <benchmark/benchmark.h>

#include "libcrux_sha3.h"
#include "libcrux_mlkem768.h"
#include "libcrux_mlkem768_portable.h"
#include "internal/libcrux_core.h"
Expand Down
20 changes: 13 additions & 7 deletions libcrux-ml-kem/c/eurydice_glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ extern "C" {

// SLICES, ARRAYS, ETC.

#if defined(__cplusplus)
#define CLITERAL(type) type
#else
#define CLITERAL(type) (type)
#endif

// We represent a slice as a pair of an (untyped) pointer, along with the length
// of the slice, i.e. the number of elements in the slice (this is NOT the
// number of bytes). This design choice has two important consequences.
Expand All @@ -41,7 +47,7 @@ typedef struct {
// cast to something that can decay (see remark above about how pointer
// arithmetic works in C), meaning either pointer or array type.
#define EURYDICE_SLICE(x, start, end) \
((Eurydice_slice){.ptr = (void *)(x + start), .len = end - start})
(CLITERAL(Eurydice_slice){.ptr = (void *)(x + start), .len = end - start})
#define EURYDICE_SLICE_LEN(s, _) s.len
// This macro is a pain because in case the dereferenced element type is an
// array, you cannot simply write `t x` as it would yield `int[4] x` instead,
Expand Down Expand Up @@ -83,10 +89,10 @@ typedef struct {
Eurydice_array_eq

#define core_slice___Slice_T___split_at(slice, mid, element_type, ret_t) \
((ret_t){.fst = EURYDICE_SLICE((element_type *)slice.ptr, 0, mid), \
(CLITERAL(ret_t){.fst = EURYDICE_SLICE((element_type *)slice.ptr, 0, mid), \
.snd = EURYDICE_SLICE((element_type *)slice.ptr, mid, slice.len)})
#define core_slice___Slice_T___split_at_mut(slice, mid, element_type, ret_t) \
((ret_t){.fst = {.ptr = slice.ptr, .len = mid}, \
(CLITERAL(ret_t){.fst = {.ptr = slice.ptr, .len = mid}, \
.snd = {.ptr = (char *)slice.ptr + mid * sizeof(element_type), \
.len = slice.len - mid}})

Expand Down Expand Up @@ -160,8 +166,8 @@ static inline uint8_t Eurydice_shr_pv_u8(uint8_t *p, int32_t v) {
#define core_num_nonzero_NonZeroUsize size_t
#define Eurydice_range_iter_next(iter_ptr, t, ret_t) \
(((iter_ptr)->start == (iter_ptr)->end) \
? ((ret_t){.tag = core_option_None}) \
: ((ret_t){.tag = core_option_Some, .f0 = (iter_ptr)->start++}))
? (CLITERAL(ret_t){.tag = core_option_None}) \
: (CLITERAL(ret_t){.tag = core_option_Some, .f0 = (iter_ptr)->start++}))

// Old name (TODO: remove once everyone has upgraded to the latest Charon)
#define core_iter_range___core__iter__traits__iterator__Iterator_for_core__ops__range__Range_A___3__next \
Expand Down Expand Up @@ -225,8 +231,8 @@ typedef struct {
#define core_slice_iter__core__slice__iter__Iter__a__T__181__next(iter, t, \
ret_t) \
(((iter)->index == (iter)->s.len) \
? ((ret_t){.tag = core_option_None}) \
: ((ret_t){.tag = core_option_Some, \
? (CLITERAL(ret_t){.tag = core_option_None}) \
: (CLITERAL(ret_t){.tag = core_option_Some, \
.f0 = ((iter)->index++, \
&((t *)((iter)->s.ptr))[(iter)->index - 1])}))

Expand Down
3 changes: 0 additions & 3 deletions libcrux-ml-kem/c/internal/libcrux_core.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ extern "C" {
#include "../libcrux_core.h"
#include "eurydice_glue.h"

extern void core_fmt_rt__core__fmt__rt__Argument__a__1__none(
core_fmt_rt_Argument x0[0U]);

extern core_fmt_Arguments core_fmt__core__fmt__Arguments__a__2__new_v1(
Eurydice_slice x0, Eurydice_slice x1);

Expand Down
Loading

0 comments on commit 7829dd1

Please sign in to comment.