This commit is contained in:
Noah Zinsmeister
2019-11-06 17:23:03 -05:00
parent 8cb70d07b2
commit 710adc3254
15 changed files with 399 additions and 315 deletions

View File

@ -1,35 +1,18 @@
pragma solidity 0.5.12;
library Math {
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/2f9ae975c8bdc5c7f7fa26204896f6c717f07164/contracts/math/Math.sol#L17
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
function min(uint256 x, uint256 y) internal pure returns (uint256 z) {
return x <= y ? x : y;
}
// https://github.com/ethereum/dapp-bin/pull/50
// https://github.com/ethereum/dapp-bin/blob/11f05fc9e3f31a00d57982bc2f65ef2654f1b569/library/math.sol#L28
function sqrt(uint256 x) internal pure returns (uint256 y) {
if (x == 0) {
y = 0;
} else if (x <= 3) {
y = 1;
} else {
y = x;
uint256 z = (x + 1) / 2;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
if (x == 0) return 0;
else if (x <= 3) return 1;
uint256 z = (x + 1) / 2;
y = x;
while (z < y) {
y = z;
z = (x / z + z) / 2;
}
}
function downcastTo64(uint256 a) internal pure returns (uint64) {
require(a <= uint64(-1), "Math: downcast overflow (uint256 to uint64)");
return uint64(a);
}
function downcastTo128(uint256 a) internal pure returns (uint128) {
require(a <= uint128(-1), "Math: downcast overflow (uint256 to uint128)");
return uint128(a);
}
}

View File

@ -1,25 +1,24 @@
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/2f9ae975c8bdc5c7f7fa26204896f6c717f07164/contracts/math/SafeMath.sol
pragma solidity 0.5.12;
library SafeMath128 {
function add(uint128 a, uint128 b) internal pure returns (uint128 c) {
c = a + b;
require(c >= a, "SafeMath128: addition overflow");
function add(uint128 x, uint128 y) internal pure returns (uint128 z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}
function sub(uint128 x, uint128 y) internal pure returns (uint128 z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
function mul(uint128 x, uint128 y) internal pure returns (uint128 z) {
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
}
function sub(uint128 a, uint128 b) internal pure returns (uint128) {
require(b <= a, "SafeMath128: subtraction overflow");
return a - b;
function oadd(uint128 w, uint128 x) internal pure returns (uint128 y, uint128 z) {
uint256 sum = uint256(w) + x;
z = uint128(sum / uint128(-1));
y = uint128(sum % uint128(-1));
}
function mul(uint128 a, uint128 b) internal pure returns (uint128 c) {
if (a == 0) return 0;
c = a * b;
require(c / a == b, "SafeMath128: multiplication overflow");
}
function div(uint128 a, uint128 b) internal pure returns (uint128) {
require(b > 0, "SafeMath128: division by zero");
return a / b;
function omul(uint128 w, uint128 x) internal pure returns (uint128 y, uint128 z) {
uint256 product = uint256(w) * x;
z = uint128(product / uint128(-1));
y = uint128(product % uint128(-1));
}
}

View File

@ -1,25 +1,18 @@
// https://github.com/OpenZeppelin/openzeppelin-contracts/blob/2f9ae975c8bdc5c7f7fa26204896f6c717f07164/contracts/math/SafeMath.sol
pragma solidity 0.5.12;
library SafeMath256 {
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
require(c >= a, "SafeMath256: addition overflow");
function add(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x + y) >= x, "ds-math-add-overflow");
}
function sub(uint256 x, uint256 y) internal pure returns (uint256 z) {
require((z = x - y) <= x, "ds-math-sub-underflow");
}
function mul(uint256 x, uint256 y) internal pure returns (uint256 z) {
require(y == 0 || (z = x * y) / y == x, "ds-math-mul-overflow");
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
require(b <= a, "SafeMath256: subtraction overflow");
return a - b;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
if (a == 0) return 0;
c = a * b;
require(c / a == b, "SafeMath256: multiplication overflow");
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
require(b > 0, "SafeMath256: division by zero");
return a / b;
function downcast128(uint256 y) internal pure returns (uint128 z) {
require(y <= uint128(-1), "downcast-128-overflow");
z = uint128(y);
}
}