SafeCast.sol 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. // SPDX-License-Identifier: MIT
  2. // OpenZeppelin Contracts v4.4.1 (utils/math/SafeCast.sol)
  3. pragma solidity ^0.8.0;
  4. /**
  5. * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
  6. * checks.
  7. *
  8. * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
  9. * easily result in undesired exploitation or bugs, since developers usually
  10. * assume that overflows raise errors. `SafeCast` restores this intuition by
  11. * reverting the transaction when such an operation overflows.
  12. *
  13. * Using this library instead of the unchecked operations eliminates an entire
  14. * class of bugs, so it's recommended to use it always.
  15. *
  16. * Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
  17. * all math on `uint256` and `int256` and then downcasting.
  18. */
  19. library SafeCast {
  20. /**
  21. * @dev Returns the downcasted uint224 from uint256, reverting on
  22. * overflow (when the input is greater than largest uint224).
  23. *
  24. * Counterpart to Solidity's `uint224` operator.
  25. *
  26. * Requirements:
  27. *
  28. * - input must fit into 224 bits
  29. */
  30. function toUint224(uint256 value) internal pure returns (uint224) {
  31. require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
  32. return uint224(value);
  33. }
  34. /**
  35. * @dev Returns the downcasted uint128 from uint256, reverting on
  36. * overflow (when the input is greater than largest uint128).
  37. *
  38. * Counterpart to Solidity's `uint128` operator.
  39. *
  40. * Requirements:
  41. *
  42. * - input must fit into 128 bits
  43. */
  44. function toUint128(uint256 value) internal pure returns (uint128) {
  45. require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
  46. return uint128(value);
  47. }
  48. /**
  49. * @dev Returns the downcasted uint96 from uint256, reverting on
  50. * overflow (when the input is greater than largest uint96).
  51. *
  52. * Counterpart to Solidity's `uint96` operator.
  53. *
  54. * Requirements:
  55. *
  56. * - input must fit into 96 bits
  57. */
  58. function toUint96(uint256 value) internal pure returns (uint96) {
  59. require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
  60. return uint96(value);
  61. }
  62. /**
  63. * @dev Returns the downcasted uint64 from uint256, reverting on
  64. * overflow (when the input is greater than largest uint64).
  65. *
  66. * Counterpart to Solidity's `uint64` operator.
  67. *
  68. * Requirements:
  69. *
  70. * - input must fit into 64 bits
  71. */
  72. function toUint64(uint256 value) internal pure returns (uint64) {
  73. require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
  74. return uint64(value);
  75. }
  76. /**
  77. * @dev Returns the downcasted uint32 from uint256, reverting on
  78. * overflow (when the input is greater than largest uint32).
  79. *
  80. * Counterpart to Solidity's `uint32` operator.
  81. *
  82. * Requirements:
  83. *
  84. * - input must fit into 32 bits
  85. */
  86. function toUint32(uint256 value) internal pure returns (uint32) {
  87. require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
  88. return uint32(value);
  89. }
  90. /**
  91. * @dev Returns the downcasted uint16 from uint256, reverting on
  92. * overflow (when the input is greater than largest uint16).
  93. *
  94. * Counterpart to Solidity's `uint16` operator.
  95. *
  96. * Requirements:
  97. *
  98. * - input must fit into 16 bits
  99. */
  100. function toUint16(uint256 value) internal pure returns (uint16) {
  101. require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
  102. return uint16(value);
  103. }
  104. /**
  105. * @dev Returns the downcasted uint8 from uint256, reverting on
  106. * overflow (when the input is greater than largest uint8).
  107. *
  108. * Counterpart to Solidity's `uint8` operator.
  109. *
  110. * Requirements:
  111. *
  112. * - input must fit into 8 bits.
  113. */
  114. function toUint8(uint256 value) internal pure returns (uint8) {
  115. require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
  116. return uint8(value);
  117. }
  118. /**
  119. * @dev Converts a signed int256 into an unsigned uint256.
  120. *
  121. * Requirements:
  122. *
  123. * - input must be greater than or equal to 0.
  124. */
  125. function toUint256(int256 value) internal pure returns (uint256) {
  126. require(value >= 0, "SafeCast: value must be positive");
  127. return uint256(value);
  128. }
  129. /**
  130. * @dev Returns the downcasted int128 from int256, reverting on
  131. * overflow (when the input is less than smallest int128 or
  132. * greater than largest int128).
  133. *
  134. * Counterpart to Solidity's `int128` operator.
  135. *
  136. * Requirements:
  137. *
  138. * - input must fit into 128 bits
  139. *
  140. * _Available since v3.1._
  141. */
  142. function toInt128(int256 value) internal pure returns (int128) {
  143. require(value >= type(int128).min && value <= type(int128).max, "SafeCast: value doesn't fit in 128 bits");
  144. return int128(value);
  145. }
  146. /**
  147. * @dev Returns the downcasted int64 from int256, reverting on
  148. * overflow (when the input is less than smallest int64 or
  149. * greater than largest int64).
  150. *
  151. * Counterpart to Solidity's `int64` operator.
  152. *
  153. * Requirements:
  154. *
  155. * - input must fit into 64 bits
  156. *
  157. * _Available since v3.1._
  158. */
  159. function toInt64(int256 value) internal pure returns (int64) {
  160. require(value >= type(int64).min && value <= type(int64).max, "SafeCast: value doesn't fit in 64 bits");
  161. return int64(value);
  162. }
  163. /**
  164. * @dev Returns the downcasted int32 from int256, reverting on
  165. * overflow (when the input is less than smallest int32 or
  166. * greater than largest int32).
  167. *
  168. * Counterpart to Solidity's `int32` operator.
  169. *
  170. * Requirements:
  171. *
  172. * - input must fit into 32 bits
  173. *
  174. * _Available since v3.1._
  175. */
  176. function toInt32(int256 value) internal pure returns (int32) {
  177. require(value >= type(int32).min && value <= type(int32).max, "SafeCast: value doesn't fit in 32 bits");
  178. return int32(value);
  179. }
  180. /**
  181. * @dev Returns the downcasted int16 from int256, reverting on
  182. * overflow (when the input is less than smallest int16 or
  183. * greater than largest int16).
  184. *
  185. * Counterpart to Solidity's `int16` operator.
  186. *
  187. * Requirements:
  188. *
  189. * - input must fit into 16 bits
  190. *
  191. * _Available since v3.1._
  192. */
  193. function toInt16(int256 value) internal pure returns (int16) {
  194. require(value >= type(int16).min && value <= type(int16).max, "SafeCast: value doesn't fit in 16 bits");
  195. return int16(value);
  196. }
  197. /**
  198. * @dev Returns the downcasted int8 from int256, reverting on
  199. * overflow (when the input is less than smallest int8 or
  200. * greater than largest int8).
  201. *
  202. * Counterpart to Solidity's `int8` operator.
  203. *
  204. * Requirements:
  205. *
  206. * - input must fit into 8 bits.
  207. *
  208. * _Available since v3.1._
  209. */
  210. function toInt8(int256 value) internal pure returns (int8) {
  211. require(value >= type(int8).min && value <= type(int8).max, "SafeCast: value doesn't fit in 8 bits");
  212. return int8(value);
  213. }
  214. /**
  215. * @dev Converts an unsigned uint256 into a signed int256.
  216. *
  217. * Requirements:
  218. *
  219. * - input must be less than or equal to maxInt256.
  220. */
  221. function toInt256(uint256 value) internal pure returns (int256) {
  222. // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
  223. require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
  224. return int256(value);
  225. }
  226. }