SafeMath.sol 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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, reverting on
  9. * overflow.
  10. *
  11. * Counterpart to Solidity's `+` operator.
  12. *
  13. * Requirements:
  14. *
  15. * - Addition cannot overflow.
  16. */
  17. function add(uint256 a, uint256 b) internal pure returns (uint256) {
  18. return a + b;
  19. }
  20. /**
  21. * @dev Returns the subtraction of two unsigned integers, reverting on
  22. * overflow (when the result is negative).
  23. *
  24. * Counterpart to Solidity's `-` operator.
  25. *
  26. * Requirements:
  27. *
  28. * - Subtraction cannot overflow.
  29. */
  30. function sub(uint256 a, uint256 b) internal pure returns (uint256) {
  31. return a - b;
  32. }
  33. /**
  34. * @dev Returns the multiplication of two unsigned integers, reverting on
  35. * overflow.
  36. *
  37. * Counterpart to Solidity's `*` operator.
  38. *
  39. * Requirements:
  40. *
  41. * - Multiplication cannot overflow.
  42. */
  43. function mul(uint256 a, uint256 b) internal pure returns (uint256) {
  44. return a * b;
  45. }
  46. /**
  47. * @dev Returns the integer division of two unsigned integers. Reverts on
  48. * division by zero. The result is rounded towards zero.
  49. *
  50. * Counterpart to Solidity's `/` operator.
  51. *
  52. * Requirements:
  53. *
  54. * - The divisor cannot be zero.
  55. */
  56. function div(uint256 a, uint256 b) internal pure returns (uint256) {
  57. return a / b;
  58. }
  59. /**
  60. * @dev Returns the remainder of dividing two unsigned integers. (unsigned integer modulo),
  61. * Reverts when dividing by zero.
  62. *
  63. * Counterpart to Solidity's `%` operator.
  64. *
  65. * Requirements:
  66. *
  67. * - The divisor cannot be zero.
  68. */
  69. function mod(uint256 a, uint256 b) internal pure returns (uint256) {
  70. return a % b;
  71. }
  72. }