SafeMath is now more consistent with itself. (#1168)

This commit is contained in:
Nicolás Venturo
2018-08-08 11:21:23 -03:00
committed by GitHub
parent eca5bf9157
commit e819416d04

View File

@ -10,7 +10,7 @@ library SafeMath {
/** /**
* @dev Multiplies two numbers, throws on overflow. * @dev Multiplies two numbers, throws on overflow.
*/ */
function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c) { function mul(uint256 _a, uint256 _b) internal pure returns (uint256) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the // Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested. // benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
@ -18,8 +18,9 @@ library SafeMath {
return 0; return 0;
} }
c = _a * _b; uint256 c = _a * _b;
assert(c / _a == _b); assert(c / _a == _b);
return c; return c;
} }
@ -28,9 +29,10 @@ library SafeMath {
*/ */
function div(uint256 _a, uint256 _b) internal pure returns (uint256) { function div(uint256 _a, uint256 _b) internal pure returns (uint256) {
// assert(_b > 0); // Solidity automatically throws when dividing by 0 // assert(_b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = _a / _b; uint256 c = _a / _b;
// assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold // assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold
return _a / _b;
return c;
} }
/** /**
@ -38,15 +40,18 @@ library SafeMath {
*/ */
function sub(uint256 _a, uint256 _b) internal pure returns (uint256) { function sub(uint256 _a, uint256 _b) internal pure returns (uint256) {
assert(_b <= _a); assert(_b <= _a);
return _a - _b; uint256 c = _a - _b;
return c;
} }
/** /**
* @dev Adds two numbers, throws on overflow. * @dev Adds two numbers, throws on overflow.
*/ */
function add(uint256 _a, uint256 _b) internal pure returns (uint256 c) { function add(uint256 _a, uint256 _b) internal pure returns (uint256) {
c = _a + _b; uint256 c = _a + _b;
assert(c >= _a); assert(c >= _a);
return c; return c;
} }
} }