Optimized
DeFi & Math
Primitives

Banner

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,729 gas - full Black-Scholes option pricing (call or put)

< 1e-12 error - verified against off-chain 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 - + 92 property-based fuzz tests, 2.94M random executions per CI run. See testing methodology

DeFiMath features

FAQ

Common questions

Is DeFiMath right for me?

Yes if you're a Solidity dev whose contract needs more than add/multiply — square roots, logarithms, normal distributions, Black-Scholes, IRR. Common fits: on-chain derivatives, options DEXs, structured-product vaults, lending protocols with non-trivial interest math, risk engines doing real-time portfolio analytics.

Which Solidity version and EVM target does it require?

Solidity ^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?

On-chain Black-Scholes pricing on every quote, IV solving in a single transaction, real-time risk fees on vaults, kurtosis-adjusted VaR, IRR for fixed-income protocols — workflows that were previously off-chain workarounds because the math was too expensive.

What's the license?

MIT. Use it commercially, fork it, embed it in proprietary contracts — no restrictions beyond preserving the copyright notice.

How does it compare to PRBMath, ABDK, Solady?

callOptionPrice 2,729 vs Derivexyz 13,360 (~4.9× cheaper); sqrt 245 vs Solady 341; ln 375 vs Solady 518, PRBMath 6,901. Precision matches Solady within 1 ulp on the shared functions. Full reproducible benchmarks at defimath-compare.

Will it work on Ethereum L2s?

Yes, on any L2 that has rolled forward to the Osaka hard fork (CLZ opcode availability). See supported chains for the live list.

Is the precision good enough for production?

Max relative error below 1e-12 on all math primitives, below 1e-10 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/Options.sol";
See getting started →

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

Development & Optimization

Testing & Audits

Testing & Audits

Web3 Integrations

Web3 Integrations

Let's connect and solve problems together

Support

Support

Need help with integrations? We can help you get started.
Community

Community

Join the conversations, we are building a strong Solidity community.
Academy

Academy

Join our newsletter to unlock tutorials and resources.