SafeCast.sol 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. /**
  4. * @dev Wrappers over Solidity's uintXX casting operators with added overflow
  5. * checks.
  6. *
  7. * Downcasting from uint256 in Solidity does not revert on overflow. This can
  8. * easily result in undesired exploitation or bugs, since developers usually
  9. * assume that overflows raise errors. `SafeCast` restores this intuition by
  10. * reverting the transaction when such an operation overflows.
  11. *
  12. * Using this library instead of the unchecked operations eliminates an entire
  13. * class of bugs, so it's recommended to use it always.
  14. *
  15. * Can be combined with {SafeMath} to extend it to smaller types, by performing
  16. * all math on `uint256` and then downcasting.
  17. */
  18. library SafeCast {
  19. /**
  20. * @dev Returns the downcasted uint128 from uint256, reverting on
  21. * overflow (when the input is greater than largest uint128).
  22. *
  23. * Counterpart to Solidity's `uint128` operator.
  24. *
  25. * Requirements:
  26. *
  27. * - input must fit into 128 bits
  28. */
  29. function toUint128(uint256 value) internal pure returns (uint128) {
  30. require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits");
  31. return uint128(value);
  32. }
  33. /**
  34. * @dev Returns the downcasted uint64 from uint256, reverting on
  35. * overflow (when the input is greater than largest uint64).
  36. *
  37. * Counterpart to Solidity's `uint64` operator.
  38. *
  39. * Requirements:
  40. *
  41. * - input must fit into 64 bits
  42. */
  43. function toUint64(uint256 value) internal pure returns (uint64) {
  44. require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits");
  45. return uint64(value);
  46. }
  47. /**
  48. * @dev Returns the downcasted uint32 from uint256, reverting on
  49. * overflow (when the input is greater than largest uint32).
  50. *
  51. * Counterpart to Solidity's `uint32` operator.
  52. *
  53. * Requirements:
  54. *
  55. * - input must fit into 32 bits
  56. */
  57. function toUint32(uint256 value) internal pure returns (uint32) {
  58. require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits");
  59. return uint32(value);
  60. }
  61. /**
  62. * @dev Returns the downcasted uint16 from uint256, reverting on
  63. * overflow (when the input is greater than largest uint16).
  64. *
  65. * Counterpart to Solidity's `uint16` operator.
  66. *
  67. * Requirements:
  68. *
  69. * - input must fit into 16 bits
  70. */
  71. function toUint16(uint256 value) internal pure returns (uint16) {
  72. require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits");
  73. return uint16(value);
  74. }
  75. /**
  76. * @dev Returns the downcasted uint8 from uint256, reverting on
  77. * overflow (when the input is greater than largest uint8).
  78. *
  79. * Counterpart to Solidity's `uint8` operator.
  80. *
  81. * Requirements:
  82. *
  83. * - input must fit into 8 bits.
  84. */
  85. function toUint8(uint256 value) internal pure returns (uint8) {
  86. require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits");
  87. return uint8(value);
  88. }
  89. /**
  90. * @dev Converts a signed int256 into an unsigned uint256.
  91. *
  92. * Requirements:
  93. *
  94. * - input must be greater than or equal to 0.
  95. */
  96. function toUint256(int256 value) internal pure returns (uint256) {
  97. require(value >= 0, "SafeCast: value must be positive");
  98. return uint256(value);
  99. }
  100. /**
  101. * @dev Converts an unsigned uint256 into a signed int256.
  102. *
  103. * Requirements:
  104. *
  105. * - input must be less than or equal to maxInt256.
  106. */
  107. function toInt256(uint256 value) internal pure returns (int256) {
  108. require(value < 2**255, "SafeCast: value doesn't fit in an int256");
  109. return int256(value);
  110. }
  111. }