Skip to content

Accumulate ML-KEM NTT dot products before Montgomery reduction - #2573

Open
carstenartur wants to merge 1 commit into
bcgit:mainfrom
carstenartur:codex/upstream-mlkem-montgomery
Open

carstenartur wants to merge 1 commit into
bcgit:mainfrom
carstenartur:codex/upstream-mlkem-montgomery

Conversation

@carstenartur

Copy link
Copy Markdown
Contributor

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 Montgomery (1985)

Developed with assistance from OpenAI Codex.

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

Copy link
Copy Markdown
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.

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.

3 participants