Merge branch 'fix/98' into 'master'

Resolve "Optimize percentageMath and wadRayMath"

Closes #98

See merge request aave-tech/protocol-v2!100
This commit is contained in:
Ernesto Boado 2020-10-28 13:41:52 +00:00
commit 47d00a0e3a
2 changed files with 15 additions and 36 deletions

View File

@ -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;
}
}

View File

@ -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;
}
/**