Update Math.sol

pull/5035/head
Hadrien Croubois 4 months ago committed by GitHub
parent cc807105a4
commit e635d9b07c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 16
      contracts/utils/math/Math.sol

@ -23,9 +23,9 @@ library Math {
* The result is stored in two 256 variables such that product = high * 2² + low. * The result is stored in two 256 variables such that product = high * 2² + low.
*/ */
function addFull(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) { function addFull(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
unchecked { assembly ("memory-safe") {
low = a + b; low: = add(a, b)
high = SafeCast.toUint(low < a); high: = lt(low, a)
} }
} }
@ -37,12 +37,10 @@ library Math {
function mulFull(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) { function mulFull(uint256 a, uint256 b) internal pure returns (uint256 high, uint256 low) {
// Compute the product mod 2² and mod 2² - 1, then use use the Chinese Remainder Theorem to reconstruct // Compute the product mod 2² and mod 2² - 1, then use use the Chinese Remainder Theorem to reconstruct
// the 512 bit result. // the 512 bit result.
unchecked { assembly ("memory-safe") {
low = a * b; let mm := mulmod(a, b, not(0))
assembly { low := mul(a, b)
let mm := mulmod(a, b, not(0)) high := sub(sub(mm, low), lt(mm, low))
high := sub(sub(mm, low), lt(mm, low))
}
} }
} }

Loading…
Cancel
Save