OpenZeppelin / OpenZeppelin/compact-contracts

U256 Math Library

Open
#216 0 comments 0 reactions 1 assignee View on GitHub

@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 checks
  • sub() – subtract two 256-bit values with underflow checks
  • mul() – multiply two 256-bit values with overflow checks

Division

  • div() – compute quotient
  • rem() – compute remainder
  • divRem() – 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() – return U256(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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.