OpenZeppelin / OpenZeppelin/compact-contracts
U256 Math Library
Open
@0xisk is already working on this.
Since May 5, 2026.
1-medium
effort: 4-L
feat:math
- Dominant language
- TypeScript
- Stars
- 55
- Forks
- 29
- Avg merge
- 5d 7h
- Merged PRs (30d)
- 25
Description
🧐 Motivation
We need a complete, safe, and reusable math library for U256 to support arithmetic, division, square root, and utility operations.
📝 Details
Implement the following operations for a U256 math library:
Arithmetic
add()– add two 256-bit values with overflow checkssub()– subtract two 256-bit values with underflow checksmul()– multiply two 256-bit values with overflow checks
Division
div()– compute quotientrem()– compute remainderdivRem()– compute quotient and remainder together
Square Root
sqrt()– compute⌊√x⌋, with optimized handling for small or limb-aligned values
Utility
-
MODULUS()– return high-limb modulus (2^128) -
MODULUS_U256()– returnU256(2^128) -
ZERO_U256()– return zero -
min(),max() -
isMultiple() -
isZero() -
Limb-specific checks:
isLowestLimbOnly()isSecondLowestLimbOnly()isThirdLowestLimbOnly()isHighestLimbOnly()
-
isExceedingFieldSize()– checks> 2^254 − 1
Future Work
pow()powU256()
Notes
- All operations should be overflow-safe and explicit about error cases
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.