SafeMath.sol
1 pragma solidity ^0.5.2; 2 3 4 library SafeMath { 5 /** 6 @dev returns the sum of _x and _y, reverts if the calculation overflows 7 @param _x value 1 8 @param _y value 2 9 @return sum 10 */ 11 function add(uint256 _x, uint256 _y) internal pure returns (uint256) { 12 uint256 z = _x + _y; 13 require(z >= _x, "SafeMath failed"); 14 return z; 15 } 16 17 /** 18 @dev returns the difference of _x minus _y, reverts if the calculation underflows 19 @param _x minuend 20 @param _y subtrahend 21 @return difference 22 */ 23 function sub(uint256 _x, uint256 _y) internal pure returns (uint256) { 24 require(_x >= _y, "SafeMath failed"); 25 return _x - _y; 26 } 27 28 /** 29 @dev returns the product of multiplying _x by _y, reverts if the calculation overflows 30 @param _x factor 1 31 @param _y factor 2 32 @return product 33 */ 34 function mul(uint256 _x, uint256 _y) internal pure returns (uint256) { 35 // gas optimization 36 if (_x == 0) 37 return 0; 38 39 uint256 z = _x * _y; 40 require(z / _x == _y, "SafeMath failed"); 41 return z; 42 } 43 44 /** 45 @dev Integer division of two numbers truncating the quotient, reverts on division by zero. 46 @param _x dividend 47 @param _y divisor 48 @return quotient 49 */ 50 function div(uint256 _x, uint256 _y) internal pure returns (uint256) { 51 require(_y > 0, "SafeMath failed"); 52 uint256 c = _x / _y; 53 54 return c; 55 } 56 }