auths-keri 0.1.3

KERI protocol types, SAID computation, and validation
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
//! KEL validation: SAID verification, chain linkage, signature verification,
//! and pre-rotation commitment checks.
//!
//! This module provides validation functions for ensuring a Key Event Log
//! is cryptographically valid and properly chained.

use crate::crypto::verify_commitment;
use crate::events::{Event, IcpEvent, IxnEvent, KeriSequence, RotEvent, Seal, SourceSeal};
use crate::keys::KeriPublicKey;
use crate::said::compute_said;
use crate::state::KeyState;
use crate::types::{CesrKey, ConfigTrait, Prefix, Said, Threshold};
use crate::witness::WitnessReceiptLookup;
use crate::witness::agreement::{AgreementStatus, WitnessAgreement};

/// Errors specific to KEL validation.
///
/// These errors represent **protocol invariant violations**. They indicate
/// structural corruption or attack, not recoverable conditions.
#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)]
#[non_exhaustive]
pub enum ValidationError {
    /// SAID (Self-Addressing Identifier) doesn't match content hash.
    #[error("Invalid SAID: expected {expected}, got {actual}")]
    InvalidSaid {
        /// The SAID that was expected from the content hash.
        expected: Said,
        /// The SAID that was actually found in the event.
        actual: Said,
    },

    /// Event references wrong previous event.
    #[error("Broken chain: event {sequence} references {referenced}, but previous was {actual}")]
    BrokenChain {
        /// Zero-based position of the event in the KEL.
        sequence: u128,
        /// The previous SAID referenced by this event.
        referenced: Said,
        /// The actual SAID of the previous event.
        actual: Said,
    },

    /// Sequence number is not monotonically increasing.
    #[error("Invalid sequence: expected {expected}, got {actual}")]
    InvalidSequence {
        /// The sequence number that was expected.
        expected: u128,
        /// The sequence number that was found.
        actual: u128,
    },

    /// Pre-rotation commitment doesn't match the new current key.
    #[error("Pre-rotation commitment mismatch at sequence {sequence}")]
    CommitmentMismatch {
        /// Zero-based position of the rotation event that failed.
        sequence: u128,
    },

    /// Cryptographic signature verification failed for an event.
    #[error("Signature verification failed at sequence {sequence}")]
    SignatureFailed {
        /// Zero-based position of the event whose signature failed.
        sequence: u128,
    },

    /// A threshold (`kt`, `nt`, or `bt`) is structurally unsatisfiable against
    /// the list it governs — e.g. `kt=5` over a single key, or a weighted
    /// clause whose length differs from the key-list length.
    #[error("Unsatisfiable threshold at sequence {sequence}: {reason}")]
    ThresholdNotSatisfiable {
        /// Zero-based position of the offending event.
        sequence: u128,
        /// Which threshold and why it cannot be met.
        reason: String,
    },

    /// A rotation's backer delta is invalid: a `br` (cut) entry isn't in the
    /// prior backer set, or a `ba` (add) entry duplicates a surviving backer.
    #[error("Invalid backer delta at sequence {sequence}: {reason}")]
    InvalidBackerDelta {
        /// Zero-based position of the offending rotation.
        sequence: u128,
        /// What was wrong with the delta.
        reason: String,
    },

    /// A rotation flips the registrar-backer role (`RB` <-> `NRB`) while
    /// retaining prior backers via a partial `br`/`ba` delta. `RB` and `NRB`
    /// carry different backer-list semantics, so a surviving backer would be
    /// governed by semantics it was never admitted under. A role flip must
    /// rebuild `b[]` — every prior backer cut (F-23).
    #[error("Invalid backer role flip at sequence {sequence}: {reason}")]
    BackerRoleFlip {
        /// Zero-based position of the offending rotation.
        sequence: u128,
        /// Which roles flipped and how many backers survived.
        reason: String,
    },

    /// Rotation event's key-list size differs from the prior next-commitment
    /// list. Properly expressing this case requires CESR indexed-signature
    /// type codes so verified indices can be mapped distinctly against prior
    /// and current key lists. Until that lands, such rotations are rejected.
    #[error(
        "Asymmetric key rotation at sequence {sequence}: prior next count {prior_next_count} != new key count {new_key_count} (removing devices requires CESR indexed signatures)"
    )]
    AsymmetricKeyRotation {
        /// Zero-based position of the rotation event.
        sequence: u128,
        /// Number of entries in the prior event's next-commitment list.
        prior_next_count: usize,
        /// Number of entries in this rotation's key list.
        new_key_count: usize,
    },

    /// A delegated event (`dip` / `drt`) references a delegator but no
    /// matching seal could be found in the delegator's KEL.
    #[error(
        "Delegator seal not found at sequence {sequence}: delegator {delegator_aid} has no ixn-anchored seal for this event"
    )]
    DelegatorSealNotFound {
        /// Zero-based position of the delegated event.
        sequence: u128,
        /// Delegator AID the event referenced (dip.di / drt.di).
        delegator_aid: String,
    },

    /// A delegated event (`dip` / `drt`) has no delegate-side source seal
    /// (`-G` couple). The delegator anchored it, but the event itself doesn't
    /// point back at that anchoring event — a one-directional (and therefore
    /// non-keripy-interoperable, weakly-bound) delegation. Bilateral required.
    #[error(
        "Delegate source seal missing at sequence {sequence}: delegated event carries no -G back-reference to its anchoring event"
    )]
    DelegateSourceSealMissing {
        /// Zero-based position of the delegated event.
        sequence: u128,
    },

    /// A delegated event's source seal (`-G` couple) points at a different
    /// delegator event than the one that actually anchored it. The bilateral
    /// binding is broken: the delegate claims anchoring location L while the
    /// delegator's `Seal::KeyEvent` lives at L′ ≠ L.
    #[error(
        "Delegation source seal back-reference mismatch at sequence {sequence}: delegate points at a different anchoring event than the delegator's seal"
    )]
    SealBackRefMismatch {
        /// Zero-based position of the delegated event.
        sequence: u128,
    },

    /// A delegated event was submitted but no `DelegatorKelLookup` was
    /// provided. Use `validate_kel_with_lookup` when processing KELs that
    /// contain `dip` or `drt` events.
    #[error(
        "Delegator lookup required for delegated event at sequence {sequence}; call validate_kel_with_lookup"
    )]
    DelegatorLookupMissing {
        /// Zero-based position of the delegated event.
        sequence: u128,
    },

    /// The first event in a KEL must be an Inception event.
    #[error("First event must be inception")]
    NotInception,

    /// The KEL contains no events.
    #[error("Empty KEL")]
    EmptyKel,

    /// More than one Inception event was found in the KEL.
    #[error("Multiple inception events in KEL")]
    MultipleInceptions,

    /// JSON serialization or deserialization failed.
    #[error("Serialization error: {0}")]
    Serialization(String),

    /// A sequence field could not be parsed as a valid hex number.
    #[error("Malformed sequence number: {raw:?}")]
    MalformedSequence {
        /// The raw string that could not be parsed.
        raw: String,
    },

    /// The key encoding prefix is unsupported or malformed.
    #[error("Invalid key encoding: {0}")]
    InvalidKey(String),

    /// The identity has been abandoned (empty next commitment) and no more events are allowed.
    #[error("Identity abandoned at sequence {sequence}, no more events allowed")]
    AbandonedIdentity {
        /// The sequence number of the rejected event.
        sequence: u128,
    },

    /// An interaction event was found in an establishment-only KEL.
    #[error("Interaction event at sequence {sequence} rejected: KEL is establishment-only (EO)")]
    EstablishmentOnly {
        /// The sequence number of the rejected event.
        sequence: u128,
    },

    /// The identity is non-transferable (inception had empty next commitments).
    #[error(
        "Non-transferable identity: inception had empty next key commitments, no subsequent events allowed"
    )]
    NonTransferable,

    /// A backer AID appears more than once in the backer list.
    #[error("Duplicate backer AID: {aid}")]
    DuplicateBacker {
        /// The duplicated AID.
        aid: String,
    },

    /// The backer threshold is inconsistent with the backer list size.
    #[error("Invalid backer threshold: bt={bt} but backer_count={backer_count}")]
    InvalidBackerThreshold {
        /// The backer threshold value.
        bt: u64,
        /// The number of backers.
        backer_count: usize,
    },

    /// A policy-only variant: an establishment event is missing the `dt`
    /// field, so the cooldown cannot be enforced. Structural validation
    /// (`validate_kel`) permits missing `dt`; the policy validator
    /// (`validate_kel_with_policy`) does not.
    #[error("Policy violation: event at seq {sequence} missing `dt`")]
    MissingTimestamp {
        /// Zero-based position of the event in the KEL.
        sequence: u128,
    },

    /// Two consecutive events have non-monotonic `dt`.
    #[error(
        "Policy violation: timestamps not monotonic at seq {sequence} (prev={prev}, curr={curr})"
    )]
    NonMonotonicTimestamp {
        /// Zero-based position of the offending event.
        sequence: u128,
        /// Previous event's `dt`.
        prev: String,
        /// Current event's `dt`.
        curr: String,
    },

    /// Two rotations happened closer together than the configured
    /// cooldown allows (and the event is not an emergency override).
    #[error(
        "Policy violation: rotation cooldown breached at seq {sequence} (interval {interval_secs}s < minimum {min_secs}s)"
    )]
    RotationCooldown {
        /// Zero-based position of the offending rotation.
        sequence: u128,
        /// Observed inter-rotation interval (seconds).
        interval_secs: i64,
        /// Configured minimum interval (seconds).
        min_secs: i64,
    },

    /// An event's `dt` is beyond the configured clock-skew tolerance.
    #[error(
        "Policy violation: clock skew at seq {sequence} ({skew_secs}s) exceeds tolerance ({tolerance_secs}s)"
    )]
    ClockSkew {
        /// Zero-based position of the event.
        sequence: u128,
        /// Observed skew vs server clock (seconds, signed).
        skew_secs: i64,
        /// Configured tolerance (seconds).
        tolerance_secs: i64,
    },
}

/// Validate a delegated event against the delegator's KEL.
///
/// Searches the delegator's KEL for an anchoring key event seal that matches
/// the delegated event's prefix, sequence number, and SAID. Also enforces
/// the `DND` (Do Not Delegate) configuration trait.
///
/// Args:
/// * `delegated_event` - The delegated event (dip or drt) to validate.
/// * `delegator_kel` - The delegator's full KEL.
pub fn validate_delegation(
    delegated_event: &Event,
    delegator_kel: &[Event],
) -> Result<(), ValidationError> {
    if !delegated_event.is_delegated() {
        return Err(ValidationError::Serialization(
            "validate_delegation called on non-delegated event".to_string(),
        ));
    }

    let event_said = delegated_event.said();
    let event_seq = delegated_event.sequence();

    // Check DND enforcement on delegator
    if let Some(Event::Icp(delegator_icp)) = delegator_kel.first()
        && delegator_icp.c.contains(&ConfigTrait::DoNotDelegate)
    {
        return Err(ValidationError::Serialization(
            "Delegator has DoNotDelegate (DND) config trait".to_string(),
        ));
    }

    // Delegator side: find the anchoring event whose a[] carries a KeyEvent seal
    // for this delegated event, and capture that event's own (sequence, SAID).
    let anchor = delegator_kel.iter().find_map(|event| {
        let anchors = event.anchors().iter().any(|seal| {
            matches!(
                seal,
                Seal::KeyEvent { i, s, d }
                if i == delegated_event.prefix()
                    && s.value() == event_seq.value()
                    && d == event_said
            )
        });
        anchors.then(|| SourceSeal {
            s: event.sequence(),
            d: event.said().clone(),
        })
    });

    let Some(anchor) = anchor else {
        return Err(ValidationError::Serialization(format!(
            "No delegation seal found in delegator KEL for prefix={}, sn={}, said={}",
            delegated_event.prefix(),
            event_seq,
            event_said
        )));
    };

    // Delegate side: the event's -G source seal must point back at that exact
    // anchoring event. A missing or mismatched back-reference is rejected.
    enforce_source_seal(delegated_event.source_seal(), &anchor, event_seq.value())
}

/// Enforce the delegate side of the bilateral delegation binding: the delegated
/// event's `-G` source seal must be present and equal the delegator's anchoring
/// event `(sequence, SAID)`.
fn enforce_source_seal(
    source_seal: Option<&SourceSeal>,
    anchor: &SourceSeal,
    sequence: u128,
) -> Result<(), ValidationError> {
    match source_seal {
        None => Err(ValidationError::DelegateSourceSealMissing { sequence }),
        Some(seal) if seal == anchor => Ok(()),
        Some(_) => Err(ValidationError::SealBackRefMismatch { sequence }),
    }
}

/// Validate a KEL and return the resulting KeyState.
///
/// This is a **pure function** serving as the core entrypoint for KEL replay.
///
/// Args:
/// * `events` - The ordered list of KERI events to validate.
///
/// Usage:
/// ```ignore
/// let key_state = validate_kel(&events)?;
/// ```
/// Pluggable cross-KEL seal lookup for validating delegated events.
///
/// A delegated identifier's rotation or inception must be anchored by the
/// delegator's KEL via an `ixn` event whose `a[]` seal references the
/// delegated event's SAID. This trait lets the validator ask "does my
/// delegator have a seal for this event?" without depending on any
/// particular KEL storage backend.
pub trait DelegatorKelLookup {
    /// Return the delegator's anchoring event — its sequence **and** SAID — whose
    /// `a[]` carries a `Seal::KeyEvent` for `seal_said`, or `None` if the
    /// delegator's KEL contains none. The returned [`SourceSeal`] is exactly what
    /// the delegated event's `-G` back-reference must equal for the bilateral
    /// binding to hold.
    fn find_seal(&self, delegator_aid: &Prefix, seal_said: &Said) -> Option<SourceSeal>;
}

/// Validate a KEL with no delegator lookup.
///
/// Convenience wrapper over [`validate_kel_with_lookup`] for ordinary KELs
/// that contain only `icp`/`rot`/`ixn` events. Use the lookup variant for KELs
/// containing delegated events (`dip`/`drt`).
///
/// Args:
/// * `events` - The ordered list of KERI events to replay and validate.
pub fn validate_kel(events: &[Event]) -> Result<KeyState, ValidationError> {
    validate_kel_with_lookup(events, None::<&dyn DelegatorKelLookup>)
}

/// Validate a KEL with a delegator-lookup hook for delegated events.
///
/// Required when the KEL contains `dip` or `drt` events; ordinary KELs
/// (only `icp`/`rot`/`ixn`) can pass `None`.
pub fn validate_kel_with_lookup(
    events: &[Event],
    lookup: Option<&dyn DelegatorKelLookup>,
) -> Result<KeyState, ValidationError> {
    match replay_kel_gated(events, lookup, None)? {
        WitnessedReplay::Accepted(state) => Ok(state),
        // With no receipt lookup the gate never runs, so `Pending` is
        // unreachable; returning the structural state preserves the
        // no-receipt contract (advance regardless of receipts).
        WitnessedReplay::Pending { state, .. } => Ok(state),
    }
}

/// The outcome of replaying a KEL through the witness-receipt gate.
///
/// Unlike [`validate_kel`] (structural only), [`validate_kel_with_receipts`]
/// will not silently advance past an establishment event that lacks M-of-N
/// witness agreement — it reports [`WitnessedReplay::Pending`] so the caller
/// (verifier policy, D.7) can warn or refuse.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WitnessedReplay {
    /// Every `bt>0` establishment event reached witness quorum; the key-state
    /// is witness-authoritative.
    Accepted(KeyState),
    /// The KEL is structurally valid, but the establishment event at `sequence`
    /// did not reach quorum. `state` is the structural replay through that event;
    /// the caller must not treat key-state at or after `sequence` as
    /// witness-authoritative.
    Pending {
        /// Structural replay result through the under-quorum event.
        state: KeyState,
        /// Sequence of the first under-quorum establishment event.
        sequence: u128,
        /// SAID of that event.
        said: Said,
        /// The backer threshold that was required.
        required: Threshold,
        /// Distinct, in-force witness receipts collected for it.
        collected: usize,
    },
}

impl WitnessedReplay {
    /// The replayed key-state, regardless of the witness-quorum outcome.
    pub fn state(&self) -> &KeyState {
        match self {
            WitnessedReplay::Accepted(state) | WitnessedReplay::Pending { state, .. } => state,
        }
    }
}

/// Validate a KEL and gate each establishment event on M-of-N witness receipts.
///
/// Extends [`validate_kel_with_lookup`] with receipt-gated replay: a `bt>0`
/// establishment event advances `KeyState` only when KAWA
/// ([`WitnessAgreement`](crate::witness::agreement::WitnessAgreement)) reports
/// agreement over receipts from **distinct** witnesses in the `b[]` set **in
/// force at that sequence**. `bt=0` events accept without receipts (the
/// zero-witness path). Receipts are matched by `(controller, sn, said)` via
/// `receipt_lookup` and deduped by witness AID; a receipt from a non-designated
/// witness never counts.
///
/// Args:
/// * `events`: The ordered KEL to replay.
/// * `delegator_lookup`: Cross-KEL seal lookup for delegated events (`dip`/`drt`).
/// * `receipt_lookup`: Source of witness receipts per event.
///
/// Usage:
/// ```ignore
/// match validate_kel_with_receipts(&events, None, &receipts)? {
///     WitnessedReplay::Accepted(state) => trust(state),
///     WitnessedReplay::Pending { sequence, .. } => warn_or_refuse(sequence),
/// }
/// ```
pub fn validate_kel_with_receipts(
    events: &[Event],
    delegator_lookup: Option<&dyn DelegatorKelLookup>,
    receipt_lookup: &dyn WitnessReceiptLookup,
) -> Result<WitnessedReplay, ValidationError> {
    replay_kel_gated(events, delegator_lookup, Some(receipt_lookup))
}

/// Shared structural replay with an optional witness-receipt gate.
///
/// With `receipt_lookup = None` this is pure structural replay (the
/// [`validate_kel`] contract). With `Some(_)` each establishment event is gated
/// on witness quorum; the first under-quorum event short-circuits to
/// [`WitnessedReplay::Pending`].
fn replay_kel_gated(
    events: &[Event],
    lookup: Option<&dyn DelegatorKelLookup>,
    receipt_lookup: Option<&dyn WitnessReceiptLookup>,
) -> Result<WitnessedReplay, ValidationError> {
    if events.is_empty() {
        return Err(ValidationError::EmptyKel);
    }

    verify_event_said(&events[0])?;
    let (mut state, inception_n_is_empty, establishment_only) = match &events[0] {
        Event::Icp(icp) => (
            validate_inception(icp)?,
            icp.n.is_empty(),
            icp.c.contains(&ConfigTrait::EstablishmentOnly),
        ),
        Event::Dip(dip) => (
            validate_delegated_inception(dip, lookup)?,
            dip.n.is_empty(),
            dip.c.contains(&ConfigTrait::EstablishmentOnly),
        ),
        _ => return Err(ValidationError::NotInception),
    };

    let controller = state.prefix.clone();

    // Gate the inception establishment event on witness quorum.
    if let Some(rl) = receipt_lookup
        && let Some(pending) = gate_establishment(&controller, &state, 0, events[0].said(), rl)
    {
        return Ok(pending);
    }

    // Non-transferable identities (inception n is empty) cannot have subsequent events
    if inception_n_is_empty && events.len() > 1 {
        return Err(ValidationError::NonTransferable);
    }

    for (idx, event) in events.iter().enumerate().skip(1) {
        let expected_seq = idx as u128;

        // Reject any event after abandonment
        if state.is_abandoned {
            return Err(ValidationError::AbandonedIdentity {
                sequence: expected_seq,
            });
        }

        // Reject IXN in establishment-only KELs
        if establishment_only && matches!(event, Event::Ixn(_)) {
            return Err(ValidationError::EstablishmentOnly {
                sequence: expected_seq,
            });
        }

        verify_event_said(event)?;
        verify_sequence(event, expected_seq)?;
        verify_chain_linkage(event, &state)?;

        match event {
            Event::Rot(rot) => validate_rotation(rot, expected_seq, &mut state)?,
            Event::Ixn(ixn) => validate_interaction(ixn, expected_seq, &mut state)?,
            Event::Icp(_) | Event::Dip(_) => return Err(ValidationError::MultipleInceptions),
            Event::Drt(drt) => {
                validate_delegated_rotation(drt, expected_seq, &mut state, lookup)?;
            }
        }

        // Gate establishment events (rot/drt) on witness quorum; ixn never gates.
        if let Some(rl) = receipt_lookup
            && matches!(event, Event::Rot(_) | Event::Drt(_))
            && let Some(pending) =
                gate_establishment(&controller, &state, expected_seq, event.said(), rl)
        {
            return Ok(pending);
        }
    }

    Ok(WitnessedReplay::Accepted(state))
}

/// Gate one establishment event on M-of-N witness agreement.
///
/// Returns `Some(WitnessedReplay::Pending)` when the in-force backer threshold
/// is not met by distinct designated-witness receipts, or `None` when the event
/// is witness-accepted (including the `bt=0` zero-witness path). KAWA does the
/// M-of-N math and the AID dedupe / non-designated-witness filtering.
fn gate_establishment(
    controller: &Prefix,
    state: &KeyState,
    sequence: u128,
    event_said: &Said,
    receipt_lookup: &dyn WitnessReceiptLookup,
) -> Option<WitnessedReplay> {
    let sn = sequence as u64;
    let agreement = WitnessAgreement::new(1);
    agreement.submit_event(
        controller,
        sn,
        event_said,
        &state.backer_threshold,
        &state.backers,
    );
    for receipt in receipt_lookup.receipts_for(controller, KeriSequence::new(sequence), event_said)
    {
        agreement.add_receipt(controller, sn, event_said, receipt.witness.as_str());
    }
    match agreement.status(controller, sn, event_said) {
        AgreementStatus::Accepted => None,
        AgreementStatus::Pending { collected } => Some(WitnessedReplay::Pending {
            state: state.clone(),
            sequence,
            said: event_said.clone(),
            required: state.backer_threshold.clone(),
            collected,
        }),
    }
}

fn validate_backer_uniqueness(backers: &[Prefix]) -> Result<(), ValidationError> {
    let mut seen = std::collections::HashSet::new();
    for b in backers {
        if !seen.insert(b.as_str()) {
            return Err(ValidationError::DuplicateBacker {
                aid: b.as_str().to_string(),
            });
        }
    }
    Ok(())
}

/// Structural threshold satisfiability for an establishment event's
/// `kt`/`nt`/`bt` against the key, next-commitment, and backer lists.
fn validate_thresholds(
    sequence: u128,
    kt: &Threshold,
    k_len: usize,
    nt: &Threshold,
    n_len: usize,
    bt: &Threshold,
    b_len: usize,
) -> Result<(), ValidationError> {
    let check = |t: &Threshold, len: usize, which: &str| {
        t.validate_satisfiable(len)
            .map_err(|e| ValidationError::ThresholdNotSatisfiable {
                sequence,
                reason: format!("{which}: {}", e.reason),
            })
    };
    check(kt, k_len, "kt")?;
    check(nt, n_len, "nt")?;
    check(bt, b_len, "bt")?;
    Ok(())
}

fn validate_inception(icp: &IcpEvent) -> Result<KeyState, ValidationError> {
    // Validate backer uniqueness
    validate_backer_uniqueness(&icp.b)?;

    // Threshold satisfiability (kt over k, nt over n, bt over b).
    validate_thresholds(
        icp.s.value(),
        &icp.kt,
        icp.k.len(),
        &icp.nt,
        icp.n.len(),
        &icp.bt,
        icp.b.len(),
    )?;

    // Validate bt consistency: empty backers must have bt == 0
    let bt_val = icp.bt.simple_value().unwrap_or(0);
    if icp.b.is_empty() && bt_val != 0 {
        return Err(ValidationError::InvalidBackerThreshold {
            bt: bt_val,
            backer_count: 0,
        });
    }

    Ok(KeyState::from_inception(
        icp.i.clone(),
        icp.k.clone(),
        icp.n.clone(),
        icp.kt.clone(),
        icp.nt.clone(),
        icp.d.clone(),
        icp.b.clone(),
        icp.bt.clone(),
        icp.c.clone(),
    ))
}

fn verify_sequence(event: &Event, expected: u128) -> Result<(), ValidationError> {
    let actual = event.sequence().value();
    if actual != expected {
        return Err(ValidationError::InvalidSequence { expected, actual });
    }
    Ok(())
}

fn verify_chain_linkage(event: &Event, state: &KeyState) -> Result<(), ValidationError> {
    let prev_said = event.previous().ok_or(ValidationError::NotInception)?;
    if *prev_said != state.last_event_said {
        return Err(ValidationError::BrokenChain {
            sequence: event.sequence().value(),
            referenced: prev_said.clone(),
            actual: state.last_event_said.clone(),
        });
    }
    Ok(())
}

/// Returns whether the new key list reveals enough prior next-key commitments
/// to satisfy the typed prior `nt` threshold.
///
/// Each prior commitment index `j` counts as "revealed" when some new key
/// hashes to `next_commitment[j]`; the typed [`Threshold::is_satisfied`] then
/// decides over those indices. This replaces the legacy
/// `simple_value().unwrap_or(1)` collapse, which silently reduced any weighted
/// `nt` to a 1-of-N (F-15).
fn prior_commitments_satisfy_threshold(
    next_commitment: &[Said],
    next_threshold: &Threshold,
    new_keys: &[CesrKey],
) -> bool {
    let revealed: Vec<u32> = next_commitment
        .iter()
        .enumerate()
        .filter_map(|(j, commitment)| {
            let matched = new_keys.iter().any(|key| {
                key.parse()
                    .map(|pk| verify_commitment(&pk, commitment))
                    .unwrap_or(false)
            });
            matched.then_some(j as u32)
        })
        .collect();
    next_threshold.is_satisfied(&revealed, next_commitment.len())
}

/// Registrar-backer role designated by an event's config traits.
///
/// `RB` and `NRB` are mutually exclusive backer semantics; the latter wins when
/// both appear (per [`ConfigTrait`] supersedence). `Unspecified` means the
/// event's `c[]` named neither, so the role is inherited rather than changed.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum BackerRole {
    Registrar,
    NoRegistrar,
    Unspecified,
}

/// Resolve the registrar-backer role designated by a config-trait list.
fn backer_role(traits: &[ConfigTrait]) -> BackerRole {
    let mut role = BackerRole::Unspecified;
    for t in traits {
        match t {
            ConfigTrait::RegistrarBackers => role = BackerRole::Registrar,
            ConfigTrait::NoRegistrarBackers => role = BackerRole::NoRegistrar,
            _ => {}
        }
    }
    role
}

fn validate_rotation(
    rot: &RotEvent,
    sequence: u128,
    state: &mut KeyState,
) -> Result<(), ValidationError> {
    // Threshold satisfiability for the new establishment config. `br`/`ba` are
    // deltas, so the post-rotation backer count is the prior set minus removals
    // plus additions.
    let post_backer_count =
        state.backers.iter().filter(|b| !rot.br.contains(b)).count() + rot.ba.len();
    validate_thresholds(
        sequence,
        &rot.kt,
        rot.k.len(),
        &rot.nt,
        rot.n.len(),
        &rot.bt,
        post_backer_count,
    )?;

    // Verify all pre-rotation commitments against the typed prior `nt`.
    if !state.next_commitment.is_empty()
        && !prior_commitments_satisfy_threshold(
            &state.next_commitment,
            &state.next_threshold,
            &rot.k,
        )
    {
        return Err(ValidationError::CommitmentMismatch { sequence });
    }

    // Validate backer uniqueness within br and ba.
    validate_backer_uniqueness(&rot.br)?;
    validate_backer_uniqueness(&rot.ba)?;
    // br and ba must not overlap.
    for aid in &rot.ba {
        if rot.br.contains(aid) {
            return Err(ValidationError::DuplicateBacker {
                aid: aid.as_str().to_string(),
            });
        }
    }
    // Each `br` (cut) must be a current backer; each `ba` (add) must not already
    // be a surviving backer. Otherwise apply_rotation's retain+extend would
    // silently corrupt the backer set and `bt` accounting (F-05).
    for aid in &rot.br {
        if !state.backers.contains(aid) {
            return Err(ValidationError::InvalidBackerDelta {
                sequence,
                reason: format!("br entry {} not in prior backers", aid.as_str()),
            });
        }
    }
    let survivors: Vec<_> = state
        .backers
        .iter()
        .filter(|b| !rot.br.contains(b))
        .collect();
    for aid in &rot.ba {
        if survivors.contains(&aid) {
            return Err(ValidationError::InvalidBackerDelta {
                sequence,
                reason: format!("ba entry {} duplicates a surviving backer", aid.as_str()),
            });
        }
    }

    // Reject a silent RB<->NRB role flip that retains prior backers. A
    // non-empty `c[]` naming the opposite role must rebuild `b[]` — cut every
    // prior backer — or a survivor ends up governed by semantics it was never
    // admitted under (F-23). An empty `c[]` inherits the role, so cannot flip.
    if !rot.c.is_empty() {
        let old_role = backer_role(&state.config_traits);
        let new_role = backer_role(&rot.c);
        let is_flip = matches!(
            (old_role, new_role),
            (BackerRole::Registrar, BackerRole::NoRegistrar)
                | (BackerRole::NoRegistrar, BackerRole::Registrar)
        );
        if is_flip && !survivors.is_empty() {
            return Err(ValidationError::BackerRoleFlip {
                sequence,
                reason: format!(
                    "{old_role:?}->{new_role:?} but {} prior backer(s) survive; \
                     a role flip must cut all prior backers",
                    survivors.len()
                ),
            });
        }
    }

    state.apply_rotation(
        rot.k.clone(),
        rot.n.clone(),
        rot.kt.clone(),
        rot.nt.clone(),
        sequence,
        rot.d.clone(),
        &rot.br,
        &rot.ba,
        rot.bt.clone(),
        rot.c.clone(),
    );

    Ok(())
}

fn validate_interaction(
    ixn: &IxnEvent,
    sequence: u128,
    state: &mut KeyState,
) -> Result<(), ValidationError> {
    // Presence check: ixn events are only valid against a transferable,
    // non-abandoned identity with an available current key. The value itself
    // is not used here — signature verification against it happens at the
    // KEL-ingest boundary.
    state
        .current_key()
        .ok_or(ValidationError::SignatureFailed { sequence })?;
    state.apply_interaction(sequence, ixn.d.clone());
    Ok(())
}

/// Validate a delegated inception event (`dip`) per KERI §11.
///
/// Beyond the standard inception checks, the validator requires the
/// delegator's KEL to contain an `ixn` event whose `a[]` seal references
/// `dip.d`. Without that seal the delegated identifier is not authorized.
fn validate_delegated_inception(
    dip: &crate::events::DipEvent,
    lookup: Option<&dyn DelegatorKelLookup>,
) -> Result<KeyState, ValidationError> {
    let sequence = dip.s.value();
    let lookup = lookup.ok_or(ValidationError::DelegatorLookupMissing { sequence })?;

    // Bilateral delegation binding: the delegator anchored this dip (delegator
    // side) AND the dip's -G source seal points back at that exact anchoring
    // event (delegate side).
    let anchor = lookup.find_seal(&dip.di, &dip.d).ok_or_else(|| {
        ValidationError::DelegatorSealNotFound {
            sequence,
            delegator_aid: dip.di.as_str().to_string(),
        }
    })?;
    enforce_source_seal(dip.source_seal.as_ref(), &anchor, sequence)?;

    // Structural checks mirrored from `validate_inception` — backers, threshold.
    validate_backer_uniqueness(&dip.b)?;
    let bt_val = dip.bt.simple_value().unwrap_or(0);
    if dip.b.is_empty() && bt_val != 0 {
        return Err(ValidationError::InvalidBackerThreshold {
            bt: bt_val,
            backer_count: 0,
        });
    }

    // Build state from the dip event.
    let is_non_transferable = dip.n.is_empty();
    Ok(KeyState {
        prefix: dip.i.clone(),
        current_keys: dip.k.clone(),
        next_commitment: dip.n.clone(),
        sequence: dip.s.value(),
        last_event_said: dip.d.clone(),
        is_abandoned: false,
        threshold: dip.kt.clone(),
        next_threshold: dip.nt.clone(),
        backers: dip.b.clone(),
        backer_threshold: dip.bt.clone(),
        config_traits: dip.c.clone(),
        is_non_transferable,
        delegator: Some(dip.di.clone()),
        last_establishment_sequence: dip.s.value(),
    })
}

/// Validate a delegated rotation event (`drt`) per KERI §11.
///
/// Requires the delegator's KEL to contain an `ixn` event anchoring this
/// rotation via its SAID. Standard rotation rules also apply (chain,
/// sequence, pre-rotation commitment).
fn validate_delegated_rotation(
    drt: &crate::events::DrtEvent,
    sequence: u128,
    state: &mut KeyState,
    lookup: Option<&dyn DelegatorKelLookup>,
) -> Result<(), ValidationError> {
    let lookup = lookup.ok_or(ValidationError::DelegatorLookupMissing { sequence })?;

    // Bilateral delegation binding (as for dip): delegator-anchored seal AND the
    // drt's -G source seal pointing back at that anchoring event.
    let anchor = lookup.find_seal(&drt.di, &drt.d).ok_or_else(|| {
        ValidationError::DelegatorSealNotFound {
            sequence,
            delegator_aid: drt.di.as_str().to_string(),
        }
    })?;
    enforce_source_seal(drt.source_seal.as_ref(), &anchor, sequence)?;

    // Standard rotation commitment/backer checks applied to drt fields.
    if !state.next_commitment.is_empty()
        && !prior_commitments_satisfy_threshold(
            &state.next_commitment,
            &state.next_threshold,
            &drt.k,
        )
    {
        return Err(ValidationError::CommitmentMismatch { sequence });
    }

    validate_backer_uniqueness(&drt.br)?;
    validate_backer_uniqueness(&drt.ba)?;
    for aid in &drt.ba {
        if drt.br.contains(aid) {
            return Err(ValidationError::DuplicateBacker {
                aid: aid.as_str().to_string(),
            });
        }
    }

    // Apply: the rotation advances the KEL state the same way a plain rot would.
    state.sequence = sequence;
    state.last_event_said = drt.d.clone();
    state.current_keys = drt.k.clone();
    state.next_commitment = drt.n.clone();
    state.threshold = drt.kt.clone();
    state.next_threshold = drt.nt.clone();
    Ok(())
}

/// Replay a KEL to get the current KeyState.
///
/// Alias for [`validate_kel`] — use whichever name fits your context better.
///
/// Args:
/// * `events` - The ordered list of KERI events to replay.
pub fn replay_kel(events: &[Event]) -> Result<KeyState, ValidationError> {
    validate_kel(events)
}

/// Validate the cryptographic integrity of a single event against the current key state.
///
/// Args:
/// * `event` - The event to validate.
/// * `current_state` - The current `KeyState` (None for inception events).
pub fn verify_event_crypto(
    event: &Event,
    current_state: Option<&KeyState>,
) -> Result<(), ValidationError> {
    match event {
        Event::Icp(icp) => {
            // Presence check only: icp must commit at least one key.
            if icp.k.is_empty() {
                return Err(ValidationError::SignatureFailed { sequence: 0 });
            }

            // Self-addressing AIDs (E-prefixed): `i` MUST equal the SAID `d`.
            let is_self_addressing = icp.i.as_str().starts_with('E');
            if is_self_addressing {
                if icp.i.as_str() != icp.d.as_str() {
                    return Err(ValidationError::InvalidSaid {
                        expected: icp.d.clone(),
                        actual: Said::new_unchecked(icp.i.as_str().to_string()),
                    });
                }
            } else {
                // Basic-derivation AIDs (D / 1AAI / 1AAJ ...): the prefix IS the
                // single inception key, so `i` MUST equal `k[0]`. Without this a
                // basic-derivation prefix could point at an arbitrary key list.
                let i_key = KeriPublicKey::parse(icp.i.as_str())
                    .map_err(|_| ValidationError::SignatureFailed { sequence: 0 })?;
                let k0 = icp.k[0]
                    .parse()
                    .map_err(|_| ValidationError::SignatureFailed { sequence: 0 })?;
                if i_key.as_bytes() != k0.as_bytes() {
                    return Err(ValidationError::InvalidSaid {
                        expected: Said::new_unchecked(icp.k[0].as_str().to_string()),
                        actual: Said::new_unchecked(icp.i.as_str().to_string()),
                    });
                }
            }

            Ok(())
        }
        Event::Rot(rot) => {
            let sequence = event.sequence().value();
            let state = current_state.ok_or(ValidationError::SignatureFailed { sequence })?;

            if state.is_abandoned || state.next_commitment.is_empty() {
                return Err(ValidationError::CommitmentMismatch { sequence });
            }

            if rot.k.is_empty() {
                return Err(ValidationError::SignatureFailed { sequence });
            }

            // Verify pre-rotation commitments against the typed prior `nt`.
            if !prior_commitments_satisfy_threshold(
                &state.next_commitment,
                &state.next_threshold,
                &rot.k,
            ) {
                return Err(ValidationError::CommitmentMismatch { sequence });
            }

            Ok(())
        }
        Event::Ixn(_) => {
            let sequence = event.sequence().value();
            let state = current_state.ok_or(ValidationError::SignatureFailed { sequence })?;

            // Presence check: ixn requires a transferable, non-abandoned state
            // with an available current key.
            state
                .current_key()
                .ok_or(ValidationError::SignatureFailed { sequence })?;

            Ok(())
        }
        // Delegated events use same crypto verification as their non-delegated counterparts
        Event::Dip(dip) => {
            if dip.k.is_empty() {
                return Err(ValidationError::SignatureFailed { sequence: 0 });
            }
            Ok(())
        }
        Event::Drt(drt) => {
            let sequence = event.sequence().value();
            let state = current_state.ok_or(ValidationError::SignatureFailed { sequence })?;

            if state.is_abandoned || state.next_commitment.is_empty() {
                return Err(ValidationError::CommitmentMismatch { sequence });
            }
            if drt.k.is_empty() {
                return Err(ValidationError::SignatureFailed { sequence });
            }
            Ok(())
        }
    }
}

/// Verify an event's SAID matches its content hash.
///
/// Args:
/// * `event` - The event to verify.
pub fn verify_event_said(event: &Event) -> Result<(), ValidationError> {
    let value =
        serde_json::to_value(event).map_err(|e| ValidationError::Serialization(e.to_string()))?;
    let computed =
        compute_said(&value).map_err(|e| ValidationError::Serialization(e.to_string()))?;
    let actual = event.said();

    if computed != *actual {
        return Err(ValidationError::InvalidSaid {
            expected: computed,
            actual: actual.clone(),
        });
    }

    Ok(())
}

/// Validate a single event for appending to a KEL with known state.
///
/// Args:
/// * `event` - The event to validate for append.
/// * `state` - The current `KeyState` (tip of the existing KEL).
pub fn validate_for_append(event: &Event, state: &KeyState) -> Result<(), ValidationError> {
    if matches!(event, Event::Icp(_)) {
        return Err(ValidationError::MultipleInceptions);
    }

    verify_event_said(event)?;
    verify_sequence(event, state.sequence + 1)?;
    verify_chain_linkage(event, state)?;
    verify_event_crypto(event, Some(state))?;

    Ok(())
}

/// Compute the SAID for an event.
///
/// Args:
/// * `event` - The event to compute the SAID for.
pub fn compute_event_said(event: &Event) -> Result<Said, ValidationError> {
    let value =
        serde_json::to_value(event).map_err(|e| ValidationError::Serialization(e.to_string()))?;
    compute_said(&value).map_err(|e| ValidationError::Serialization(e.to_string()))
}

/// Serialize a finalized event for signing.
///
/// KERI signs over the fully-formed event bytes — `d` (SAID) and `i` (prefix)
/// already populated by `finalize_*_event`, and the version string declaring
/// the true body length. A spec verifier (KERIpy/KERIox) parses `v` first and
/// frames the body by that length, so the signed bytes MUST equal the wire
/// bytes. (The prior implementation cleared `d`/`i` after finalization, making
/// the signed body shorter than `v` claimed — a hard interop break.)
///
/// Args:
/// * `event` - The finalized event to serialize for signing.
pub fn serialize_for_signing(event: &Event) -> Result<Vec<u8>, ValidationError> {
    serde_json::to_vec(event).map_err(|e| ValidationError::Serialization(e.to_string()))
}

/// Validate a signed event's crypto (signatures + commitments) against key state.
///
/// This is the preferred entry point for validating events with externalized signatures.
///
/// Args:
/// * `signed` - The signed event with detached signatures.
/// * `current_state` - The current `KeyState` (None for inception events).
pub fn validate_signed_event(
    signed: &crate::events::SignedEvent,
    current_state: Option<&KeyState>,
) -> Result<(), ValidationError> {
    let event = &signed.event;
    let sequence = event.sequence().value();

    if signed.signatures.is_empty() {
        return Err(ValidationError::SignatureFailed { sequence });
    }

    // Determine the key list and threshold for verification
    let (keys, threshold) = match event {
        Event::Icp(icp) => (&icp.k, &icp.kt),
        Event::Dip(dip) => (&dip.k, &dip.kt),
        Event::Rot(rot) => (&rot.k, &rot.kt),
        Event::Drt(drt) => (&drt.k, &drt.kt),
        Event::Ixn(_) => {
            let state = current_state.ok_or(ValidationError::SignatureFailed { sequence })?;
            (&state.current_keys, &state.threshold)
        }
    };

    if keys.is_empty() {
        return Err(ValidationError::SignatureFailed { sequence });
    }

    // Verify each signature and collect verified indices
    let canonical = serialize_for_signing(event)?;
    let mut verified_indices = Vec::new();

    for sig in &signed.signatures {
        let idx = sig.index as usize;
        if idx >= keys.len() {
            continue; // out-of-range index, skip
        }
        let key = &keys[idx];
        if let Ok(pk) = key.parse()
            && pk.verify_signature(&canonical, &sig.sig).is_ok()
        {
            verified_indices.push(sig.index);
        }
    }

    // Check threshold satisfaction (current key threshold)
    if !threshold.is_satisfied(&verified_indices, keys.len()) {
        return Err(ValidationError::SignatureFailed { sequence });
    }

    // For rotation events: also check prior next-threshold from the previous
    // establishment event. The spec requires signatures satisfy BOTH the current
    // signing threshold AND the prior next rotation threshold.
    if matches!(event, Event::Rot(_) | Event::Drt(_))
        && let Some(state) = current_state
    {
        let n_len = state.next_commitment.len();

        // Bind each verifying signature to the prior commitment it reveals: the
        // new key `k[index]` must hash to `n[prior_index]` (or `n[index]` for a
        // single-index sig, where keripy emits code `A` with ondex == index). The
        // prior `nt` must then be met over the DISTINCT prior-commitment indices.
        let mut verified_prior: Vec<u32> = Vec::new();
        for sig in &signed.signatures {
            let Some(key) = keys.get(sig.index as usize) else {
                continue;
            };
            let Ok(pk) = key.parse() else {
                continue;
            };
            if pk.verify_signature(&canonical, &sig.sig).is_err() {
                continue;
            }
            let j = sig.prior_index.unwrap_or(sig.index) as usize;
            let Some(commitment) = state.next_commitment.get(j) else {
                continue;
            };
            if crate::crypto::verify_commitment(&pk, commitment) {
                verified_prior.push(j as u32);
            }
        }

        // A cardinality-changing rotation in which NO signature revealed a prior
        // commitment is unbindable — surface the diagnostic rather than a generic
        // signature failure. (A well-formed removal binds at least one; a single
        // signer at prior slot 0 binds via the index == ondex fallback.)
        if n_len != keys.len() && verified_prior.is_empty() {
            return Err(ValidationError::AsymmetricKeyRotation {
                sequence,
                prior_next_count: n_len,
                new_key_count: keys.len(),
            });
        }

        if !state.next_threshold.is_satisfied(&verified_prior, n_len) {
            return Err(ValidationError::SignatureFailed { sequence });
        }
    }

    Ok(())
}

/// Create an inception event with a properly computed SAID.
///
/// Args:
/// * `icp` - The inception event to finalize.
pub fn finalize_icp_event(mut icp: IcpEvent) -> Result<IcpEvent, ValidationError> {
    let value = serde_json::to_value(Event::Icp(icp.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    let said = compute_said(&value).map_err(|e| ValidationError::Serialization(e.to_string()))?;

    icp.d = said.clone();
    // Only set i = d for self-addressing AIDs (empty or E-prefixed)
    if icp.i.is_empty() || icp.i.as_str().starts_with('E') {
        icp.i = Prefix::new_unchecked(said.into_inner());
    }

    // Set version string with actual byte count
    let final_bytes = serde_json::to_vec(&Event::Icp(icp.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    icp.v = crate::types::VersionString::json(final_bytes.len() as u32);

    Ok(icp)
}

/// Create a delegated inception (`dip`) event with a properly computed SAID.
///
/// Mirrors [`finalize_icp_event`] for `dip`: a delegated AID's prefix is
/// self-addressing (the SAID of its own inception event), so `i` is set to `d`.
///
/// Args:
/// * `dip` - The delegated inception event to finalize (with `di` set to the delegator).
pub fn finalize_dip_event(
    mut dip: crate::events::DipEvent,
) -> Result<crate::events::DipEvent, ValidationError> {
    let value = serde_json::to_value(Event::Dip(dip.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    let said = compute_said(&value).map_err(|e| ValidationError::Serialization(e.to_string()))?;

    dip.d = said.clone();
    // A delegated AID is self-addressing: its prefix is the SAID of the dip.
    if dip.i.is_empty() || dip.i.as_str().starts_with('E') {
        dip.i = Prefix::new_unchecked(said.into_inner());
    }

    let final_bytes = serde_json::to_vec(&Event::Dip(dip.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    dip.v = crate::types::VersionString::json(final_bytes.len() as u32);

    Ok(dip)
}

/// Create a rotation event with a properly computed SAID.
///
/// Args:
/// * `rot` - The rotation event to finalize.
pub fn finalize_rot_event(mut rot: RotEvent) -> Result<RotEvent, ValidationError> {
    let value = serde_json::to_value(Event::Rot(rot.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    let said = compute_said(&value).map_err(|e| ValidationError::Serialization(e.to_string()))?;
    rot.d = said;

    let final_bytes = serde_json::to_vec(&Event::Rot(rot.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    rot.v = crate::types::VersionString::json(final_bytes.len() as u32);

    Ok(rot)
}

/// Create a delegated rotation (`drt`) event with a properly computed SAID.
///
/// Mirrors [`finalize_rot_event`]. A `drt` is **not** self-addressing — its `i`
/// is the existing delegated AID prefix — so only `d` and `v` are set (`i` is
/// left unchanged, unlike `dip`).
///
/// Args:
/// * `drt` - The delegated rotation event to finalize.
pub fn finalize_drt_event(
    mut drt: crate::events::DrtEvent,
) -> Result<crate::events::DrtEvent, ValidationError> {
    let value = serde_json::to_value(Event::Drt(drt.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    let said = compute_said(&value).map_err(|e| ValidationError::Serialization(e.to_string()))?;
    drt.d = said;

    let final_bytes = serde_json::to_vec(&Event::Drt(drt.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    drt.v = crate::types::VersionString::json(final_bytes.len() as u32);

    Ok(drt)
}

/// Create an interaction event with a properly computed SAID.
///
/// Args:
/// * `ixn` - The interaction event to finalize.
pub fn finalize_ixn_event(mut ixn: IxnEvent) -> Result<IxnEvent, ValidationError> {
    let value = serde_json::to_value(Event::Ixn(ixn.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    let said = compute_said(&value).map_err(|e| ValidationError::Serialization(e.to_string()))?;
    ixn.d = said;

    let final_bytes = serde_json::to_vec(&Event::Ixn(ixn.clone()))
        .map_err(|e| ValidationError::Serialization(e.to_string()))?;
    ixn.v = crate::types::VersionString::json(final_bytes.len() as u32);

    Ok(ixn)
}

/// Search for a seal with the given digest in any IXN event in the KEL.
///
/// Returns the sequence number of the IXN event if found.
///
/// Args:
/// * `events` - The event log to search.
/// * `digest` - The SAID digest to search for.
pub fn find_seal_in_kel(events: &[Event], digest: &str) -> Option<u128> {
    for event in events {
        if let Event::Ixn(ixn) = event {
            for seal in &ixn.a {
                if seal.digest_value().is_some_and(|d| d.as_str() == digest) {
                    return Some(ixn.s.value());
                }
            }
        }
    }
    None
}

/// Parse a KEL from a JSON string.
///
/// Args:
/// * `json` - JSON string containing a list of KERI events.
pub fn parse_kel_json(json: &str) -> Result<Vec<Event>, ValidationError> {
    serde_json::from_str(json).map_err(|e| ValidationError::Serialization(e.to_string()))
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used)]
mod tests {
    use super::*;
    use crate::events::{IndexedSignature, KeriSequence, Seal, SignedEvent};
    use crate::types::{CesrKey, Threshold, VersionString};
    use ring::rand::SystemRandom;
    use ring::signature::{Ed25519KeyPair, KeyPair};

    fn gen_keypair() -> Ed25519KeyPair {
        let rng = SystemRandom::new();
        let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
        Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap()
    }

    fn encode_pubkey(kp: &Ed25519KeyPair) -> String {
        crate::cesr_encode::encode_verkey(kp.public_key().as_ref(), cesride::matter::Codex::Ed25519)
            .unwrap()
    }

    fn make_raw_icp(key: &str, next: &str) -> IcpEvent {
        IcpEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::default(),
            s: KeriSequence::new(0),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked(key.to_string())],
            nt: Threshold::Simple(1),
            n: vec![Said::new_unchecked(next.to_string())],
            bt: Threshold::Simple(0),
            b: vec![],
            c: vec![],
            a: vec![],
        }
    }

    fn make_signed_icp() -> (IcpEvent, Ed25519KeyPair) {
        let rng = SystemRandom::new();
        let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
        let keypair = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
        let key_encoded = encode_pubkey(&keypair);

        let icp = IcpEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::default(),
            s: KeriSequence::new(0),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked(key_encoded)],
            nt: Threshold::Simple(1),
            n: vec![Said::new_unchecked("ENextCommitment".to_string())],
            bt: Threshold::Simple(0),
            b: vec![],
            c: vec![],
            a: vec![],
        };

        let finalized = finalize_icp_event(icp).unwrap();
        (finalized, keypair)
    }

    fn make_signed_ixn(
        prefix: &Prefix,
        prev_said: &Said,
        seq: u128,
        _keypair: &Ed25519KeyPair,
    ) -> IxnEvent {
        let mut ixn = IxnEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: prefix.clone(),
            s: KeriSequence::new(seq),
            p: prev_said.clone(),
            a: vec![Seal::digest("EAttest")],
        };

        let value = serde_json::to_value(Event::Ixn(ixn.clone())).unwrap();
        ixn.d = compute_said(&value).unwrap();

        ixn
    }

    #[test]
    fn finalize_icp_sets_said() {
        let icp = make_raw_icp("DKey1", "ENext1");
        let finalized = finalize_icp_event(icp).unwrap();

        assert!(!finalized.d.is_empty());
        assert_eq!(finalized.d.as_str(), finalized.i.as_str());
        assert!(finalized.d.as_str().starts_with('E'));
    }

    #[test]
    fn validates_single_inception() {
        let (icp, _keypair) = make_signed_icp();
        let events = vec![Event::Icp(icp.clone())];

        let state = validate_kel(&events).unwrap();
        assert_eq!(state.prefix, icp.i);
        assert_eq!(state.sequence, 0);
    }

    #[test]
    fn rejects_empty_kel() {
        let result = validate_kel(&[]);
        assert!(matches!(result, Err(ValidationError::EmptyKel)));
    }

    #[test]
    fn rejects_non_inception_first() {
        let mut ixn = IxnEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::new_unchecked("ETest".to_string()),
            s: KeriSequence::new(0),
            p: Said::new_unchecked("EPrev".to_string()),
            a: vec![],
        };
        // Compute a valid SAID so verify_event_said passes — the test
        // should fail on NotInception, not on SaidMismatch.
        let event = Event::Ixn(ixn.clone());
        if let Ok(said) = compute_event_said(&event) {
            ixn.d = said;
        }
        let events = vec![Event::Ixn(ixn)];
        let result = validate_kel(&events);
        assert!(matches!(result, Err(ValidationError::NotInception)));
    }

    #[test]
    fn rejects_broken_sequence() {
        let (icp, _keypair) = make_signed_icp();

        let mut ixn = IxnEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: icp.i.clone(),
            s: KeriSequence::new(5),
            p: icp.d.clone(),
            a: vec![],
        };

        let value = serde_json::to_value(Event::Ixn(ixn.clone())).unwrap();
        ixn.d = compute_said(&value).unwrap();

        let events = vec![Event::Icp(icp), Event::Ixn(ixn)];
        let result = validate_kel(&events);
        assert!(matches!(
            result,
            Err(ValidationError::InvalidSequence {
                expected: 1,
                actual: 5
            })
        ));
    }

    #[test]
    fn rejects_broken_chain() {
        let (icp, _keypair) = make_signed_icp();

        let mut ixn = IxnEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: icp.i.clone(),
            s: KeriSequence::new(1),
            p: Said::new_unchecked("EWrongPrevious".to_string()),
            a: vec![],
        };

        let value = serde_json::to_value(Event::Ixn(ixn.clone())).unwrap();
        ixn.d = compute_said(&value).unwrap();

        let events = vec![Event::Icp(icp), Event::Ixn(ixn)];
        let result = validate_kel(&events);
        assert!(matches!(result, Err(ValidationError::BrokenChain { .. })));
    }

    #[test]
    fn rejects_invalid_said() {
        let icp = make_raw_icp("DKey1", "ENext1");
        let finalized = finalize_icp_event(icp).unwrap();

        let mut tampered = finalized.clone();
        tampered.d = Said::new_unchecked("EWrongSaid".to_string());

        let events = vec![Event::Icp(tampered)];
        let result = validate_kel(&events);
        assert!(matches!(result, Err(ValidationError::InvalidSaid { .. })));
    }

    #[test]
    fn validates_icp_then_ixn() {
        let (icp, keypair) = make_signed_icp();
        let ixn = make_signed_ixn(&icp.i, &icp.d, 1, &keypair);

        let events = vec![Event::Icp(icp), Event::Ixn(ixn.clone())];
        let state = validate_kel(&events).unwrap();
        assert_eq!(state.sequence, 1);
        assert_eq!(state.last_event_said, ixn.d);
    }

    #[test]
    fn compute_event_said_works() {
        let icp = make_raw_icp("DKey1", "ENext1");
        let event = Event::Icp(icp);
        let said = compute_event_said(&event).unwrap();
        assert!(said.as_str().starts_with('E'));
        assert!(!said.is_empty());
    }

    // Sanity control: a correctly-signed SignedEvent must be accepted. Without
    // this, a regression that makes `validate_signed_event` always return
    // `SignatureFailed` would silently "pass" the rejection tests below.
    #[test]
    fn accepts_correct_signature() {
        let (icp, keypair) = make_signed_icp();
        let event = Event::Icp(icp);
        let canonical = serialize_for_signing(&event).unwrap();
        let sig = keypair.sign(&canonical).as_ref().to_vec();
        let signed = SignedEvent::new(
            event,
            vec![IndexedSignature {
                index: 0,
                prior_index: None,
                sig,
            }],
        );

        validate_signed_event(&signed, None).expect("correct signature must validate");
    }

    // Intent: a SignedEvent whose attached signature bytes do not match the
    // canonical event body must be rejected. Uses the externalized-signature
    // entry point (`validate_signed_event`); `validate_kel` only checks KEL
    // structure and does not consume attached signatures, so it cannot be
    // used to test signature-level rejection.
    #[test]
    fn rejects_forged_signature() {
        let (icp, _keypair) = make_signed_icp();
        let event = Event::Icp(icp);
        let forged_sig = vec![0u8; 64]; // valid length, invalid content
        let signed = SignedEvent::new(
            event,
            vec![IndexedSignature {
                index: 0,
                prior_index: None,
                sig: forged_sig,
            }],
        );

        assert!(matches!(
            validate_signed_event(&signed, None),
            Err(ValidationError::SignatureFailed { sequence: 0 })
        ));
    }

    // `rejects_missing_signature` was tied to the legacy in-body `x` field.
    // Signatures are externalized now; the equivalent check is covered by
    // `validate_signed_event` tests in `multi_key_threshold.rs`.

    // Intent: a SignedEvent signed by a keypair other than the one committed
    // in `icp.k` must be rejected. The wrong-key signature is structurally
    // valid (correct length, correct type) but fails Ed25519 verification
    // against the committed public key.
    #[test]
    fn rejects_wrong_key_signature() {
        let committed = gen_keypair();
        let key_encoded = encode_pubkey(&committed);

        let icp = IcpEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::default(),
            s: KeriSequence::new(0),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked(key_encoded)],
            nt: Threshold::Simple(1),
            n: vec![Said::new_unchecked("ENextCommitment".to_string())],
            bt: Threshold::Simple(0),
            b: vec![],
            c: vec![],
            a: vec![],
        };
        let icp = finalize_icp_event(icp).unwrap();
        let event = Event::Icp(icp);

        let wrong = gen_keypair();
        let canonical = serialize_for_signing(&event).unwrap();
        let wrong_sig = wrong.sign(&canonical).as_ref().to_vec();
        let signed = SignedEvent::new(
            event,
            vec![IndexedSignature {
                index: 0,
                prior_index: None,
                sig: wrong_sig,
            }],
        );

        assert!(matches!(
            validate_signed_event(&signed, None),
            Err(ValidationError::SignatureFailed { sequence: 0 })
        ));
    }

    #[test]
    fn crypto_accepts_valid_inception() {
        let (icp, _keypair) = make_signed_icp();
        let result = verify_event_crypto(&Event::Icp(icp), None);
        assert!(result.is_ok());
    }

    #[test]
    fn find_seal_in_kel_finds_digest() {
        let (icp, keypair) = make_signed_icp();
        let ixn = make_signed_ixn(&icp.i, &icp.d, 1, &keypair);
        let events = vec![Event::Icp(icp), Event::Ixn(ixn)];
        assert_eq!(find_seal_in_kel(&events, "EAttest"), Some(1));
        assert_eq!(find_seal_in_kel(&events, "ENonExistent"), None);
    }

    #[test]
    fn parse_kel_json_rejects_invalid_hex_sequence() {
        let json = r#"[{"v":"KERI10JSON","t":"icp","i":"E123","s":"not_hex","kt":"1","k":["DKey"],"nt":"1","n":["ENext"],"bt":"0","b":[]}]"#;
        let result = parse_kel_json(json);
        assert!(result.is_err(), "expected error for invalid hex sequence");
    }

    /// Build a signed ICP with caller-supplied overrides applied after keypair
    /// generation but before finalization and signing.
    fn make_custom_signed_icp(customize: impl FnOnce(&mut IcpEvent)) -> (IcpEvent, Ed25519KeyPair) {
        let rng = SystemRandom::new();
        let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
        let keypair = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
        let key_encoded = encode_pubkey(&keypair);

        let mut icp = IcpEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::default(),
            s: KeriSequence::new(0),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked(key_encoded)],
            nt: Threshold::Simple(1),
            n: vec![Said::new_unchecked("ENextCommitment".to_string())],
            bt: Threshold::Simple(0),
            b: vec![],
            c: vec![],
            a: vec![],
        };

        customize(&mut icp);

        let finalized = finalize_icp_event(icp).unwrap();
        (finalized, keypair)
    }

    #[test]
    fn rejects_events_after_abandonment() {
        // Abandonment = rotation with empty n (not inception — that's NonTransferable).
        let kp2 = gen_keypair();

        // Use make_custom_signed_icp with pre-committed key for kp2
        let commitment2 = crate::crypto::compute_next_commitment(
            &crate::keys::KeriPublicKey::ed25519(kp2.public_key().as_ref()).unwrap(),
        );
        let (icp, _kp1) = make_custom_signed_icp(|icp| {
            icp.n = vec![commitment2.clone()];
        });
        let prefix = icp.i.clone();

        // Rotation that abandons (empty n)
        let mut rot = RotEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: prefix.clone(),
            s: KeriSequence::new(1),
            p: icp.d.clone(),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked(encode_pubkey(&kp2))],
            nt: Threshold::Simple(0),
            n: vec![],
            bt: Threshold::Simple(0),
            br: vec![],
            ba: vec![],
            c: vec![],
            a: vec![],
        };
        let val = serde_json::to_value(Event::Rot(rot.clone())).unwrap();
        rot.d = compute_said(&val).unwrap();

        let ixn = make_signed_ixn(&prefix, &rot.d, 2, &kp2);
        let events = vec![Event::Icp(icp), Event::Rot(rot), Event::Ixn(ixn)];
        let result = validate_kel(&events);
        assert!(
            matches!(result, Err(ValidationError::AbandonedIdentity { .. })),
            "expected AbandonedIdentity, got: {result:?}"
        );
    }

    #[test]
    fn rejects_ixn_in_establishment_only_kel() {
        let (icp, keypair) = make_custom_signed_icp(|icp| {
            icp.c = vec![ConfigTrait::EstablishmentOnly];
        });
        let ixn = make_signed_ixn(&icp.i, &icp.d, 1, &keypair);
        let events = vec![Event::Icp(icp), Event::Ixn(ixn)];
        let result = validate_kel(&events);
        assert!(
            matches!(result, Err(ValidationError::EstablishmentOnly { .. })),
            "expected EstablishmentOnly, got: {result:?}"
        );
    }

    #[test]
    fn rejects_events_after_non_transferable_inception() {
        let (icp, keypair) = make_custom_signed_icp(|icp| {
            icp.n = vec![];
            icp.nt = Threshold::Simple(0);
        });
        let ixn = make_signed_ixn(&icp.i, &icp.d, 1, &keypair);
        let events = vec![Event::Icp(icp), Event::Ixn(ixn)];
        let result = validate_kel(&events);
        assert!(
            matches!(
                result,
                Err(ValidationError::NonTransferable)
                    | Err(ValidationError::AbandonedIdentity { .. })
            ),
            "expected NonTransferable or AbandonedIdentity, got: {result:?}"
        );
    }

    #[test]
    fn rejects_duplicate_backers() {
        let (_, result) = {
            let rng = SystemRandom::new();
            let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
            let keypair = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
            let key_encoded = encode_pubkey(&keypair);

            let dup_backer = Prefix::new_unchecked("DWit1".to_string());
            let icp = IcpEvent {
                v: VersionString::placeholder(),
                d: Said::default(),
                i: Prefix::default(),
                s: KeriSequence::new(0),
                kt: Threshold::Simple(1),
                k: vec![CesrKey::new_unchecked(key_encoded)],
                nt: Threshold::Simple(1),
                n: vec![Said::new_unchecked("ENextCommitment".to_string())],
                bt: Threshold::Simple(2),
                b: vec![dup_backer.clone(), dup_backer],
                c: vec![],
                a: vec![],
            };

            let finalized = finalize_icp_event(icp).unwrap();
            let events = vec![Event::Icp(finalized)];
            (keypair, validate_kel(&events))
        };
        assert!(
            matches!(result, Err(ValidationError::DuplicateBacker { .. })),
            "expected DuplicateBacker, got: {result:?}"
        );
    }

    #[test]
    fn rejects_invalid_backer_threshold() {
        let (_, result) = {
            let rng = SystemRandom::new();
            let pkcs8 = Ed25519KeyPair::generate_pkcs8(&rng).unwrap();
            let keypair = Ed25519KeyPair::from_pkcs8(pkcs8.as_ref()).unwrap();
            let key_encoded = encode_pubkey(&keypair);

            let icp = IcpEvent {
                v: VersionString::placeholder(),
                d: Said::default(),
                i: Prefix::default(),
                s: KeriSequence::new(0),
                kt: Threshold::Simple(1),
                k: vec![CesrKey::new_unchecked(key_encoded)],
                nt: Threshold::Simple(1),
                n: vec![Said::new_unchecked("ENextCommitment".to_string())],
                bt: Threshold::Simple(2),
                b: vec![],
                c: vec![],
                a: vec![],
            };

            let finalized = finalize_icp_event(icp).unwrap();
            let events = vec![Event::Icp(finalized)];
            (keypair, validate_kel(&events))
        };
        // `bt=2` over zero backers is now caught by the stricter structural
        // threshold-satisfiability guard (A.4) before the legacy
        // empty-backers/bt!=0 check.
        assert!(
            matches!(result, Err(ValidationError::ThresholdNotSatisfiable { .. })),
            "expected ThresholdNotSatisfiable, got: {result:?}"
        );
    }

    #[test]
    fn sign_over_finalized_bytes_roundtrips() {
        // A.2: the bytes handed to the signer must equal the wire bytes, whose
        // length the version string `v` declares. (Previously d/i were cleared
        // after finalize, making the signed body shorter than `v` claimed.)
        let (icp, _kp) = make_signed_icp();
        let bytes = serialize_for_signing(&Event::Icp(icp.clone())).unwrap();
        assert_eq!(
            bytes.len() as u32,
            icp.v.size,
            "signed byte length must equal the version-string size field"
        );
        let reparsed: Event = serde_json::from_slice(&bytes).unwrap();
        assert!(reparsed.is_inception());
    }

    #[test]
    fn threshold_rejects_kt_gt_k() {
        // A.4: a signing threshold larger than the key-list length is
        // structurally unsatisfiable and must be rejected at validation.
        let kp = gen_keypair();
        let key = encode_pubkey(&kp);
        let icp = IcpEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::default(),
            s: KeriSequence::new(0),
            kt: Threshold::Simple(5),
            k: vec![CesrKey::new_unchecked(key)],
            nt: Threshold::Simple(1),
            n: vec![Said::new_unchecked("ENextCommitment".to_string())],
            bt: Threshold::Simple(0),
            b: vec![],
            c: vec![],
            a: vec![],
        };
        let finalized = finalize_icp_event(icp).unwrap();
        let result = validate_kel(&[Event::Icp(finalized)]);
        assert!(
            matches!(result, Err(ValidationError::ThresholdNotSatisfiable { .. })),
            "expected ThresholdNotSatisfiable, got: {result:?}"
        );
    }

    #[test]
    fn rotation_rejects_br_not_in_prior() {
        // A.10 (F-05): a rotation that cuts a backer not in the prior set, or
        // adds a backer that already survives, must be rejected before
        // apply_rotation corrupts the backer set.
        let state = KeyState::from_inception(
            Prefix::new_unchecked("EPrefix".to_string()),
            vec![CesrKey::new_unchecked("DKey1".to_string())],
            vec![], // empty next_commitment -> commitment check skipped
            Threshold::Simple(1),
            Threshold::Simple(0),
            Said::new_unchecked("ESAID".to_string()),
            vec![Prefix::new_unchecked("BWit1".to_string())],
            Threshold::Simple(0),
            vec![],
        );

        let make_rot = |br: Vec<Prefix>, ba: Vec<Prefix>| RotEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::new_unchecked("EPrefix".to_string()),
            s: KeriSequence::new(1),
            p: Said::new_unchecked("ESAID".to_string()),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked("DKey2".to_string())],
            nt: Threshold::Simple(0),
            n: vec![],
            bt: Threshold::Simple(0),
            br,
            ba,
            c: vec![],
            a: vec![],
        };

        // br entry not in prior backers -> rejected.
        let bad_cut = make_rot(vec![Prefix::new_unchecked("BWitX".to_string())], vec![]);
        assert!(matches!(
            validate_rotation(&bad_cut, 1, &mut state.clone()),
            Err(ValidationError::InvalidBackerDelta { .. })
        ));

        // ba entry duplicating a surviving backer -> rejected.
        let bad_add = make_rot(vec![], vec![Prefix::new_unchecked("BWit1".to_string())]);
        assert!(matches!(
            validate_rotation(&bad_add, 1, &mut state.clone()),
            Err(ValidationError::InvalidBackerDelta { .. })
        ));

        // valid delta (cut the existing backer) -> ok.
        let ok = make_rot(vec![Prefix::new_unchecked("BWit1".to_string())], vec![]);
        assert!(validate_rotation(&ok, 1, &mut state.clone()).is_ok());
    }

    #[test]
    fn rotation_rejects_silent_backer_role_flip() {
        // A.13 (F-23): flipping RB<->NRB while a prior backer survives is
        // rejected; the same flip is allowed once every prior backer is cut
        // (b[] rebuilt). An empty c[] inherits the role and never flips.
        let nrb_state = || {
            KeyState::from_inception(
                Prefix::new_unchecked("EPrefix".to_string()),
                vec![CesrKey::new_unchecked("DKey1".to_string())],
                vec![],
                Threshold::Simple(1),
                Threshold::Simple(0),
                Said::new_unchecked("ESAID".to_string()),
                vec![Prefix::new_unchecked("BWit1".to_string())],
                Threshold::Simple(0),
                vec![ConfigTrait::NoRegistrarBackers],
            )
        };

        let make_rot = |br: Vec<Prefix>, ba: Vec<Prefix>, c: Vec<ConfigTrait>| RotEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: Prefix::new_unchecked("EPrefix".to_string()),
            s: KeriSequence::new(1),
            p: Said::new_unchecked("ESAID".to_string()),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked("DKey2".to_string())],
            nt: Threshold::Simple(0),
            n: vec![],
            bt: Threshold::Simple(0),
            br,
            ba,
            c,
            a: vec![],
        };

        // Flip NRB->RB while BWit1 survives -> rejected.
        let flip_keep = make_rot(vec![], vec![], vec![ConfigTrait::RegistrarBackers]);
        assert!(matches!(
            validate_rotation(&flip_keep, 1, &mut nrb_state()),
            Err(ValidationError::BackerRoleFlip { .. })
        ));

        // Flip NRB->RB after cutting every prior backer -> ok (b[] rebuilt).
        let flip_rebuild = make_rot(
            vec![Prefix::new_unchecked("BWit1".to_string())],
            vec![],
            vec![ConfigTrait::RegistrarBackers],
        );
        assert!(validate_rotation(&flip_rebuild, 1, &mut nrb_state()).is_ok());

        // Same role kept (NRB->NRB) with the backer surviving -> ok (no flip).
        let same_role = make_rot(vec![], vec![], vec![ConfigTrait::NoRegistrarBackers]);
        assert!(validate_rotation(&same_role, 1, &mut nrb_state()).is_ok());

        // Empty c[] inherits the role -> ok even though the backer survives.
        let inherit = make_rot(vec![], vec![], vec![]);
        assert!(validate_rotation(&inherit, 1, &mut nrb_state()).is_ok());
    }

    // ── D.6: receipt-gated replay ────────────────────────────────────────────

    use crate::witness::WitnessReceipt;

    /// Said-keyed witness-receipt source for replay-gate tests.
    struct MapReceipts {
        by_said: std::collections::HashMap<String, Vec<WitnessReceipt>>,
    }

    impl WitnessReceiptLookup for MapReceipts {
        fn receipts_for(
            &self,
            _controller: &Prefix,
            _sn: KeriSequence,
            said: &Said,
        ) -> Vec<WitnessReceipt> {
            self.by_said.get(said.as_str()).cloned().unwrap_or_default()
        }
    }

    fn witness_aid(aid: &str) -> Prefix {
        Prefix::new_unchecked(aid.to_string())
    }

    fn receipt_from(aid: &str) -> WitnessReceipt {
        WitnessReceipt {
            witness: witness_aid(aid),
            signature: vec![],
        }
    }

    fn receipts_under(said: &Said, aids: &[&str]) -> MapReceipts {
        let mut by_said = std::collections::HashMap::new();
        by_said.insert(
            said.as_str().to_string(),
            aids.iter().map(|a| receipt_from(a)).collect(),
        );
        MapReceipts { by_said }
    }

    /// A finalized inception designating `aids` as backers with threshold `bt`.
    fn icp_with_backers(aids: &[&str], bt: u64) -> IcpEvent {
        let backers: Vec<Prefix> = aids.iter().map(|a| witness_aid(a)).collect();
        let (icp, _kp) = make_custom_signed_icp(|icp| {
            icp.b = backers.clone();
            icp.bt = Threshold::Simple(bt);
        });
        icp
    }

    #[test]
    fn replay_bt_zero_accepts_without_receipts() {
        let (icp, _kp) = make_signed_icp(); // bt=0, b=[]
        let events = vec![Event::Icp(icp)];
        let lookup = MapReceipts {
            by_said: std::collections::HashMap::new(),
        };
        let outcome = validate_kel_with_receipts(&events, None, &lookup).unwrap();
        assert!(matches!(outcome, WitnessedReplay::Accepted(_)));
    }

    #[test]
    fn replay_at_quorum_accepts() {
        let icp = icp_with_backers(&["BWit1", "BWit2"], 2);
        let said = icp.d.clone();
        let lookup = receipts_under(&said, &["BWit1", "BWit2"]);
        let events = vec![Event::Icp(icp)];
        let outcome = validate_kel_with_receipts(&events, None, &lookup).unwrap();
        assert!(matches!(outcome, WitnessedReplay::Accepted(_)));
    }

    #[test]
    fn replay_under_quorum_is_pending() {
        let icp = icp_with_backers(&["BWit1", "BWit2"], 2);
        let said = icp.d.clone();
        let lookup = receipts_under(&said, &["BWit1"]); // only 1 of 2 required
        let events = vec![Event::Icp(icp)];
        match validate_kel_with_receipts(&events, None, &lookup).unwrap() {
            WitnessedReplay::Pending {
                sequence,
                collected,
                ..
            } => {
                assert_eq!(sequence, 0);
                assert_eq!(collected, 1);
            }
            WitnessedReplay::Accepted(_) => panic!("expected Pending under quorum"),
        }
    }

    #[test]
    fn replay_ignores_duplicate_witness_receipts() {
        let icp = icp_with_backers(&["BWit1", "BWit2", "BWit3"], 2);
        let said = icp.d.clone();
        let lookup = receipts_under(&said, &["BWit1", "BWit1"]); // same witness twice
        let events = vec![Event::Icp(icp)];
        assert!(matches!(
            validate_kel_with_receipts(&events, None, &lookup).unwrap(),
            WitnessedReplay::Pending { .. }
        ));
    }

    #[test]
    fn replay_ignores_receipt_for_wrong_said() {
        let icp = icp_with_backers(&["BWit1", "BWit2"], 2);
        // Receipts stored under a different event SAID must never satisfy this event.
        let wrong = Said::new_unchecked("EWrongEventSaid".to_string());
        let lookup = receipts_under(&wrong, &["BWit1", "BWit2"]);
        let events = vec![Event::Icp(icp)];
        match validate_kel_with_receipts(&events, None, &lookup).unwrap() {
            WitnessedReplay::Pending { collected, .. } => assert_eq!(collected, 0),
            WitnessedReplay::Accepted(_) => panic!("wrong-SAID receipts must not count"),
        }
    }

    #[test]
    fn replay_uses_witness_set_in_force_at_seq() {
        // icp designates {BWit1} bt=1; rot at seq 1 cuts BWit1, adds BWit2, bt=1.
        // The seq-1 gate must use the post-rotation set {BWit2}.
        let kp2 = gen_keypair();
        let kp3 = gen_keypair();
        let commitment2 = crate::crypto::compute_next_commitment(
            &crate::keys::KeriPublicKey::ed25519(kp2.public_key().as_ref()).unwrap(),
        );
        let commitment3 = crate::crypto::compute_next_commitment(
            &crate::keys::KeriPublicKey::ed25519(kp3.public_key().as_ref()).unwrap(),
        );
        let (icp, _kp1) = make_custom_signed_icp(|icp| {
            icp.b = vec![witness_aid("BWit1")];
            icp.bt = Threshold::Simple(1);
            icp.n = vec![commitment2.clone()];
        });
        let prefix = icp.i.clone();
        let icp_said = icp.d.clone();

        let mut rot = RotEvent {
            v: VersionString::placeholder(),
            d: Said::default(),
            i: prefix.clone(),
            s: KeriSequence::new(1),
            p: icp_said.clone(),
            kt: Threshold::Simple(1),
            k: vec![CesrKey::new_unchecked(encode_pubkey(&kp2))],
            nt: Threshold::Simple(1),
            n: vec![commitment3.clone()],
            bt: Threshold::Simple(1),
            br: vec![witness_aid("BWit1")],
            ba: vec![witness_aid("BWit2")],
            c: vec![],
            a: vec![],
        };
        let val = serde_json::to_value(Event::Rot(rot.clone())).unwrap();
        rot.d = compute_said(&val).unwrap();
        let rot_said = rot.d.clone();

        let mut by_said = std::collections::HashMap::new();
        by_said.insert(icp_said.as_str().to_string(), vec![receipt_from("BWit1")]);
        by_said.insert(rot_said.as_str().to_string(), vec![receipt_from("BWit2")]);
        let lookup = MapReceipts { by_said };

        let events = vec![Event::Icp(icp), Event::Rot(rot)];
        // BWit2 is only in the post-rotation set; acceptance proves the in-force
        // set (not the stale {BWit1}) gated the rotation.
        assert!(matches!(
            validate_kel_with_receipts(&events, None, &lookup).unwrap(),
            WitnessedReplay::Accepted(_)
        ));
    }

    #[test]
    fn validate_kel_advances_without_receipt_gate() {
        // Back-compat: plain validate_kel ignores receipts and advances a bt>0 KEL.
        let icp = icp_with_backers(&["BWit1", "BWit2"], 2);
        let events = vec![Event::Icp(icp)];
        assert!(validate_kel(&events).is_ok());
    }
}

// =============================================================================
// Time-aware policy validation — rotation cooldown, clock-skew, emergency
// override. `validate_kel` stays pure / clock-free (structural invariants
// only); callers who want time-aware checks reach for
// `validate_kel_with_policy`.
// =============================================================================

/// Configurable policy for time-aware KEL validation. Defaults match
/// the plan text: 24h minimum rotation interval, 60s clock-skew
/// tolerance, no emergency-override identifier.
#[derive(Debug, Clone)]
pub struct KelPolicy {
    /// Minimum wall-clock interval between two consecutive rotation
    /// events. Default: 24 hours.
    pub min_rotation_interval: chrono::Duration,
    /// Maximum allowed skew between an event's `dt` and the wall
    /// clock used for validation. Default: 60 seconds.
    pub clock_skew_tolerance: chrono::Duration,
    /// AID that is permitted to skip the rotation-cooldown check
    /// (e.g. the controller's emergency-rotation key). `None` means
    /// no override is configured and every rotation must respect
    /// the cooldown.
    pub emergency_override_did: Option<crate::types::Prefix>,
}

impl Default for KelPolicy {
    fn default() -> Self {
        Self {
            min_rotation_interval: chrono::Duration::hours(24),
            clock_skew_tolerance: chrono::Duration::seconds(60),
            emergency_override_did: None,
        }
    }
}

/// Validate a KEL against a time-aware [`KelPolicy`].
///
/// Runs the structural [`validate_kel`] first; on success, layers on
/// three additional checks that depend on the `dt` field added to
/// establishment and interaction events:
///
/// 1. Every event MUST carry a `dt`. Pre-`dt`-migration events
///    (where `dt` is `None`) fail with
///    [`ValidationError::MissingTimestamp`].
/// 2. `dt` MUST be monotonically non-decreasing across consecutive
///    events. Backward-moving timestamps are evidence of tampering.
/// 3. Consecutive rotation events MUST be at least
///    [`KelPolicy::min_rotation_interval`] apart (unless the event's
///    controller matches [`KelPolicy::emergency_override_did`]).
/// 4. Every `dt` must be within
///    [`KelPolicy::clock_skew_tolerance`] of `now`.
///
/// Args:
/// * `events`: The ordered KEL.
/// * `policy`: [`KelPolicy`] governing the time checks.
/// * `now`: The daemon's wall clock at validation time. Inject via
///   [`chrono::Utc::now`] at the presentation boundary; domain layers
///   pass a clock.
pub fn validate_kel_with_policy(
    events: &[Event],
    timestamps: &[Option<chrono::DateTime<chrono::Utc>>],
    policy: &KelPolicy,
    now: chrono::DateTime<chrono::Utc>,
) -> Result<KeyState, ValidationError> {
    let state = validate_kel(events)?;

    let mut last_rotation_dt: Option<chrono::DateTime<chrono::Utc>> = None;
    let mut last_any_dt: Option<chrono::DateTime<chrono::Utc>> = None;

    for (idx, evt) in events.iter().enumerate() {
        let seq = idx as u128;
        let (is_rotation, controller) = match evt {
            Event::Icp(e) => (false, &e.i),
            Event::Rot(e) => (true, &e.i),
            Event::Ixn(e) => (false, &e.i),
            Event::Dip(e) => (false, &e.i),
            Event::Drt(e) => (true, &e.i),
        };
        let Some(dt) = timestamps.get(idx).copied().flatten() else {
            return Err(ValidationError::MissingTimestamp { sequence: seq });
        };
        // Monotonicity.
        if let Some(prev) = last_any_dt
            && dt < prev
        {
            return Err(ValidationError::NonMonotonicTimestamp {
                sequence: seq,
                prev: prev.to_rfc3339(),
                curr: dt.to_rfc3339(),
            });
        }
        // Clock skew.
        let skew = (dt - now).num_seconds();
        if skew.abs() > policy.clock_skew_tolerance.num_seconds() {
            return Err(ValidationError::ClockSkew {
                sequence: seq,
                skew_secs: skew,
                tolerance_secs: policy.clock_skew_tolerance.num_seconds(),
            });
        }
        // Cooldown on rotations.
        if is_rotation && let Some(prev) = last_rotation_dt {
            let interval = dt - prev;
            let is_override = policy
                .emergency_override_did
                .as_ref()
                .is_some_and(|ov| ov == controller);
            if !is_override && interval < policy.min_rotation_interval {
                return Err(ValidationError::RotationCooldown {
                    sequence: seq,
                    interval_secs: interval.num_seconds(),
                    min_secs: policy.min_rotation_interval.num_seconds(),
                });
            }
        }
        last_any_dt = Some(dt);
        if is_rotation {
            last_rotation_dt = Some(dt);
        }
    }

    Ok(state)
}

#[cfg(test)]
mod policy_tests {
    use super::*;
    use chrono::{Duration as ChronoDuration, TimeZone, Utc};

    fn base_now() -> chrono::DateTime<chrono::Utc> {
        Utc.with_ymd_and_hms(2026, 1, 1, 0, 0, 0).unwrap()
    }

    #[test]
    fn policy_rejects_missing_dt_via_empty_kel_path() {
        // Structural validation fires first; empty KEL is rejected
        // before any policy check runs. Locks in that the policy
        // validator doesn't accidentally accept an empty KEL.
        let events: Vec<crate::events::Event> = vec![];
        let r = validate_kel_with_policy(&events, &[], &KelPolicy::default(), base_now());
        assert!(matches!(r, Err(ValidationError::EmptyKel)));
    }

    #[test]
    fn policy_default_values_match_plan() {
        let p = KelPolicy::default();
        assert_eq!(p.min_rotation_interval, ChronoDuration::hours(24));
        assert_eq!(p.clock_skew_tolerance, ChronoDuration::seconds(60));
        assert!(p.emergency_override_did.is_none());
    }
}