Skip to content

Commit

Permalink
fix: Make WadRayMath and PercentageMath unchecked
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jul 28, 2021
1 parent 932f591 commit 1a81ecf
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 41 deletions.
34 changes: 19 additions & 15 deletions contracts/protocol/libraries/math/PercentageMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,18 @@ library PercentageMath {
* @return The percentage of value
**/
function percentMul(uint256 value, uint256 percentage) internal pure returns (uint256) {
if (value == 0 || percentage == 0) {
return 0;
}
unchecked {
if (value == 0 || percentage == 0) {
return 0;
}

require(
value <= (type(uint256).max - HALF_PERCENT) / percentage,
Errors.MATH_MULTIPLICATION_OVERFLOW
);
require(
value <= (type(uint256).max - HALF_PERCENT) / percentage,
Errors.MATH_MULTIPLICATION_OVERFLOW
);

return (value * percentage + HALF_PERCENT) / PERCENTAGE_FACTOR;
return (value * percentage + HALF_PERCENT) / PERCENTAGE_FACTOR;
}
}

/**
Expand All @@ -41,14 +43,16 @@ library PercentageMath {
* @return The value divided the percentage
**/
function percentDiv(uint256 value, uint256 percentage) internal pure returns (uint256) {
require(percentage != 0, Errors.MATH_DIVISION_BY_ZERO);
uint256 halfPercentage = percentage / 2;
unchecked {
require(percentage != 0, Errors.MATH_DIVISION_BY_ZERO);
uint256 halfPercentage = percentage / 2;

require(
value <= (type(uint256).max - halfPercentage) / PERCENTAGE_FACTOR,
Errors.MATH_MULTIPLICATION_OVERFLOW
);
require(
value <= (type(uint256).max - halfPercentage) / PERCENTAGE_FACTOR,
Errors.MATH_MULTIPLICATION_OVERFLOW
);

return (value * PERCENTAGE_FACTOR + halfPercentage) / percentage;
return (value * PERCENTAGE_FACTOR + halfPercentage) / percentage;
}
}
}
62 changes: 37 additions & 25 deletions contracts/protocol/libraries/math/WadRayMath.sol
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,15 @@ library WadRayMath {
* @return The result of a*b, in wad
**/
function wadMul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0 || b == 0) {
return 0;
}
unchecked {
if (a == 0 || b == 0) {
return 0;
}

require(a <= (type(uint256).max - halfWAD) / b, Errors.MATH_MULTIPLICATION_OVERFLOW);
require(a <= (type(uint256).max - halfWAD) / b, Errors.MATH_MULTIPLICATION_OVERFLOW);

return (a * b + halfWAD) / WAD;
return (a * b + halfWAD) / WAD;
}
}

/**
Expand All @@ -70,12 +72,14 @@ library WadRayMath {
* @return The result of a/b, in wad
**/
function wadDiv(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, Errors.MATH_DIVISION_BY_ZERO);
uint256 halfB = b / 2;
unchecked {
require(b != 0, Errors.MATH_DIVISION_BY_ZERO);
uint256 halfB = b / 2;

require(a <= (type(uint256).max - halfB) / WAD, Errors.MATH_MULTIPLICATION_OVERFLOW);
require(a <= (type(uint256).max - halfB) / WAD, Errors.MATH_MULTIPLICATION_OVERFLOW);

return (a * WAD + halfB) / b;
return (a * WAD + halfB) / b;
}
}

/**
Expand All @@ -85,13 +89,15 @@ library WadRayMath {
* @return The result of a*b, in ray
**/
function rayMul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0 || b == 0) {
return 0;
}
unchecked {
if (a == 0 || b == 0) {
return 0;
}

require(a <= (type(uint256).max - halfRAY) / b, Errors.MATH_MULTIPLICATION_OVERFLOW);
require(a <= (type(uint256).max - halfRAY) / b, Errors.MATH_MULTIPLICATION_OVERFLOW);

return (a * b + halfRAY) / RAY;
return (a * b + halfRAY) / RAY;
}
}

/**
Expand All @@ -101,12 +107,14 @@ library WadRayMath {
* @return The result of a/b, in ray
**/
function rayDiv(uint256 a, uint256 b) internal pure returns (uint256) {
require(b != 0, Errors.MATH_DIVISION_BY_ZERO);
uint256 halfB = b / 2;
unchecked {
require(b != 0, Errors.MATH_DIVISION_BY_ZERO);
uint256 halfB = b / 2;

require(a <= (type(uint256).max - halfB) / RAY, Errors.MATH_MULTIPLICATION_OVERFLOW);
require(a <= (type(uint256).max - halfB) / RAY, Errors.MATH_MULTIPLICATION_OVERFLOW);

return (a * RAY + halfB) / b;
return (a * RAY + halfB) / b;
}
}

/**
Expand All @@ -115,11 +123,13 @@ library WadRayMath {
* @return a casted to wad, rounded half up to the nearest wad
**/
function rayToWad(uint256 a) internal pure returns (uint256) {
uint256 halfRatio = WAD_RAY_RATIO / 2;
uint256 result = halfRatio + a;
require(result >= halfRatio, Errors.MATH_ADDITION_OVERFLOW);
unchecked {
uint256 halfRatio = WAD_RAY_RATIO / 2;
uint256 result = halfRatio + a;
require(result >= halfRatio, Errors.MATH_ADDITION_OVERFLOW);

return result / WAD_RAY_RATIO;
return result / WAD_RAY_RATIO;
}
}

/**
Expand All @@ -128,8 +138,10 @@ library WadRayMath {
* @return a converted in ray
**/
function wadToRay(uint256 a) internal pure returns (uint256) {
uint256 result = a * WAD_RAY_RATIO;
require(result / WAD_RAY_RATIO == a, Errors.MATH_MULTIPLICATION_OVERFLOW);
return result;
unchecked {
uint256 result = a * WAD_RAY_RATIO;
require(result / WAD_RAY_RATIO == a, Errors.MATH_MULTIPLICATION_OVERFLOW);
return result;
}
}
}
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ let forkMode;

const buidlerConfig: HardhatUserConfig = {
gasReporter: {
enabled: false,
enabled: true,
},
contractSizer: {
alphaSort: true,
Expand Down

0 comments on commit 1a81ecf

Please sign in to comment.