Repository navigation
Accumulate ML-KEM NTT dot products before Montgomery reduction - #2573
Open
carstenartur wants to merge 1 commit into
Open
carstenartur wants to merge 1 commit into
carstenartur wants to merge 1 commit into
Conversation
Accumulate the products across each K-row ML-KEM vector dot before reducing. This uses three Montgomery reductions per two-coefficient block instead of 5K (384 per polynomial dot instead of 1,280/1,920/2,560) and removes one temporary polynomial and its `short[256]` per dot. ### Why this is equivalent For each block let `A00 = sum(a0*b0)`, `A11 = sum(a1*b1)`, and `Across = sum(a0*b1 + a1*b0)`, summing over K rows. Write `M(x) = x*R^-1` modulo `q`, with `q = 3329` and `R = 2^16`. Since the stored signed `zeta = gamma*R` modulo `q`, `v = M(A11)`, `even = M(A00 + zeta*v)`, and `odd = M(Across)` represent `(A00 + gamma*A11)/R` and `Across/R` modulo `q`. The existing zeta sign order and final Barrett reductions are retained. For `K <= 4`, decoded first operands lie in `[0,4095]`, including noncanonical keys; post-NTT second operands lie in `[0,3329]`. Diagonal sums are at most 54,529,020 and the cross sum at most 109,058,040. New accumulations and reduction numerators fit `int`; results fit `short`. The four engine callers use independent outputs, and their later normalization preserves encoded results. New branches and array indices depend only on public parameters and coefficient indices. ### Validation and measurements Recompiled the complete core on this upstream-based branch and passed all 21 ML-KEM and internal tests on JDK 21, including boundary, noncanonical-input and input-preservation tests. On the upstream bb81bfa baseline with OpenJDK 21, paired median encapsulation CPU time fell 6.6%/4.0%/7.4% for 512/768/1024. Allocation fell by 1,056-3,168 bytes per complete operation. Single-host timings vary; the study includes all operations, controls and reproduction commands. [Detailed proof and measurements](https://github.com/carstenartur/bc-java/blob/475def1f62cc4ada95dfc6913dbff449f7b7fafe/scripts/bench/montgomery-study.md) [Montgomery (1985)](https://doi.org/10.1090/S0025-5718-1985-0777282-X) Developed with assistance from OpenAI Codex.
Collaborator
|
Thanks for the PRs and the detailed write-up. They overlap with work we already have in progress on the ML-KEM/ML-DSA NTT code, so we'll look at them together with that and come back to you here. |
Kamil-0425
approved these changes
Nov 22, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Accumulate the products across each K-row ML-KEM vector dot before reducing. This uses three Montgomery reductions per two-coefficient block instead of 5K (384 per polynomial dot instead of 1,280/1,920/2,560) and removes one temporary polynomial and its
short[256]per dot.Why this is equivalent
For each block let
A00 = sum(a0*b0),A11 = sum(a1*b1), andAcross = sum(a0*b1 + a1*b0), summing over K rows. WriteM(x) = x*R^-1moduloq, withq = 3329andR = 2^16. Since the stored signedzeta = gamma*Rmoduloq,v = M(A11),even = M(A00 + zeta*v), andodd = M(Across)represent(A00 + gamma*A11)/RandAcross/Rmoduloq. The existing zeta sign order and final Barrett reductions are retained.For
K <= 4, decoded first operands lie in[0,4095], including noncanonical keys; post-NTT second operands lie in[0,3329]. Diagonal sums are at most 54,529,020 and the cross sum at most 109,058,040. New accumulations and reduction numerators fitint; results fitshort. The four engine callers use independent outputs, and their later normalization preserves encoded results. New branches and array indices depend only on public parameters and coefficient indices.Validation and measurements
Recompiled the complete core on this upstream-based branch and passed all 21 ML-KEM and internal tests on JDK 21, including boundary, noncanonical-input and input-preservation tests.
On the upstream bb81bfa baseline with OpenJDK 21, paired median encapsulation CPU time fell 6.6%/4.0%/7.4% for 512/768/1024. Allocation fell by 1,056-3,168 bytes per complete operation. Single-host timings vary; the study includes all operations, controls and reproduction commands.
Detailed proof and measurements Montgomery (1985)
Developed with assistance from OpenAI Codex.