Skip to content

PPC64LE: Camellia's serial modes on a verified single-block core - #4734

Merged
alex merged 8 commits into
mainfrom
claude/ppc64le-camellia-serial
Nov 8, 2026
Merged

alex merged 8 commits into
mainfrom
claude/ppc64le-camellia-serial

Conversation

@alex

@alex alex commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

On PPC64LE, Camellia's serial modes (CBC and CFB encryption, OFB) and the _final functions encipher one block per step. Until now each step ran the bitsliced core, which costs a whole batch of eight blocks for that one block. This PR adds a verified single-block core and runs those functions on it (a follow-up to finding 5 of the ppc64le audit), as #4698 does for SM4.

Function Core
vg_camellia_cbc_encrypt_chained, vg_camellia_cfb128_encrypt, vg_camellia_ofb single-block (new)
vg_camellia_cfb128_encrypt_final, vg_camellia_cfb128_decrypt_final, vg_camellia_ofb_final single-block (new)
CBC and CFB decryption, CTR bitsliced, eight blocks a step (#4671), unchanged

No TCB or Spec change: each function meets the contract it met before.

How

Impl/Camellia/PPC64LE/Serial.lean (new): Serial.core runs RFC 3713's Feistel network with the halves D1, D2 in r6 and r7.

  • S-boxes. SBOX1 is affine-equivalent to the AES S-box, SBOX1(x) = A_out(S_AES(A_in(x))) (Impl/Camellia/Circuit.lean). SBOX2, SBOX3 and SBOX4 rotate its output or input, which composes with A_out or A_in. An affine map on a byte is two 16-byte nibble tables, which vperm looks up for all eight bytes at once. Bit 4 of an index chooses between two tables. So sLayer:
    • splits the bytes into nibbles (mask 0x0f0f… in r12) and moves them to vector registers;
    • looks up A_in, or A_in ∘ rotl 1 for SBOX4's bytes (selIn);
    • applies vsbox;
    • splits again and looks up A_out, or rotl 1 ∘ A_out for SBOX2's bytes (selOut);
    • looks up rotl 7 ∘ A_out, and takes that for SBOX3's bytes with vsel (mask36).
  • P-function (pFn). Rotations of the two 32-bit halves, as the AArch64 single-block core computes it.
  • Key schedule. The subkeys are read big-endian (ldbrx) from the schedule at r3, in its stored order.
  • Rounds. FL and FLINV run between the groups of six rounds. The one branch is on the public number of rounds (18 or 24, in r28).
  • Tables and scratch. The tables are a static (VG_CAMELLIA_PPC64LE_AFFINE), loaded with lxvd2xTbl. The core keeps the bitsliced core's slots and buffer, so the modes keep their [u64; 422] scratch buffer.

Proofs (Proof/Camellia/PPC64LE/SerialSbox.lean, SerialCore.lean):

  • sbox1_affine … sbox4_affine: the composed maps are SBOX1 … SBOX4 on all 256 bytes. The kernel checks them on truth tables (Proof.Aes.sboxT, two materialized values, lit_decide).
  • sLayer_run: the S layer is substituted. pFn_run: the P-function is linearWord. With f_eq, a round is Spec.Camellia.f.
  • fl_run: FL and FLINV are Spec.Camellia.fl and flinv.
  • six_run, groupFl_run, prefix_run: the rounds are Proof.Camellia.group.
  • crypt_wp: both branches compute cryptWords, so Spec.Camellia.cipher (cipher_bytes, cryptWords_congr).
  • coreSpec : CoreSpec Serial.core. ModesVerified.lean and FbFinalVerified.lean prove the six functions over it. seqPre_of, keyArgs_of and run_wrapped now take the core.
  • Constant time: these functions call nothing, so they are proven with the vector taint analysis (vectorTaint, taint_decide).

Shared lemmas moved, not copied:

  • Proof/Camellia/AArch64/Serial/Pure.lean becomes the architecture-independent Proof/Camellia/SerialPure.lean (namespace VG.Proof.Camellia.Serial), with cryptWords_congr from AArch64's Cipher.lean. The AArch64 files open it; their proofs are unchanged.
  • The register- and vector-byte lemmas of PPC64LE: SM4's serial modes on a verified single-block core #4698's SM4 proof move to Proof/Framework/PPC64LE/VectorBytes.lean, which both cores use.

Tests

The proofs cover correctness, and the existing Camellia CBC, CFB and OFB vector tests cover these functions. I ran them on ppc64le under qemu-ppc64le, and they pass. This PR adds no tests.

Benchmarks

Benchmarks run 42523420396: POWER9, arches=powerpc64, modules camellia_cbc camellia_cfb camellia_ofb, against #4698's head (9e0890b, the code main has since #4698 merged). Medians, base → this PR:

Benchmark 64 B 1 KiB 16 KiB
Camellia-128 CBC encrypt 5.33 → 2.32 µs (2.3×) 63.6 → 23.7 µs (2.7×) 997 → 366 µs (2.7×)
Camellia-256 CBC encrypt 6.68 → 2.78 µs (2.4×) 82.8 → 31.1 µs (2.7×) 1.30 ms → 484 µs (2.7×)
Camellia-128 CFB128 encrypt 5.37 → 2.36 µs (2.3×) 64.0 → 24.1 µs (2.7×) 1.00 ms → 370 µs (2.7×)
Camellia-256 CFB128 encrypt 6.73 → 2.82 µs (2.4×) 83.1 → 31.3 µs (2.7×) 1.31 ms → 487 µs (2.7×)
Camellia-128 OFB 5.37 → 2.36 µs (2.3×) 63.7 → 23.8 µs (2.7×) 997 → 367 µs (2.7×)
Camellia-256 OFB 6.71 → 2.82 µs (2.4×) 82.9 → 31.2 µs (2.7×) 1.30 ms → 485 µs (2.7×)

Camellia-192 matches Camellia-256 (2.4× at 64 B, 2.7× above). CBC and CFB decryption, whose code this PR does not change, are unchanged (1.00× at every size).

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
CBC and CFB encryption, OFB and the `_final` functions take one block a step,
but ran the bitsliced core, which enciphers a batch of sixteen for each.
`Impl/Sm4/PPC64LE/Serial.lean` is a single-block core: the standard's rounds
a word at a time, the S-box as the AES S-box (`vsbox`) between two affine
maps, each two nibble tables looked up by `vperm`. Its proofs
(`Proof/Sm4/PPC64LE/SerialSbox.lean`, `SerialCore.lean`) give its `CoreSpec`,
and the six functions are proven over it, constant time by the vector taint
analysis.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT
#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
CBC and CFB encryption, OFB and the `_final` functions take one block a step,
but ran the bitsliced core, which enciphers a batch of eight for each.
`Impl/Camellia/PPC64LE/Serial.lean` is a single-block core: the standard's
Feistel network on the halves in general-purpose registers, the eight S-boxes
of a round on the vector unit as the AES S-box (`vsbox`) between nibble
tables of affine maps (`vperm`, bit 4 of the index choosing `SBOX4`'s input
map and `SBOX2`'s output map, `vsel` `SBOX3`'s), the P-function by rotations
of the 32-bit halves, FL and FLINV, and a branch on the public number of
rounds. Its proofs (`Proof/Camellia/PPC64LE/SerialSbox.lean`, `SerialCore.lean`)
give its `CoreSpec`, and the six functions are proven over it, constant time by
the vector taint analysis.

The word-level F-function lemmas the AArch64 single-block core's proof had
(`Pure.lean`, and `cryptWords_congr`) move to `Proof/Camellia/SerialPure.lean`,
which both targets use, and the vector-byte lemmas of SM4's to
`Proof/Framework/PPC64LE/VectorBytes.lean`.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHFCoYzWWszXpPsgsoT
@github-actions github-actions Bot added the untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified. label Nov 8, 2026
Base automatically changed from claude/ppc64le-sm4-serial to main November 8, 2026 21:35
@github-actions

github-actions Bot commented Nov 8, 2026

Copy link
Copy Markdown

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

…ia-serial

# Conflicts:
#	lean/VerifiedGarbage/Proof/Sm4/PPC64LE/SerialCore.lean
@alex
alex marked this pull request as ready for review November 8, 2026 21:42
@alex
alex enabled auto-merge November 8, 2026 21:45
@alex
alex added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit ca13d7f Nov 8, 2026
22 of 42 checks passed
@alex
alex deleted the claude/ppc64le-camellia-serial branch November 8, 2026 21:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

untrusted-only This changes exclusively implementations and proofs, and thus is entirely formally verified.

Development

Successfully merging this pull request may close these issues.

2 participants