github.com/klaytn/klaytn@v1.12.1/contracts/reward/contract/SafeMath.sol (about) 1 // Copyright 2019 The klaytn Authors 2 // This file is part of the klaytn library. 3 // 4 // The klaytn library is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU Lesser General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // The klaytn library is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU Lesser General Public License for more details. 13 // 14 // You should have received a copy of the GNU Lesser General Public License 15 // along with the klaytn library. If not, see <http://www.gnu.org/licenses/>. 16 17 pragma solidity ^0.4.24; 18 19 /** 20 * @title SafeMath 21 * @dev Unsigned math operations with safety checks that revert on error 22 */ 23 library SafeMath { 24 /** 25 * @dev Multiplies two unsigned integers, reverts on overflow. 26 */ 27 function mul(uint256 a, uint256 b) internal pure returns (uint256) { 28 // Gas optimization: this is cheaper than requiring 'a' not being zero, but the 29 // benefit is lost if 'b' is also tested. 30 // See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 31 if (a == 0) { 32 return 0; 33 } 34 35 uint256 c = a * b; 36 require(c / a == b); 37 38 return c; 39 } 40 41 /** 42 * @dev Integer division of two unsigned integers truncating the quotient, reverts on division by zero. 43 */ 44 function div(uint256 a, uint256 b) internal pure returns (uint256) { 45 // Solidity only automatically asserts when dividing by 0 46 require(b > 0); 47 uint256 c = a / b; 48 // assert(a == b * c + a % b); // There is no case in which this doesn't hold 49 50 return c; 51 } 52 53 /** 54 * @dev Subtracts two unsigned integers, reverts on overflow (i.e. if subtrahend is greater than minuend). 55 */ 56 function sub(uint256 a, uint256 b) internal pure returns (uint256) { 57 require(b <= a); 58 uint256 c = a - b; 59 60 return c; 61 } 62 63 /** 64 * @dev Adds two unsigned integers, reverts on overflow. 65 */ 66 function add(uint256 a, uint256 b) internal pure returns (uint256) { 67 uint256 c = a + b; 68 require(c >= a); 69 70 return c; 71 } 72 73 /** 74 * @dev Divides two unsigned integers and returns the remainder (unsigned integer modulo), 75 * reverts when dividing by zero. 76 */ 77 function mod(uint256 a, uint256 b) internal pure returns (uint256) { 78 require(b != 0); 79 return a % b; 80 } 81 }