Skip to content

x86 and ARM: vg_chacha20_poly1305_open_to sharing open's body, and decrypt on both - #4466

Merged
reaperhulk merged 2 commits into
mainfrom
claude/determined-gates-p7sa7b-x86-open-to
Nov 7, 2026
Merged

reaperhulk merged 2 commits into
mainfrom
claude/determined-gates-p7sa7b-x86-open-to

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

This PR adds out-of-place ChaCha20-Poly1305 decryption without a copy on x86 (32-bit) and ARM, in one PR. It is their counterpart of #4384 (RISC-V) and #4427 (PPC64LE), and builds on their src/dst vg_chacha20_xor (#4447, #4436). The ARM part was #4443, which this replaces. Each architecture is one commit. No change to TCB/ or Spec/.

On both architectures, open and open_to share one decryption body; only a short entry differs: seal = inPlace; sealBody, open = inPlace; openBody, openTo = toEntry; openBody.

x86: vg_chacha20_poly1305_open_to and _ssse3

x86 has no spare callee-saved register for dst, so the body keeps what it needs in the function's own copy of its stack arguments. withStackScratchWiped lays that copy out, and the contract already lets the function write it (writeArgs).

  • What the body expects: the context (work) in eax, the source and len in slots 4 and 5, the tag in slot 6 ([esp+28]), and the destination in slot 7 ([esp+32]).
  • Entries.
    • inPlace is three instructions: it loads work and copies data into slot 7.
    • toEntry is five: it loads dst, tag and work, then stores tag and dst into slots 6 and 7.
  • Body changes. crypt passes dst to the kernel with mov esi, [esp+32], and zeroArgs zeroes the destination from [esp+32].
  • Frames. open_to is withStackScratchWiped 748 9 176, with a stack of 780. seal/open keep 740/772.
  • Variants. openToApi is registered in Generic/ChaCha20Xor/X86/ChaCha20Poly1305.lean with the same suffix and features as seal/open. That emits it for SSE2 and SSSE3, and the dispatcher vg_chacha20_poly1305_open_to_auto chooses between them.
  • Proofs.
    • APre describes the body's entry state.
    • inPlace_ok/toEntry_ok/Entered connect each entry to the body.
    • openBody_correct and openBody_rel are proven once and used by open and open_to.
    • The entries are checked by taint from esp alone.
    • openTo_verified is proven against openToScratchContract X86.abi 88 32.
    • X86.Verified.stackScratchWipedPre is new: its hpostOut may assume the precondition. stackScratchWiped wraps it, and its statement is unchanged.

ARM: vg_chacha20_poly1305_open_to

  • Registers. The body reads the source at r10 and writes the destination held in r6.
    • crypt passes it to the kernel (mov r5, r6), and zeroData zeroes from it.
    • The tag is read through r4, so the body reads no stack argument.
  • Entries. Each entry is a short prologue: prologue w t dst = entry w ++ save ++ moves t ++ [dst], with one definition generating the saves.
    • inPlace ends with mov r6, r10.
    • toEntry ends with ldr r6, [sp, #8].
  • Frames. open_to is withStackScratchWiped 664 5 158, with a stack of 672. seal/open keep 656 3 158 and 664, so seal_gather is unchanged.
  • Proofs.
    • APre takes membership facts, inout and disjointness of the destination.
    • openBody_ct and sealBody_ct are each proven once.
    • vg_chacha20_xor's secret stores through the destination make the analysis forget the public slots. So crypt and compare are two taint runs, with r4/r6 re-established between them from the invariant.
    • openTo_verified is proven against openToScratchContract Arm.abi 79 8.
    • Verified.stackScratchWipedPre is new, with stackScratchWiped as its corollary.

Rust

  • ChaCha20Poly1305::decrypt is enabled on x86 and ARM, alongside RISC-V and PPC64LE, with its unit test decrypt_out_of_place, the Wycheproof out-of-place check and the benchmark group.
  • Following ChaCha20, ChaCha20-Poly1305: choose with their generated dispatchers #4464, decrypt calls the generated dispatcher vg_chacha20_poly1305_open_to_auto. On x86 the dispatcher picks SSSE3 or SSE2; elsewhere it is another name for the one implementation. CI's x86 jobs run both with SSSE3 and with VG_CPU_FEATURES=none.

Generated code

  • crates/verified-garbage-asm-x86: chacha20poly1305 and its dispatch file. These add open_to and open_to_ssse3. seal, seal_ssse3, open and open_ssse3 each gain two entry instructions and load the destination from [esp+32].
  • crates/verified-garbage-asm-arm: chacha20poly1305 and its dispatch file. These add open_to. seal/open change by a few register moves, and the tag's stack loads become r4.

Checks

  • For each architecture, the full lake build, Emit.lean and Emit.lean --check pass, re-run after rebasing onto main with the dispatchers of Emit a dispatcher for every function's implementations #4428.
  • Every step of the Static checks job passes, as does check_lean_public_imports.py.
  • x86: cargo test --target i686-unknown-linux-gnu chacha passes with Wycheproof, both with the CPU's features and with VG_CPU_FEATURES=none. Clippy for i686 passes. Both were re-run after the last rebase.
  • ARM: before the last rebase, cargo test chacha passed under qemu on armv7-unknown-linux-gnueabihf and thumbv7neon-unknown-linux-gnueabihf, including decrypt_out_of_place and Wycheproof.
    • The last rebase changed only the function decrypt calls, to the dispatcher.
    • I could not re-run the ARM tests after it, because this environment lost its ARM cross linker. CI runs them.
  • cargo fmt --check passes for the library and the bench crate, and host clippy passes, on the combined head.
  • Not run here: the bench crate's clippy for i686, because openssl-sys does not build for it here. CI's lint job covers it.

Benchmarks: not run. seal/open change by a few instructions on both architectures. A scoped manual Benchmarks run should go with this PR (ChaCha20-Poly1305 on x86 and ARM, including the new out-of-place group). I have not triggered one.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWwrECv53ESEcwjcHaU

@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 a conflict with the base branch Nov 7, 2026
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after 8537ba6 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.

Adds `vg_chacha20_poly1305_open_to` (and `_ssse3`) on x86, decrypting from
`src` into `dst` without a copy. One body serves `open` (in place) and
`open_to` (out of place): it reads the source at stack slot 4 and writes the
destination held in slot 7, with the tag in slot 6 and the context in `eax`.
Each entry moves the arguments there within its own (frame-copied, writable)
argument slots: `seal` and `open` copy `data` into `work`'s slot
(`inPlace`); `open_to` moves `tag` and `dst` into the slots of `dst` and
`dst_len` (`toEntry`). `vg_chacha20_xor` is then called from `src` to `dst`
(`XorImpl.call_to`), and a wrong tag zeroes the destination.

Proofs: the body is proven from the state it is entered in (`APre`, now
about the body's entry state, with `inout`: in place, or apart for `open`),
once for both `open` and `open_to` (`openBody_correct`, `openBody_rel`);
`Entered` relates it to the function's entry. `open_to` is generic over the
`vg_chacha20_xor` variants like `seal`/`open`, and framed by the new
`X86.Verified.stackScratchWipedPre`, whose `hpostOut` may assume the
precondition (`dst_len = len`).

Rust: `decrypt` (out of place) on x86, choosing `open_to` or
`open_to_ssse3` by the backend, as `decrypt_in_place` does; its unit test,
the Wycheproof out-of-place check and the benchmark now run on x86 too.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwrECv53ESEcwjcHaU
@reaperhulk
reaperhulk force-pushed the claude/determined-gates-p7sa7b-x86-open-to branch from c90ef7b to 9a153e9 Compare November 7, 2026 16:23
@reaperhulk
reaperhulk enabled auto-merge November 7, 2026 16:26
… ARM

Out-of-place ChaCha20-Poly1305 decryption on ARM with no copy. The
decryption body exists once: it reads the source at r10 and writes the
destination kept in r6 (crypt passes it to vg_chacha20_xor in r5,
zeroData zeroes from it), and reads the received tag through r4, so it
reads no stack argument. Each entry is a prologue (the callee-saved
registers saved, the arguments moved) ending in one instruction: seal
and open set r6 := r10 (in place), open_to loads dst from its stack
arguments. open_to is withStackScratchWiped 664 5 158, stack 672; seal
and open keep their frames.

The body's correctness and constant-time proofs are shared by open and
open_to (openBody_ct, sealBody_ct); openTo_verified bridges to the
shared openToScratchContract, with Verified.stackScratchWipedPre, whose
hpostOut may assume the precondition (stackScratchWiped is now its
corollary).

ChaCha20Poly1305::decrypt, its unit test, the Wycheproof out-of-place
check and the benchmark group are enabled on ARM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWwrECv53ESEcwjcHaU
@reaperhulk reaperhulk changed the title x86: vg_chacha20_poly1305_open_to sharing open's body, and decrypt on x86 x86 and ARM: vg_chacha20_poly1305_open_to sharing open's body, and decrypt on both Nov 7, 2026
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit 53d1861 Nov 7, 2026
42 checks passed
@reaperhulk
reaperhulk deleted the claude/determined-gates-p7sa7b-x86-open-to branch November 7, 2026 17:10
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