Skip to content

refactor(prover): centralize u64 limb decomposition into a limbs helper#693

Draft
diegokingston wants to merge 7 commits into
mainfrom
refactor/limb-decomposition
Draft

refactor(prover): centralize u64 limb decomposition into a limbs helper#693
diegokingston wants to merge 7 commits into
mainfrom
refactor/limb-decomposition

Conversation

@diegokingston

@diegokingston diegokingston commented Jun 19, 2026

Copy link
Copy Markdown
Collaborator

What

Centralizes the repeated u64 → 16/32-bit little-endian limb decomposition that was open-coded across the VM trace tables. Adds a small prover/src/tables/limbs.rs module and converts ~20 tables to use it.

The helper (prover/src/tables/limbs.rs)

pub const fn limbs_16(x: u64) -> [u64; 4]   // [x[0..16], x[16..32], x[32..48], x[48..64]]
pub const fn limbs_32(x: u64) -> [u64; 2]   // [lo, hi]
pub const fn limb_16(x: u64, i: u32) -> u64 // (x >> 16*i) & 0xFFFF
pub fn set_limbs_16(data: &mut [FE], col: usize, x: u64) // FE limbs into data[col..col+4]
pub fn set_limbs_32(data: &mut [FE], col: usize, x: u64) // FE limbs into data[col..col+2]

Tests live in the dedicated prover/src/tests/limbs_tests.rs (no inline tests in the production module).

Conversions

set_limbs_16 / set_limbs_32 replace the data[base + cols::X_0] = FE::from(v & 0xFFFF); … blocks; limbs_16 / limbs_32 replace the integer [u32;4]/[u64;…] decomposition arrays. Tables touched: mul, dvrm, decode, cpu, cpu32, commit, load, page, register, memw, memw_aligned, memw_register, lt, branch, eq, store, ec_scalar, ecdas, keccak, keccak_rnd, shift, plus the trace_builder bus-value arrays.

Every converted column group was verified consecutive in its cols module before conversion.

Deliberately left as-is

Parametric column indices (cols_i[..], state_ptr(lane, k), lo_col/hi_col vars), byte (& 0xFF) / nibble (& 0xF) splits, non-standard groupings (e.g. DWordWHH, limbs starting at index 1), sign-fill constants, and the local half closure in trace_builder (already a local dedup).

Correctness

Pure refactor — byte-identical trace output. cargo test -p lambda-vm-prover --lib is unchanged at 322 passed / 104 failed; the 104 failures are pre-existing and environmental (tests that read executor/program_artifacts/*.elf, absent in the sandbox — they fail identically on main). cargo clippy -p lambda-vm-prover is clean. The 5 new limbs_tests cover the helper (LE order, reconstruction, a drift-guard against the open-coded form, and consecutive-column writes).

Centralize the repeated u64 -> 16/32-bit little-endian limb decomposition:
- limbs_16 / limbs_32 / limb_16: integer decomposition
- set_limbs_16 / set_limbs_32: write limbs as FE into consecutive columns

Tests live in the dedicated prover/src/tests/limbs_tests.rs (no inline tests
in the production module). No call sites converted yet.
Replace open-coded 16/32-bit limb decompositions with set_limbs_16 /
set_limbs_32 / limbs_16. Byte-identical (mul_tests + dvrm_tests pass,
global lib test count unchanged at 322 passed / 104 pre-existing env
failures).
Byte-identical (decode tests match baseline: ELF-dependent commitment
tests fail identically with/without the change).
…emw tables

Replace open-coded consecutive LE limb column-writes with set_limbs_16 /
set_limbs_32 across the CPU, COMMIT, LOAD, PAGE, REGISTER, MEMW and
MEMW_A tables. All converted column groups verified consecutive in their
cols modules.

Byte-identical: global lib test count unchanged at 322 passed / 104
pre-existing env failures; clippy clean.
…r arrays

- lt/branch: collapse intermediate limb vars feeding consecutive column
  writes into set_limbs_16 / set_limbs_32 (drops redundant as-uXX casts).
- trace_builder: use limbs_16/limbs_32 for the 4x16 bus-value arrays
  (n/d/r/count_decr halves) and the register pack_value padded array.

Byte-identical: global lib test count unchanged at 322 passed / 104
pre-existing env failures; clippy clean.
Convert the remaining consecutive LE limb column-writes to set_limbs_16 /
set_limbs_32 across EQ, STORE, EC_SCALAR, ECDAS, KECCAK, KECCAK_RND,
MEMW_REGISTER and CPU32. All converted column groups verified consecutive.
Parametric-column / byte / non-standard sites left as-is.

Byte-identical: global lib test count unchanged at 322 passed / 104
pre-existing env failures; clippy clean.
Byte-identical (lib test count unchanged at 322/104); clippy clean.
@diegokingston diegokingston changed the title Refactor/limb decomposition refactor(prover): centralize u64 limb decomposition into a limbs helper Jun 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant