SafeMath.sol 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts (last updated v4.6.0) (utils/math/SafeMath.sol)
  3. pragma solidity ^0.8.0;
  4. // CAUTION
  5. // This version of SafeMath should only be used with Solidity 0.8 or later,
  6. // because it relies on the compiler's built in overflow checks.
  7. /**
  8. * @dev Wrappers over Solidity's arithmetic operations.
  9. *
  10. * NOTE: `SafeMath` is generally not needed starting with Solidity 0.8, since the compiler
  11. * now has built in overflow checking.
  12. */
  13. library SafeMath {
  14. /**
  15. * @dev Returns the addition of two unsigned integers, with an overflow flag.
  16. *
  17. * _Available since v3.4._
  18. */
  19. function tryAdd(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  20. unchecked {
  21. uint256 c = a + b;
  22. if (c < a) return (false, 0);
  23. return (true, c);
  24. }
  25. }
  26. /**
  27. * @dev Returns the subtraction of two unsigned integers, with an overflow flag.
  28. *
  29. * _Available since v3.4._
  30. */
  31. function trySub(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  32. unchecked {
  33. if (b > a) return (false, 0);
  34. return (true, a - b);
  35. }
  36. }
  37. /**
  38. * @dev Returns the multiplication of two unsigned integers, with an overflow flag.
  39. *
  40. * _Available since v3.4._
  41. */
  42. function tryMul(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  43. unchecked {
  44. // Gas optimization: this is cheaper than requiring 'a' not being zero, but the
  45. // benefit is lost if 'b' is also tested.
  46. // See: https://github.com/OpenZeppelin/openzeppelin-contracts/pull/522
  47. if (a == 0) return (true, 0);
  48. uint256 c = a * b;
  49. if (c / a != b) return (false, 0);
  50. return (true, c);
  51. }
  52. }
  53. /**
  54. * @dev Returns the division of two unsigned integers, with a division by zero flag.
  55. *
  56. * _Available since v3.4._
  57. */
  58. function tryDiv(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  59. unchecked {
  60. if (b == 0) return (false, 0);
  61. return (true, a / b);
  62. }
  63. }
  64. /**
  65. * @dev Returns the remainder of dividing two unsigned integers, with a division by zero flag.
  66. *
  67. * _Available since v3.4._
  68. */
  69. function tryMod(uint256 a, uint256 b) internal pure returns (bool, uint256) {
  70. unchecked {
  71. if (b == 0) return (false, 0);
  72. return (true, a % b);
  73. }
  74. }
  75. /**
  76. * @dev Returns the addition of two unsigned integers, reverting on
  77. * overflow.
  78. *
  79. * Counterpart to Solidity's `+` operator.
  80. *
  81. * Requirements:
  82. *
  83. * - Addition cannot overflow.
  84. */
  85. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  86. return a + b;
  87. }
  88. /**
  89. * @dev Returns the subtraction of two unsigned integers, reverting on
  90. * overflow (when the result is negative).
  91. *
  92. * Counterpart to Solidity's `-` operator.
  93. *
  94. * Requirements:
  95. *
  96. * - Subtraction cannot overflow.
  97. */
  98. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  99. return a - b;
  100. }
  101. /**
  102. * @dev Returns the multiplication of two unsigned integers, reverting on
  103. * overflow.
  104. *
  105. * Counterpart to Solidity's `*` operator.
  106. *
  107. * Requirements:
  108. *
  109. * - Multiplication cannot overflow.
  110. */
  111. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  112. return a * b;
  113. }
  114. /**
  115. * @dev Returns the integer division of two unsigned integers, reverting on
  116. * division by zero. The result is rounded towards zero.
  117. *
  118. * Counterpart to Solidity's `/` operator.
  119. *
  120. * Requirements:
  121. *
  122. * - The divisor cannot be zero.
  123. */
  124. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  125. return a / b;
  126. }
  127. /**
  128. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  129. * reverting when dividing by zero.
  130. *
  131. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  132. * opcode (which leaves remaining gas untouched) while Solidity uses an
  133. * invalid opcode to revert (consuming all remaining gas).
  134. *
  135. * Requirements:
  136. *
  137. * - The divisor cannot be zero.
  138. */
  139. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  140. return a % b;
  141. }
  142. /**
  143. * @dev Returns the subtraction of two unsigned integers, reverting with custom message on
  144. * overflow (when the result is negative).
  145. *
  146. * CAUTION: This function is deprecated because it requires allocating memory for the error
  147. * message unnecessarily. For custom revert reasons use {trySub}.
  148. *
  149. * Counterpart to Solidity's `-` operator.
  150. *
  151. * Requirements:
  152. *
  153. * - Subtraction cannot overflow.
  154. */
  155. function sub(
  156. uint256 a,
  157. uint256 b,
  158. string memory errorMessage
  159. ) internal pure returns (uint256) {
  160. unchecked {
  161. require(b <= a, errorMessage);
  162. return a - b;
  163. }
  164. }
  165. /**
  166. * @dev Returns the integer division of two unsigned integers, reverting with custom message on
  167. * division by zero. The result is rounded towards zero.
  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(
  178. uint256 a,
  179. uint256 b,
  180. string memory errorMessage
  181. ) internal pure returns (uint256) {
  182. unchecked {
  183. require(b > 0, errorMessage);
  184. return a / b;
  185. }
  186. }
  187. /**
  188. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  189. * reverting with custom message when dividing by zero.
  190. *
  191. * CAUTION: This function is deprecated because it requires allocating memory for the error
  192. * message unnecessarily. For custom revert reasons use {tryMod}.
  193. *
  194. * Counterpart to Solidity's `%` operator. This function uses a `revert`
  195. * opcode (which leaves remaining gas untouched) while Solidity uses an
  196. * invalid opcode to revert (consuming all remaining gas).
  197. *
  198. * Requirements:
  199. *
  200. * - The divisor cannot be zero.
  201. */
  202. function mod(
  203. uint256 a,
  204. uint256 b,
  205. string memory errorMessage
  206. ) internal pure returns (uint256) {
  207. unchecked {
  208. require(b > 0, errorMessage);
  209. return a % b;
  210. }
  211. }
  212. }