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
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.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ debug = true

[patch.crates-io]
ml-kem = { path = "./ml-kem" }
module-lattice = { path = "./module-lattice" }
1 change: 1 addition & 0 deletions ml-kem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ hazmat = []

[dependencies]
array = { package = "hybrid-array", version = "0.4.4", features = ["extra-sizes", "subtle"] }
module-lattice = "0.1.0-pre.0"
kem = "0.3.0-rc.2"
rand_core = "0.10.0-rc-6"
sha3 = { version = "0.11.0-rc.3", default-features = false }
Expand Down
11 changes: 6 additions & 5 deletions ml-kem/src/algebra.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use array::{Array, typenum::U256};
use core::ops::{Add, Mul, Sub};
use module_lattice::util::Truncate;
use sha3::digest::XofReader;
use subtle::{Choice, ConstantTimeEq};

use crate::crypto::{PRF, PrfOutput, XOF};
use crate::encode::Encode;
use crate::param::{ArraySize, CbdSamplingSize};
use crate::util::{B32, Truncate};
use crate::util::B32;

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;
Expand Down Expand Up @@ -46,9 +47,9 @@ impl FieldElement {

fn barrett_reduce(x: u32) -> u16 {
let product = u64::from(x) * Self::BARRETT_MULTIPLIER;
let quotient = (product >> Self::BARRETT_SHIFT).truncate();
let quotient: u32 = Truncate::truncate(product >> Self::BARRETT_SHIFT);
let remainder = x - quotient * Self::Q32;
Self::small_reduce(remainder.truncate())
Self::small_reduce(Truncate::truncate(remainder))
}

// Algorithm 11. BaseCaseMultiply
Expand Down Expand Up @@ -176,7 +177,7 @@ impl<K: ArraySize> PolynomialVector<K> {
Eta: CbdSamplingSize,
{
Self(Array::from_fn(|i| {
let N = start_n + i.truncate();
let N = start_n + u8::truncate(i);
let prf_output = PRF::<Eta>(sigma, N);
Polynomial::sample_cbd::<Eta>(&prf_output)
}))
Expand Down Expand Up @@ -432,7 +433,7 @@ impl<K: ArraySize> NttVector<K> {
pub fn sample_uniform(rho: &B32, i: usize, transpose: bool) -> Self {
Self(Array::from_fn(|j| {
let (i, j) = if transpose { (j, i) } else { (i, j) };
let mut xof = XOF(rho, j.truncate(), i.truncate());
let mut xof = XOF(rho, Truncate::truncate(j), Truncate::truncate(i));
NttPolynomial::sample_uniform(&mut xof)
}))
}
Expand Down
8 changes: 4 additions & 4 deletions ml-kem/src/compress.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::algebra::{FieldElement, Integer, Polynomial, PolynomialVector};
use crate::param::{ArraySize, EncodingSize};
use crate::util::Truncate;
use module_lattice::util::Truncate;

// A convenience trait to allow us to associate some constants with a typenum
pub trait CompressionFactor: EncodingSize {
Expand Down Expand Up @@ -37,16 +37,16 @@ impl Compress for FieldElement {
fn compress<D: CompressionFactor>(&mut self) -> &Self {
const Q_HALF: u64 = (FieldElement::Q64 + 1) >> 1;
let x = u64::from(self.0);
let y = ((((x << D::USIZE) + Q_HALF) * D::DIV_MUL) >> D::DIV_SHIFT).truncate();
self.0 = y.truncate() & D::MASK;
let y = (((x << D::USIZE) + Q_HALF) * D::DIV_MUL) >> D::DIV_SHIFT;
self.0 = u16::truncate(y) & D::MASK;
Comment on lines -40 to +41
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case confused inference the most because it was using truncate twice (let y = (...).truncate(); self.0 = y.truncate() ...) which also seemed necessary before because there is no truncate impl that goes u64 => u16, which has been added to module-lattice.

D::MASK is a u16, so this seems correct.

self
}

// Equation 4.6: Decompress_d(x) = round((q / 2^d) x)
fn decompress<D: CompressionFactor>(&mut self) -> &Self {
let x = u32::from(self.0);
let y = ((x * FieldElement::Q32) + D::POW2_HALF) >> D::USIZE;
self.0 = y.truncate();
self.0 = Truncate::truncate(y);
self
}
}
Expand Down
4 changes: 2 additions & 2 deletions ml-kem/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ use array::{
Array,
typenum::{U256, Unsigned},
};
use module_lattice::util::Truncate;

use crate::algebra::{
FieldElement, Integer, NttPolynomial, NttVector, Polynomial, PolynomialVector,
};
use crate::param::{ArraySize, EncodedPolynomial, EncodingSize, VectorEncodingSize};
use crate::util::Truncate;

type DecodedValue = Array<FieldElement, U256>;

Expand Down Expand Up @@ -53,7 +53,7 @@ fn byte_decode<D: EncodingSize>(bytes: &EncodedPolynomial<D>) -> DecodedValue {

let x = u128::from_le_bytes(xb);
for (j, vj) in v.iter_mut().enumerate() {
let val: Integer = (x >> (D::USIZE * j)).truncate();
let val: Integer = Truncate::truncate(x >> (D::USIZE * j));
vj.0 = val & mask;

if D::USIZE == 12 {
Expand Down
25 changes: 0 additions & 25 deletions ml-kem/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,6 @@ use core::ptr;
/// A 32-byte array, defined here for brevity because it is used several times
pub type B32 = Array<u8, U32>;

/// Safely truncate an unsigned integer value to shorter representation
pub trait Truncate<T> {
fn truncate(self) -> T;
}

macro_rules! define_truncate {
($from:ident, $to:ident) => {
impl Truncate<$to> for $from {
fn truncate(self) -> $to {
// This line is marked unsafe because the `unwrap_unchecked` call is UB when its
// `self` argument is `Err`. It never will be, because we explicitly zeroize the
// high-order bits before converting. We could have used `unwrap()`, but chose to
// avoid the possibility of panic.
unsafe { (self & $from::from($to::MAX)).try_into().unwrap_unchecked() }
}
}
};
}

define_truncate!(u32, u16);
define_truncate!(u64, u32);
define_truncate!(usize, u8);
define_truncate!(u128, u16);
define_truncate!(u128, u8);

/// Defines a sequence of sequences that can be merged into a bigger overall seequence
pub trait Flatten<T, M: ArraySize> {
type OutputSize: ArraySize;
Expand Down
1 change: 1 addition & 0 deletions module-lattice/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ macro_rules! define_truncate {
}

define_truncate!(u32, u16);
define_truncate!(u64, u16);
define_truncate!(u64, u32);
define_truncate!(u128, u8);
define_truncate!(u128, u16);
Expand Down