|
@@ -1,19 +1,9 @@
|
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
|
|
-pragma solidity >=0.6.0 <0.8.0;
|
|
|
+pragma solidity ^0.8.0;
|
|
|
|
|
|
/**
|
|
|
- * @dev Wrappers over Solidity's arithmetic operations with added overflow
|
|
|
- * checks.
|
|
|
- *
|
|
|
- * Arithmetic operations in Solidity wrap on overflow. This can easily result
|
|
|
- * in bugs, because programmers usually assume that an overflow raises an
|
|
|
- * error, which is the standard behavior in high level programming languages.
|
|
|
- * `SafeMath` restores this intuition by reverting the transaction when an
|
|
|
- * operation overflows.
|
|
|
- *
|
|
|
- * Using this library instead of the unchecked operations eliminates an entire
|
|
|
- * class of bugs, so it's recommended to use it always.
|
|
|
+ * @dev Wrappers over Solidity's arithmetic operations.
|
|
|
*/
|
|
|
library SafeMath {
|
|
|
/**
|
|
@@ -22,9 +12,11 @@ library SafeMath {
|
|
|
* _Available since v3.4._
|
|
|
*/
|
|
|
function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
|
- uint256 c = a + b;
|
|
|
- if (c < a) return (false, 0);
|
|
|
- return (true, c);
|
|
|
+ unchecked {
|
|
|
+ uint256 c = a + b;
|
|
|
+ if (c < a) return (false, 0);
|
|
|
+ return (true, c);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -33,8 +25,10 @@ library SafeMath {
|
|
|
* _Available since v3.4._
|
|
|
*/
|
|
|
function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
|
- if (b > a) return (false, 0);
|
|
|
- return (true, a - b);
|
|
|
+ unchecked {
|
|
|
+ if (b > a) return (false, 0);
|
|
|
+ return (true, a - b);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -43,13 +37,15 @@ library SafeMath {
|
|
|
* _Available since v3.4._
|
|
|
*/
|
|
|
function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
|
- // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
|
|
|
- // benefit is lost if 'b' is also tested.
|
|
|
- // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
|
|
|
- if (a == 0) return (true, 0);
|
|
|
- uint256 c = a * b;
|
|
|
- if (c / a != b) return (false, 0);
|
|
|
- return (true, c);
|
|
|
+ unchecked {
|
|
|
+ // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
|
|
|
+ // benefit is lost if 'b' is also tested.
|
|
|
+ // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
|
|
|
+ if (a == 0) return (true, 0);
|
|
|
+ uint256 c = a * b;
|
|
|
+ if (c / a != b) return (false, 0);
|
|
|
+ return (true, c);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -58,8 +54,10 @@ library SafeMath {
|
|
|
* _Available since v3.4._
|
|
|
*/
|
|
|
function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
|
- if (b == 0) return (false, 0);
|
|
|
- return (true, a / b);
|
|
|
+ unchecked {
|
|
|
+ if (b == 0) return (false, 0);
|
|
|
+ return (true, a / b);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -68,8 +66,10 @@ library SafeMath {
|
|
|
* _Available since v3.4._
|
|
|
*/
|
|
|
function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
|
|
|
- if (b == 0) return (false, 0);
|
|
|
- return (true, a % b);
|
|
|
+ unchecked {
|
|
|
+ if (b == 0) return (false, 0);
|
|
|
+ return (true, a % b);
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -83,9 +83,7 @@ library SafeMath {
|
|
|
* - Addition cannot overflow.
|
|
|
*/
|
|
|
function add(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
- uint256 c = a + b;
|
|
|
- require(c >= a, "SafeMath: addition overflow");
|
|
|
- return c;
|
|
|
+ return a + b;
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -99,7 +97,6 @@ library SafeMath {
|
|
|
* - Subtraction cannot overflow.
|
|
|
*/
|
|
|
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
- require(b <= a, "SafeMath: subtraction overflow");
|
|
|
return a - b;
|
|
|
}
|
|
|
|
|
@@ -114,26 +111,20 @@ library SafeMath {
|
|
|
* - Multiplication cannot overflow.
|
|
|
*/
|
|
|
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
- if (a == 0) return 0;
|
|
|
- uint256 c = a * b;
|
|
|
- require(c / a == b, "SafeMath: multiplication overflow");
|
|
|
- return c;
|
|
|
+ return a * b;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @dev Returns the integer division of two unsigned integers, reverting on
|
|
|
* division by zero. The result is rounded towards zero.
|
|
|
*
|
|
|
- * Counterpart to Solidity's `/` operator. Note: this function uses a
|
|
|
- * `revert` opcode (which leaves remaining gas untouched) while Solidity
|
|
|
- * uses an invalid opcode to revert (consuming all remaining gas).
|
|
|
+ * Counterpart to Solidity's `/` operator.
|
|
|
*
|
|
|
* Requirements:
|
|
|
*
|
|
|
* - The divisor cannot be zero.
|
|
|
*/
|
|
|
function div(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
- require(b > 0, "SafeMath: division by zero");
|
|
|
return a / b;
|
|
|
}
|
|
|
|
|
@@ -150,7 +141,6 @@ library SafeMath {
|
|
|
* - The divisor cannot be zero.
|
|
|
*/
|
|
|
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
|
- require(b > 0, "SafeMath: modulo by zero");
|
|
|
return a % b;
|
|
|
}
|
|
|
|
|
@@ -168,16 +158,19 @@ library SafeMath {
|
|
|
* - Subtraction cannot overflow.
|
|
|
*/
|
|
|
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
|
|
|
- require(b <= a, errorMessage);
|
|
|
- return a - b;
|
|
|
+ unchecked {
|
|
|
+ require(b <= a, errorMessage);
|
|
|
+ return a - b;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* @dev Returns the integer division of two unsigned integers, reverting with custom message on
|
|
|
* division by zero. The result is rounded towards zero.
|
|
|
*
|
|
|
- * CAUTION: This function is deprecated because it requires allocating memory for the error
|
|
|
- * message unnecessarily. For custom revert reasons use {tryDiv}.
|
|
|
+ * Counterpart to Solidity's `%` operator. This function uses a `revert`
|
|
|
+ * opcode (which leaves remaining gas untouched) while Solidity uses an
|
|
|
+ * invalid opcode to revert (consuming all remaining gas).
|
|
|
*
|
|
|
* Counterpart to Solidity's `/` operator. Note: this function uses a
|
|
|
* `revert` opcode (which leaves remaining gas untouched) while Solidity
|
|
@@ -188,8 +181,10 @@ library SafeMath {
|
|
|
* - The divisor cannot be zero.
|
|
|
*/
|
|
|
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
|
|
|
- require(b > 0, errorMessage);
|
|
|
- return a / b;
|
|
|
+ unchecked {
|
|
|
+ require(b > 0, errorMessage);
|
|
|
+ return a / b;
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
/**
|
|
@@ -208,7 +203,9 @@ library SafeMath {
|
|
|
* - The divisor cannot be zero.
|
|
|
*/
|
|
|
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
|
|
|
- require(b > 0, errorMessage);
|
|
|
- return a % b;
|
|
|
+ unchecked {
|
|
|
+ require(b > 0, errorMessage);
|
|
|
+ return a % b;
|
|
|
+ }
|
|
|
}
|
|
|
}
|