Skip to content

Specify optional ML-DSA prepared key polynomial caches - #3806

Closed
reaperhulk wants to merge 3 commits into
mainfrom
codex/mldsa-prepared-key-specs
Closed

reaperhulk wants to merge 3 commits into
mainfrom
codex/mldsa-prepared-key-specs

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Oct 31, 2026 •

Copy link
Copy Markdown
Member

ML-DSA signing and verification currently decode and transform the same key polynomials for every operation. This adds contracts for optional prepared keys containing the ordinary encoded key followed by canonical NTT coefficients. It adds no implementation, registration, generated assembly, Rust API or change to existing key types.

This is a trusted specification change, intentionally separate from its eventual implementation. Public caches represent NTT(t1 * 2^d); secret caches represent NTT(s1), NTT(s2) and NTT(t0). Each cache is constrained to the encoded prefix with canonical field coefficients. Preparation copies that prefix and derives the cache. Signing/verification preserve the existing mathematical results, bounded failure behavior, message/context handling and permitted leakage. Message verification also preserves the existing tr = H(pk) requirement. No expanded matrix or new ISA semantics are introduced.

The intended Rust API now keeps the existing signing and verification methods, with optional internal caches prepared automatically on reuse when allocation is enabled. Allocation-free builds retain the ordinary path. This specification does not dictate the promotion policy or change any existing Rust type. Cached secret coefficients require destruction along with the encoded private key. The preparer and operation signatures preserve disjointness and writable-buffer obligations through Sig.contract.

An unproved, Lean-printer-generated prototype establishes the performance motivation; it is not included in this PR and is not a verified implementation. All measurements compare production base 3d37034d64a308e5d18de2908d95013cdef5e943 with prototype d065a0607 on bench/arm64-mldsa-key-cache. The prototype retains ordinary timed scratch clearing and private-key cache zeroization.

M1 Max operation Base Prepared NTT cache Latency reduction
ML-DSA-44 sign 42.499 µs 39.518 µs 7.01%
ML-DSA-44 verify 13.738 µs 12.654 µs 7.89%
ML-DSA-65 sign 64.904 µs 59.894 µs 7.72%
ML-DSA-65 verify 21.721 µs 20.234 µs 6.85%
ML-DSA-87 sign 81.736 µs 75.348 µs 7.82%
ML-DSA-87 verify 36.074 µs 34.049 µs 5.61%

M1 values are medians of three interleaved run means (50 samples, 0.3 s warmup, 1.5 s measurement per case, 4000 resamples; Rust 1.99.0/LLVM 23.1.1, detected SHA3). Decoded-only caching reduced message signing/verification latency by about 2–3.5%, using the same cache space. NTT-ready caching is therefore the proposed representation. Fixed-μ signing also improves, but its rejection-sampling workload differs from message signing.

Scoped native AArch64 run on Neoverse N2 (CPU part 0xd49) measured default signing reductions of 5.7/6.4/7.4% and verification reductions of 6.1/5.6/4.5%, for 44/65/87. Forced-scalar reductions were 3.2/7.2/4.5% signing and 6.1/5.4/4.1% verification. This is a single short interleaved A/B pass, 25 samples, 0.1 s warmup/0.3 s measurement; it is weaker repeatability evidence than the M1 runs. This specification-only PR changes no emitted code and needs no additional timing run.

The run failed its slowdown guard: eagerly preparing both keys made key generation/construction/destruction 37.4–53.4% slower on N2. M1 creation+drop rose from 14.754/25.030/40.458 µs to 23.862/39.864/59.447 µs. Those are real costs, motivating an opt-in API for reused keys; the threshold was not changed and the run was not repeated. Key-loading latency was not measured separately.

Public polynomial caches cost 4/6/8 KiB and secret polynomial caches 12/17/23 KiB. The measurement prototype embeds a prepared public key inside its signing key, so its total signing-key growth is 16/23/31 KiB; a production API can prepare those independently. The benchmark sizes and construction costs must not be interpreted as a final production API footprint.

Both prototype representations passed the two ML-DSA unit and 15 published ACVP/Wycheproof integration tests in detected-SHA3 and forced-scalar configurations. These tests do not prove the new cache contracts.

Validation after rebase onto main 4170a1cf4:

  • Full Lean build passes (27,741 jobs), using the published cache for ddb0ab2fd (main has only CI changes beyond that revision).
  • Full Emit.lean --check passes; emitted files unchanged. Standard-axiom, compiler-override and spec-origin audits pass.
  • Public-import audit passes for the new module.
  • All static workflow check scripts pass except the existing CI-tool unit-test suite on this local environment. That suite ran 592 tests across 36 modules; four modules fail: executable detection expects a thin executable but macOS /bin/sh is universal; archive tests require GNU tar's --concatenate (local tar is BSD); subprocess tests invoke the system Python without tomllib; the Lean-installed import integration fixture redirects check_lean_imports.LEAN but not lean_shards.LEAN, producing missing fixture-path lookups. None of those files are changed here. These failures are retained rather than reported as a green unit-test suite. Linux CI remains the validation for its supported environment.
  • git diff --check passes. No runtime source changes or measurement artifacts are included.

Automatic promotion follow-up (unproved prototype)

The user requested a single API with automatic promotion instead of separate prepared-key types. The experiment on codex/mldsa-auto-cache-experiment retains the existing methods, prepares signing and verification caches independently on second use, publishes each immutable cache once, and lets concurrent callers fall back while preparation runs. Allocation failure preserves the ordinary path; clones start cold; secret caches are wiped on drop. The ordinary operations and prepared operations remain separate contracts, so no specification change is needed for that Rust policy.

M1 Max measurements now time complete lifetimes across 32 fixed-key fixtures, using deterministic signing and valid verification. Three interleaved passes each report medians of 16 samples. The second-use policy breaks even at about four operations: one use is essentially unchanged; exactly two uses cost about 3–4%; eight uses save about 3–5%; 100 uses save about 5–9%. These are controlled lifetime workloads, distinct from the earlier eager-cache per-operation table.

The automatic prototype adds 16 bytes of metadata to a public key, or 32 bytes to a signing key including its embedded public key. With the current contiguous contracts, its heap cache also duplicates the encoded prefix: roughly 5–11 KiB for a verifier and 15–28 KiB for a signer, plus allocator overhead. No heap cache is allocated on first use. The prototype duplicates kernel bodies and adds about 80 KiB of executable text in the all-three-parameter lifetime binary; production should investigate sharing those bodies.

Publication/failure/panic/concurrent-reader tests pass. A test allocator observes zero secret bytes immediately before cache deallocation. Published ACVP/Wycheproof vectors are repeated across promotion and pass under detected SHA3 and forced scalar, and allocation-free builds/tests pass. These remain tests of unproved code, not proofs of these contracts.

The initial N2 automatic-cache experiment also broke even at about four operations, but warm four-thread signing regressed for ML-DSA-44 and ML-DSA-65 (3.4% and 9.8%). Those results are retained: https://github.com/pyca/verified-garbage/actions/runs/41225088645. The 64-byte-aligned follow-up retained the M1 lifetime gains: at eight uses, 3.4–4.6% saved; at 100 uses, 5.3–8.6%. Native N2 results also retained the approximately four-operation crossover, with 2.7–3.5% saved at eight uses and 4.4–6.8% at 100 uses. One use remained approximately flat; exactly two uses cost about 3–4%. These are single-thread complete-lifetime measurements, not steady-state single-call timings.

The N2 follow-up changed warm four-thread ML-DSA-65 signing from a 9.8% regression to a 4.3% improvement, but shared-key signing remains mixed: cold ML-DSA-65 was 3.4% slower and warm ML-DSA-44 was 0.5% slower. Timing variation is retained in the raw three-pass results; these rows are not dismissed as noise or claimed as universal improvements. More controlled shared-key throughput measurements are needed before production. Four-thread verification improved in every follow-up row. Follow-up run: https://github.com/pyca/verified-garbage/actions/runs/41225930954; candidate e5547bb50. No runtime code or measurement artifacts are included in this specification PR.

Shared implementation interface

Rebased onto main 83ebe70221e47f7c32ceb9693f45a6f11f814383, including the move of generated assembly into architecture crates. The implementation is being developed in a separate PR stacked on this specification change.

This revision also specifies private common sign/verify kernels accepting an optional read-only polynomial-cache slice. A zero-length slice selects the existing decode/transform path. A present cache has the exact polynomial count and must correspond to the same encoded key, with canonical coefficients. Results, writable-buffer restrictions and permitted leakage remain those of the existing signing/verification contracts. The choice is public through the slice length. This lets ordinary and prepared entry points share their large kernels; it does not expose a new public Rust API or prescribe promotion policy.

A second unproved Lean-generated prototype restores cached coefficients into the existing workspace before the existing arithmetic. In three interleaved M1 Max complete-lifetime passes it retains 3.1–4.1% savings at eight operations and 5.1–8.8% at 100 operations across the three parameter sets and both operations. Exactly two operations cost 3.4–4.5% more. Added executable TEXT in the all-parameter lifetime binary falls from 80 KiB to 16 KiB. Accelerated and forced-scalar published vectors pass. These results motivate sharing the kernels; they are not a claim that the implementation proofs are complete.

@reaperhulk
reaperhulk added this pull request to stack #3878 November 1, 2026 14:04
@reaperhulk
reaperhulk force-pushed the codex/mldsa-prepared-key-specs branch from 6ad5eb1 to 3eeb18a Compare November 1, 2026 14:06
@reaperhulk reaperhulk closed this Nov 6, 2026
reaperhulk pushed a commit that referenced this pull request Nov 6, 2026
vg_mldsa*_prepare_signing_key and vg_mldsa*_prepare_verifying_key copy an
encoded key and append the NTTs of its key polynomials (NTT(s1), NTT(s2),
NTT(t0) for a private key; NTT(t1 * 2^d) for a public key), stored as
canonical u32 coefficients (PolyStored). vg_mldsa*_sign_message_prepared
and vg_mldsa*_verify_message_prepared take such a prepared key in place of
the encoded key, with the results, failures and permitted leakage of
vg_mldsa*_sign_message and vg_mldsa*_verify_message_cached. A batch API
prepares a key once and signs or verifies many messages with it.

Adapted from the specification of #3806, with only the
functions the Rust code will call.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWy42drMfWKyQXeWwK8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant