as-bignum icon indicating copy to clipboard operation
as-bignum copied to clipboard

Add div and mod to u256 with their dependencies

Open gregLibert opened this issue 1 year ago • 2 comments

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 and u128 to prevent overflow issues.
  • Division for u128:
    • Added div64
    • Added quoRem (returns quotient and remainder) for a u64 divisor.
  • Division for u256:
    • Added div128
    • Added quoRem, Division, and Modulus functions.

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.

gregLibert avatar Aug 24 '23 09:08 gregLibert

Hello I'm curious what's missing for this PR to be merged? Would some one tell me please?

bilboquet avatar Sep 28 '23 11:09 bilboquet

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

MaxGraey avatar Sep 28 '23 11:09 MaxGraey

A more closely aligned implementation with the existing code has been proposed in #88.

gregLibert avatar May 17 '24 14:05 gregLibert