Separate unsigned and signed safemath libraries (#1588)

* separate unsigned and signed safemath libraries

* update changelog entry for SignedSafeMath
This commit is contained in:
Francisco Giordano
2019-01-04 14:57:04 -03:00
committed by GitHub
parent 40f08a8c0b
commit a5b14f262e
7 changed files with 326 additions and 308 deletions

View File

@ -0,0 +1,60 @@
pragma solidity ^0.5.0;
/**
* @title SignedSafeMath
* @dev Signed math operations with safety checks that revert on error
*/
library SignedSafeMath {
int256 constant private INT256_MIN = -2**255;
/**
* @dev Multiplies two signed integers, reverts on overflow.
*/
function mul(int256 a, int256 b) internal pure returns (int256) {
// 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;
}
require(!(a == -1 && b == INT256_MIN)); // This is the only case of overflow not detected by the check below
int256 c = a * b;
require(c / a == b);
return c;
}
/**
* @dev Integer division of two signed integers truncating the quotient, reverts on division by zero.
*/
function div(int256 a, int256 b) internal pure returns (int256) {
require(b != 0); // Solidity only automatically asserts when dividing by 0
require(!(b == -1 && a == INT256_MIN)); // This is the only case of overflow
int256 c = a / b;
return c;
}
/**
* @dev Subtracts two signed integers, reverts on overflow.
*/
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
require((b >= 0 && c <= a) || (b < 0 && c > a));
return c;
}
/**
* @dev Adds two signed integers, reverts on overflow.
*/
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
require((b >= 0 && c >= a) || (b < 0 && c < a));
return c;
}
}

View File

@ -2,11 +2,9 @@ pragma solidity ^0.5.0;
/**
* @title SafeMath
* @dev Math operations with safety checks that revert on error
* @dev Unsigned math operations with safety checks that revert on error
*/
library SafeMath {
int256 constant private INT256_MIN = -2**255;
/**
* @dev Multiplies two unsigned integers, reverts on overflow.
*/
@ -24,25 +22,6 @@ library SafeMath {
return c;
}
/**
* @dev Multiplies two signed integers, reverts on overflow.
*/
function mul(int256 a, int256 b) internal pure returns (int256) {
// 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;
}
require(!(a == -1 && b == INT256_MIN)); // This is the only case of overflow not detected by the check below
int256 c = a * b;
require(c / a == b);
return c;
}
/**
* @dev Integer division of two unsigned integers truncating the quotient, reverts on division by zero.
*/
@ -55,18 +34,6 @@ library SafeMath {
return c;
}
/**
* @dev Integer division of two signed integers truncating the quotient, reverts on division by zero.
*/
function div(int256 a, int256 b) internal pure returns (int256) {
require(b != 0); // Solidity only automatically asserts when dividing by 0
require(!(b == -1 && a == INT256_MIN)); // This is the only case of overflow
int256 c = a / b;
return c;
}
/**
* @dev Subtracts two unsigned integers, reverts on overflow (i.e. if subtrahend is greater than minuend).
*/
@ -77,16 +44,6 @@ library SafeMath {
return c;
}
/**
* @dev Subtracts two signed integers, reverts on overflow.
*/
function sub(int256 a, int256 b) internal pure returns (int256) {
int256 c = a - b;
require((b >= 0 && c <= a) || (b < 0 && c > a));
return c;
}
/**
* @dev Adds two unsigned integers, reverts on overflow.
*/
@ -97,16 +54,6 @@ library SafeMath {
return c;
}
/**
* @dev Adds two signed integers, reverts on overflow.
*/
function add(int256 a, int256 b) internal pure returns (int256) {
int256 c = a + b;
require((b >= 0 && c >= a) || (b < 0 && c < a));
return c;
}
/**
* @dev Divides two unsigned integers and returns the remainder (unsigned integer modulo),
* reverts when dividing by zero.

View File

@ -1,43 +1,25 @@
pragma solidity ^0.5.0;
import "../math/SafeMath.sol";
contract SafeMathMock {
function mulUints(uint256 a, uint256 b) public pure returns (uint256) {
function mul(uint256 a, uint256 b) public pure returns (uint256) {
return SafeMath.mul(a, b);
}
function mulInts(int256 a, int256 b) public pure returns (int256) {
return SafeMath.mul(a, b);
}
function divUints(uint256 a, uint256 b) public pure returns (uint256) {
function div(uint256 a, uint256 b) public pure returns (uint256) {
return SafeMath.div(a, b);
}
function divInts(int256 a, int256 b) public pure returns (int256) {
return SafeMath.div(a, b);
}
function subUints(uint256 a, uint256 b) public pure returns (uint256) {
function sub(uint256 a, uint256 b) public pure returns (uint256) {
return SafeMath.sub(a, b);
}
function subInts(int256 a, int256 b) public pure returns (int256) {
return SafeMath.sub(a, b);
}
function addUints(uint256 a, uint256 b) public pure returns (uint256) {
function add(uint256 a, uint256 b) public pure returns (uint256) {
return SafeMath.add(a, b);
}
function addInts(int256 a, int256 b) public pure returns (int256) {
return SafeMath.add(a, b);
}
function modUints(uint256 a, uint256 b) public pure returns (uint256) {
function mod(uint256 a, uint256 b) public pure returns (uint256) {
return SafeMath.mod(a, b);
}
}

View File

@ -0,0 +1,21 @@
pragma solidity ^0.5.0;
import "../drafts/SignedSafeMath.sol";
contract SignedSafeMathMock {
function mul(int256 a, int256 b) public pure returns (int256) {
return SignedSafeMath.mul(a, b);
}
function div(int256 a, int256 b) public pure returns (int256) {
return SignedSafeMath.div(a, b);
}
function sub(int256 a, int256 b) public pure returns (int256) {
return SignedSafeMath.sub(a, b);
}
function add(int256 a, int256 b) public pure returns (int256) {
return SignedSafeMath.add(a, b);
}
}