SafeMath.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity >=0.6.0 <0.8.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. *
  25. * - Addition cannot overflow.
  26. */
  27. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  28. uint256 c = a + b;
  29. require(c >= a, "SafeMath: addition overflow");
  30. return c;
  31. }
  32. /**
  33. * @dev Returns the subtraction of two unsigned integers, reverting on
  34. * overflow (when the result is negative).
  35. *
  36. * Counterpart to Solidity's `-` operator.
  37. *
  38. * Requirements:
  39. *
  40. * - Subtraction cannot overflow.
  41. */
  42. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  43. return sub(a, b, "SafeMath: subtraction overflow");
  44. }
  45. /**
  46. * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
  47. * overflow (when the result is negative).
  48. *
  49. * Counterpart to Solidity's `-` operator.
  50. *
  51. * Requirements:
  52. *
  53. * - Subtraction cannot overflow.
  54. */
  55. function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  56. require(b <= a, errorMessage);
  57. uint256 c = a - b;
  58. return c;
  59. }
  60. /**
  61. * @dev Returns the multiplication of two unsigned integers, reverting on
  62. * overflow.
  63. *
  64. * Counterpart to Solidity's `*` operator.
  65. *
  66. * Requirements:
  67. *
  68. * - Multiplication cannot overflow.
  69. */
  70. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  71. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  72. // benefit is lost if 'b' is also tested.
  73. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
  74. if (a == 0) {
  75. return 0;
  76. }
  77. uint256 c = a * b;
  78. require(c / a == b, "SafeMath: multiplication overflow");
  79. return c;
  80. }
  81. /**
  82. * @dev Returns the integer division of two unsigned integers. Reverts on
  83. * division by zero. The result is rounded towards zero.
  84. *
  85. * Counterpart to Solidity's `/` operator. Note: this function uses a
  86. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  87. * uses an invalid opcode to revert (consuming all remaining gas).
  88. *
  89. * Requirements:
  90. *
  91. * - The divisor cannot be zero.
  92. */
  93. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  94. return div(a, b, "SafeMath: division by zero");
  95. }
  96. /**
  97. * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
  98. * division by zero. The result is rounded towards zero.
  99. *
  100. * Counterpart to Solidity's `/` operator. Note: this function uses a
  101. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  102. * uses an invalid opcode to revert (consuming all remaining gas).
  103. *
  104. * Requirements:
  105. *
  106. * - The divisor cannot be zero.
  107. */
  108. function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  109. require(b > 0, errorMessage);
  110. uint256 c = a / b;
  111. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  112. return c;
  113. }
  114. /**
  115. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  116. * Reverts when dividing by zero.
  117. *
  118. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  119. * opcode (which leaves remaining gas untouched) while Solidity uses an
  120. * invalid opcode to revert (consuming all remaining gas).
  121. *
  122. * Requirements:
  123. *
  124. * - The divisor cannot be zero.
  125. */
  126. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  127. return mod(a, b, "SafeMath: modulo by zero");
  128. }
  129. /**
  130. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  131. * Reverts with custom message when dividing by zero.
  132. *
  133. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  134. * opcode (which leaves remaining gas untouched) while Solidity uses an
  135. * invalid opcode to revert (consuming all remaining gas).
  136. *
  137. * Requirements:
  138. *
  139. * - The divisor cannot be zero.
  140. */
  141. function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  142. require(b != 0, errorMessage);
  143. return a % b;
  144. }
  145. }