SafeCast.sol 3.6 KB

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