Skip to content

x86-64 ChaCha20 apply: keep the data and its length in r14 and r15, and take the state back after vg_chacha20_xor - #4748

Merged
reaperhulk merged 1 commit into
mainfrom
claude/chacha20-apply-regs
Nov 9, 2026
Merged

reaperhulk merged 1 commit into
mainfrom
claude/chacha20-apply-regs

Conversation

@reaperhulk

@reaperhulk reaperhulk commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

Groundwork for verified residue clearing on x86-64 (proving that a function leaves no secret in registers or on the stack when it returns). It changes no contract, spec or TCB.

What changes

vg_chacha20_apply (Impl/ChaCha20/X86_64/Stream.lean) keeps three values across its call of vg_chacha20_xor: the state pointer, the data pointer and the bytes left.

  • The data pointer and the bytes left move from rbp and r12 to r14 and r15.
  • The state pointer is no longer trusted to survive the call in rbx. After the call it is taken again from the buffer the callee returns in rsi, which its contract already guarantees: stateBack, mov rbx, rsi; sub rbx, 256.

The instructions and the memory layout are otherwise the same.

Why

Every implementation of vg_chacha20_xor keeps its caller's rbx, rbp, r12 and r13 in memory while it stores into the data at offsets the residue analysis cannot bound. After those stores, the analysis no longer knows the reloaded values are public. apply then branches on the bytes left and addresses memory through the pointers, so the analysis rejects it. r14 and r15 are only saved around stores at known offsets, and rsi is set from an argument, so all three stay public. With this change the analysis accepts apply over every implementation of vg_chacha20_xor (scalar, SSSE3, AVX2, AVX-512, AVX-512VL), with no stack bytes left to wipe.

Proofs

  • Proof/ChaCha20/X86_64/Stream/{Apply,ApplyCT,Bytes}.lean: the same with the registers renamed, plus stateBack's two instructions (stateBack_exec, stateBack_ok) and the constant-time proof over them.
  • Stream/Calls.lean: xor_call also gives rsi on return, from xorTo's postcondition.

Checks

  • lake build, Emit.lean, the Static checks job, check_lean_public_imports.py: pass.
  • cargo test with Wycheproof, with every CPU feature and with VG_CPU_FEATURES=none: pass.
  • Speed, locally: calling each implementation's vg_chacha20_apply directly against main's, within ±2 cycles from 16 B to 1 KiB, AVX-512 and AVX-512VL included (Xeon). ci/bench_compare.py on chacha20 was within ±4% with every CPU feature.
  • Speed, Benchmarks workflow scoped to chacha20 on x86-64 (run 42526287820), an AMD EPYC 7763, one interleaved run, 5 B to 16 KiB:
    • VG_CPU_FEATURES=none (scalar): 0.0–2.9% faster at every size.
    • sha,ssse3 (SSSE3): 0.2% slower to 6.0% faster.
    • Default (AVX2 on this CPU): 1.1% slower to 2.2% faster.
    • avx,avx2,pclmulqdq,ssse3 (AVX2): within 2.5% but 64 B, 9.8% slower (131 → 143 ns). The default configuration runs the same code at 64 B and was 0.2% slower (118 → 118 ns), so this looks like noise.
    • The AVX-512 and AVX-512VL configurations were not measured: the runner lacks AVX-512. The local measurement above covers them.

🤖 Generated with Claude Code

https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa

…nd take the state again after vg_chacha20_xor

The implementations of vg_chacha20_xor keep their caller's rbx, rbp, r12 and
r13 in memory while they store into the data at offsets the residue analysis
cannot bound, after which it no longer knows them to be public. apply keeps
the data pointer and the bytes left in r14 and r15, which they leave alone,
and takes the state again from the buffer they return in rsi (stateBack).
Re-proven: the registers swapped, stateBack's two instructions, and
xor_call now also gives rsi on return.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 8, 2026
@reaperhulk
reaperhulk added this pull request to the merge queue Nov 9, 2026
Merged via the queue into main with commit 84b78da Nov 9, 2026
46 checks passed
@reaperhulk
reaperhulk deleted the claude/chacha20-apply-regs branch November 9, 2026 04:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

2 participants