SignedSafeMath.sol 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.0;
  3. /**
  4. * @title SignedSafeMath
  5. * @dev Signed math operations that revert on error.
  6. */
  7. library SignedSafeMath {
  8. /**
  9. * @dev Returns the multiplication of two signed integers, reverting on
  10. * overflow.
  11. *
  12. * Counterpart to Solidity's `*` operator.
  13. *
  14. * Requirements:
  15. *
  16. * - Multiplication cannot overflow.
  17. */
  18. function mul(int256 a, int256 b) internal pure returns (int256) {
  19. return a * b;
  20. }
  21. /**
  22. * @dev Returns the integer division of two signed integers. Reverts on
  23. * division by zero. The result is rounded towards zero.
  24. *
  25. * Counterpart to Solidity's `/` operator.
  26. *
  27. * Requirements:
  28. *
  29. * - The divisor cannot be zero.
  30. */
  31. function div(int256 a, int256 b) internal pure returns (int256) {
  32. return a / b;
  33. }
  34. /**
  35. * @dev Returns the subtraction of two signed integers, reverting on
  36. * overflow.
  37. *
  38. * Counterpart to Solidity's `-` operator.
  39. *
  40. * Requirements:
  41. *
  42. * - Subtraction cannot overflow.
  43. */
  44. function sub(int256 a, int256 b) internal pure returns (int256) {
  45. return a - b;
  46. }
  47. /**
  48. * @dev Returns the addition of two signed integers, reverting on
  49. * overflow.
  50. *
  51. * Counterpart to Solidity's `+` operator.
  52. *
  53. * Requirements:
  54. *
  55. * - Addition cannot overflow.
  56. */
  57. function add(int256 a, int256 b) internal pure returns (int256) {
  58. return a + b;
  59. }
  60. }