SafeMath.sol 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  52. require(b <= a, errorMessage);
  53. uint256 c = a - b;
  54. return c;
  55. }
  56. /**
  57. * @dev Returns the multiplication of two unsigned integers, reverting on
  58. * overflow.
  59. *
  60. * Counterpart to Solidity's `*` operator.
  61. *
  62. * Requirements:
  63. * - Multiplication cannot overflow.
  64. */
  65. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  66. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  67. // benefit is lost if 'b' is also tested.
  68. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
  69. if (a == 0) {
  70. return 0;
  71. }
  72. uint256 c = a * b;
  73. require(c / a == b, "SafeMath: multiplication overflow");
  74. return c;
  75. }
  76. /**
  77. * @dev Returns the integer division of two unsigned integers. Reverts on
  78. * division by zero. The result is rounded towards zero.
  79. *
  80. * Counterpart to Solidity's `/` operator. Note: this function uses a
  81. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  82. * uses an invalid opcode to revert (consuming all remaining gas).
  83. *
  84. * Requirements:
  85. * - The divisor cannot be zero.
  86. */
  87. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  88. return div(a, b, "SafeMath: division by zero");
  89. }
  90. /**
  91. * @dev Returns the integer division of two unsigned integers. Reverts with custom message on
  92. * division by zero. The result is rounded towards zero.
  93. *
  94. * Counterpart to Solidity's `/` operator. Note: this function uses a
  95. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  96. * uses an invalid opcode to revert (consuming all remaining gas).
  97. *
  98. * Requirements:
  99. * - The divisor cannot be zero.
  100. */
  101. function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  102. // Solidity only automatically asserts when dividing by 0
  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. }