LPsLux Proposals
Q-Chain
LP-4072

lp-4072-karatsuba-modexp


lp: 4072 title: Karatsuba modexp description: Recursive Karatsuba limb multiplication inside modexp for 2048-bit and larger moduli, replacing schoolbook O(n²) with O(n^1.585) above a measured 32-limb threshold author: Lux Core Team (@luxfi) discussions-to: https://github.com/luxfi/lps/discussions status: Draft implementation-rust: none implementation-go: partial implementation-cpp: shipped type: Standards Track category: Core tags: [modexp, karatsuba, bigint, arithmetic, rsa]

  • lp-4070
  • lp-4040

Abstract

Karatsuba multiplication for the modexp precompile (LP-4070) and for RSA-attestation paths at M_len ∈ {512, 1024, 2048, 4096} bits. It replaces schoolbook O(n²) limb multiplication with the recursive O(n^{log₂ 3}) ≈ O(n^{1.585}) split.

The size where this matters: at M_len = 4096 — RSA-4096 attestation — the operand is 64 limbs of 64 bits, and a single exponentiation issues roughly 6,144 multiplications under a 5-bit sliding window. That is where an exponent of 1.585 instead of 2 stops being an asymptotic remark.

The EIP-198 hot path (M_len ≤ 256) sits below the Karatsuba threshold and is unaffected.

Specification

Algorithm

Split n-limb operands A = A_h·B^{n/2} + A_l and B = B_h·B^{n/2} + B_l, where the limb base B = 2^64:

karatsuba_mul(A, B, n):
    if n <= K_THRESHOLD:
        return schoolbook_mul(A, B, n)        # 32-limb cutover
    h = n / 2
    A_l, A_h = split(A, h)
    B_l, B_h = split(B, h)
    z_0 = karatsuba_mul(A_l, B_l, h)
    z_2 = karatsuba_mul(A_h, B_h, h)
    z_1 = karatsuba_mul(A_l + A_h, B_l + B_h, h+1) - z_0 - z_2
    return z_2 * B^{2h} + z_1 * B^h + z_0

Three recursive multiplications and linear-time additions, against four multiplications for schoolbook. K_THRESHOLD = 32 limbs (2048 bits) was chosen by sweeping the crossover on each backend — below 2048 bits schoolbook's tighter inner loop wins, and the recursion's bookkeeping is pure overhead.

modexp wraps it in sliding-window exponentiation:

modexp(b, e, m):
    R = mont_form(1, m)
    pre[i] = mont_mul(b^(2i+1), m) for i in 0..2^{w-1}-1     # window = 5
    for chunk in scan(e, window=5):
        R = mont_sqr(R, m) repeated chunk.zeros + 1 times
        if chunk.window != 0:
            R = mont_mul(R, pre[chunk.window >> 1], m)
    return mont_unform(R, m)

mont_mul reduces with Barrett and calls karatsuba_mul for the limb product when n > K_THRESHOLD. Below that it dispatches to schoolbook unchanged.

Known-answer tests

  • EIP-198 / EIP-2565 reference vectors: PASS (covering M_len ∈ {32, 96, 256} — the schoolbook path).
  • 4096-bit RSA verification vectors at crypto/modexp/test/kat_rsa4096.json, generated with libgmp mpz_powm as oracle.
  • Cross-oracle: intx::umul (schoolbook reference) against gmp::mpz_mul (Karatsuba reference), equality over N = 1000 random (B, E, M) triples per M_len.

Performance

Per-modexp wall clock at e = 65537 (the RSA-2048/4096 verify hot path). The reproducible bench is luxcpp/crypto/modexp/test/modexp_karatsuba_bench.cpp — CIOS schoolbook against Karatsuba-SOS, single-threaded, deterministic PRNG, warmup plus median.

M_len (bits)PathRatio
256below cutover, unchanged1.0×
512below cutover, unchanged1.0×
1024below cutover, unchanged1.0×
2048Karatsuba-SOShost-dependent, from bench
4096Karatsuba-SOShost-dependent, from bench

The 2048 and 4096 ratios are produced by the bench binary at build time and are host-dependent; they are recorded per host rather than asserted here.

Implementation

CPU canonical

  • luxcpp/intx (luxfi fork) — adds intx::karatsuba_mul<n> plus threshold dispatch inside intx::umul.
  • luxcpp/crypto/modexp/cpp/modexp.{hpp,cpp} calls intx::umul unchanged; the Karatsuba path activates inside intx when n > K_THRESHOLD.
  • Go canonical luxfi/crypto/modexp uses math/big, which already switches to Karatsuba above 64 limbs natively.

GPU kernels

  • Metal: modexp_karatsuba.metal — the host driver issues three child command buffers per split (the sub-products z0, z1', z2) plus a fix-up kernel that sums them per the recurrence.
  • CUDA: modexp_karatsuba.cu — three child kernels on independent streams when the split is profitable; schoolbook below threshold.
  • WGSL: modexp_karatsuba.wgsl — three concurrent workgroups.

C-ABI

Unchanged from LP-4070:

int modexp_eip198(const uint8_t *input,    // header + B || E || M
                  size_t         input_len,
                  uint8_t       *output,
                  size_t         output_len);

The Karatsuba path is internal to intx and invisible to callers. That is the point — a caller should not have to know which multiplication algorithm ran.

Security Considerations

  1. The public-exponent path (EVM modexp over transaction-supplied e) is variable-time; e is public input.
  2. The secret-exponent path (RSA-2048/4096 attestation, RSA-PKCS#1 v1.5 signing) MUST set intx::ct_modexp. That disables sliding-window optimization in favor of regular fixed-window square-and-multiply and removes the Karatsuba branch on the scanning of e. B and M remain Karatsuba-multiplied, and that is correct: the leak surface is the exponent's window pattern, not operand size.
  3. This LP adds only a recursive divide-and-conquer wrapper around an existing audited umul. No new modular-arithmetic surface.

Crossover

K_THRESHOLD = 32 limbs (2048 bits) for the limb multiplier itself. At the dispatcher: M_len ≤ 256 bytes → schoolbook, M_len > 256 bytes → Karatsuba. The GPU kernel uses the same threshold; below it the host driver issues a single-workgroup schoolbook multiplication so the bytes still match the CPU body.

References

  • Karatsuba, Ofman, "Multiplication of multidigit numbers on automata" (1962)
  • Knuth, "TAOCP" Vol 2 §4.3.3
  • Bernstein, "Multidigit multiplication for mathematicians" (1999) — threshold methodology
  • EIP-198, EIP-2565

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

Licensed under the MIT License.