Skip to content

ML-DSA RISC-V: prepared keys for many signatures and verifications - #4383

Merged
reaperhulk merged 1 commit into
mainfrom
claude/mldsa-prepared-riscv64
Nov 7, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/mldsa-prepared-riscv64

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Follows #4375 (AArch64) and #4366 (the spec). On RISC-V, PreparedSigningKey{44,65,87} and PreparedVerifyingKey{44,65,87} now call verified prepared-key functions instead of falling back to the ordinary ones.

Is it worth it?

Before implementing, I counted dynamic instructions under qemu-riscv64 -d in_asm,exec for one operation per parameter set. Decoding the key and transforming it with the NTT takes this share of each operation:

44 65 87
sign 6.2% 4.0% 7.9%
verify 7.0% 6.3% 4.9%

That is an NTT at about 18.4k instructions per polynomial. Copying a cached polynomial byte by byte costs about 6.1k instructions per 1024 bytes, so the expected net saving is about 3–5.6%. I judged that worth doing.

Verified code (RISC-V)

  • Registered entries.
    • The new registration file Artifacts/MlDsaPreparedKey/RISCV64.lean registers vg_mldsa*_prepare_{signing,verifying}_key, with 1072 bytes of stack.
    • Artifacts/MlDsaSignMessage/RISCV64.lean and Artifacts/MlDsaVerifyMessageCached/RISCV64.lean add vg_mldsa*_{sign,verify}_message_prepared, with 1184.
  • Shared kernels. As on AArch64, the ordinary and prepared entries share one kernel per parameter set: the internal functions vg_mldsa*_sign_with_cache and vg_mldsa*_verify_with_cache. Each takes an optional cache slice and selects it in s3 without a branch (cache if cache_len ≠ 0, else 0).
    • A prepared key is a byte array, and the RISC-V model faults on misaligned accesses. So a cached polynomial is copied a byte at a time into the kernel's aligned working space (Impl/MlDsa/RISCV64/CacheCopy.lean), where the kernel would otherwise decode and transform it.
    • The entries vg_mldsa*_{sign,verify} and the internal vg_mldsa*_{sign,verify}_prepared are a 16-byte frame around a call of the kernel (PreparedKey/Entry.lean).
  • Preparation. Each polynomial is decoded into a 1072-byte frame on the stack, which withStackScratchWiped wipes afterwards. It is transformed with vg_mldsa_ntt, using the caller's scratch, and copied after the key. Correctness and constant time are proven together with the Piece framework. Two runs whose pointers agree leak the same.
  • Message functions. The sign_message and verify_message_cached proofs (Proof/MlDsa/RISCV64/Message/) are generalized over an encoded or prepared key (q : Bool): the key region's length, the prepared-key precondition, and the callee's contract (signEK/verifyEK). The prepared message functions reuse those proofs.
  • Satisfiability. The prepared contracts' preconditions are shown satisfiable from a memory that the verified prepare function itself produced.
  • Trust. Nothing in TCB/, Spec/ or Proof/Framework/ changes. The internal contracts are the existing untrusted ones in Proof/MlDsa/PreparedKeyContract.lean.

Rust

src/mldsa_common.rs: the prepared paths' cfg(target_arch = "aarch64") becomes any(aarch64, riscv64). The _sha3 match arms stay AArch64-only, so the match on Backend stays exhaustive. RISC-V has no ML-DSA variants, so backend()'s REQUIRED list is unchanged there.

Performance (RISC-V)

Benchmarks run, scoped to arches=riscv64, modules mldsa44 mldsa65 mldsa87, and the sign/verify/prepare groups, compared against main 9ff6f96c. It's one short pass of 25 samples, so the numbers are indicative.

main this PR
44 sign (deterministic), prepared 1.17 ms 1.08 ms 7.3% faster
65 sign (deterministic), prepared 1.92 ms 1.66 ms 13.7% faster
87 sign (deterministic), prepared 2.34 ms 2.28 ms 2.6% faster
44 verify, prepared 326.21 µs 302.25 µs 7.3% faster
65 verify, prepared 541.77 µs 505.79 µs 6.6% faster
87 verify, prepared 905.89 µs 863.90 µs 4.6% faster
  • Ordinary methods.
    • Verification is within 1.2% of main.
    • Deterministic signing moves both ways (44: 4.8% slower, 65: 9.9% faster, 87: 1.5% slower). That is noise in a single short pass: the ordinary path adds only a few register instructions to select the empty cache.
  • Preparation cost.
    • prepare/verifying now does the decoding and NTTs: 35 / 53 / 70 µs, against a move of the key on main. It pays for itself after about 1.5 verifications.
    • prepare/signing times key generation plus preparation together.
    • The run's comparison job fails on these prepare groups because of their threshold; see the comment below.

Validation

  • Lean.
    • Every new and edited module builds.
    • The full Emit.lean runs clean (axiom, compiled-code and spec-origin audits) and changes only crates/verified-garbage-asm-riscv64.
    • check_lean_public_imports.py --base origin/main passes on the edited modules.
    • Every Static checks step passes locally.
  • Rust.
    • Under qemu-riscv64, the ML-DSA ACVP and Wycheproof tests pass (24, including prepared, sign_seed_prepared and verify_prepared, which now take the new path), both by default and with VG_CPU_FEATURES=none.
    • cargo fmt --check is clean, and so is cargo clippy --all-targets -D warnings, on the host and for riscv64gc-unknown-linux-gnu.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWuEfjMr9yebNZeGk2z

Copy link
Copy Markdown
Member Author

The Compare with base (riscv64) job of the manual Benchmarks run fails on the mldsa*_prepare groups. That is the expected effect of this PR, not a regression.

On main, RISC-V's prepare() only moved the key into the prepared type (161–278 ns). Now it decodes the key and computes its NTTs once, which is the work this PR moves out of every signature and verification.

main this PR
44 sign (deterministic), prepared 1.17 ms 1.08 ms 7.3% faster
65 sign (deterministic), prepared 1.92 ms 1.66 ms 13.7% faster
87 sign (deterministic), prepared 2.34 ms 2.28 ms 2.6% faster
44 verify, prepared 326.21 µs 302.25 µs 7.3% faster
65 verify, prepared 541.77 µs 505.79 µs 6.6% faster
87 verify, prepared 905.89 µs 863.90 µs 4.6% faster
44 / 65 / 87 verify, ordinary 0.3% / 0.3% / 1.2% faster
44 / 65 / 87 sign (deterministic), ordinary 4.8% slower / 9.9% faster / 1.5% slower
44 / 65 / 87 prepare/verifying 161 / 225 / 278 ns 34.8 / 53.1 / 69.9 µs
44 / 65 / 87 prepare/signing (key generation + preparation) 355 / 581 / 975 µs 464 / 733 / 1190 µs
  • Preparing a public key pays for itself after about 1.5 verifications: it costs 35–70 µs and saves 24–42 µs on each one.
  • The ordinary signing deltas are noise. They point both ways (9.9% faster on 65, 4.8% slower on 44), and this is one short pass of 25 samples. The ordinary path adds only a few register instructions to select the empty cache, and the verification deltas, which take the same path, are within ±1.2%.

Generated by Claude Code

@github-actions

github-actions Bot commented Nov 6, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after f63cc42 landed on main: rebase it to resolve them. Before rebasing, see CLAUDE.md on checking how much Lean main changed, and on restacking a stacked pull request.

Posted by .github/workflows/conflicts.yml, once per push to a conflicting pull request.

On RISC-V, vg_mldsa*_prepare_signing_key and vg_mldsa*_prepare_verifying_key
copy the encoded key and append its polynomials (NTT(s1), NTT(s2), NTT(t0);
NTT(t1 * 2^d)), and vg_mldsa*_sign_message_prepared and
vg_mldsa*_verify_message_prepared sign and verify with them, as on AArch64.

* The ordinary and prepared entries share one kernel per parameter set,
  the internal vg_mldsa*_sign_with_cache and vg_mldsa*_verify_with_cache,
  which take an optional cache of the key's polynomials. The prepared key
  is bytes and need not be aligned, and the RISC-V model faults on
  misaligned accesses, so the kernels copy a cached polynomial a byte at a
  time into their aligned working space.
* The prepare functions decode each polynomial into a 1072-byte frame on
  the stack (withStackScratchWiped, which wipes it), transform it with
  vg_mldsa_ntt and copy it after the key.
* The message-level proofs (sign_message, verify_message_cached) are
  generalized over an encoded or prepared key, so the prepared message
  functions reuse them.
* PreparedSigningKey and PreparedVerifyingKey use the prepared functions on
  riscv64 too.

Nothing in TCB/, Spec/ or Proof/Framework/ changes.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWuEfjMr9yebNZeGk2z
@reaperhulk
reaperhulk force-pushed the claude/mldsa-prepared-riscv64 branch from ef089c4 to d5152f9 Compare November 6, 2026 23:40
@reaperhulk
reaperhulk enabled auto-merge November 6, 2026 23:46
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 6, 2026
Merged via the queue into main with commit 8b31da4 Nov 7, 2026
35 checks passed
@reaperhulk
reaperhulk deleted the claude/mldsa-prepared-riscv64 branch November 7, 2026 00:01
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.

2 participants