SafeCast.sol 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.6.0;
  3. /**
  4. * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
  5. * checks.
  6. *
  7. * Downcasting from uint256/int256 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} and {SignedSafeMath} to extend it to smaller types, by performing
  16. * all math on `uint256` and `int256` 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 Returns the downcasted int128 from int256, reverting on
  102. * overflow (when the input is less than smallest int128 or
  103. * greater than largest int128).
  104. *
  105. * Counterpart to Solidity's `int128` operator.
  106. *
  107. * Requirements:
  108. *
  109. * - input must fit into 128 bits
  110. */
  111. function toInt128(int256 value) internal pure returns (int128) {
  112. require(value >= -2**127 && value < 2**127, "SafeCast: value doesn\'t fit in 128 bits");
  113. return int128(value);
  114. }
  115. /**
  116. * @dev Returns the downcasted int64 from int256, reverting on
  117. * overflow (when the input is less than smallest int64 or
  118. * greater than largest int64).
  119. *
  120. * Counterpart to Solidity's `int64` operator.
  121. *
  122. * Requirements:
  123. *
  124. * - input must fit into 64 bits
  125. */
  126. function toInt64(int256 value) internal pure returns (int64) {
  127. require(value >= -2**63 && value < 2**63, "SafeCast: value doesn\'t fit in 64 bits");
  128. return int64(value);
  129. }
  130. /**
  131. * @dev Returns the downcasted int32 from int256, reverting on
  132. * overflow (when the input is less than smallest int32 or
  133. * greater than largest int32).
  134. *
  135. * Counterpart to Solidity's `int32` operator.
  136. *
  137. * Requirements:
  138. *
  139. * - input must fit into 32 bits
  140. */
  141. function toInt32(int256 value) internal pure returns (int32) {
  142. require(value >= -2**31 && value < 2**31, "SafeCast: value doesn\'t fit in 32 bits");
  143. return int32(value);
  144. }
  145. /**
  146. * @dev Returns the downcasted int16 from int256, reverting on
  147. * overflow (when the input is less than smallest int16 or
  148. * greater than largest int16).
  149. *
  150. * Counterpart to Solidity's `int16` operator.
  151. *
  152. * Requirements:
  153. *
  154. * - input must fit into 16 bits
  155. */
  156. function toInt16(int256 value) internal pure returns (int16) {
  157. require(value >= -2**15 && value < 2**15, "SafeCast: value doesn\'t fit in 16 bits");
  158. return int16(value);
  159. }
  160. /**
  161. * @dev Returns the downcasted int8 from int256, reverting on
  162. * overflow (when the input is less than smallest int8 or
  163. * greater than largest int8).
  164. *
  165. * Counterpart to Solidity's `int8` operator.
  166. *
  167. * Requirements:
  168. *
  169. * - input must fit into 8 bits.
  170. */
  171. function toInt8(int256 value) internal pure returns (int8) {
  172. require(value >= -2**7 && value < 2**7, "SafeCast: value doesn\'t fit in 8 bits");
  173. return int8(value);
  174. }
  175. /**
  176. * @dev Converts an unsigned uint256 into a signed int256.
  177. *
  178. * Requirements:
  179. *
  180. * - input must be less than or equal to maxInt256.
  181. */
  182. function toInt256(uint256 value) internal pure returns (int256) {
  183. require(value < 2**255, "SafeCast: value doesn't fit in an int256");
  184. return int256(value);
  185. }
  186. }