Remove LibSafeMath and refactor 6/96 bit versions

This commit is contained in:
Amir Bandeali
2019-08-25 18:50:36 -07:00
parent c926a586d2
commit d1bed5729d
5 changed files with 65 additions and 140 deletions

View File

@@ -94,7 +94,7 @@ contract MixinStorage is
// shadow balances by
mapping (address => mapping (bytes32 => uint256)) internal shadowRewardsInPoolByOwner;
// registrered 0x exchanges
// registered 0x Exchange contracts
mapping (address => bool) internal validExchanges;
// ZRX vault

View File

@@ -0,0 +1,53 @@
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.5.9;
library LibSafeDowncast {
/// @dev Safely downcasts to a uint96
/// Note that this reverts if the input value is too large.
function downcastToUint96(uint256 a)
internal
pure
returns (uint96 b)
{
b = uint96(a);
require(
uint256(b) == a,
"VALUE_TOO_LARGE_TO_DOWNCAST_TO_UINT96"
);
return b;
}
/// @dev Safely downcasts to a uint64
/// Note that this reverts if the input value is too large.
function downcastToUint64(uint256 a)
internal
pure
returns (uint64 b)
{
b = uint64(a);
require(
uint256(b) == a,
"VALUE_TOO_LARGE_TO_DOWNCAST_TO_UINT96"
);
return b;
}
}

View File

@@ -1,128 +0,0 @@
/*
Copyright 2018 ZeroEx Intl.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
pragma solidity ^0.5.9;
import "@0x/contracts-utils/contracts/src/LibRichErrors.sol";
import "@0x/contracts-utils/contracts/src/LibSafeMathRichErrors.sol";
library LibSafeMath {
uint256 constant internal MAX_UINT_96 = 79228162514264337593543950335; // 2**96-1
uint256 constant internal MAX_UINT_64 = 18446744073709551615; // 2**64-1
/// @dev Returns the addition of two unsigned integers, reverting on overflow.
/// Note that this reverts on overflow.
function _add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
if (c < a) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.ADDITION_OVERFLOW,
a,
b
));
}
return c;
}
/// @dev Returns the subtraction of two unsigned integers.
/// Note that this reverts on underflow.
function _sub(uint256 a, uint256 b) internal pure returns (uint256) {
if (b > a) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.SUBTRACTION_UNDERFLOW,
a,
b
));
}
uint256 c = a - b;
return c;
}
/// @dev Returns the multiplication of two unsigned integers, reverting on overflow.
/// Note that this reverts on overflow.
function _mul(uint256 a, uint256 b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
uint256 c = a * b;
if (c / a != b) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.MULTIPLICATION_OVERFLOW,
a,
b
));
}
return c;
}
/// @dev Returns the integer division of two unsigned integers.
/// Note that this reverts on division by zero. The result is rounded towards zero.
function _div(uint256 a, uint256 b) internal pure returns (uint256) {
if (b == 0) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.DIVISION_BY_ZERO,
a,
b
));
}
uint256 c = a / b;
return c;
}
/// @dev Safely downcasts to a uint96
/// Note that this reverts if the input value is too large.
function _downcastToUint96(uint256 a)
internal
pure
returns (uint96)
{
if (a > MAX_UINT_96) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256DowncastError(
LibSafeMathRichErrors.DowncastErrorCodes.VALUE_TOO_LARGE_TO_DOWNCAST_TO_UINT96,
a
));
}
return uint96(a);
}
/// @dev Safely downcasts to a uint64
/// Note that this reverts if the input value is too large.
function _downcastToUint64(uint256 a)
internal
pure
returns (uint64)
{
if (a > MAX_UINT_64) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint256DowncastError(
LibSafeMathRichErrors.DowncastErrorCodes.VALUE_TOO_LARGE_TO_DOWNCAST_TO_UINT64,
a
));
}
return uint64(a);
}
}

View File

@@ -26,7 +26,7 @@ library LibSafeMath64 {
/// @dev Returns the addition of two unsigned integers, reverting on overflow.
/// Note that this reverts on overflow.
function _add(uint64 a, uint64 b) internal pure returns (uint64) {
function safeAdd(uint64 a, uint64 b) internal pure returns (uint64) {
uint64 c = a + b;
if (c < a) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint64BinOpError(
@@ -41,7 +41,7 @@ library LibSafeMath64 {
/// @dev Returns the subtraction of two unsigned integers.
/// Note that this reverts on underflow.
function _sub(uint64 a, uint64 b) internal pure returns (uint64) {
function safeSub(uint64 a, uint64 b) internal pure returns (uint64) {
if (b > a) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint64BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.SUBTRACTION_UNDERFLOW,
@@ -56,7 +56,7 @@ library LibSafeMath64 {
/// @dev Returns the multiplication of two unsigned integers, reverting on overflow.
/// Note that this reverts on overflow.
function _mul(uint64 a, uint64 b) internal pure returns (uint64) {
function safeMul(uint64 a, uint64 b) internal pure returns (uint64) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
@@ -78,7 +78,7 @@ library LibSafeMath64 {
/// @dev Returns the integer division of two unsigned integers.
/// Note that this reverts on division by zero. The result is rounded towards zero.
function _div(uint64 a, uint64 b) internal pure returns (uint64) {
function safeDiv(uint64 a, uint64 b) internal pure returns (uint64) {
if (b == 0) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint64BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.DIVISION_BY_ZERO,

View File

@@ -26,7 +26,7 @@ library LibSafeMath96 {
/// @dev Returns the addition of two unsigned integers, reverting on overflow.
/// Note that this reverts on overflow.
function _add(uint96 a, uint96 b) internal pure returns (uint96) {
function safeAdd(uint96 a, uint96 b) internal pure returns (uint96) {
uint96 c = a + b;
if (c < a) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint96BinOpError(
@@ -41,7 +41,7 @@ library LibSafeMath96 {
/// @dev Returns the subtraction of two unsigned integers.
/// Note that this reverts on underflow.
function _sub(uint96 a, uint96 b) internal pure returns (uint96) {
function safeSub(uint96 a, uint96 b) internal pure returns (uint96) {
if (b > a) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint96BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.SUBTRACTION_UNDERFLOW,
@@ -56,7 +56,7 @@ library LibSafeMath96 {
/// @dev Returns the multiplication of two unsigned integers, reverting on overflow.
/// Note that this reverts on overflow.
function _mul(uint96 a, uint96 b) internal pure returns (uint96) {
function safeMul(uint96 a, uint96 b) internal pure returns (uint96) {
// Gas optimization: this is cheaper than requiring 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
@@ -78,7 +78,7 @@ library LibSafeMath96 {
/// @dev Returns the integer division of two unsigned integers.
/// Note that this reverts on division by zero. The result is rounded towards zero.
function _div(uint96 a, uint96 b) internal pure returns (uint96) {
function safeDiv(uint96 a, uint96 b) internal pure returns (uint96) {
if (b == 0) {
LibRichErrors.rrevert(LibSafeMathRichErrors.Uint96BinOpError(
LibSafeMathRichErrors.BinOpErrorCodes.DIVISION_BY_ZERO,
@@ -100,11 +100,11 @@ library LibSafeMath96 {
pure
returns (uint96)
{
uint96 scaledNumerator = _mul(value, slice);
uint96 scaledNumerator = safeMul(value, slice);
uint96 ceilScalar = uint96(99);
uint96 denominator = uint96(100);
return _div(
_add(scaledNumerator, ceilScalar),
return safeDiv(
safeAdd(scaledNumerator, ceilScalar),
denominator
);
}