Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix big-endian support in cg #649

Merged
merged 8 commits into from
Nov 5, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 3 additions & 10 deletions .github/workflows/c.yml
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ jobs:

- name: 🔨 Build
run: |
cmake -B build
LIBCRUX_BENCHMARKS=1 cmake -B build
cmake --build build

- name: 🏃🏻‍♀️ Test
Expand All @@ -132,7 +132,7 @@ jobs:
- name: 🔨 Build Release
run: |
rm -rf build
cmake -B build -DCMAKE_BUILD_TYPE=Release
LIBCRUX_BENCHMARKS=1 cmake -B build -DCMAKE_BUILD_TYPE=Release
cmake --build build --config Release
if: ${{ matrix.os != 'windows-latest' }}

Expand All @@ -159,14 +159,7 @@ jobs:
cmake -B build
cmake --build build
# FIXME: Benchmark build for cg on Windows CI is not working right now.
if: ${{ matrix.os != 'windows-latest' }}

# FIXME: Benchmark build for cg on Windows CI are not working right now.
# - name: 🏃🏻‍♀️ Test (cg)
# working-directory: libcrux-ml-kem/cg
# run: ./build/Debug/ml_kem_test
# if: ${{ matrix.os == 'windows-latest' }}

- name: 🏃🏻‍♀️ Test
run: ./build/ml_kem_test
if: ${{ matrix.os != 'windows-latest' }}
if: ${{ matrix.os == 'windows-latest' }}
franziskuskiefer marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 44 additions & 0 deletions .github/workflows/s390x.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
name: s390x - Build & Test

on:
push:
pull_request:
branches: ["main", "dev"]
workflow_dispatch:
merge_group:

env:
CARGO_TERM_COLOR: always

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
s390x:
runs-on: ubuntu-latest
name: Build on ubuntu-22.04 s390x
steps:
- uses: actions/checkout@v4
- uses: uraimo/run-on-arch-action@v2
name: Run
id: runcmd
with:
arch: s390x
distro: ubuntu22.04

# Speed up builds by storing container images in
# a GitHub package registry.
githubToken: ${{ github.token }}

run: |
apt-get -y update
apt-get install -y curl gcc g++ make cmake ninja-build git
cd libcrux-ml-kem/c
cmake -B build -G"Ninja Multi-Config"
cmake --build build
./build/Debug/ml_kem_test
cd ../cg
cmake -B build -G"Ninja Multi-Config"
cmake --build build
./build/Debug/ml_kem_test
88 changes: 45 additions & 43 deletions libcrux-ml-kem/c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ if(NOT MSVC)
# TODO: Clean up
add_compile_options(
-Wall

# -Wextra
# -pedantic
# -Wconversion
Expand All @@ -29,6 +30,7 @@ if(NOT MSVC)
endif(NOT MSVC)

set(CMAKE_COLOR_DIAGNOSTICS "ON")

# For LSP-based editors
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
include_directories(
Expand Down Expand Up @@ -101,12 +103,10 @@ if(CMAKE_SYSTEM_PROCESSOR MATCHES "aarch64|arm64|arm64v8" AND DEFINED ENV{LIBCRU
endif()

# --- Tests

if(DEFINED ENV{LIBCRUX_UNPACKED})
add_compile_definitions(LIBCRUX_UNPACKED)
endif(DEFINED ENV{LIBCRUX_UNPACKED})


# Get gtests
include(FetchContent)
FetchContent_Declare(googletest
Expand Down Expand Up @@ -144,52 +144,54 @@ target_link_libraries(sha3_test PRIVATE
)

# --- Benchmarks
FetchContent_Declare(benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.8.4
)
FetchContent_MakeAvailable(benchmark)
if(DEFINED ENV{LIBCRUX_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
ml_kem_static
benchmark::benchmark
)
add_executable(ml_kem_bench
${PROJECT_SOURCE_DIR}/benches/mlkem768.cc
)
target_link_libraries(ml_kem_bench PRIVATE
ml_kem_static
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})
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
ml_kem_static
benchmark::benchmark
)

add_executable(ml_kem_encaps
${PROJECT_SOURCE_DIR}/benches/mlkem768_encaps.cc
)
target_link_libraries(ml_kem_encaps PRIVATE
ml_kem_static
benchmark::benchmark
)
add_executable(ml_kem_keygen
${PROJECT_SOURCE_DIR}/benches/mlkem768_keygen.cc
)
target_link_libraries(ml_kem_keygen PRIVATE
ml_kem_static
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
add_executable(ml_kem_encaps
${PROJECT_SOURCE_DIR}/benches/mlkem768_encaps.cc
)
target_link_libraries(sha3_bench PRIVATE
target_link_libraries(ml_kem_encaps PRIVATE
ml_kem_static
benchmark::benchmark
)
endif(NOT MSVC)

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
ml_kem_static
benchmark::benchmark
)
endif(NOT MSVC)
endif(DEFINED ENV{LIBCRUX_BENCHMARKS})
86 changes: 44 additions & 42 deletions libcrux-ml-kem/cg/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ if(NOT MSVC)
endif(NOT MSVC)

if((CMAKE_C_COMPILER_ID STREQUAL "Clang" AND
CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0.0") OR
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND
CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.1.6"))
add_compile_options(-Werror -Wframe-larger-than=25344)
CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.0.0") OR
(CMAKE_C_COMPILER_ID STREQUAL "AppleClang" AND
CMAKE_C_COMPILER_VERSION VERSION_GREATER_EQUAL "13.1.6"))
add_compile_options(-Werror -Wframe-larger-than=25344)
endif()

set(CMAKE_COLOR_DIAGNOSTICS "ON")
Expand Down Expand Up @@ -95,48 +95,50 @@ target_link_libraries(sha3_test PRIVATE
)

# --- 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{LIBCRUX_BENCHMARKS})
FetchContent_Declare(benchmark
GIT_REPOSITORY https://github.com/google/benchmark.git
GIT_TAG v1.8.4
)
FetchContent_MakeAvailable(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_bench
${PROJECT_SOURCE_DIR}/benches/mlkem768.cc
)
target_link_libraries(ml_kem_bench PRIVATE
benchmark::benchmark
)

add_executable(ml_kem_keygen
${PROJECT_SOURCE_DIR}/benches/mlkem768_keygen.cc
)
target_link_libraries(ml_kem_keygen 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_encaps
${PROJECT_SOURCE_DIR}/benches/mlkem768_encaps.cc
)
target_link_libraries(ml_kem_encaps PRIVATE
benchmark::benchmark
)
add_executable(ml_kem_keygen
${PROJECT_SOURCE_DIR}/benches/mlkem768_keygen.cc
)
target_link_libraries(ml_kem_keygen 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
add_executable(ml_kem_encaps
${PROJECT_SOURCE_DIR}/benches/mlkem768_encaps.cc
)
target_link_libraries(sha3_bench PRIVATE
target_link_libraries(ml_kem_encaps PRIVATE
benchmark::benchmark
)
endif(NOT MSVC)

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)
endif(DEFINED ENV{LIBCRUX_BENCHMARKS})
13 changes: 5 additions & 8 deletions libcrux-ml-kem/cg/eurydice_glue.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ extern "C" {
#include <string.h>

#include "karamel/target.h"
#include "karamel/endianness.h"

// SLICES, ARRAYS, ETC.

Expand Down Expand Up @@ -88,7 +89,7 @@ typedef struct {
#define Eurydice_slice_copy(dst, src, t) \
memcpy(dst.ptr, src.ptr, dst.len * sizeof(t))
#define core_array___Array_T__N__23__as_slice(len_, ptr_, t, _ret_t) \
((Eurydice_slice){.ptr = ptr_, .len = len_})
(CLITERAL(Eurydice_slice){.ptr = ptr_, .len = len_})

#define core_array___core__clone__Clone_for__Array_T__N___20__clone( \
len, src, dst, elem_type, _ret_t) \
Expand Down Expand Up @@ -130,18 +131,14 @@ static inline void Eurydice_slice_to_array3(uint8_t *dst_tag, char *dst_ok,
// CORE STUFF (conversions, endianness, ...)

static inline void core_num__u64_9__to_le_bytes(uint64_t v, uint8_t buf[8]) {
memcpy(buf, &v, sizeof(v));
store64_le(buf, v);
}
static inline uint64_t core_num__u64_9__from_le_bytes(uint8_t buf[8]) {
uint64_t v;
memcpy(&v, buf, sizeof(v));
return v;
return load64_le(buf);
}

static inline uint32_t core_num__u32_8__from_le_bytes(uint8_t buf[4]) {
uint32_t v;
memcpy(&v, buf, sizeof(v));
return v;
return load32_le(buf);
}

static inline uint32_t core_num__u8_6__count_ones(uint8_t x0) {
Expand Down
Loading
Loading