entviz 0.15.1

Visualize high-entropy values as comparable SVG diagrams (entviz, spec v15).
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
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
//! Format-specific entropy parsing (port of `src/entviz/entropy.py`).
//!
//! `parse()` dispatches over the registered parsers in order (order is
//! semantics) and returns the first match, or falls back to disproof-based
//! alphabet detection. The pipeline re-encodes to base64url only when this
//! returns `None`. A hard parse error (EIP-55 checksum failure) aborts the
//! whole render — the conformance contract rejects that input.

use crate::keccak::keccak256_hex;
use crate::Alphabet;

// --------------------------------------------------------------------------
// Alphabets (mirror entropy.py)
// --------------------------------------------------------------------------
// HEX and BASE64URL are the crate-root canonical alphabet constants (used by
// the shared core in lib.rs). Re-export them here so the parsers reference a
// single definition rather than a second copy (MNT-F3). `crate::entropy::HEX`
// / `crate::entropy::BASE64URL` remain valid paths for existing call sites.
pub use crate::{BASE64URL, HEX};

pub const BASE58: Alphabet = Alphabet {
    name: "base58",
    chars: "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz",
    bits_per_char: 6,
};
pub const BASE64: Alphabet = Alphabet {
    name: "base64",
    chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
    bits_per_char: 6,
};
pub const BASE32: Alphabet = Alphabet {
    name: "base32",
    chars: "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567",
    bits_per_char: 5,
};
pub const BECH32: Alphabet = Alphabet {
    name: "bech32",
    chars: "qpzry9x8gf2tvdw0s3jn54khce6mua7l",
    bits_per_char: 5,
};
pub const CROCKFORD32: Alphabet = Alphabet {
    name: "crockford32",
    chars: "0123456789ABCDEFGHJKMNPQRSTVWXYZ",
    bits_per_char: 5,
};
pub const BASE36: Alphabet = Alphabet {
    name: "base36",
    chars: "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
    bits_per_char: 6,
};
pub const DECIMAL: Alphabet = Alphabet {
    name: "decimal",
    chars: "0123456789",
    bits_per_char: 4,
};
const HEX_CHARS: &str = "0123456789abcdef";
const BASE58_CHARS: &str = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
const BECH32_CHARS: &str = "qpzry9x8gf2tvdw0s3jn54khce6mua7l";
const BASE32_CHARS_UP: &str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ234567";

#[derive(Debug, Clone)]
pub struct Parsed {
    pub type_name: String,
    pub alphabet: Alphabet,
    pub prefix: Option<String>,
    pub core: String,
    pub suffix: Option<String>,
    pub prefix_semantic: bool,
}

impl Parsed {
    fn new(
        type_name: &str,
        alphabet: Alphabet,
        prefix: Option<String>,
        core: String,
        suffix: Option<String>,
    ) -> Parsed {
        Parsed {
            type_name: type_name.to_string(),
            alphabet,
            prefix,
            core,
            suffix,
            prefix_semantic: false,
        }
    }
    fn semantic(mut self) -> Parsed {
        self.prefix_semantic = true;
        self
    }
}

#[derive(Debug)]
pub enum ParseError {
    /// EIP-55 mixed-case Ethereum checksum mismatch (`position` = first bad hex).
    Eip55 { position: usize },
    /// A base58check body (BTC/LTC legacy) whose 4-byte double-SHA256 checksum
    /// does not verify. The checksum is surfaced as the bottom-strip
    /// suffix, so a structural match with a bad checksum REJECTS.
    Base58Check,
    /// A bech32 address (BTC segwit `bc1`, LTC `ltc1`, Cardano Shelley, generic
    /// cosmos) whose BIP-173/BIP-350 polymod does not verify; rejected.
    Bech32Checksum,
    /// A CashAddr (Bitcoin Cash) whose 40-bit BCH polymod does not verify.
    CashAddrChecksum,
    /// A 20-char LEI (with the reserved `00`) whose MOD 97-10 check digits do
    /// not verify; rejected rather than falling through to a bare base36.
    LeiChecksum,
}

type PResult = Result<Option<Parsed>, ParseError>;

// --------------------------------------------------------------------------
// Small char-class helpers
// --------------------------------------------------------------------------
fn is_hex(s: &str) -> bool {
    !s.is_empty() && s.chars().all(|c| c.is_ascii_hexdigit())
}
fn all_in(s: &str, set: &str) -> bool {
    s.chars().all(|c| set.contains(c))
}
fn is_base58(s: &str) -> bool {
    !s.is_empty() && all_in(s, BASE58_CHARS)
}
fn is_bech32_either(s: &str) -> bool {
    !s.is_empty() && all_in(&s.to_lowercase(), BECH32_CHARS)
}
fn is_base32_either(s: &str) -> bool {
    !s.is_empty() && all_in(&s.to_uppercase(), BASE32_CHARS_UP)
}

// --------------------------------------------------------------------------
// Individual parsers
// --------------------------------------------------------------------------

fn parse_cesr(text: &str) -> PResult {
    // (code, label, total_len)
    const ONE: &[(&str, &str, usize)] = &[
        ("A", "Ed25519 seed", 44),
        ("B", "Ed25519 nt pubkey", 44),
        ("C", "X25519 pub enckey", 44),
        ("D", "Ed25519 pubkey", 44),
        ("E", "Blake3-256", 44),
        ("F", "Blake2b-256", 44),
        ("G", "Blake2s-256", 44),
        ("H", "SHA3-256", 44),
        ("I", "SHA2-256", 44),
        ("J", "secp256k1 seed", 44),
        ("K", "Ed448 seed", 76),
        ("L", "X448 pub enckey", 76),
        ("O", "X25519 priv deckey", 44),
        ("P", "X25519 124 cipher 44 seed", 124),
        ("Q", "secp256r1 seed", 44),
        ("a", "blinding factor", 44),
        ("c", "FN-DSA-512 seed", 44),
        ("d", "FN-DSA-1024 seed", 44),
        ("e", "FN-DSA-1024 sig", 1708),
        ("b", "FN-DSA-1024 pubkey", 2392),
    ];
    const TWO: &[(&str, &str, usize)] = &[
        ("0A", "random 128-bit number", 24),
        ("0B", "Ed25519 sig", 88),
        ("0C", "secp256k1 sig", 88),
        ("0D", "Blake3-512", 88),
        ("0E", "Blake2b-512", 88),
        ("0F", "SHA3-512", 88),
        ("0G", "SHA2-512", 88),
        ("0I", "secp256r1 sig", 88),
    ];
    const FOUR: &[(&str, &str, usize)] = &[
        ("1AAA", "secp256k1 nt pubkey", 48),
        ("1AAB", "secp256k1 pub/enc key", 48),
        ("1AAC", "Ed448 nt pubkey", 80),
        ("1AAD", "Ed448 pubkey", 80),
        ("1AAE", "Ed448 sig", 156),
        ("1AAH", "X25519 100 cipher 24 salt", 100),
        ("1AAI", "secp256r1 nt pubkey", 48),
        ("1AAJ", "secp256r1 pub/enc key", 48),
        ("1AAR", "FN-DSA-512 sig", 892),
        ("1AAQ", "FN-DSA-512 pubkey", 1200),
        // Dater (MtrDex.DateTime): an ISO-8601 datetime with `:`->`c`, `.`->`d`,
        // `+`->`p` substitutions, so the whole qb64 is base64url. Recognized only
        // to LABEL it correctly (not `raw`); a datetime is low-entropy and
        // directly human-readable, so `characterize` assigns it NO role (see
        // this.i:idxs1gs0). It is a Matter code with a fixed 4-char prefix +
        // fixed size, so it fits this flat table with no special path.
        ("1AAG", "datetime", 36),
    ];
    // CESR Indexer code table (keri.core.coring.IdrDex) — the indexed signatures
    // a KEL carries (controller/witness sigs). STRUCTURALLY DIFFERENT from the
    // Matter tables above: each qb64 is `hard-code + index + material`, so the
    // characters AFTER the hard code vary with the signature's index and CANNOT
    // be matched as a fixed leading substring. Recognition keys on (hard-code,
    // full-size `fs`) ONLY; the index chars stay in the core like any other body
    // chars (and so still drive the cells and the fingerprint). Every variant of
    // one algorithm — current-only ("Crt"), "Big" (2-char hard code, wider
    // index) — collapses to a single label. Ported wholesale from keripy's
    // IdrDex. Matter-vs-Indexer is decided by (code, length), never by leading
    // char alone: `A` is a Matter seed at 44 but an Indexer sig at 88, `0A`/`0B`
    // are Matter codes at 24/88 but Ed448 indexer sigs at 156. parse_cesr tries
    // Matter first, then this table. See issue #36, this.i:idxs1gs0.
    // (hard_code, label, fs)
    const INDEXER: &[(&str, &str, usize)] = &[
        ("A", "Ed25519 idx sig", 88),    // Ed25519_Sig
        ("B", "Ed25519 idx sig", 88),    // Ed25519_Crt_Sig
        ("C", "secp256k1 idx sig", 88),  // ECDSA_256k1_Sig
        ("D", "secp256k1 idx sig", 88),  // ECDSA_256k1_Crt_Sig
        ("E", "secp256r1 idx sig", 88),  // ECDSA_256r1_Sig
        ("F", "secp256r1 idx sig", 88),  // ECDSA_256r1_Crt_Sig
        ("0A", "Ed448 idx sig", 156),    // Ed448_Sig
        ("0B", "Ed448 idx sig", 156),    // Ed448_Crt_Sig
        ("2A", "Ed25519 idx sig", 92),   // Ed25519_Big_Sig
        ("2B", "Ed25519 idx sig", 92),   // Ed25519_Big_Crt_Sig
        ("2C", "secp256k1 idx sig", 92), // ECDSA_256k1_Big_Sig
        ("2D", "secp256k1 idx sig", 92), // ECDSA_256k1_Big_Crt_Sig
        ("2E", "secp256r1 idx sig", 92), // ECDSA_256r1_Big_Sig
        ("2F", "secp256r1 idx sig", 92), // ECDSA_256r1_Big_Crt_Sig
        ("3A", "Ed448 idx sig", 160),    // Ed448_Big_Sig
        ("3B", "Ed448 idx sig", 160),    // Ed448_Big_Crt_Sig
    ];
    if text.is_empty() {
        return Ok(None);
    }
    let len = text.chars().count();
    let first = text.chars().next().unwrap();
    let items: Option<&[(&str, &str, usize)]> = match first {
        '0' if TWO.iter().any(|x| x.2 == len) => Some(TWO),
        '1' if FOUR.iter().any(|x| x.2 == len) => Some(FOUR),
        c if c != '0' && c != '1' && ONE.iter().any(|x| x.2 == len) => Some(ONE),
        _ => None,
    };
    if let Some(items) = items {
        for &(code, label, total) in items {
            if text.starts_with(code) && len == total && is_base64url_nopad(text) {
                return Ok(Some(Parsed::new(
                    &format!("CESR {label}"),
                    BASE64URL,
                    None,
                    text.to_string(),
                    None,
                )));
            }
        }
    }
    // Matter did not match — try the Indexer table (indexed signatures).
    // Deliberately AFTER Matter so (code, length) decides Matter-vs-Indexer,
    // never the leading char alone. Match keys on the hard code + full size
    // only; the variable index chars are left in the core. See this.i:idxs1gs0.
    if INDEXER.iter().any(|x| x.2 == len) {
        for &(hard, label, fs) in INDEXER {
            if len == fs && text.starts_with(hard) && is_base64url_nopad(text) {
                return Ok(Some(Parsed::new(
                    &format!("CESR {label}"),
                    BASE64URL,
                    None,
                    text.to_string(),
                    None,
                )));
            }
        }
    }
    Ok(None)
}

fn is_base64url_nopad(s: &str) -> bool {
    !s.is_empty()
        && s.chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '-' || c == '_')
}

// SSH key type prefixes: (short_name, match_str, prefix_length).
const SSH_KEY_TYPES: &[(&str, &str, usize)] = &[
    (
        "ecdsa-nistp256",
        "AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABB",
        52,
    ),
    (
        "ecdsa-nistp384",
        "AAAAE2VjZHNhLXNoYTItbmlzdHAzODQAAAAIbmlzdHAzODQAAABh",
        52,
    ),
    (
        "ecdsa-nistp521",
        "AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACF",
        52,
    ),
    ("rsa", "AAAAB3NzaC1yc2EAAAADAQAB", 28),
    ("ed25519", "AAAAC3NzaC1lZDI1NTE5AAAA", 24),
    ("dss", "AAAAB3NzaC1kc3M", 15),
];

fn parse_ssh_key(text: &str) -> PResult {
    // SSH_LINE_REGEX: optional leading "<type> " then payload (AAAA...base64),
    // then optional whitespace+comment. We hand-parse it.
    let (payload, _comment) = match ssh_line_split(text) {
        Some(v) => v,
        None => {
            // Bare AAAA-base64 blob fallback (SSH_KEY_REGEX).
            if let Some((p, rest)) = ssh_key_regex(text) {
                return Ok(Some(Parsed::new("SSH key", BASE64, Some(p), rest, None)));
            }
            return Ok(None);
        }
    };
    for &(short_name, match_str, prefix_length) in SSH_KEY_TYPES {
        if payload.starts_with(match_str) && payload.chars().count() >= prefix_length {
            let chars: Vec<char> = payload.chars().collect();
            let prefix: String = chars[..prefix_length].iter().collect();
            let body: String = chars[prefix_length..].iter().collect();
            return Ok(Some(Parsed::new(
                &format!("SSH {short_name}"),
                BASE64,
                Some(prefix),
                body,
                None,
            )));
        }
    }
    if let Some((p, rest)) = ssh_key_regex(&payload) {
        return Ok(Some(Parsed::new("SSH key", BASE64, Some(p), rest, None)));
    }
    Ok(None)
}

// AAAA-prefixed base64 blob, optionally trailing '='. Returns (prefix "AAAA", rest).
fn ssh_key_regex(text: &str) -> Option<(String, String)> {
    if !text.starts_with("AAAA") {
        return None;
    }
    let rest = &text[4..];
    if rest.is_empty() {
        return None;
    }
    // body = [0-9A-Za-z+/]+ then up to 3 '='
    let body_end = rest.find('=').unwrap_or(rest.len());
    let (body, pad) = rest.split_at(body_end);
    if body.is_empty()
        || !body
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '/')
    {
        return None;
    }
    if pad.len() > 3 || !pad.chars().all(|c| c == '=') {
        return None;
    }
    Some(("AAAA".to_string(), rest.to_string()))
}

// Split a full openssh line: [<type-string> ] <AAAA-payload> [ <comment>].
fn ssh_line_split(text: &str) -> Option<(String, Option<String>)> {
    let mut s = text;
    // Strip an optional leading recognized type token.
    let type_prefixes = [
        "ssh-ed25519",
        "ssh-rsa",
        "ssh-dss",
        "ecdsa-sha2-nistp256",
        "ecdsa-sha2-nistp384",
        "ecdsa-sha2-nistp521",
    ];
    for tp in type_prefixes {
        if let Some(rest) = s.strip_prefix(tp) {
            if rest.starts_with(char::is_whitespace) {
                s = rest.trim_start();
                break;
            }
        }
    }
    if !s.starts_with("AAAA") {
        return None;
    }
    // payload = AAAA[0-9A-Za-z+/]+={0,3}; comment = optional whitespace + rest.
    let bytes: Vec<char> = s.chars().collect();
    // consume payload chars
    let mut i = 0;
    while i < bytes.len() {
        let c = bytes[i];
        if c.is_ascii_alphanumeric() || c == '+' || c == '/' {
            i += 1;
        } else {
            break;
        }
    }
    // trailing '=' padding
    while i < bytes.len() && bytes[i] == '=' {
        i += 1;
    }
    let payload_end = i;
    let payload: String = bytes[..payload_end].iter().collect();
    if !payload.starts_with("AAAA") {
        return None;
    }
    let rest: String = bytes[payload_end..].iter().collect();
    let comment = {
        let t = rest.trim();
        if t.is_empty() {
            None
        } else {
            Some(t.to_string())
        }
    };
    // If there were trailing non-whitespace chars immediately after payload with
    // no separating whitespace, the regex wouldn't match. Enforce that.
    if !rest.is_empty() && !rest.starts_with(char::is_whitespace) {
        return None;
    }
    Some((payload, comment))
}

fn parse_bitcoin_address(text: &str) -> PResult {
    // Legacy: ^[123mn] base58{21,30} base58{4}$
    let chars: Vec<char> = text.chars().collect();
    if let Some(first) = chars.first() {
        if "123mn".contains(*first) {
            let body = &text[first.len_utf8()..];
            let n = body.chars().count();
            if (25..=34).contains(&n) && is_base58(body) {
                // split last 4 as suffix; middle 21..30
                let bchars: Vec<char> = body.chars().collect();
                let mid: String = bchars[..bchars.len() - 4].iter().collect();
                let suf: String = bchars[bchars.len() - 4..].iter().collect();
                if (21..=30).contains(&mid.chars().count()) {
                    // The 4-byte double-SHA256 checksum is surfaced as the
                    // suffix, so it MUST verify. A structural match with a bad
                    // checksum rejects.
                    if !base58check_ok(text) {
                        return Err(ParseError::Base58Check);
                    }
                    return Ok(Some(Parsed::new(
                        "BTC legacy",
                        BASE58,
                        Some(first.to_string()),
                        mid,
                        Some(suf),
                    )));
                }
            }
        }
    }
    // SegWit: ^(bc1|tb1) bech32{39,69}$ (case-insensitive)
    if let Some(m) = match_prefix_bech32(text, &["bc1", "tb1"], 39, 69) {
        let (prefix, body) = m;
        // Verify the BIP-173/BIP-350 polymod (an earlier parser revision
        // skipped it). The HRP is "bc"/"tb" (strip the '1' separator).
        let hrp = prefix.to_lowercase();
        let hrp = hrp.trim_end_matches('1');
        match bech32_checksum_const(hrp, &body.to_lowercase()) {
            Some(c) if c == 1 || c == 0x2bc830a3 => {}
            _ => return Err(ParseError::Bech32Checksum),
        }
        return Ok(Some(Parsed::new(
            "BTC SegWit",
            BECH32,
            Some(prefix.to_lowercase()),
            body.to_lowercase(),
            None,
        )));
    }
    Ok(None)
}

// Match <one of prefixes><bech32 body of length in [lo,hi]>$ (case-insensitive
// on prefix and body). Returns (prefix_as_matched, body_as_matched).
fn match_prefix_bech32(
    text: &str,
    prefixes: &[&str],
    lo: usize,
    hi: usize,
) -> Option<(String, String)> {
    let low = text.to_lowercase();
    for p in prefixes {
        if low.starts_with(p) {
            let prefix: String = text.chars().take(p.chars().count()).collect();
            let body: String = text.chars().skip(p.chars().count()).collect();
            let n = body.chars().count();
            if (lo..=hi).contains(&n) && is_bech32_either(&body) {
                return Some((prefix, body));
            }
        }
    }
    None
}

fn parse_ripple_address(text: &str) -> PResult {
    // ^r base58{33}$
    if let Some(rest) = text.strip_prefix('r') {
        if rest.chars().count() == 33 && is_base58(rest) {
            return Ok(Some(Parsed::new(
                "XRP",
                BASE58,
                Some("r".to_string()),
                rest.to_string(),
                None,
            )));
        }
    }
    Ok(None)
}

fn parse_ethereum_address(text: &str) -> PResult {
    // ^(0x)?[0-9a-f]{40}$ case-insensitive
    let (has_prefix, body) =
        if let Some(b) = text.strip_prefix("0x").or_else(|| text.strip_prefix("0X")) {
            (true, b)
        } else {
            (false, text)
        };
    if body.chars().count() != 40 || !is_hex(body) {
        return Ok(None);
    }
    let letters: Vec<char> = body.chars().filter(|c| c.is_ascii_alphabetic()).collect();
    let has_lower = letters.iter().any(|c| c.is_ascii_lowercase());
    let has_upper = letters.iter().any(|c| c.is_ascii_uppercase());
    let is_mixed = has_lower && has_upper;

    if !has_prefix {
        if !is_mixed {
            return Ok(None);
        }
        validate_eip55(body)?;
    } else if is_mixed {
        validate_eip55(body)?;
    }
    Ok(Some(Parsed::new(
        "ETH",
        HEX,
        Some("0x".to_string()),
        body.to_lowercase(),
        None,
    )))
}

fn validate_eip55(body: &str) -> Result<(), ParseError> {
    let lower = body.to_lowercase();
    let digest_hex = keccak256_hex(lower.as_bytes());
    let dh: Vec<char> = digest_hex.chars().collect();
    for (i, c) in body.chars().enumerate() {
        if !c.is_ascii_alphabetic() {
            continue;
        }
        let canonical_upper = dh[i].to_digit(16).unwrap() >= 8;
        let expected = if canonical_upper {
            c.to_ascii_uppercase()
        } else {
            c.to_ascii_lowercase()
        };
        if c != expected {
            return Err(ParseError::Eip55 { position: i });
        }
    }
    Ok(())
}

fn parse_litecoin_address(text: &str) -> PResult {
    // Legacy: ^t?L base58{33}$
    for prefix in ["tL", "L"] {
        if let Some(rest) = text.strip_prefix(prefix) {
            if rest.chars().count() == 33 && is_base58(rest) {
                // Litecoin legacy is base58check; verify the double-SHA256
                // checksum — a bad checksum rejects.
                if !base58check_ok(text) {
                    return Err(ParseError::Base58Check);
                }
                return Ok(Some(Parsed::new(
                    "LTC legacy",
                    BASE58,
                    Some(prefix.to_string()),
                    rest.to_string(),
                    None,
                )));
            }
        }
    }
    // ltc1 bech32{38,68}
    if let Some((prefix, body)) = match_prefix_bech32(text, &["ltc1"], 38, 68) {
        // Verify the polymod (an earlier parser revision skipped it).
        // The HRP is "ltc" (strip the '1' separator).
        let hrp = prefix.to_lowercase();
        let hrp = hrp.trim_end_matches('1');
        match bech32_checksum_const(hrp, &body.to_lowercase()) {
            Some(c) if c == 1 || c == 0x2bc830a3 => {}
            _ => return Err(ParseError::Bech32Checksum),
        }
        return Ok(Some(Parsed::new(
            "LTC",
            BECH32,
            Some(prefix.to_lowercase()),
            body.to_lowercase(),
            None,
        )));
    }
    Ok(None)
}

fn parse_bitcoin_cash_address(text: &str) -> PResult {
    // ^((bitcoincash|bchtest):)?[pq]bech32{41}$  (case-insensitive)
    let low = text.to_lowercase();
    let (prefix, rest) = if low.starts_with("bitcoincash:") {
        let n = "bitcoincash:".len();
        (Some(&text[..n]), &text[n..])
    } else if low.starts_with("bchtest:") {
        let n = "bchtest:".len();
        (Some(&text[..n]), &text[n..])
    } else {
        (None, text)
    };
    let rchars: Vec<char> = rest.chars().collect();
    if let Some(first) = rchars.first() {
        if (*first == 'p' || *first == 'q' || *first == 'P' || *first == 'Q') && rchars.len() == 42
        {
            let body: String = rchars[1..].iter().collect();
            if is_bech32_either(&body) {
                let full_body: String = rest.to_lowercase();
                // Verify the 40-bit CashAddr BCH checksum (a DIFFERENT code
                // from bech32's polymod). The checksum HRP is the prefix WITHOUT
                // the colon, defaulting to "bitcoincash" for a bare q…/p… address;
                // the payload (INCLUDING its 8 trailing checksum chars) is the
                // whole `rest`. A structural match with a bad checksum rejects.
                let hrp = prefix
                    .map(|p| p.trim_end_matches(':').to_lowercase())
                    .unwrap_or_else(|| "bitcoincash".to_string());
                if !cashaddr_verify(&hrp, &full_body) {
                    return Err(ParseError::CashAddrChecksum);
                }
                return Ok(Some(Parsed::new(
                    "BCH",
                    BECH32,
                    prefix.map(|p| p.to_string()),
                    full_body,
                    None,
                )));
            }
        }
    }
    Ok(None)
}

fn parse_stellar_address(text: &str) -> PResult {
    let chars: Vec<char> = text.chars().collect();
    if let Some(first) = chars.first() {
        if (*first == 'G' || *first == 'g') && chars.len() == 56 {
            let body: String = chars[1..].iter().collect();
            if is_base32_either(&body) {
                return Ok(Some(Parsed::new(
                    "XLM",
                    BASE32,
                    Some("G".to_string()),
                    body.to_uppercase(),
                    None,
                )));
            }
        }
        if (*first == 'M' || *first == 'm') && chars.len() == 69 {
            let body: String = chars[1..].iter().collect();
            if is_base32_either(&body) {
                return Ok(Some(Parsed::new(
                    "XLM muxed",
                    BASE32,
                    Some("M".to_string()),
                    body.to_uppercase(),
                    None,
                )));
            }
        }
    }
    Ok(None)
}

fn parse_uuid(text: &str) -> PResult {
    // ^\{?hex{8}-?hex{4}-?hex{4}-?hex{4}-?hex{12}\}?$  case-insensitive
    let mut s = text;
    let had_brace_open = s.starts_with('{');
    if had_brace_open {
        s = &s[1..];
    }
    let mut s = s.to_string();
    if s.ends_with('}') {
        s.pop();
    }
    // Now match the 8-4-4-4-12 with optional dashes.
    let groups = [8usize, 4, 4, 4, 12];
    let stripped: String = s.chars().filter(|&c| c != '-').collect();
    // Validate structure: rebuild allowed forms. Simplest faithful check:
    // remove dashes, require 32 hex; AND ensure dashes (if present) sit only at
    // the group boundaries. We check via a small scan.
    if stripped.chars().count() != 32 || !is_hex(&stripped) {
        return Ok(None);
    }
    // Verify the dash placement matches the regex (optional dash after each group
    // except the last). Walk the original `s` against the group pattern.
    let sc: Vec<char> = s.chars().collect();
    let mut pos = 0;
    for (gi, &glen) in groups.iter().enumerate() {
        for _ in 0..glen {
            if pos >= sc.len() || !sc[pos].is_ascii_hexdigit() {
                return Ok(None);
            }
            pos += 1;
        }
        if gi < groups.len() - 1 && pos < sc.len() && sc[pos] == '-' {
            pos += 1;
        }
    }
    if pos != sc.len() {
        return Ok(None);
    }
    Ok(Some(Parsed::new(
        "UUID",
        HEX,
        None,
        stripped.to_lowercase(),
        None,
    )))
}

fn parse_ulid(text: &str) -> PResult {
    // ^[0-9A-TV-Za-tv-z]{26}$  (Crockford32 + I/L/O aliases, no U)
    if text.chars().count() != 26 {
        return Ok(None);
    }
    for c in text.chars() {
        let ok = c.is_ascii_digit()
            || ('A'..='T').contains(&c)
            || ('V'..='Z').contains(&c)
            || ('a'..='t').contains(&c)
            || ('v'..='z').contains(&c);
        if !ok {
            return Ok(None);
        }
    }
    let normalized: String = text
        .chars()
        .map(|c| match c {
            'I' | 'i' | 'L' | 'l' => '1',
            'O' | 'o' => '0',
            other => other,
        })
        .collect::<String>()
        .to_uppercase();
    Ok(Some(Parsed::new(
        "ULID",
        CROCKFORD32,
        None,
        normalized,
        None,
    )))
}

fn parse_snowflake(text: &str) -> PResult {
    let n = text.chars().count();
    if !(17..=20).contains(&n) || !text.chars().all(|c| c.is_ascii_digit()) {
        return Ok(None);
    }
    let val: u128 = match text.parse() {
        Ok(v) => v,
        Err(_) => return Ok(None),
    };
    if val >> 63 != 0 {
        return Ok(None);
    }
    Ok(Some(Parsed::new(
        "snowflake",
        DECIMAL,
        None,
        text.to_string(),
        None,
    )))
}

fn parse_lei(text: &str) -> PResult {
    // ^[0-9A-Z]{20}$ case-insensitive; upper[4:6]=="00"; MOD 97-10 == 1
    if text.chars().count() != 20 || !text.chars().all(|c| c.is_ascii_alphanumeric()) {
        return Ok(None);
    }
    let upper = text.to_uppercase();
    if &upper[4..6] != "00" {
        return Ok(None);
    }
    if !lei_checksum_ok(&upper) {
        // 20 base36 chars WITH the reserved "00" is an unambiguous LEI
        // match, and the MOD 97-10 check digits are surfaced as the bound
        // suffix — so a bad checksum REJECTS rather than falling through to a
        // generic base36 encoding.
        return Err(ParseError::LeiChecksum);
    }
    Ok(Some(Parsed::new(
        "LEI",
        BASE36,
        None,
        upper[..18].to_string(),
        Some(upper[18..].to_string()),
    )))
}

fn lei_checksum_ok(lei: &str) -> bool {
    let mut digits = String::new();
    for c in lei.chars() {
        if c.is_ascii_digit() {
            digits.push(c);
        } else if c.is_ascii_uppercase() {
            digits.push_str(&(c as u32 - 'A' as u32 + 10).to_string());
        } else {
            return false;
        }
    }
    // mod 97 over a big decimal string
    let mut rem: u64 = 0;
    for ch in digits.bytes() {
        rem = (rem * 10 + (ch - b'0') as u64) % 97;
    }
    rem == 1
}

fn parse_swhid(text: &str) -> PResult {
    // ^(swh:1:(snp|rel|rev|dir|cnt):)([0-9a-f]{40})(?:;(.+))?$  case-insensitive
    let low = text.to_lowercase();
    let types = ["snp", "rel", "rev", "dir", "cnt"];
    for t in types {
        let pre = format!("swh:1:{t}:");
        if low.starts_with(&pre) {
            let rest = &low[pre.len()..];
            // optional ;qualifiers
            let (hexpart, _qual) = match rest.find(';') {
                Some(i) => (&rest[..i], Some(&rest[i + 1..])),
                None => (rest, None),
            };
            if hexpart.chars().count() == 40 && is_hex(hexpart) {
                let prefix: String = text.chars().take(pre.len()).collect();
                return Ok(Some(
                    Parsed::new(
                        "",
                        HEX,
                        Some(prefix.to_lowercase()),
                        hexpart.to_string(),
                        None,
                    )
                    .semantic(),
                ));
            }
        }
    }
    Ok(None)
}

fn parse_gitoid(text: &str) -> PResult {
    // ^(gitoid:(blob|tree|commit|tag):(sha1|sha256):)([0-9a-f]+)$ case-insensitive
    let low = text.to_lowercase();
    if !low.starts_with("gitoid:") {
        return Ok(None);
    }
    let parts: Vec<&str> = low.splitn(4, ':').collect();
    if parts.len() != 4 {
        return Ok(None);
    }
    let obj = parts[1];
    let algo = parts[2];
    let body = parts[3];
    if !["blob", "tree", "commit", "tag"].contains(&obj) {
        return Ok(None);
    }
    let want = match algo {
        "sha1" => 40,
        "sha256" => 64,
        _ => return Ok(None),
    };
    if body.chars().count() != want || !is_hex(body) {
        return Ok(None);
    }
    let prefix = format!("gitoid:{obj}:{algo}:");
    Ok(Some(
        Parsed::new("", HEX, Some(prefix), body.to_string(), None).semantic(),
    ))
}

// ---- DID / URN (RFC 8141) ----
// Both bind by prefix-fold (prefix_semantic=true): no type, base64url alphabet,
// core kept verbatim, suffix None.

// DID: ^did:([a-z0-9]+):([A-Za-z0-9._%:-]+)(?:[/?#].*)?$
// method = lowercase [a-z0-9]+; method-specific-id = [A-Za-z0-9._%:-]+ (MAY
// contain ':'), ending only at the first '/', '?', or '#' (DID-URL tail dropped).
fn parse_did(text: &str) -> PResult {
    let rest = match text.strip_prefix("did:") {
        Some(r) => r,
        None => return Ok(None),
    };
    // method = [a-z0-9]+ up to the next ':'
    let colon = match rest.find(':') {
        Some(i) => i,
        None => return Ok(None),
    };
    let method = &rest[..colon];
    if method.is_empty()
        || !method
            .chars()
            .all(|c| c.is_ascii_lowercase() || c.is_ascii_digit())
    {
        return Ok(None);
    }
    let body = &rest[colon + 1..];
    // method-specific-id ends at the first '/', '?', or '#'.
    let msid_end = body.find(['/', '?', '#']).unwrap_or(body.len());
    let msid = &body[..msid_end];
    if msid.is_empty()
        || !msid
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, '.' | '_' | '%' | ':' | '-'))
    {
        return Ok(None);
    }
    let prefix = format!("did:{method}:");
    Ok(Some(
        Parsed::new("", BASE64URL, Some(prefix), msid.to_string(), None).semantic(),
    ))
}

// URN (RFC 8141): ^urn:([A-Za-z0-9][A-Za-z0-9-]{0,31}):([^?#]+)(?:[?#].*)?$
// scheme + NID are case-insensitive (NID is lowercased in the prefix); NSS keeps
// '/' and ends only at '?' or '#' (r-/q-/f-components dropped); NSS case preserved.
fn parse_urn(text: &str) -> PResult {
    let low = text.to_lowercase();
    if !low.starts_with("urn:") {
        return Ok(None);
    }
    let rest = &text[4..]; // keep original case for NSS
    let colon = match rest.find(':') {
        Some(i) => i,
        None => return Ok(None),
    };
    let nid = &rest[..colon];
    // NID = [A-Za-z0-9][A-Za-z0-9-]{0,31} (1..=32 chars)
    let nid_chars: Vec<char> = nid.chars().collect();
    if nid_chars.is_empty() || nid_chars.len() > 32 {
        return Ok(None);
    }
    if !nid_chars[0].is_ascii_alphanumeric() {
        return Ok(None);
    }
    if !nid_chars[1..]
        .iter()
        .all(|&c| c.is_ascii_alphanumeric() || c == '-')
    {
        return Ok(None);
    }
    let body = &rest[colon + 1..];
    // NSS = [^?#]+ ; ends at the first '?' or '#'.
    let nss_end = body.find(['?', '#']).unwrap_or(body.len());
    let nss = &body[..nss_end];
    if nss.is_empty() {
        return Ok(None);
    }
    let prefix = format!("urn:{}:", nid.to_lowercase());
    Ok(Some(
        Parsed::new("", BASE64URL, Some(prefix), nss.to_string(), None).semantic(),
    ))
}

// ---- base58check (BTC / LTC legacy) ----

/// Decode a base58 (Bitcoin alphabet) string to raw bytes, preserving
/// leading-zero bytes (each leading '1' is a 0x00 byte). Used only for checksum
/// verification; the visualized core is the original text. Returns `None` on any
/// character outside the base58 alphabet.
fn base58_decode_bytes(s: &str) -> Option<Vec<u8>> {
    // Accumulate a base-256 big-endian big integer: acc = acc * 58 + v.
    let mut acc: Vec<u8> = vec![0];
    for c in s.chars() {
        let v = BASE58_CHARS.find(c)? as u32;
        let mut carry: u32 = v;
        for byte in acc.iter_mut().rev() {
            let cur = (*byte as u32) * 58 + carry;
            *byte = (cur & 0xff) as u8;
            carry = cur >> 8;
        }
        while carry > 0 {
            acc.insert(0, (carry & 0xff) as u8);
            carry >>= 8;
        }
    }
    // Strip leading zero bytes from the numeric value, then re-add one 0x00 for
    // each leading '1' (base58's leading-zero convention).
    let first_nonzero = acc.iter().position(|&b| b != 0).unwrap_or(acc.len());
    let mut body: Vec<u8> = acc[first_nonzero..].to_vec();
    let pad = s.chars().take_while(|&c| c == '1').count();
    let mut out = vec![0u8; pad];
    out.append(&mut body);
    Some(out)
}

/// True iff `s` decodes to `payload || checksum` where checksum is the first 4
/// bytes of double-SHA256(payload). The near-universal base58check construction
/// used by Bitcoin/Litecoin legacy addresses.
fn base58check_ok(s: &str) -> bool {
    use sha2::{Digest, Sha256};
    let raw = match base58_decode_bytes(s) {
        Some(r) => r,
        None => return false,
    };
    if raw.len() < 5 {
        return false;
    }
    let (payload, checksum) = raw.split_at(raw.len() - 4);
    let d1 = Sha256::digest(payload);
    let d2 = Sha256::digest(d1);
    d2[..4] == *checksum
}

// ---- CashAddr (Bitcoin Cash) 40-bit BCH checksum ----

/// The 40-bit BCH checksum polymod used by Bitcoin Cash CashAddr (NOT the
/// bech32 polymod). `values` is a slice of 5-bit ints. Valid iff it returns 0.
fn cashaddr_polymod(values: &[u64]) -> u64 {
    const GEN: [u64; 5] = [
        0x98f2bc8e61,
        0x79b76d99e2,
        0xf33e5fb3c4,
        0xae2eabe2a8,
        0x1e4f43e470,
    ];
    let mut c: u64 = 1;
    for &d in values {
        let c0 = c >> 35;
        c = ((c & 0x07ffffffff) << 5) ^ d;
        for (i, g) in GEN.iter().enumerate() {
            if (c0 >> i) & 1 != 0 {
                c ^= g;
            }
        }
    }
    c ^ 1
}

/// True iff CashAddr `payload` (bech32-charset body INCLUDING the trailing 8
/// checksum chars) carries a valid BCH checksum under `prefix` (lowercase, e.g.
/// 'bitcoincash' / 'bchtest').
fn cashaddr_verify(prefix: &str, payload: &str) -> bool {
    let mut values: Vec<u64> = prefix.chars().map(|c| (c as u64) & 0x1f).collect();
    values.push(0);
    for c in payload.to_lowercase().chars() {
        match BECH32_CHARS.find(c) {
            Some(i) => values.push(i as u64),
            None => return false,
        }
    }
    cashaddr_polymod(&values) == 0
}

// ---- bech32 checksum (generic Cosmos-style) ----
fn bech32_polymod(values: &[u32]) -> u32 {
    const GEN: [u32; 5] = [0x3b6a57b2, 0x26508e6d, 0x1ea119fa, 0x3d4233dd, 0x2a1462b3];
    let mut chk: u32 = 1;
    for &v in values {
        let top = chk >> 25;
        chk = ((chk & 0x1ffffff) << 5) ^ v;
        for (i, g) in GEN.iter().enumerate() {
            if (top >> i) & 1 != 0 {
                chk ^= g;
            }
        }
    }
    chk
}
fn bech32_hrp_expand(hrp: &str) -> Vec<u32> {
    let mut out: Vec<u32> = hrp.chars().map(|c| (c as u32) >> 5).collect();
    out.push(0);
    out.extend(hrp.chars().map(|c| (c as u32) & 31));
    out
}
fn bech32_checksum_const(hrp: &str, data: &str) -> Option<u32> {
    let mut values = Vec::new();
    for c in data.chars() {
        let idx = BECH32_CHARS.find(c)?;
        values.push(idx as u32);
    }
    let mut full = bech32_hrp_expand(hrp);
    full.extend(values);
    Some(bech32_polymod(&full))
}

fn parse_bech32_address(text: &str) -> PResult {
    // ^([a-z]{1,83})1(bech32{8,})$  case-insensitive; checksum valid
    // Find the LAST '1' as separator? The regex is greedy on hrp [a-z]{1,83}
    // then literal '1' then bech32{8,}. Python re is greedy, so hrp matches as
    // many [a-z] as possible before a '1' that still leaves >=8 bech32 chars.
    let low = text.to_lowercase();
    let chars: Vec<char> = low.chars().collect();
    // hrp must be letters; find separator positions where char=='1'.
    // Greedy: try the largest hrp first.
    // hrp = chars[..sep], all ascii lowercase letters; data = chars[sep+1..].
    let mut sep_candidates: Vec<usize> = Vec::new();
    for (i, &c) in chars.iter().enumerate() {
        if c == '1' {
            sep_candidates.push(i);
        }
    }
    // greedy hrp => prefer the LARGEST separator index that satisfies the
    // structural constraints. The Python anchored regex commits to that single
    // greedy split; we mirror it — the first structurally-valid split is the
    // match, and its checksum then decides accept vs REJECT (a `<hrp>1<8+
    // bech32>` string is a clear bech32 structural match, and the 6-char
    // checksum is surfaced as the bound suffix, so an invalid polymod rejects
    // rather than falling through to a bare encoding).
    for &sep in sep_candidates.iter().rev() {
        if !(1..=83).contains(&sep) {
            continue;
        }
        let hrp: String = chars[..sep].iter().collect();
        if !hrp.chars().all(|c| c.is_ascii_lowercase()) {
            continue;
        }
        let data: String = chars[sep + 1..].iter().collect();
        if data.chars().count() < 8 || !all_in(&data, BECH32_CHARS) {
            continue;
        }
        // Structural match committed (greedy). Validate the checksum.
        match bech32_checksum_const(&hrp, &data) {
            Some(c) if c == 1 || c == 0x2bc830a3 => {
                let dchars: Vec<char> = data.chars().collect();
                let core: String = dchars[..dchars.len() - 6].iter().collect();
                let suffix: String = dchars[dchars.len() - 6..].iter().collect();
                return Ok(Some(Parsed::new(
                    "bech32",
                    BECH32,
                    Some(format!("{hrp}1")),
                    core,
                    Some(suffix),
                )));
            }
            _ => return Err(ParseError::Bech32Checksum),
        }
    }
    Ok(None)
}

// ---- IPFS CID ----
fn parse_ipfs_cid(text: &str) -> PResult {
    // CIDv0: ^Qm base58{44}$
    if let Some(rest) = text.strip_prefix("Qm") {
        if rest.chars().count() == 44 && is_base58(rest) {
            return Ok(Some(Parsed::new(
                "CIDv0",
                BASE58,
                Some("Qm".to_string()),
                rest.to_string(),
                None,
            )));
        }
    }
    // CIDv1: ^b base32{58,112}$ (either case)
    if let Some(rest) = text.strip_prefix('b') {
        let n = rest.chars().count();
        if (58..=112).contains(&n) && is_base32_either(rest) {
            let mut label = "CIDv1".to_string();
            if let Some((codec, hash)) = b32_decode_multicodec(rest) {
                label = format!("CIDv1 {codec}");
                if hash != "sha2-256" {
                    label.push('/');
                    label.push_str(&hash);
                }
            }
            return Ok(Some(Parsed::new(
                &label,
                BASE32,
                Some("b".to_string()),
                rest.to_uppercase(),
                None,
            )));
        }
    }
    Ok(None)
}

fn b32_decode_multicodec(body: &str) -> Option<(String, String)> {
    let bytes = base32_decode(&body.to_uppercase())?;
    let (version, p1) = read_uvarint(&bytes, 0)?;
    if version != 1 {
        return None;
    }
    let (codec, p2) = read_uvarint(&bytes, p1)?;
    let (hash_fn, _p3) = read_uvarint(&bytes, p2)?;
    let codec_name = multicodec_content(codec)?;
    let hash_name = multihash_func(hash_fn)?;
    Some((codec_name.to_string(), hash_name.to_string()))
}

fn read_uvarint(data: &[u8], mut pos: usize) -> Option<(u64, usize)> {
    let mut result: u64 = 0;
    let mut shift = 0u32;
    while pos < data.len() {
        let b = data[pos];
        pos += 1;
        result |= ((b & 0x7f) as u64) << shift;
        if b & 0x80 == 0 {
            return Some((result, pos));
        }
        shift += 7;
    }
    None
}

fn base32_decode(s: &str) -> Option<Vec<u8>> {
    // RFC4648 base32, upper, no padding required.
    let mut bits = 0u32;
    let mut value = 0u32;
    let mut out = Vec::new();
    for c in s.chars() {
        let idx = BASE32_CHARS_UP.find(c)? as u32;
        value = (value << 5) | idx;
        bits += 5;
        if bits >= 8 {
            bits -= 8;
            out.push(((value >> bits) & 0xff) as u8);
        }
    }
    Some(out)
}

fn multicodec_content(code: u64) -> Option<&'static str> {
    Some(match code {
        0x00 => "identity",
        0x51 => "cbor",
        0x55 => "raw",
        0x60 => "rlp",
        0x70 => "dag-pb",
        0x71 => "dag-cbor",
        0x72 => "libp2p-key",
        0x78 => "git-raw",
        0x90 => "eth-block",
        0x97 => "eth-tx",
        0x0129 => "dag-json",
        0x0202 => "car",
        _ => return None,
    })
}

fn multihash_func(code: u64) -> Option<&'static str> {
    Some(match code {
        0x11 => "sha1",
        0x12 => "sha2-256",
        0x13 => "sha2-512",
        0x14 => "sha3-224",
        0x15 => "sha3-256",
        0x16 => "sha3-384",
        0x17 => "sha3-512",
        0x1b => "keccak-256",
        0x41 => "blake2b-256",
        _ => return None,
    })
}

fn parse_hex(text: &str) -> PResult {
    if text.is_empty() {
        return Ok(None);
    }
    let mut prefix = None;
    let mut body = text;
    if (text.starts_with("0x") || text.starts_with("0X")) && text.chars().count() > 2 {
        prefix = Some("0x".to_string());
        body = &text[2..];
    } else if !text.chars().count().is_multiple_of(2) {
        return Ok(None);
    }
    if is_hex(body) {
        return Ok(Some(Parsed::new(
            "hex",
            HEX,
            prefix,
            body.to_lowercase(),
            None,
        )));
    }
    Ok(None)
}

fn parse_eos_address(text: &str) -> PResult {
    // (^[a-z1-5.]{1,11}[a-z1-5]$)|(^[a-z1-5.]{12}[a-j1-5]$)
    let ok = eos_regex(text);
    if !ok {
        return Ok(None);
    }
    if text.chars().all(|c| "0123456789abcdef".contains(c)) {
        return Ok(None);
    }
    Ok(Some(Parsed::new(
        "EOS",
        BASE64,
        None,
        text.to_string(),
        None,
    )))
}

fn eos_regex(text: &str) -> bool {
    let chars: Vec<char> = text.chars().collect();
    let in_set = |c: char| {
        c.is_ascii_lowercase() && c.is_ascii_lowercase() || ('1'..='5').contains(&c) || c == '.'
    };
    let body_ok = |s: &[char]| s.iter().all(|&c| in_set(c));
    let n = chars.len();
    // form 1: {1,11}[a-z1-5] => total 2..12, last char in a-z1-5
    if (2..=12).contains(&n) {
        let last = chars[n - 1];
        if body_ok(&chars[..n - 1]) && (last.is_ascii_lowercase() || ('1'..='5').contains(&last)) {
            return true;
        }
    }
    // form 2: {12}[a-j1-5] => total 13, last in a-j1-5
    if n == 13 {
        let last = chars[12];
        if body_ok(&chars[..12]) && ((('a'..='j').contains(&last)) || ('1'..='5').contains(&last)) {
            return true;
        }
    }
    false
}

// --------------------------------------------------------------------------
// Dispatch
// --------------------------------------------------------------------------
type ParserFn = fn(&str) -> PResult;

const PARSERS: &[ParserFn] = &[
    // parse_hex_multihash is not exercised by the corpus and is omitted; the
    // remaining order matches entropy.py's parse_funcs (incl. parse_did/parse_urn).
    parse_cesr,
    parse_ssh_key,
    parse_bitcoin_address,
    parse_ripple_address,
    parse_ethereum_address,
    parse_litecoin_address,
    parse_bitcoin_cash_address,
    // parse_cardano_address omitted (not in corpus; bech32 generic covers none of it)
    parse_stellar_address,
    parse_uuid,
    parse_ulid,
    parse_snowflake,
    parse_lei,
    parse_did,
    parse_urn,
    parse_swhid,
    parse_gitoid,
    parse_bech32_address,
    parse_ipfs_cid,
    parse_hex,
    parse_eos_address,
];

/// Parse the (already-stripped) entropy string. Returns:
/// * `Ok(Some(parsed))` on a recognized type or disproof-detected alphabet,
/// * `Ok(None)` when nothing matches (caller re-encodes to base64url),
/// * `Err(..)` on a hard rejection (EIP-55 checksum failure).
pub fn parse(entropy: &str) -> Result<Option<Parsed>, ParseError> {
    let entropy = entropy.trim();
    for f in PARSERS {
        match f(entropy)? {
            Some(p) => return Ok(Some(p)),
            None => continue,
        }
    }
    if let Some(detected) = detect_alphabet_by_disproof(entropy) {
        let core = if detected.name == "base32" {
            entropy.to_uppercase()
        } else if detected.name == "bech32" || detected.name == "hex" {
            entropy.to_lowercase()
        } else {
            entropy.to_string()
        };
        return Ok(Some(Parsed::new(detected.name, detected, None, core, None)));
    }
    Ok(None)
}

fn detect_alphabet_by_disproof(text: &str) -> Option<Alphabet> {
    if text.is_empty() {
        return None;
    }
    let lower = text.to_lowercase();
    // (alphabet, charset, case_sensitive)
    let order: [(Alphabet, &str, bool); 6] = [
        (HEX, HEX_CHARS, false),
        (BASE32, "abcdefghijklmnopqrstuvwxyz234567", false),
        (BECH32, BECH32_CHARS, false),
        (BASE58, BASE58_CHARS, true),
        (
            BASE64,
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",
            true,
        ),
        (
            BASE64URL,
            "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_",
            true,
        ),
    ];
    for (alpha, charset, case_sensitive) in order {
        let view = if case_sensitive { text } else { lower.as_str() };
        if view.chars().all(|c| charset.contains(c)) {
            return Some(alpha);
        }
    }
    None
}

// --------------------------------------------------------------------------
// Large-input tokenization (head + fingerprint-middle + tail)
// --------------------------------------------------------------------------
use crate::{second_digest, tokenize, Token};

const HEAD_TOKENS: usize = 8;
const TAIL_TOKENS: usize = 8;
const MAX_TOKENS: usize = 22;

fn core_byte_length(core: &str, alphabet: &Alphabet) -> usize {
    (core.chars().count() * alphabet.bits_per_char as usize) / 8
}

/// Encode a 24-bit value as 5 lowercase Crockford base32 chars (middle cell readout).
pub fn crockford5(value: u32) -> String {
    const C: &[u8; 32] = b"0123456789ABCDEFGHJKMNPQRSTVWXYZ";
    let mut out = [0u8; 5];
    let mut v = value;
    for i in 0..5 {
        out[4 - i] = C[(v & 0x1F) as usize];
        v >>= 5;
    }
    String::from_utf8(out.to_vec()).unwrap().to_lowercase()
}

/// Tokenize entropy with large-input handling. Returns (tokens, truncated).
pub fn tokenize_entropy(core: &str, alphabet: &Alphabet) -> (Vec<Token>, bool) {
    let token_len = (24 / alphabet.bits_per_char) as usize;
    let n_bytes = core_byte_length(core, alphabet);
    let token_count = core.chars().count().div_ceil(token_len); // ceil
    if token_count <= MAX_TOKENS && n_bytes <= 64 {
        return (tokenize(core, alphabet), false);
    }
    let chars: Vec<char> = core.chars().collect();
    let head_chars = HEAD_TOKENS * token_len;
    let tail_chars = TAIL_TOKENS * token_len;
    let head: String = chars[..head_chars.min(chars.len())].iter().collect();
    let tail_start = chars.len().saturating_sub(tail_chars);
    let tail: String = chars[tail_start..].iter().collect();
    let head_tokens = tokenize(&head, alphabet);
    let tail_tokens = tokenize(&tail, alphabet);

    let second = second_digest(core);
    let mut middle = Vec::with_capacity(4);
    for i in 0..4 {
        let quant = ((second[3 * i] as u32) << 16)
            | ((second[3 * i + 1] as u32) << 8)
            | (second[3 * i + 2] as u32);
        middle.push(Token {
            text: crockford5(quant),
            index: i,
            quant,
        });
    }

    let mut combined: Vec<Token> = Vec::with_capacity(20);
    combined.extend(head_tokens);
    combined.extend(middle);
    combined.extend(tail_tokens);
    let renumbered: Vec<Token> = combined
        .into_iter()
        .enumerate()
        .map(|(i, t)| Token {
            text: t.text,
            index: i,
            quant: t.quant,
        })
        .collect();
    (renumbered, true)
}

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

    fn ptop(entropy: &str) -> (String, String, Option<String>, Option<String>) {
        let p = parse(entropy).unwrap().unwrap();
        (p.type_name, p.core, p.prefix, p.suffix)
    }

    #[test]
    fn hex_and_uuid_boundary() {
        // 16-hex -> hex(16); 32-hex -> UUID (matches reference dispatch).
        assert_eq!(ptop("a1b2c3d4e5f6a7b8").0, "hex");
        assert_eq!(ptop("0123456789abcdef0123456789abcdef").0, "UUID");
    }

    #[test]
    fn uuid_dashed_equals_undashed_core() {
        let a = parse("550e8400-e29b-41d4-a716-446655440000")
            .unwrap()
            .unwrap();
        let b = parse("550e8400e29b41d4a716446655440000").unwrap().unwrap();
        assert_eq!(a.core, b.core);
        assert_eq!(a.core, "550e8400e29b41d4a716446655440000");
    }

    #[test]
    fn eth_eip55_good_and_bad() {
        assert_eq!(
            parse("0x742d35cc6634c0532925a3b844bc454e4438f44e")
                .unwrap()
                .unwrap()
                .type_name,
            "ETH"
        );
        assert_eq!(
            parse("0x5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
                .unwrap()
                .unwrap()
                .type_name,
            "ETH"
        );
        assert!(matches!(
            parse("0x5aaeb6053F3E94C9b9A09f33669435E7Ef1BeAed"),
            Err(ParseError::Eip55 { .. })
        ));
    }

    #[test]
    fn swhid_gitoid_semantic_prefix() {
        let s = parse("swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d")
            .unwrap()
            .unwrap();
        assert!(s.prefix_semantic);
        assert_eq!(s.prefix.as_deref(), Some("swh:1:rev:"));
        assert_eq!(s.core, "309cf2674ee7a0749978cf8265ab91a60aea0f7d");
        let g = parse(
            "gitoid:blob:sha256:473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813",
        )
        .unwrap()
        .unwrap();
        assert!(g.prefix_semantic);
        assert_eq!(g.prefix.as_deref(), Some("gitoid:blob:sha256:"));
    }

    #[test]
    fn did_parsing() {
        // did:web basic
        let p = parse("did:web:example.com").unwrap().unwrap();
        assert_eq!(p.type_name, "");
        assert_eq!(p.prefix.as_deref(), Some("did:web:"));
        assert_eq!(p.core, "example.com");
        assert!(p.prefix_semantic);
        assert!(p.suffix.is_none());

        // did:web with colon path segments (core keeps colons, NOT case-folded)
        let p = parse("did:web:example.com%3A3000:user:Alice")
            .unwrap()
            .unwrap();
        assert_eq!(p.prefix.as_deref(), Some("did:web:"));
        assert_eq!(p.core, "example.com%3A3000:user:Alice");
        assert!(p.prefix_semantic);

        // did:web with /path?q#frag -> DID-URL tail dropped, core == bare
        let bare = parse("did:web:example.com:user:Alice").unwrap().unwrap();
        let tailed = parse("did:web:example.com:user:Alice/path?q=1#frag")
            .unwrap()
            .unwrap();
        assert_eq!(tailed.core, bare.core);
        assert_eq!(tailed.core, "example.com:user:Alice");
        assert_eq!(tailed.prefix.as_deref(), Some("did:web:"));

        // did:key fragment dropped
        let p = parse("did:key:z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK#z6Mkha")
            .unwrap()
            .unwrap();
        assert_eq!(p.prefix.as_deref(), Some("did:key:"));
        assert_eq!(p.core, "z6MkhaXgBZDvotDkL5257faiztiGiC2QtKLGpbnnEGta2doK");
        assert!(p.prefix_semantic);
    }

    #[test]
    fn urn_parsing() {
        // urn:isbn basic
        let p = parse("urn:isbn:0451450523").unwrap().unwrap();
        assert_eq!(p.type_name, "");
        assert_eq!(p.prefix.as_deref(), Some("urn:isbn:"));
        assert_eq!(p.core, "0451450523");
        assert!(p.prefix_semantic);
        assert!(p.suffix.is_none());

        // URN:ISBN: lowercases prefix (NID) only; NSS case preserved
        let p = parse("URN:ISBN:X0451450523abc").unwrap().unwrap();
        assert_eq!(p.prefix.as_deref(), Some("urn:isbn:"));
        assert_eq!(p.core, "X0451450523abc");

        // NSS keeps '/'
        let p = parse("urn:example:weather/oregon/portland")
            .unwrap()
            .unwrap();
        assert_eq!(p.prefix.as_deref(), Some("urn:example:"));
        assert_eq!(p.core, "weather/oregon/portland");

        // r-/q-/f-components dropped (?=... and #frag)
        let p = parse("urn:example:foo-bar?=baz=1#section")
            .unwrap()
            .unwrap();
        assert_eq!(p.prefix.as_deref(), Some("urn:example:"));
        assert_eq!(p.core, "foo-bar");
        assert!(p.prefix_semantic);
    }

    #[test]
    fn lei_suffix() {
        let p = parse("5493001KJTIIGC8Y1R12").unwrap().unwrap();
        assert_eq!(p.type_name, "LEI");
        assert_eq!(p.core, "5493001KJTIIGC8Y1R12"[..18].to_string());
        assert_eq!(p.suffix.as_deref(), Some("12"));
    }

    #[test]
    fn cesr_codes() {
        assert_eq!(
            parse("DKxy2sgzfplyr_tgwIxS19f2OchFHtLwPWD3v4oYimBx")
                .unwrap()
                .unwrap()
                .type_name,
            "CESR Ed25519 pubkey"
        );
        assert_eq!(
            parse("BKxy2sgzfplyr_tgwIxS19f2OchFHtLwPWD3v4oYimBx")
                .unwrap()
                .unwrap()
                .type_name,
            "CESR Ed25519 nt pubkey"
        );
        assert_eq!(
            parse("EBfdlu8R27Fbx_ehrqwImnK_8Cm79sqbAQ4caaZG_LFv")
                .unwrap()
                .unwrap()
                .type_name,
            "CESR Blake3-256"
        );
    }

    #[test]
    fn bech32_cosmos_suffix() {
        let p = parse("cosmos1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnrk363e")
            .unwrap()
            .unwrap();
        assert_eq!(p.type_name, "bech32");
        assert_eq!(p.prefix.as_deref(), Some("cosmos1"));
        assert_eq!(p.suffix.as_deref(), Some("rk363e"));
    }

    #[test]
    fn cid_v1_label() {
        let p = parse("bafybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi")
            .unwrap()
            .unwrap();
        assert_eq!(p.type_name, "CIDv1 dag-pb");
    }

    #[test]
    fn ssh_ed25519() {
        let p = parse("ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIDtJVH9hM+2DyhmgRZBfeIDoVqCTbXY+0nKlS5pTkkXY user@example.com").unwrap().unwrap();
        assert_eq!(p.type_name, "SSH ed25519");
        assert_eq!(p.prefix.as_deref(), Some("AAAAC3NzaC1lZDI1NTE5AAAA"));
    }

    #[test]
    fn snowflake_decimal() {
        assert_eq!(
            parse("80351110224678912").unwrap().unwrap().type_name,
            "snowflake"
        );
    }

    #[test]
    fn text_fallback_is_none() {
        assert!(parse("hello world").unwrap().is_none());
    }

    // ===================================================================
    // Char-class helpers
    // ===================================================================
    #[test]
    fn char_class_helpers() {
        assert!(is_hex("0aF9"));
        assert!(!is_hex(""));
        assert!(!is_hex("0g"));
        assert!(is_base58("abcXYZ123"));
        assert!(!is_base58("")); // empty
        assert!(!is_base58("0OIl")); // base58 excludes 0 O I l
        assert!(is_bech32_either("QPZRY")); // case-insensitive
        assert!(!is_bech32_either(""));
        assert!(!is_bech32_either("bob")); // 'o','b' not in bech32
        assert!(is_base32_either("abc234"));
        assert!(!is_base32_either("")); // empty
        assert!(!is_base32_either("089")); // 0,8,9 not in base32
        assert!(all_in("abc", "abcdef"));
        assert!(!all_in("abx", "abcdef"));
        assert!(is_base64url_nopad("Ab9-_"));
        assert!(!is_base64url_nopad(""));
        assert!(!is_base64url_nopad("a+b")); // '+' not url-safe
    }

    // ===================================================================
    // CESR
    // ===================================================================
    #[test]
    fn cesr_empty_and_nonmatch() {
        assert!(parse_cesr("").unwrap().is_none());
        // first char '0' but wrong length
        assert!(parse_cesr("0AshortXX").unwrap().is_none());
        // valid-length one-char code but non-base64url char present
        let bad = format!("D{}", "*".repeat(43));
        assert!(parse_cesr(&bad).unwrap().is_none());
    }

    #[test]
    fn cesr_two_and_four_char_codes() {
        let two = format!("0A{}", "A".repeat(22)); // 24 chars total
        let p = parse_cesr(&two).unwrap().unwrap();
        assert_eq!(p.type_name, "CESR random 128-bit number");
        let four = format!("1AAA{}", "A".repeat(44)); // 48 chars total
        let p = parse_cesr(&four).unwrap().unwrap();
        assert_eq!(p.type_name, "CESR secp256k1 nt pubkey");
    }

    // Issue #36 — the CESR recognizer must cover the Indexer table (indexed
    // signatures) and the Dater (datetime, Matter 1AAG), instead of dropping
    // them to the base64url `raw` fallback. Vectors are authoritative — generated
    // from keripy 1.1.33 (`Siger`/`Dater`), hardcoded so the test has no keripy
    // dependency. See this.i:idxs1gs0. (qb64, expected CESR label) — one per
    // length class and per algorithm, small + big variants.
    const INDEXED_SIGS: &[(&str, &str)] = &[
        // small (hs1/hs2), fs 88 / 156
        ("ABCfhtCBiEx9ZZov6qDFWtAVn4bQgYhMfWWaL-qgxVrQFZ-G0IGITH1lmi_qoMVa0BWfhtCBiEx9ZZov6qDFWtAV", "Ed25519 idx sig"),
        ("BDCfhtCBiEx9ZZov6qDFWtAVn4bQgYhMfWWaL-qgxVrQFZ-G0IGITH1lmi_qoMVa0BWfhtCBiEx9ZZov6qDFWtAV", "Ed25519 idx sig"),
        ("CCCfhtCBiEx9ZZov6qDFWtAVn4bQgYhMfWWaL-qgxVrQFZ-G0IGITH1lmi_qoMVa0BWfhtCBiEx9ZZov6qDFWtAV", "secp256k1 idx sig"),
        ("EFCfhtCBiEx9ZZov6qDFWtAVn4bQgYhMfWWaL-qgxVrQFZ-G0IGITH1lmi_qoMVa0BWfhtCBiEx9ZZov6qDFWtAV", "secp256r1 idx sig"),
        ("0ACCAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5", "Ed448 idx sig"),
        // big (hs2), fs 92 / 160
        ("2AAFAFCfhtCBiEx9ZZov6qDFWtAVn4bQgYhMfWWaL-qgxVrQFZ-G0IGITH1lmi_qoMVa0BWfhtCBiEx9ZZov6qDFWtAV", "Ed25519 idx sig"),
        ("2CABABCfhtCBiEx9ZZov6qDFWtAVn4bQgYhMfWWaL-qgxVrQFZ-G0IGITH1lmi_qoMVa0BWfhtCBiEx9ZZov6qDFWtAV", "secp256k1 idx sig"),
        ("2EAHAHCfhtCBiEx9ZZov6qDFWtAVn4bQgYhMfWWaL-qgxVrQFZ-G0IGITH1lmi_qoMVa0BWfhtCBiEx9ZZov6qDFWtAV", "secp256r1 idx sig"),
        ("3AAADAADAQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5AQIDBAUGBwgJCgsMDQ4PEBESExQVFhcYGRobHB0eHyAhIiMkJSYnKCkqKywtLi8wMTIzNDU2Nzg5", "Ed448 idx sig"),
    ];
    // keri.core.coring.Dater(dts="2020-08-22T17:50:09.988921+00:00").qb64
    const DATER: &str = "1AAG2020-08-22T17c50c09d988921p00c00";

    #[test]
    fn issue36_indexed_sigs_recognized_not_raw() {
        for &(qb64, label) in INDEXED_SIGS {
            let p = parse_cesr(qb64)
                .unwrap()
                .unwrap_or_else(|| panic!("indexed sig fell through to raw: {qb64}"));
            assert_eq!(p.type_name, format!("CESR {label}"), "{qb64}");
            // The derivation code + index stay IN the core (rendered in cells and
            // hashed); nothing is split to prefix.
            assert!(p.prefix.is_none());
            assert_eq!(p.core, qb64);
        }
    }

    #[test]
    fn issue36_indexed_sigs_dispatch_via_parse() {
        for &(qb64, label) in INDEXED_SIGS {
            let p = parse(qb64).unwrap().unwrap();
            assert_eq!(p.type_name, format!("CESR {label}"));
        }
    }

    #[test]
    fn issue36_matter_vs_indexer_disambiguation_by_length() {
        // A 44-char 'A...' is the Matter Ed25519 SEED; an 88-char 'A...' is the
        // Indexer signature. Length must decide, not the leading char alone.
        let seed = format!("A{}", "A".repeat(43)); // 44 chars
        assert_eq!(
            parse_cesr(&seed).unwrap().unwrap().type_name,
            "CESR Ed25519 seed"
        );
        let sig = INDEXED_SIGS[0].0;
        assert_eq!(sig.chars().count(), 88);
        assert!(sig.starts_with('A'));
        assert_eq!(
            parse_cesr(sig).unwrap().unwrap().type_name,
            "CESR Ed25519 idx sig"
        );
    }

    #[test]
    fn issue36_dater_recognized_not_raw() {
        let p = parse_cesr(DATER)
            .unwrap()
            .expect("Dater fell through to raw");
        assert_eq!(p.type_name, "CESR datetime");
        assert_eq!(p.core, DATER);
    }

    // ===================================================================
    // SSH keys
    // ===================================================================
    #[test]
    fn ssh_rsa_and_ecdsa() {
        let rsa = parse(
            "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDSD+oM4kLidAptE5pjRA8OBIWNysc9reQJjK comment",
        )
        .unwrap()
        .unwrap();
        assert_eq!(rsa.type_name, "SSH rsa");
        // rsa prefix_length is 28 chars (longer than the 24-char match string).
        assert_eq!(rsa.prefix.as_deref(), Some("AAAAB3NzaC1yc2EAAAADAQABAAAB"));
        let ec = parse("AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBNSBA0Md9MCwp")
            .unwrap()
            .unwrap();
        assert_eq!(ec.type_name, "SSH ecdsa-nistp256");
    }

    #[test]
    fn ssh_bare_blob_fallback() {
        // AAAA-prefixed blob that matches no known key type -> generic "SSH key".
        let p = parse_ssh_key("AAAAXabcd1234").unwrap().unwrap();
        assert_eq!(p.type_name, "SSH key");
        assert_eq!(p.prefix.as_deref(), Some("AAAA"));
        // Not an SSH blob at all.
        assert!(parse_ssh_key("not a key").unwrap().is_none());
    }

    #[test]
    fn ssh_bare_aaaa_only_is_none() {
        // payload is exactly "AAAA" -> empty body -> Ok(None) fall-through.
        assert!(parse_ssh_key("AAAA").unwrap().is_none());
    }

    #[test]
    fn ssh_key_regex_branches() {
        assert!(ssh_key_regex("BBBBxxxx").is_none()); // no AAAA prefix
        assert!(ssh_key_regex("AAAA").is_none()); // empty rest
        assert!(ssh_key_regex("AAAA=abc").is_none()); // body starts with '='
        assert!(ssh_key_regex("AAAAab====").is_none()); // >3 padding
        let (pre, rest) = ssh_key_regex("AAAAabcd==").unwrap();
        assert_eq!(pre, "AAAA");
        assert_eq!(rest, "abcd==");
    }

    #[test]
    fn ssh_line_split_branches() {
        let (payload, comment) = ssh_line_split("ssh-rsa AAAAB3Nz hello").unwrap();
        assert_eq!(payload, "AAAAB3Nz");
        assert_eq!(comment.as_deref(), Some("hello"));
        // padding consumed, no comment
        let (payload, comment) = ssh_line_split("AAAAabcd==").unwrap();
        assert_eq!(payload, "AAAAabcd==");
        assert!(comment.is_none());
        // type token present but no AAAA payload -> None
        assert!(ssh_line_split("ssh-rsa notbase64").is_none());
        // trailing non-whitespace immediately after payload -> None
        assert!(ssh_line_split("AAAAabcd!x").is_none());
        // no recognizable payload at all
        assert!(ssh_line_split("zzz").is_none());
    }

    // ===================================================================
    // Bitcoin / Ripple / Litecoin / BCH / Stellar
    // ===================================================================
    #[test]
    fn bitcoin_legacy_and_segwit() {
        // Addresses must carry a VALID checksum to parse (real corpus
        // vectors), and a structural match with a bad checksum is rejected.
        let legacy = parse_bitcoin_address("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa")
            .unwrap()
            .unwrap();
        assert_eq!(legacy.type_name, "BTC legacy");
        assert_eq!(legacy.prefix.as_deref(), Some("1"));
        assert_eq!(legacy.suffix.as_ref().unwrap().chars().count(), 4);
        let segwit = parse_bitcoin_address("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
            .unwrap()
            .unwrap();
        assert_eq!(segwit.type_name, "BTC SegWit");
        // Legacy structural match, corrupted last checksum char -> reject.
        assert!(matches!(
            parse_bitcoin_address("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb"),
            Err(ParseError::Base58Check)
        ));
        // Segwit structural match, corrupted checksum -> reject.
        assert!(matches!(
            parse_bitcoin_address("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5"),
            Err(ParseError::Bech32Checksum)
        ));
        // too-short body -> no match
        assert!(parse_bitcoin_address(&format!("1{}", "a".repeat(20)))
            .unwrap()
            .is_none());
        // wrong leading char -> no match
        assert!(parse_bitcoin_address(&format!("z{}", "a".repeat(33)))
            .unwrap()
            .is_none());
    }

    #[test]
    fn match_prefix_bech32_none() {
        assert!(match_prefix_bech32("zzz", &["bc1"], 10, 20).is_none());
        // right prefix, body too short
        assert!(match_prefix_bech32("bc1qq", &["bc1"], 10, 20).is_none());
    }

    #[test]
    fn ripple_address() {
        let p = parse("rUocf1ixKzTuEe34kmVhRvGqNCofY1NJzV")
            .unwrap()
            .unwrap();
        assert_eq!(p.type_name, "XRP");
        assert_eq!(p.prefix.as_deref(), Some("r"));
        // wrong length
        assert!(parse_ripple_address("rabc").unwrap().is_none());
    }

    #[test]
    fn litecoin_legacy_and_bech32() {
        // A real base58check-valid Litecoin legacy address (mainnet 'L').
        let legacy = parse_litecoin_address("LKDyUEtTR1HXamkiEphisSiBJu6o3ZPE34")
            .unwrap()
            .unwrap();
        assert_eq!(legacy.type_name, "LTC legacy");
        assert_eq!(legacy.prefix.as_deref(), Some("L"));
        // Corrupted checksum on the legacy path -> reject.
        assert!(matches!(
            parse_litecoin_address("LKDyUEtTR1HXamkiEphisSiBJu6o3ZPE3a"),
            Err(ParseError::Base58Check)
        ));
        // ltc1 bech32 (corpus vector).
        let bech = parse("ltc1qw508d6qejxtdg4y5r3zarvary0c5xw7kgmn4n9")
            .unwrap()
            .unwrap();
        assert_eq!(bech.type_name, "LTC");
        // Corrupted ltc1 checksum -> reject.
        assert!(matches!(
            parse_litecoin_address("ltc1qw508d6qejxtdg4y5r3zarvary0c5xw7kgmn4n8"),
            Err(ParseError::Bech32Checksum)
        ));
        assert!(parse_litecoin_address("Lshort").unwrap().is_none());
    }

    #[test]
    fn bitcoin_cash_with_and_without_prefix() {
        // CashAddr must carry a valid 40-bit BCH checksum (real vectors).
        let p = parse("bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a")
            .unwrap()
            .unwrap();
        assert_eq!(p.type_name, "BCH");
        assert_eq!(p.prefix.as_deref(), Some("bitcoincash:"));
        // bchtest prefix branch (checksum computed under the bchtest hrp).
        let t = parse_bitcoin_cash_address("bchtest:qpur7lcrzhq247gvqs5n79hj0tz4edmdqg09nfandy")
            .unwrap()
            .unwrap();
        assert_eq!(t.prefix.as_deref(), Some("bchtest:"));
        // bare p/q body, no prefix -> checksum verified under the default
        // "bitcoincash" hrp.
        let bare = parse_bitcoin_cash_address("qpur7lcrzhq247gvqs5n79hj0tz4edmdqgthhwly2c")
            .unwrap()
            .unwrap();
        assert!(bare.prefix.is_none());
        // A structural CashAddr match with a corrupted checksum -> reject.
        assert!(matches!(
            parse_bitcoin_cash_address("bitcoincash:qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6q"),
            Err(ParseError::CashAddrChecksum)
        ));
        // wrong leading char
        assert!(parse_bitcoin_cash_address(&format!("z{}", "q".repeat(41)))
            .unwrap()
            .is_none());
    }

    #[test]
    fn stellar_public_and_muxed() {
        let g = parse("GCKFBEIYTKP5RDBQMUTAPDCDHF2TR4LPNRGW4JBQQTQUYZP4LDKP3SGM")
            .unwrap()
            .unwrap();
        assert_eq!(g.type_name, "XLM");
        assert_eq!(g.prefix.as_deref(), Some("G"));
        let m = parse("MA7QYNF7SOWQ3GLR2BGMZEHXAVIRZA4KVWLTJJFC7MGXUA74P7UJVAAAAAAAAAAAAAJLK")
            .unwrap()
            .unwrap();
        assert_eq!(m.type_name, "XLM muxed");
        assert_eq!(m.prefix.as_deref(), Some("M"));
        // wrong length
        assert!(parse_stellar_address("Gabc").unwrap().is_none());
    }

    // ===================================================================
    // UUID / ULID / Snowflake / LEI
    // ===================================================================
    #[test]
    fn uuid_braced_and_invalid() {
        let braced = parse_uuid("{550e8400-e29b-41d4-a716-446655440000}")
            .unwrap()
            .unwrap();
        assert_eq!(braced.core, "550e8400e29b41d4a716446655440000");
        // 31 hex digits -> wrong total length
        assert!(parse_uuid("550e8400e29b41d4a71644665544000")
            .unwrap()
            .is_none());
        // misplaced dash -> structure check fails
        assert!(parse_uuid("550e84-00e29b41d4a716446655440000")
            .unwrap()
            .is_none());
        // non-hex
        assert!(parse_uuid("zzze8400e29b41d4a716446655440000")
            .unwrap()
            .is_none());
        // valid 8-4-4-4-12 but trailing dash leaves unconsumed input
        assert!(parse_uuid("550e8400-e29b-41d4-a716-446655440000-")
            .unwrap()
            .is_none());
    }

    #[test]
    fn ulid_valid_and_aliases_and_invalid() {
        let p = parse("01ARZ3NDEKTSV4RRFFQ69G5FAV").unwrap().unwrap();
        assert_eq!(p.type_name, "ULID");
        // I/L/O aliases get normalized to 1/1/0
        let aliased = parse_ulid(&format!("OIL{}A", "0".repeat(22)))
            .unwrap()
            .unwrap();
        assert!(!aliased.core.contains('O'));
        assert!(!aliased.core.contains('I'));
        assert!(!aliased.core.contains('L'));
        // wrong length
        assert!(parse_ulid("01ARZ3").unwrap().is_none());
        // 'U' is disallowed in ULID
        assert!(parse_ulid(&format!("U{}", "0".repeat(25)))
            .unwrap()
            .is_none());
    }

    #[test]
    fn snowflake_branches() {
        assert_eq!(
            parse("80351110224678912").unwrap().unwrap().type_name,
            "snowflake"
        );
        // too few / too many digits
        assert!(parse_snowflake("123").unwrap().is_none());
        // non-digit
        assert!(parse_snowflake("8035111022467891x").unwrap().is_none());
        // 19 digits but value exceeds 63 bits (high bit set)
        assert!(parse_snowflake("9300000000000000000").unwrap().is_none());
    }

    #[test]
    fn lei_branches() {
        let p = parse("5493001KJTIIGC8Y1R12").unwrap().unwrap();
        assert_eq!(p.type_name, "LEI");
        // positions [4:6] must be "00"
        assert!(parse_lei("5493991KJTIIGC8Y1R12").unwrap().is_none());
        // wrong length
        assert!(parse_lei("549300").unwrap().is_none());
        // Valid structure (reserved "00" present) but bad MOD 97-10 check
        // digits -> REJECT (previously fell through to a bare base36 encoding).
        assert!(matches!(
            parse_lei("5493001KJTIIGC8Y1R99"),
            Err(ParseError::LeiChecksum)
        ));
    }

    #[test]
    fn lei_checksum_helper() {
        assert!(lei_checksum_ok("5493001KJTIIGC8Y1R12"));
        // lowercase / non-alnum chars make the helper reject
        assert!(!lei_checksum_ok("abc def"));
    }

    // ===================================================================
    // Checksum helpers — corpus-independent (exercised in the coverage job).
    // ===================================================================
    #[test]
    fn base58_decode_preserves_leading_zeros() {
        // A valid BTC legacy address (version byte 0x00 -> one leading '1').
        let raw = base58_decode_bytes("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa").unwrap();
        assert_eq!(raw[0], 0x00); // leading '1' -> 0x00 payload byte
        assert_eq!(raw.len(), 25); // 1 version + 20 hash + 4 checksum
                                   // A char outside the base58 alphabet -> None.
        assert!(base58_decode_bytes("0OIl").is_none());
    }

    #[test]
    fn base58check_ok_accepts_valid_rejects_corrupt() {
        assert!(base58check_ok("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNa"));
        assert!(!base58check_ok("1A1zP1eP5QGefi2DMPTfTL5SLmv7DivfNb"));
        // Too short to hold a 4-byte checksum -> false.
        assert!(!base58check_ok("1"));
    }

    #[test]
    fn cashaddr_verify_accepts_valid_rejects_corrupt() {
        assert!(cashaddr_verify(
            "bitcoincash",
            "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6a"
        ));
        assert!(!cashaddr_verify(
            "bitcoincash",
            "qpm2qsznhks23z7629mms6s4cwef74vcwvy22gdx6q"
        ));
        // A non-charset character in the payload -> false.
        assert!(!cashaddr_verify("bitcoincash", "qpm2!"));
    }

    #[test]
    fn cashaddr_polymod_is_zero_for_valid() {
        // The verify helper returns true iff cashaddr_polymod(values) == 0;
        // exercise the raw polymod on a valid vector and a mutated one.
        let ok = cashaddr_verify("bchtest", "qpur7lcrzhq247gvqs5n79hj0tz4edmdqg09nfandy");
        assert!(ok);
    }

    // ===================================================================
    // SWHID / gitoid
    // ===================================================================
    #[test]
    fn swhid_with_qualifiers() {
        // a `;`-separated qualifier tail is allowed and stripped from the core.
        let p = parse_swhid("swh:1:rev:309cf2674ee7a0749978cf8265ab91a60aea0f7d;origin=https://x")
            .unwrap()
            .unwrap();
        assert_eq!(p.core, "309cf2674ee7a0749978cf8265ab91a60aea0f7d");
        assert!(p.prefix_semantic);
    }

    #[test]
    fn gitoid_too_few_parts() {
        // splitn yields < 4 colon-separated parts.
        assert!(parse_gitoid("gitoid:blob").unwrap().is_none());
    }

    #[test]
    fn bech32_separator_at_index_zero_is_skipped() {
        // '1' at index 0 gives sep=0, outside the 1..=83 hrp range -> skipped.
        assert!(parse_bech32_address(&format!("1{}", "q".repeat(10)))
            .unwrap()
            .is_none());
    }

    #[test]
    fn swhid_and_gitoid_negatives() {
        // unknown object type
        assert!(
            parse_swhid("swh:1:zzz:309cf2674ee7a0749978cf8265ab91a60aea0f7d")
                .unwrap()
                .is_none()
        );
        // gitoid bad algo
        assert!(parse_gitoid("gitoid:blob:md5:abcd").unwrap().is_none());
        // gitoid wrong hash length
        assert!(parse_gitoid("gitoid:blob:sha1:abcd").unwrap().is_none());
        // gitoid bad object type
        assert!(
            parse_gitoid("gitoid:zzz:sha1:309cf2674ee7a0749978cf8265ab91a60aea0f7d")
                .unwrap()
                .is_none()
        );
        // not a gitoid at all
        assert!(parse_gitoid("hello").unwrap().is_none());
        // gitoid sha256 happy path
        let g = parse_gitoid(
            "gitoid:blob:sha256:473a0f4c3be8a93681a267e3b1e9a7dcda1185436fe141f7749120a303721813",
        )
        .unwrap()
        .unwrap();
        assert!(g.prefix_semantic);
    }

    // ===================================================================
    // bech32 checksum internals + generic bech32 address
    // ===================================================================
    #[test]
    fn bech32_polymod_and_expand() {
        assert_eq!(bech32_polymod(&[]), 1);
        assert_eq!(bech32_hrp_expand("a"), vec![3, 0, 1]); // 'a'=0x61
    }

    #[test]
    fn bech32_generic_cosmos() {
        let p = parse_bech32_address("cosmos1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnrk363e")
            .unwrap()
            .unwrap();
        assert_eq!(p.type_name, "bech32");
        assert_eq!(p.prefix.as_deref(), Some("cosmos1"));
        // no separator '1' -> no match
        assert!(parse_bech32_address("cosmosqqqq").unwrap().is_none());
        // A `<hrp>1<8+ bech32>` structural match with a bad checksum now
        // REJECTS (previously fell through to Ok(None)).
        assert!(matches!(
            parse_bech32_address("cosmos1qqqsyqcyq5rqwzqfpg9scrgwpugpzysnrk363f"),
            Err(ParseError::Bech32Checksum)
        ));
    }

    // ===================================================================
    // IPFS CID + multicodec/multihash/base32/uvarint
    // ===================================================================
    #[test]
    fn cid_v0_and_v1_raw() {
        let v0 = parse("QmYwAPJzv5CZsnA625s3Xf2nemtYgPpHdWEz79ojWnPbdG")
            .unwrap()
            .unwrap();
        assert_eq!(v0.type_name, "CIDv0");
        let raw = parse("bafkreigh2akiscaildcqabsyg3dfr6chu3fgpregiymsck7e7aqa4s52zy")
            .unwrap()
            .unwrap();
        assert_eq!(raw.type_name, "CIDv1 raw");
    }

    #[test]
    fn b32_decode_multicodec_direct() {
        let body = "afybeigdyrzt5sfp7udm7hu76uh7y26nf3efuylqabf3oclgtqy55fbzdi";
        let (codec, hash) = b32_decode_multicodec(body).unwrap();
        assert_eq!(codec, "dag-pb");
        assert_eq!(hash, "sha2-256");
    }

    #[test]
    fn multicodec_and_multihash_tables() {
        let codecs = [
            (0x00u64, "identity"),
            (0x51, "cbor"),
            (0x55, "raw"),
            (0x60, "rlp"),
            (0x70, "dag-pb"),
            (0x71, "dag-cbor"),
            (0x72, "libp2p-key"),
            (0x78, "git-raw"),
            (0x90, "eth-block"),
            (0x97, "eth-tx"),
            (0x0129, "dag-json"),
            (0x0202, "car"),
        ];
        for (code, name) in codecs {
            assert_eq!(multicodec_content(code), Some(name));
        }
        assert_eq!(multicodec_content(0xdead), None);

        let hashes = [
            (0x11u64, "sha1"),
            (0x12, "sha2-256"),
            (0x13, "sha2-512"),
            (0x14, "sha3-224"),
            (0x15, "sha3-256"),
            (0x16, "sha3-384"),
            (0x17, "sha3-512"),
            (0x1b, "keccak-256"),
            (0x41, "blake2b-256"),
        ];
        for (code, name) in hashes {
            assert_eq!(multihash_func(code), Some(name));
        }
        assert_eq!(multihash_func(0xff), None);
    }

    #[test]
    fn base32_decode_and_uvarint() {
        assert_eq!(base32_decode("MZXW6"), Some(b"foo".to_vec()));
        assert_eq!(base32_decode("0"), None); // '0' not in RFC4648 base32
        assert_eq!(read_uvarint(&[0x01], 0), Some((1, 1)));
        assert_eq!(read_uvarint(&[0xAC, 0x02], 0), Some((300, 2)));
        assert_eq!(read_uvarint(&[0x80], 0), None); // truncated (continuation bit, no next byte)
    }

    // ===================================================================
    // hex / EOS
    // ===================================================================
    #[test]
    fn hex_branches() {
        assert_eq!(
            parse_hex("0x1234").unwrap().unwrap().prefix.as_deref(),
            Some("0x")
        );
        assert_eq!(parse_hex("abcd").unwrap().unwrap().type_name, "hex");
        assert!(parse_hex("").unwrap().is_none()); // empty
        assert!(parse_hex("abc").unwrap().is_none()); // odd length, no prefix
        assert!(parse_hex("zzzz").unwrap().is_none()); // not hex
    }

    #[test]
    fn eos_branches() {
        let p = parse_eos_address("eosaccount1").unwrap().unwrap();
        assert_eq!(p.type_name, "EOS");
        // 13-char form (form 2): {12}[a-j1-5]
        assert!(eos_regex(&format!("{}a", "a".repeat(12))));
        // all-hex input is rejected as EOS (would be hex instead)
        assert!(parse_eos_address("abcdef").unwrap().is_none());
        // illegal char
        assert!(parse_eos_address("EOS!!").unwrap().is_none());
    }

    // ===================================================================
    // EIP-55 validation
    // ===================================================================
    #[test]
    fn eip55_no_prefix_paths() {
        // mixed-case 40-hex without 0x prefix, valid checksum -> ETH
        let p = parse_ethereum_address("5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed")
            .unwrap()
            .unwrap();
        assert_eq!(p.type_name, "ETH");
        // all-lowercase, no prefix, not mixed -> None (defers to hex parser)
        assert!(
            parse_ethereum_address("5aaeb6053f3e94c9b9a09f33669435e7ef1beaed")
                .unwrap()
                .is_none()
        );
        // wrong length
        assert!(parse_ethereum_address("0x1234").unwrap().is_none());
        // mixed-case bad checksum -> hard error
        assert!(matches!(
            parse_ethereum_address("5aaeb6053F3E94C9b9A09f33669435E7Ef1BeAed"),
            Err(ParseError::Eip55 { .. })
        ));
    }

    #[test]
    fn validate_eip55_direct() {
        assert!(validate_eip55("5aAeb6053F3E94C9b9A09f33669435E7Ef1BeAed").is_ok());
        assert!(matches!(
            validate_eip55("5AAeb6053F3E94C9b9A09f33669435E7Ef1BeAed"),
            Err(ParseError::Eip55 { .. })
        ));
    }

    // ===================================================================
    // Disproof-based alphabet detection + parse() core casing
    // ===================================================================
    #[test]
    fn detect_alphabet_by_disproof_branches() {
        assert!(detect_alphabet_by_disproof("").is_none());
        assert_eq!(
            detect_alphabet_by_disproof("abcdef0123").unwrap().name,
            "hex"
        );
        assert_eq!(
            detect_alphabet_by_disproof("wxyz67").unwrap().name,
            "base32"
        );
        assert_eq!(
            detect_alphabet_by_disproof("qpz089").unwrap().name,
            "bech32"
        );
        assert_eq!(detect_alphabet_by_disproof("ABC+/").unwrap().name, "base64");
        assert_eq!(
            detect_alphabet_by_disproof("ABC-_").unwrap().name,
            "base64url"
        );
    }

    #[test]
    fn parse_falls_back_to_detected_alphabet_with_casing() {
        // base32 fallback uppercases the core
        let p = parse("wxyz67wxyz67").unwrap().unwrap();
        assert_eq!(p.type_name, "base32");
        assert_eq!(p.core, "WXYZ67WXYZ67");
        // bech32 fallback lowercases the core
        let p = parse("QPZ08QPZ08").unwrap().unwrap();
        assert_eq!(p.type_name, "bech32");
        assert_eq!(p.core, "qpz08qpz08");
    }

    // ===================================================================
    // Large-input tokenization helpers
    // ===================================================================
    #[test]
    fn crockford5_encoding() {
        assert_eq!(crockford5(0), "00000");
        assert_eq!(crockford5(0x1F), "0000z"); // last symbol, lowercased
        assert_eq!(crockford5(1), "00001");
        assert_eq!(crockford5(0xFFFFFF).chars().count(), 5);
    }

    #[test]
    fn core_byte_length_helper() {
        // hex: 4 bits/char -> 2 chars per byte
        assert_eq!(core_byte_length("abcd", &HEX), 2);
        // base64: 6 bits/char
        assert_eq!(core_byte_length("AAAA", &BASE64), 3);
    }

    #[test]
    fn tokenize_entropy_small_is_not_truncated() {
        let (toks, truncated) = tokenize_entropy("0123456789abcdef", &HEX);
        assert!(!truncated);
        assert_eq!(toks.len(), 3);
    }

    #[test]
    fn tokenize_entropy_large_is_truncated_with_middle() {
        // 200 hex chars -> > 22 tokens / > 64 bytes -> head+middle+tail layout.
        let core = "a".repeat(200);
        let (toks, truncated) = tokenize_entropy(&core, &HEX);
        assert!(truncated);
        assert_eq!(toks.len(), 8 + 4 + 8); // head + fingerprint-middle + tail
                                           // indices are renumbered 0..20
        for (i, t) in toks.iter().enumerate() {
            assert_eq!(t.index, i);
        }
        // the 4 middle tokens are crockford5 of the second digest
        let second = second_digest(&core);
        for i in 0..4 {
            let quant = ((second[3 * i] as u32) << 16)
                | ((second[3 * i + 1] as u32) << 8)
                | (second[3 * i + 2] as u32);
            assert_eq!(toks[8 + i].text, crockford5(quant));
        }
    }
}