Version 3 - mainnet - 0xc7...40ff78
Readme
Versions
3
Dependencies
0
Dependents
0
Analytics
On the way
openzeppelin_math[!WARNING] This package is open-source and not actively maintained.
The
UpgradeCapfor packages published to the Move Registry under@openzeppelin-move/*has been made immutable, so these packages can no longer be upgraded by OpenZeppelin or anyone else.
Overflow-safe arithmetic helpers for unsigned integers with configurable rounding.
[dependencies]
openzeppelin_math = { r.mvr = "@openzeppelin-move/integer-math" }
Operations for u8, u16, u32, u64, u128, and u256, including:
mul_div: Multiply then divide with roundingmul_shr: Multiply then shift right with roundingaverage: Arithmetic mean with roundingchecked_shl / checked_shr: Safe shifts that return Optionclz: Count leading zero bitsmsb: Position of the most significant bitlog2 / log10 / log256: Integer logs with roundingsqrt: Integer square root with roundinginv_mod: Modular multiplicative inversemul_mod: Modular multiplicationis_power_of_ten: Power-of-ten checkGeneric over u8..u256, with comparator-based sorting available for other types:
vector::quick_sort! / vector::quick_sort_by!: In-place iterative quicksort with three-way partitioningvector::median!: Median of a borrowed unsigned integer vector with configurable rounding for even-length input; uses quickselect instead of sorting the full vector and aborts on empty inputvector::median_u8 … vector::median_u256: Precompiled median wrappers for each unsigned integer width (u8, u16, u32, u64, u128, u256); same quickselect algorithm and abort behavior as median!, with the selection bytecode compiled once in the library instead of inlined at the call siteuse openzeppelin_math::rounding;
use openzeppelin_math::u128;
let result = u128::mul_div(100, 200, 3, rounding::up());
// result = Some(6667) (rounded up from 6666.66...)
use openzeppelin_math::rounding;
use openzeppelin_math::u64;
let mean = u64::average(5, 6, rounding::down());
// mean = 5
use openzeppelin_math::rounding;
use openzeppelin_math::vector;
let med = vector::median!(&vector[5u64, 1, 9, 3, 7], rounding::down());
// med = 5
[!Warning] These are unaudited illustrations of how the primitives can be integrated, not production-ready code.
Complete, compilable integration examples live in examples/:
integer_math - a constant-product AMM pricing toolkit: mul_div for swap quotes (output rounded down, fee rounded up), sqrt for LP shares, average, mul_shr for a Q32.32 factor, and log10 for magnitude, plus the same mul_div one width up on u256 returning none on overflow.rounding - a solvency-preserving fee/payout splitter: round one side with the chosen RoundingMode, derive the other as the remainder, so the parts always sum to the whole. Shows why Down/Up/Nearest is an economic decision.vector - a bounded median price oracle: median_u64 for a manipulation-resistant aggregate and the quick_sort! macro for a sorted view.decimal_scaling - a ledger that reconciles one token's 6-decimal canonical and 9-decimal bridged forms onto a common 18-decimal u256 basis with safe_upcast_balance / safe_downcast_balance, where downcasting truncates dust to preserve solvency.llms.txt: discovery entry point for AI integratorsYou can install this package in your Move project by calling
mvr add @openzeppelin-move/integer-mathOverflow-safe arithmetic helpers for unsigned integers with configurable rounding.