Skip to content

Commit

Permalink
More inlining
Browse files Browse the repository at this point in the history
  • Loading branch information
jschneider-bensch committed Oct 17, 2024
1 parent d9858f6 commit 5c6d1b5
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 2 deletions.
10 changes: 10 additions & 0 deletions libcrux-ml-kem/src/ind_cca/instantiations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ macro_rules! instantiate {
};

/// Portable generate key pair.
#[inline(always)]
pub(crate) fn generate_keypair<
const K: usize,
const CPA_PRIVATE_KEY_SIZE: usize,
Expand All @@ -33,6 +34,7 @@ macro_rules! instantiate {
}

#[cfg(feature = "kyber")]
#[inline(always)]
pub(crate) fn kyber_generate_keypair<
const K: usize,
const CPA_PRIVATE_KEY_SIZE: usize,
Expand Down Expand Up @@ -93,6 +95,7 @@ macro_rules! instantiate {

/// Portable encapsulate
#[cfg(feature = "kyber")]
#[inline(always)]
pub(crate) fn kyber_encapsulate<
const K: usize,
const CIPHERTEXT_SIZE: usize,
Expand Down Expand Up @@ -131,6 +134,7 @@ macro_rules! instantiate {
>(public_key, randomness)
}

#[inline(always)]
pub(crate) fn encapsulate<
const K: usize,
const CIPHERTEXT_SIZE: usize,
Expand Down Expand Up @@ -171,6 +175,7 @@ macro_rules! instantiate {

/// Portable decapsulate
#[cfg(feature = "kyber")]
#[inline(always)]
pub fn kyber_decapsulate<
const K: usize,
const SECRET_KEY_SIZE: usize,
Expand Down Expand Up @@ -216,6 +221,7 @@ macro_rules! instantiate {
}

/// Portable decapsulate
#[inline(always)]
pub fn decapsulate<
const K: usize,
const SECRET_KEY_SIZE: usize,
Expand Down Expand Up @@ -270,6 +276,7 @@ macro_rules! instantiate {
crate::ind_cca::unpacked::MlKemPublicKeyUnpacked<K, $vector>;

/// Get the unpacked public key.
#[inline(always)]
pub(crate) fn unpack_public_key<
const K: usize,
const T_AS_NTT_ENCODED_SIZE: usize,
Expand All @@ -290,6 +297,7 @@ macro_rules! instantiate {
}

/// Generate a key pair
#[inline(always)]
pub(crate) fn generate_keypair<
const K: usize,
const CPA_PRIVATE_KEY_SIZE: usize,
Expand Down Expand Up @@ -317,6 +325,7 @@ macro_rules! instantiate {
}

/// Unpacked encapsulate
#[inline(always)]
pub(crate) fn encapsulate<
const K: usize,
const CIPHERTEXT_SIZE: usize,
Expand Down Expand Up @@ -355,6 +364,7 @@ macro_rules! instantiate {
}

/// Unpacked decapsulate
#[inline(always)]
pub(crate) fn decapsulate<
const K: usize,
const SECRET_KEY_SIZE: usize,
Expand Down
2 changes: 2 additions & 0 deletions libcrux-ml-kem/src/ind_cpa.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ fn sample_vector_cbd_then_ntt_out<
/// The NIST FIPS 203 standard can be found at
/// <https://csrc.nist.gov/pubs/fips/203/ipd>.
#[allow(non_snake_case)]
#[inline(always)]
pub(crate) fn generate_keypair_unpacked<
const K: usize,
const ETA1: usize,
Expand Down Expand Up @@ -273,6 +274,7 @@ pub(crate) fn generate_keypair_unpacked<
}

#[allow(non_snake_case)]
#[inline(always)]
pub(crate) fn generate_keypair<
const K: usize,
const PRIVATE_KEY_SIZE: usize,
Expand Down
6 changes: 4 additions & 2 deletions libcrux-ml-kem/src/vector/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,19 +65,21 @@ pub trait Operations: Copy + Clone {
}

// hax does not support trait with default implementations, so we use the following pattern
#[inline(always)]
pub fn montgomery_multiply_fe<T: Operations>(v: T, fer: i16) -> T {
T::montgomery_multiply_by_constant(v, fer)
}
#[inline(always)]
pub fn to_standard_domain<T: Operations>(v: T) -> T {
T::montgomery_multiply_by_constant(v, MONTGOMERY_R_SQUARED_MOD_FIELD_MODULUS as i16)
}

#[inline(always)]
pub fn to_unsigned_representative<T: Operations>(a: T) -> T {
let t = T::shift_right::<15>(a);
let fm = T::bitwise_and_with_constant(t, FIELD_MODULUS);
T::add(a, &fm)
}

#[inline(always)]
pub fn decompress_1<T: Operations>(v: T) -> T {
T::bitwise_and_with_constant(T::sub(T::ZERO(), &v), 1665)
}

0 comments on commit 5c6d1b5

Please sign in to comment.