SafeCast.sol 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. // SPDX-License-Identifier: MIT
  2. pragma solidity ^0.7.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. * _Available since v3.1._
  112. */
  113. function toInt128(int256 value) internal pure returns (int128) {
  114. require(value >= -2**127 && value < 2**127, "SafeCast: value doesn\'t fit in 128 bits");
  115. return int128(value);
  116. }
  117. /**
  118. * @dev Returns the downcasted int64 from int256, reverting on
  119. * overflow (when the input is less than smallest int64 or
  120. * greater than largest int64).
  121. *
  122. * Counterpart to Solidity's `int64` operator.
  123. *
  124. * Requirements:
  125. *
  126. * - input must fit into 64 bits
  127. *
  128. * _Available since v3.1._
  129. */
  130. function toInt64(int256 value) internal pure returns (int64) {
  131. require(value >= -2**63 && value < 2**63, "SafeCast: value doesn\'t fit in 64 bits");
  132. return int64(value);
  133. }
  134. /**
  135. * @dev Returns the downcasted int32 from int256, reverting on
  136. * overflow (when the input is less than smallest int32 or
  137. * greater than largest int32).
  138. *
  139. * Counterpart to Solidity's `int32` operator.
  140. *
  141. * Requirements:
  142. *
  143. * - input must fit into 32 bits
  144. *
  145. * _Available since v3.1._
  146. */
  147. function toInt32(int256 value) internal pure returns (int32) {
  148. require(value >= -2**31 && value < 2**31, "SafeCast: value doesn\'t fit in 32 bits");
  149. return int32(value);
  150. }
  151. /**
  152. * @dev Returns the downcasted int16 from int256, reverting on
  153. * overflow (when the input is less than smallest int16 or
  154. * greater than largest int16).
  155. *
  156. * Counterpart to Solidity's `int16` operator.
  157. *
  158. * Requirements:
  159. *
  160. * - input must fit into 16 bits
  161. *
  162. * _Available since v3.1._
  163. */
  164. function toInt16(int256 value) internal pure returns (int16) {
  165. require(value >= -2**15 && value < 2**15, "SafeCast: value doesn\'t fit in 16 bits");
  166. return int16(value);
  167. }
  168. /**
  169. * @dev Returns the downcasted int8 from int256, reverting on
  170. * overflow (when the input is less than smallest int8 or
  171. * greater than largest int8).
  172. *
  173. * Counterpart to Solidity's `int8` operator.
  174. *
  175. * Requirements:
  176. *
  177. * - input must fit into 8 bits.
  178. *
  179. * _Available since v3.1._
  180. */
  181. function toInt8(int256 value) internal pure returns (int8) {
  182. require(value >= -2**7 && value < 2**7, "SafeCast: value doesn\'t fit in 8 bits");
  183. return int8(value);
  184. }
  185. /**
  186. * @dev Converts an unsigned uint256 into a signed int256.
  187. *
  188. * Requirements:
  189. *
  190. * - input must be less than or equal to maxInt256.
  191. */
  192. function toInt256(uint256 value) internal pure returns (int256) {
  193. require(value < 2**255, "SafeCast: value doesn't fit in an int256");
  194. return int256(value);
  195. }
  196. }