Skip to content

v3.6.0

Latest

Choose a tag to compare

@nebojsakonsta nebojsakonsta released this 19 Jul 08:39

v3.6.0 — sqrt & cbrt handle the full uint256 domain

Two headline changes, plus cascading gas savings.

Highlights

  • sqrt and cbrt now accept the full uint256 domain. No more upper-bound revert — any input in [0, uint256.max] returns a valid FP18 result. sqrt also drops 245 → 197 gas (−20%) and cbrt 368 → 340 (−8%) via a new CLZ-derived power-of-two seed + Newton refinement.
  • pow overflow bugfix. The internal a · ln(x) inside unchecked could silently overflow int256 for adversarial exponents (|a| ≳ 3.25e56) and return plausible-looking garbage. Now bounded by MAX_POW_EXPONENT = 1e54 with a typed PowExponentOutOfBoundsError revert — ~18 orders of magnitude above any realistic exponent.

📖 Deep dive on the new sqrt: How I wrote a fixed-point Solidity sqrt that runs in 197 gas

⚠️ Breaking changes

  • Removed errors SqrtUpperBoundError(), CbrtUpperBoundError().
  • Removed constants SQRT_UPPER_BOUND, CBRT_UPPER_BOUND.
  • Added PowExponentOutOfBoundsError() and MAX_POW_EXPONENT.

If you caught SqrtUpperBoundError / CbrtUpperBoundError, remove those handlers — the code paths no longer exist. Callers passing finite exponents to pow see no behavior change.

Gas changes

sqrtTime also picked up the new seed, so its savings cascade into every Black-Scholes function with an annualized time term (Greeks, IV, pricers). expm1/log1p shed a few gas each, flowing into Rates; the sqrt win compounds across Statistics. pow is the only function that went up — the new overflow check costs ~40 gas.

Function v3.5.0 v3.6.0 Change
sqrt 245 197 −48 (−20%)
cbrt 368 340 −28 (−8%)
sqrtTime 184 163 −21 (−11%)
callOptionPrice 2,729 2,708 −21 (−0.8%)
binaryCallPrice 2,018 1,997 −21 (−1%)
pow 763 803 +40 (safety)
expm1 415 407 −8
log1p 487 476 −11
continuousToDiscrete 507 491 −16
discreteToContinuous 589 574 −15
geometricMean 330 284 −46
stdDev (N=100) 49,811 49,765 −46
historicalVolatility (N=100) 86,595 86,500 −95
sharpeRatio (N=100) 86,733 86,638 −95

Install

npm install defimath-lib@3.6.0

Foundry:

forge install defimath-lib=MerkleBlue/defimath

Links

Full Changelog: v3.5.0...v3.6.0