Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pkcs8 = ["dep:const-oid", "dep:pkcs8"]
zeroize = ["module-lattice/zeroize", "dep:zeroize"]

[dependencies]
array = { version = "0.4.4", package = "hybrid-array", features = ["extra-sizes", "subtle"] }
array = { version = "0.4.7", package = "hybrid-array", features = ["extra-sizes", "subtle"] }
module-lattice = { version = "0.1.0-rc.0", features = ["subtle"] }
kem = "0.3.0-rc.3"
rand_core = "0.10.0-rc-6"
Expand Down
8 changes: 5 additions & 3 deletions ml-kem/src/algebra.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use array::{Array, typenum::U256};
use module_lattice::{
algebra::{Field, MultiplyNtt},
encoding::Encode,
utils::Truncate,
truncate::Truncate,
};
use sha3::digest::XofReader;

Expand Down Expand Up @@ -319,8 +319,10 @@ mod test {
Array, ArraySize, B32, BaseField, Elem, Field, Int, Ntt, NttInverse, NttMatrix,
NttPolynomial, NttVector, PRF, Polynomial, U256, XOF,
};
use array::typenum::{U2, U3, U8};
use module_lattice::utils::Flatten;
use array::{
Flatten,
typenum::{U2, U3, U8},
};

/// A polynomial with only a scalar component, to make simple test cases
fn const_ntt(x: Int) -> NttPolynomial {
Expand Down
2 changes: 1 addition & 1 deletion ml-kem/src/compress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::algebra::{BaseField, Elem, Int, Polynomial, Vector};
use crate::param::{ArraySize, EncodingSize};
use module_lattice::{algebra::Field, utils::Truncate};
use module_lattice::{algebra::Field, truncate::Truncate};

// A convenience trait to allow us to associate some constants with a typenum
pub(crate) trait CompressionFactor: EncodingSize {
Expand Down
2 changes: 1 addition & 1 deletion module-lattice/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ categories = ["cryptography", "no-std"]
keywords = ["crypto", "kyber", "lattice", "post-quantum"]

[dependencies]
array = { version = "0.4", package = "hybrid-array", features = ["extra-sizes"] }
array = { version = "0.4.7", package = "hybrid-array", features = ["extra-sizes"] }
num-traits = { version = "0.2", default-features = false }

# optional dependencies
Expand Down
4 changes: 2 additions & 2 deletions module-lattice/src/algebra.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::utils::Truncate;
use super::truncate::Truncate;

use array::{Array, ArraySize, typenum::U256};
use core::fmt::Debug;
Expand Down Expand Up @@ -80,7 +80,7 @@ macro_rules! define_field {
let product = x * Self::BARRETT_MULTIPLIER;
let quotient = product >> Self::BARRETT_SHIFT;
let remainder = x - quotient * Self::QLL;
Self::small_reduce($crate::utils::Truncate::truncate(remainder))
Self::small_reduce($crate::truncate::Truncate::truncate(remainder))
}
}
};
Expand Down
4 changes: 2 additions & 2 deletions module-lattice/src/encoding.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use array::{
Array,
Array, Flatten, Unflatten,
typenum::{Gcd, Gcf, Prod, Quot, U0, U8, U32, U256, Unsigned},
};
use core::fmt::Debug;
use core::ops::{Div, Mul, Rem};
use num_traits::One;

use super::algebra::{Elem, Field, NttPolynomial, NttVector, Polynomial, Vector};
use super::utils::{Flatten, Truncate, Unflatten};
use super::truncate::Truncate;

/// An array length with other useful properties
pub trait ArraySize: array::ArraySize + PartialEq + Debug {}
Expand Down
2 changes: 1 addition & 1 deletion module-lattice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,4 @@ pub mod encoding;

/// Utility functions such as truncating integers, flattening arrays of arrays, and unflattening
/// arrays into arrays of arrays.
pub mod utils;
pub mod truncate;
31 changes: 31 additions & 0 deletions module-lattice/src/truncate.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/// Safely truncate an unsigned integer value to shorter representation
pub trait Truncate<T> {
/// Truncate value to the width of `Self`.
fn truncate(x: T) -> Self;
}

macro_rules! define_truncate {
($from:ident, $to:ident) => {
impl Truncate<$from> for $to {
// Truncation should always function as intended here:
// - we ensure `$to` is small enough to infallibly convert to `$from` via the
// `$from::from($to::MAX)` conversion, which will fail if not widening.
// - we are deliberately masking to the smaller size, i.e. truncation is intentional
// (though that's not enough for `clippy` for some reason). Arguably the truncation
// of the `as` cast is sufficient, but this makes it explicit
#[allow(clippy::cast_possible_truncation)]
fn truncate(x: $from) -> $to {
(x & $from::from($to::MAX)) as $to
}
}
};
}

define_truncate!(u32, u16);
define_truncate!(u64, u16);
define_truncate!(u64, u32);
define_truncate!(u128, u8);
define_truncate!(u128, u16);
define_truncate!(u128, u32);
define_truncate!(usize, u8);
define_truncate!(usize, u16);
177 changes: 0 additions & 177 deletions module-lattice/src/utils.rs

This file was deleted.