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

Extract ML-KEM SIMD code to C #296

Merged
merged 99 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
8c02e75
workarounds for charon
franziskuskiefer May 20, 2024
594ad84
Merge branch 'dev' into franziskus/ml-kem-c-extraction
franziskuskiefer May 20, 2024
9d0c481
refactor: kill `GenericOperations` in favor of functions
W95Psp May 21, 2024
f85d218
refactor: kill strings in unreachable
W95Psp May 21, 2024
1dfd5d5
inlining
W95Psp May 23, 2024
a2bc1fb
kill two debug_asserts that cause trait issues in Charon
W95Psp May 24, 2024
3b3164c
commit C snapshot
W95Psp May 24, 2024
2180172
wip
W95Psp May 24, 2024
d712003
wip
W95Psp May 24, 2024
8511062
update sha3 api to hide internal state
franziskuskiefer May 24, 2024
886f965
add dummy intrinsics file for extraction of avx2
franziskuskiefer May 20, 2024
35d28a0
F* extraction of poly-avx2
franziskuskiefer May 20, 2024
5232b29
move neon hash functions into sha3 for ml-kem
franziskuskiefer May 22, 2024
ec4959c
inline hashing and avx2 hax extraction
franziskuskiefer May 22, 2024
b46607b
changes for F* extraction
franziskuskiefer May 22, 2024
3239743
updated F*
franziskuskiefer May 22, 2024
aca1b2a
portable rejection sampling for neon for now
franziskuskiefer May 23, 2024
6f0495d
feat(ci/F*): get rid of timestamps in patch files
W95Psp May 22, 2024
4e777f8
chore(kyber/fstar): update patches following hacspec/hax#676
W95Psp May 22, 2024
67618a4
Added scaffolding to get work started on ML-DSA. (#289)
xvzcf May 23, 2024
6ca5766
wip
W95Psp May 24, 2024
9226a0b
fixup
franziskuskiefer May 26, 2024
4411d7f
actually inline neon/avx2
franziskuskiefer May 26, 2024
8738c18
fixups and allow portable only extraction
franziskuskiefer May 26, 2024
4a445d0
fixup avx2
franziskuskiefer May 26, 2024
b13f986
fixup neon
franziskuskiefer May 26, 2024
a84d517
fixups for avx2 extraction
franziskuskiefer May 27, 2024
e78b2d7
Merge branch 'ml-kem-c-extraction-inlining-crates' of github.com:crys…
franziskuskiefer May 27, 2024
0b6dd0f
allow extracting everywhere
franziskuskiefer May 27, 2024
66c2e55
fixup c.sh
franziskuskiefer May 27, 2024
03461b6
add new config c2.yml
franziskuskiefer May 27, 2024
cd1de6b
externalize intrinsics into a `libcrux-intrinsics` crate
W95Psp May 28, 2024
d950056
fixup neon; add new extraction (broken)
franziskuskiefer May 28, 2024
6127191
update extraction
franziskuskiefer May 28, 2024
4c90f8e
externalize more intrinsics
W95Psp May 28, 2024
2a53025
fix: assert_debug, expect 32, not 16
W95Psp May 28, 2024
40bb6e8
kill `avx2/debug` module
W95Psp May 28, 2024
1cbc9a7
kill stale intrinsics modules
W95Psp May 28, 2024
15a7b29
check in `c` extractions
W95Psp May 28, 2024
f75fee4
Revert "check in `c` extractions"
W95Psp May 28, 2024
c7724dd
check in a snapshot of the `c` folder in `c-snapshot`
W95Psp May 28, 2024
3015d42
made sha3 safe
karthikbhargavan May 28, 2024
99909f5
add a script for extracting c
W95Psp May 28, 2024
49ca8f7
work around eurydice issues in sha3
franziskuskiefer May 28, 2024
c8a38e2
use config to extract
franziskuskiefer May 28, 2024
12fb7ff
fixup sha3
franziskuskiefer May 28, 2024
e12075e
eurydice workaround
franziskuskiefer May 28, 2024
ed89380
A config to do separate bundling
msprotz May 28, 2024
fe31398
testing one wrapper before going deeper
karthikbhargavan May 29, 2024
39e5d0d
testing one wrapper before going deeper
karthikbhargavan May 29, 2024
04843f2
re-extract into `c-snapshot/`
W95Psp May 29, 2024
540556f
c.yaml: replicate neon options for avx2
W95Psp May 29, 2024
7536322
sha3 c.yaml
franziskuskiefer May 29, 2024
88b6c42
Fix bundling
msprotz May 29, 2024
c243e01
c code sha3 with build
franziskuskiefer May 29, 2024
8515d8b
drop chunk
franziskuskiefer May 29, 2024
3cc1486
Dummies to make compilation work without hand edits
msprotz May 29, 2024
38dd756
missing intrinsics and functions
karthikbhargavan May 29, 2024
cd9ad91
patch for intrinsics ordering
karthikbhargavan May 29, 2024
29e23ec
fixup build
franziskuskiefer May 29, 2024
49b7904
add sha3 test
franziskuskiefer May 29, 2024
9df56fd
fix intrinsics
franziskuskiefer May 29, 2024
165e74e
refresh
msprotz May 29, 2024
798f69c
refresh source
msprotz May 29, 2024
2c373e6
And fix up the config for neon, too
msprotz May 29, 2024
ba2cbcb
Single run of Eurydice -- much easier
msprotz May 30, 2024
1366394
Merge branch 'lucas/extract-intrinsics' of github.com:cryspen/libcrux…
msprotz May 30, 2024
98edcf1
WIP simplifying build
msprotz May 30, 2024
b015f6f
Snapshot of the C code now lives in libcrux-ml-kem/c
msprotz May 30, 2024
0382b43
Regenerate the code on AVX2... and update config so that AVX2 code-ge…
msprotz May 30, 2024
bc9b3ac
Restore some config tweaks, reinstate the build of the sha3 test
msprotz May 30, 2024
af4e0cf
Tweaks, good progress on whole mlkem compilation
msprotz May 30, 2024
a0b0b93
Work around aeneasverif/eurydice#12
msprotz May 30, 2024
381a0a4
Use a two-dimensional array now that it's supported
msprotz May 30, 2024
0ac31ce
It compiles
msprotz May 30, 2024
d89558c
zomg
msprotz May 30, 2024
3b85af7
updated C build and mlkem tests
franziskuskiefer May 30, 2024
8b112db
Regenerate the code with revised general-purpose slice index operation
msprotz May 30, 2024
34f92e1
With loop unrolling
msprotz May 30, 2024
5b5b73e
benches
karthikbhargavan May 30, 2024
08aee25
bench
karthikbhargavan May 30, 2024
746f356
benches
karthikbhargavan May 30, 2024
87a5209
a snapshot of hand edits to speed up sha3
karthikbhargavan May 30, 2024
8f297df
wip bundling
msprotz May 30, 2024
5bc3ad6
Regenerated C code with fresh config
msprotz May 31, 2024
4efb699
Revised config
msprotz May 31, 2024
a91effc
Refresh
msprotz May 31, 2024
a9888dd
clang-format
franziskuskiefer May 31, 2024
67d7b0f
update boringssl benchmarks
franziskuskiefer May 31, 2024
a62ee49
readme for C code
franziskuskiefer May 31, 2024
95c3ab5
inline sha3
franziskuskiefer Jun 3, 2024
ab7694b
Merge branch 'dev' into franziskus/mlkem-c
franziskuskiefer Jun 3, 2024
be73700
rustfmt
franziskuskiefer Jun 3, 2024
a5394b9
fixup ml-dsa again
franziskuskiefer Jun 3, 2024
cd246f5
Add more instructions to c README.md
franziskuskiefer Jun 3, 2024
295f500
drop c experiments
franziskuskiefer Jun 4, 2024
09f13ef
drop more files
franziskuskiefer Jun 4, 2024
8246792
keep supporting old rustc
franziskuskiefer Jun 4, 2024
bc3565b
Merge branch 'dev' into franziskus/mlkem-c
franziskuskiefer Jun 4, 2024
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
5 changes: 1 addition & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ members = [
"libcrux-ml-kem",
"libcrux-simd",
"libcrux-sha3",
"polynomials",
"polynomials-avx2",
"traits",
"polynomials-aarch64",
"libcrux-ml-dsa",
"libcrux-intrinsics",
]

[workspace.package]
Expand Down
2 changes: 1 addition & 1 deletion benchmarks/benches/boringssl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ FetchContent_MakeAvailable(benchmark)
FetchContent_Declare(
boringssl
GIT_REPOSITORY https://boringssl.googlesource.com/boringssl
GIT_TAG 1e3da32f3754b1b9136247ee26308cfd959cbeba
GIT_TAG 1eda2363f9e79aaa5febe91d31b6756ae4f24f30
)
FetchContent_MakeAvailable(boringssl)

Expand Down
6 changes: 3 additions & 3 deletions benchmarks/benches/boringssl/kyber768.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#include <stdlib.h>

#include <openssl/bytestring.h>
#include <openssl/kyber.h>
#include <openssl/experimental/kyber.h>

#include <benchmark/benchmark.h>

Expand Down Expand Up @@ -48,7 +48,7 @@ static void BM_Encapsulation(benchmark::State &state) {
state.SkipWithError("Error: KYBER_parse_public_key");
}

KYBER_encap(ciphertext, shared_secret, sizeof(shared_secret), &pub);
KYBER_encap(ciphertext, shared_secret, &pub);
}
}

Expand Down Expand Up @@ -87,7 +87,7 @@ static void BM_Decapsulation(benchmark::State &state) {
state.SkipWithError("Error: KYBER_parse_private_key()");
}

KYBER_decap(shared_secret, sizeof(shared_secret), ciphertext, &priv);
KYBER_decap(shared_secret, ciphertext, &priv);
}
}

Expand Down
14 changes: 9 additions & 5 deletions benchmarks/benches/boringssl/shake.cxx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include <stdlib.h>
#include <string>

#include "crypto/kyber/internal.h"
#include "crypto/keccak/internal.h"

#include <benchmark/benchmark.h>

Expand All @@ -16,8 +16,10 @@ static void BM_SHAKE128(benchmark::State &state) {
uint8_t output[SHAKE128_BYTES_TO_OUTPUT];

for (auto _ : state) {
BORINGSSL_keccak(output, SHAKE128_BYTES_TO_OUTPUT, input, sizeof(input),
boringssl_shake128);
struct BORINGSSL_keccak_st keccak_ctx;
BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake128);
BORINGSSL_keccak_absorb(&keccak_ctx, input, sizeof(input));
BORINGSSL_keccak_squeeze(&keccak_ctx, output, sizeof(output));
}
}

Expand All @@ -32,8 +34,10 @@ static void BM_SHAKE256(benchmark::State &state) {
uint8_t output[SHAKE256_BYTES_TO_OUTPUT];

for (auto _ : state) {
BORINGSSL_keccak(output, SHAKE256_BYTES_TO_OUTPUT, input, sizeof(input),
boringssl_shake256);
struct BORINGSSL_keccak_st keccak_ctx;
BORINGSSL_keccak_init(&keccak_ctx, boringssl_shake256);
BORINGSSL_keccak_absorb(&keccak_ctx, input, sizeof(input));
BORINGSSL_keccak_squeeze(&keccak_ctx, output, sizeof(output));
}
}

Expand Down
23 changes: 23 additions & 0 deletions libcrux-intrinsics/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[package]
name = "libcrux-intrinsics"
version.workspace = true
authors.workspace = true
license.workspace = true
homepage.workspace = true
edition.workspace = true
repository.workspace = true
readme.workspace = true

[dependencies]
# libcrux-platform = { version = "0.0.2-pre.2", path = "../sys/platform" }

# This is only required for verification.
# The hax config is set by the hax toolchain.
[target.'cfg(hax)'.dependencies]
hax-lib = { git = "https://github.com/hacspec/hax/" }

[features]
simd128 = []
simd256 = []

[dev-dependencies]
26 changes: 16 additions & 10 deletions polynomials/build.rs → libcrux-intrinsics/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ use std::env;

fn main() {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let disable_simd128 = match env::var("LIBCRUX_DISABLE_SIMD128") {
Ok(s) => s == "1" || s == "y" || s == "Y",
Err(_) => false,
};
let disable_simd128 = read_env("LIBCRUX_DISABLE_SIMD128");
let disable_simd256 = read_env("LIBCRUX_DISABLE_SIMD256");

let disable_simd256 = match env::var("LIBCRUX_DISABLE_SIMD256") {
Ok(s) => s == "1" || s == "y" || s == "Y",
Err(_) => false,
};
// Force a simd build. Make sure you know what you're doing.
let enable_simd128 = read_env("LIBCRUX_ENABLE_SIMD128");
let enable_simd256 = read_env("LIBCRUX_ENABLE_SIMD256");

if target_arch == "aarch64" && !disable_simd128 {
let simd128_possible = target_arch == "aarch64";
if (simd128_possible || enable_simd128) && !disable_simd128 {
// We enable simd128 on all aarch64 builds.
println!("cargo:rustc-cfg=feature=\"simd128\"");
}
if target_arch == "x86_64" && !disable_simd256 {
let simd126_possible = target_arch == "x86_64";
if (simd126_possible || enable_simd256) && !disable_simd256 {
// We enable simd256 on all x86_64 builds.
// Note that this doesn't mean the required CPU features are available.
// But the compiler will support them and the runtime checks ensure that
Expand All @@ -26,3 +25,10 @@ fn main() {
println!("cargo:rustc-cfg=feature=\"simd256\"");
}
}

fn read_env(key: &str) -> bool {
match env::var(key) {
Ok(s) => s == "1" || s == "y" || s == "Y",
Err(_) => false,
}
}
Loading
Loading