Skip to content

Commit

Permalink
Merge pull request #333 from cryspen/franziskus/mlkem-variants-features
Browse files Browse the repository at this point in the history
Add features for ml-kem variants
  • Loading branch information
franziskuskiefer authored Jun 26, 2024
2 parents 9302724 + a2c7a1a commit 5611bb3
Show file tree
Hide file tree
Showing 28 changed files with 18,716 additions and 30 deletions.
5 changes: 4 additions & 1 deletion libcrux-ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ libcrux-intrinsics = { version = "0.0.2-pre.2", path = "../libcrux-intrinsics" }
hax-lib = { version = "0.1.0-pre.1", git = "https://github.com/hacspec/hax/" }

[features]
default = ["std"]
default = ["std", "mlkem512", "mlkem768", "mlkem1024"]
simd128 = ["libcrux-sha3/simd128"]
simd256 = ["libcrux-sha3/simd256"]
mlkem512 = []
mlkem768 = []
mlkem1024 = []
std = []
kyber = []

Expand Down
26 changes: 17 additions & 9 deletions libcrux-ml-kem/c.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ portable_only=0
no_hacl=0
no_charon=0
clean=0
config=c.yaml
out=c
glue=$EURYDICE_HOME/include/eurydice_glue.h
features=

# Parse command line arguments.
all_args=("$@")
Expand All @@ -25,6 +29,10 @@ while [ $# -gt 0 ]; do
--no-hacl) no_hacl=1 ;;
--no-charon) no_charon=1 ;;
-c | --clean) clean=1 ;;
--config) config="$2"; shift ;;
--out) out="$2"; shift ;;
--glue) glue="$2"; shift ;;
--mlkem768) features="--cargo-arg=--no-default-features --cargo-arg=--features=mlkem768" ;;
esac
shift
done
Expand All @@ -38,20 +46,20 @@ fi
if [[ "$no_charon" = 0 ]]; then
rm -rf ../libcrux_ml_kem.llbc ../libcrux_sha3.llbc
echo "Running charon (sha3) ..."
(cd ../libcrux-sha3 && RUSTFLAGS="--cfg eurydice" $CHARON_HOME/bin/charon --errors-as-warnings)
(cd ../libcrux-sha3 && RUSTFLAGS="--cfg eurydice" $CHARON_HOME/bin/charon)
if ! [[ -f ../libcrux_sha3.llbc ]]; then
echo "😱😱😱 You are the victim of this bug: https://hacspec.zulipchat.com/#narrow/stream/433829-Circus/topic/charon.20declines.20to.20generate.20an.20llbc.20file"
echo "Suggestion: rm -rf ../target or cargo clean"
exit 1
fi
echo "Running charon (ml-kem) ..."
RUSTFLAGS="--cfg eurydice" $CHARON_HOME/bin/charon --errors-as-warnings
RUSTFLAGS="--cfg eurydice" $CHARON_HOME/bin/charon $features
else
echo "Skipping charon"
fi

mkdir -p c
cd c
mkdir -p $out
cd $out

# Clean only when requesting it.
# Note that we can not extract for all platforms on any platform right now.
Expand All @@ -62,18 +70,18 @@ if [[ "$clean" = 1 ]]; then
fi

echo "Running eurydice ..."
$EURYDICE_HOME/eurydice --config ../c.yaml ../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc
$EURYDICE_HOME/eurydice --config ../$config ../../libcrux_ml_kem.llbc ../../libcrux_sha3.llbc
cp $EURYDICE_HOME/include/eurydice_glue.h .

clang-format --style=Google -i *.c *.h
clang-format --style=Google -i internal/*.h
clang-format --style=Google -i intrinsics/*.h

# Write out infos about the used tools
[ -n "$CHARON_REV" ] || export CHARON_REV=$(git -C $CHARON_HOME rev-parse HEAD)
[ -n "$EURYDICE_REV" ] || export EURYDICE_REV=$(git -C $EURYDICE_HOME rev-parse HEAD)
[ -n "$KRML_REV" ] || export KRML_REV=$(git -C $KRML_HOME rev-parse HEAD)
[ -n "$FSTAR_REV" ] || export FSTAR_REV=$(git -C $FSTAR_HOME rev-parse HEAD)
[[ -z "$CHARON_REV" && -d $CHARON_HOME/.git ]] && export CHARON_REV=$(git -C $CHARON_HOME rev-parse HEAD)
[[ -z "$EURYDICE_REV" && -d $EURYDICE_HOME/.git ]] && export EURYDICE_REV=$(git -C $EURYDICE_HOME rev-parse HEAD)
[[ -z "$KRML_REV" && -d $KRML_HOME/.git ]] && export KRML_REV=$(git -C $KRML_HOME rev-parse HEAD)
[[ -z "$FSTAR_REV" && -d $FSTAR_HOME/.git ]] && export FSTAR_REV=$(git -C $FSTAR_HOME rev-parse HEAD)
rm -f code_gen.txt
echo "This code was generated with the following tools:" >> code_gen.txt
echo -n "Charon: " >> code_gen.txt
Expand Down
155 changes: 155 additions & 0 deletions libcrux-ml-kem/cg.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
files:
# INTRINSICS

- name: libcrux_intrinsics_avx2
library: true
inline_static: true
api:
- [libcrux_intrinsics, avx2]

# SHA3 (no mention of libcrux_mlkem in this section, please)

- name: libcrux_sha3_avx2
inline_static: true
# This is needed solely for the benchmarking test -- otherwise these would
# all be private. Note that the order matters! So we put these first so that
# they match immediately (and get promoted to internal), then the rest of
# the behavior applies.
api:
patterns:
- [libcrux_sha3, avx2, "*"]
- [libcrux_sha3, simd, avx2, "*"]
exact:
- [libcrux_sha3, avx2, x4, incremental, KeccakState4]
monomorphizations_exact:
- [libcrux_sha3, avx2, x4, incremental, KeccakState4]
- [libcrux_sha3, generic_keccak, absorb_final__core_core_arch_x86___m256i_4size_t_168size_t_31uint8_t ]
- [libcrux_sha3, generic_keccak, squeeze_first_three_blocks__core_core_arch_x86___m256i_4size_t_168size_t ]
- [libcrux_sha3, generic_keccak, "KeccakState__core_core_arch_x86___m256i_$4size_t"]
monomorphizations_of:
- [libcrux_sha3, avx2, "*"]
- [libcrux_sha3, simd, avx2, "*"]
monomorphizations_using:
# Should this also include the monomorphizations using
# core.arch.x86.__m256i?
- [libcrux_sha3, avx2, "*"]
- [libcrux_sha3, simd, avx2, "*"]
include_in_h:
- '"intrinsics/libcrux_intrinsics_avx2.h"'

# Portable SHA3
- name: libcrux_sha3_portable
inline_static: true
api:
patterns:
- [libcrux_sha3, "*"]
monomorphizations_of:
- [libcrux_sha3, "*"]
monomorphizations_using:
- [libcrux_sha3, "*"]

# Common parts of SHA3 (this catches stuff that hasn't matched above). Must
# # come after the (more precise) patterns above concerning platform-specific hash_functions
# - name: libcrux_sha3_internal
# api:
# patterns:
# - [libcrux_sha3, "*"]
# monomorphizations_of:
# - [libcrux_sha3, "*"]
# monomorphizations_using:
# - [libcrux_sha3, "*"]
# inline_static: true

# MLKEM: HASH FUNCTIONS (as used by mlkem)

# - name: libcrux_mlkem_avx2
# api:
# - [libcrux_ml_kem, vector, avx2, "*"]
# - [libcrux_ml_kem, hash_functions, avx2, "*"]
# private:
# monomorphizations_using:
# - [libcrux_ml_kem, vector, avx2, "*"]
# - [libcrux_ml_kem, hash_functions, avx2, "*"]
# monomorphizations_of:
# - [libcrux_ml_kem, vector, avx2, "*"]
# - [libcrux_ml_kem, hash_functions, avx2, "*"]

# # This covers slightly more than the two bundles above, but this greatly
# # simplifies our lives.
# - name: libcrux_mlkem_portable
# inline_static: true
# api:
# patterns:
# - [libcrux_ml_kem, vector, "*"]
# - [libcrux_ml_kem, hash_functions, portable, "*"]
# - [ libcrux_ml_kem, polynomial, "*" ]
# monomorphizations_using:
# - [ libcrux_ml_kem, polynomial, "*" ]
# - [libcrux_ml_kem, vector, "*"]
# - [libcrux_ml_kem, hash_functions, portable, "*"]
# monomorphizations_of:
# - [ libcrux_ml_kem, polynomial, "*" ]
# - [libcrux_ml_kem, vector, "*"]
# - [libcrux_ml_kem, hash_functions, portable, "*"]

# MLKEM: MISC NON-ARCHITECTURE SPECIFIC HEADERS
- name: libcrux_core
inline_static: true
private:
monomorphizations_of:
- [core, "*"]
- [libcrux_ml_kem, types, "*"]
- [libcrux_ml_kem, constant_time_ops, "*"]
- [libcrux_ml_kem, utils, "*" ]
monomorphizations_using:
- [Eurydice, "*" ]
- [libcrux_ml_kem, types, "*"]
patterns:
- [core, "*"]
- [libcrux_ml_kem, types ]
- [libcrux_ml_kem, constants ]
- [libcrux_ml_kem, constant_time_ops, "*"]
- [libcrux_ml_kem, utils, "*" ]
api:
- [Eurydice, "*"]

# MLKEM-768

- name: libcrux_mlkem768_avx2
inline_static: true
api:
patterns:
- [libcrux_ml_kem, vector, avx2, "*"]
- [libcrux_ml_kem, hash_functions, avx2, "*"]
- [libcrux_ml_kem, mlkem768, avx2]
- [libcrux_ml_kem, ind_cca, instantiations, avx2]
monomorphizations_of:
- [libcrux_ml_kem, vector, avx2, "*"]
- [libcrux_ml_kem, hash_functions, avx2, "*"]
- [libcrux_ml_kem, mlkem768, avx2]
- [libcrux_ml_kem, ind_cca, instantiations, avx2]
monomorphizations_using:
- [libcrux_ml_kem, vector, avx2, "*"]
- [libcrux_ml_kem, hash_functions, avx2, "*"]

- name: libcrux_mlkem768_portable
inline_static: true
api:
patterns:
- [libcrux_ml_kem, "*"]
- [libcrux_ml_kem, vector, "*"]
- [libcrux_ml_kem, hash_functions, portable, "*"]
# - [libcrux_ml_kem, polynomial, "*" ]
- [libcrux_ml_kem, mlkem768, portable, "*"]
- [libcrux_ml_kem, ind_cca, instantiations, portable, "*"]
monomorphizations_of:
- [libcrux_ml_kem, polynomial, "*" ]
- [libcrux_ml_kem, vector, "*"]
- [libcrux_ml_kem, hash_functions, portable, "*"]
- [libcrux_ml_kem, mlkem768, portable]
- [libcrux_ml_kem, ind_cca, instantiations, portable, "*"]
monomorphizations_using:
- [libcrux_ml_kem, polynomial, "*" ]
- [libcrux_ml_kem, vector, "*"]
- [libcrux_ml_kem, hash_functions, portable, "*"]
- [libcrux_ml_kem, ind_cca, instantiations, portable, "*"]
1 change: 1 addition & 0 deletions libcrux-ml-kem/cg/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
build
129 changes: 129 additions & 0 deletions libcrux-ml-kem/cg/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# cmake -B build -G "Ninja Multi-Config"
# cmake --build build
# # For release (benchmarks)
# cmake --build build --config Release

cmake_minimum_required(VERSION 3.10)

project(libcrux-ml-kem
VERSION 0.1.0
LANGUAGES C CXX
)

set(CMAKE_C_STANDARD 11)
set(CMAKE_CXX_STANDARD 20)

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(
${PROJECT_SOURCE_DIR}
${PROJECT_SOURCE_DIR}/internal
${PROJECT_SOURCE_DIR}/karamel/include
)

if(CMAKE_SYSTEM_PROCESSOR MATCHES "x86_64|amd64|AMD64")
message(STATUS "Detected an x64 architecture")
add_compile_definitions(LIBCRUX_X64)
endif()

if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8" AND DEFINED ENV{LIBCRUX_NEON})
message(STATUS "Detected an arm64 architecture")
add_compile_definitions(LIBCRUX_AARCH64)
endif()

# --- Tests

# Get gtests
include(FetchContent)
FetchContent_Declare(googletest
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
URL https://github.com/google/googletest/archive/refs/tags/release-1.11.0.zip
)

# For Windows: Prevent overriding the parent project's compiler/linker settings
set(gtest_force_shared_crt ON CACHE BOOL "" FORCE)
FetchContent_MakeAvailable(googletest)

# Get nlohmann json
FetchContent_Declare(json
DOWNLOAD_EXTRACT_TIMESTAMP TRUE
URL https://github.com/nlohmann/json/archive/refs/tags/v3.10.3.zip
)
FetchContent_MakeAvailable(json)

add_executable(ml_kem_test
${PROJECT_SOURCE_DIR}/tests/mlkem768.cc
)
target_link_libraries(ml_kem_test PRIVATE
gtest_main
nlohmann_json::nlohmann_json
)

add_executable(sha3_test
${PROJECT_SOURCE_DIR}/tests/sha3.cc
)
target_link_libraries(sha3_test PRIVATE
gtest_main
nlohmann_json::nlohmann_json
)

# --- Benchmarks
FetchContent_Declare(benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.8.4
)
FetchContent_MakeAvailable(benchmark)

add_executable(ml_kem_bench
${PROJECT_SOURCE_DIR}/benches/mlkem768.cc
)
target_link_libraries(ml_kem_bench PRIVATE
benchmark::benchmark
)

if(DEFINED ENV{SYMCRYPT_PATH})
message("Symcrypt path: $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}/bin/lib)
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
)
target_link_libraries(ml_kem_keygen PRIVATE
benchmark::benchmark
)

add_executable(ml_kem_encaps
${PROJECT_SOURCE_DIR}/benches/mlkem768_encaps.cc
)
target_link_libraries(ml_kem_encaps PRIVATE
benchmark::benchmark
)

if(NOT MSVC)
# We benchmark internal functions here that are inlined and thus not available
# in MSVC.
add_executable(sha3_bench
${PROJECT_SOURCE_DIR}/benches/sha3.cc
)
target_link_libraries(sha3_bench PRIVATE
benchmark::benchmark
)
endif(NOT MSVC)
Loading

0 comments on commit 5611bb3

Please sign in to comment.