Skip to content

Commit

Permalink
allow symcrypt benches
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Jun 10, 2024
1 parent 48db367 commit e29b810
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 6 deletions.
14 changes: 8 additions & 6 deletions libcrux-ml-kem/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ 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>)

if(NOT MSVC)
target_compile_options(ml_kem_vec256 PRIVATE
-mavx
Expand Down Expand Up @@ -152,8 +153,13 @@ target_link_libraries(ml_kem_bench PRIVATE
ml_kem_static
benchmark::benchmark
)
target_compile_definitions(ml_kem_bench PUBLIC HACL_CAN_COMPILE_VEC256)
target_compile_options(ml_kem_bench PRIVATE)

if(DEFINED $ENV{SYMCRYPT_PATH})
add_compile_definitions(LIBCRUX_SYMCRYPT)
target_include_directories(ml_kem_bench PRIVATE $ENV{SYMCRYPT_PATH})
target_link_directories(ml_kem_bench PRIVATE $ENV{SYMCRYPT_PATH}/build/module/generic/)
target_link_libraries(ml_kem_bench PRIVATE symcrypt)
endif(DEFINED $ENV{SYMCRYPT_PATH})

add_executable(ml_kem_keygen
${PROJECT_SOURCE_DIR}/benches/mlkem768_keygen.cc
Expand All @@ -162,8 +168,6 @@ target_link_libraries(ml_kem_keygen PRIVATE
ml_kem_static
benchmark::benchmark
)
target_compile_definitions(ml_kem_keygen PUBLIC HACL_CAN_COMPILE_VEC256)
target_compile_options(ml_kem_keygen PRIVATE)

add_executable(ml_kem_encaps
${PROJECT_SOURCE_DIR}/benches/mlkem768_encaps.cc
Expand All @@ -172,8 +176,6 @@ target_link_libraries(ml_kem_encaps PRIVATE
ml_kem_static
benchmark::benchmark
)
target_compile_definitions(ml_kem_encaps PUBLIC HACL_CAN_COMPILE_VEC256)
target_compile_options(ml_kem_encaps PRIVATE)

if(NOT MSVC)
# We benchmark internal functions here that are inlined and thus not available
Expand Down
20 changes: 20 additions & 0 deletions libcrux-ml-kem/c/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,31 @@ the script sets all necessary configuration flags.

## Build

Make sure to use `CC=clang CXX=clang++` when benchmarking on Linux to get full performance.

```bash
cmake -B build -G "Ninja Multi-Config"
cmake --build build
```

### Symcrypt benchmarks

First get and build symcrypt and set `SYMCRYPT_PATH` for the build.
Ensure you have `elftools` installed (`pip install pyelftools`).

```bash
git clone https://github.com/microsoft/symcrypt
cd symcrypt
git checkout b070a5d236a4d40aa90524cb5b492463c5452b40
scripts/build.py cmake build --config Release
```

```bash
SYMCRYPT_PATH=<your-path> CC=clang CXX=clang++ cmake -B build -G Ninja Multi-Config"
cmake --build build --config Release
./build/Release/ml_kem_bench
```
### Test
```bash
Expand Down
65 changes: 65 additions & 0 deletions libcrux-ml-kem/c/benches/mlkem768.cc
Original file line number Diff line number Diff line change
Expand Up @@ -179,4 +179,69 @@ BENCHMARK(kyber768_encapsulation_avx2);
BENCHMARK(kyber768_decapsulation_avx2);
#endif

#ifdef LIBCRUX_SYMCRYPT
#include "inc/symcrypt.h"

static void
symcrypt_kyber768_key_generation(benchmark::State &state)
{
uint8_t randomness[64];
generate_random(randomness, 64);
auto pKey = SymCryptMlKemkeyAllocate(SymCryptMlKemParamsDraft203MlKem768);
SymCryptMlKemkeyGenerate(pKey, 0);

for (auto _ : state)
{
pKey = SymCryptMlKemkeyAllocate(SymCryptMlKemParamsDraft203MlKem768);
SymCryptMlKemkeyGenerate(pKey, 0);
}
}

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

auto pKey = SymCryptMlKemkeyAllocate(SymCryptMlKemParamsDraft203MlKem768);
SymCryptMlKemkeyGenerate(pKey, 0);
generate_random(randomness, 32);

BYTE secret[32];
BYTE cipher[1088];
SymCryptMlKemEncapsulate(pKey, secret, 32, cipher, 1088);

for (auto _ : state)
{
SymCryptMlKemEncapsulate(pKey, secret, 32, cipher, 1088);
}
}

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

auto pKey = SymCryptMlKemkeyAllocate(SymCryptMlKemParamsDraft203MlKem768);
SymCryptMlKemkeyGenerate(pKey, 0);

generate_random(randomness, 32);
BYTE secret[32];
BYTE cipher[1088];
SymCryptMlKemEncapsulate(pKey, secret, 32, cipher, 1088);

BYTE sharedSecret2[32];

for (auto _ : state)
{
SymCryptMlKemDecapsulate(pKey, cipher, 1088, sharedSecret2, 32);
}
}

BENCHMARK(symcrypt_kyber768_key_generation);
BENCHMARK(symcrypt_kyber768_encapsulation);
BENCHMARK(symcrypt_kyber768_decapsulation);
#endif

BENCHMARK_MAIN();

0 comments on commit e29b810

Please sign in to comment.