Repository navigation
ML-DSA RISC-V: prepared keys for many signatures and verifications - #4383
Merged
Merged
Conversation
Member
Author
|
The On main, RISC-V's
Generated by Claude Code |
|
This pull request has conflicts with Posted by |
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
force-pushed
the
claude/mldsa-prepared-riscv64
branch
from
November 6, 2026 23:40
ef089c4 to
d5152f9
Compare
reaperhulk
enabled auto-merge
November 6, 2026 23:46
This was referenced Nov 7, 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.
Follows #4375 (AArch64) and #4366 (the spec). On RISC-V,
PreparedSigningKey{44,65,87}andPreparedVerifyingKey{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,execfor one operation per parameter set. Decoding the key and transforming it with the NTT takes this share of each operation: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)
Artifacts/MlDsaPreparedKey/RISCV64.leanregistersvg_mldsa*_prepare_{signing,verifying}_key, with 1072 bytes of stack.Artifacts/MlDsaSignMessage/RISCV64.leanandArtifacts/MlDsaVerifyMessageCached/RISCV64.leanaddvg_mldsa*_{sign,verify}_message_prepared, with 1184.vg_mldsa*_sign_with_cacheandvg_mldsa*_verify_with_cache. Each takes an optional cache slice and selects it ins3without a branch (cacheifcache_len ≠ 0, else 0).Impl/MlDsa/RISCV64/CacheCopy.lean), where the kernel would otherwise decode and transform it.vg_mldsa*_{sign,verify}and the internalvg_mldsa*_{sign,verify}_preparedare a 16-byte frame around a call of the kernel (PreparedKey/Entry.lean).withStackScratchWipedwipes afterwards. It is transformed withvg_mldsa_ntt, using the caller'sscratch, and copied after the key. Correctness and constant time are proven together with thePieceframework. Two runs whose pointers agree leak the same.sign_messageandverify_message_cachedproofs (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.TCB/,Spec/orProof/Framework/changes. The internal contracts are the existing untrusted ones inProof/MlDsa/PreparedKeyContract.lean.Rust
src/mldsa_common.rs: the prepared paths'cfg(target_arch = "aarch64")becomesany(aarch64, riscv64). The_sha3match arms stay AArch64-only, so the match onBackendstays exhaustive. RISC-V has no ML-DSA variants, sobackend()'sREQUIREDlist is unchanged there.Performance (RISC-V)
Benchmarks run, scoped to
arches=riscv64, modulesmldsa44 mldsa65 mldsa87, and the sign/verify/prepare groups, compared against main9ff6f96c. It's one short pass of 25 samples, so the numbers are indicative.prepare/verifyingnow 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/signingtimes key generation plus preparation together.preparegroups because of their threshold; see the comment below.Validation
Emit.leanruns clean (axiom, compiled-code and spec-origin audits) and changes onlycrates/verified-garbage-asm-riscv64.check_lean_public_imports.py --base origin/mainpasses on the edited modules.qemu-riscv64, the ML-DSA ACVP and Wycheproof tests pass (24, includingprepared,sign_seed_preparedandverify_prepared, which now take the new path), both by default and withVG_CPU_FEATURES=none.cargo fmt --checkis clean, and so iscargo clippy --all-targets -D warnings, on the host and forriscv64gc-unknown-linux-gnu.🤖 Generated with Claude Code
https://claude.ai/code/session_015ajQWuEfjMr9yebNZeGk2z