123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- // SPDX-License-Identifier: MIT
- pragma solidity ^0.8.0;
- /**
- * @dev Wrappers over Solidity's uintXX/intXX casting operators with added overflow
- * checks.
- *
- * Downcasting from uint256/int256 in Solidity does not revert on overflow. This can
- * easily result in undesired exploitation or bugs, since developers usually
- * assume that overflows raise errors. `SafeCast` restores this intuition by
- * reverting the transaction when such an operation overflows.
- *
- * Using this library instead of the unchecked operations eliminates an entire
- * class of bugs, so it's recommended to use it always.
- *
- * Can be combined with {SafeMath} and {SignedSafeMath} to extend it to smaller types, by performing
- * all math on `uint256` and `int256` and then downcasting.
- */
- library SafeCast {
- /**
- * @dev Returns the downcasted uint224 from uint256, reverting on
- * overflow (when the input is greater than largest uint224).
- *
- * Counterpart to Solidity's `uint224` operator.
- *
- * Requirements:
- *
- * - input must fit into 224 bits
- */
- function toUint224(uint256 value) internal pure returns (uint224) {
- require(value <= type(uint224).max, "SafeCast: value doesn't fit in 224 bits");
- return uint224(value);
- }
- /**
- * @dev Returns the downcasted uint128 from uint256, reverting on
- * overflow (when the input is greater than largest uint128).
- *
- * Counterpart to Solidity's `uint128` operator.
- *
- * Requirements:
- *
- * - input must fit into 128 bits
- */
- function toUint128(uint256 value) internal pure returns (uint128) {
- require(value <= type(uint128).max, "SafeCast: value doesn't fit in 128 bits");
- return uint128(value);
- }
- /**
- * @dev Returns the downcasted uint96 from uint256, reverting on
- * overflow (when the input is greater than largest uint96).
- *
- * Counterpart to Solidity's `uint96` operator.
- *
- * Requirements:
- *
- * - input must fit into 96 bits
- */
- function toUint96(uint256 value) internal pure returns (uint96) {
- require(value <= type(uint96).max, "SafeCast: value doesn't fit in 96 bits");
- return uint96(value);
- }
- /**
- * @dev Returns the downcasted uint64 from uint256, reverting on
- * overflow (when the input is greater than largest uint64).
- *
- * Counterpart to Solidity's `uint64` operator.
- *
- * Requirements:
- *
- * - input must fit into 64 bits
- */
- function toUint64(uint256 value) internal pure returns (uint64) {
- require(value <= type(uint64).max, "SafeCast: value doesn't fit in 64 bits");
- return uint64(value);
- }
- /**
- * @dev Returns the downcasted uint32 from uint256, reverting on
- * overflow (when the input is greater than largest uint32).
- *
- * Counterpart to Solidity's `uint32` operator.
- *
- * Requirements:
- *
- * - input must fit into 32 bits
- */
- function toUint32(uint256 value) internal pure returns (uint32) {
- require(value <= type(uint32).max, "SafeCast: value doesn't fit in 32 bits");
- return uint32(value);
- }
- /**
- * @dev Returns the downcasted uint16 from uint256, reverting on
- * overflow (when the input is greater than largest uint16).
- *
- * Counterpart to Solidity's `uint16` operator.
- *
- * Requirements:
- *
- * - input must fit into 16 bits
- */
- function toUint16(uint256 value) internal pure returns (uint16) {
- require(value <= type(uint16).max, "SafeCast: value doesn't fit in 16 bits");
- return uint16(value);
- }
- /**
- * @dev Returns the downcasted uint8 from uint256, reverting on
- * overflow (when the input is greater than largest uint8).
- *
- * Counterpart to Solidity's `uint8` operator.
- *
- * Requirements:
- *
- * - input must fit into 8 bits.
- */
- function toUint8(uint256 value) internal pure returns (uint8) {
- require(value <= type(uint8).max, "SafeCast: value doesn't fit in 8 bits");
- return uint8(value);
- }
- /**
- * @dev Converts a signed int256 into an unsigned uint256.
- *
- * Requirements:
- *
- * - input must be greater than or equal to 0.
- */
- function toUint256(int256 value) internal pure returns (uint256) {
- require(value >= 0, "SafeCast: value must be positive");
- return uint256(value);
- }
- /**
- * @dev Returns the downcasted int128 from int256, reverting on
- * overflow (when the input is less than smallest int128 or
- * greater than largest int128).
- *
- * Counterpart to Solidity's `int128` operator.
- *
- * Requirements:
- *
- * - input must fit into 128 bits
- *
- * _Available since v3.1._
- */
- function toInt128(int256 value) internal pure returns (int128) {
- require(value >= type(int128).min && value <= type(int128).max, "SafeCast: value doesn't fit in 128 bits");
- return int128(value);
- }
- /**
- * @dev Returns the downcasted int64 from int256, reverting on
- * overflow (when the input is less than smallest int64 or
- * greater than largest int64).
- *
- * Counterpart to Solidity's `int64` operator.
- *
- * Requirements:
- *
- * - input must fit into 64 bits
- *
- * _Available since v3.1._
- */
- function toInt64(int256 value) internal pure returns (int64) {
- require(value >= type(int64).min && value <= type(int64).max, "SafeCast: value doesn't fit in 64 bits");
- return int64(value);
- }
- /**
- * @dev Returns the downcasted int32 from int256, reverting on
- * overflow (when the input is less than smallest int32 or
- * greater than largest int32).
- *
- * Counterpart to Solidity's `int32` operator.
- *
- * Requirements:
- *
- * - input must fit into 32 bits
- *
- * _Available since v3.1._
- */
- function toInt32(int256 value) internal pure returns (int32) {
- require(value >= type(int32).min && value <= type(int32).max, "SafeCast: value doesn't fit in 32 bits");
- return int32(value);
- }
- /**
- * @dev Returns the downcasted int16 from int256, reverting on
- * overflow (when the input is less than smallest int16 or
- * greater than largest int16).
- *
- * Counterpart to Solidity's `int16` operator.
- *
- * Requirements:
- *
- * - input must fit into 16 bits
- *
- * _Available since v3.1._
- */
- function toInt16(int256 value) internal pure returns (int16) {
- require(value >= type(int16).min && value <= type(int16).max, "SafeCast: value doesn't fit in 16 bits");
- return int16(value);
- }
- /**
- * @dev Returns the downcasted int8 from int256, reverting on
- * overflow (when the input is less than smallest int8 or
- * greater than largest int8).
- *
- * Counterpart to Solidity's `int8` operator.
- *
- * Requirements:
- *
- * - input must fit into 8 bits.
- *
- * _Available since v3.1._
- */
- function toInt8(int256 value) internal pure returns (int8) {
- require(value >= type(int8).min && value <= type(int8).max, "SafeCast: value doesn't fit in 8 bits");
- return int8(value);
- }
- /**
- * @dev Converts an unsigned uint256 into a signed int256.
- *
- * Requirements:
- *
- * - input must be less than or equal to maxInt256.
- */
- function toInt256(uint256 value) internal pure returns (int256) {
- // Note: Unsafe cast below is okay because `type(int256).max` is guaranteed to be positive
- require(value <= uint256(type(int256).max), "SafeCast: value doesn't fit in an int256");
- return int256(value);
- }
- }
|