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 libgmpmpz_powmas oracle. - Cross-oracle:
intx::umul(schoolbook reference) againstgmp::mpz_mul(Karatsuba reference), equality over N = 1000 random(B, E, M)triples perM_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) | Path | Ratio |
|---|---|---|
| 256 | below cutover, unchanged | 1.0× |
| 512 | below cutover, unchanged | 1.0× |
| 1024 | below cutover, unchanged | 1.0× |
| 2048 | Karatsuba-SOS | host-dependent, from bench |
| 4096 | Karatsuba-SOS | host-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) — addsintx::karatsuba_mul<n>plus threshold dispatch insideintx::umul.luxcpp/crypto/modexp/cpp/modexp.{hpp,cpp}callsintx::umulunchanged; the Karatsuba path activates insideintxwhenn > K_THRESHOLD.- Go canonical
luxfi/crypto/modexpusesmath/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-productsz0,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
- The public-exponent path (EVM modexp over transaction-supplied
e) is variable-time;eis public input. - 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 ofe.BandMremain Karatsuba-multiplied, and that is correct: the leak surface is the exponent's window pattern, not operand size. - 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
Copyright (C) 2024-2026, Lux Industries Inc. All rights reserved.
Licensed under the MIT License.