From 064af06385742d510e872d4e75d955ae1377e8d8 Mon Sep 17 00:00:00 2001 From: emilio Date: Wed, 28 Oct 2020 11:47:01 +0100 Subject: [PATCH 1/2] updated percentageMath --- contracts/libraries/math/WadRayMath.sol | 29 +++++++++---------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/contracts/libraries/math/WadRayMath.sol b/contracts/libraries/math/WadRayMath.sol index a1a8d8f4..48985580 100644 --- a/contracts/libraries/math/WadRayMath.sol +++ b/contracts/libraries/math/WadRayMath.sol @@ -54,15 +54,13 @@ library WadRayMath { * @return the result of a*b, in wad **/ function wadMul(uint256 a, uint256 b) internal pure returns (uint256) { - if (a == 0) { + if (a == 0 || b == 0) { return 0; } - uint256 result = a * b + halfWAD; + require(a <= (type(uint256).max-halfWAD)/b, Errors.MULTIPLICATION_OVERFLOW); - require(result >= halfWAD && (result - halfWAD) / a == b, Errors.MULTIPLICATION_OVERFLOW); - - return result / WAD; + return (a * b + halfWAD) / WAD; } /** @@ -73,14 +71,11 @@ library WadRayMath { **/ function wadDiv(uint256 a, uint256 b) internal pure returns (uint256) { require(b != 0, Errors.DIVISION_BY_ZERO); - uint256 halfB = b / 2; - uint256 result = a * WAD + halfB; + require(a <= (type(uint256).max-halfB)/WAD, Errors.MULTIPLICATION_OVERFLOW); - require(result >= halfB && (result - halfB) / WAD == a, Errors.MULTIPLICATION_OVERFLOW); - - return result / b; + return (a * WAD + halfB) / b; } /** @@ -90,15 +85,14 @@ library WadRayMath { * @return the result of a*b, in ray **/ function rayMul(uint256 a, uint256 b) internal pure returns (uint256) { - if (a == 0) { + if (a == 0 || b == 0) { return 0; } - uint256 result = a * b + halfRAY; + require(a <= (type(uint256).max-halfRAY)/b, Errors.MULTIPLICATION_OVERFLOW); - require(result >= halfRAY && (result - halfRAY) / a == b, Errors.MULTIPLICATION_OVERFLOW); + return (a * b + halfRAY) / RAY; - return result / RAY; } /** @@ -109,14 +103,11 @@ library WadRayMath { **/ function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) { require(b != 0, Errors.DIVISION_BY_ZERO); - uint256 halfB = b / 2; - uint256 result = a * RAY + halfB; + require(a <= (type(uint256).max-halfB)/RAY, Errors.MULTIPLICATION_OVERFLOW); - require(result >= halfB && (result - halfB) / RAY == a, Errors.MULTIPLICATION_OVERFLOW); - - return result / b; + return (a * RAY + halfB) / b; } /** From e91e3595d6c5e82d294e15f3ca2225730ad581f7 Mon Sep 17 00:00:00 2001 From: emilio Date: Wed, 28 Oct 2020 12:39:11 +0100 Subject: [PATCH 2/2] Updated PercentageMath --- contracts/libraries/math/PercentageMath.sol | 22 +++++---------------- 1 file changed, 5 insertions(+), 17 deletions(-) diff --git a/contracts/libraries/math/PercentageMath.sol b/contracts/libraries/math/PercentageMath.sol index b853f1db..52a38cd4 100644 --- a/contracts/libraries/math/PercentageMath.sol +++ b/contracts/libraries/math/PercentageMath.sol @@ -22,19 +22,13 @@ library PercentageMath { * @return the percentage of value **/ function percentMul(uint256 value, uint256 percentage) internal pure returns (uint256) { - if (value == 0) { + if (value == 0 || percentage == 0) { return 0; } - uint256 result = value * percentage; + require(value <= (type(uint256).max - HALF_PERCENT)/percentage, Errors.MULTIPLICATION_OVERFLOW); - require(result / value == percentage, Errors.MULTIPLICATION_OVERFLOW); - - result += HALF_PERCENT; - - require(result >= HALF_PERCENT, Errors.ADDITION_OVERFLOW); - - return result / PERCENTAGE_FACTOR; + return (value * percentage + HALF_PERCENT) / PERCENTAGE_FACTOR; } /** @@ -47,14 +41,8 @@ library PercentageMath { require(percentage != 0, Errors.DIVISION_BY_ZERO); uint256 halfPercentage = percentage / 2; - uint256 result = value * PERCENTAGE_FACTOR; + require(value <= (type(uint256).max - halfPercentage)/PERCENTAGE_FACTOR, Errors.MULTIPLICATION_OVERFLOW); - require(result / PERCENTAGE_FACTOR == value, Errors.MULTIPLICATION_OVERFLOW); - - result += halfPercentage; - - require(result >= halfPercentage, Errors.ADDITION_OVERFLOW); - - return result / percentage; + return (value * PERCENTAGE_FACTOR + halfPercentage) / percentage; } }