SafeMath.sol 7.0 KB

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