as-bignum
as-bignum copied to clipboard
Add div and mod to u256 with their dependencies
Description
This pull request aims to introduce div
and mod
operations to the u256
data type as part of Massa's ongoing smart contract development.
These functionalities have been specifically requested by one of our partners and are expected to enhance the arithmetic capabilities of our smart contracts.
Changes
-
Safe Bitwise Shift Operations: Implemented safe left and right bitwise shift operations for
u64
andu128
to prevent overflow issues. -
Division for
u128
:- Added
div64
- Added
quoRem
(returns quotient and remainder) for au64
divisor.
- Added
-
Division for
u256
:- Added
div128
- Added
quoRem
, Division, and Modulus functions.
- Added
Challenges and Solutions
The primary challenge was the sheer number of changes required, along with the potential for overflow in bitwise shift operations. To address this, safe implementations were created for both u64
and u128
.
Additional Considerations
The algorithms should be performant, as they were inspired by existing Golang implementations. For reference, see here. However, there may be room for further code factorization. Additionally, some functions may need to be relocated.
Hello I'm curious what's missing for this PR to be merged? Would some one tell me please?
I'm curious what's missing for this PR to be merged?
Inconsistency and breaking changes for some existing code
https://github.com/MaxGraey/as-bignum/pull/86#discussion_r1304156623 https://github.com/MaxGraey/as-bignum/pull/86#discussion_r1304324224
A more closely aligned implementation with the existing code has been proposed in #88.