/ contracts / utils / SafeMath.sol
SafeMath.sol
  1  // SPDX-License-Identifier: MIT
  2  
  3  pragma solidity >=0.6.0 <0.8.0;
  4  
  5  /**
  6   * @dev Wrappers over Solidity's arithmetic operations with added overflow
  7   * checks.
  8   *
  9   * Arithmetic operations in Solidity wrap on overflow. This can easily result
 10   * in bugs, because programmers usually assume that an overflow raises an
 11   * error, which is the standard behavior in high level programming languages.
 12   * `SafeMath` restores this intuition by reverting the transaction when an
 13   * operation overflows.
 14   *
 15   * Using this library instead of the unchecked operations eliminates an entire
 16   * class of bugs, so it's recommended to use it always.
 17   */
 18  library SafeMath {
 19      /**
 20       * @dev Returns the addition of two unsigned integers, reverting on
 21       * overflow.
 22       *
 23       * Counterpart to Solidity's `+` operator.
 24       *
 25       * Requirements:
 26       *
 27       * - Addition cannot overflow.
 28       */
 29      function add(uint256 a, uint256 b) internal pure returns (uint256) {
 30          uint256 c = a + b;
 31          require(c >= a, "SafeMath: addition overflow");
 32  
 33          return c;
 34      }
 35  
 36      /**
 37       * @dev Returns the subtraction of two unsigned integers, reverting on
 38       * overflow (when the result is negative).
 39       *
 40       * Counterpart to Solidity's `-` operator.
 41       *
 42       * Requirements:
 43       *
 44       * - Subtraction cannot overflow.
 45       */
 46      function sub(uint256 a, uint256 b) internal pure returns (uint256) {
 47          return sub(a, b, "SafeMath: subtraction overflow");
 48      }
 49  
 50      /**
 51       * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
 52       * overflow (when the result is negative).
 53       *
 54       * Counterpart to Solidity's `-` operator.
 55       *
 56       * Requirements:
 57       *
 58       * - Subtraction cannot overflow.
 59       */
 60      function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
 61          require(b <= a, errorMessage);
 62          uint256 c = a - b;
 63  
 64          return c;
 65      }
 66  
 67      /**
 68       * @dev Returns the multiplication of two unsigned integers, reverting on
 69       * overflow.
 70       *
 71       * Counterpart to Solidity's `*` operator.
 72       *
 73       * Requirements:
 74       *
 75       * - Multiplication cannot overflow.
 76       */
 77      function mul(uint256 a, uint256 b) internal pure returns (uint256) {
 78          // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
 79          // benefit is lost if 'b' is also tested.
 80          // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
 81          if (a == 0) {
 82              return 0;
 83          }
 84  
 85          uint256 c = a * b;
 86          require(c / a == b, "SafeMath: multiplication overflow");
 87  
 88          return c;
 89      }
 90  
 91      /**
 92       * @dev Returns the integer division of two unsigned integers. Reverts on
 93       * division by zero. The result is rounded towards zero.
 94       *
 95       * Counterpart to Solidity's `/` operator. Note: this function uses a
 96       * `revert` opsymbol (which leaves remaining gas untouched) while Solidity
 97       * uses an invalid opsymbol to revert (consuming all remaining gas).
 98       *
 99       * Requirements:
100       *
101       * - The divisor cannot be zero.
102       */
103      function div(uint256 a, uint256 b) internal pure returns (uint256) {
104          return div(a, b, "SafeMath: division by zero");
105      }
106  
107      /**
108       * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
109       * division by zero. The result is rounded towards zero.
110       *
111       * Counterpart to Solidity's `/` operator. Note: this function uses a
112       * `revert` opsymbol (which leaves remaining gas untouched) while Solidity
113       * uses an invalid opsymbol to revert (consuming all remaining gas).
114       *
115       * Requirements:
116       *
117       * - The divisor cannot be zero.
118       */
119      function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
120          require(b > 0, errorMessage);
121          uint256 c = a / b;
122          // assert(a == b * c + a % b); // There is no case in which this doesn't hold
123  
124          return c;
125      }
126  
127      /**
128       * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
129       * Reverts when dividing by zero.
130       *
131       * Counterpart to Solidity's `%` operator. This function uses a `revert`
132       * opsymbol (which leaves remaining gas untouched) while Solidity uses an
133       * invalid opsymbol to revert (consuming all remaining gas).
134       *
135       * Requirements:
136       *
137       * - The divisor cannot be zero.
138       */
139      function mod(uint256 a, uint256 b) internal pure returns (uint256) {
140          return mod(a, b, "SafeMath: modulo by zero");
141      }
142  
143      /**
144       * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
145       * Reverts with custom message when dividing by zero.
146       *
147       * Counterpart to Solidity's `%` operator. This function uses a `revert`
148       * opsymbol (which leaves remaining gas untouched) while Solidity uses an
149       * invalid opsymbol to revert (consuming all remaining gas).
150       *
151       * Requirements:
152       *
153       * - The divisor cannot be zero.
154       */
155      function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
156          require(b != 0, errorMessage);
157          return a % b;
158      }
159  }