lp: 4070 title: modexp — Big-Integer Modular Exponentiation description: The EIP-198 modexp precompile at 0x05 — arbitrary-precision b^e mod m, its EIP-2565 gas formula, and the constant-time discipline that separates the public-exponent path from the secret-exponent one author: Lux Core Team (@luxfi) discussions-to: https://github.com/luxfi/lps/discussions status: Final implementation-rust: shipped implementation-cpp: shipped implementation-go: shipped type: Standards Track category: Core tags: [modexp, bigint, precompile, arithmetic, rsa, eip-198]
- lp-4010
- lp-4040
Abstract
The EVM precompile at 0x05 computes arbitrary-precision modular
exponentiation b^e mod m. It is what RSA verifiers, BLS12-381
(LP-4110) cofactor clearing, and several proof-of-work variants are
built on.
Specification
Interface
- Precompile address:
0x05 - Input:
(B_len, E_len, M_len, B, E, M)— three 32-byte length prefixes followed by big-endian byte strings - Output:
B^E mod M, big-endian, lengthM_len - Gas, per EIP-2565:
gas = max(200, mult_complexity · iter_count / G_QUADDIVISOR)withG_QUADDIVISOR = 3
Algorithm
Sliding-window exponentiation with Montgomery REDC for odd modulus. For an even modulus, fall back to schoolbook exponentiation with Barrett reduction — Montgomery form requires an odd modulus to have an inverse mod 2^k.
Conditional swaps are branchless, to keep the secret-key path free of a data-dependent branch.
Known-answer tests
- EIP-198 / EIP-2565 reference vectors: PASS
- Wycheproof RSA-OAEP and RSA-PKCS1 vectors: PASS (transitive correctness — a modexp bug shows up as an RSA failure)
crypto/modexp/test/kat.json
Implementation
Go canonical
luxfi/crypto/modexp — math/big plus a Montgomery wrapper.
C++ CPU canonical
luxcpp/crypto/modexp/cpp/modexp.{hpp,cpp}, built on the intx fork.
C-ABI at luxcpp/crypto/modexp/c-abi/modexp_capi.h, shipped as
libmodexp.a.
GPU kernels
- Metal:
luxcpp/crypto/modexp/gpu/metal/modexp.metal— batched modexp for BLS12-381 cofactor clearing (LP-4110's hot path). - CUDA and WGSL: scaffold pending. Deliberately low priority — verifier batches are dominated by pairing cost, not by modexp.
Determinism
CPU↔GPU byte-equality over N = 1000 random (B, E, M) tuples with
M_len ∈ {32, 96, 256, 512, 1024}: PASS.
Test oracles
Ethereum execution-spec-tests EIP-198/2565 vectors, and libgmp
mpz_powm (test-only).
Security Considerations
- The two exponent paths have different rules. When
Ecomes from transaction calldata it is public, and windowed sliding — which leaks the exponent's window pattern through timing — is both faster and harmless. WhenEis a secret RSA exponent, it is not. Callers on the secret path must select the constant-time variant; see LP-4072 for the flag and what it turns off. - Gas metering is what prevents a denial-of-service through a large
M_len. - The big-integer layer (
intx) is audited upstream.
References
- EIP-198 — Byzantium modexp precompile
- EIP-2565 — Berlin gas-cost repricing
- Hankerson, Menezes, Vanstone, "Guide to Elliptic Curve Cryptography" §2.1
Copyright
Copyright (C) 2024-2026, Lux Industries Inc. All rights reserved.
Licensed under the MIT License.