SafeCast.sol 7.6 KB

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