freecoding.school100% FREE · NO SIGNUP
The EVM CoreISSUE #17 of 48

precompiles · ecrecover · modexp · pairings

SoliaVSThe Reentrancy Reaper
Solia saysPrecompiles are pseudo-contracts at addresses 0x01-0x09 that expose expensive primitives (ecrecover, modexp, pairings) at gas costs the EVM could not afford to implement in bytecode.

Precompiles are special addresses that execute native code instead of EVM bytecode. They exist because primitives like ecrecover or BN254 pairings are far too expensive to run as Solidity — they need native crypto. The fixed addresses 0x01-0x09 cover signature recovery, hashes (SHA-256, RIPEMD-160), identity (memcpy), modular exponentiation, elliptic-curve add/mul, pairings, and BLAKE2.

You invoke them like any other contract — usually via staticcall or assembly. Gas costs are bespoke: ecrecover is 3,000 flat, modexp depends on input sizes, pairings cost 100,000+ per pairing. Knowing which precompile exists is the difference between an unwritable contract and a one-line call.

Power-ups you unlock

The Reentrancy Reaper attacks — common mistakes

Boss battleList the precompile addresses, their primitives, and approximate gas costs.

Example code

<!doctype html><html><head><meta charset="utf-8"></head>
<body style="background:#06040d;color:#e6e0ff;font-family:monospace;padding:20px"><pre id="o"></pre>
<script>
const precompiles = [
  { addr:'0x01', name:'ecrecover',  gas:'3000',    use:'recover signer from ECDSA sig' },
  { addr:'0x02', name:'sha256',     gas:'60+12/w', use:'SHA-256 hash' },
  { addr:'0x03', name:'ripemd160',  gas:'600+120/w', use:'RIPEMD-160 hash' },
  { addr:'0x04', name:'identity',   gas:'15+3/w',  use:'memcpy (input → output)' },
  { addr:'0x05', name:'modexp',     gas:'variable', use:'modular exponentiation' },
  { addr:'0x06', name:'ecadd',      gas:'150',     use:'BN254 point addition' },
  { addr:'0x07', name:'ecmul',      gas:'6000',    use:'BN254 scalar mul' },
  { addr:'0x08', name:'ecpairing',  gas:'45000+34000/pair', use:'BN254 pairing check' },
  { addr:'0x09', name:'blake2f',    gas:'variable', use:'BLAKE2b F compression' }
];
document.getElementById('o').textContent = [
  'precompiled contracts (native, faster than EVM):',
  '  ' + 'addr'.padEnd(6) + 'name'.padEnd(12) + 'gas'.padEnd(24) + 'use',
  ...precompiles.map(p => '  ' + p.addr.padEnd(6) + p.name.padEnd(12) + p.gas.padEnd(24) + p.use),
  '',
  'called with staticcall; revert on invalid input'
].join('\n');
</script></body></html>
▶ Open the interactive comic issue
‹ Storage Slots · Packing · RefundsCreate Vs Create2 · Deterministic Addresses ›