DeFiMath v3.3.0 — stdNormCDF rewrite, call/put 5% cheaper
This is a pure optimization release. No new features, no API changes — same precision,
same behaviour, just fewer opcodes in stdNormCDF. Every option pricer
and greek built on it gets the savings automatically.
Where the savings come from
The previous stdNormCDF scaled its argument by 1/√2 and then called an
internal erfPositiveHalf helper that multiplied right back by √2 — two unnecessary
MUL+DIV pairs in every call. Inlining the body into stdNormCDF and dropping the
helper (which had no other callers) eliminated the round-trip and lets each branch
write straight to the result. Net: 74 gas per stdNormCDF call.
That single change propagates everywhere stdNormCDF is used inside the library.
The numbers
Biggest absolute wins are on the option pricers themselves — callOptionPrice and
putOptionPrice each call stdNormCDF twice (one for Φ(d₁), one for Φ(d₂)):
| Function | v3.2.0 gas | v3.3.0 gas | Saved | % |
|---|---|---|---|---|
| callOptionPrice | 2,876 | 2,729 | −147 | −5.1% |
| putOptionPrice | 2,887 | 2,739 | −148 | −5.1% |
| delta | 1,797 | 1,724 | −73 | −4.1% |
| theta | 3,441 | 3,293 | −148 | −4.3% |
| binaryCallPrice | 2,092 | 2,018 | −74 | −3.5% |
| binaryPutPrice | 2,097 | 2,023 | −74 | −3.5% |
| binaryTheta | 3,501 | 3,353 | −148 | −4.2% |
| impliedVolatility (call) | 13,100 | 12,370 | −730 | −5.6% |
| impliedVolatility (put) | 13,184 | 12,446 | −738 | −5.6% |
| stdNormCDF | 734 | 660 | −74 | −10.1% |
gamma, vega, binaryDelta, binaryGamma, binaryVega are unchanged — they're
built on the standard normal PDF, not the CDF, so this optimization doesn't reach
them.
impliedVolatility shows the largest absolute drop because it calls stdNormCDF
inside every Newton-Raphson iteration — roughly 10 iterations to converge, each
saving ~74 gas.
No behavioural change
Same input domain, same return values to the last wei. The 396 tests across Math,
Options, and Binary all pass at the same precision thresholds. The strict
equality assertions in the perf suite caught the change automatically and forced
the threshold updates — that's exactly what they're for.
Get it
npm install [email protected]
- Release notes: github.com/MerkleBlue/defimath/releases/tag/v3.3.0
- Package: npmjs.com/package/defimath-lib