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