SafeMath.sol 6.6 KB

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