Skip to content

3DES PPC64LE: CBC and CFB64 decryption 64 blocks at a time - #4694

Closed
alex wants to merge 8 commits into
mainfrom
claude/ppc64le-3des-cbc-cfb-linear
Closed

alex wants to merge 8 commits into
mainfrom
claude/ppc64le-3des-cbc-cfb-linear

Conversation

@alex

@alex alex commented Nov 8, 2026

Copy link
Copy Markdown
Member

Depends on #4671 (the PPC64LE batched-modes framework, Impl/Modes/PPC64LE/Wide.lean). This PR's base is #4671's branch. It stays a draft until #4671 merges, and will then be restacked onto main. It replaces #4679, whose stack was not linear: the same change, with the same tree as #4679's last head (f8cb6ceb, CI green), as four commits on #4671's head ad3b520b. The first two are #4666 (bitsliced ECB, merged to main as 9d4f8cd4), and they drop out at the restack.

Finding 4 of the PPC64LE audit (#4141 (comment)): 3DES CBC and CFB64 decryption call the block function once per block. x86-64 batched them in #4416.

What changes

vg_triple_des_cbc_decrypt_chained and vg_triple_des_cfb_decrypt on ppc64le now take 64 blocks a step while at least 64 are left, then one block a step (Core.seq2, with Mode.cbcDecWide and cfbDecWide).

The core. wideCore d has a 64-block buffer. Its block cipher is a call of the new internal functions vg_triple_des_ecb64_{en,de}crypt (Bitslice.ecb64): #4666's bitsliced ECB on exactly 64 blocks, as one batch with the copies unrolled. ECB leaves the scratch buffer's and the schedule's addresses in r10 and r7, and the core restores sb and r3 from there.

Why a call, and why unrolled:

  • Branch range. Inlined, the 32 KB of ECB put the modes' loops out of reach of PPC conditional branches (±32 KB). A bl reaches.
  • Constant time. ECB saves r29–r31 (the modes' state) in the scratch buffer and restores them. With every store at a fixed offset, slotTaint sees those slots come back public.

Stack. The working space grows from 72 to 200 words, so CBC and CFB64 decryption take a 1632-byte frame instead of 608. The decryption block function vg_triple_des_decrypt_block is now unused, and the emitter dropped it.

Proofs

  • Proof/TripleDes/PPC64LE/Bitsliced/Ecb64.lean: ecb64_wp.
  • Proof/TripleDes/PPC64LE/ModeCoreWide.lean: wideCoreSpec.
  • ModesVerified.lean, ModesFrame.lean: the 200-word contracts and frames. Correctness comes from cbcDecWide_wp and cfbDecWide_wp.
  • ModesConstantTime.lean: slotTaint for the two functions.

No TCB or Spec changes.

Benchmarks

Scoped run (powerpc64; base 9de9b1b9, #4671's branch plus #4666), on POWER9: https://github.com/pyca/verified-garbage/actions/runs/42513716834

Benchmark Base Head Change
triple-des-cbc-decrypt/64 46.41 µs 38.16 µs 17.8% faster
triple-des-cbc-decrypt/1024 719.20 µs 11.11 µs 98.5% faster
triple-des-cbc-decrypt/16384 11.49 ms 156.67 µs 98.6% faster
triple-des-cfb64-decrypt/64 46.43 µs 38.49 µs 17.1% faster
triple-des-cfb64-decrypt/1024 719.21 µs 10.96 µs 98.5% faster
triple-des-cfb64-decrypt/16384 11.48 ms 154.38 µs 98.7% faster
encrypt groups within 2% (unchanged)

Generated by Claude Code

PPC64LE Camellia's core (vg_camellia_crypt_prepared) enciphers the eight
blocks of its tail buffer per call, but the modes used only the first, so
every mode cost a whole batch per block. CBC and CFB decryption and CTR
now take eight blocks a step while eight are left, then one a step.

This ports RISC-V's framework for batched modes (#3978, #4012) to PPC64LE:
Core.seq is now Core.seqWith (Core.steps m), the same code, and
Core.seq2 runs the steps of a widened mode (Mode.widen) while r30 >> lg
is not zero, then the narrow ones. The step proofs take the buffer's
bytes a step uses, the regions the key is ready for and the count a step
takes (Fixed, Inv), so one proof serves both loops; Proof/Modes/PPC64LE/
Wide.lean proves the loops of seq2 for CBC and CFB decryption and both
counter modes from the architecture-independent Proof/Modes/Wide.lean and
WideCtr.lean. No TCB or Spec change; the other modes' code is unchanged.

Part of #4141.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT
…cks at a time

PPC64LE SM4's core (vg_sm4_crypt_prepared) enciphers the sixteen blocks of
its tail buffer per call, but the modes used only the first. CBC and CFB
decryption, CTR and the counter mode with GCM's inc32 (vg_sm4_ctr32, which
SM4-GCM inlines through its contract) now take sixteen blocks a step
while sixteen are left, then one, with Core.seq2. The narrow counter
modes step the counter block before the block cipher (Mode.ctrEarly,
Mode.ctr32Early), as the wide ones do; Proof/Modes/PPC64LE/Ctr.lean and
Ctr32.lean, the one-block counter modes, are no longer used and go.

New tests check the API against itself at lengths around the groups:
SM4-CBC decryption and SM4-CTR, and Camellia-CBC decryption, whole, a
block at a time and in two pieces.

Part of #4141.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT
SEED's blocks function computes the rounds of sixteen lanes whatever the
number of blocks, so the modes, which called it on one block a step, paid
a whole batch per block. The core is now parameterized by its buffer's
number of blocks (core d g): the serial modes keep dirCore d = core d 1,
the same code and 1184-byte frame, and CBC and CFB decryption use
wideCore d = core d 16 with Core.seq2, sixteen blocks a step while
sixteen are left, then one, in a 1424-byte frame (174 words of working
space). modeContract and modeTaint take the working space's size, and
τCallW adds the blocks functions' taint summaries at it.

A new test checks SEED-CBC decryption against itself at lengths around the
groups.

Part of #4141.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT
…es with the full emitter

EmitOne had placed the internal functions the modes share
(vg_camellia_crypt_prepared, vg_seed_encrypt_blocks, ...) in each file it
emitted; Emit.lean puts them back in their shared modules.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT
ECB on ppc64le called the block function once per block (about 5.6 µs a
block, 9-15x OpenSSL; finding 4 of the PPC64LE audit in #4141). It now
encrypts and decrypts 64 blocks at a time, bitsliced in 64-bit GPRs, as on
RISC-V: the blocks are copied into the scratch buffer's state words and
transposed, the S-boxes run as Rusakov's circuits (`BitsliceCircuit`),
allocated to registers for this model's and/or/xor (NOT and AND-NOT
through a register of all ones), and the state is transposed back.

The functions keep the contract and the frame they had
(`ecbScratchContract`, `ecb_framed`), so the Rust code is unchanged. The
block-at-a-time ECB loop and its proofs are gone; the modes and CMAC still
call the block functions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
`ecb_wp` assumed the contract's exact lists of readable and writable
regions. It now needs only the schedule readable and the blocks and the
scratch buffer writable, and also gives the regions it writes and, when
there are blocks, the scratch buffer and the schedule's addresses left in
`r10` and `r7`, so that the modes can run the same code on their buffer.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
…es regenerating)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
…its own module

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFbSpgWBYWAsEnfUks
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

This pull request has conflicts with main, found after ef28190 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 commented Nov 8, 2026

Copy link
Copy Markdown
Member Author

Superseded by #4738: the same change, restacked onto main now that #4671 has merged. Closing this one.


Generated by Claude Code

@alex alex closed this Nov 8, 2026
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