SafeCast.sol 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. pragma solidity ^0.5.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. * _Available since v2.5.0._
  18. */
  19. library SafeCast {
  20. /**
  21. * @dev Returns the downcasted uint128 from uint256, reverting on
  22. * overflow (when the input is greater than largest uint128).
  23. *
  24. * Counterpart to Solidity's `uint128` operator.
  25. *
  26. * Requirements:
  27. *
  28. * - input must fit into 128 bits
  29. */
  30. function toUint128(uint256 value) internal pure returns (uint128) {
  31. require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits");
  32. return uint128(value);
  33. }
  34. /**
  35. * @dev Returns the downcasted uint64 from uint256, reverting on
  36. * overflow (when the input is greater than largest uint64).
  37. *
  38. * Counterpart to Solidity's `uint64` operator.
  39. *
  40. * Requirements:
  41. *
  42. * - input must fit into 64 bits
  43. */
  44. function toUint64(uint256 value) internal pure returns (uint64) {
  45. require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits");
  46. return uint64(value);
  47. }
  48. /**
  49. * @dev Returns the downcasted uint32 from uint256, reverting on
  50. * overflow (when the input is greater than largest uint32).
  51. *
  52. * Counterpart to Solidity's `uint32` operator.
  53. *
  54. * Requirements:
  55. *
  56. * - input must fit into 32 bits
  57. */
  58. function toUint32(uint256 value) internal pure returns (uint32) {
  59. require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits");
  60. return uint32(value);
  61. }
  62. /**
  63. * @dev Returns the downcasted uint16 from uint256, reverting on
  64. * overflow (when the input is greater than largest uint16).
  65. *
  66. * Counterpart to Solidity's `uint16` operator.
  67. *
  68. * Requirements:
  69. *
  70. * - input must fit into 16 bits
  71. */
  72. function toUint16(uint256 value) internal pure returns (uint16) {
  73. require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits");
  74. return uint16(value);
  75. }
  76. /**
  77. * @dev Returns the downcasted uint8 from uint256, reverting on
  78. * overflow (when the input is greater than largest uint8).
  79. *
  80. * Counterpart to Solidity's `uint8` operator.
  81. *
  82. * Requirements:
  83. *
  84. * - input must fit into 8 bits.
  85. */
  86. function toUint8(uint256 value) internal pure returns (uint8) {
  87. require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits");
  88. return uint8(value);
  89. }
  90. }