SafeMath.sol 5.2 KB

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