LPsLux Proposals
Q-Chain
LP-4080

lp-4080-batched-montgomery-inversion


lp: 4080 title: Batched Montgomery Inversion description: Montgomery's trick — one field inversion plus 3(N-1) multiplications in place of N inversions — as a single curve-parameterized kernel shared by every curve in the corpus author: Lux Core Team (@luxfi) discussions-to: https://github.com/luxfi/lps/discussions status: Final implementation-rust: none implementation-cpp: shipped implementation-go: partial type: Standards Track category: Core tags: [arithmetic, inversion, montgomery, gpu, secp256k1, bn254, bls12-381]

  • lp-4040

Abstract

Montgomery's batched inversion replaces N independent field inversions with one inversion and 3(N−1) multiplications. Since an inversion costs roughly 256 squarings plus 128 multiplications for a 256-bit field and a multiplication costs one Montgomery reduction, the saving is close to two orders of magnitude at moderate N.

The kernel is written once, parameterized over the field, and shared by every curve — secp256k1, BN254 Fp/Fr, BLS12-381 Fp/Fr, Banderwagon Fp. Four near-identical hand-written kernels became one.

Specification

Algorithm

Given a_0, …, a_{N−1} in a field F_q (Montgomery form):

# Forward prefix product
p_0 = a_0
for i in 1..N-1:
    p_i = p_{i-1} * a_i

# Single inversion
inv = p_{N-1}^{-1}        # one Fermat exponentiation: a^(q-2) mod q

# Backward sweep
for i in N-1 .. 1:
    out_i  = inv * p_{i-1}
    inv    = inv * a_i
out_0 = inv

Cost: 1 inversion + 3(N−1) multiplications.

Why this kernel is single-threaded

The forward prefix product and the backward sweep are serial by construction — p_i depends on p_{i−1}, and the running inv depends on the previous inv. The kernel runs as one workgroup of one thread. Per LP-4040 this is the "batch-inversion serial chain" case: the chain is the reduction, and byte-equality across CPU, Metal, CUDA and WGSL is unconditional as a result. The parallel work in this pattern lives in the per-element multiplications inside each sweep, which vectorize on their own.

Performance

Speedup against N independent Fermat inversions, same curve, same hardware:

BackendN=64N=256N=1024
Metal (M1 Max)6×11×14×

CUDA and WGSL figures are recorded from the linux-amd64 CI lane when they land. macOS development builds run Metal only, by design.

Implementation

CPU canonical

luxcpp/crypto/gpukit/cpp/cpu_reference/batch_inversion.{hpp,cpp}, parameterized over field primitives supplied through curve_traits<C> (mul, sqr, inv). Per-curve headers consume the specializations.

GPU kernels

gpukit/gpu/{metal,cuda,wgsl}/batch_inversion.* hold the canonical body; each curve gets a thin wrapper at crypto/<curve>/gpu/<backend>/<curve>_batch_inv.*.

C-ABI

// gpukit C-ABI shim (one entry, curve_id selects parameter set)
int gpukit_batch_inv(uint8_t curve_id,
                     const uint8_t *in,    // N * field_bytes Montgomery form
                     uint8_t       *out,   // N * field_bytes Montgomery form
                     uint32_t       n);

// Per-curve thin wrappers re-exported under each curve's c-abi:
int secp256k1_batch_inv_fp(const uint8_t *in, uint8_t *out, uint32_t n);
int bn254_batch_inv_fr    (const uint8_t *in, uint8_t *out, uint32_t n);
int bls12_381_batch_inv_fp(const uint8_t *in, uint8_t *out, uint32_t n);
int banderwagon_batch_inv (const uint8_t *in, uint8_t *out, uint32_t n);

Determinism harness

gpukit/test/batch_inversion_test.{cpp,mm,cu} — N ∈ {1, 2, 8, 64, 256, 1024, 4096}, 100 random inputs each, byte-equal across every backend. Cross-oracles: gnark-crypto BatchInvert for BN254 and BLS12-381, bitcoin-core/secp256k1 secp256k1_fe_inv_var for secp256k1.

Security Considerations

  1. The caller MUST ensure no input is zero. The kernel does not check. A single zero anywhere in the batch zeroes the prefix product and corrupts every output, not just its own. Callers working on coordinate batches that may include the point at infinity must filter first.
  2. The single Fermat inversion is variable-time. Callers handling secret scalars — signing nonces, key shares — must use the constant-time variant gpukit_batch_inv_ct or blind their inputs.
  3. On the verifier-side paths this kernel serves (batch ECDSA verify, batch ecrecover, MSM bucket inversion) inputs and outputs are public. Secrecy is the consumer's concern, not the kernel's.

Crossover

Below N* = 8 the per-dispatch overhead exceeds the algorithmic saving and the dispatcher routes to the per-element CPU path. Above it: N* = 8 on Metal, 16 on CUDA, 32 on WGSL. Thresholds are profiled per device.

References

  • Montgomery, "Modular Multiplication Without Trial Division" (1985)
  • Knuth, "TAOCP" Vol 2 §4.6.1 — modular batch inversion

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

Licensed under the MIT License.