Skip to content

RISC-V AES-GCM: open_to decrypts from src to dst in one pass - #4555

Merged
reaperhulk merged 1 commit into
mainfrom
claude/gcm-open-to-one-pass-riscv64
Nov 7, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/gcm-open-to-one-pass-riscv64

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

vg_aes_gcm_open_to on RISC-V no longer copies src to dst and decrypts there in place (by a call of vg_aes_gcm_open). It is now vg_aes_gcm_open's body out of place (openWith true): it hashes the ciphertext at src and, if the tag is right, decrypts it from src straight to dst, with no copy on any path. If the tag or its length is wrong, it zeroes dst a byte at a time (wipe) and returns 0 (openToPost). As on x86-64 (#4388, #4346, #4390); the proof follows ChaCha20-Poly1305 on RISC-V (#4384).

One body, one proof

The in-place and out-of-place code share one body and one proof each, generic in o : Bool. The in-place definitions are one-line instances (… := …With false), and the old in-place proofs were replaced rather than kept beside the new ones.

  • AES counter mode (Impl/Aes/RISCV64/Ctr32.lean): ctr32With o. vg_aes_ctr32 is o = false. A new internal function, vg_aes_ctr32_to (o = true, source in a6), reads from the source and writes to the destination. Out of place, the source is held in ra and our return address in a scratch slot, since every other register is in use. Doublewords are used only when both addresses are multiples of 8. Proved once: correctWith.
  • GCM crypt (Impl/AesGcm/RISCV64.lean): xorLoopWith o, cryptHeadWith/cryptWholeWith/cryptTailWith/cryptFillWith/cryptWith o. Out of place, s9 holds dst - src (stored by the entry at W + dstO and loaded once, after the tag comparison, by deltaArg), and each destination address is computed from the source pointer. Proved once each (xorLoopWith_ok, crypt_ok, and the generic constant-time proofs in CTCrypt.lean), over Alias o S D n (S = D in place, disjoint out of place).
  • open's body: openGoodWith o and openWith o («open» := openWith false, openTo := openWith true). Proved once: open_body / open_good, openGood_ct.

The only paired items are what each emitted function needs on its own: a taint_decide check per function (ctr32_ct / ctr32To_ct) and a call wrapper per callee contract (ctr_call / ctrTo_call).

vg_aes_gcm_open's and vg_aes_gcm_seal's emitted code is byte-identical. In the generated crate, vg_aes_gcm_open_to changes and internal/vg_aes_ctr32_to is new. The rest of the diff in aes.rs/gcm.rs is only where the emitter places the shared internal modules (symbol paths).

The contract and the frame

  • Proof/AesGcm/RISCV64/OpenTo/Fn.lean: openToRISCV64, with the working space as a fifth stack argument, and its correctness (openTo_wp, including wipe_ok).
  • OpenTo/CT.lean: constant time. It branches only on public lengths and on whether the tag is right (openToLeak).
  • OpenTo/Verified.lean: Verified against Sig.scratchContract … openToSig "work" .u64 320 … 2048 (some openToLeak), then inside the frame withStackScratchArgs 2608 4 (Verified.stackScratchArgs), against openToRoundsContract RISCV64.abi 4656. This needs the locality lemmas openToPre_local, openToPost_local and openToLeak_local, which reuse Proof/AesGcm/Scratch.lean's openResult_local.
  • Registration (Artifacts/AesGcm/RISCV64.lean): stack 4672 → 4656.

No Spec/ or TCB/ changes. No shared files outside the RISC-V AES/AES-GCM Impl/ and Proof/ files, the registration and the generated crates/verified-garbage-asm-riscv64/.

Checks run

  • Full lake build --log-level=warning, lake env lean --run Emit.lean and Emit.lean --check, on the restored cache of main at c569192.
  • Every step of the Static checks job (check_lean_single_importer.py --base origin/main).
  • ci/check_lean_public_imports.py --base origin/main -m <changed modules>.
  • cargo fmt --check; cargo clippy --all-targets -- -D warnings on the host and on riscv64gc-unknown-linux-gnu.
  • cargo test gcm for riscv64gc-unknown-linux-gnu under qemu-riscv64, with Wycheproof (unit tests including out_of_place, CAVP decrypt_to, Wycheproof AES-GCM): all pass.

Benchmark

Run 42420666804 (riscv64, aes_gcm, aes-128-gcm-decrypt-out-of-place, base c569192). The table gives the median over the run's 8 base and 8 head copies of each copy's median time, in ns:

bytes base (ns) head (ns) change head throughput
64 9,664 10,033 +3.8% 6.4 MB/s
128 11,519 11,837 +2.8% 10.8 MB/s
192 13,390 13,635 +1.8% 14.1 MB/s
384 18,883 19,003 +0.6% 20.2 MB/s
1024 37,264 37,171 −0.2% 27.5 MB/s
16384 489,790 481,426 −1.7% 34.0 MB/s
1048576 33,026,260 31,738,390 −3.9% 33.0 MB/s

Long inputs are about 4% faster (no copy pass). Inputs of a few blocks are 2–4% slower (a few hundred ns): the frame now copies four stack arguments, and the entry stores the distance to the destination and loads it back.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWua1KdmeJmEbr5Ydzz

vg_aes_gcm_open_to on RISC-V no longer copies src to dst and decrypts
there in place by a call of vg_aes_gcm_open: it is vg_aes_gcm_open's body
out of place (openWith true). It hashes the ciphertext at src and, if the
tag is right, decrypts it from src straight to dst; if the tag or its
length is wrong it zeroes dst and returns 0.

One body and one proof serve both: AES's counter mode (ctr32With o, the
new internal vg_aes_ctr32_to out of place, vg_aes_ctr32 in place), GCM's
crypt (cryptWith o, xorLoopWith o) and open's body (openWith o) are
proved once, generic in o, with the in-place functions their instances.
vg_aes_gcm_open's and vg_aes_gcm_seal's emitted code is unchanged.

No Spec/ or TCB/ change.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWua1KdmeJmEbr5Ydzz
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit f5b36e1 Nov 7, 2026
23 checks passed
@reaperhulk
reaperhulk deleted the claude/gcm-open-to-one-pass-riscv64 branch November 7, 2026 22:44
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