nybbles 0.4.8

Efficient nibble-sized (4-bit) byte sequence data structure
Documentation
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
use cfg_if::cfg_if;
use core::{
    cmp::{self, Ordering},
    fmt,
    mem::MaybeUninit,
    ops::{Bound, Deref, RangeBounds},
    slice,
    str::FromStr,
};
use ruint::aliases::U256;
use smallvec::SmallVec;

#[cfg(not(feature = "nightly"))]
#[allow(unused_imports)]
use core::convert::{identity as likely, identity as unlikely};
#[cfg(feature = "nightly")]
#[allow(unused_imports)]
use core::intrinsics::{likely, unlikely};

#[cfg(not(feature = "std"))]
use alloc::vec::Vec;

/// The size of [`U256`] in nibbles.
const NIBBLES: usize = 64;

/// This array contains 65 bitmasks used in [`Nibbles::slice`].
///
/// Each mask is a [`U256`] where:
/// - Index 0 is just 0 (no bits set)
/// - Index 1 has the highest 4 bits set (one nibble)
/// - Index 2 has the highest 8 bits set (two nibbles)
/// - ...and so on
/// - Index 64 has all bits set ([`U256::MAX`])
static SLICE_MASKS: [U256; 65] = {
    let mut masks = [U256::ZERO; 65];
    let mut i = 0;
    while i <= NIBBLES {
        masks[i] = if i == 0 { U256::ZERO } else { U256::MAX.wrapping_shl((NIBBLES - i) * 4) };
        i += 1;
    }
    masks
};

/// This array contains 65 values to add that are used in [`Nibbles::increment`].
///
/// Each value is a [`U256`] equal to `1 << ((64 - i) * 4)`.
static INCREMENT_VALUES: [U256; 65] = {
    let mut masks = [U256::ZERO; 65];
    let mut i = 0;
    while i <= NIBBLES {
        masks[i] = U256::ONE.wrapping_shl((NIBBLES - i) * 4);
        i += 1;
    }
    masks
};

/// Structure representing a sequence of nibbles.
///
/// A nibble is a 4-bit value, and this structure is used to store the nibble sequence representing
/// the keys in a Merkle Patricia Trie (MPT).
/// Using nibbles simplifies trie operations and enables consistent key representation in the MPT.
///
/// # Internal representation
///
/// The internal representation is currently a [`U256`] that stores two nibbles per byte. Nibbles
/// are stored inline (on the stack), and can be up to a length of 64 nibbles, or 32 unpacked bytes.
///
/// Additionally, a separate `length` field is stored to track the actual length of the nibble
/// sequence. When the [`U256`] is modified directly, the `length` field must be updated
/// accordingly. Otherwise, the behavior is undefined.
///
/// Nibbles are stored with most significant bits set first, meaning that a nibble sequence `0x101`
/// will be stored as `0x101...0`, and not `0x0...101`.
///
/// # Examples
///
/// ```
/// use nybbles::Nibbles;
///
/// let bytes = [0xAB, 0xCD];
/// let nibbles = Nibbles::unpack(&bytes);
/// assert_eq!(nibbles, Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]));
/// assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
///
/// let packed = nibbles.pack();
/// assert_eq!(&packed[..], &bytes[..]);
/// ```
#[repr(C)] // We want to preserve the order of fields in the memory layout.
#[derive(Default, Clone, Copy, Eq)]
pub struct Nibbles {
    /// Nibbles length.
    pub(crate) len: usize,
    /// The nibbles themselves,
    /// stored as a 256-bit unsigned integer with most significant bits set first.
    pub(crate) nibbles: U256,
}

impl fmt::Debug for Nibbles {
    #[inline]
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.is_empty() {
            write!(f, "Nibbles(0x)")
        } else {
            let shifted = self.nibbles >> ((NIBBLES - self.len()) * 4);
            write!(f, "Nibbles(0x{:0width$x})", shifted, width = self.len())
        }
    }
}

type AsArray = [u64; 5];

impl PartialEq for Nibbles {
    #[inline]
    fn eq(&self, other: &Self) -> bool {
        if let Some(arr) = self.as_array()
            && let Some(other_arr) = other.as_array()
        {
            arr == other_arr
        } else {
            self.len == other.len && self.nibbles == other.nibbles
        }
    }
}

impl core::hash::Hash for Nibbles {
    #[inline]
    fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
        if let Some(arr) = self.as_array() {
            arr.hash(state);
        } else {
            self.len.hash(state);
            self.nibbles.hash(state);
        }
    }
}

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Nibbles {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let length = u.int_in_range(0..=NIBBLES)?;
        let nibbles = Nibbles::from_nibbles_unchecked(
            (0..length).map(|_| u.int_in_range(0..=0xf)).collect::<Result<Vec<_>, _>>()?,
        );
        Ok(nibbles)
    }
}

impl PartialOrd for Nibbles {
    #[inline]
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

// Deriving [`Ord`] for [`Nibbles`] is not correct, because they will be compared as unsigned
// integers without accounting for length. This is incorrect, because `0x1` should be considered
// greater than `0x02`.
impl Ord for Nibbles {
    #[inline]
    fn cmp(&self, other: &Self) -> Ordering {
        let self_len = self.byte_len();
        let other_len = other.byte_len();
        let l = cmp::min(self_len, other_len);
        let len_cmp = self.len().cmp(&other.len());

        let byte_idx = longest_prefix_byte(&self.nibbles, &other.nibbles);
        let r = if byte_idx < l {
            // SAFETY: `byte_idx` < 32, so `31 - byte_idx` is valid.
            let le_idx = 31 - byte_idx;
            let get = |x: &U256| unsafe { *as_le_slice(x).get_unchecked(le_idx) };
            let a = get(&self.nibbles);
            let b = get(&other.nibbles);
            a.cmp(&b)
        } else {
            Ordering::Equal
        };
        core::hint::select_unpredictable(r == Ordering::Equal, len_cmp, r)
    }
}

impl FromIterator<u8> for Nibbles {
    #[inline]
    fn from_iter<T: IntoIterator<Item = u8>>(iter: T) -> Self {
        let mut nibbles = Self::default();
        Extend::extend(&mut nibbles, iter);
        nibbles
    }
}

impl Extend<u8> for Nibbles {
    #[inline]
    fn extend<T: IntoIterator<Item = u8>>(&mut self, iter: T) {
        for n in iter {
            self.push(n);
        }
    }
}

impl FromStr for Nibbles {
    type Err = &'static str;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        // Check if string starts with "0x"
        let hex_str = s.strip_prefix("0x").ok_or("missing 0x prefix")?;

        // Empty case
        if hex_str.is_empty() {
            return Ok(Self::new());
        }

        // Check length
        if hex_str.len() > NIBBLES {
            return Err("hex string too long");
        }

        // Check that all characters are valid hex characters. We do this once ahead of time so we
        // can pass an iter into [`Self::from_iter_unchecked`], saving a Vec alloc.
        for ch in hex_str.chars() {
            let _ = ch.to_digit(16).ok_or("invalid hex character")?;
        }

        let iter = hex_str.chars().map(|ch| ch.to_digit(16).expect("already validated") as u8);
        Ok(Self::from_iter_unchecked(iter))
    }
}

#[cfg(feature = "rlp")]
impl alloy_rlp::Encodable for Nibbles {
    #[inline]
    fn encode(&self, out: &mut dyn alloy_rlp::BufMut) {
        alloy_rlp::Header { list: true, payload_length: self.len() }.encode(out);
        for i in 0..self.len() {
            self.get_unchecked(i).encode(out);
        }
    }

    #[inline]
    fn length(&self) -> usize {
        let payload_length = self.len();
        payload_length + alloy_rlp::length_of_length(payload_length)
    }
}

#[cfg(feature = "arbitrary")]
impl proptest::arbitrary::Arbitrary for Nibbles {
    type Parameters = proptest::collection::SizeRange;
    type Strategy = proptest::strategy::Map<
        proptest::collection::VecStrategy<core::ops::RangeInclusive<u8>>,
        fn(Vec<u8>) -> Self,
    >;

    #[inline]
    fn arbitrary_with(size: proptest::collection::SizeRange) -> Self::Strategy {
        use proptest::prelude::*;
        proptest::collection::vec(0x0..=0xf, size).prop_map(Self::from_nibbles_unchecked)
    }
}

#[cfg(feature = "serde")]
impl serde::Serialize for Nibbles {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        if self.is_empty() {
            serializer.serialize_str("0x")
        } else {
            let shifted = self.nibbles >> ((NIBBLES - self.len()) * 4);
            let hex_str = format!("0x{:0width$x}", shifted, width = self.len());
            serializer.serialize_str(&hex_str)
        }
    }
}

#[cfg(feature = "serde")]
impl<'de> serde::Deserialize<'de> for Nibbles {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let s = alloc::borrow::Cow::<str>::deserialize(deserializer)?;
        Self::from_str(&s).map_err(serde::de::Error::custom)
    }
}

impl Nibbles {
    /// Creates a new empty [`Nibbles`] instance.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::new();
    /// assert_eq!(nibbles.len(), 0);
    /// ```
    #[inline]
    pub const fn new() -> Self {
        Self { len: 0, nibbles: U256::ZERO }
    }

    /// Creates a new [`Nibbles`] instance from the given iterator over nibbles, without checking
    /// their validity.
    ///
    /// Note that only the low nibble of every byte will be stored as a nibble, i.e. for `0x12` we
    /// will store a nibble `2`.
    ///
    /// For checked version, use the [`FromIterator`] implementation.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_iter_unchecked([0x0A, 0x0B, 0x0C, 0x0D]);
    /// assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
    pub fn from_iter_unchecked<I>(iter: I) -> Self
    where
        I: IntoIterator<Item = u8>,
    {
        let mut packed = Self::default();
        for n in iter {
            packed.push_unchecked(n);
        }
        packed
    }

    /// Creates a new [`Nibbles`] instance from the given nibbles.
    ///
    /// # Panics
    ///
    /// Panics if the any of the bytes is not a valid nibble (`0..=0x0f`).
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
    /// assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
    /// ```
    ///
    /// Invalid values will cause panics:
    ///
    /// ```should_panic
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0xFF]);
    /// ```
    #[inline]
    #[track_caller]
    pub fn from_nibbles<T: AsRef<[u8]>>(nibbles: T) -> Self {
        let bytes = nibbles.as_ref();
        check_nibbles(bytes);
        Self::from_iter_unchecked(bytes.iter().copied())
    }

    /// Creates a new [`Nibbles`] instance from the given nibbles.
    ///
    /// Note that only the low nibble of every byte will be stored as a nibble, i.e. for `0x12` we
    /// will store a nibble `2`.
    ///
    /// For checked version, use [`Nibbles::from_nibbles`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles_unchecked(&[0x0A, 0x0B, 0x0C, 0x0D]);
    /// assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
    /// ```
    ///
    /// Invalid values will not cause panics:
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles_unchecked(&[0xFF]);
    /// assert_eq!(nibbles.to_vec(), vec![0x0F]);
    /// ```
    #[inline]
    pub fn from_nibbles_unchecked<T: AsRef<[u8]>>(nibbles: T) -> Self {
        Self::from_iter_unchecked(nibbles.as_ref().iter().copied())
    }

    /// Converts a byte slice into a [`Nibbles`] instance containing the nibbles (half-bytes or 4
    /// bits) that make up the input byte data.
    ///
    /// # Panics
    ///
    /// Panics if the length of the input is greater than 32 bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::unpack(&[0xAB, 0xCD]);
    /// assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
    /// ```
    ///
    /// ```should_panic
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::unpack(&[0xAB; 33]);
    /// ```
    #[inline]
    #[track_caller]
    pub fn unpack(data: impl AsRef<[u8]>) -> Self {
        assert!(data.as_ref().len() <= U256::BYTES);
        // SAFETY: we checked that the length is less than or equal to the size of U256
        unsafe { Self::unpack_unchecked(data.as_ref()) }
    }

    /// Converts a byte slice into a [`Nibbles`] instance containing the nibbles (half-bytes or 4
    /// bits) that make up the input byte data.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the length of the input is less than or equal to the size of
    /// U256, which is 32 bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// // SAFETY: the length of the input is less than 32 bytes.
    /// let nibbles = unsafe { Nibbles::unpack_unchecked(&[0xAB, 0xCD]) };
    /// assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
    /// ```
    pub unsafe fn unpack_unchecked(data: &[u8]) -> Self {
        let len = data.len() * 2;
        debug_assert!(len <= NIBBLES);

        cfg_if! {
            if #[cfg(target_endian = "little")] {
                let mut nibbles = U256::ZERO;
                let nibbles_slice = unsafe { nibbles.as_le_slice_mut() };
            } else {
                let mut nibbles_slice = [0u8; 32];
            }
        }

        // Source pointer is at the beginning
        let mut src = data.as_ptr().cast::<u8>();
        // Move destination pointer to the end of the little endian slice
        let mut dst = unsafe { nibbles_slice.as_mut_ptr().add(U256::BYTES) };
        // On each iteration, decrement the destination pointer by one, set the destination
        // byte, and increment the source pointer by one
        for _ in 0..data.len() {
            unsafe {
                dst = dst.sub(1);
                *dst = *src;
                src = src.add(1);
            }
        }

        cfg_if! {
            if #[cfg(target_endian = "big")] {
                let nibbles = U256::from_le_bytes(nibbles_slice);
            }
        }

        Self { len, nibbles }
    }

    /// Converts a fixed 32 byte array into a [`Nibbles`] instance. Similar to [`Nibbles::unpack`],
    /// but is not `unsafe`.
    #[inline]
    pub const fn unpack_array(data: &[u8; 32]) -> Self {
        let nibbles = U256::from_be_bytes(*data);
        Self { len: NIBBLES, nibbles }
    }

    /// Packs the nibbles into the given slice.
    ///
    /// This method combines each pair of consecutive nibbles into a single byte,
    /// effectively reducing the size of the data by a factor of two.
    ///
    /// If the number of nibbles is odd, the last nibble is shifted left by 4 bits and
    /// added to the packed byte vector.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C]);
    /// assert_eq!(nibbles.pack()[..], [0xAB, 0xC0]);
    /// ```
    #[inline]
    pub fn pack(&self) -> SmallVec<[u8; 32]> {
        let packed_len = self.byte_len();
        unsafe { smallvec_with(packed_len, |out| self.pack_to_slice_unchecked(out)) }
    }

    /// Packs the nibbles into the given slice.
    ///
    /// See [`pack`](Self::pack) for more information.
    ///
    /// # Panics
    ///
    /// Panics if the slice is not at least `(self.len() + 1) / 2` bytes long.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C]);
    /// let mut packed = [0; 2];
    /// nibbles.pack_to(&mut packed);
    /// assert_eq!(packed[..], [0xAB, 0xC0]);
    /// ```
    #[inline]
    #[track_caller]
    pub fn pack_to(&self, out: &mut [u8]) {
        assert!(out.len() >= self.byte_len());
        // SAFETY: asserted length.
        unsafe { self.pack_to_unchecked(out.as_mut_ptr()) }
    }

    /// Packs the nibbles into the given pointer.
    ///
    /// See [`pack`](Self::pack) for more information.
    ///
    /// # Safety
    ///
    /// `ptr` must be valid for at least `(self.len() + 1) / 2` bytes.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
    /// let mut packed = [0; 2];
    /// // SAFETY: enough capacity.
    /// unsafe { nibbles.pack_to_unchecked(packed.as_mut_ptr()) };
    /// assert_eq!(packed[..], [0xAB, 0xCD]);
    /// ```
    #[inline]
    pub unsafe fn pack_to_unchecked(&self, ptr: *mut u8) {
        unsafe {
            let slice = slice::from_raw_parts_mut(ptr.cast(), self.byte_len());
            pack_to_unchecked(self, slice);
        }
    }

    /// Packs the nibbles into the given slice without checking its length.
    ///
    /// See [`pack`](Self::pack) for more information.
    ///
    /// # Safety
    ///
    /// `out` must be valid for at least `(self.len() + 1) / 2` bytes.
    #[inline]
    pub unsafe fn pack_to_slice_unchecked(&self, out: &mut [MaybeUninit<u8>]) {
        unsafe { pack_to_unchecked(self, out) }
    }

    /// Converts the nibbles into a vector of nibbles.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
    /// assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
    /// ```
    pub fn to_vec(&self) -> Vec<u8> {
        self.iter().collect()
    }

    /// Returns an iterator over the nibbles.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
    /// let collected: Vec<u8> = nibbles.iter().collect();
    /// assert_eq!(collected, vec![0x0A, 0x0B, 0x0C, 0x0D]);
    /// ```
    #[inline]
    pub const fn iter(&self) -> NibblesIter<'_> {
        NibblesIter { current: 0, nibbles: self }
    }

    /// Gets the byte at the given index by combining two consecutive nibbles.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
    /// assert_eq!(nibbles.get_byte(0), Some(0xAB));
    /// assert_eq!(nibbles.get_byte(1), Some(0xBC));
    /// assert_eq!(nibbles.get_byte(2), Some(0xCD));
    /// assert_eq!(nibbles.get_byte(3), None);
    /// ```
    pub fn get_byte(&self, i: usize) -> Option<u8> {
        if likely((i < usize::MAX) & self.check_index(i.wrapping_add(1))) {
            Some(self.get_byte_unchecked(i))
        } else {
            None
        }
    }

    /// Gets the byte at the given index by combining two consecutive nibbles.
    ///
    /// # Panics
    ///
    /// Panics if `i..i + 1` is out of bounds.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
    /// // SAFETY: in range.
    /// unsafe {
    ///     assert_eq!(nibbles.get_byte_unchecked(0), 0xAB);
    ///     assert_eq!(nibbles.get_byte_unchecked(1), 0xBC);
    ///     assert_eq!(nibbles.get_byte_unchecked(2), 0xCD);
    /// }
    /// ```
    #[inline]
    #[track_caller]
    pub fn get_byte_unchecked(&self, i: usize) -> u8 {
        self.assert_index(i);
        if i % 2 == 0 {
            as_le_slice(&self.nibbles)[U256::BYTES - i / 2 - 1]
        } else {
            self.get_unchecked(i) << 4 | self.get_unchecked(i + 1)
        }
    }

    /// Increments the nibble sequence by one.
    #[inline]
    pub fn increment(&self) -> Option<Self> {
        if self.is_empty() || self.nibbles == SLICE_MASKS[self.len()] {
            return None;
        }

        let mut incremented = *self;
        let add = INCREMENT_VALUES[self.len()];
        incremented.nibbles += add;
        Some(incremented)
    }

    /// The last element of the hex vector is used to determine whether the nibble sequence
    /// represents a leaf or an extension node. If the last element is 0x10 (16), then it's a leaf.
    #[inline]
    pub fn is_leaf(&self) -> bool {
        self.last() == Some(16)
    }

    /// Returns `true` if this nibble sequence starts with the given prefix.
    #[inline]
    pub fn starts_with(&self, other: &Self) -> bool {
        other.len() <= self.len() && (self.nibbles & SLICE_MASKS[other.len()]) == other.nibbles
    }

    /// Returns `true` if this nibble sequence ends with the given suffix.
    pub fn ends_with(&self, other: &Self) -> bool {
        // If other is empty, it's a suffix of any sequence
        if other.is_empty() {
            return true;
        }

        // If other is longer than self, it can't be a suffix
        if other.len() > self.len() {
            return false;
        }

        // Fast path for even-even and odd-odd sequences
        if self.len() % 2 == other.len() % 2 {
            return as_le_slice(&self.nibbles)
                [(NIBBLES - self.len()) / 2..(NIBBLES - self.len() + other.len()) / 2]
                == as_le_slice(&other.nibbles)[(NIBBLES - other.len()) / 2..];
        }

        let mut i = 0;
        while i < other.len() {
            if self.get_unchecked(self.len() - i - 1) != other.get_unchecked(other.len() - i - 1) {
                return false;
            }
            i += 1;
        }

        true
    }

    /// Returns the nibble at the given index.
    #[inline]
    pub fn get(&self, i: usize) -> Option<u8> {
        if self.check_index(i) { Some(self.get_unchecked(i)) } else { None }
    }

    /// Returns the nibble at the given index.
    ///
    /// # Panics
    ///
    /// Panics if the index is out of bounds.
    #[inline]
    #[track_caller]
    pub fn get_unchecked(&self, i: usize) -> u8 {
        self.assert_index(i);
        let byte = as_le_slice(&self.nibbles)[U256::BYTES - i / 2 - 1];
        if i % 2 == 0 { byte >> 4 } else { byte & 0x0F }
    }

    /// Sets the nibble at the given index.
    ///
    /// # Panics
    ///
    /// Panics if the index is out of bounds, or if `value` is not a valid nibble (`0..=0x0f`).
    #[inline]
    #[track_caller]
    pub fn set_at(&mut self, i: usize, value: u8) {
        assert!(self.check_index(i) && value <= 0xf);
        // SAFETY: index is checked above
        unsafe { self.set_at_unchecked(i, value) };
    }

    /// Sets the nibble at the given index, without checking its validity.
    ///
    /// # Safety
    ///
    /// The caller must ensure that the index is within bounds.
    #[inline]
    pub unsafe fn set_at_unchecked(&mut self, i: usize, value: u8) {
        let byte_index = U256::BYTES - i / 2 - 1;

        // SAFETY: index checked above
        cfg_if! {
            if #[cfg(target_endian = "little")] {
                let byte = unsafe { &mut self.nibbles.as_le_slice_mut()[byte_index] };
            } else {
                // Big-endian targets must first copy the nibbles to a little-endian slice.
                // Underneath the hood, `as_le_slice` will perform a stack copy, and we
                // replace the underlying `nibbles` after we've updated the given nibble.
                let mut le_copy = as_le_slice(&self.nibbles);
                let byte = &mut le_copy.to_mut()[byte_index];
            }
        }

        if i % 2 == 0 {
            *byte = *byte & 0x0f | value << 4;
        } else {
            *byte = *byte & 0xf0 | value;
        }

        // For big-endian targets, replace the underlying U256 with the mutated LE slice.
        #[cfg(target_endian = "big")]
        {
            self.nibbles = U256::from_le_slice(&le_copy);
        }
    }

    /// Returns the first nibble of this nibble sequence.
    #[inline]
    pub fn first(&self) -> Option<u8> {
        self.get(0)
    }

    /// Returns the last nibble of this nibble sequence.
    #[inline]
    pub fn last(&self) -> Option<u8> {
        let len = self.len();
        if len == 0 { None } else { Some(self.get_unchecked(len - 1)) }
    }

    /// Returns the length of the common prefix between this nibble sequence and the given.
    ///
    /// # Examples
    ///
    /// ```
    /// # use nybbles::Nibbles;
    /// let a = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F]);
    /// let b = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0E]);
    /// assert_eq!(a.common_prefix_length(&b), 3);
    /// ```
    #[inline]
    pub fn common_prefix_length(&self, other: &Self) -> usize {
        let l = self.len().min(other.len());
        self.common_prefix_length_raw(other).min(l)
    }

    #[inline]
    fn common_prefix_length_raw(&self, other: &Self) -> usize {
        longest_prefix_bit(&self.nibbles, &other.nibbles) / 4
    }

    /// Returns the total number of bits in this [`Nibbles`].
    #[inline]
    const fn bit_len(&self) -> usize {
        self.len() * 4
    }

    /// Returns `true` if this [`Nibbles`] is empty.
    #[inline]
    pub const fn is_empty(&self) -> bool {
        self.len() == 0
    }

    /// Returns `true` if all nibbles in this [`Nibbles`] are zero.
    #[inline]
    pub fn is_zeroes(&self) -> bool {
        self.nibbles.is_zero()
    }

    /// Returns the total number of nibbles in this [`Nibbles`].
    #[inline]
    pub const fn len(&self) -> usize {
        let len = self.len;
        debug_assert!(len <= NIBBLES);
        unsafe { core::hint::assert_unchecked(len <= NIBBLES) };
        len
    }

    /// Returns the total number of bytes in this [`Nibbles`].
    #[inline]
    pub const fn byte_len(&self) -> usize {
        byte_len(self.len())
    }

    /// Returns a mutable reference to the underlying [`U256`].
    ///
    /// Note that it is possible to create invalid [`Nibbles`] instances using this method. See
    /// [the type docs](Self) for more details.
    #[inline]
    pub const fn as_mut_uint_unchecked(&mut self) -> &mut U256 {
        &mut self.nibbles
    }

    /// Returns the next nibble sequence in lexicographical order that is not a prefix of `self`.
    ///
    /// Returns `None` if the nibbles are empty or if no such value exists (all nibbles are 0xF).
    ///
    /// # Examples
    ///
    /// ```
    /// use nybbles::Nibbles;
    ///
    /// let nibbles = Nibbles::from_nibbles([0x1, 0x2, 0x3]);
    /// assert_eq!(nibbles.next_without_prefix(), Some(Nibbles::from_nibbles([0x1, 0x2, 0x4])));
    ///
    /// let nibbles = Nibbles::from_nibbles([0x0, 0x0, 0xF]);
    /// assert_eq!(nibbles.next_without_prefix(), Some(Nibbles::from_nibbles([0x0, 0x1])));
    ///
    /// let nibbles = Nibbles::from_nibbles([0xF, 0xF, 0xF]);
    /// assert_eq!(nibbles.next_without_prefix(), None);
    /// ```
    #[inline]
    pub fn next_without_prefix(&self) -> Option<Self> {
        let result = self.increment()?;

        // truncate to position of last non-zero Nibble
        let len = NIBBLES - (result.nibbles.trailing_zeros() / 4);
        Some(Self { len, nibbles: result.nibbles })
    }

    /// Creates new nibbles containing the nibbles in the specified range `[start, end)`
    /// without checking bounds.
    ///
    /// # Safety
    ///
    /// This method does not verify that the provided range is valid for this nibble sequence.
    /// The caller must ensure that `start <= end` and `end <= self.len()`.
    #[inline]
    pub fn slice_unchecked(&self, start: usize, end: usize) -> Self {
        #[cfg(debug_assertions)]
        self.slice_check(start, end);
        let len = end - start;
        if len == 0 {
            return Self::new();
        }
        if len == self.len() {
            return *self;
        }
        let mask = SLICE_MASKS[len];
        let mut nibbles = self.nibbles;
        if start != 0 {
            nibbles <<= start * 4;
        }
        nibbles &= mask;
        Self { len, nibbles }
    }

    /// Creates new nibbles containing the nibbles in the specified range.
    ///
    /// # Panics
    ///
    /// This method will panic if the range is out of bounds for this nibble sequence.
    pub fn slice(&self, range: impl RangeBounds<usize>) -> Self {
        // Determine the start and end nibble indices from the range bounds
        let start = match range.start_bound() {
            Bound::Included(&idx) => idx,
            Bound::Excluded(&idx) => idx + 1,
            Bound::Unbounded => 0,
        };
        let end = match range.end_bound() {
            Bound::Included(&idx) => idx + 1,
            Bound::Excluded(&idx) => idx,
            Bound::Unbounded => self.len(),
        };
        self.slice_check(start, end);
        // Extra hint to remove the bounds check in `slice_unchecked`.
        // SAFETY: `start <= end <= self.len() <= NIBBLES`
        unsafe { core::hint::assert_unchecked(end - start <= NIBBLES) };

        self.slice_unchecked(start, end)
    }

    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    const fn slice_check(&self, start: usize, end: usize) {
        if !(start <= end && end <= self.len()) {
            panic_invalid_slice(start, end, self.len());
        }
    }

    /// Join two nibble sequences together.
    #[inline]
    pub fn join(&self, other: &Self) -> Self {
        let mut new = *self;
        new.extend(other);
        new
    }

    /// Pushes a nibble to the end of the current nibbles.
    ///
    /// # Panics
    ///
    /// Panics if the nibble is not a valid nibble (`0..=0x0f`).
    #[inline]
    #[track_caller]
    pub fn push(&mut self, nibble: u8) {
        assert!(nibble <= 0xf);
        self.push_unchecked(nibble);
    }

    /// Pushes a nibble to the end of the current nibbles without checking its validity.
    ///
    /// Note that only the low nibble of the byte is used. For example, for byte `0x12`, only the
    /// nibble `0x2` is pushed.
    #[inline]
    pub const fn push_unchecked(&mut self, nibble: u8) {
        let len = self.len();
        self.len = len + 1;
        let _ = self.len(); // Assert invariant.

        let nibble_val = (nibble & 0x0F) as u64;
        if nibble_val == 0 {
            // Nothing to do, limb nibbles are already set to zero by default
            return;
        }

        let bit_pos = (NIBBLES - len - 1) * 4;
        let limb_idx = bit_pos / 64;
        let shift_in_limb = bit_pos % 64;

        // SAFETY: limb_idx is always valid because bit_pos < 256
        unsafe {
            let limbs = self.nibbles.as_limbs_mut();
            limbs[limb_idx] |= nibble_val << shift_in_limb;
        }
    }

    /// Pops a nibble from the end of the current nibbles.
    pub fn pop(&mut self) -> Option<u8> {
        if self.is_empty() {
            return None;
        }

        // The last nibble is at bit position (64 - length) * 4 from the MSB
        let shift = (NIBBLES - self.len()) * 4;

        // Extract the nibble - after shifting right, it's in the lowest bits of limb 0
        let nibble = (self.nibbles >> shift).as_limbs()[0] as u8 & 0xF;

        // Clear the nibble using a more efficient mask creation
        // Instead of U256::from(0xF_u8) << shift, we can create the mask directly
        let mask_limb_idx = shift / 64;
        let mask_shift = shift % 64;

        if mask_limb_idx < 4 {
            // SAFETY: We know the limb index is valid
            unsafe {
                let limbs = self.nibbles.as_limbs_mut();
                limbs[mask_limb_idx] &= !(0xF << mask_shift);
            }
        }

        self.len -= 1;
        Some(nibble)
    }

    /// Extend the current nibbles with another nibbles.
    pub fn extend(&mut self, other: &Nibbles) {
        self.extend_check(other.len());
        if other.is_empty() {
            return;
        }

        self.nibbles |= other.nibbles >> self.bit_len();
        self.len += other.len;
    }

    /// Extend the current nibbles with another byte slice.
    pub fn extend_from_slice(&mut self, other: &[u8]) {
        self.extend_check(other.len() * 2);
        self.extend_from_slice_unchecked(other);
    }

    #[inline]
    #[cfg_attr(debug_assertions, track_caller)]
    fn extend_check(&self, other_len: usize) {
        assert!(
            self.len() + other_len <= NIBBLES,
            "Cannot extend: resulting length would exceed maximum capacity"
        );
    }

    /// Extend the current nibbles with another byte slice.
    ///
    /// # Caution
    ///
    /// This method does not check if the resulting length would exceed the maximum capacity of
    /// [`Nibbles`].
    pub fn extend_from_slice_unchecked(&mut self, other: &[u8]) {
        if other.is_empty() {
            return;
        }

        let len_bytes = other.len();
        let mut other = U256::from_be_slice(other);
        if len_bytes > 0 {
            other <<= (U256::BYTES - len_bytes) * 8;
        }
        self.nibbles |= other >> self.bit_len();
        self.len += len_bytes * 2;
    }

    /// Truncates the current nibbles to the given length.
    #[inline]
    pub fn truncate(&mut self, new_len: usize) {
        assert!(
            new_len <= self.len(),
            "Cannot truncate to a length greater than the current length"
        );
        *self = self.slice_unchecked(0, new_len);
    }

    /// Clears the current nibbles.
    #[inline]
    pub const fn clear(&mut self) {
        *self = Self::new();
    }

    /// Checks if the given index is within the bounds of the current nibbles.
    #[inline]
    const fn check_index(&self, i: usize) -> bool {
        i < self.len()
    }

    /// Panics if the given index is out of bounds of the current nibbles.
    #[inline]
    fn assert_index(&self, i: usize) {
        let len = self.len();
        if i >= len {
            panic_invalid_index(len, i);
        }
    }

    #[inline]
    const fn as_array(&self) -> Option<&AsArray> {
        cfg_if! {
            if #[cfg(target_pointer_width = "64")] {
                // SAFETY: `#[repr(C)]` guarantees memory layout,
                // and 64 bit usize means this struct is exactly 5 u64s.
                const {
                    assert!(size_of::<Self>() == size_of::<AsArray>());
                    assert!(align_of::<Self>() >= align_of::<AsArray>());
                }
                Some(unsafe { &*(self as *const Self as *const AsArray) })
            } else {
                None
            }
        }
    }
}

/// Iterator over individual nibbles within a [`Nibbles`] structure.
///
/// This iterator provides efficient access to each nibble in sequence,
/// using unchecked access for performance.
///
/// # Examples
///
/// ```
/// # use nybbles::Nibbles;
/// let nibbles = Nibbles::from_nibbles(&[0x0A, 0x0B, 0x0C, 0x0D]);
/// let collected: Vec<u8> = nibbles.iter().collect();
/// assert_eq!(collected, vec![0x0A, 0x0B, 0x0C, 0x0D]);
/// ```
#[derive(Debug, Clone)]
pub struct NibblesIter<'a> {
    /// Current position in the iteration.
    current: usize,
    /// Reference to the nibbles being iterated over.
    nibbles: &'a Nibbles,
}

impl<'a> Iterator for NibblesIter<'a> {
    type Item = u8;

    #[inline]
    fn next(&mut self) -> Option<Self::Item> {
        self.nibbles.get(self.current).inspect(|_| self.current += 1)
    }

    #[inline]
    fn size_hint(&self) -> (usize, Option<usize>) {
        let len = self.len();
        (len, Some(len))
    }
}

impl<'a> ExactSizeIterator for NibblesIter<'a> {
    #[inline]
    fn len(&self) -> usize {
        self.nibbles.len() - self.current
    }
}

/// Packs the nibbles into the given slice without checking its length.
///
/// # Safety
///
/// `out` must be valid for at least `(self.len() + 1) / 2` bytes.
#[inline]
unsafe fn pack_to_unchecked(nibbles: &Nibbles, out: &mut [MaybeUninit<u8>]) {
    let byte_len = nibbles.byte_len();
    debug_assert!(out.len() >= byte_len);
    // Move source pointer to the end of the little endian slice
    let sl = as_le_slice(&nibbles.nibbles);
    let mut src = unsafe { sl.as_ptr().add(U256::BYTES) };
    // Destination pointer is at the beginning of the output slice
    let mut dst = out.as_mut_ptr().cast::<u8>();
    // On each iteration, decrement the source pointer by one, set the destination byte, and
    // increment the destination pointer by one
    for _ in 0..byte_len {
        unsafe {
            src = src.sub(1);
            *dst = *src;
            dst = dst.add(1);
        }
    }
}

/// Initializes a smallvec with the given length and a closure that initializes the buffer.
///
/// Optimized version of `SmallVec::with_capacity` + `f()` + `.set_len`.
///
/// # Safety
///
/// The closure must fully initialize the buffer with the given length.
#[inline]
pub unsafe fn smallvec_with<const N: usize>(
    len: usize,
    f: impl FnOnce(&mut [MaybeUninit<u8>]),
) -> SmallVec<[u8; N]> {
    let mut buf = unsafe { smallvec_with_len::<N>(len) };
    f(unsafe { slice::from_raw_parts_mut(buf.as_mut_ptr().cast(), len) });
    buf
}

#[inline]
#[allow(clippy::uninit_vec)]
unsafe fn smallvec_with_len<const N: usize>(len: usize) -> SmallVec<[u8; N]> {
    if likely(len <= N) {
        unsafe { SmallVec::from_buf_and_len_unchecked(MaybeUninit::<[u8; N]>::uninit(), len) }
    } else {
        let mut vec = Vec::with_capacity(len);
        unsafe { vec.set_len(len) };
        SmallVec::from_vec(vec)
    }
}

#[inline]
#[track_caller]
fn check_nibbles(nibbles: &[u8]) {
    if !valid_nibbles(nibbles) {
        panic_invalid_nibbles();
    }
}

fn valid_nibbles(nibbles: &[u8]) -> bool {
    nibbles.iter().all(|&nibble| nibble <= 0xf)
}

#[cold]
#[inline(never)]
#[cfg_attr(debug_assertions, track_caller)]
const fn panic_invalid_nibbles() -> ! {
    panic!("attempted to create invalid nibbles");
}

#[cold]
#[inline(never)]
#[cfg_attr(debug_assertions, track_caller)]
fn panic_invalid_index(len: usize, i: usize) -> ! {
    panic!("index out of bounds: {i} for nibbles of length {len}");
}

#[cold]
#[inline(never)]
#[cfg_attr(debug_assertions, track_caller)]
const fn panic_invalid_slice(start: usize, end: usize, len: usize) -> ! {
    assert!(start <= end, "Cannot slice with a start index greater than the end index");
    assert!(end <= len, "Cannot slice with an end index greater than the length of the nibbles");
    unreachable!()
}

/// Internal container for owned/borrowed byte slices.
enum ByteContainer<'a, const N: usize> {
    /// Borrowed variant holds a reference to a slice of bytes.
    #[cfg_attr(target_endian = "big", allow(unused))]
    Borrowed(&'a [u8]),
    /// Owned variant holds a fixed-size array of bytes.
    #[cfg_attr(target_endian = "little", allow(unused))]
    Owned([u8; N]),
}

impl<'a, const N: usize> ByteContainer<'a, N> {
    /// Returns a mutable reference to the underlying byte array, converting from borrowed to owned
    /// if necessary.
    ///
    /// ## Panics
    /// Panics if the current variant is `Borrowed` and the slice length is less than `N`.
    #[cfg_attr(target_endian = "little", allow(unused))]
    #[inline]
    pub(crate) fn to_mut(&mut self) -> &mut [u8; N] {
        match self {
            ByteContainer::Borrowed(slice) => {
                let mut array = [0u8; N];
                array[..N].copy_from_slice(&slice[..N]);
                *self = ByteContainer::Owned(array);
                self.to_mut()
            }
            ByteContainer::Owned(array) => array,
        }
    }
}

impl<'a, const N: usize> Deref for ByteContainer<'a, N> {
    type Target = [u8];

    #[inline]
    fn deref(&self) -> &Self::Target {
        match self {
            ByteContainer::Borrowed(slice) => slice,
            ByteContainer::Owned(array) => array.as_slice(),
        }
    }
}

/// Returns a little-endian byte slice representation of the given [`U256`] value.
#[inline]
const fn as_le_slice(x: &U256) -> ByteContainer<'_, { U256::BYTES }> {
    cfg_if! {
        if #[cfg(target_endian = "little")] {
            ByteContainer::Borrowed(x.as_le_slice())
        } else {
            ByteContainer::Owned(x.to_le_bytes())
        }
    }
}

#[inline]
fn longest_prefix_byte(a: &U256, b: &U256) -> usize {
    longest_prefix::<false>(a, b) / 8
}

#[inline]
fn longest_prefix_bit(a: &U256, b: &U256) -> usize {
    longest_prefix::<true>(a, b)
}

#[inline]
fn longest_prefix<const EXACT: bool>(a: &U256, b: &U256) -> usize {
    cfg_if! {
        if #[cfg(all(target_arch = "x86_64", target_feature = "avx2"))] {
            return unsafe {
                use core::arch::x86_64::*;
                let x = _mm256_loadu_si256(a.as_limbs().as_ptr().cast());
                let y = _mm256_loadu_si256(b.as_limbs().as_ptr().cast());
                let diff = _mm256_cmpeq_epi8(x, y);
                let mask = _mm256_movemask_epi8(diff);
                let bytes = mask.leading_ones() as usize;
                if !EXACT || bytes == 32 {
                    return bytes * 8;
                }
                let le_idx = 31 - bytes;
                let a = *a.as_le_slice().get_unchecked(le_idx);
                let b = *b.as_le_slice().get_unchecked(le_idx);
                let diff = a ^ b;
                let bits = diff.leading_zeros() as usize;
                bytes * 8 + bits
            };
        }
    }

    let diff = *a ^ *b;
    diff.leading_zeros()
}

#[inline]
#[allow(clippy::manual_div_ceil)] // Div_ceil has superfluous overflow check.
const fn byte_len(nibble_len: usize) -> usize {
    (nibble_len + 1) / 2
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn pack() {
        let tests = [
            (&[][..], &[][..]),
            (&[0xa], &[0xa0]),
            (&[0xa, 0x0], &[0xa0]),
            (&[0xa, 0xb], &[0xab]),
            (&[0xa, 0xb, 0x2], &[0xab, 0x20]),
            (&[0xa, 0xb, 0x2, 0x0], &[0xab, 0x20]),
            (&[0xa, 0xb, 0x2, 0x7], &[0xab, 0x27]),
        ];
        for (input, expected) in tests {
            assert!(valid_nibbles(input));
            let nibbles = Nibbles::from_nibbles(input);
            let encoded = nibbles.pack();
            assert_eq!(
                &encoded[..],
                expected,
                "input: {input:x?}, expected: {expected:x?}, got: {encoded:x?}",
            );
        }
    }

    #[test]
    fn get_unchecked() {
        for len in 0..NIBBLES {
            let raw = (0..16).cycle().take(len).collect::<Vec<u8>>();
            let nibbles = Nibbles::from_nibbles(&raw);
            for (i, raw_nibble) in raw.into_iter().enumerate() {
                assert_eq!(nibbles.get_unchecked(i), raw_nibble);
            }
        }
    }

    #[test]
    fn set_at_unchecked() {
        for len in 0..=NIBBLES {
            let raw = (0..16).cycle().take(len).collect::<Vec<u8>>();
            let mut nibbles = Nibbles::from_nibbles(&raw);
            for (i, raw_nibble) in raw.iter().enumerate() {
                let new_nibble = (raw_nibble + 1) % 16;
                unsafe { nibbles.set_at_unchecked(i, new_nibble) };

                let mut new_raw_nibbles = nibbles.clone().to_vec();
                new_raw_nibbles[i] = new_nibble;
                let new_nibbles = Nibbles::from_nibbles(&new_raw_nibbles);
                assert_eq!(nibbles, new_nibbles,);
            }
        }
    }

    #[test]
    fn push_pop() {
        let mut nibbles = Nibbles::new();
        nibbles.push(0x0A);
        assert_eq!(nibbles.get_unchecked(0), 0x0A);
        assert_eq!(nibbles.len(), 1);

        assert_eq!(nibbles.pop(), Some(0x0A));
        assert_eq!(nibbles.len(), 0);
    }

    #[test]
    fn get_byte() {
        let nibbles = Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D]);
        assert_eq!(nibbles.get_byte(0), Some(0xAB));
        assert_eq!(nibbles.get_byte(1), Some(0xBC));
        assert_eq!(nibbles.get_byte(2), Some(0xCD));
        assert_eq!(nibbles.get_byte(3), None);
        assert_eq!(nibbles.get_byte(usize::MAX), None);
    }

    #[test]
    fn get_byte_unchecked() {
        let nibbles = Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D]);
        assert_eq!(nibbles.get_byte_unchecked(0), 0xAB);
        assert_eq!(nibbles.get_byte_unchecked(1), 0xBC);
        assert_eq!(nibbles.get_byte_unchecked(2), 0xCD);
    }

    #[test]
    fn clone() {
        let a = Nibbles::from_nibbles([1, 2, 3]);
        #[allow(clippy::redundant_clone)]
        let b = a;
        assert_eq!(a, b);
    }

    #[test]
    fn ord() {
        // Test empty nibbles
        let nibbles1 = Nibbles::default();
        let nibbles2 = Nibbles::default();
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Equal);

        // Test with one empty
        let nibbles1 = Nibbles::default();
        let nibbles2 = Nibbles::from_nibbles([0]);
        assert_eq!(nibbles2.cmp(&nibbles1), Ordering::Greater);

        // Test with same nibbles
        let nibbles1 = Nibbles::unpack([0x12, 0x34]);
        let nibbles2 = Nibbles::unpack([0x12, 0x34]);
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Equal);

        // Test with different lengths
        let short = Nibbles::unpack([0x12]);
        let long = Nibbles::unpack([0x12, 0x34]);
        assert_eq!(short.cmp(&long), Ordering::Less);

        // Test with common prefix but different values
        let nibbles1 = Nibbles::unpack([0x12, 0x34]);
        let nibbles2 = Nibbles::unpack([0x12, 0x35]);
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Less);

        // Test with differing first byte
        let nibbles1 = Nibbles::unpack([0x12, 0x34]);
        let nibbles2 = Nibbles::unpack([0x13, 0x34]);
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Less);

        // Test with odd length nibbles
        let nibbles1 = Nibbles::unpack([0x1]);
        let nibbles2 = Nibbles::unpack([0x2]);
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Less);

        // Test with odd and even length nibbles
        let odd = Nibbles::unpack([0x1]);
        let even = Nibbles::unpack([0x12]);
        assert_eq!(odd.cmp(&even), Ordering::Less);

        // Test with longer sequences
        let nibbles1 = Nibbles::unpack([0x12, 0x34, 0x56, 0x78]);
        let nibbles2 = Nibbles::unpack([0x12, 0x34, 0x56, 0x79]);
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Less);

        let nibbles1 = Nibbles::from_nibbles([0x0, 0x0]);
        let nibbles2 = Nibbles::from_nibbles([0x1]);
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Less);

        let nibbles1 = Nibbles::from_nibbles([0x1]);
        let nibbles2 = Nibbles::from_nibbles([0x0, 0x2]);
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Greater);

        let nibbles1 = Nibbles::from_nibbles([vec![0; 61], vec![1; 1], vec![0; 1]].concat());
        let nibbles2 = Nibbles::from_nibbles([vec![0; 61], vec![1; 1], vec![0; 2]].concat());
        assert_eq!(nibbles1.cmp(&nibbles2), Ordering::Less);
    }

    #[test]
    fn starts_with() {
        let nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);

        // Test empty nibbles
        let empty = Nibbles::default();
        assert!(nibbles.starts_with(&empty));
        assert!(empty.starts_with(&empty));
        assert!(!empty.starts_with(&nibbles));

        // Test with same nibbles
        assert!(nibbles.starts_with(&nibbles));

        // Test with prefix
        let prefix = Nibbles::from_nibbles([1, 2]);
        assert!(nibbles.starts_with(&prefix));
        assert!(!prefix.starts_with(&nibbles));

        // Test with different first nibble
        let different = Nibbles::from_nibbles([2, 2, 3, 4]);
        assert!(!nibbles.starts_with(&different));

        // Test with longer sequence
        let longer = Nibbles::from_nibbles([1, 2, 3, 4, 5, 6]);
        assert!(!nibbles.starts_with(&longer));

        // Test with even nibbles and odd prefix
        let even_nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);
        let odd_prefix = Nibbles::from_nibbles([1, 2, 3]);
        assert!(even_nibbles.starts_with(&odd_prefix));

        // Test with odd nibbles and even prefix
        let odd_nibbles = Nibbles::from_nibbles([1, 2, 3]);
        let even_prefix = Nibbles::from_nibbles([1, 2]);
        assert!(odd_nibbles.starts_with(&even_prefix));
    }

    #[test]
    fn ends_with() {
        let nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);

        // Test empty nibbles
        let empty = Nibbles::default();
        assert!(nibbles.ends_with(&empty));
        assert!(empty.ends_with(&empty));
        assert!(!empty.ends_with(&nibbles));

        // Test with same nibbles
        assert!(nibbles.ends_with(&nibbles));

        // Test with suffix
        let suffix = Nibbles::from_nibbles([3, 4]);
        assert!(nibbles.ends_with(&suffix));
        assert!(!suffix.ends_with(&nibbles));

        // Test with different last nibble
        let different = Nibbles::from_nibbles([2, 3, 5]);
        assert!(!nibbles.ends_with(&different));

        // Test with longer sequence
        let longer = Nibbles::from_nibbles([2, 3, 4, 5, 6]);
        assert!(!nibbles.ends_with(&longer));

        // Test with even nibbles and odd suffix
        let even_nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);
        let odd_suffix = Nibbles::from_nibbles([2, 3, 4]);
        assert!(even_nibbles.ends_with(&odd_suffix));

        // Test with odd nibbles and even suffix
        let odd_nibbles = Nibbles::from_nibbles([1, 2, 3]);
        let even_suffix = Nibbles::from_nibbles([2, 3]);
        assert!(odd_nibbles.ends_with(&even_suffix));
    }

    #[test]
    fn slice() {
        // Test with empty nibbles
        let empty = Nibbles::default();
        assert_eq!(empty.slice(..), empty);

        // Test with even number of nibbles
        let even = Nibbles::from_nibbles([0, 1, 2, 3, 4, 5]);

        // Full slice
        assert_eq!(even.slice(..), even);
        assert_eq!(even.slice(0..6), even);

        // Empty slice
        assert_eq!(even.slice(3..3), Nibbles::default());

        // Beginning slices (even start)
        assert_eq!(even.slice(0..2), Nibbles::from_iter(0..2));

        // Middle slices (even start, even end)
        assert_eq!(even.slice(2..4), Nibbles::from_iter(2..4));

        // End slices (even start)
        assert_eq!(even.slice(4..6), Nibbles::from_iter(4..6));

        // Test with odd number of nibbles
        let odd = Nibbles::from_iter(0..5);

        // Full slice
        assert_eq!(odd.slice(..), odd);
        assert_eq!(odd.slice(0..5), odd);

        // Beginning slices (odd length)
        assert_eq!(odd.slice(0..3), Nibbles::from_iter(0..3));

        // Middle slices with odd start
        assert_eq!(odd.slice(1..4), Nibbles::from_iter(1..4));

        // Middle slices with odd end
        assert_eq!(odd.slice(1..3), Nibbles::from_iter(1..3));

        // End slices (odd start)
        assert_eq!(odd.slice(2..5), Nibbles::from_iter(2..5));

        // Special cases - both odd start and end
        assert_eq!(odd.slice(1..4), Nibbles::from_iter(1..4));

        // Single nibble slices
        assert_eq!(even.slice(2..3), Nibbles::from_iter(2..3));

        assert_eq!(even.slice(3..4), Nibbles::from_iter(3..4));

        // Test with alternate syntax
        assert_eq!(even.slice(2..), Nibbles::from_iter(2..6));
        assert_eq!(even.slice(..4), Nibbles::from_iter(0..4));
        assert_eq!(even.slice(..=3), Nibbles::from_iter(0..4));

        // More complex test case with the max length array sliced at the end
        assert_eq!(
            Nibbles::from_iter((0..16).cycle().take(64)).slice(1..),
            Nibbles::from_iter((0..16).cycle().take(64).skip(1))
        );
    }

    #[test]
    fn common_prefix_length() {
        // Test with empty nibbles
        let empty = Nibbles::default();
        assert_eq!(empty.common_prefix_length(&empty), 0);

        // Test with same nibbles
        let nibbles1 = Nibbles::from_nibbles([1, 2, 3, 4]);
        let nibbles2 = Nibbles::from_nibbles([1, 2, 3, 4]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 4);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 4);

        // Test with partial common prefix (byte aligned)
        let nibbles1 = Nibbles::from_nibbles([1, 2, 3, 4]);
        let nibbles2 = Nibbles::from_nibbles([1, 2, 5, 6]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 2);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 2);

        // Test with partial common prefix (half-byte aligned)
        let nibbles1 = Nibbles::from_nibbles([1, 2, 3, 4]);
        let nibbles2 = Nibbles::from_nibbles([1, 2, 3, 7]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 3);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 3);

        // Test with no common prefix
        let nibbles1 = Nibbles::from_nibbles([5, 6, 7, 8]);
        let nibbles2 = Nibbles::from_nibbles([1, 2, 3, 4]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 0);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 0);

        // Test with different lengths but common prefix
        let nibbles1 = Nibbles::from_nibbles([1, 2, 3, 4, 5, 6]);
        let nibbles2 = Nibbles::from_nibbles([1, 2, 3]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 3);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 3);

        // Test with odd number of nibbles
        let nibbles1 = Nibbles::from_nibbles([1, 2, 3]);
        let nibbles2 = Nibbles::from_nibbles([1, 2, 7]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 2);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 2);

        // Test with half-byte difference in first byte
        let nibbles1 = Nibbles::from_nibbles([1, 2, 3, 4]);
        let nibbles2 = Nibbles::from_nibbles([5, 2, 3, 4]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 0);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 0);

        // Test with one empty and one non-empty
        let nibbles1 = Nibbles::from_nibbles([1, 2, 3, 4]);
        assert_eq!(nibbles1.common_prefix_length(&empty), 0);
        assert_eq!(empty.common_prefix_length(&nibbles1), 0);

        // Test with longer sequences (16 nibbles)
        let nibbles1 =
            Nibbles::from_nibbles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]);
        let nibbles2 =
            Nibbles::from_nibbles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 15);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 15);

        // Test with different lengths but same prefix (32 vs 16 nibbles)
        let nibbles1 =
            Nibbles::from_nibbles([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0]);
        let nibbles2 = Nibbles::from_nibbles([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 0,
        ]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 16);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 16);

        // Test with very long sequences (32 nibbles) with different endings
        let nibbles1 = Nibbles::from_nibbles([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 0,
        ]);
        let nibbles2 = Nibbles::from_nibbles([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 1,
        ]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 31);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 31);

        // Test with 48 nibbles with different endings
        let nibbles1 = Nibbles::from_nibbles([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0,
        ]);
        let nibbles2 = Nibbles::from_nibbles([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1,
        ]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 47);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 47);

        // Test with 64 nibbles with different endings
        let nibbles1 = Nibbles::from_nibbles([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3,
            4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0,
        ]);
        let nibbles2 = Nibbles::from_nibbles([
            1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
            11, 12, 13, 14, 15, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 0, 1, 2, 3,
            4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1,
        ]);
        assert_eq!(nibbles1.common_prefix_length(&nibbles2), 63);
        assert_eq!(nibbles2.common_prefix_length(&nibbles1), 63);

        let current = Nibbles::from_nibbles([0u8; 64]);
        let path = Nibbles::from_nibbles([vec![0u8; 63], vec![2u8]].concat());
        assert_eq!(current.common_prefix_length(&path), 63);

        let current = Nibbles::from_nibbles([0u8; 63]);
        let path = Nibbles::from_nibbles([vec![0u8; 62], vec![1u8], vec![0u8]].concat());
        assert_eq!(current.common_prefix_length(&path), 62);
    }

    #[test]
    fn truncate() {
        // Test truncating empty nibbles
        let mut nibbles = Nibbles::default();
        nibbles.truncate(0);
        assert_eq!(nibbles, Nibbles::default());

        // Test truncating to zero length
        let mut nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);
        nibbles.truncate(0);
        assert_eq!(nibbles, Nibbles::default());

        // Test truncating to same length (should be no-op)
        let mut nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);
        nibbles.truncate(4);
        assert_eq!(nibbles, Nibbles::from_nibbles([1, 2, 3, 4]));

        // Individual nibble test with a simple 2-nibble truncation
        let mut nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);
        nibbles.truncate(2);
        assert_eq!(nibbles, Nibbles::from_nibbles([1, 2]));

        // Test simple truncation
        let mut nibbles = Nibbles::from_nibbles([1, 2, 3, 4]);
        nibbles.truncate(2);
        assert_eq!(nibbles, Nibbles::from_nibbles([1, 2]));

        // Test truncating to single nibble
        let mut nibbles = Nibbles::from_nibbles([5, 6, 7, 8]);
        nibbles.truncate(1);
        assert_eq!(nibbles, Nibbles::from_nibbles([5]));
    }

    #[test]
    fn next_without_prefix() {
        let test_cases: Vec<(Nibbles, Option<Nibbles>)> = vec![
            // Simple increment, with no trailing zeros
            (Nibbles::from_nibbles([0x1, 0x2, 0x3]), Some(Nibbles::from_nibbles([0x1, 0x2, 0x4]))),
            // Trailing zeros
            (Nibbles::from_nibbles([0x0, 0x0, 0xF]), Some(Nibbles::from_nibbles([0x0, 0x1]))),
            (Nibbles::from_nibbles([0x0, 0xF, 0xF]), Some(Nibbles::from_nibbles([0x1]))),
            (Nibbles::from_nibbles([0xE, 0xF, 0xF]), Some(Nibbles::from_nibbles([0xF]))),
            (Nibbles::from_nibbles([0x1, 0x2, 0xF, 0xF]), Some(Nibbles::from_nibbles([0x1, 0x3]))),
            // Other Cases
            (Nibbles::from_nibbles([0xF; 64]), None),
            (Nibbles::from_nibbles([0xF]), None),
            (Nibbles::new(), None),
            (Nibbles::from_nibbles([0x0, 0xF, 0xF, 0xF, 0xF]), Some(Nibbles::from_nibbles([0x1]))),
        ];

        for (input, expected) in test_cases {
            let result = input.next_without_prefix();
            assert_eq!(result, expected, "Failed for input: {:?}", input);
        }
    }

    #[test]
    fn push_unchecked() {
        // Test pushing to empty nibbles
        let mut nibbles = Nibbles::default();
        nibbles.push_unchecked(0x5);
        assert_eq!(nibbles, Nibbles::from_nibbles([0x5]));

        // Test pushing a second nibble
        nibbles.push_unchecked(0xA);
        assert_eq!(nibbles, Nibbles::from_nibbles([0x5, 0xA]));

        // Test pushing multiple nibbles to build a sequence
        let mut nibbles = Nibbles::default();
        for nibble in [0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8] {
            nibbles.push_unchecked(nibble);
        }
        assert_eq!(nibbles, Nibbles::from_nibbles([0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8]));

        // Test pushing nibbles with values that exceed 4 bits (should be masked to 0x0F)
        let mut nibbles = Nibbles::default();
        nibbles.push_unchecked(0xFF); // Should become 0xF
        nibbles.push_unchecked(0x1A); // Should become 0xA
        nibbles.push_unchecked(0x25); // Should become 0x5
        assert_eq!(nibbles, Nibbles::from_nibbles([0xF, 0xA, 0x5]));

        // Test pushing to existing nibbles (adding to the end)
        let mut nibbles = Nibbles::from_nibbles([0x1, 0x2, 0x3]);
        nibbles.push_unchecked(0x4);
        assert_eq!(nibbles, Nibbles::from_nibbles([0x1, 0x2, 0x3, 0x4]));

        // Test boundary values (0 and 15)
        let mut nibbles = Nibbles::default();
        nibbles.push_unchecked(0x0);
        nibbles.push_unchecked(0xF);
        assert_eq!(nibbles, Nibbles::from_nibbles([0x0, 0xF]));

        // Test pushing many nibbles to verify no overflow issues
        let mut nibbles = Nibbles::default();
        let test_sequence: Vec<u8> = (0..32).map(|i| i % 16).collect();
        for &nibble in &test_sequence {
            nibbles.push_unchecked(nibble);
        }
        assert_eq!(nibbles, Nibbles::from_nibbles(test_sequence));
    }

    #[test]
    fn unpack() {
        for (test_idx, bytes) in (0..=32).map(|i| vec![0xFF; i]).enumerate() {
            let packed_nibbles = Nibbles::unpack(&bytes);
            let nibbles = Nibbles::unpack(&bytes);

            assert_eq!(
                packed_nibbles.len(),
                nibbles.len(),
                "Test case {test_idx}: Length mismatch for bytes {bytes:?}",
            );
            assert_eq!(
                packed_nibbles.len(),
                bytes.len() * 2,
                "Test case {test_idx}: Expected length to be 2x byte length",
            );

            // Compare each nibble individually
            for i in 0..packed_nibbles.len() {
                assert_eq!(
                    packed_nibbles.get_unchecked(i),
                    nibbles.get_unchecked(i),
                    "Test case {}: Nibble at index {} differs for bytes {:?}: Nibbles={:?}, Nibbles={:?}",
                    test_idx,
                    i,
                    bytes,
                    packed_nibbles.get_unchecked(i),
                    nibbles.get_unchecked(i)
                );
            }
        }

        let nibbles = Nibbles::unpack([0xAB, 0xCD]);
        assert_eq!(nibbles.to_vec(), vec![0x0A, 0x0B, 0x0C, 0x0D]);
    }

    #[test]
    fn increment() {
        // Test basic increment
        assert_eq!(
            Nibbles::from_nibbles([0x0, 0x0, 0x0]).increment().unwrap(),
            Nibbles::from_nibbles([0x0, 0x0, 0x1])
        );

        // Test increment with carry
        assert_eq!(
            Nibbles::from_nibbles([0x0, 0x0, 0xF]).increment().unwrap(),
            Nibbles::from_nibbles([0x0, 0x1, 0x0])
        );

        // Test multiple carries
        assert_eq!(
            Nibbles::from_nibbles([0x0, 0xF, 0xF]).increment().unwrap(),
            Nibbles::from_nibbles([0x1, 0x0, 0x0])
        );

        // Test increment from all F's except first nibble
        assert_eq!(
            Nibbles::from_nibbles([0xE, 0xF, 0xF]).increment().unwrap(),
            Nibbles::from_nibbles([0xF, 0x0, 0x0])
        );

        // Test overflow - all nibbles are 0xF
        assert_eq!(Nibbles::from_nibbles([0xF, 0xF, 0xF]).increment(), None);

        // Test empty nibbles
        assert_eq!(Nibbles::new().increment(), None);

        // Test single nibble
        assert_eq!(Nibbles::from_nibbles([0x5]).increment().unwrap(), Nibbles::from_nibbles([0x6]));

        // Test single nibble at max
        assert_eq!(Nibbles::from_nibbles([0xF]).increment(), None);

        // Test longer sequence
        assert_eq!(
            Nibbles::from_nibbles([0x1, 0x2, 0x3, 0x4, 0x5]).increment().unwrap(),
            Nibbles::from_nibbles([0x1, 0x2, 0x3, 0x4, 0x6])
        );

        // Test longer sequence with carries
        assert_eq!(
            Nibbles::from_nibbles([0x1, 0x2, 0x3, 0xF, 0xF]).increment().unwrap(),
            Nibbles::from_nibbles([0x1, 0x2, 0x4, 0x0, 0x0])
        );
    }

    #[test]
    fn from_str() {
        // Test empty string
        assert_eq!(Nibbles::from_str("0x").unwrap(), Nibbles::new());

        // Test single nibble
        assert_eq!(Nibbles::from_str("0x5").unwrap(), Nibbles::from_nibbles([0x5]));

        // Test multiple nibbles
        assert_eq!(
            Nibbles::from_str("0xabcd").unwrap(),
            Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D])
        );

        // Test odd nibbles
        assert_eq!(Nibbles::from_str("0xabc").unwrap(), Nibbles::from_nibbles([0x0A, 0x0B, 0x0C]));

        // Test leading zeros
        assert_eq!(
            Nibbles::from_str("0x0012").unwrap(),
            Nibbles::from_nibbles([0x0, 0x0, 0x1, 0x2])
        );

        // Test max nibbles
        let hex_str = format!("0x{}", "f".repeat(64));
        assert_eq!(Nibbles::from_str(&hex_str).unwrap(), Nibbles::from_nibbles([0xF; 64]));

        // Test missing prefix
        assert_eq!(Nibbles::from_str("abcd").unwrap_err(), "missing 0x prefix");

        // Test invalid hex character
        assert_eq!(Nibbles::from_str("0xghij").unwrap_err(), "invalid hex character");

        // Test too long
        let too_long = format!("0x{}", "f".repeat(65));
        assert_eq!(Nibbles::from_str(&too_long).unwrap_err(), "hex string too long");

        // Test uppercase hex characters
        assert_eq!(
            Nibbles::from_str("0xABCD").unwrap(),
            Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D])
        );

        // Test mixed case
        assert_eq!(
            Nibbles::from_str("0xAbCd").unwrap(),
            Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D])
        );
    }

    #[test]
    fn iter() {
        // Test empty nibbles
        let empty = Nibbles::new();
        assert!(empty.iter().collect::<Vec<_>>().is_empty());

        // Test basic iteration
        let nibbles = Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D]);
        let collected: Vec<u8> = nibbles.iter().collect();
        assert_eq!(collected, vec![0x0A, 0x0B, 0x0C, 0x0D]);

        // Test that iter() produces same result as to_vec()
        assert_eq!(nibbles.iter().collect::<Vec<_>>(), nibbles.to_vec());

        // Test single nibble
        let single = Nibbles::from_nibbles([0x05]);
        assert_eq!(single.iter().collect::<Vec<_>>(), vec![0x05]);

        // Test odd number of nibbles
        let odd = Nibbles::from_nibbles([0x01, 0x02, 0x03]);
        assert_eq!(odd.iter().collect::<Vec<_>>(), vec![0x01, 0x02, 0x03]);

        // Test max length nibbles
        let max_nibbles: Vec<u8> = (0..64).map(|i| (i % 16) as u8).collect();
        let max = Nibbles::from_nibbles(&max_nibbles);
        assert_eq!(max.iter().collect::<Vec<_>>(), max_nibbles);

        // Test iterator size_hint and len
        let nibbles = Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D]);
        let mut iter = nibbles.iter();
        assert_eq!(iter.len(), 4);
        assert_eq!(iter.size_hint(), (4, Some(4)));

        iter.next();
        assert_eq!(iter.len(), 3);
        assert_eq!(iter.size_hint(), (3, Some(3)));

        iter.next();
        iter.next();
        assert_eq!(iter.len(), 1);
        assert_eq!(iter.size_hint(), (1, Some(1)));

        iter.next();
        assert_eq!(iter.len(), 0);
        assert_eq!(iter.size_hint(), (0, Some(0)));
        assert_eq!(iter.next(), None);

        // Test cloning iterator
        let nibbles = Nibbles::from_nibbles([0x01, 0x02, 0x03, 0x04]);
        let mut iter1 = nibbles.iter();
        iter1.next();
        let iter2 = iter1.clone();

        assert_eq!(iter1.collect::<Vec<_>>(), vec![0x02, 0x03, 0x04]);
        assert_eq!(iter2.collect::<Vec<_>>(), vec![0x02, 0x03, 0x04]);
    }

    #[cfg(feature = "arbitrary")]
    mod arbitrary {
        use super::*;
        use proptest::{collection::vec, prelude::*};

        proptest::proptest! {
            #[test]
            #[cfg_attr(miri, ignore = "no proptest")]
            fn pack_unpack_roundtrip(input in vec(any::<u8>(), 0..32)) {
                let nibbles = Nibbles::unpack(&input);
                prop_assert!(valid_nibbles(&nibbles.to_vec()));
                let packed = nibbles.pack();
                prop_assert_eq!(&packed[..], input);
            }
        }
    }

    #[cfg(feature = "serde")]
    mod serde_tests {
        use super::*;
        use crate::alloc::string::ToString;

        #[test]
        fn serde_empty() {
            let nibbles = Nibbles::new();
            let serialized = serde_json::to_string(&nibbles).unwrap();
            assert_eq!(serialized, r#""0x""#);

            let deserialized: Nibbles = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, nibbles);
        }

        #[test]
        fn serde_single_nibble() {
            let nibbles = Nibbles::from_nibbles([0x5]);
            let serialized = serde_json::to_string(&nibbles).unwrap();
            assert_eq!(serialized, r#""0x5""#);

            let deserialized: Nibbles = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, nibbles);
        }

        #[test]
        fn serde_multiple_nibbles() {
            let nibbles = Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D]);
            let serialized = serde_json::to_string(&nibbles).unwrap();
            assert_eq!(serialized, r#""0xabcd""#);

            let deserialized: Nibbles = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, nibbles);
        }

        #[test]
        fn serde_odd_nibbles() {
            let nibbles = Nibbles::from_nibbles([0x0A, 0x0B, 0x0C]);
            let serialized = serde_json::to_string(&nibbles).unwrap();
            assert_eq!(serialized, r#""0xabc""#);

            let deserialized: Nibbles = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, nibbles);
        }

        #[test]
        fn serde_leading_zeros() {
            let nibbles = Nibbles::from_nibbles([0x0, 0x0, 0x1, 0x2]);
            let serialized = serde_json::to_string(&nibbles).unwrap();
            assert_eq!(serialized, r#""0x0012""#);

            let deserialized: Nibbles = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, nibbles);
        }

        #[test]
        fn serde_max_nibbles() {
            let nibbles = Nibbles::from_nibbles([0xF; 64]);
            let serialized = serde_json::to_string(&nibbles).unwrap();
            assert_eq!(
                serialized,
                r#""0xffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff""#
            );

            let deserialized: Nibbles = serde_json::from_str(&serialized).unwrap();
            assert_eq!(deserialized, nibbles);
        }

        #[test]
        fn deserialize_missing_prefix() {
            let result: Result<Nibbles, _> = serde_json::from_str(r#""abcd""#);
            assert!(result.is_err());
            assert!(result.unwrap_err().to_string().contains("missing 0x prefix"));
        }

        #[test]
        fn deserialize_invalid_hex() {
            let result: Result<Nibbles, _> = serde_json::from_str(r#""0xghij""#);
            assert!(result.is_err());
            assert!(result.unwrap_err().to_string().contains("invalid hex character"));
        }

        #[test]
        fn deserialize_too_long() {
            let too_long = format!(r#""0x{}""#, "f".repeat(65));
            let result: Result<Nibbles, _> = serde_json::from_str(&too_long);
            assert!(result.is_err());
            assert!(result.unwrap_err().to_string().contains("hex string too long"));
        }

        #[test]
        fn serde_from_object() {
            // Test deserializing when the Nibbles is embedded in an object
            #[derive(serde::Serialize, serde::Deserialize)]
            struct TestStruct {
                nibbles: Nibbles,
            }

            let original = TestStruct { nibbles: Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D]) };
            let json = serde_json::to_string(&original).unwrap();
            let deserialized: TestStruct = serde_json::from_str(&json).unwrap();
            assert_eq!(deserialized.nibbles, original.nibbles);
        }

        #[test]
        fn serde_from_parsed_value() {
            // Test deserializing from a pre-parsed JSON value
            let json_str = r#"{"nibbles": "0xabcd"}"#;
            let value: serde_json::Value = serde_json::from_str(json_str).unwrap();
            let nibbles: Nibbles = serde_json::from_value(value["nibbles"].clone()).unwrap();
            assert_eq!(nibbles, Nibbles::from_nibbles([0x0A, 0x0B, 0x0C, 0x0D]));
        }
    }
}