aboutsummaryrefslogtreecommitdiffstats
path: root/contracts/utils/contracts/src/SafeMath.sol
diff options
context:
space:
mode:
Diffstat (limited to 'contracts/utils/contracts/src/SafeMath.sol')
-rw-r--r--contracts/utils/contracts/src/SafeMath.sol87
1 files changed, 87 insertions, 0 deletions
diff --git a/contracts/utils/contracts/src/SafeMath.sol b/contracts/utils/contracts/src/SafeMath.sol
new file mode 100644
index 000000000..d7a4a603e
--- /dev/null
+++ b/contracts/utils/contracts/src/SafeMath.sol
@@ -0,0 +1,87 @@
+pragma solidity ^0.4.24;
+
+
+contract SafeMath {
+
+ function safeMul(uint256 a, uint256 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ if (a == 0) {
+ return 0;
+ }
+ uint256 c = a * b;
+ require(
+ c / a == b,
+ "UINT256_OVERFLOW"
+ );
+ return c;
+ }
+
+ function safeDiv(uint256 a, uint256 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ uint256 c = a / b;
+ return c;
+ }
+
+ function safeSub(uint256 a, uint256 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ require(
+ b <= a,
+ "UINT256_UNDERFLOW"
+ );
+ return a - b;
+ }
+
+ function safeAdd(uint256 a, uint256 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ uint256 c = a + b;
+ require(
+ c >= a,
+ "UINT256_OVERFLOW"
+ );
+ return c;
+ }
+
+ function max64(uint64 a, uint64 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ return a >= b ? a : b;
+ }
+
+ function min64(uint64 a, uint64 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ return a < b ? a : b;
+ }
+
+ function max256(uint256 a, uint256 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ return a >= b ? a : b;
+ }
+
+ function min256(uint256 a, uint256 b)
+ internal
+ pure
+ returns (uint256)
+ {
+ return a < b ? a : b;
+ }
+}