Skip to content

Commit

Permalink
Add libcrux kyber (#442)
Browse files Browse the repository at this point in the history
Co-authored-by: xvzcf <[email protected]>
Co-authored-by: Jonathan Protzenko <[email protected]>
  • Loading branch information
3 people authored Jan 5, 2024
1 parent 34243fb commit fee3dc1
Show file tree
Hide file tree
Showing 20 changed files with 5,927 additions and 47 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,5 @@ rust/.c
.idea
cmake-build-debug

# ctags
tags
39 changes: 36 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,11 @@ if(ENABLE_UBSAN)
add_link_options(-fsanitize=undefined)
endif()

if(ENABLE_MSAN)
add_compile_options(-fsanitize=memory -fsanitize-memory-track-origins)
add_link_options(-fsanitize=memory)
endif()

# Sources are written by mach.py into the following lists
# - SOURCES_std: All regular files
# - SOURCES_vec128: Files that require vec128 hardware
Expand Down Expand Up @@ -335,7 +340,7 @@ configure_file(config/Config.h.in config.h)
# # Dynamic library
add_library(hacl SHARED ${SOURCES_std} ${VALE_OBJECTS})
if(NOT MSVC)
target_compile_options(hacl PRIVATE -Wsign-conversion -Wconversion -Wall -Wextra -pedantic)
target_compile_options(hacl PRIVATE -Wall -Wextra -pedantic)
endif()

if(TOOLCHAIN_CAN_COMPILE_VEC128 AND HACL_VEC128_O)
Expand All @@ -362,7 +367,7 @@ endif()
if(BUILD_LIBCRUX)
add_library(libcrux_static STATIC ${LIBCRUX_SOURCES})
if(NOT MSVC)
target_compile_options(libcrux_static PRIVATE -Wsign-conversion -Wconversion -Wall -Wextra -pedantic)
target_compile_options(libcrux_static PRIVATE -Wall -Wextra -pedantic -Wshadow -Wunused-function)
endif()
endif()

Expand Down Expand Up @@ -511,10 +516,10 @@ if(ENABLE_TESTS)
add_dependencies(${TEST_NAME} hacl hacl_cpu_features)
target_link_libraries(${TEST_NAME} PRIVATE
gtest_main
hacl_static
hacl_cpu_features
nlohmann_json::nlohmann_json
libcrux_static
hacl_static
)

if(EXISTS ${PROJECT_SOURCE_DIR}/tests/${TEST_NAME})
Expand Down Expand Up @@ -596,4 +601,32 @@ if(ENABLE_BENCHMARKS)
benchmark::benchmark
)
endforeach()

if(BUILD_LIBCRUX)
foreach(BENCH_FILE IN LISTS LIBCRUX_BENCHMARK_SOURCES)
get_filename_component(BENCH_NAME ${BENCH_FILE} NAME_WE)
set(BENCH_NAME ${BENCH_NAME}_benchmark)
add_executable(${BENCH_NAME}
${BENCH_FILE}
)

# Use modern C++
if(NOT MSVC)
target_compile_options(${BENCH_NAME} PRIVATE -std=c++17)
else()
# MSVC needs a modern C++ for designated initializers.
target_compile_options(${BENCH_NAME} PRIVATE /std:c++20)
endif(NOT MSVC)

target_compile_definitions(${BENCH_NAME} PUBLIC NO_OPENSSL)

add_dependencies(${BENCH_NAME} hacl hacl_cpu_features)
target_link_libraries(${BENCH_NAME} PRIVATE
hacl_cpu_features
benchmark::benchmark
libcrux_static
hacl_static
)
endforeach()
endif()
endif(ENABLE_BENCHMARKS)
73 changes: 73 additions & 0 deletions benchmarks/kyber.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright 2022 Cryspen Sarl
*
* Licensed under the Apache License, Version 2.0 or MIT.
* - http://www.apache.org/licenses/LICENSE-2.0
* - http://opensource.org/licenses/MIT
*/

#include "Libcrux_Kem_Kyber_Kyber768.h"
#include "util.h"

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

uint8_t public_key[KYBER768_PUBLICKEYBYTES];
uint8_t secret_key[KYBER768_SECRETKEYBYTES];

for (auto _ : state) {
Libcrux_Kyber768_GenerateKeyPair(public_key, secret_key, randomness);
}
}

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

uint8_t public_key[KYBER768_PUBLICKEYBYTES];
uint8_t secret_key[KYBER768_SECRETKEYBYTES];

uint8_t ciphertext[KYBER768_CIPHERTEXTBYTES];
uint8_t sharedSecret[KYBER768_SHAREDSECRETBYTES];

Libcrux_Kyber768_GenerateKeyPair(public_key, secret_key, randomness);

for (auto _ : state) {
Libcrux_Kyber768_Encapsulate(
ciphertext, sharedSecret, &public_key, randomness);
}
}

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

uint8_t public_key[KYBER768_PUBLICKEYBYTES];
uint8_t secret_key[KYBER768_SECRETKEYBYTES];

uint8_t ciphertext[KYBER768_CIPHERTEXTBYTES];
uint8_t sharedSecret[KYBER768_SHAREDSECRETBYTES];

generate_random(randomness, 64);
Libcrux_Kyber768_GenerateKeyPair(public_key, secret_key, randomness);

generate_random(randomness, 32);
Libcrux_Kyber768_Encapsulate(
ciphertext, sharedSecret, &public_key, randomness);

for (auto _ : state) {
Libcrux_Kyber768_Decapsulate(sharedSecret, &ciphertext, &secret_key);
}
}

BENCHMARK(kyber768_key_generation)->Setup(DoSetup);
BENCHMARK(kyber768_encapsulation)->Setup(DoSetup);
BENCHMARK(kyber768_decapsulation)->Setup(DoSetup);

BENCHMARK_MAIN();
17 changes: 16 additions & 1 deletion config/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
"vale/include"
],
"libcrux_include_paths": [
"libcrux/include"
"libcrux/include",
"libcrux/eurydice"
],
"hacl_sources": {
"nacl": [
Expand Down Expand Up @@ -344,6 +345,15 @@
"kyber": [
{
"file": "Libcrux_Kem_Kyber_Kyber768.c"
},
{
"file": "libcrux_kyber.c"
},
{
"file": "libcrux_hacl_glue.c"
},
{
"file": "core.c"
}
]
},
Expand Down Expand Up @@ -570,5 +580,10 @@
"rsapss": [
"rsapss.cc"
]
},
"libcrux_benchmarks": {
"kyber": [
"kyber.cc"
]
}
}
26 changes: 26 additions & 0 deletions libcrux/include/Eurydice.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
This file was generated by KaRaMeL <https://github.com/FStarLang/karamel>
KaRaMeL invocation: /Users/franziskus/repos/eurydice//eurydice ../libcrux_kyber.llbc
F* version: d0aa54cf
KaRaMeL version: 0eda808b
*/

#ifndef __Eurydice_H
#define __Eurydice_H

#include "eurydice_glue.h"

typedef struct core_ops_range_Range__size_t_s
{
size_t start;
size_t end;
}
core_ops_range_Range__size_t;

extern uint8_t Eurydice_bitand_pv_u8(uint8_t *x, uint8_t y);

extern uint8_t Eurydice_shr_pv_u8(uint8_t *x, int32_t y);


#define __Eurydice_H_DEFINED
#endif
21 changes: 11 additions & 10 deletions libcrux/include/Libcrux_Kem_Kyber_Kyber768.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@ extern "C"
#define KYBER768_PUBLICKEYBYTES 1184
#define KYBER768_CIPHERTEXTBYTES 1088
#define KYBER768_SHAREDSECRETBYTES 32
void Libcrux_Kyber768_GenerateKeyPair(uint8_t* pk,
uint8_t* sk,
uint8_t randomness[64]);

int Libcrux_Kyber768_GenerateKeyPair(uint8_t* pk,
uint8_t* sk,
const uint8_t* randomness);
int Libcrux_Kyber768_Encapsulate(uint8_t* ct,
uint8_t* ss,
const uint8_t* pk,
const uint8_t* randomness);
int Libcrux_Kyber768_Decapsulate(uint8_t* ss,
const uint8_t* ct,
const uint8_t* sk);
void Libcrux_Kyber768_Encapsulate(uint8_t* ct,
uint8_t* ss,
uint8_t (*pk)[1184],
uint8_t randomness[32]);

void Libcrux_Kyber768_Decapsulate(uint8_t ss[32U],
uint8_t (*ct)[1088U],
uint8_t (*sk)[2400U]);

#if defined(__cplusplus)
}
Expand Down
Loading

0 comments on commit fee3dc1

Please sign in to comment.