Repository navigation
PPC64LE: Camellia, SEED and SM4 CBC and CFB decryption and counter modes a batch at a time - #4671
Merged
Merged
Conversation
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
This was referenced Nov 8, 2026
alex
pushed a commit
that referenced
this pull request
Nov 8, 2026
#4671, which this branch was stacked on, merged as ef28190. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT
This was referenced Nov 8, 2026
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.
PPC64LE's Camellia, SEED and SM4 cores encipher a batch of blocks per call (eight, sixteen and sixteen), but every mode used only the first block of the batch. So CBC and CFB decryption and CTR (and SM4-GCM, whose counter mode is SM4's
ctr32) cost a whole batch per block: 8–16× each cipher's own ECB on POWER9 (finding 5 of the ppc64le audit).The parallel modes now take a whole batch per step while a batch is left, then one block a step for the rest:
vg_camellia_cbc_decrypt_chained,vg_camellia_cfb128_decrypt,vg_camellia_ctrvg_sm4_cbc_decrypt_chained,vg_sm4_cfb128_decrypt,vg_sm4_ctr,vg_sm4_ctr32(and so SM4-GCM)vg_seed_cbc_decrypt_chained,vg_seed_cfb128_decryptThe serial modes (CBC and CFB encryption, OFB) and the
_finalfunctions are unchanged.How
This ports RISC-V's framework for batched modes (#3978, #4012) to PPC64LE. No TCB or Spec change: each function meets the contract it met before.
Impl/Modes/PPC64LE/Seq.lean:Core.seq mis nowCore.seqWith (Core.steps m) m.ivOut, which emits the same code.Impl/Modes/PPC64LE/Wide.lean(new):Core.seq2 wide narrow lg.Mode.widen, fromImpl/Modes/Wide.lean) whiler30 >> lg(computed intor8) is not zero, then the narrow mode's steps.r30, the public count.Proof/Modes/PPC64LE/Step.leanandSeq.lean: these are generalized as RISC-V's were.FixedandInvtake the buffer's bytes a step uses, the regions the key is ready for, and the count a step takes.step_wp) serves both loops:loop_wpfor one block a step,loopWide_wpfor a group.seqWith_changes_wpproves the function around any loops that take every step (LoopsOk).Proof/Modes/PPC64LE/Wide.lean(new):wideSteps_okproves the loops ofseq2, from the architecture-independentProof/Modes/Wide.leanandWideCtr.lean. From it comecbcDecWide_wp,cfbDecWide_wp,ctrWide_wpandctr32Wide_wp, with the conclusions of the one-block lemmas.G := 8,G := 16).crypt_wpproves every block of it from ECB's postcondition, as the code already did.Mode.ctrEarly,Mode.ctr32Early), as the wide ones do.Proof/Modes/PPC64LE/Ctr.leanandCtr32.leanare no longer used, so they are deleted.core d gis now parameterized by its buffer's number of blocks.dirCore d = core d 1: same code, same 1184-byte frame.wideCore d = core d 16, whose scratch buffer is 174 words (1424-byte frame).modeContractandmodeTainttake the scratch size, andτCallWadds the taint summaries of the blocks functions' calls at that size.vg_sm4_ctr32throughctr32_correct, whose statement is unchanged, so the SM4-GCM proofs needed no change.Constant time is proven as before (
taint_decide_sumover the new code).Tests
The proofs cover correctness. These new tests check the API against itself at lengths around the groups, with no reference model: whole, a block at a time and in two pieces, with encryption undoing decryption.
tests/cavp/camellia_cbc.rs::batchestests/cavp/seed_cbc.rs::batchestests/draft_ribose_sm4/sm4_cbc.rs::batchestests/draft_ribose_sm4/sm4_ctr.rs::batches, which checks 50 blocks against each counter block's own keystreamCFB decryption already had such tests for all three ciphers.
I ran the Camellia, SEED and SM4 (and SM4-GCM) tests on ppc64le under
qemu-ppc64le, built with a cross toolchain, and they pass. CI's ppc64le jobs run them on POWER.Benchmarks
Benchmarks run 42512162879: POWER9,
arches=powerpc64, the nine changed modules, againstmainat 0abea26. Medians, base → this PR:Camellia-192 and -256 are 6.7–7.7×. At 64 bytes, less than one batch, every mode is unchanged (0.99–1.02×), and so are the serial modes at every size (CBC and CFB encryption, 0.99–1.00×). The batched modes now cost about what each cipher's ECB does in the audit (Camellia-128 ECB 126 µs, SEED 277 µs, SM4 80 µs at 16 KiB).
Part of #4141.
🤖 Generated with Claude Code
https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT