SafeCast.sol 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.8.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 uint224 from uint256, reverting on
  21. * overflow (when the input is greater than largest uint224).
  22. *
  23. * Counterpart to Solidity's `uint224` operator.
  24. *
  25. * Requirements:
  26. *
  27. * - input must fit into 224 bits
  28. */
  29. function toUint224(uint256 value) internal pure returns (uint224) {
  30. require(value < 2**224, "SafeCast: value doesn\'t fit in 224 bits");
  31. return uint224(value);
  32. }
  33. /**
  34. * @dev Returns the downcasted uint128 from uint256, reverting on
  35. * overflow (when the input is greater than largest uint128).
  36. *
  37. * Counterpart to Solidity's `uint128` operator.
  38. *
  39. * Requirements:
  40. *
  41. * - input must fit into 128 bits
  42. */
  43. function toUint128(uint256 value) internal pure returns (uint128) {
  44. require(value < 2**128, "SafeCast: value doesn\'t fit in 128 bits");
  45. return uint128(value);
  46. }
  47. /**
  48. * @dev Returns the downcasted uint64 from uint256, reverting on
  49. * overflow (when the input is greater than largest uint64).
  50. *
  51. * Counterpart to Solidity's `uint64` operator.
  52. *
  53. * Requirements:
  54. *
  55. * - input must fit into 64 bits
  56. */
  57. function toUint64(uint256 value) internal pure returns (uint64) {
  58. require(value < 2**64, "SafeCast: value doesn\'t fit in 64 bits");
  59. return uint64(value);
  60. }
  61. /**
  62. * @dev Returns the downcasted uint32 from uint256, reverting on
  63. * overflow (when the input is greater than largest uint32).
  64. *
  65. * Counterpart to Solidity's `uint32` operator.
  66. *
  67. * Requirements:
  68. *
  69. * - input must fit into 32 bits
  70. */
  71. function toUint32(uint256 value) internal pure returns (uint32) {
  72. require(value < 2**32, "SafeCast: value doesn\'t fit in 32 bits");
  73. return uint32(value);
  74. }
  75. /**
  76. * @dev Returns the downcasted uint16 from uint256, reverting on
  77. * overflow (when the input is greater than largest uint16).
  78. *
  79. * Counterpart to Solidity's `uint16` operator.
  80. *
  81. * Requirements:
  82. *
  83. * - input must fit into 16 bits
  84. */
  85. function toUint16(uint256 value) internal pure returns (uint16) {
  86. require(value < 2**16, "SafeCast: value doesn\'t fit in 16 bits");
  87. return uint16(value);
  88. }
  89. /**
  90. * @dev Returns the downcasted uint8 from uint256, reverting on
  91. * overflow (when the input is greater than largest uint8).
  92. *
  93. * Counterpart to Solidity's `uint8` operator.
  94. *
  95. * Requirements:
  96. *
  97. * - input must fit into 8 bits.
  98. */
  99. function toUint8(uint256 value) internal pure returns (uint8) {
  100. require(value < 2**8, "SafeCast: value doesn\'t fit in 8 bits");
  101. return uint8(value);
  102. }
  103. /**
  104. * @dev Converts a signed int256 into an unsigned uint256.
  105. *
  106. * Requirements:
  107. *
  108. * - input must be greater than or equal to 0.
  109. */
  110. function toUint256(int256 value) internal pure returns (uint256) {
  111. require(value >= 0, "SafeCast: value must be positive");
  112. return uint256(value);
  113. }
  114. /**
  115. * @dev Returns the downcasted int128 from int256, reverting on
  116. * overflow (when the input is less than smallest int128 or
  117. * greater than largest int128).
  118. *
  119. * Counterpart to Solidity's `int128` operator.
  120. *
  121. * Requirements:
  122. *
  123. * - input must fit into 128 bits
  124. *
  125. * _Available since v3.1._
  126. */
  127. function toInt128(int256 value) internal pure returns (int128) {
  128. require(value >= -2**127 && value < 2**127, "SafeCast: value doesn\'t fit in 128 bits");
  129. return int128(value);
  130. }
  131. /**
  132. * @dev Returns the downcasted int64 from int256, reverting on
  133. * overflow (when the input is less than smallest int64 or
  134. * greater than largest int64).
  135. *
  136. * Counterpart to Solidity's `int64` operator.
  137. *
  138. * Requirements:
  139. *
  140. * - input must fit into 64 bits
  141. *
  142. * _Available since v3.1._
  143. */
  144. function toInt64(int256 value) internal pure returns (int64) {
  145. require(value >= -2**63 && value < 2**63, "SafeCast: value doesn\'t fit in 64 bits");
  146. return int64(value);
  147. }
  148. /**
  149. * @dev Returns the downcasted int32 from int256, reverting on
  150. * overflow (when the input is less than smallest int32 or
  151. * greater than largest int32).
  152. *
  153. * Counterpart to Solidity's `int32` operator.
  154. *
  155. * Requirements:
  156. *
  157. * - input must fit into 32 bits
  158. *
  159. * _Available since v3.1._
  160. */
  161. function toInt32(int256 value) internal pure returns (int32) {
  162. require(value >= -2**31 && value < 2**31, "SafeCast: value doesn\'t fit in 32 bits");
  163. return int32(value);
  164. }
  165. /**
  166. * @dev Returns the downcasted int16 from int256, reverting on
  167. * overflow (when the input is less than smallest int16 or
  168. * greater than largest int16).
  169. *
  170. * Counterpart to Solidity's `int16` operator.
  171. *
  172. * Requirements:
  173. *
  174. * - input must fit into 16 bits
  175. *
  176. * _Available since v3.1._
  177. */
  178. function toInt16(int256 value) internal pure returns (int16) {
  179. require(value >= -2**15 && value < 2**15, "SafeCast: value doesn\'t fit in 16 bits");
  180. return int16(value);
  181. }
  182. /**
  183. * @dev Returns the downcasted int8 from int256, reverting on
  184. * overflow (when the input is less than smallest int8 or
  185. * greater than largest int8).
  186. *
  187. * Counterpart to Solidity's `int8` operator.
  188. *
  189. * Requirements:
  190. *
  191. * - input must fit into 8 bits.
  192. *
  193. * _Available since v3.1._
  194. */
  195. function toInt8(int256 value) internal pure returns (int8) {
  196. require(value >= -2**7 && value < 2**7, "SafeCast: value doesn\'t fit in 8 bits");
  197. return int8(value);
  198. }
  199. /**
  200. * @dev Converts an unsigned uint256 into a signed int256.
  201. *
  202. * Requirements:
  203. *
  204. * - input must be less than or equal to maxInt256.
  205. */
  206. function toInt256(uint256 value) internal pure returns (int256) {
  207. require(value < 2**255, "SafeCast: value doesn't fit in an int256");
  208. return int256(value);
  209. }
  210. }