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