SafeMath

Overview

License: MIT

library SafeMath

Wrappers over Solidity's arithmetic operations.

NOTE: SafeMath is generally not needed starting with Solidity 0.8, since the compiler now has built in overflow checking.

Functions info

tryAdd

function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256)

Returns the addition of two unsigned integers, with an overflow flag.

Available since v3.4.

trySub

function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256)

Returns the substraction of two unsigned integers, with an overflow flag.

Available since v3.4.

tryMul

function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256)

Returns the multiplication of two unsigned integers, with an overflow flag.

Available since v3.4.

tryDiv

function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256)

Returns the division of two unsigned integers, with a division by zero flag.

Available since v3.4.

tryMod

function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256)

Returns the remainder of dividing two unsigned integers, with a division by zero flag.

Available since v3.4.

add

function add(uint256 a, uint256 b) internal pure returns (uint256)

Returns the addition of two unsigned integers, reverting on overflow.

Counterpart to Solidity's + operator.

Requirements:

  • Addition cannot overflow.

sub

function sub(uint256 a, uint256 b) internal pure returns (uint256)

Returns the subtraction of two unsigned integers, reverting on overflow (when the result is negative).

Counterpart to Solidity's - operator.

Requirements:

  • Subtraction cannot overflow.

mul

function mul(uint256 a, uint256 b) internal pure returns (uint256)

Returns the multiplication of two unsigned integers, reverting on overflow.

Counterpart to Solidity's * operator.

Requirements:

  • Multiplication cannot overflow.

div

function div(uint256 a, uint256 b) internal pure returns (uint256)

Returns the integer division of two unsigned integers, reverting on division by zero. The result is rounded towards zero.

Counterpart to Solidity's / operator.

Requirements:

  • The divisor cannot be zero.

mod

function mod(uint256 a, uint256 b) internal pure returns (uint256)

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.

sub

function sub(
    uint256 a,
    uint256 b,
    string memory errorMessage
) internal pure returns (uint256)

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.

div

function div(
    uint256 a,
    uint256 b,
    string memory errorMessage
) internal pure returns (uint256)

Returns the integer division of two unsigned integers, reverting with custom message 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.

mod

function mod(
    uint256 a,
    uint256 b,
    string memory errorMessage
) internal pure returns (uint256)

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.

Last updated