SafeMath.sol 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. require(b > 0, errorMessage);
  104. uint256 c = a / b;
  105. // assert(a == b * c + a % b); // There is no case in which this doesn't hold
  106. return c;
  107. }
  108. /**
  109. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  110. * Reverts when dividing by zero.
  111. *
  112. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  113. * opcode (which leaves remaining gas untouched) while Solidity uses an
  114. * invalid opcode to revert (consuming all remaining gas).
  115. *
  116. * Requirements:
  117. * - The divisor cannot be zero.
  118. */
  119. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  120. return mod(a, b, "SafeMath: modulo by zero");
  121. }
  122. /**
  123. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  124. * Reverts with custom message when dividing by zero.
  125. *
  126. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  127. * opcode (which leaves remaining gas untouched) while Solidity uses an
  128. * invalid opcode to revert (consuming all remaining gas).
  129. *
  130. * Requirements:
  131. * - The divisor cannot be zero.
  132. */
  133. function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  134. require(b != 0, errorMessage);
  135. return a % b;
  136. }
  137. }