SafeMath.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. /**
  4. * @dev Wrappers over Solidity's arithmetic operations with added overflow
  5. * checks.
  6. *
  7. * Arithmetic operations in Solidity wrap on overflow. This can easily result
  8. * in bugs, because programmers usually assume that an overflow raises an
  9. * error, which is the standard behavior in high level programming languages.
  10. * `SafeMath` restores this intuition by reverting the transaction when an
  11. * operation overflows.
  12. *
  13. * Using this library instead of the unchecked operations eliminates an entire
  14. * class of bugs, so it's recommended to use it always.
  15. */
  16. library SafeMath {
  17. /**
  18. * @dev Returns the addition of two unsigned integers, reverting on
  19. * overflow.
  20. *
  21. * Counterpart to Solidity's `+` operator.
  22. *
  23. * Requirements:
  24. * - Addition cannot overflow.
  25. */
  26. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  27. uint256 c = a + b;
  28. require(c >= a, "SafeMath: addition overflow");
  29. return c;
  30. }
  31. /**
  32. * @dev Returns the subtraction of two unsigned integers, reverting on
  33. * overflow (when the result is negative).
  34. *
  35. * Counterpart to Solidity's `-` operator.
  36. *
  37. * Requirements:
  38. * - Subtraction cannot overflow.
  39. */
  40. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  41. return sub(a, b, "SafeMath: subtraction overflow");
  42. }
  43. /**
  44. * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
  45. * overflow (when the result is negative).
  46. *
  47. * Counterpart to Solidity's `-` operator.
  48. *
  49. * Requirements:
  50. * - Subtraction cannot overflow.
  51. */
  52. function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  53. require(b <= a, errorMessage);
  54. uint256 c = a - b;
  55. return c;
  56. }
  57. /**
  58. * @dev Returns the multiplication of two unsigned integers, reverting on
  59. * overflow.
  60. *
  61. * Counterpart to Solidity's `*` operator.
  62. *
  63. * Requirements:
  64. * - Multiplication cannot overflow.
  65. */
  66. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  67. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  68. // benefit is lost if 'b' is also tested.
  69. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
  70. if (a == 0) {
  71. return 0;
  72. }
  73. uint256 c = a * b;
  74. require(c / a == b, "SafeMath: multiplication overflow");
  75. return c;
  76. }
  77. /**
  78. * @dev Returns the integer division of two unsigned integers. Reverts on
  79. * division by zero. The result is rounded towards zero.
  80. *
  81. * Counterpart to Solidity's `/` operator. Note: this function uses a
  82. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  83. * uses an invalid opcode to revert (consuming all remaining gas).
  84. *
  85. * Requirements:
  86. * - The divisor cannot be zero.
  87. */
  88. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  89. return div(a, b, "SafeMath: division by zero");
  90. }
  91. /**
  92. * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
  93. * division by zero. The result is rounded towards zero.
  94. *
  95. * Counterpart to Solidity's `/` operator. Note: this function uses a
  96. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  97. * uses an invalid opcode to revert (consuming all remaining gas).
  98. *
  99. * Requirements:
  100. * - The divisor cannot be zero.
  101. */
  102. function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  103. // Solidity only automatically asserts when dividing by 0
  104. require(b > 0, errorMessage);
  105. uint256 c = a / b;
  106. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  107. return c;
  108. }
  109. /**
  110. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  111. * Reverts when dividing by zero.
  112. *
  113. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  114. * opcode (which leaves remaining gas untouched) while Solidity uses an
  115. * invalid opcode to revert (consuming all remaining gas).
  116. *
  117. * Requirements:
  118. * - The divisor cannot be zero.
  119. */
  120. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  121. return mod(a, b, "SafeMath: modulo by zero");
  122. }
  123. /**
  124. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  125. * Reverts with custom message when dividing by zero.
  126. *
  127. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  128. * opcode (which leaves remaining gas untouched) while Solidity uses an
  129. * invalid opcode to revert (consuming all remaining gas).
  130. *
  131. * Requirements:
  132. * - The divisor cannot be zero.
  133. */
  134. function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  135. require(b != 0, errorMessage);
  136. return a % b;
  137. }
  138. }