You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.8 KiB
60 lines
1.8 KiB
pragma solidity ^0.6.0;
|
|
|
|
/**
|
|
* @title SignedSafeMath
|
|
* @dev Signed math operations with safety checks that revert on error.
|
|
*/
|
|
library SignedSafeMath {
|
|
int256 constant private INT256_MIN = -2**255;
|
|
|
|
/**
|
|
* @dev Multiplies two signed integers, reverts on overflow.
|
|
*/
|
|
function mul(int256 a, int256 b) internal pure returns (int256) {
|
|
// 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 0;
|
|
}
|
|
|
|
require(!(a == -1 && b == INT256_MIN), "SignedSafeMath: multiplication overflow");
|
|
|
|
int256 c = a * b;
|
|
require(c / a == b, "SignedSafeMath: multiplication overflow");
|
|
|
|
return c;
|
|
}
|
|
|
|
/**
|
|
* @dev Integer division of two signed integers truncating the quotient, reverts on division by zero.
|
|
*/
|
|
function div(int256 a, int256 b) internal pure returns (int256) {
|
|
require(b != 0, "SignedSafeMath: division by zero");
|
|
require(!(b == -1 && a == INT256_MIN), "SignedSafeMath: division overflow");
|
|
|
|
int256 c = a / b;
|
|
|
|
return c;
|
|
}
|
|
|
|
/**
|
|
* @dev Subtracts two signed integers, reverts on overflow.
|
|
*/
|
|
function sub(int256 a, int256 b) internal pure returns (int256) {
|
|
int256 c = a - b;
|
|
require((b >= 0 && c <= a) || (b < 0 && c > a), "SignedSafeMath: subtraction overflow");
|
|
|
|
return c;
|
|
}
|
|
|
|
/**
|
|
* @dev Adds two signed integers, reverts on overflow.
|
|
*/
|
|
function add(int256 a, int256 b) internal pure returns (int256) {
|
|
int256 c = a + b;
|
|
require((b >= 0 && c >= a) || (b < 0 && c < a), "SignedSafeMath: addition overflow");
|
|
|
|
return c;
|
|
}
|
|
}
|
|
|