SafeMath.sol 6.4 KB

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