Skip to content

Emit a dispatcher for every table of functions - #4468

Merged
alex merged 2 commits into
mainfrom
claude/table-dispatchers
Nov 7, 2026
Merged

alex merged 2 commits into
mainfrom
claude/table-dispatchers

Conversation

@alex

@alex alex commented Nov 7, 2026

Copy link
Copy Markdown
Member

This is a TCB change (lean/VerifiedGarbage/TCB/Rust.lean, the emitter). It adds Rust that the emitter writes; no ISA model, contract, Spec/ or proof changes.

Why

#4428 gave every function a generated dispatcher, <fn>_auto, so that the Rust code stops choosing implementations by CPU feature. The hash functions can't follow until their tables of functions get the same. RSAES-OAEP, RSASSA-PSS and deterministic ECDSA take a hash function as a table (VG_HASH_SHA256, _SHANI, _AVX2, …). Today the Rust chooses which one through hashes::tables::TableHash → <Hash>Backend::select(f).tables(). This PR adds the generated choice; the hash migration that uses it (deleting every <Hash>Backend) follows separately.

What the emitter now writes

Each crate gets dispatch/fn_tables.rs (Rust.tableDispatcher). For every table that needs no CPU feature, it gets a safe function returning the best table the CPU supports among its implementations:

#[inline]
pub fn vg_hash_sha256_auto() -> &'static crate::fn_tables::Hash {
    let cpu = verified_garbage_cpu::detected();
    if cpu.contains(crate::fn_tables::VG_HASH_SHA256_SHANI_FEATURES) {
        return &crate::fn_tables::VG_HASH_SHA256_SHANI;
    }
    if cpu.contains(crate::fn_tables::VG_HASH_SHA256_AVX2_FEATURES) {
        return &crate::fn_tables::VG_HASH_SHA256_AVX2;
    }
    &crate::fn_tables::VG_HASH_SHA256
}
  • Implementations of a table (Rust.tableGroupsOf): the tables of the same interface and the same words of data whose names are its own followed by _<SUFFIX>.
  • Order: the suffix in lowercase is ranked by VG.Dispatch.ranking in the module of the table's first function, exactly as for functions, with extra, vendor and compound suffixes.
  • Other cases: a table that needs CPU features gets no dispatcher. A table with no other implementation gets one that returns it.

Why returning any of them is sound

A function that takes tables is proven for every table of their interface (Abi.withTables: the parameter holds the words of one of them). What it computes depends on which one only through the table's data. For example, OAEP's contract names the hash function at the index in the table's first word of data (HashTable.hashAt, Spec/RsaOaep/Tables.lean). So tables with the same interface and data give the same results, and the dispatcher only checks that the CPU has the chosen table's features.

Requiring equal data is what keeps, say, VG_HASH_SHA512_224 from being taken as a variant of VG_HASH_SHA512. A test checks this case. Abi.withTables's documentation already said "the Rust caller chooses the table by the CPU's features".

Refactor

The function dispatchers' Arm now holds an Impl (a name, the module of its item and features constant, its features and cfg predicates) instead of an Artifact, so that tables share armsOf, condOf and the ranking. The generated function dispatchers are byte for byte unchanged: the only changes under crates/ are each target's new dispatch/fn_tables.rs and its mod line.

Checks

  • Choices: the generated choices match today's <Hash>Backend::select. For example, x86-64 tries SHA-NI, then AVX2, then the baseline, and AArch64's SHA-512 tries _SHA3, then the baseline.
  • New guards in VerifiedGarbageTest/Dispatch.lean, on made-up tables:
    • the grouping, including a table whose name extends another's but whose data differs;
    • the ranking's order and cfg gates;
    • a table with no other implementation;
    • a table that needs a feature, which gets no dispatcher;
    • an unranked suffix;
    • a dispatcher name that clashes with an artifact.
  • Coverage: ci/merge_rust_coverage.py drops an unreached table dispatcher (pub fn) as it does a function's, with a test. Nothing calls them yet.
  • Docs: CLAUDE.md and lean/README.md mention the table dispatchers.
  • Locally:
    • every Static checks step;
    • the full emitter;
    • cargo fmt --check;
    • Clippy (-D warnings) on x86-64, AArch64, ARMv7, x86, RISC-V and PPC64LE;
    • the test binaries build.
  • Speed: no benchmark, since nothing calls the new functions yet. The hash migration will be benchmarked.

Generated by Claude Code

`dispatch/fn_tables.rs` of each crate gets `<table>_auto()`, a safe
function returning the best table the CPU supports of those of the same
interface and data whose names are the table's own followed by
`_<SUFFIX>` (`Rust.tableGroupsOf`), ranked as their suffixes in lowercase
in the module of the table's first function (`Rust.tableDispatcher`).
A function taking tables is proven for every table of their interface,
and computes the same with any of the same data (`Abi.withTables`), so the
Rust code can call it with the dispatcher's table, as it calls a
function's dispatcher, instead of choosing the table itself, as
`hashes::tables::TableHash` does today.

The function dispatchers' `Arm` now holds an `Impl` (a name, a module, its
features and `cfg` predicates) instead of an `Artifact`, so that both kinds
share `armsOf`, `condOf` and the ranking. The coverage merge drops an
unreached table dispatcher (`pub fn`) as it does a function's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHE2dqL6Q2PDogxfD5f
@alex
alex enabled auto-merge November 7, 2026 15:42
@alex
alex added this pull request to the merge queue Nov 7, 2026
Merged via the queue into main with commit d0caf97 Nov 7, 2026
41 checks passed
@alex
alex deleted the claude/table-dispatchers branch November 7, 2026 16:28
alex pushed a commit that referenced this pull request Nov 7, 2026
Main removed declarations only this branch still used (#4336, #4367,
#4514): `fin64_exec_li_lit`, the RISC-V Miller–Rabin search pieces the
sieved parameter candidate calls, and three of `ModExpWin`'s WP lemmas.
They return, unchanged, as FFDH-only modules (`Proof/Ffdh/RISCV64/Li.lean`,
`Impl/Ffdh/RISCV64/Search.lean`, `Proof/Ffdh/RISCV64/Search.lean`) and
local lemmas in `Cached/Rows.lean`. The registrations drop `consts :=`
(#4380). The generated code takes #4514's Montgomery schedule and #4468's
dispatcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHEKNw8eCDhgbwoFBL
alex pushed a commit that referenced this pull request Nov 15, 2026
Main removed declarations only this branch still used (#4336, #4367,
#4514): `fin64_exec_li_lit`, the RISC-V Miller–Rabin search pieces the
sieved parameter candidate calls, and three of `ModExpWin`'s WP lemmas.
They return, unchanged, as FFDH-only modules (`Proof/Ffdh/RISCV64/Li.lean`,
`Impl/Ffdh/RISCV64/Search.lean`, `Proof/Ffdh/RISCV64/Search.lean`) and
local lemmas in `Cached/Rows.lean`. The registrations drop `consts :=`
(#4380). The generated code takes #4514's Montgomery schedule and #4468's
dispatcher.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01TKUFHHEKNw8eCDhgbwoFBL
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