Skip to content

AArch64 AES-GCM: one-pass out-of-place decryption for vg_aes_gcm_open_to_with_powers (interleaved variant) - #4563

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

reaperhulk merged 15 commits into
mainfrom
claude/gcm-open-to-one-pass-aarch64

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

What changed

On AArch64, vg_aes_gcm_open_to_with_powers_aes_sha3 (the variant with the interleaved CTR+GHASH loop) no longer copies src to dst before decrypting in place, for TLS-shaped records. It now reads the ciphertext from src, hashes it, and writes the plaintext straight to dst. If the tag is wrong, dst is still zeroed.

  • New internal function vg_aes_gcm_open_split_with_powers (Impl/AesGcm/AArch64/Stitch.lean, openSplitWithPw). It is open_with_powers' path for TLS records (openFastS), out of place.
    • Arguments: (ctx, rounds, nonce, nonce_len, aad, aad_len, dst, len, tag, tag_len, pw, src, src_len).
    • It runs with its working space framed (withStackArgScratch 2608 5).
    • Its contract is in Proof/AesGcm/AArch64/OpenSplit/Contract.lean; only verified code calls it, so there is no Api.
    • It is Verified in OpenSplit/Run.lean (what it computes) and OpenSplit/Verified.lean (constant time, and the stack-scratch framing).
  • vg_aes_gcm_open_to_with_powers for the interleaved variant is now OpenTo.openToSel.
    • A branch-free block first sets x10 = (aad_len >> 4) | ((len - 768) >> 63) | (nonce_len ^ 12).
    • Then Code.iteCold chooses between two paths:
      • openToSplit: a 48-byte frame that calls the new function, then zeroes dst if it returns 0.
      • the existing openTo 3 copy path, used otherwise.
    • The two paths are composed with Verified.entryBranch (Proof/AesGcm/AArch64/OpenTo/Split.lean), against openToWithPowersRoundsContract with 2656 bytes of stack. Before this change it used 2624 bytes; the extra 32 come from the new frame plus the callee's working space.
  • The registration in Generic/AesGcm/AArch64/AesGcm.lean uses openToSel when v.stitch, and leaves every other variant unchanged.
  • Generated code: in gcm.s, only vg_aes_gcm_open_to_with_powers_aes_sha3 changed. The new internal function internal/vg_aes_gcm_open_split_with_powers_aes_sha3 was added. vg_aes_gcm_open's emitted code is byte-identical.

How the code is shared, not duplicated

  • The interleaved decryption loop (Impl/Gcm/AArch64/Stitch.lean): decBody, decLoop and dec take a store mode to.
    • to = false stores over the text (the in-place code, emitted unchanged).
    • to = true stores to x15 and advances it.
    • The loop's proofs (Proof/Gcm/AArch64/Stitch/{Spec,Base,Steps,Loop,Ok,Final}) are stated once, over KPreM s₀ to and DPostT. The in-place dec_ok is an instance of decM_ok.
  • openRunWithS takes the same mode through decPart.
    • Out of place, decPart first copies only the tail after the loop's whole batches to dst (tailTo).
    • It then runs dec true from src to dst. The rest of open runs on dst unchanged.
    • Its proofs (Stitch/Pre, Open, OpenB, OpenW, OpenCT, ShortOpenCT) are generic over the mode. The in-place lemmas (openRunWithS_postX, dec_ok, …) are instances of the generic ones (openRunWithS_postXT, decM_ok).
  • Shared AArch64 lemmas were generalized additively, with the old statements kept as instances:
    • openEntryW_ok handles work at stack argument 5.
    • OpenPostG, oiteWithG_ok, openRestWithG_wp and openRestWithG_rel handle any ciphertext.
    • OpenIn.of_regs works over any registers the entry does not hold.
    • entry_rel and entryStash_rel also accept work at [sp + 40].
  • KPreM no longer has d_k (text disjoint from the key context). The out-of-place source is a read-only buffer, which the contract lets overlap the key context, and nothing needed d_k except a length bound that the output's disjointness also gives.
  • The wrapper's after-call phase (openToSplit's wipe and return, Split.lean) follows OpenTo/Fn.lean's. It reuses its generic lemmas (head_ok, copyStep_ok, wipeLoop_wp, bytesAt_zeros, WArgs). The frame and stack offsets (48/2608/2656 against 32/2592/2624) are restated in SLay, SCalled and SDone.

Trust

No changes to Spec/ or TCB/.

Constant time holds as before. The new code branches only on public lengths (nonce_len, aad_len, len) and on whether the tag is right. The new taint checks use taint_decide (pwRestDecTo_rel for dec true, and the wrapper's entry, wipe and return).

There are no sorry, native_decide, bv_decide or resource options. The one new decide +kernel is in stitchOpenSplit_keepsV, a vector-register keep check that mirrors the existing stitchOpenWithS_keepsV. It is not a constant-time proof.

Not done (left for follow-ups)

These paths still copy src to dst and decrypt in place (openTo):

  • Scalar vg_aes_gcm_open_to, _aes vg_aes_gcm_open_to, and _aes vg_aes_gcm_open_to_with_powers. They decrypt with vg_aes_ctr32, which works only in place.
  • _aes_sha3 vg_aes_gcm_open_to_with_powers outside the one-pass condition. That is, a nonce other than 12 bytes, 16 or more bytes of AAD, or fewer than 768 bytes.
    • Below 768 bytes, open_with_powers uses its short path (openShortF) or openFastP, and for other nonce/AAD lengths it uses openRest. None of these is generic over the output yet.
    • Planned fix: make them generic the way the interleaved loop was here. That PR also deletes the openTo copy wrapper.
  • The generated code contains the interleaved loop twice, dec false in open_with_powers and dec true in the new internal function. The source and proof exist once. A follow-up will try one emitted instance, measured by one aes-128-gcm-decrypt run, and keep it only if no size gets slower.

Checks run

  • Full lake build --log-level=warning, after rebasing onto main (f5b36e1), on main's restored CI cache.
  • The full lake env lean --run Emit.lean, then lake env lean --run Emit.lean --check.
  • Every step of the Static checks job, with --base origin/main for the single-importer check.
  • ci/check_lean_public_imports.py --base origin/main -m <changed modules>.
  • cargo fmt --check, and cargo clippy --all-targets -- -D warnings on the host and for aarch64-unknown-linux-gnu.
  • The AES-GCM tests under qemu-aarch64 -cpu max, with Wycheproof (WYCHEPROOF_ROOT), with VG_CPU_FEATURES unset and set to none.
    • All pass.
    • No published vector is 768 bytes or longer. The one-pass path is reached by the unit tests backends_agree and prepared_boundaries: out-of-place decrypt at 768–10000 bytes, a 12-byte nonce, short AAD, and a wrong tag that must zero the output.

Benchmark

Scoped run, as the task asked: https://github.com/pyca/verified-garbage/actions/runs/42423188866 (arches=aarch64, cpu_features='-', modules=aes_gcm, groups='aes-128-gcm-decrypt-out-of-place').

The runner was a Neoverse N2 (CPU part 0xd49) with SHA3, so this run measured the _aes_sha3 variant, the one this PR changes.

Benchmark Base Head Change
aes-128-gcm-decrypt-out-of-place/64 149 ns 149 ns 0.1% slower
aes-128-gcm-decrypt-out-of-place/128 167 ns 167 ns 0.1% faster
aes-128-gcm-decrypt-out-of-place/192 185 ns 186 ns 0.3% slower
aes-128-gcm-decrypt-out-of-place/384 230 ns 231 ns 0.2% slower
aes-128-gcm-decrypt-out-of-place/1024 373 ns 374 ns 0.5% slower
aes-128-gcm-decrypt-out-of-place/16384 3.88 µs 3.90 µs 0.5% slower
aes-128-gcm-decrypt-out-of-place/1048576 244.24 µs 245.06 µs 0.3% slower
Benchmark Base copies Head copies
aes-128-gcm-decrypt-out-of-place/64 149 ns – 150 ns (0.4%) 149 ns – 150 ns (0.4%)
aes-128-gcm-decrypt-out-of-place/128 167 ns – 168 ns (0.4%) 167 ns – 167 ns (0.2%)
aes-128-gcm-decrypt-out-of-place/192 185 ns – 185 ns (0.1%) 186 ns – 186 ns (0.2%)
aes-128-gcm-decrypt-out-of-place/384 230 ns – 230 ns (0.1%) 231 ns – 231 ns (0.1%)
aes-128-gcm-decrypt-out-of-place/1024 373 ns – 373 ns (0.1%) 374 ns – 375 ns (0.2%)
aes-128-gcm-decrypt-out-of-place/16384 3.88 µs – 3.89 µs (0.1%) 3.90 µs – 3.90 µs (0.1%)
aes-128-gcm-decrypt-out-of-place/1048576 244.24 µs – 245.86 µs (0.7%) 245.06 µs – 246.16 µs (0.4%)

What this shows:

  • No size got faster. At 1 KiB and above the one-pass path runs instead of the copy, but head is 0.3–0.5% slower.
  • At 1 KiB and 16 KiB that is just outside the copy-to-copy spread; at 1 MiB it is within it.
  • Below 768 bytes the code is unchanged, and the differences are noise.

So on this core, removing the copy did not pay for itself. The likely costs are the out-of-place loop's extra output pointer, and the call and frame of openToSplit. The copy it replaces was cheap on N2. I have not rerun the benchmark.

https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6


Generated by Claude Code

claude added 15 commits November 7, 2026 22:49
…place

Impl.Gcm.AArch64.Stitch.decBody, decLoop and dec take a store mode: over
the ciphertext (x9, the code of before), or to a destination at x15,
advanced with it. The proofs of the loop (KPreM, LInv, DPostT) are generic
over the mode; dec_ok, the in-place case, keeps its statement.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
…open_split_with_powers' code

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
…ny output

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
…ace; MidS over the ciphertext

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
…verlap the key context

The text only read by the loop, out of place, is a read-only buffer the
contract lets overlap the key context; nothing needed it apart, but
for the bound on its length, which the output's disjointness gives.
OpenIn.of_regs over any registers the entry state does not hold.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
openRestWithG_rel (any ciphertext, as openRestWithG_wp), the entry's CT
lemma for work at [sp + 40].

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
Constant time (openSp_ctN): the entry and the pointer to src, loaded from
the stack, are public; the rest is open's path for TLS records out of
place. Framed with its working space on the stack (openSp_framed).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
…l), its call's proof

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
…ied.entryBranch), registered for the interleaved variant

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
…l functions

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWvMpP5uQgBBji1Ngb6
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit 917c3c0 Nov 8, 2026
25 checks passed
@reaperhulk
reaperhulk deleted the claude/gcm-open-to-one-pass-aarch64 branch November 8, 2026 07:27
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