Skip to content

3DES x86-64: CBC and CFB64 decryption many blocks at a time - #4416

Merged
alex merged 2 commits into
mainfrom
claude/perf-4141-3des-decrypt
Nov 7, 2026
Merged

alex merged 2 commits into
mainfrom
claude/perf-4141-3des-decrypt

Conversation

@alex

@alex alex commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

Part of #4141 (finding 2).

What was slow, and why

On x86-64, vg_triple_des_cbc_decrypt_chained and vg_triple_des_cfb64_decrypt decrypted 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:

  • CBC computes Pⱼ = CIPH⁻¹(Cⱼ) ⊕ Cⱼ₋₁.
  • CFB64 computes 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:

    1. copies the group's ciphertext to a scratch buffer;
    2. for CFB, shifts in the previous ciphertext blocks;
    3. makes one call to an ECB function on the group in place (ECB decrypt for CBC, ECB encrypt for CFB);
    4. XORs in the chaining words;
    5. carries the last ciphertext block forward.

    The IV's continuation (Cₙ₋₁) is written before any block is decrypted, as before.

  • Proofs (Proof/TripleDes/X86_64/Batch/{Call,Verified}.lean):

    • Correctness is proved against the existing cbcDecryptChainedContract and cfbDecryptContract. The callee is the public framed ECB function, through WP.callF with ecbContract.
    • Constant time is proved relationally (RelCT): taint for the straight-line pieces, RelCT.callEx for the call, RelCT.loop for the groups.
    • No sorry, native_decide or new axioms, and no resource options.
  • Registration: generic over the TripleDesEcb interface (Generic/TripleDesEcb/X86_64/TripleDes{Cbc,Cfb}.lean), with variants Variants/TripleDesEcb/X86_64/{Sse2,Avx2,Avx512}.lean. This emits vg_triple_des_cbc_decrypt_chained{,_avx2,_avx512} and vg_triple_des_cfb64_decrypt{,_avx2,_avx512}.

  • Rust:

  • Removed dead code: the old one-block decrypt code (cbcDecryptChained, cfbDecrypt, their proofs and literals, and the internal vg_triple_des_decrypt_block).

  • Unchanged: encryption, OFB, CFB8 and the final functions.

  • Tests: new long_messages tests in tests/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.py groups and rust_matrix.toml entries for triple_des_cbc and triple_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's crypt, with constant time proved by taint alone. 3DES needs three things it doesn't have:

  • 8-byte blocks;
  • a call to the public ECB variant function, so that the _avx2 and _avx512 kernels are reached;
  • a relational constant-time proof, because the framed ECB code restores callee-saved registers from stack slots that the taint analysis forgets after secret stores.

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 _avx512 variant 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:

benchmark features before after OpenSSL
CBC decrypt 16 KiB none 9.2 ms 55 µs 520 µs
CBC decrypt 16 KiB avx2 10.0 ms 31 µs
CBC decrypt 16 KiB all 8.8 ms 32 µs
CBC decrypt 1 KiB all 560 µs 4.2 µs 35 µs
CBC decrypt 64 B all 38 µs 4.2 µs 2.7 µs
CFB64 decrypt 16 KiB none 9.6 ms 58 µs 600 µs
CFB64 decrypt 16 KiB avx2 10.1 ms 34 µs
CFB64 decrypt 16 KiB all 9.0 ms 32 µs
CFB64 decrypt 64 B all 38–47 µs 3.4–4.0 µs

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

  • Static checks pass.
  • check_lean_public_imports.py passes on the changed modules.
  • A full lake build and Emit.lean --check pass after the rebase.
  • cargo fmt and clippy -D warnings pass.
  • The 3DES tests pass under VG_CPU_FEATURES=none, avx,avx2 and unrestricted.

No TCB/ or Spec/ changes.

🤖 Generated with Claude Code

https://claude.ai/code/session_01TKUFHH61L4k8hk2MfCZReW

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
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown

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

@alex
alex force-pushed the claude/perf-4141-3des-decrypt branch from 05bcc99 to e719747 Compare November 7, 2026 09:31
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHH61L4k8hk2MfCZReW
@alex
alex enabled auto-merge November 7, 2026 09:40
@alex
alex added this pull request to the merge queue Nov 7, 2026

alex commented Nov 7, 2026

Copy link
Copy Markdown
Member Author

Results of the Benchmarks run https://github.com/pyca/verified-garbage/actions/runs/42359580293:

  • x86_64 runner, base 8b31da4;
  • head 05bcc99: before the rebase, with the same decryption code;
  • one interleaved run per configuration;
  • no benchmark slowed down.

Unrestricted features (AVX-512 runner), with OpenSSL:

Benchmark Base Head Change OpenSSL Head vs OpenSSL
triple-des-cbc-decrypt/64 44.54 µs 4.31 µs 90.3% faster 3.02 µs 1.4× slower
triple-des-cbc-decrypt/1024 696.60 µs 4.64 µs 99.3% faster 35.27 µs 7.6× faster
triple-des-cbc-decrypt/16384 11.14 ms 33.55 µs 99.7% faster 551.45 µs 16.4× faster
triple-des-cfb64-decrypt/64 44.46 µs 4.35 µs 90.2% faster 3.14 µs 1.4× slower
triple-des-cfb64-decrypt/1024 695.20 µs 4.75 µs 99.3% faster 38.31 µs 8.1× faster
triple-des-cfb64-decrypt/16384 11.12 ms 32.69 µs 99.7% faster 600.87 µs 18.4× faster

VG_CPU_FEATURES=avx,avx2:

Benchmark Base Head Change
triple-des-cbc-decrypt/64 46.51 µs 4.25 µs 90.9% faster
triple-des-cbc-decrypt/1024 726.89 µs 4.31 µs 99.4% faster
triple-des-cbc-decrypt/16384 11.64 ms 28.59 µs 99.8% faster
triple-des-cfb64-decrypt/64 46.19 µs 4.28 µs 90.7% faster
triple-des-cfb64-decrypt/1024 730.65 µs 4.45 µs 99.4% faster
triple-des-cfb64-decrypt/16384 11.62 ms 31.83 µs 99.7% faster

VG_CPU_FEATURES=none (baseline):

Benchmark Base Head Change
triple-des-cbc-decrypt/64 44.46 µs 4.30 µs 90.3% faster
triple-des-cbc-decrypt/1024 695.89 µs 4.63 µs 99.3% faster
triple-des-cbc-decrypt/16384 11.12 ms 57.80 µs 99.5% faster
triple-des-cfb64-decrypt/64 44.43 µs 4.32 µs 90.3% faster
triple-des-cfb64-decrypt/1024 695.26 µs 4.75 µs 99.3% faster
triple-des-cfb64-decrypt/16384 11.10 ms 59.28 µs 99.5% faster

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

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