Repository navigation
3DES x86-64: CBC and CFB64 decryption many blocks at a time - #4416
Conversation
CBC and CFB64 decryption need no block's result for the next, so decrypt them in groups of up to 256 blocks, each by one call of the ECB kernel (baseline, _avx2, _avx512), registered generically over the TripleDesEcb variants and chosen as ECB's are. Encryption, OFB and CFB8 are unchanged. Part of #4141. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH61L4k8hk2MfCZReW
|
This pull request has conflicts with Posted by |
05bcc99 to
e719747
Compare
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHH61L4k8hk2MfCZReW
|
Results of the Benchmarks run https://github.com/pyca/verified-garbage/actions/runs/42359580293:
Unrestricted features (AVX-512 runner), with OpenSSL:
As expected from the 256-block group (see the description), the unrestricted (AVX-512) configuration runs at about AVX2 speed. Only one-block messages stay behind OpenSSL, because of the bitsliced kernel's fixed cost per call. Generated by Claude Code |
Part of #4141 (finding 2).
What was slow, and why
On x86-64,
vg_triple_des_cbc_decrypt_chainedandvg_triple_des_cfb64_decryptdecrypted one block at a time with the scalar one-block 3DES (vg_triple_des_decrypt_block). That made them about 17× slower than OpenSSL and about 160× slower than our own ECB kernels at 16 KiB. In both modes, no block's decryption depends on another block's result:Pⱼ = CIPH⁻¹(Cⱼ) ⊕ Cⱼ₋₁.Pⱼ = CIPH(Cⱼ₋₁) ⊕ Cⱼ.So both can use the bitsliced ECB kernels.
What changed
Impl/TripleDes/X86_64/Batch.lean: a new function,decrypt cfb name code. It decrypts in groups of up to 256 blocks. For each group it:The IV's continuation (
Cₙ₋₁) is written before any block is decrypted, as before.Proofs (
Proof/TripleDes/X86_64/Batch/{Call,Verified}.lean):cbcDecryptChainedContractandcfbDecryptContract. The callee is the public framed ECB function, throughWP.callFwithecbContract.RelCT): taint for the straight-line pieces,RelCT.callExfor the call,RelCT.loopfor the groups.sorry,native_decideor new axioms, and no resource options.Registration: generic over the
TripleDesEcbinterface (Generic/TripleDesEcb/X86_64/TripleDes{Cbc,Cfb}.lean), with variantsVariants/TripleDesEcb/X86_64/{Sse2,Avx2,Avx512}.lean. This emitsvg_triple_des_cbc_decrypt_chained{,_avx2,_avx512}andvg_triple_des_cfb64_decrypt{,_avx2,_avx512}.Rust:
cipher::triple_des::ecb::Backend(an exhaustive match), the same choice ECB makes.vg_triple_des_cfb64_decrypt_final, after the chosen whole-block function.Removed dead code: the old one-block decrypt code (
cbcDecryptChained,cfbDecrypt, their proofs and literals, and the internalvg_triple_des_decrypt_block).Unchanged: encryption, OFB, CFB8 and the final functions.
Tests: new
long_messagestests intests/cavp/triple_des_{cbc,cfb}.rs. They take a CAVP MMT plaintext repeated to 1000 blocks, encrypt it with the (serial) encryptor, and decrypt it whole and in pieces of 1, 255, 256, 257, 511, 512 and 513 blocks; the plaintext must come back. They cross group boundaries and test the chaining across calls.CI: added
cpu_tests.pygroups andrust_matrix.tomlentries fortriple_des_cbcandtriple_des_cfb, so each CPU-feature configuration tests these modules.README: tables regenerated.
Why CFB64 doesn't use #4399's
cfbDecryptBatched#4399's generic batched CFB decryption (
Core.cfbDecryptBatched,CfbDec.batched_wp) supports only 16-byte blocks (bw = 2). It runs the cipher inline as the core'scrypt, with constant time proved by taint alone. 3DES needs three things it doesn't have:_avx2and_avx512kernels are reached;So CFB64 shares this PR's batched function with CBC, which differs only in the shift loop.
Limitation: AVX-512 runs at AVX2 speed
The TCB limits a stack frame to under 4096 bytes, and the scratch buffer for a group of 512 blocks (the AVX-512 kernel's batch) does not fit. So the group is 256 blocks, and the
_avx512variant gets about the same speed as_avx2. A follow-up could split the buffer across two frames to reach ECB's AVX-512 speed.Numbers
Local measurements, Intel Xeon @ 2.10 GHz:
For very short messages, the bitsliced kernel's fixed cost per call makes a 64-byte CBC decrypt slower than OpenSSL's (4.2 µs against 2.7 µs). It is still about 9× faster than before.
Benchmarks run (x86_64;
-,avx,avx2,none; with OpenSSL): https://github.com/pyca/verified-garbage/actions/runs/42359580293. It measures the commit before the rebase onto #4394, whose decryption code is the same. I'll post its results here when it finishes.Checks
check_lean_public_imports.pypasses on the changed modules.lake buildandEmit.lean --checkpass after the rebase.cargo fmtandclippy -D warningspass.VG_CPU_FEATURES=none,avx,avx2and unrestricted.No
TCB/orSpec/changes.🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHH61L4k8hk2MfCZReW