SafeMath.sol 6.5 KB

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