Skip to content

Reduce ML-DSA matrix-row dot products once per coefficient - #2574

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

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

Conversation

@carstenartur

Copy link
Copy Markdown
Contributor

Accumulate each ML-DSA NTT matrix-row dot product coefficient in a long and apply one Montgomery reduction. For L = 4, 5, or 7, this lowers the count per row from 1,024/1,280/1,792 to 256, with no additional allocations.

Why this is equivalent

Write M(x) = x*R^-1 modulo q, with q = 8380417 and R = 2^32. For products p_i, sum(M(p_i)) = sum(p_i)*R^-1 = M(sum(p_i)) modulo q, so the Montgomery scale is preserved.

Across the four engine callers, matrix entries lie in [0,q-1] and transformed vector coefficients have magnitude at most 34,162,429, including malformed eta encodings. Thus abs(sum(p_i)) <= 7*(q-1)*34,162,429 < q*2^31. The accumulation and Montgomery numerator fit long, and the result fits int. The callers use independent outputs. Existing reduction, inverse NTT and final canonicalization preserve packed outputs, rounding and verification decisions. The optional terms depend only on public L; no secret-dependent branch or address is added.

Validation and measurements

Recompiled the complete core on this upstream-based branch and passed all 28 ML-DSA, Wycheproof and internal tests on JDK 21. Differential tests cover every parameter set, endpoints and canonical results after inverse NTT; a malformed eta regression checks key derivation.

Against upstream bb81bfa on OpenJDK 21, median kernel CPU time fell from 2.36/2.76/4.51 us to 1.83/1.66/2.27 us for 44/65/87. Full-operation timings were mixed: ML-DSA-44 keygen was +6.5% CPU in the primary run and +1.5% in a longer follow-up. Allocation was unchanged. Single-host timings are not a uniform speedup claim.

Detailed proof and measurements Montgomery (1985) Dilithium v3.1, section 5.6

Developed with assistance from OpenAI Codex.

Accumulate each ML-DSA NTT matrix-row dot product coefficient in a `long` and apply one Montgomery reduction. For `L = 4, 5, or 7`, this lowers the count per row from 1,024/1,280/1,792 to 256, with no additional allocations.

### Why this is equivalent
Write `M(x) = x*R^-1` modulo `q`, with `q = 8380417` and `R = 2^32`. For products `p_i`, `sum(M(p_i)) = sum(p_i)*R^-1 = M(sum(p_i))` modulo `q`, so the Montgomery scale is preserved.

Across the four engine callers, matrix entries lie in `[0,q-1]` and transformed vector coefficients have magnitude at most 34,162,429, including malformed eta encodings. Thus `abs(sum(p_i)) <= 7*(q-1)*34,162,429 < q*2^31`. The accumulation and Montgomery numerator fit `long`, and the result fits `int`. The callers use independent outputs. Existing reduction, inverse NTT and final canonicalization preserve packed outputs, rounding and verification decisions. The optional terms depend only on public `L`; no secret-dependent branch or address is added.

### Validation and measurements
Recompiled the complete core on this upstream-based branch and passed all 28 ML-DSA, Wycheproof and internal tests on JDK 21. Differential tests cover every parameter set, endpoints and canonical results after inverse NTT; a malformed eta regression checks key derivation.

Against upstream bb81bfa on OpenJDK 21, median kernel CPU time fell from 2.36/2.76/4.51 us to 1.83/1.66/2.27 us for 44/65/87. Full-operation timings were mixed: ML-DSA-44 keygen was +6.5% CPU in the primary run and +1.5% in a longer follow-up. Allocation was unchanged. Single-host timings are not a uniform speedup claim.

[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)
[Dilithium v3.1, section 5.6](https://pq-crystals.org/dilithium/data/dilithium-specification-round3-20210208.pdf)

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.

2 participants