Optimized
DeFi & Math
Primitives

Introducing DeFiMath
The math layer for on-chain finance
Pricing options on-chain used to mean off-chain oracles or hand-rolled approximations. DeFiMath gives you 40+ Solidity primitives - Black-Scholes, Greeks, implied vol, rates, statistics - at gas costs lower than a single storage read, with precision under 1e-12.
2,582 gas - full Black-Scholes option pricing (call or put) at < 5e-12 rel. error
< 1e-12 rel. error - on math primitives, verified against reference implementations
40+ primitives - Black-Scholes, binaries, Greeks, IV solver, futures, rates, statistics. See the full list →
0 dependencies - pure Solidity ^0.8.31, MIT licensed
100% test coverage - + 114 property-based fuzz tests, 3.65M random executions per CI run. See testing methodology →

Stay Updated
Latest news
DeFiMath v4.1.0: φ(x) joins Φ(x)
The normal CDF has been in DeFiMath for as long as it has priced options. v4.1.0 adds its derivative — stdNormPDF, the standard normal density — at 320 gas, completing the Gaussian pair. Drop-in from v4.0.0.
DeFiMath v4.0.0: Black-76 options on futures, and a cleaner API
The first major release since v3.0.0. A new Black-76 module prices European options on a future — call, put, the four Greeks, and a Newton-Raphson IV solver, from 2,552 gas. And every library is renamed for a cleaner API — DeFiMathOptions becomes BlackScholes, finance/ splits into rates/ and statistics/, and redundant function prefixes are gone. Plus an expired-put delta fix.
DeFiMath v3.7.0: a rebuilt exp cascades across the library
exp is rebuilt on a two-stage range reduction plus a [3,3] Padé approximant — 327 → 289 gas with a tighter error bound. Because exp underpins Black-Scholes pricing, the Greeks, the IV solver, futures, and rate math, the savings cascade — callOptionPrice −126, continuousToDiscrete −116, expm1 −112. Error bounds tightened across primitives, and all gas + precision figures now live in a single Constants.mjs asserted in CI.
From the blog
How I Wrote A Fixed-Point Solidity Sqrt That Runs In 197 Gas
A step-by-step walkthrough of writing a fixed-point sqrt in Solidity at 197 gas — from the first design decision to the final assembly, with gas benchmarks measured along the way.
Counting leading zeros in Solidity using CLZ opcode
How DeFiMath uses the new CLZ opcode (EIP-7939) to power sqrt, cbrt, and ln — initial guesses and range reduction in a single instruction.
Custom Development
We can help you with your next Solidity Project
Contact us today to discuss your project and find out how we can help you bring your vision to life.
Development & Optimization | |
Testing & Audits | |
Web3 Integrations |
FAQ
Common questions
Is DeFiMath right for me?
Which Solidity version and EVM target does it require?
^0.8.31 and evmVersion: "osaka". The 0.8.31 floor is the first version to expose the clz Yul builtin (EIP-7939), which powers sqrt, cbrt, and ln. Add both to your hardhat.config.js / foundry.toml.What use cases does this unlock?
What's the license?
How does it compare to PRBMath, ABDK, Solady?
call 2,582 vs Derivexyz 13,360 (~5.2× cheaper); sqrt 197 vs Solady 384; ln 390 vs Solady 518, PRBMath 6,914. Precision matches Solady within 1 ulp on the shared functions. Full reproducible benchmarks at defimath-compare.Will it work on Ethereum L2s?
CLZ opcode availability). See supported chains for the live list.Is the precision good enough for production?
1e-12 on all math primitives, below 5e-12 on Black-Scholes option pricing. Every function is validated continuously against simple-statistics, black-scholes, greeks, and math-erf reference libraries. 100% test branch coverage.How do I install it?
npm install defimath-lib, then import the module you need:import "defimath-lib/contracts/derivatives/BlackScholes.sol";See getting started →