Skip to content

Commit

Permalink
Merge pull request #524 from cryspen/franziskus/ml-dsa-boilerplate
Browse files Browse the repository at this point in the history
ML-DSA 44 instantiations and multiplexing
  • Loading branch information
franziskuskiefer authored Sep 9, 2024
2 parents b1c4c44 + 717b165 commit 4a129c7
Show file tree
Hide file tree
Showing 60 changed files with 3,315 additions and 2,509 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions libcrux-ml-dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,15 @@ bench = false # so libtest doesn't eat the arguments to criterion
[dependencies]
libcrux-sha3 = { version = "0.0.2-alpha.3", path = "../libcrux-sha3" }
libcrux-intrinsics = { version = "0.0.2-alpha.3", path = "../libcrux-intrinsics" }
libcrux-platform = { version = "0.0.2-alpha.3", path = "../sys/platform" }

[dev-dependencies]
rand = { version = "0.8" }
hex = { version = "0.4.3", features = ["serde"] }
serde_json = { version = "1.0" }
serde = { version = "1.0", features = ["derive"] }
criterion = "0.5"
pqcrypto-dilithium = { version = "0.5.0" } #, default-features = false
pqcrypto-dilithium = { version = "0.5.0" } #, default-features = false

[features]
simd128 = []
Expand All @@ -46,4 +47,3 @@ harness = false
[[bench]]
name = "ml-dsa"
harness = false

15 changes: 8 additions & 7 deletions libcrux-ml-dsa/benches/bench_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ macro_rules! bench {

#[macro_export]
macro_rules! bench_group_libcrux {
($variant:literal, $mod:ident, $keypair_t:ident, $signature_t:ident) => {{
($variant:literal, $mod:path, $keypair_t:ident, $signature_t:ident) => {{
use $mod as p;
bench!(
"(libcrux) KeyGen",
$variant,
Expand All @@ -81,7 +82,7 @@ macro_rules! bench_group_libcrux {
key_generation_seed
},
|key_generation_seed: [u8; KEY_GENERATION_RANDOMNESS_SIZE]| {
$mod::generate_key_pair(key_generation_seed)
p::generate_key_pair(key_generation_seed)
}
);

Expand All @@ -94,15 +95,15 @@ macro_rules! bench_group_libcrux {
bench_utils::random_array();
let signing_randomness: [u8; SIGNING_RANDOMNESS_SIZE] = bench_utils::random_array();
let message = bench_utils::random_array::<1023>();
let keypair = $mod::generate_key_pair(key_generation_seed);
let keypair = p::generate_key_pair(key_generation_seed);

(keypair, message, signing_randomness)
},
|(keypair, message, signing_randomness): (
$keypair_t,
[u8; 1023],
[u8; SIGNING_RANDOMNESS_SIZE]
)| { $mod::sign(&keypair.signing_key, &message, signing_randomness) }
)| { p::sign(&keypair.signing_key, &message, signing_randomness) }
);

bench!(
Expand All @@ -114,12 +115,12 @@ macro_rules! bench_group_libcrux {
bench_utils::random_array();
let signing_randomness: [u8; SIGNING_RANDOMNESS_SIZE] = bench_utils::random_array();
let message = bench_utils::random_array::<1023>();
let keypair = $mod::generate_key_pair(key_generation_seed);
let signature = $mod::sign(&keypair.signing_key, &message, signing_randomness);
let keypair = p::generate_key_pair(key_generation_seed);
let signature = p::sign(&keypair.signing_key, &message, signing_randomness);
(keypair, message, signature)
},
|(keypair, message, signature): ($keypair_t, [u8; 1023], $signature_t)| {
$mod::verify(&keypair.verification_key, &message, &signature).unwrap()
p::verify(&keypair.verification_key, &message, &signature).unwrap()
}
);

Expand Down
21 changes: 20 additions & 1 deletion libcrux-ml-dsa/benches/manual44.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,25 @@ use pqcrypto_dilithium;
mod bench_utils;

fn main() {
bench_group_libcrux!("44", ml_dsa_44, MLDSA44KeyPair, MLDSA44Signature);
bench_group_libcrux!(
"44 portable",
ml_dsa_44::portable,
MLDSA44KeyPair,
MLDSA44Signature
);
#[cfg(feature = "simd128")]
bench_group_libcrux!(
"44 sim1d28",
ml_dsa_44::neon,
MLDSA44KeyPair,
MLDSA44Signature
);
#[cfg(feature = "simd256")]
bench_group_libcrux!(
"44 simd256",
ml_dsa_44::avx2,
MLDSA44KeyPair,
MLDSA44Signature
);
bench_group_pqclean!("44", dilithium2);
}
36 changes: 36 additions & 0 deletions libcrux-ml-dsa/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
use std::env;

fn main() {
let target_arch = env::var("CARGO_CFG_TARGET_ARCH").unwrap();
let disable_simd128 = read_env("LIBCRUX_DISABLE_SIMD128");
let disable_simd256 = read_env("LIBCRUX_DISABLE_SIMD256");

// 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");

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\"");
}
let simd256_possible = target_arch == "x86_64";
if (simd256_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
// it's only used when available.
//
// We don't enable this on x86 because it seems to generate invalid code.
println!("cargo:rustc-cfg=feature=\"simd256\"");
}

println!("cargo::rustc-check-cfg=cfg(eurydice)");
}

fn read_env(key: &str) -> bool {
match env::var(key) {
Ok(s) => s == "1" || s == "y" || s == "Y",
Err(_) => false,
}
}
21 changes: 21 additions & 0 deletions libcrux-ml-dsa/examples/sign_44.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use libcrux_ml_dsa::ml_dsa_44::*;
use rand::{rngs::OsRng, RngCore};

fn random_array<const L: usize>() -> [u8; L] {
let mut rng = OsRng;
let mut seed = [0; L];
rng.try_fill_bytes(&mut seed).unwrap();
seed
}

fn main() {
let key_generation_seed = random_array();
let signing_randomness = random_array();
let message = random_array::<1023>();

let keypair = generate_key_pair(key_generation_seed);

for _i in 0..100_000 {
let _ = core::hint::black_box(sign(&keypair.signing_key, &message, signing_randomness));
}
}
Loading

0 comments on commit 4a129c7

Please sign in to comment.