-
Notifications
You must be signed in to change notification settings - Fork 10
Expand file tree
/
Copy pathAsCast.sol
More file actions
275 lines (250 loc) · 9.89 KB
/
AsCast.sol
File metadata and controls
275 lines (250 loc) · 9.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// SPDX-License-Identifier: BUSL-1.1
pragma solidity 0.8.25;
/**
* _ _ _
* __ _ ___| |_ _ __ ___ | | __ _| |__
* / ` / __| _| '__/ \| |/ ` | ' \
* | O \__ \ |_| | | O | | O | O |
* \__,_|___/.__|_| \___/|_|\__,_|_.__/ ©️ 2024
*
* @title AsCast Library - Astrolab's type casting library
* @author Astrolab DAO
*/
library AsCast {
/*═══════════════════════════════════════════════════════════════╗
║ ERRORS ║
╚═══════════════════════════════════════════════════════════════*/
error ValueOutOfCastRange();
/*═══════════════════════════════════════════════════════════════╗
║ LOGIC ║
╚═══════════════════════════════════════════════════════════════*/
/**
* @dev Returns the downcasted int8 from int256, reverting on overflow
* @param value int256 to be downcasted
* @return downcasted Downcasted int8 value
* Counterpart to Solidity's `int8` operator
*/
function toInt8(int256 value) internal pure returns (int8 downcasted) {
downcasted = int8(value);
if (downcasted != value) {
revert ValueOutOfCastRange();
}
}
/**
* @dev Returns the downcasted int16 from int256, reverting on overflow
* @param value int256 to be downcasted
* @return downcasted Downcasted int16 value
* Counterpart to Solidity's `int16` operator
*/
function toInt16(int256 value) internal pure returns (int16 downcasted) {
downcasted = int16(value);
if (downcasted != value) {
revert ValueOutOfCastRange();
}
}
/**
* @dev Returns the downcasted int32 from int256, reverting on overflow
* @param value int256 to be downcasted
* @return downcasted Downcasted int32 value
* Counterpart to Solidity's `int32` operator
*/
function toInt32(int256 value) internal pure returns (int32 downcasted) {
downcasted = int32(value);
if (downcasted != value) {
revert ValueOutOfCastRange();
}
}
/**
* @dev Returns the downcasted int64 from int256, reverting on overflow
* @param value int256 to be downcasted
* @return downcasted Downcasted int64 value
* Counterpart to Solidity's `int64` operator
*/
function toInt64(int256 value) internal pure returns (int64 downcasted) {
downcasted = int64(value);
if (downcasted != value) {
revert ValueOutOfCastRange();
}
}
/**
* @dev Returns the downcasted int128 from int256, reverting on overflow
* @param value int256 to be downcasted
* @return downcasted Downcasted int128 value
* Counterpart to Solidity's `int128` operator
*/
function toInt128(int256 value) internal pure returns (int128 downcasted) {
downcasted = int128(value);
if (downcasted != value) {
revert ValueOutOfCastRange();
}
}
/**
* @dev Returns the downcasted int192 from int256, reverting on overflow
* @param value int256 to be downcasted
* @return downcasted Downcasted int192 value
* Counterpart to Solidity's `int192` operator (not directly supported)
*/
function toInt192(int256 value) internal pure returns (int192 downcasted) {
downcasted = int192(value);
if (downcasted != value) {
revert ValueOutOfCastRange();
}
}
/**
* @dev Returns the downcasted int224 from int256, reverting on overflow
* @param value int256 to be downcasted
* @return downcasted Downcasted int224 value
* Counterpart to Solidity's `int224` operator (not directly supported)
*/
function toInt224(int256 value) internal pure returns (int224 downcasted) {
downcasted = int224(value);
if (downcasted != value) {
revert ValueOutOfCastRange();
}
}
/**
* @dev Returns the downcasted int256 from uint256, reverting on overflow
* @param value Uint256 to be downcasted
* @return downcasted Downcasted int256 value
* Counterpart to Solidity's `int256` operator
*/
function toInt256(uint256 value) internal pure returns (int256 downcasted) {
downcasted = int256(value);
if (downcasted < 0) {
revert ValueOutOfCastRange();
}
}
/**
* @notice Converts an unsigned integer to an 8-bit unsigned integer
* @dev Requires the input to be within the valid range for an 8-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as an 8-bit unsigned integer
*/
function toUint8(uint256 x) internal pure returns (uint8) {
if (x > type(uint8).max) revert ValueOutOfCastRange();
return uint8(x);
}
/**
* @notice Converts an unsigned integer to a 16-bit unsigned integer
* @dev Requires the input to be within the valid range for a 16-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 16-bit unsigned integer
*/
function toUint16(uint256 x) internal pure returns (uint16) {
if (x > type(uint16).max) revert ValueOutOfCastRange();
return uint16(x);
}
/**
* @notice Converts an unsigned integer to a 32-bit unsigned integer
* @dev Requires the input to be within the valid range for a 32-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 32-bit unsigned integer
*/
function toUint32(uint256 x) internal pure returns (uint32) {
if (x > type(uint32).max) revert ValueOutOfCastRange();
return uint32(x);
}
/**
* @notice Converts an unsigned integer to a 64-bit unsigned integer
* @dev Requires the input to be within the valid range for a 64-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 64-bit unsigned integer
*/
function toUint64(uint256 x) internal pure returns (uint64) {
if (x > type(uint64).max) revert ValueOutOfCastRange();
return uint64(x);
}
/**
* @notice Converts an unsigned integer to a 96-bit unsigned integer
* @dev Requires the input to be within the valid range for a 96-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 96-bit unsigned integer
*/
function toUint96(uint256 x) internal pure returns (uint96) {
if (x > type(uint96).max) revert ValueOutOfCastRange();
return uint96(x);
}
/**
* @notice Converts an unsigned integer to a 128-bit unsigned integer
* @dev Requires the input to be within the valid range for a 128-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 128-bit unsigned integer
*/
function toUint128(uint256 x) internal pure returns (uint128) {
if (x > type(uint128).max) revert ValueOutOfCastRange();
return uint128(x);
}
/**
* @notice Converts an unsigned integer to a 160-bit unsigned integer
* @dev Requires the input to be within the valid range for a 160-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 160-bit unsigned integer
*/
function toUint160(uint256 x) internal pure returns (uint160) {
if (x > type(uint160).max) revert ValueOutOfCastRange();
return uint160(x);
}
/**
* @notice Converts an unsigned integer to a 192-bit unsigned integer
* @dev Requires the input to be within the valid range for a 192-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 192-bit unsigned integer
*/
function toUint192(uint256 x) internal pure returns (uint192) {
if (x > type(uint192).max) revert ValueOutOfCastRange();
return uint192(x);
}
/**
* @notice Converts an unsigned integer to a 224-bit unsigned integer
* @dev Requires the input to be within the valid range for a 224-bit unsigned integer
* @param x Input unsigned integer
* @return Input value as a 224-bit unsigned integer
*/
function toUint224(uint256 x) internal pure returns (uint224) {
if (x > type(uint224).max) revert ValueOutOfCastRange();
return uint224(x);
}
/**
* @notice Converts an unsigned integer to a 256-bit unsigned integer
* @dev Requires the input to be within the valid range for a 256-bit unsigned integer
* @param x Input integer
* @return Input value as a 256-bit unsigned integer
*/
function toUint256(int256 x) internal pure returns (uint256) {
if (x < 0) revert ValueOutOfCastRange();
return uint256(x);
}
/**
* @dev Converts an address to bytes32
* @param addr Address to be converted
* @return Bytes32 representation of the address
*/
function toBytes32(address addr) internal pure returns (bytes32) {
return bytes32(uint256(uint160(addr)));
}
/**
* @dev Converts a bytes32 value to an address
* @param b Bytes32 value to convert
* @return Converted address
*/
function toAddress(bytes32 b) internal pure returns (address) {
return address(toUint160(uint256(b)));
}
/**
* @notice Encodes two addresses into a single bytes32 value
* @dev Not collision safe, but almost (1e-16% for 1m hashes birthday paradox collision rate)
* @param a First address to encode
* @param b Second address to encode
* @return c Resulting bytes32 value, containing the encoded addresses
*/
function hashFast(address a, address b) internal pure returns (bytes32 c) {
assembly {
// load the 20-byte representations of the addresses
let baseBytes := and(a, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
let quoteBytes := and(b, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF)
// shift the first address 12 bytes (96 bits) to the left
// then combine with the second address
c := or(shl(96, baseBytes), quoteBytes)
}
}
}