LPsLux Proposals
Precompiles
LP-9061

DeFi Precompile Gas Benchmarks

Draft

Measured gas cost of Lux native DeFi precompiles versus the equivalent Solidity implementation, with explicit per-figure provenance. The evidence layer for the native-precompile thesis.

Category
DeFi
Created
2026-06-27

Implementation status (code-audited 2026-07-03): PARTIAL Gas constants confirmed in code (secp256r1 3450, Poseidon 700, V4 swap 50k — precompile/*/contract.go); no committed harness reproduces the measured tables; StableSwap Newton loop is now metered (real get_dy ≈15,240 gas).

LP-9061: DeFi Precompile Gas Benchmarks

Abstract

This LP quantifies, with explicit provenance, how much gas Lux's native DeFi precompiles save versus the equivalent Solidity implementation. It is the evidence layer under the native-precompile design (LP-078, lp-9060): the registry says what exists, the protocol LPs say how it behaves, this LP says what it costs. No architecture or roadmap is duplicated here — only measured numbers.

Method

  • Native gas — executed, not cited. Each figure is produced by importing the real precompile package (github.com/luxfi/precompile/...) and printing the charged RequiredGas — flat constants directly, runtime formulas (modexp, bls.PairingGas, poseidon.RequiredGas) by calling them. Constants are cross-checked against source.
  • Solidity-equivalent — tagged by provenance. No foundry/hardhat harness exists in-repo (verified: zero foundry.toml/*.t.sol/.gas-snapshot), so the Solidity side is not measured in-repo. Each Solidity figure is tagged: measured (third-party benchmarked), published (EIP/vendor doc), or reasoned (derived from authoritative per-opcode costs).
  • No fabrication. Where a pure-Solidity implementation is genuinely infeasible (modexp at RSA sizes, BLS12-381 pairing, KZG), the row is marked enablement, not given an invented number.

Results

Protocol / opNative gasSolidity-equivalentProvenanceSavings
Curve StableSwap exchange/get_dy5,000130,126 mean (85,958–150,079, 536 txns)measured (curvefi)26.0× (17–30×)
secp256r1 / P-256 verify3,450~330,000 (FreshCryptoLib software)published (RIP-7212)95.7×
Poseidon hash-pair (BN254)70021,124 (poseidon-solidity) / 32,173 (circomlib)measured30.2× (46×)
Uniswap-V2 constant-product math5,000~127,000published25.4×
DEX V4 swap (0x9999 settlement)50,000≥127,000 (AMM floor; CLOB higher)published / reasoned≥2.5×
modexp RSA-2048 (e=65537)5,461pure-EVM bignum infeasible (OOG)published (EIP-198)enablement
BLS12-381 pairing (1 pair)108,000pure-Solidity infeasible (millions)published (EIP-2537)enablement
KZG point-eval (0x0a)50,000needs BLS12-381 pairing → infeasiblereasoned (EIP-4844)enablement

Supporting native constants (executed): bls12381 G1Add 500 / G1Mul 12,000 / G2Mul 45,000 / Pairing(2) 151,000; kzg4844 50,000; blake3-256 103; modexp 4096-bit worst 5,591,040 (Byzantium native was 22,853,376 → EIP-2565 cut ~3.8×).

Interpretation

Two regimes — state them separately; do not conflate.

  1. Speedup — for ops expressible in Solidity (AMM swap, Curve exchange, P-256, Poseidon): a concrete 2.5×–96×. Lead with the two independently-measured rows: P-256 verify 95.7× and Curve StableSwap 26× (curvefi's own 536-txn dataset).
  2. Enablement — for ops not practically expressible (modexp at RSA sizes, BLS12-381 pairing, KZG): the precompile is the only viable implementation; the Solidity loop exceeds the block gas limit. This is why Ethereum ships these (EIP-198/2537/4844), not a Lux optimization.

Honesty notes for reviewers:

  • The DEX V4 swap is 2.5×, not 25× — its 50k bundles real custody + settlement, not just curve math. The 25× applies to raw AMM math (stableswap base), which a real swap still wraps in ERC-20 transfers.
  • stableswap charges a flat GasBase=5000 while running up to 256 Newton iterations unmetered in-process (stableswap/contract.go). The 5,000 is the genuine native cost; the unpriced loop is a DoS-pricing question (LP-079 / security review), independent of the savings claim.
  • secp256r1 is 3,450 today; EIP-7951 reprices the canonical precompile to 6,900 — still 48×. Decide whether Lux tracks EIP-7951.

Reproduce

Native column: import each precompile/<pkg> + luxfi/geth/core/vm, print RequiredGas, build CGO_ENABLED=0 GOWORK=off (the cgo path needs absent Metal/accel libs; pure-Go fallbacks give identical gas). To make every Solidity figure measured rather than partly reasoned, stand up a forge --gas-report over precompile/solidity/{swap,dex}/*.sol, stableswap/IStableSwap.sol, and a UniswapV2 pair — then this table cites in-repo gas snapshots.

Sources

curvefi/curve-gas-estimates (536 txns) · RIP-7212 / EIP-7951 · chancehudson/poseidon-solidity · Uniswap v2-periphery #173 · arXiv 2309.13648 (Uniswap swap cost) · EIP-198 / EIP-2565 (modexp) · EIP-2537 (BLS12-381) · EIP-4844 (KZG) · EIP-2929/2200 (opcode costs).

Copyright and related rights waived via CC0.