Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
franziskuskiefer committed Jan 2, 2025
1 parent 6ed6fa3 commit 9252b9d
Show file tree
Hide file tree
Showing 14 changed files with 106 additions and 84 deletions.
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,9 @@ lto = "fat"
codegen-units = 1
panic = "abort"

[profile.dev.package."libcrux-ml-dsa"]
opt-level = 1
# XXX: Not needed anymore, but nice for test speed
# [profile.dev.package."libcrux-ml-dsa"]
# opt-level = 1

[lints.rust]
unexpected_cfgs = { level = "warn", check-cfg = [
Expand Down
1 change: 1 addition & 0 deletions libcrux-ml-dsa/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ bench = false # so libtest doesn't eat the arguments to criterion
libcrux-sha3 = { version = "0.0.2-beta.2", path = "../libcrux-sha3" }
libcrux-intrinsics = { version = "0.0.2-beta.2", path = "../libcrux-intrinsics" }
libcrux-platform = { version = "0.0.2-beta.2", path = "../sys/platform" }
libcrux-macros = { version = "0.0.2-beta.2", path = "../macros" }
hax-lib = { version = "0.1.0-alpha.1", git = "https://github.com/hacspec/hax/" }

[dev-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-dsa/boring.sh
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ done
if [[ "$no_clean" = 0 ]]; then
cargo clean
fi
# TODO: add feature flags for mldsa65

./c.sh --config cg.yaml --out cg --mldsa65\
--no-glue --no-unrolling --no-karamel_include --no-karamel_include

Expand Down
2 changes: 1 addition & 1 deletion libcrux-ml-dsa/cg/code_gen.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ Charon: db4e045d4597d06d854ce7a2c10e8dcfda6ecd25
Eurydice: 75eae2e2534a16f5ba5430e6ee5c69d8a46f3bea
Karamel: 3823e3d82fa0b271d799b61c59ffb4742ddc1e65
F*: b0961063393215ca65927f017720cb365a193833-dirty
Libcrux: 834b7f51701fa4e8695a784c138ed230f49f0c4e
Libcrux: a596b564bbc047e157eb19f66887f965403a30e6
2 changes: 1 addition & 1 deletion libcrux-ml-dsa/cg/header.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@
* Eurydice: 75eae2e2534a16f5ba5430e6ee5c69d8a46f3bea
* Karamel: 3823e3d82fa0b271d799b61c59ffb4742ddc1e65
* F*: b0961063393215ca65927f017720cb365a193833-dirty
* Libcrux: 834b7f51701fa4e8695a784c138ed230f49f0c4e
* Libcrux: a596b564bbc047e157eb19f66887f965403a30e6
*/
3 changes: 3 additions & 0 deletions libcrux-ml-dsa/src/ml_dsa_generic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,21 @@ pub(crate) mod multiplexing;
#[libcrux_macros::consts(
// Key size specific constants
v44 {
#[cfg(feature = "mldsa44")]
const ROWS_IN_A: usize = constants::v44::ROWS_IN_A;
const COLUMNS_IN_A: usize = constants::v44::COLUMNS_IN_A;
const ETA: Eta = constants::v44::ETA;
const BITS_PER_ERROR_COEFFICIENT: usize = constants::v44::BITS_PER_ERROR_COEFFICIENT;
},
v65 {
#[cfg(feature = "mldsa65")]
const ROWS_IN_A: usize = constants::v65::ROWS_IN_A;
const COLUMNS_IN_A: usize = constants::v65::COLUMNS_IN_A;
const ETA: Eta = constants::v65::ETA;
const BITS_PER_ERROR_COEFFICIENT: usize = constants::v65::BITS_PER_ERROR_COEFFICIENT;
},
v87 {
#[cfg(feature = "mldsa87")]
const ROWS_IN_A: usize = constants::v87::ROWS_IN_A;
const COLUMNS_IN_A: usize = constants::v87::COLUMNS_IN_A;
const ETA: Eta = constants::v87::ETA;
Expand Down
3 changes: 3 additions & 0 deletions libcrux-ml-dsa/src/ml_dsa_generic/instantiations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ macro_rules! instantiate {
};
}

#[cfg(feature = "mldsa44")]
generate_key_pair!(generate_key_pair_v44);
#[cfg(feature = "mldsa65")]
generate_key_pair!(generate_key_pair_v65);
#[cfg(feature = "mldsa87")]
generate_key_pair!(generate_key_pair_v87);

/// Sign.
Expand Down
88 changes: 31 additions & 57 deletions libcrux-ml-dsa/src/ml_dsa_generic/instantiations/avx2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,65 +8,36 @@ use crate::{
mod avx2_feature {
use super::*;

/// Generate key pair.
#[cfg_attr(not(hax), target_feature(enable = "avx2"))]
#[allow(unsafe_code)]
pub(super) unsafe fn generate_key_pair_v44(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8],
verification_key: &mut [u8],
) {
crate::ml_dsa_generic::generate_key_pair_v44::<
crate::simd::avx2::AVX2SIMDUnit,
crate::samplex4::avx2::AVX2Sampler,
crate::hash_functions::simd256::Shake128x4,
crate::hash_functions::simd256::Shake256,
// We use the portable version here.
// It doesn' make sense to do these in parallel.
crate::hash_functions::portable::Shake256Xof,
crate::hash_functions::simd256::Shake256x4,
>(randomness, signing_key, verification_key)
macro_rules! generate_key_pair {
($name:ident) => {
/// Generate key pair.
#[cfg_attr(not(hax), target_feature(enable = "avx2"))]
#[allow(unsafe_code)]
pub(super) unsafe fn $name(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8],
verification_key: &mut [u8],
) {
crate::ml_dsa_generic::$name::<
crate::simd::avx2::AVX2SIMDUnit,
crate::samplex4::avx2::AVX2Sampler,
crate::hash_functions::simd256::Shake128x4,
crate::hash_functions::simd256::Shake256,
// We use the portable version here.
// It doesn' make sense to do these in parallel.
crate::hash_functions::portable::Shake256Xof,
crate::hash_functions::simd256::Shake256x4,
>(randomness, signing_key, verification_key)
}
};
}

/// Generate key pair.
#[cfg_attr(not(hax), target_feature(enable = "avx2"))]
#[allow(unsafe_code)]
pub(super) unsafe fn generate_key_pair_v65(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8],
verification_key: &mut [u8],
) {
crate::ml_dsa_generic::generate_key_pair_v65::<
crate::simd::avx2::AVX2SIMDUnit,
crate::samplex4::avx2::AVX2Sampler,
crate::hash_functions::simd256::Shake128x4,
crate::hash_functions::simd256::Shake256,
// We use the portable version here.
// It doesn' make sense to do these in parallel.
crate::hash_functions::portable::Shake256Xof,
crate::hash_functions::simd256::Shake256x4,
>(randomness, signing_key, verification_key)
}

/// Generate key pair.
#[cfg_attr(not(hax), target_feature(enable = "avx2"))]
#[allow(unsafe_code)]
pub(super) unsafe fn generate_key_pair_v87(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8],
verification_key: &mut [u8],
) {
crate::ml_dsa_generic::generate_key_pair_v87::<
crate::simd::avx2::AVX2SIMDUnit,
crate::samplex4::avx2::AVX2Sampler,
crate::hash_functions::simd256::Shake128x4,
crate::hash_functions::simd256::Shake256,
// We use the portable version here.
// It doesn' make sense to do these in parallel.
crate::hash_functions::portable::Shake256Xof,
crate::hash_functions::simd256::Shake256x4,
>(randomness, signing_key, verification_key)
}
#[cfg(feature = "mldsa44")]
generate_key_pair!(generate_key_pair_v44);
#[cfg(feature = "mldsa65")]
generate_key_pair!(generate_key_pair_v65);
#[cfg(feature = "mldsa87")]
generate_key_pair!(generate_key_pair_v87);

/// Sign.
#[cfg_attr(not(hax), target_feature(enable = "avx2"))]
Expand Down Expand Up @@ -382,6 +353,7 @@ mod avx2_feature {
}
}

#[cfg(feature = "mldsa44")]
/// Generate key pair.
#[allow(unsafe_code)]
pub(crate) fn generate_key_pair_v44(
Expand All @@ -392,6 +364,7 @@ pub(crate) fn generate_key_pair_v44(
unsafe { avx2_feature::generate_key_pair_v44(randomness, signing_key, verification_key) }
}

#[cfg(feature = "mldsa65")]
/// Generate key pair.
#[allow(unsafe_code)]
pub(crate) fn generate_key_pair_v65(
Expand All @@ -402,6 +375,7 @@ pub(crate) fn generate_key_pair_v65(
unsafe { avx2_feature::generate_key_pair_v65(randomness, signing_key, verification_key) }
}

#[cfg(feature = "mldsa87")]
/// Generate key pair.
#[allow(unsafe_code)]
pub(crate) fn generate_key_pair_v87(
Expand Down
31 changes: 21 additions & 10 deletions libcrux-ml-dsa/src/ml_dsa_generic/multiplexing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@ use libcrux_platform;

#[cfg(feature = "simd256")]
use instantiations::avx2::{
generate_key_pair_v44 as generate_key_pair_v44_avx2,
generate_key_pair_v65 as generate_key_pair_v65_avx2,
generate_key_pair_v87 as generate_key_pair_v87_avx2, sign as sign_avx2,
sign_pre_hashed_shake128 as sign_pre_hashed_shake128_avx2, verify as verify_avx2,
verify_pre_hashed_shake128 as verify_pre_hashed_shake128_avx2,
sign as sign_avx2, sign_pre_hashed_shake128 as sign_pre_hashed_shake128_avx2,
verify as verify_avx2, verify_pre_hashed_shake128 as verify_pre_hashed_shake128_avx2,
};

#[cfg(all(feature = "simd256", feature = "mldsa44"))]
use instantiations::portable::generate_key_pair_v44 as generate_key_pair_v44_avx2;
#[cfg(all(feature = "simd256", feature = "mldsa65"))]
use instantiations::portable::generate_key_pair_v65 as generate_key_pair_v65_avx2;
#[cfg(all(feature = "simd256", feature = "mldsa87"))]
use instantiations::portable::generate_key_pair_v87 as generate_key_pair_v87_avx2;

#[cfg(all(feature = "simd256", feature = "acvp"))]
use instantiations::avx2::{
sign_internal as sign_internal_avx2, verify_internal as verify_internal_avx2,
Expand Down Expand Up @@ -50,13 +54,18 @@ use instantiations::portable::{

#[cfg(not(feature = "simd128"))]
use instantiations::portable::{
generate_key_pair_v44 as generate_key_pair_v44_neon,
generate_key_pair_v65 as generate_key_pair_v65_neon,
generate_key_pair_v87 as generate_key_pair_v87_neon, sign as sign_neon,
sign_pre_hashed_shake128 as sign_pre_hashed_shake128_neon, verify as verify_neon,
verify_pre_hashed_shake128 as verify_pre_hashed_shake128_neon,
sign as sign_neon, sign_pre_hashed_shake128 as sign_pre_hashed_shake128_neon,
verify as verify_neon, verify_pre_hashed_shake128 as verify_pre_hashed_shake128_neon,
};

#[cfg(all(not(feature = "simd128"), feature = "mldsa44"))]
use instantiations::portable::generate_key_pair_v44 as generate_key_pair_v44_neon;
#[cfg(all(not(feature = "simd128"), feature = "mldsa65"))]
use instantiations::portable::generate_key_pair_v65 as generate_key_pair_v65_neon;
#[cfg(all(not(feature = "simd128"), feature = "mldsa87"))]
use instantiations::portable::generate_key_pair_v87 as generate_key_pair_v87_neon;

#[cfg(feature = "mldsa44")]
pub(crate) fn generate_key_pair_v44(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8],
Expand All @@ -71,6 +80,7 @@ pub(crate) fn generate_key_pair_v44(
}
}

#[cfg(feature = "mldsa65")]
pub(crate) fn generate_key_pair_v65(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8],
Expand All @@ -85,6 +95,7 @@ pub(crate) fn generate_key_pair_v65(
}
}

#[cfg(feature = "mldsa87")]
pub(crate) fn generate_key_pair_v87(
randomness: [u8; KEY_GENERATION_RANDOMNESS_SIZE],
signing_key: &mut [u8],
Expand Down
10 changes: 6 additions & 4 deletions libcrux-ml-dsa/tests/nistkats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ macro_rules! impl_nist_known_answer_tests {

// 44

#[cfg(feature = "mldsa44")]
impl_nist_known_answer_tests!(
nist_known_answer_tests_44,
nist_known_answer_tests_pre_hashed_44,
Expand All @@ -131,6 +132,7 @@ impl_nist_known_answer_tests!(
libcrux_ml_dsa::ml_dsa_44::verify_pre_hashed_shake128
);

#[cfg(feature = "mldsa44")]
impl_nist_known_answer_tests!(
nist_known_answer_tests_44_portable,
nist_known_answer_tests_pre_hashed_44_portable,
Expand All @@ -142,7 +144,7 @@ impl_nist_known_answer_tests!(
libcrux_ml_dsa::ml_dsa_44::verify_pre_hashed_shake128
);

#[cfg(feature = "simd128")]
#[cfg(all(feature = "simd128", feature = "mldsa44"))]
impl_nist_known_answer_tests!(
nist_known_answer_tests_44_simd128,
nist_known_answer_tests_pre_hashed_44_simd128,
Expand All @@ -154,7 +156,7 @@ impl_nist_known_answer_tests!(
libcrux_ml_dsa::ml_dsa_44::verify_pre_hashed_shake128
);

#[cfg(feature = "simd256")]
#[cfg(all(feature = "simd256", feature = "mldsa44"))]
impl_nist_known_answer_tests!(
nist_known_answer_tests_44_simd256,
nist_known_answer_tests_pre_hashed_44_simd256,
Expand All @@ -167,7 +169,7 @@ impl_nist_known_answer_tests!(
);

// 65

#[cfg(feature = "mldsa65")]
impl_nist_known_answer_tests!(
nist_known_answer_tests_65,
nist_known_answer_tests_pre_hashed_65,
Expand All @@ -180,7 +182,7 @@ impl_nist_known_answer_tests!(
);

// 87

#[cfg(feature = "mldsa87")]
impl_nist_known_answer_tests!(
nist_known_answer_tests_87,
nist_known_answer_tests_pre_hashed_87,
Expand Down
4 changes: 4 additions & 0 deletions libcrux-sha3/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -319,23 +319,27 @@ pub mod portable {
/// Shake256 XOF in absorb state
impl Xof<136> for Shake256Xof {
/// Shake256 new state
#[inline(always)]
fn new() -> Self {
Self {
state: KeccakXofState::<1, 136, u64>::new(),
}
}

/// Shake256 absorb
#[inline(always)]
fn absorb(&mut self, input: &[u8]) {
self.state.absorb([input]);
}

/// Shake256 absorb final
#[inline(always)]
fn absorb_final(&mut self, input: &[u8]) {
self.state.absorb_final::<0x1fu8>([input]);
}

/// Shake256 squeeze
#[inline(always)]
fn squeeze(&mut self, out: &mut [u8]) {
self.state.squeeze([out]);
}
Expand Down
2 changes: 2 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ readme.workspace = true
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
quote = "1.0.37"
syn = { version = "2.0.89", features = ["full"] }

[lib]
proc-macro = true
Loading

0 comments on commit 9252b9d

Please sign in to comment.