Skip to content

PPC64LE: SEED's serial modes on a verified single-block core - #4746

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

alex merged 11 commits into
mainfrom
claude/ppc64le-seed-serial

Conversation

@alex

@alex alex commented Nov 8, 2026 •

Copy link
Copy Markdown
Member

On PPC64LE, SEED's serial modes (CBC and CFB encryption, OFB) and the _final functions encipher one block per step. Until now each step called the blocks function on one block, and that function computes the rounds of sixteen lanes whatever the number of blocks. This PR adds a verified single-block core and runs those functions on it. It follows the SM4 (#4698) and Camellia (#4734) PRs and finding 5 of the ppc64le audit.

Function Core
vg_seed_cbc_encrypt_chained, vg_seed_cfb128_encrypt, vg_seed_ofb single-block (new)
vg_seed_cfb128_encrypt_final, vg_seed_cfb128_decrypt_final, vg_seed_ofb_final single-block (new)
CBC and CFB decryption the blocks function, sixteen blocks a step (#4671), unchanged

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

How

Impl/Seed/PPC64LE/Serial.lean (new): Serial.core, a Modes.PPC64LE.Core that runs RFC 4269's sixteen rounds on one block. The halves are in general-purpose registers, and the rounds are unrolled, so the halves trade registers every round.

  • S-boxes. SEED's S1 and S2 are each affine-equivalent to the AES S-box: S(x) = A_out(S_AES(A_in(x))), with the same A_in for both. gFn computes G on the vector unit:
    • it splits the word into nibbles (and and srwi with the mask 0x0f0f0f0f);
    • it moves them to vector registers (mtvsrd);
    • it looks up A_in as two 16-byte nibble tables (vperm, XORed);
    • vsbox applies the AES S-box;
    • it looks up A_out the same way. A selector table sets bit 4 of the index for the bytes X1 and X3, so that vperm picks S2's table for them.
  • G's mixing. vperm broadcasts each byte's image to a word, vand applies G's masks, and two vsldoi rotations with XORs sum the four words. mfvsrd moves the word back.
  • Constant time. No address or branch depends on the key or the data.
  • Tables. The nine tables are a static (VG_SEED_PPC64LE_AFFINE), loaded with lxvd2xTbl.
  • Key and scratch. The round keys are read in place from the schedule at r3, so prepare is empty. The core's slots and buffer are those of the blocks function's core on one block, so every mode keeps its [u64; 144] scratch buffer. bufSlot moves to Serial.lean, so that Modes.lean can import it.

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

  • s0_affine, s1_affine: on all 256 bytes, the tables' affine maps around the AES S-box are S1 and S2. The kernel evaluates the AES S-box of A_in's images on truth tables (Proof.Aes.sboxT, materialized). lit_decide checks them against the S-boxes' truth tables (Proof.Seed.tableT).
  • gA_run, gB_run, gFn_run: G, against Spec.Seed.g (through Proof.Seed.getLsbD_g).
  • fRound_run, round_run, rounds_run: F, a round with the alternating registers (Spec.Seed.round), and the sixteen (Proof.Seed.roundsN).
  • crypt_wp: the tables, the block's big-endian words (lwbrx), the rounds and the swapped store compute Spec.Seed.cipher (Proof.Seed.crypt_eq).
  • coreSpec : CoreSpec Serial.core, with the blocks function's one-block core's KeyArgs and Ready.
  • ModesVerified.lean and FbFinalVerified.lean prove the six functions from the modes' existing lemmas, over Serial.coreSpec. seqPre_of and keyArgs_of now take the core.
  • Constant time: these functions call nothing, so they are proven with the vector taint analysis (vectorTaint, taint_decide) rather than the slot analysis with the blocks functions' summaries.

Tests

The proofs cover correctness. The existing SEED CBC, CFB and OFB tests cover these functions, and they pass on ppc64le under qemu-ppc64le. This PR adds no tests.

Benchmarks

Benchmarks run 42525825207: POWER9, arches=powerpc64, modules seed_cbc seed_cfb seed_ofb, against #4734's head (c8c32ce, the code main has since #4734 merged). Medians, base → this PR:

Benchmark 64 B 1 KiB 16 KiB
SEED CBC encrypt 19.2 → 3.47 µs (5.6×) 280 → 50.4 µs (5.6×) 4.47 → 0.80 ms (5.6×)
SEED CFB128 encrypt 18.0 → 3.51 µs (5.1×) 281 → 50.4 µs (5.6×) 4.49 → 0.80 ms (5.6×)
SEED OFB 17.7 → 3.51 µs (5.0×) 277 → 50.3 µs (5.5×) 4.44 → 0.80 ms (5.6×)

CBC and CFB decryption, whose code this PR does not change, stayed within 1% (0.99–1.00×).

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
The serial modes (CBC and CFB encryption, OFB) and the _final functions
encipher one block per step, but the blocks function computes sixteen
lanes whatever the number of blocks. Serial.core enciphers the one block
of the buffer with RFC 4269's rounds in general-purpose registers, G's
S-boxes on the vector unit through the AES S-box (each is affine-
equivalent to it), with its proof (SerialSbox, SerialCore) and the
modes' proofs 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
@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-camellia-serial to main November 8, 2026 21:58
@alex
alex marked this pull request as ready for review November 8, 2026 22:18
@alex
alex added this pull request to the merge queue Nov 8, 2026
Merged via the queue into main with commit 85135ee Nov 8, 2026
24 of 25 checks passed
@alex
alex deleted the claude/ppc64le-seed-serial branch November 8, 2026 22:45
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