123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204 |
- // SPDX-License-Identifier: MIT
- pragma solidity >=0.6.0 <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.
- */
- library SafeMath {
- /**
- * @dev Returns the addition of two unsigned integers, with an overflow flag.
- */
- function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
- uint256 c = a + b;
- if (c < a) return (false, 0);
- return (true, c);
- }
- /**
- * @dev Returns the substraction of two unsigned integers, with an overflow flag.
- */
- function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
- if (b > a) return (false, 0);
- return (true, a - b);
- }
- /**
- * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
- */
- 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);
- }
- /**
- * @dev Returns the division of two unsigned integers, with a division by zero flag.
- */
- function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
- if (b == 0) return (false, 0);
- return (true, a / b);
- }
- /**
- * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
- */
- function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
- if (b == 0) return (false, 0);
- return (true, a % b);
- }
- /**
- * @dev Returns the addition of two unsigned integers, reverting on
- * overflow.
- *
- * Counterpart to Solidity's `+` operator.
- *
- * Requirements:
- *
- * - 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;
- }
- /**
- * @dev Returns the subtraction of two unsigned integers, reverting on
- * overflow (when the result is negative).
- *
- * Counterpart to Solidity's `-` operator.
- *
- * Requirements:
- *
- * - Subtraction cannot overflow.
- */
- function sub(uint256 a, uint256 b) internal pure returns (uint256) {
- require(b <= a, "SafeMath: subtraction overflow");
- return a - b;
- }
- /**
- * @dev Returns the multiplication of two unsigned integers, reverting on
- * overflow.
- *
- * Counterpart to Solidity's `*` operator.
- *
- * Requirements:
- *
- * - 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;
- }
- /**
- * @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).
- *
- * 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;
- }
- /**
- * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
- * reverting when dividing by zero.
- *
- * 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).
- *
- * Requirements:
- *
- * - 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;
- }
- /**
- * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
- * overflow (when the result is negative).
- *
- * CAUTION: This function is deprecated because it requires allocating memory for the error
- * message unnecessarily. For custom revert reasons use {trySub}.
- *
- * Counterpart to Solidity's `-` operator.
- *
- * Requirements:
- *
- * - Subtraction cannot overflow.
- */
- function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
- 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. Note: this function uses a
- * `revert` opcode (which leaves remaining gas untouched) while Solidity
- * uses an invalid opcode to revert (consuming all remaining gas).
- *
- * Requirements:
- *
- * - 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;
- }
- /**
- * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
- * reverting with custom message when dividing by zero.
- *
- * CAUTION: This function is deprecated because it requires allocating memory for the error
- * message unnecessarily. For custom revert reasons use {tryMod}.
- *
- * 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).
- *
- * Requirements:
- *
- * - 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;
- }
- }
|