Repository navigation
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
Conversation
…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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 ofvg_chacha20_xor: the state pointer, the data pointer and the bytes left.rbpandr12tor14andr15.rbx. After the call it is taken again from the buffer the callee returns inrsi, 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_xorkeeps its caller'srbx,rbp,r12andr13in 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.applythen branches on the bytes left and addresses memory through the pointers, so the analysis rejects it.r14andr15are only saved around stores at known offsets, andrsiis set from an argument, so all three stay public. With this change the analysis acceptsapplyover every implementation ofvg_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, plusstateBack's two instructions (stateBack_exec,stateBack_ok) and the constant-time proof over them.Stream/Calls.lean:xor_callalso givesrsion return, fromxorTo's postcondition.Checks
lake build,Emit.lean, the Static checks job,check_lean_public_imports.py: pass.cargo testwith Wycheproof, with every CPU feature and withVG_CPU_FEATURES=none: pass.vg_chacha20_applydirectly againstmain's, within ±2 cycles from 16 B to 1 KiB, AVX-512 and AVX-512VL included (Xeon).ci/bench_compare.pyonchacha20was within ±4% with every CPU feature.chacha20on 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.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.🤖 Generated with Claude Code
https://claude.ai/code/session_015ajQWxYnEynffr2A7QLPVa