Skip to content

PPC64LE: Camellia, SEED and SM4 CBC and CFB decryption and counter modes a batch at a time - #4671

Merged
alex merged 4 commits into
mainfrom
claude/ppc64le-block-ciphers
Nov 8, 2026
Merged

alex merged 4 commits into
mainfrom
claude/ppc64le-block-ciphers

Conversation

@alex

@alex alex commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

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:

Function Blocks a step
vg_camellia_cbc_decrypt_chained, vg_camellia_cfb128_decrypt, vg_camellia_ctr 8
vg_sm4_cbc_decrypt_chained, vg_sm4_cfb128_decrypt, vg_sm4_ctr, vg_sm4_ctr32 (and so SM4-GCM) 16
vg_seed_cbc_decrypt_chained, vg_seed_cfb128_decrypt 16

The serial modes (CBC and CFB encryption, OFB) and the _final functions 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 m is now Core.seqWith (Core.steps m) m.ivOut, which emits the same code.
  • Impl/Modes/PPC64LE/Wide.lean (new): Core.seq2 wide narrow lg.
    • It runs the steps of a widened mode (Mode.widen, from Impl/Modes/Wide.lean) while r30 >> lg (computed into r8) is not zero, then the narrow mode's steps.
    • Both loops branch only on r30, the public count.
  • Proof/Modes/PPC64LE/Step.lean and Seq.lean: these are generalized as RISC-V's were.
    • Fixed and Inv take the buffer's bytes a step uses, the regions the key is ready for, and the count a step takes.
    • One step proof (step_wp) serves both loops: loop_wp for one block a step, loopWide_wp for a group.
    • seqWith_changes_wp proves the function around any loops that take every step (LoopsOk).
  • Proof/Modes/PPC64LE/Wide.lean (new): wideSteps_ok proves the loops of seq2, from the architecture-independent Proof/Modes/Wide.lean and WideCtr.lean. From it come cbcDecWide_wp, cfbDecWide_wp, ctrWide_wp and ctr32Wide_wp, with the conclusions of the one-block lemmas.
  • Camellia and SM4: the cores now say they encipher their whole tail buffer (G := 8, G := 16). crypt_wp proves every block of it from ECB's postcondition, as the code already did.
  • Counter modes: the narrow counter modes now 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 are no longer used, so they are deleted.
  • SEED: SEED's blocks function computes sixteen lanes whatever the number of blocks. So core d g is now parameterized by its buffer's number of blocks.
    • The serial modes keep dirCore d = core d 1: same code, same 1184-byte frame.
    • CBC and CFB decryption use wideCore d = core d 16, whose scratch buffer is 174 words (1424-byte frame).
    • modeContract and modeTaint take the scratch size, and τCallW adds the taint summaries of the blocks functions' calls at that size.
  • SM4-GCM: it inlines vg_sm4_ctr32 through ctr32_correct, whose statement is unchanged, so the SM4-GCM proofs needed no change.

Constant time is proven as before (taint_decide_sum over 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::batches
  • tests/cavp/seed_cbc.rs::batches
  • tests/draft_ribose_sm4/sm4_cbc.rs::batches
  • tests/draft_ribose_sm4/sm4_ctr.rs::batches, which checks 50 blocks against each counter block's own keystream

CFB 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, against main at 0abea26. Medians, base → this PR:

Benchmark 1 KiB 16 KiB
Camellia-128 CBC decrypt 64.0 → 9.6 µs (6.6×) 1001 → 133 µs (7.5×)
Camellia-128 CFB128 decrypt 63.8 → 9.5 µs (6.7×) 999 → 131 µs (7.6×)
Camellia-128 CTR 63.8 → 9.8 µs (6.5×) 998 → 135 µs (7.4×)
SEED CBC decrypt 276 → 18.2 µs (15.2×) 4405 → 285 µs (15.5×)
SEED CFB decrypt 276 → 18.1 µs (15.3×) 4421 → 284 µs (15.6×)
SM4 CBC decrypt 79.4 → 7.8 µs (10.2×) 1231 → 85.3 µs (14.4×)
SM4 CFB128 decrypt 79.3 → 7.7 µs (10.2×) 1229 → 84.4 µs (14.6×)
SM4 CTR 79.1 → 7.9 µs (10.1×) 1228 → 88.2 µs (13.9×)
SM4-GCM encrypt 82.6 → 11.2 µs (7.4×) 1234 → 90.8 µs (13.6×)
SM4-GCM decrypt 82.4 → 11.2 µs (7.4×) 1232 → 91.0 µs (13.5×)

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

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
@alex
alex added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit ef28190 Nov 8, 2026
44 checks passed
@alex
alex deleted the claude/ppc64le-block-ciphers branch November 8, 2026 20:45
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
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