v3.2.0
Adds 7 utility math primitives to DeFiMath — gas-tight, branchless where possible, validated against BigInt references over 500+ random inputs per function.
- mulDiv(a, b, d) — 512-bit-precision a · b / d (Remco Bloemen algorithm). ~238 gas. Reverts on d == 0 or quotient overflow.
- mul(a, b) — fixed-point multiply a · b / 1e18 with the denominator baked in. ~16% cheaper than mulDiv(_, _, 1e18).
- abs(int256) — branchless absolute value. 17 gas. Handles type(int256).min cleanly.
- min(a, b) / max(a, b) — 3-opcode branchless minimum and maximum.
- clamp(x, lo, hi) — max(x, lo) composed with min(_, hi).
- avg(a, b) — overflow-safe (a + b) / 2 via (a & b) + ((a ^ b) >> 1). No overflow at the uint256 ceiling.
Install:
npm install defimath-lib@3.2.0