solidity-scale-codec 0.1.0 → 0.1.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (43) hide show
  1. package/README.md +3 -1
  2. package/package.json +5 -2
  3. package/src/LittleEndian/LittleEndianU128.sol +49 -0
  4. package/src/LittleEndian/LittleEndianU16.sol +29 -0
  5. package/src/LittleEndian/LittleEndianU256.sol +101 -0
  6. package/src/LittleEndian/LittleEndianU32.sol +42 -0
  7. package/src/LittleEndian/LittleEndianU64.sol +45 -0
  8. package/src/LittleEndian/LittleEndianU8.sol +26 -0
  9. package/src/Scale/Array/BoolArr.sol +49 -35
  10. package/src/Scale/Array/I128Arr.sol +49 -35
  11. package/src/Scale/Array/I16Arr.sol +49 -35
  12. package/src/Scale/Array/I256Arr.sol +49 -35
  13. package/src/Scale/Array/I32Arr.sol +49 -35
  14. package/src/Scale/Array/I64Arr.sol +49 -35
  15. package/src/Scale/Array/I8Arr.sol +49 -35
  16. package/src/Scale/Array/U128Arr.sol +49 -35
  17. package/src/Scale/Array/U16Arr.sol +49 -35
  18. package/src/Scale/Array/U256Arr.sol +49 -35
  19. package/src/Scale/Array/U32Arr.sol +49 -35
  20. package/src/Scale/Array/U64Arr.sol +49 -35
  21. package/src/Scale/Array/U8Arr.sol +49 -35
  22. package/src/Scale/Array.sol +14 -15
  23. package/src/Scale/Bool/Bool.sol +1 -1
  24. package/src/Scale/Bool.sol +1 -1
  25. package/src/Scale/Compact/Compact.sol +89 -460
  26. package/src/Scale/Compact.sol +1 -1
  27. package/src/Scale/Signed/I128.sol +13 -6
  28. package/src/Scale/Signed/I16.sol +10 -5
  29. package/src/Scale/Signed/I256.sol +13 -6
  30. package/src/Scale/Signed/I32.sol +10 -5
  31. package/src/Scale/Signed/I64.sol +10 -5
  32. package/src/Scale/Signed/I8.sol +10 -5
  33. package/src/Scale/Signed.sol +7 -8
  34. package/src/Scale/Unsigned/U128.sol +15 -9
  35. package/src/Scale/Unsigned/U16.sol +15 -9
  36. package/src/Scale/Unsigned/U256.sol +15 -9
  37. package/src/Scale/Unsigned/U32.sol +15 -9
  38. package/src/Scale/Unsigned/U64.sol +15 -9
  39. package/src/Scale/Unsigned/U8.sol +13 -9
  40. package/src/Scale/Unsigned.sol +7 -8
  41. package/src/Scale/Compact/Compact.t.sol +0 -326
  42. package/src/Utils/LittleEndian/LittleEndian.sol +0 -354
  43. package/src/Utils/LittleEndian/LittleEndian.t.sol +0 -542
@@ -1,326 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
3
-
4
- import {Compact} from "./Compact.sol";
5
- import {Test} from "forge-std/Test.sol";
6
-
7
- /// @dev Wrapper contract to test reverts (vm.expectRevert needs external calls)
8
- contract CompactWrapper {
9
- function decode(
10
- bytes memory data
11
- ) external pure returns (uint256, uint256) {
12
- return Compact.decode(data);
13
- }
14
-
15
- function decodeAt(
16
- bytes memory data,
17
- uint256 offset
18
- ) external pure returns (uint256, uint256) {
19
- return Compact.decodeAt(data, offset);
20
- }
21
- }
22
-
23
- contract CompactTest is Test {
24
- CompactWrapper wrapper;
25
-
26
- function setUp() public {
27
- wrapper = new CompactWrapper();
28
- }
29
-
30
- // ============ Single-byte mode (0-63) ============
31
-
32
- function test_Encode_0() public pure {
33
- assertEq(Compact.encode(0), hex"00");
34
- }
35
-
36
- function test_Encode_1() public pure {
37
- assertEq(Compact.encode(1), hex"04");
38
- }
39
-
40
- function test_Encode_2() public pure {
41
- assertEq(Compact.encode(2), hex"08");
42
- }
43
-
44
- function test_Encode_3() public pure {
45
- assertEq(Compact.encode(3), hex"0c");
46
- }
47
-
48
- function test_Encode_63() public pure {
49
- assertEq(Compact.encode(63), hex"fc");
50
- }
51
-
52
- // ============ Two-byte mode (64-16383) ============
53
-
54
- function test_Encode_64() public pure {
55
- assertEq(Compact.encode(64), hex"0101");
56
- }
57
-
58
- function test_Encode_65() public pure {
59
- assertEq(Compact.encode(65), hex"0501");
60
- }
61
-
62
- function test_Encode_16383() public pure {
63
- assertEq(Compact.encode(16383), hex"fdff");
64
- }
65
-
66
- // ============ Four-byte mode (16384-1073741823) ============
67
-
68
- function test_Encode_16384() public pure {
69
- assertEq(Compact.encode(16384), hex"02000100");
70
- }
71
-
72
- function test_Encode_1073741823() public pure {
73
- assertEq(Compact.encode(1073741823), hex"feffffff");
74
- }
75
-
76
- // ============ Big-integer mode (>1073741823) ============
77
-
78
- function test_Encode_1073741824() public pure {
79
- assertEq(Compact.encode(1073741824), hex"0300000040");
80
- }
81
-
82
- function test_Encode_MaxUint32() public pure {
83
- assertEq(Compact.encode(type(uint32).max), hex"03ffffffff");
84
- }
85
-
86
- function test_Encode_MaxUint64() public pure {
87
- bytes memory encoded = Compact.encode(type(uint64).max);
88
- assertEq(encoded[0], bytes1(0x13));
89
- assertEq(encoded.length, 9);
90
- }
91
-
92
- function test_Encode_BigInt_100000000000000() public pure {
93
- assertEq(Compact.encode(100000000000000), hex"0b00407a10f35a");
94
- }
95
-
96
- // ============ Decode single-byte ============
97
-
98
- function test_Decode_0() public pure {
99
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"00");
100
- assertEq(value, 0);
101
- assertEq(bytesRead, 1);
102
- }
103
-
104
- function test_Decode_1() public pure {
105
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"04");
106
- assertEq(value, 1);
107
- assertEq(bytesRead, 1);
108
- }
109
-
110
- function test_Decode_63() public pure {
111
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"fc");
112
- assertEq(value, 63);
113
- assertEq(bytesRead, 1);
114
- }
115
-
116
- // ============ Decode two-byte ============
117
-
118
- function test_Decode_64() public pure {
119
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"0101");
120
- assertEq(value, 64);
121
- assertEq(bytesRead, 2);
122
- }
123
-
124
- function test_Decode_65() public pure {
125
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"0501");
126
- assertEq(value, 65);
127
- assertEq(bytesRead, 2);
128
- }
129
-
130
- function test_Decode_16383() public pure {
131
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"fdff");
132
- assertEq(value, 16383);
133
- assertEq(bytesRead, 2);
134
- }
135
-
136
- // ============ Decode four-byte ============
137
-
138
- function test_Decode_16384() public pure {
139
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"02000100");
140
- assertEq(value, 16384);
141
- assertEq(bytesRead, 4);
142
- }
143
-
144
- function test_Decode_1073741823() public pure {
145
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"feffffff");
146
- assertEq(value, 1073741823);
147
- assertEq(bytesRead, 4);
148
- }
149
-
150
- // ============ Decode big-integer ============
151
-
152
- function test_Decode_1073741824() public pure {
153
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"0300000040");
154
- assertEq(value, 1073741824);
155
- assertEq(bytesRead, 5);
156
- }
157
-
158
- function test_Decode_MaxUint32() public pure {
159
- (uint256 value, uint256 bytesRead) = Compact.decode(hex"03ffffffff");
160
- assertEq(value, type(uint32).max);
161
- assertEq(bytesRead, 5);
162
- }
163
-
164
- function test_Decode_BigInt_100000000000000() public pure {
165
- (uint256 value, uint256 bytesRead) = Compact.decode(
166
- hex"0b00407a10f35a"
167
- );
168
- assertEq(value, 100000000000000);
169
- assertEq(bytesRead, 7);
170
- }
171
-
172
- // ============ DecodeAt with offset ============
173
-
174
- function test_DecodeAt() public pure {
175
- bytes memory data = hex"04010102000100";
176
-
177
- (uint256 v1, uint256 r1) = Compact.decodeAt(data, 0);
178
- assertEq(v1, 1);
179
- assertEq(r1, 1);
180
-
181
- (uint256 v2, uint256 r2) = Compact.decodeAt(data, 1);
182
- assertEq(v2, 64);
183
- assertEq(r2, 2);
184
-
185
- (uint256 v3, uint256 r3) = Compact.decodeAt(data, 3);
186
- assertEq(v3, 16384);
187
- assertEq(r3, 4);
188
- }
189
-
190
- // ============ Typed encoding ============
191
-
192
- function test_EncodeU8() public pure {
193
- assertEq(Compact.encodeU8(42), hex"a8");
194
- assertEq(Compact.encodeU8(255), hex"fd03");
195
- }
196
-
197
- function test_EncodeU16() public pure {
198
- assertEq(Compact.encodeU16(1000), hex"a10f");
199
- assertEq(Compact.encodeU16(65535), hex"feff0300");
200
- }
201
-
202
- function test_EncodeU32() public pure {
203
- assertEq(Compact.encodeU32(1000000), hex"02093d00");
204
- }
205
-
206
- // ============ Typed decoding ============
207
-
208
- function test_DecodeU8() public pure {
209
- (uint8 value, uint256 bytesRead) = Compact.decodeU8(hex"a8");
210
- assertEq(value, 42);
211
- assertEq(bytesRead, 1);
212
- }
213
-
214
- function test_DecodeU16() public pure {
215
- (uint16 value, uint256 bytesRead) = Compact.decodeU16(hex"a10f");
216
- assertEq(value, 1000);
217
- assertEq(bytesRead, 2);
218
- }
219
-
220
- function test_DecodeU32() public pure {
221
- (uint32 value, uint256 bytesRead) = Compact.decodeU32(hex"02093d00");
222
- assertEq(value, 1000000);
223
- assertEq(bytesRead, 4);
224
- }
225
-
226
- // ============ encodedLength ============
227
-
228
- function test_EncodedLength() public pure {
229
- assertEq(Compact.encodedLength(0), 1);
230
- assertEq(Compact.encodedLength(63), 1);
231
- assertEq(Compact.encodedLength(64), 2);
232
- assertEq(Compact.encodedLength(16383), 2);
233
- assertEq(Compact.encodedLength(16384), 4);
234
- assertEq(Compact.encodedLength(1073741823), 4);
235
- assertEq(Compact.encodedLength(1073741824), 5);
236
- assertEq(Compact.encodedLength(type(uint64).max), 9);
237
- }
238
-
239
- // ============ Mode checks ============
240
-
241
- function test_IsSingleByte() public pure {
242
- assertTrue(Compact.isSingleByte(0));
243
- assertTrue(Compact.isSingleByte(63));
244
- assertFalse(Compact.isSingleByte(64));
245
- }
246
-
247
- function test_IsTwoByte() public pure {
248
- assertFalse(Compact.isTwoByte(63));
249
- assertTrue(Compact.isTwoByte(64));
250
- assertTrue(Compact.isTwoByte(16383));
251
- assertFalse(Compact.isTwoByte(16384));
252
- }
253
-
254
- function test_IsFourByte() public pure {
255
- assertFalse(Compact.isFourByte(16383));
256
- assertTrue(Compact.isFourByte(16384));
257
- assertTrue(Compact.isFourByte(1073741823));
258
- assertFalse(Compact.isFourByte(1073741824));
259
- }
260
-
261
- function test_IsBigInt() public pure {
262
- assertFalse(Compact.isBigInt(1073741823));
263
- assertTrue(Compact.isBigInt(1073741824));
264
- assertTrue(Compact.isBigInt(type(uint256).max));
265
- }
266
-
267
- // ============ Fuzz roundtrip ============
268
-
269
- function testFuzz_Roundtrip(uint256 value) public pure {
270
- bytes memory encoded = Compact.encode(value);
271
- (uint256 decoded, ) = Compact.decode(encoded);
272
- assertEq(decoded, value);
273
- }
274
-
275
- function testFuzz_RoundtripU8(uint8 value) public pure {
276
- bytes memory encoded = Compact.encodeU8(value);
277
- (uint8 decoded, ) = Compact.decodeU8(encoded);
278
- assertEq(decoded, value);
279
- }
280
-
281
- function testFuzz_RoundtripU16(uint16 value) public pure {
282
- bytes memory encoded = Compact.encodeU16(value);
283
- (uint16 decoded, ) = Compact.decodeU16(encoded);
284
- assertEq(decoded, value);
285
- }
286
-
287
- function testFuzz_RoundtripU32(uint32 value) public pure {
288
- bytes memory encoded = Compact.encodeU32(value);
289
- (uint32 decoded, ) = Compact.decodeU32(encoded);
290
- assertEq(decoded, value);
291
- }
292
-
293
- function testFuzz_RoundtripU64(uint64 value) public pure {
294
- bytes memory encoded = Compact.encodeU64(value);
295
- (uint64 decoded, ) = Compact.decodeU64(encoded);
296
- assertEq(decoded, value);
297
- }
298
-
299
- function testFuzz_RoundtripU128(uint128 value) public pure {
300
- bytes memory encoded = Compact.encodeU128(value);
301
- (uint128 decoded, ) = Compact.decodeU128(encoded);
302
- assertEq(decoded, value);
303
- }
304
-
305
- // ============ Error cases (using wrapper for external calls) ============
306
-
307
- function test_RevertOnEmptyData() public {
308
- vm.expectRevert(Compact.InvalidCompactEncoding.selector);
309
- wrapper.decode(hex"");
310
- }
311
-
312
- function test_RevertOnTruncatedTwoByte() public {
313
- vm.expectRevert(Compact.InvalidCompactEncoding.selector);
314
- wrapper.decode(hex"01"); // Mode 0b01 but only 1 byte
315
- }
316
-
317
- function test_RevertOnTruncatedFourByte() public {
318
- vm.expectRevert(Compact.InvalidCompactEncoding.selector);
319
- wrapper.decode(hex"020001"); // Mode 0b10 but only 3 bytes
320
- }
321
-
322
- function test_RevertOnTruncatedBigInt() public {
323
- vm.expectRevert(Compact.InvalidCompactEncoding.selector);
324
- wrapper.decode(hex"030000"); // Header says 4 bytes but only 2 provided
325
- }
326
- }
@@ -1,354 +0,0 @@
1
- // SPDX-License-Identifier: Apache-2.0
2
- pragma solidity ^0.8.20;
3
-
4
- /// @title LittleEndian
5
- /// @notice Gas-optimized library for converting unsigned integers from big-endian to little-endian
6
- library LittleEndian {
7
- /// @notice Converts a uint8 to little-endian bytes1
8
- function toLittleEndianU8(uint8 value) internal pure returns (bytes1) {
9
- return bytes1(value);
10
- }
11
-
12
- /// @notice Converts an int8 to little-endian bytes1 (two's complement)
13
- function toLittleEndianI8(int8 value) internal pure returns (bytes1) {
14
- return bytes1(uint8(value));
15
- }
16
-
17
- /// @notice Converts a uint16 to little-endian bytes2
18
- function toLittleEndianU16(uint16 value) internal pure returns (bytes2) {
19
- return bytes2(uint16((value >> 8) | (value << 8)));
20
- }
21
-
22
- /// @notice Converts an int16 to little-endian bytes2 (two's complement)
23
- function toLittleEndianI16(int16 value) internal pure returns (bytes2) {
24
- return toLittleEndianU16(uint16(value));
25
- }
26
-
27
- /// @notice Converts a uint32 to little-endian bytes4
28
- function toLittleEndianU32(
29
- uint32 value
30
- ) internal pure returns (bytes4 result) {
31
- assembly {
32
- let v := or(
33
- or(
34
- shl(24, and(value, 0xff)),
35
- shl(16, and(shr(8, value), 0xff))
36
- ),
37
- or(shl(8, and(shr(16, value), 0xff)), and(shr(24, value), 0xff))
38
- )
39
- result := shl(224, v) // Shift to bytes4 position (left-align in 256-bit word)
40
- }
41
- }
42
-
43
- /// @notice Converts an int32 to little-endian bytes4 (two's complement)
44
- function toLittleEndianI32(int32 value) internal pure returns (bytes4) {
45
- return toLittleEndianU32(uint32(value));
46
- }
47
-
48
- /// @notice Converts a uint64 to little-endian bytes8
49
- function toLittleEndianU64(
50
- uint64 value
51
- ) internal pure returns (bytes8 result) {
52
- assembly {
53
- let v := value
54
- v := or(
55
- shl(8, and(v, 0x00FF00FF00FF00FF)),
56
- shr(8, and(v, 0xFF00FF00FF00FF00))
57
- )
58
- v := or(
59
- shl(16, and(v, 0x0000FFFF0000FFFF)),
60
- shr(16, and(v, 0xFFFF0000FFFF0000))
61
- )
62
- v := or(shl(32, v), shr(32, v))
63
- result := shl(192, v)
64
- }
65
- }
66
-
67
- /// @notice Converts an int64 to little-endian bytes8 (two's complement)
68
- function toLittleEndianI64(int64 value) internal pure returns (bytes8) {
69
- return toLittleEndianU64(uint64(value));
70
- }
71
-
72
- /// @notice Converts a uint128 to little-endian bytes16
73
- function toLittleEndianU128(
74
- uint128 value
75
- ) internal pure returns (bytes16 result) {
76
- assembly {
77
- let v := value
78
- v := or(
79
- shl(8, and(v, 0x00FF00FF00FF00FF00FF00FF00FF00FF)),
80
- shr(8, and(v, 0xFF00FF00FF00FF00FF00FF00FF00FF00))
81
- )
82
- v := or(
83
- shl(16, and(v, 0x0000FFFF0000FFFF0000FFFF0000FFFF)),
84
- shr(16, and(v, 0xFFFF0000FFFF0000FFFF0000FFFF0000))
85
- )
86
- v := or(
87
- shl(32, and(v, 0x00000000FFFFFFFF00000000FFFFFFFF)),
88
- shr(32, and(v, 0xFFFFFFFF00000000FFFFFFFF00000000))
89
- )
90
- v := or(shl(64, v), shr(64, v))
91
- result := shl(128, v)
92
- }
93
- }
94
-
95
- /// @notice Converts an int128 to little-endian bytes16 (two's complement)
96
- function toLittleEndianI128(int128 value) internal pure returns (bytes16) {
97
- return toLittleEndianU128(uint128(value));
98
- }
99
-
100
- /// @notice Converts a uint256 to little-endian bytes32
101
- function toLittleEndianU256(
102
- uint256 value
103
- ) internal pure returns (bytes32 result) {
104
- assembly {
105
- let v := value
106
- v := or(
107
- shl(
108
- 8,
109
- and(
110
- v,
111
- 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF
112
- )
113
- ),
114
- shr(
115
- 8,
116
- and(
117
- v,
118
- 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00
119
- )
120
- )
121
- )
122
- v := or(
123
- shl(
124
- 16,
125
- and(
126
- v,
127
- 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF
128
- )
129
- ),
130
- shr(
131
- 16,
132
- and(
133
- v,
134
- 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
135
- )
136
- )
137
- )
138
- v := or(
139
- shl(
140
- 32,
141
- and(
142
- v,
143
- 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF
144
- )
145
- ),
146
- shr(
147
- 32,
148
- and(
149
- v,
150
- 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000
151
- )
152
- )
153
- )
154
- v := or(
155
- shl(
156
- 64,
157
- and(
158
- v,
159
- 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF
160
- )
161
- ),
162
- shr(
163
- 64,
164
- and(
165
- v,
166
- 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000
167
- )
168
- )
169
- )
170
- v := or(shl(128, v), shr(128, v))
171
- result := v
172
- }
173
- }
174
-
175
- /// @notice Converts an int256 to little-endian bytes32 (two's complement)
176
- function toLittleEndianI256(int256 value) internal pure returns (bytes32) {
177
- return toLittleEndianU256(uint256(value));
178
- }
179
-
180
- /// @notice Converts little-endian bytes1 to uint8
181
- function fromLittleEndianU8(bytes1 value) internal pure returns (uint8) {
182
- return uint8(value);
183
- }
184
-
185
- /// @notice Converts little-endian bytes1 to int8
186
- function fromLittleEndianI8(bytes1 value) internal pure returns (int8) {
187
- return int8(uint8(value));
188
- }
189
-
190
- /// @notice Converts little-endian bytes2 to uint16
191
- function fromLittleEndianU16(bytes2 value) internal pure returns (uint16) {
192
- return uint16(uint8(value[0])) | (uint16(uint8(value[1])) << 8);
193
- }
194
-
195
- /// @notice Converts little-endian bytes2 to int16
196
- function fromLittleEndianI16(bytes2 value) internal pure returns (int16) {
197
- return int16(fromLittleEndianU16(value));
198
- }
199
-
200
- /// @notice Converts little-endian bytes4 to uint32
201
- function fromLittleEndianU32(
202
- bytes4 value
203
- ) internal pure returns (uint32 result) {
204
- assembly {
205
- let v := shr(224, value)
206
- v := or(
207
- or(shl(24, and(v, 0xff)), shl(16, and(shr(8, v), 0xff))),
208
- or(shl(8, and(shr(16, v), 0xff)), and(shr(24, v), 0xff))
209
- )
210
- result := v
211
- }
212
- }
213
-
214
- /// @notice Converts little-endian bytes4 to int32
215
- function fromLittleEndianI32(bytes4 value) internal pure returns (int32) {
216
- return int32(fromLittleEndianU32(value));
217
- }
218
-
219
- /// @notice Converts little-endian bytes8 to uint64
220
- function fromLittleEndianU64(
221
- bytes8 value
222
- ) internal pure returns (uint64 result) {
223
- assembly {
224
- let v := shr(192, value)
225
- v := or(
226
- shl(8, and(v, 0x00FF00FF00FF00FF)),
227
- shr(8, and(v, 0xFF00FF00FF00FF00))
228
- )
229
- v := or(
230
- shl(16, and(v, 0x0000FFFF0000FFFF)),
231
- shr(16, and(v, 0xFFFF0000FFFF0000))
232
- )
233
- v := or(shl(32, v), shr(32, v))
234
- result := v
235
- }
236
- }
237
-
238
- /// @notice Converts little-endian bytes8 to int64
239
- function fromLittleEndianI64(bytes8 value) internal pure returns (int64) {
240
- return int64(fromLittleEndianU64(value));
241
- }
242
-
243
- /// @notice Converts little-endian bytes16 to uint128
244
- function fromLittleEndianU128(
245
- bytes16 value
246
- ) internal pure returns (uint128 result) {
247
- assembly {
248
- let v := shr(128, value)
249
- v := or(
250
- shl(8, and(v, 0x00FF00FF00FF00FF00FF00FF00FF00FF)),
251
- shr(8, and(v, 0xFF00FF00FF00FF00FF00FF00FF00FF00))
252
- )
253
- v := or(
254
- shl(16, and(v, 0x0000FFFF0000FFFF0000FFFF0000FFFF)),
255
- shr(16, and(v, 0xFFFF0000FFFF0000FFFF0000FFFF0000))
256
- )
257
- v := or(
258
- shl(32, and(v, 0x00000000FFFFFFFF00000000FFFFFFFF)),
259
- shr(32, and(v, 0xFFFFFFFF00000000FFFFFFFF00000000))
260
- )
261
- v := or(shl(64, v), shr(64, v))
262
- result := v
263
- }
264
- }
265
-
266
- /// @notice Converts little-endian bytes16 to int128
267
- function fromLittleEndianI128(
268
- bytes16 value
269
- ) internal pure returns (int128) {
270
- return int128(fromLittleEndianU128(value));
271
- }
272
-
273
- /// @notice Converts little-endian bytes32 to uint256
274
- function fromLittleEndianU256(
275
- bytes32 value
276
- ) internal pure returns (uint256 result) {
277
- assembly {
278
- let v := value
279
- v := or(
280
- shl(
281
- 8,
282
- and(
283
- v,
284
- 0x00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF
285
- )
286
- ),
287
- shr(
288
- 8,
289
- and(
290
- v,
291
- 0xFF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00FF00
292
- )
293
- )
294
- )
295
- v := or(
296
- shl(
297
- 16,
298
- and(
299
- v,
300
- 0x0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF
301
- )
302
- ),
303
- shr(
304
- 16,
305
- and(
306
- v,
307
- 0xFFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000FFFF0000
308
- )
309
- )
310
- )
311
- v := or(
312
- shl(
313
- 32,
314
- and(
315
- v,
316
- 0x00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF
317
- )
318
- ),
319
- shr(
320
- 32,
321
- and(
322
- v,
323
- 0xFFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000FFFFFFFF00000000
324
- )
325
- )
326
- )
327
- v := or(
328
- shl(
329
- 64,
330
- and(
331
- v,
332
- 0x0000000000000000FFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF
333
- )
334
- ),
335
- shr(
336
- 64,
337
- and(
338
- v,
339
- 0xFFFFFFFFFFFFFFFF0000000000000000FFFFFFFFFFFFFFFF0000000000000000
340
- )
341
- )
342
- )
343
- v := or(shl(128, v), shr(128, v))
344
- result := v
345
- }
346
- }
347
-
348
- /// @notice Converts little-endian bytes32 to int256
349
- function fromLittleEndianI256(
350
- bytes32 value
351
- ) internal pure returns (int256) {
352
- return int256(fromLittleEndianU256(value));
353
- }
354
- }