Skip to content

ml-kem: use module_lattice::util::Truncate#202

Merged
tarcieri merged 1 commit intomasterfrom
ml-kem/use-module-lattice-truncate
Jan 28, 2026
Merged

ml-kem: use module_lattice::util::Truncate#202
tarcieri merged 1 commit intomasterfrom
ml-kem/use-module-lattice-truncate

Conversation

@tarcieri
Copy link
Copy Markdown
Member

Replaces the Truncate trait and its impls in the ml-kem crate with the one from the module-lattice crate.

As this is the very first integration, it adds an initial dependency on module-lattice in ml-kem.

The trait has a slightly different shape than before, being defined as a static method on the $to type as opposed to accepting a self parameter. This commit mostly followed the convention in the ml-dsa crate of using Truncate::truncate and inferring the types, but in some cases it was necessary to use an explicit u*::truncate because inference wasn't able to figure things out.

Replaces the `Truncate` trait and its impls in the `ml-kem` crate with
the one from the `module-lattice` crate.

As this is the very first integration, it adds an initial dependency on
`module-lattice` in `ml-kem`.

The trait has a slightly different shape than before, being defined as a
static method on the `$to` type as opposed to accepting a `self`
parameter. This commit mostly followed the convention in the `ml-dsa`
crate of using `Truncate::truncate` and inferring the types, but in some
cases it was necessary to use an explicit `u*::truncate` because
inference wasn't able to figure things out.
@tarcieri tarcieri requested a review from bifurcation January 28, 2026 22:42
Comment thread ml-kem/src/compress.rs
Comment on lines -40 to +41
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;
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.

@tarcieri tarcieri merged commit df1d9b5 into master Jan 28, 2026
44 checks passed
@tarcieri tarcieri deleted the ml-kem/use-module-lattice-truncate branch January 28, 2026 22:48
@tarcieri tarcieri mentioned this pull request Apr 28, 2026
tarcieri added a commit that referenced this pull request Apr 28, 2026
## Added
- `Seed` support e.g. `DecapsulationKey::from_seed` (#133, #138)
- PKCS#8 support (#135)
- `KeyInit`, `KeySizeUser`, and `KeyExport` impls for decapsulation keys
  (#156, #228)
- Parameter set modules: `ml_kem_512`, `mk_kem_768`, `mk_kem_1024`
  (#162)
- `DecapsulationKey::from_expanded` deprecated compatibility support
  (#163)
- `TryKeyInit` and `KeyExport` impls for encapsulation keys (#188)
- Validations against Wycheproof test vectors (#213, #214, #215,
  #217)
- Implement `kem::Kem` trait (#223)
- Support for `kem::FromSeed` trait (#255)

## Changed
- Edition changed to 2024 and MSRV bumped to 1.85 (#118)
- Relax MSRV policy and allow MSRV bumps in patch releases
- Upgrade `hybrid-array` dependency to 0.4 (#129)
- Extract `module-lattice` crate (#199, #202, #204, #209,
  #210, #211, #212, #218, #219, #220)
- Replace `EncodedSizeUser` with `ExpandedKeyEncoding` (#226)
- Bump `getrandom` to v0.4 (#245)
- Bump `rand_core` to v0.10 (#245)
- Migrate from `subtle` to `ctutils` (#277)
- Bump `sha3` dependency to v0.11 (#282)
- Bump `kem` dependency to v0.3 (#283)
- Bump `pkcs8` dependency to v0.11 (#291)

## Fixed
- Validate encryption/encapsulation keys (#179)
- Validate expanded decapsulation key hash (#207)

## Removed
- `Kem` struct and `KemCore` trait - replaced by `kem::Kem` (#223)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant