SafeMath.sol 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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, with an overflow flag.
  19. */
  20. function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  21. uint256 c = a + b;
  22. if (c < a) return (false, 0);
  23. return (true, c);
  24. }
  25. /**
  26. * @dev Returns the substraction of two unsigned integers, with an overflow flag.
  27. */
  28. function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  29. if (b > a) return (false, 0);
  30. return (true, a - b);
  31. }
  32. /**
  33. * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
  34. */
  35. function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  36. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  37. // benefit is lost if 'b' is also tested.
  38. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
  39. if (a == 0) return (true, 0);
  40. uint256 c = a * b;
  41. if (c / a != b) return (false, 0);
  42. return (true, c);
  43. }
  44. /**
  45. * @dev Returns the division of two unsigned integers, with a division by zero flag.
  46. */
  47. function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  48. if (b == 0) return (false, 0);
  49. return (true, a / b);
  50. }
  51. /**
  52. * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
  53. */
  54. function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  55. if (b == 0) return (false, 0);
  56. return (true, a % b);
  57. }
  58. /**
  59. * @dev Returns the addition of two unsigned integers, reverting on
  60. * overflow.
  61. *
  62. * Counterpart to Solidity's `+` operator.
  63. *
  64. * Requirements:
  65. *
  66. * - Addition cannot overflow.
  67. */
  68. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  69. uint256 c = a + b;
  70. require(c >= a, "SafeMath: addition overflow");
  71. return c;
  72. }
  73. /**
  74. * @dev Returns the subtraction of two unsigned integers, reverting on
  75. * overflow (when the result is negative).
  76. *
  77. * Counterpart to Solidity's `-` operator.
  78. *
  79. * Requirements:
  80. *
  81. * - Subtraction cannot overflow.
  82. */
  83. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  84. require(b <= a, "SafeMath: subtraction overflow");
  85. return a - b;
  86. }
  87. /**
  88. * @dev Returns the multiplication of two unsigned integers, reverting on
  89. * overflow.
  90. *
  91. * Counterpart to Solidity's `*` operator.
  92. *
  93. * Requirements:
  94. *
  95. * - Multiplication cannot overflow.
  96. */
  97. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  98. if (a == 0) return 0;
  99. uint256 c = a * b;
  100. require(c / a == b, "SafeMath: multiplication overflow");
  101. return c;
  102. }
  103. /**
  104. * @dev Returns the integer division of two unsigned integers, reverting on
  105. * division by zero. The result is rounded towards zero.
  106. *
  107. * Counterpart to Solidity's `/` operator. Note: this function uses a
  108. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  109. * uses an invalid opcode to revert (consuming all remaining gas).
  110. *
  111. * Requirements:
  112. *
  113. * - The divisor cannot be zero.
  114. */
  115. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  116. require(b > 0, "SafeMath: division by zero");
  117. return a / b;
  118. }
  119. /**
  120. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  121. * reverting when dividing by zero.
  122. *
  123. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  124. * opcode (which leaves remaining gas untouched) while Solidity uses an
  125. * invalid opcode to revert (consuming all remaining gas).
  126. *
  127. * Requirements:
  128. *
  129. * - The divisor cannot be zero.
  130. */
  131. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  132. require(b > 0, "SafeMath: modulo by zero");
  133. return a % b;
  134. }
  135. /**
  136. * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
  137. * overflow (when the result is negative).
  138. *
  139. * CAUTION: This function is deprecated because it requires allocating memory for the error
  140. * message unnecessarily. For custom revert reasons use {trySub}.
  141. *
  142. * Counterpart to Solidity's `-` operator.
  143. *
  144. * Requirements:
  145. *
  146. * - Subtraction cannot overflow.
  147. */
  148. function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  149. require(b <= a, errorMessage);
  150. return a - b;
  151. }
  152. /**
  153. * @dev Returns the integer division of two unsigned integers, reverting with custom message on
  154. * division by zero. The result is rounded towards zero.
  155. *
  156. * CAUTION: This function is deprecated because it requires allocating memory for the error
  157. * message unnecessarily. For custom revert reasons use {tryDiv}.
  158. *
  159. * Counterpart to Solidity's `/` operator. Note: this function uses a
  160. * `revert` opcode (which leaves remaining gas untouched) while Solidity
  161. * uses an invalid opcode to revert (consuming all remaining gas).
  162. *
  163. * Requirements:
  164. *
  165. * - The divisor cannot be zero.
  166. */
  167. function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  168. require(b > 0, errorMessage);
  169. return a / b;
  170. }
  171. /**
  172. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  173. * reverting with custom message when dividing by zero.
  174. *
  175. * CAUTION: This function is deprecated because it requires allocating memory for the error
  176. * message unnecessarily. For custom revert reasons use {tryMod}.
  177. *
  178. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  179. * opcode (which leaves remaining gas untouched) while Solidity uses an
  180. * invalid opcode to revert (consuming all remaining gas).
  181. *
  182. * Requirements:
  183. *
  184. * - The divisor cannot be zero.
  185. */
  186. function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
  187. require(b > 0, errorMessage);
  188. return a % b;
  189. }
  190. }