SafeMath.sol 6.2 KB

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