Skip to content

Arm: vg_aes_gcm_open_to in one pass, sharing open's body - #4561

Merged
reaperhulk merged 3 commits into
mainfrom
claude/gcm-open-to-one-pass-arm
Nov 8, 2026
Merged

reaperhulk merged 3 commits into
mainfrom
claude/gcm-open-to-one-pass-arm

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

On Arm (A32 and Thumb, one crate), vg_aes_gcm_open_to used to copy the ciphertext from src to dst and call vg_aes_gcm_open on dst in place. Now it reads the ciphertext from src, hashes it, and decrypts it straight into dst in one pass, on every path including the last partial block. If the tag is wrong it zeroes the len bytes at dst, as openToPost requires.

No Spec/ or TCB/ change, and no Rust change.

Sharing, not duplicating

Each piece is one definition and one proof, generic over the mode (to : Bool). The in-place instance's code is unchanged:

  • Impl/Aes/Arm/Ctr32.lean: ctr32Gen to.
    • vg_aes_ctr32 is ctr32Gen false, byte-identical to before.
    • The new internal vg_aes_ctr32_to is ctr32Gen true. It keeps the destination pointer in scratch slot 48, loading it before each group's XOR and storing it back advanced after, on either branch, so it stays public. The XOR loads from r10 and stores through kp.
    • The proofs in Proof/Aes/Arm/Group.lean and Ctr32.lean are generic, both functional and constant-time.
  • Impl/AesGcm/Arm.lean: open and open_to are one body, openG to. It differs only in:
    • the stack offsets (tagOff, tagLenOff, workOff);
    • the decryption step oneCryptG: crypt in place, or cryptTo from src to dst through vg_aes_ctr32_to;
    • the end openEnd: open_to zeroes dst when the result is 0.
  • Shared steps and their proofs.
    • crypt's whole-block and tail steps are generic (cryptWholeG, cryptTailG).
    • So is the byte-XOR loop, xorLoopG to. xorLoop is xorLoopG false and keeps its code, so its other users (CCM, OCB, GCM-SIV, SIV) are unchanged. Out of place it reads the text through r4 and writes dst.
    • The body's correctness and constant-time proofs are generic too: CryptOk.lean, CTCrypt.lean, Loops.lean, Open.lean (openBody_wp) and CTOpen.lean (openBody_rel).
  • Proof/AesGcm/Arm/OpenTo.lean replaces the copy-based OpenTo/ proofs and Impl/AesGcm/Arm/OpenTo.lean. It has:
    • the contract and the wipe;
    • constant time: the only branches are on public lengths and on whether the tag is right, which openToLeak makes public;
    • the frame that puts the working space on the stack. That frame is 2608 bytes (8 copied stack-argument words; 2600 can't be encoded as an immediate), for 2620 bytes of stack in all, down from 2648.

Generated code

  • Only vg_aes_gcm_open_to changes, and vg_aes_ctr32_to is added.
  • vg_aes_gcm_open, vg_aes_ctr32 and every other crate are byte-identical.

The two counter-mode instances, deduplicated in a follow-up PR

vg_aes_ctr32 and vg_aes_ctr32_to are one definition with one proof, but the binary carries both. On Arm, vg_aes_ctr32 is internal, and GCM, CCM, GCM-SIV, SIV and CMAC call it.

A separate follow-up PR will drop the in-place instance:

  • Every one of those callers moves to vg_aes_ctr32_to with dst = src, which needs its contract widened to "src = dst or disjoint".
  • Their call frames grow to 12 bytes, and with them those functions' stack contracts.
  • That PR benchmarks one in-place group per affected algorithm in one scoped run, and makes the change only if nothing is slower beyond the copy-to-copy spread.

Changes to shared Arm proof files

  • onePre (Proof/AesGcm/Arm/Contract.lean) now lets the data be read-only, as open_to's source is. The data must be disjoint from the other buffers only when it is written; seal and open in place still prove with it.
  • Callee.lean adds the 12-byte-push call of vg_aes_ctr32_to.
  • Compare.lean and CTVerify.lean take the received-tag offset as a parameter.

Checks run

  • lake build --log-level=warning (all 28614 jobs) and lake env lean --run Emit.lean --check, after rebasing onto main and restoring its cache.
  • Every step of Static checks, with check_lean_single_importer.py --base origin/main.
  • ci/check_lean_public_imports.py --base origin/main -m on the changed modules.
  • cargo fmt --check, and cargo clippy --all-targets -- -D warnings on the host, armv7-unknown-linux-gnueabihf and thumbv7neon-unknown-linux-gnueabihf.
  • AES-GCM Wycheproof and CAVP tests under qemu-arm on both Arm targets: pass.

Benchmark

Scoped run: https://github.com/pyca/verified-garbage/actions/runs/42422486721 (arm, VG_CPU_FEATURES=-, aes_gcm, aes-128-gcm-decrypt-out-of-place, against main 4f412a1).

Each cell is the median over the run's 8 interleaved copies of each side's median time. The spread is (max − min) / median across those copies.

size (B) main (µs) this PR (µs) change copy spread (main / PR)
64 3.24 3.23 −0.3% 0.06% / 0.04%
128 4.80 4.79 −0.3% 0.06% / 0.04%
192 6.37 6.35 −0.3% 0.05% / 0.07%
384 11.08 11.05 −0.3% 0.07% / 0.10%
1024 26.76 26.68 −0.3% 0.09% / 0.06%
16384 402.79 401.53 −0.3% 0.07% / 0.02%
1048576 25705.47 25608.83 −0.4% 0.04% / 0.02%

Every size is about 0.3% faster, beyond the copy-to-copy spread. The copy of the ciphertext is gone, and the decryption itself (bitsliced AES, then GHASH) dominates the time.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWykBrvUW5UtsP5uhDu

vg_aes_gcm_open_to on Arm (A32 and Thumb) used to copy the ciphertext
from src to dst and call vg_aes_gcm_open on dst in place. It now reads
the ciphertext from src, hashes it, and decrypts it straight into dst,
and zeroes the len bytes at dst if the tag is wrong, as the contract
(openToPost) requires. The spec, the TCB and the Rust code are unchanged.

The code is shared, not duplicated:

* Impl/Aes/Arm/Ctr32.lean: ctr32Gen to; vg_aes_ctr32 is ctr32Gen false
  (its code is unchanged) and the new internal vg_aes_ctr32_to is
  ctr32Gen true, which keeps the destination pointer in a scratch slot
  and stores the XOR through it. Its proofs (Proof/Aes/Arm/Group.lean,
  Ctr32.lean) are generic over the mode.
* Impl/AesGcm/Arm.lean: open and open_to are one body, openG to, which
  differs only in the stack offsets, the decryption step (oneCryptG:
  crypt in place, or cryptTo from src to dst through vg_aes_ctr32_to)
  and the end (openEnd: open_to's wipe of dst when the result is 0).
  vg_aes_gcm_open's emitted code is unchanged. The whole-block and tail
  steps of crypt are generic (cryptWholeG, cryptTailG), as are their
  proofs (CryptOk.lean, CTCrypt.lean) and the body's correctness and
  constant-time proofs (Open.lean openBody_wp, CTOpen.lean openBody_rel).
* Proof/AesGcm/Arm/OpenTo.lean replaces the copy-based OpenTo/ proofs:
  the contract, the wipe, constant time (branches only on public lengths
  and on whether the tag is right, which openToLeak makes public), and
  the frame that puts the working space on the stack (2608 bytes: 8
  words of copied stack arguments; 2620 bytes of stack in all, from
  2648).

onePre (Contract.lean) now lets the data be read-only, as open_to's
source is, with its disjointness from the other buffers required only
when it is written.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWykBrvUW5UtsP5uhDu
The tail of open_to no longer copies the last bytes of src to dst and
XORs them in place: the byte-XOR loop is generic (xorLoopG), reading the
text through r4 out of place and writing it to dst, with one proof for
both modes. xorLoop is xorLoopG false, whose code is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWykBrvUW5UtsP5uhDu
Only vg_aes_gcm_open_to changes, and the new internal vg_aes_ctr32_to
is added; vg_aes_gcm_open and vg_aes_ctr32 are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWykBrvUW5UtsP5uhDu
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 7, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Nov 7, 2026
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit b5c640f Nov 8, 2026
25 checks passed
@reaperhulk
reaperhulk deleted the claude/gcm-open-to-one-pass-arm branch November 8, 2026 07:23
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