LPsLux Proposals
Q-Chain
LP-4040

lp-4040-gpu-kernel-design-four-kernel-template


lp: 4040 title: GPU Kernel Design — the Four-Kernel Template description: The canonical shape every batched Lux crypto and VM kernel takes, why the single-thread commit step is the determinism oracle rather than a parallelization gap, and the variable-time and subgroup rules every kernel inherits author: Lux Core Team (@luxfi) discussions-to: https://github.com/luxfi/lps/discussions status: Final type: Informational category: Core tags: [gpu, kernel, determinism, parallelization, metal, cuda, wgsl, cross-cut]

  • lp-4010

Abstract

Every Lux primitive that consumes a batch of inputs and produces a commitment has the same skeleton:

expand_inputs  →  parallel_eval  →  reduce_or_batch_verify  →  commit_root

This LP defines that skeleton, the rules that follow from it, and — the part that gets misread most often — why the single-threaded final step is deliberate.

The four kernels

StepPatternThreads
expand_inputshoist per-input scratch into device-resident arenas; structure-of-arrays layoutparallel per input or per arena slot
parallel_evalper-input compute (leaf hash, signature verify, MSM window, NTT butterfly)one thread per input
reduce_or_batch_verifyaccumulate partials (Merkle fold, batch pairing, MSM accumulate)parallel where the reduction is associative; serial where it is not
commit_roothash the canonical-order leaves, write the result structone thread

Steps 1, 2 and 4 parallelize trivially. Step 3 parallelizes when the reduction is associative — sum, MSM accumulate, batch-pairing fold — and is serial when it is not: a Keccak fold, a round-chained protocol, UTXO consumption where one transaction spends a previous one's output.

The one-thread fold is the oracle, not a gap

commit_root runs on a single thread. That looks like leftover work someone forgot to parallelize. It is the opposite.

Byte-equal output across CPU, Metal, CUDA and WGSL is guaranteed precisely because step 4 folds in canonical order on one thread. Fan it out and the fold order becomes a function of thread scheduling, and the same inputs on the same device produce different bytes on different runs. The determinism contract that makes the whole byte-equality chain (LP-4010) meaningful lives in that one thread.

The same reasoning covers every other place a kernel narrows to a single lane. Six kinds, and none of them is deferred work:

KindWhy it is one thread
Canonical fold (commit_root)fold order must be reproducible
Canonical locate / collision-resolved slot assignmentslot assignment must match the CPU oracle across the same collision sequence; parallelizing changes the sequence
Round-by-round protocolround N consumes round N−1's transcript
Batch-inversion serial chainMontgomery's trick is one forward sweep, one inversion, one backward sweep — the chain is the reduction
Final reductionthe output is a single element
Service-drain leaderone thread per service group; fan-out comes from group count

Where the template is honored by splitting, not by fanning out

When an operation is structurally serial, the right move is not to force it parallel — it is to split it so the parallel work becomes step 2 and the serial work shrinks to the smallest possible step 3: a one-thread fold over precomputed leaves.

The cases that stay serial per-instance, with the reason:

  1. FROST keygen and signing rounds (LP-4710) — 3-round keygen, 2-round signing; round N depends on round N−1's broadcast transcript. Serial by definition per ceremony. Cross-ceremony batch is template-fit: run many ceremonies in parallel, fold their roots canonically.
  2. CGGMP21 (LP-4720) — same shape as FROST.
  3. IPA rounds within one proof (LP-4845) — log-N rounds, each halving the proof. Serial within a proof; cross-proof batched verify is template-fit.
  4. Fiat-Shamir transcript sampling — top-level challenge sampling is single-thread so the transcript is byte-equal. The NTT / polynomial layer underneath is already SIMD-parallel.
  5. Batch inversion chain (LP-4080) — sequential by algorithm. The parallel work is the per-input field multiplications inside each sweep, which already vectorize.
  6. Attestation parsers — branch-heavy decode of vendor claim formats. Per-input dispatch overhead beats per-input compute; batch at very large N is template-fit but no consensus path reaches that scale.

All six fit the template at the batch level. None can be flattened into one parallel sweep without either changing the protocol or losing byte-equivalence.

Backend classification

Each primitive carries one of these:

ClassMeaning
AGPU-native today: kernel ships on at least one backend, byte-equal CPU oracle test passes
BGPU-feasible: CPU body works, batched-N is naturally parallel, kernel pending
CStructurally CPU-only or round-by-round: single-call dispatch overhead beats compute, or round N depends on round N−1. Cross-batch is template-fit; per-instance is not
DNo first-party CPU body authored yet
EBootstrap-blocked: a C++ body exists but transitively needs a dependency not yet present

The distinction that matters: C is a property of the algorithm; D and E are properties of the calendar. Nothing in the corpus is class C for the wrong reason, and 29 of 29 primitives are template-fit at the algorithmic level. Where coverage is short, the bound is body authoring and dependency bootstrap, not parallelization shape.

Rules every kernel inherits

Variable-time is allowed only on public inputs. MSM bucket indexing, Pippenger window selection, Miller-loop line evaluation and the single Fermat inversion inside batch inversion are all variable-time. That is fine on the verifier side, where every input is already public on-chain. It is not fine on the prover side. A caller with secret scalars — signing nonces, key shares, blinded balances — MUST use the blinded variant (*_blinded: multiplicative scalar randomization before the operation, unblind after) or blind its inputs itself.

Subgroup membership is the caller's job. Pippenger does not reject off-curve or wrong-subgroup points, and neither does a fused Miller loop. Validators MUST subgroup-check public keys before dispatching.

Canonical order is normative. Where a kernel fixes an accumulation order — ascending pair index in a fused Miller loop, left-to-right pairing per tree level in a reduction — that order is part of the specification, not an implementation detail. Changing it changes the output bytes.

Crossover thresholds are measured, not assumed. Every kernel publishes the N below which the dispatcher routes to the CPU or to the simpler algorithm. Below the threshold, dispatch overhead exceeds the algorithmic saving. Thresholds are profiled per device.

Report only what was measured. A benchmark table carries the backend, the device, and the source of the number. Backends that have not run on real hardware get a dash, not a projection.

GPU residency

State and canonical transition logic both live on-device. The CPU handles ingress envelopes, cold-state page service, the attestation handshake and the watchdog — not the hot path. Each VM pairs its GPU engine with a CPU reference body and a determinism test asserting byte-equality between them.

Structural ceilings (intentional, not defects)

  • Single pairing on Metal. Roughly 475 ms per pairing on M1 Max against 510 µs for host blst — about 930× slower at N = 1. The serial Fp12 chain simply does not match SIMD GPU shape. Batched pairing wins; single pairing does not, and the production path is the host computation. On-device single pairing needs CUDA-class hardware.
  • Persistent hot-spinning kernels are broken on Apple Silicon. The Metal substrate uses bounded wave ticks with host re-launch; fan-out comes from group count, not from intra-service thread count.
  • Round-by-round MPC — see the six cases above. Invest in cross-ceremony batching; within-ceremony parallelization is not available without changing the protocol.

References

  • Harris, "Parallel Prefix Sum (Scan) with CUDA" (2007) — tree-reduce shared-memory pattern
  • luxcpp/crypto/gpukit/README.md — the canonical four-kernel embodiment
  • luxcpp/crypto/CROSSOVER.md — measured per-device thresholds

Copyright (C) 2024-2026, Lux Industries Inc. All rights reserved.

Licensed under the MIT License.