DeFiMath is a pure-Solidity library of gas-optimized 18-decimal fixed-point math primitives — 40+ functions spanning six modules: math, options, binary options, futures, rates, and statistics.
2,729 gas
Black-Scholes option pricing
< 1e-12
Max abs. error on options pricing
100%
Test coverage · Solidity 0.8.35
0
Runtime dependencies
Full test coverage across Hardhat and Foundry property-based fuzz suites. MIT-licensed, no runtime dependencies.
Exp, log, sqrt, pow, standard normal CDF, error function, and more.
Black-Scholes pricing, full Greeks, and an iterative implied-volatility solver.
Cash-or-nothing call and put pricing with full Greeks.
Continuous-compounding futures price.
Compound interest, present value, log returns, YTM, IRR.
Mean, std dev, historical volatility, Sharpe, max drawdown, VaR, CVaR.
Headline functions vs. the next-best on-chain implementation in each category:
| Function | DeFiMath | Next best | Multiple |
|---|---|---|---|
| callOptionPrice | 2,729 | 13,360 (Derivexyz) | 4.9× |
| putOptionPrice | 2,739 | 13,363 (Derivexyz) | 4.9× |
| binaryCallPrice | 2,018 | 16,218 (Haptic) | 8.0× |
| delta | 1,724 | 8,621 (Derivexyz) | 5.0× |
| vega | 1,436 | 7,490 (Derivexyz) | 5.2× |
| ln | 375 | 518 (Solady) | 1.4× |
| sqrt | 245 | 341 (Solady) | 1.4× |
| cbrt | 368 | 550 (Solady) | 1.5× |
| stdNormCDF | 660 | 2,794 (SolStat) | 4.2× |
Full per-function tables in the defimath-compare README.
DeFiMath ships with two independent test layers. Each library function is the unit — its own describe block with a fixed taxonomy of sub-tests, so anyone auditing the suite can find the exact coverage for any function in seconds.
604 tests validating against external JavaScript references (JS Math, math-erf, black-scholes, greeks, simple-statistics) at concrete points across the operational domain. Every function in every module follows the same five-category taxonomy:
Math.random()assert.equal (fails on both regression AND improvement). Gas threshold is in the test name, so any change shows up in the PR diff.92 mathematical properties × 32,000 random runs each = 2,944,000 random executions per CI run. Validates the algebraic structure of the library, not just concrete points. Foundry automatically shrinks counterexamples on failure. Properties are organized into five categories:
681
Total tests (Hardhat + Foundry)
2,944,000
Random executions per CI run
< 1 min
Full suite wall-time
Each module page has its own Testing section detailing per-function coverage. All code lives at test/ on GitHub — test/hardhat/ for the correctness layer, test/foundry/ for the properties.
DeFiMath is published on npm as defimath-lib. All functions are internal pure, so the library compiles directly into your contract bytecode — no linker step and no runtime contract to deploy.
npm install defimath-lib
forge install defimath-lib=MerkleBlue/defimath
Then add to remappings.txt:
defimath-lib/=lib/defimath-lib/The defimath-lib= install alias makes Foundry place the repo at lib/defimath-lib/ so the same defimath-lib/contracts/... import path works under both toolchains. The remapping then overrides Foundry's auto-detect, which would otherwise treat contracts/ as the src directory and produce defimath-lib/=lib/defimath-lib/contracts/ — colliding with the leading contracts/ in the import path.
Compiler requirements
^0.8.31osaka (Fusaka)The library uses the clz Yul builtin (Solidity 0.8.31+) which emits the CLZ opcode introduced in Osaka — both the compiler version and EVM target are hard requirements.
Pricing a European call option — every other module imports the same way.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.31;
import "defimath-lib/contracts/derivatives/Options.sol";
contract OptionsPricer {
function priceCall(
uint128 spot, uint128 strike, uint32 timeToExp,
uint64 vol, uint64 rate
) external pure returns (uint256) {
return DeFiMathOptions.callOptionPrice(spot, strike, timeToExp, vol, rate);
}
}Or composing math primitives directly — the 68-95-99.7 rule, on-chain:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.31;
import "defimath-lib/contracts/math/Math.sol";
contract Confidence {
// P(-k < Z < k) = 2·Φ(k) - 1 — probability that a normally-distributed
// value falls within k standard deviations of the mean (e.g., k=1 → ~68%,
// k=2 → ~95%, k=3 → ~99.7%). The 68-95-99.7 rule on-chain.
function withinKStdevs(int256 k) external pure returns (uint256) {
return 2 * DeFiMath.stdNormCDF(k) - 1e18;
}
}