commonware-consensus 2026.7.0

Order opaque messages in a Byzantine environment.
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
//! Twins framework scenario generator and executor helpers.
//!
//! This module follows the testing architecture from
//! [Twins: BFT Systems Made Robust](https://arxiv.org/pdf/2004.10617):
//! 1. Generate primary/secondary recipient-set scenarios.
//! 2. Combine those sets with leader choices per round.
//! 3. Arrange those round choices into full multi-round scenarios.
//! 4. Execute each scenario across compromised-node assignments.
//!
//! The original paper models disjoint network partitions over nodes and their
//! twins. This harness deliberately uses a stronger Simplex-specific
//! recipient-set model: the primary and secondary halves are represented as
//! independent recipient masks, so a participant may be visible to both halves,
//! one half, or neither half in a scripted round. After the scripted prefix,
//! traffic returns to all-to-all synchrony and leader election resumes through
//! the caller's fallback elector.
//!
//! Campaigns are intended to cover small representative Twins schedules rather
//! than production-sized validator sets. Participant sets are capped at 64 so
//! recipient sets and residual cell boundaries can be stored as plain `u64`
//! masks.
//!
//! Scenarios are generated in a canonical unlabeled form instead of by
//! enumerating every participant relabeling. The generator tracks participant
//! symmetry classes as `cells`, where each cell is a group of participants that
//! have been indistinguishable across all attack rounds generated so far.
//! Advancing one round refines those cells by splitting each group according to
//! the role its members take in the new round.
//!
//! ## Algorithm
//!
//! In each round, every non-leader participant belongs to one of four ordered
//! role buckets: outside both halves, visible to both halves, visible only to
//! the secondary half, or visible only to the primary half. The generated
//! leader is a singleton at the end of its cell and must be visible to at least
//! one half. Total leader isolation is outside this scenario space.
//!
//! A residual cell is a group of participants with identical role history in
//! the scripted prefix. When a participant leads, it becomes a permanent
//! singleton because future rounds can distinguish it from the other members of
//! its old cell. A transition records only the next residual cell boundaries.
//! For a fixed set of part sizes, multiple role-bucket choices can produce the
//! same next boundaries, so the transition stores their count as
//! `multiplicity`. Scenario counts are computed over this compressed transition
//! DAG, and `round_from_edge` reconstructs sampled rounds by decoding the
//! chosen edge's rank as mixed-radix digits in cell order.
//!
//! For example, with one cell of three participants and leader `2`, a round can
//! split the two non-leaders as `[both = 1, primary-only = 1]` and place the
//! leader in the secondary half. The canonical representative sets primary to
//! `{0, 1}` and secondary to `{0, 2}`, producing residual cells `[1, 1, 1]`.
//! The same part sizes with `both` and `secondary-only` roles instead produce
//! the same residual cells but different masks, so they share the transition
//! target and contribute to its multiplicity.
//!
//! Scenario generation guarantees that every case within a campaign is
//! structurally distinct -- no duplicate (scenario, compromised-assignment)
//! pairs are ever emitted. The scenario space is counted with an exact
//! compressed transition DAG: each edge stores a residual symmetry-cell
//! transition and the exact number of concrete round scenarios represented by
//! that transition. Counts are computed bottom-up over the reachable residual
//! states. When the scenario space exceeds the configured budget, sampled
//! campaigns choose canonical scenarios uniformly without replacement. For each
//! selected scenario, `cases()` computes the residual symmetry cells and
//! generates only the unique compromised-node assignments: two assignments that
//! differ only in which members of a cell are chosen are equivalent and
//! collapsed to a single representative.
//!
//! These recipient sets are not required to be disjoint. A participant may
//! appear in both masks for a round, meaning both twin halves can exchange
//! messages with that participant identity in that view.

use crate::{
    simplex::elector::{Config as ElectorConfig, Elector as Elected},
    types::{Participant, Round, View},
};
use commonware_cryptography::certificate::Scheme;
use commonware_p2p::simulated::SplitTarget;
use commonware_utils::ordered::Set;
use rand::{seq::SliceRandom, Rng, RngExt as _};
use std::{
    collections::{HashMap, HashSet},
    sync::Arc,
};

/// Maximum participant count supported by the scenario generator.
const MAX_PARTICIPANTS: usize = u64::BITS as usize;

/// Sizes of the residual symmetry cells in canonical participant order.
///
/// For example, `[2, 1]` represents cells `{0, 1}` and `{2}`.
type Cells = Vec<usize>;

/// Sizes of contiguous role parts within one residual cell.
type Parts = Vec<usize>;

/// Participant index in canonical scenario order.
type ParticipantIndex = usize;

/// Canonical compromised participant indices for one generated case.
type Compromised = Vec<ParticipantIndex>;

/// Per-round adversarial setting from the Twins framework.
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct RoundScenario {
    // Participant index chosen to lead this round.
    leader: ParticipantIndex,
    // Bitmasks selecting which participant identities each twin half can
    // exchange messages with. The masks may overlap.
    primary_mask: u64,
    secondary_mask: u64,
}

impl RoundScenario {
    /// Returns the designated leader index for this round scenario.
    pub const fn leader(&self) -> usize {
        self.leader
    }

    /// Returns recipient sets for this round's twin halves.
    fn partitions<P: Clone>(&self, participants: &[P]) -> (Vec<P>, Vec<P>) {
        let primary = partition_for_mask(self.primary_mask, participants);
        let secondary = partition_for_mask(self.secondary_mask, participants);
        (primary, secondary)
    }

    /// Routes a sender according to this round's recipient masks.
    fn route<P: PartialEq>(&self, sender: &P, participants: &[P]) -> SplitTarget {
        let sender_idx = participants
            .iter()
            .position(|participant| participant == sender)
            .expect("sender missing from runtime participant list");
        let in_primary = mask_contains(self.primary_mask, sender_idx);
        let in_secondary = mask_contains(self.secondary_mask, sender_idx);
        match (in_primary, in_secondary) {
            (true, true) => SplitTarget::Both,
            (true, false) => SplitTarget::Primary,
            (false, true) => SplitTarget::Secondary,
            (false, false) => SplitTarget::None,
        }
    }
}

/// Multi-round scenario from the Twins framework.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct Scenario {
    rounds: Vec<RoundScenario>,
}

impl Scenario {
    /// Returns the round scenarios in this scenario.
    pub fn rounds(&self) -> &[RoundScenario] {
        &self.rounds
    }

    /// Returns recipient sets for each twin half at a given view.
    ///
    /// These sets are not required to be disjoint: if the same participant
    /// appears in both, both twin halves may exchange messages with that
    /// participant in the given view.
    ///
    /// Views after the configured adversarial rounds use full synchrony
    /// (`all -> all`) to model eventual synchrony for liveness checks.
    pub fn partitions<P: Clone>(&self, view: View, participants: &[P]) -> (Vec<P>, Vec<P>) {
        let idx = view.get().saturating_sub(1) as usize;
        if let Some(round) = self.rounds.get(idx) {
            return round.partitions(participants);
        }
        (participants.to_vec(), participants.to_vec())
    }

    /// Routes a message from sender to the correct twin half at a given view.
    ///
    /// For scripted rounds, inbound twin traffic is routed by checking the
    /// sender against the primary and secondary bitmasks. When a sender appears
    /// in both masks, this returns [`SplitTarget::Both`].
    ///
    /// # Panics
    ///
    /// Panics if `sender` is not present in `participants` for a scripted
    /// round.
    pub fn route<P: PartialEq>(&self, view: View, sender: &P, participants: &[P]) -> SplitTarget {
        let idx = view.get().saturating_sub(1) as usize;
        if let Some(round) = self.rounds.get(idx) {
            return round.route(sender, participants);
        }
        // After attack rounds, both halves see everyone.
        SplitTarget::Both
    }
}

/// Routes a sender according to explicit primary and secondary recipient sets.
fn route_with_groups<P: PartialEq>(sender: &P, primary: &[P], secondary: &[P]) -> SplitTarget {
    let in_primary = primary.contains(sender);
    let in_secondary = secondary.contains(sender);
    match (in_primary, in_secondary) {
        (true, true) => SplitTarget::Both,
        (true, false) => SplitTarget::Primary,
        (false, true) => SplitTarget::Secondary,
        (false, false) => panic!("sender not in any partition"),
    }
}

/// Returns the strict disjoint split at index `view % n`.
///
/// # Panics
///
/// Panics if `participants` is empty.
pub fn view_partitions<P: Clone>(view: View, participants: &[P]) -> (Vec<P>, Vec<P>) {
    let split = (view.get() as usize) % participants.len();
    let (primary, secondary) = participants.split_at(split);
    (primary.to_vec(), secondary.to_vec())
}

/// Routes a sender according to [`view_partitions`].
///
/// # Panics
///
/// Panics if `participants` is empty or `sender` is not present in
/// `participants`.
pub fn view_route<P: Clone + PartialEq>(view: View, sender: &P, participants: &[P]) -> SplitTarget {
    let (primary, secondary) = view_partitions(view, participants);
    route_with_groups(sender, &primary, &secondary)
}

/// Twins leader-election config that follows scripted scenario leaders before
/// delegating to a fallback elector.
#[derive(Clone, Debug)]
pub struct Elector<C> {
    fallback: C,
    round_leaders: Arc<[Participant]>,
}

impl<C: Default> Default for Elector<C> {
    fn default() -> Self {
        Self {
            fallback: C::default(),
            round_leaders: Arc::from(Vec::new()),
        }
    }
}

impl<C> Elector<C> {
    /// Create a twins elector from a scenario and fallback elector.
    ///
    /// # Panics
    ///
    /// Panics if any scenario leader is outside `0..participants`.
    pub fn new(fallback: C, scenario: &Scenario, participants: usize) -> Self {
        let round_leaders: Vec<_> = scenario
            .rounds()
            .iter()
            .map(|round| {
                assert!(
                    round.leader() < participants,
                    "scenario leader out of bounds"
                );
                Participant::from_usize(round.leader())
            })
            .collect();
        Self {
            fallback,
            round_leaders: Arc::from(round_leaders),
        }
    }
}

/// Initialized twins leader elector built from [`Elector`].
#[derive(Clone, Debug)]
pub struct ElectorState<E> {
    fallback: E,
    round_leaders: Arc<[Participant]>,
}

impl<S, C> ElectorConfig<S> for Elector<C>
where
    S: Scheme,
    C: ElectorConfig<S>,
{
    type Elector = ElectorState<C::Elector>;

    fn build(self, participants: &Set<S::PublicKey>) -> Self::Elector {
        ElectorState {
            fallback: self.fallback.build(participants),
            round_leaders: self.round_leaders,
        }
    }
}

impl<S, E> Elected<S> for ElectorState<E>
where
    S: Scheme,
    E: Elected<S>,
{
    fn elect(&self, round: Round, certificate: Option<&S::Certificate>) -> Participant {
        let idx = round.view().get().saturating_sub(1) as usize;
        if let Some(&leader) = self.round_leaders.get(idx) {
            return leader;
        }

        // After the scripted attack prefix, intentionally resume the caller's
        // fallback elector rather than forcing an honest-only suffix. Twins
        // campaigns should not prevent the protocol from timing out in
        // later views (if a twin is elected).
        self.fallback.elect(round, certificate)
    }
}

/// Controls how multi-round scenarios are constructed.
#[derive(Clone, Copy, Debug)]
pub enum Mode {
    /// Each round gets independently chosen primary and secondary recipient
    /// sets and a leader.
    Sampled,
    /// A single 1-round recipient-set pattern is repeated across all rounds,
    /// modeling a persistent adversarial split.
    Sustained,
}

/// Framework configuration for generating Twins cases.
///
/// The generator uses `u64` masks for recipient sets and residual cell
/// boundaries, so campaigns support at most 64 participants.
///
/// Each canonical scenario tracks residual symmetry cells -- participants that
/// were treated identically across all rounds. Two compromised-node assignments
/// that differ only in which members of a symmetry cell are compromised are
/// equivalent under relabeling for the adversarial prefix, so the framework
/// generates only one representative per equivalence class. This keeps the case
/// budget focused on structurally distinct attack configurations. The
/// synchronous suffix may elect different concrete participant indices after
/// such a relabeling, but under full synchrony the pass/fail verdict is
/// invariant to that relabeling.
///
/// The generator selects canonical scenarios first: all scenarios when the
/// scenario count fits within [`Framework::max_cases`], or uniformly sampled
/// scenario ranks when it does not. It then expands the selected scenarios
/// across their symmetry-unique compromised assignments, shuffles the cases,
/// and truncates to [`Framework::max_cases`]. Scenario counts must fit in
/// `u128`, overflowing configurations panic.
#[derive(Clone, Copy, Debug)]
pub struct Framework {
    /// Number of participants in the network. Must be at most 64.
    pub participants: usize,
    /// Number of compromised participants.
    pub faults: usize,
    /// Number of adversarial rounds before synchronous suffix.
    pub rounds: usize,
    /// How multi-round scenarios are constructed.
    pub mode: Mode,
    /// Upper bound on emitted cases. This also caps the number of canonical
    /// scenarios selected before compromised assignments are expanded.
    pub max_cases: usize,
}

/// Executable case from the Twins framework.
#[derive(Clone, Debug)]
pub struct Case {
    /// Compromised participant indices for this case.
    pub compromised: Compromised,
    /// Multi-round scenario for this case.
    pub scenario: Scenario,
}

/// Generate executable Twins cases from framework parameters.
///
/// The generator computes exact scenario counts from compressed transition
/// multiplicities. It emits every scenario when the count fits within
/// [`Framework::max_cases`] and samples scenario ranks uniformly without
/// replacement when it does not. For each canonical scenario, the generator
/// computes the residual symmetry cells and emits only the structurally unique
/// compromised-node assignments.
///
/// # Panics
///
/// Panics if `framework` has fewer than 2 participants, zero faults, faults
/// greater than or equal to participants, more than 64 participants, zero
/// rounds, or zero max cases. Panics if the canonical scenario count overflows
/// `u128`.
pub fn cases(rng: &mut impl Rng, framework: Framework) -> Vec<Case> {
    assert!(framework.participants > 1, "participants must be > 1");
    assert!(
        framework.participants <= MAX_PARTICIPANTS,
        "participants must be <= {MAX_PARTICIPANTS}"
    );
    assert!(framework.faults > 0, "faults must be > 0");
    assert!(
        framework.faults < framework.participants,
        "faults must be less than participants"
    );
    assert!(framework.rounds > 0, "rounds must be > 0");
    assert!(framework.max_cases > 0, "max_cases must be > 0");

    let mut generator = ScenarioGenerator::new(framework.participants);
    let scenarios = match framework.mode {
        Mode::Sampled => generator.generate(rng, framework.rounds, framework.max_cases),
        Mode::Sustained => {
            // Generate 1-round scenarios and repeat across all rounds.
            // The 1-round residual cells are valid here because applying
            // the same recipient-set pattern repeatedly never distinguishes
            // participants that were indistinguishable after the first round.
            let single_round = generator.generate(rng, 1, framework.max_cases);
            single_round
                .into_iter()
                .map(|(s, cells)| {
                    let scenario = Scenario {
                        rounds: vec![s.rounds[0]; framework.rounds],
                    };
                    (scenario, cells)
                })
                .collect()
        }
    };

    let mut result: Vec<Case> = scenarios
        .iter()
        .flat_map(|(scenario, residual_cells)| {
            compromised_sets_for_cells(residual_cells, framework.faults)
                .into_iter()
                .map(move |compromised| Case {
                    compromised,
                    scenario: scenario.clone(),
                })
        })
        .collect();
    result.shuffle(rng);
    result.truncate(framework.max_cases);
    result
}

/// Materializes the participants selected by a bitmask.
fn partition_for_mask<P: Clone>(mask: u64, participants: &[P]) -> Vec<P> {
    let mut group = Vec::new();
    for (idx, participant) in participants.iter().enumerate() {
        if mask_contains(mask, idx) {
            group.push(participant.clone());
        }
    }
    group
}

/// Sets bit `idx` in `mask`.
#[inline]
const fn set_mask_bit(mask: &mut u64, idx: usize) {
    *mask |= 1u64 << idx;
}

/// Sets every bit in the contiguous range `[start, start + len)`.
#[inline]
const fn set_mask_range(mask: &mut u64, start: usize, len: usize) {
    if len == 0 {
        return;
    }
    let range = if len == u64::BITS as usize {
        u64::MAX
    } else {
        ((1u64 << len) - 1) << start
    };
    *mask |= range;
}

/// Returns whether bit `idx` is set in `mask`.
#[inline]
const fn mask_contains(mask: u64, idx: usize) -> bool {
    idx < MAX_PARTICIPANTS && (mask & (1u64 << idx)) != 0
}

/// Converts cell sizes into contiguous index ranges.
fn cells_to_ranges(cells: &[usize]) -> Vec<(usize, usize)> {
    let mut start = 0usize;
    cells
        .iter()
        .copied()
        .map(|size| {
            let range = (start, size);
            start += size;
            range
        })
        .collect()
}

/// Compact representation of a canonical cell partition.
///
/// Bit `i` is set when there is a cell boundary after participant `i`. The
/// unset gaps between boundaries belong to the same residual symmetry cell.
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct CellState {
    boundaries: u64,
}

impl CellState {
    /// Returns the state with a single symmetry cell covering all participants.
    fn initial(n: usize) -> Self {
        assert!(
            n <= MAX_PARTICIPANTS,
            "participants must be <= {MAX_PARTICIPANTS}"
        );
        Self { boundaries: 0 }
    }

    /// Converts explicit cell sizes into a boundary mask.
    #[cfg(test)]
    fn from_cells(cells: &[usize]) -> Self {
        let n = cells.iter().sum();
        let mut state = Self::initial(n);
        let mut end = 0usize;
        for size in cells.iter().take(cells.len().saturating_sub(1)) {
            end += size;
            set_mask_bit(&mut state.boundaries, end - 1);
        }
        state
    }

    /// Returns whether this state has a boundary after `idx`.
    const fn boundary_after(&self, idx: usize) -> bool {
        mask_contains(self.boundaries, idx)
    }

    /// Converts the state back into cell sizes.
    fn to_cells(self, n: usize) -> Cells {
        self.ranges(n).into_iter().map(|(_, len)| len).collect()
    }

    /// Converts the state into contiguous `(start, len)` cell ranges.
    fn ranges(&self, n: usize) -> Vec<(usize, usize)> {
        let mut ranges = Vec::new();
        let mut start = 0usize;
        for idx in 0..n.saturating_sub(1) {
            if self.boundary_after(idx) {
                ranges.push((start, idx + 1 - start));
                start = idx + 1;
            }
        }
        ranges.push((start, n - start));
        ranges
    }

    /// Returns the positive parts of the cell `[start, start + size)`.
    fn parts_in_cell(&self, start: usize, size: usize) -> Parts {
        let end = start + size;
        let mut parts = Vec::new();
        let mut part_start = start;
        for idx in start..end {
            if idx + 1 == end || self.boundary_after(idx) {
                parts.push(idx + 1 - part_start);
                part_start = idx + 1;
            }
        }
        parts
    }

    /// Adds boundaries for `parts` starting at `start`.
    fn add_local_boundaries(&mut self, start: usize, n: usize, parts: &[usize]) {
        let mut offset = 0usize;
        for part in parts {
            offset += part;
            let end = start + offset;
            if end < n {
                set_mask_bit(&mut self.boundaries, end - 1);
            }
        }
    }
}

/// One local cell refinement used while building compressed transition edges.
///
/// A non-leader cell can split into at most the four non-empty role buckets
/// `outside`, `both`, `secondary-only`, and `primary-only`. The leader cell
/// uses the same buckets for its non-leader prefix and then appends the leader
/// singleton as the final part. `parts` stores the resulting non-empty bucket
/// sizes, `multiplicity` counts how many ordered role-bucket choices produce
/// those same sizes.
#[derive(Clone, Copy, Debug)]
struct LocalRefinement {
    parts: [usize; 5],
    len: usize,
    multiplicity: u128,
}

impl LocalRefinement {
    /// Creates a refinement from `parts`, copying them into fixed storage.
    fn new(parts: &[usize], multiplicity: u128) -> Self {
        let mut stored = [0usize; 5];
        stored[..parts.len()].copy_from_slice(parts);
        Self {
            parts: stored,
            len: parts.len(),
            multiplicity,
        }
    }

    /// Returns the non-zero contiguous parts that form this refinement.
    fn parts(&self) -> &[usize] {
        &self.parts[..self.len]
    }
}

/// Compressed transition to a residual cell state.
///
/// `multiplicity` is the exact number of `RoundScenario`s that produce `next`
/// when `leader_cell` supplies the canonical leader. During suffix counting,
/// this edge contributes `multiplicity * suffix_count(next)`. This value must
/// match the product of the per-cell local spaces decoded by `round_from_edge`.
#[derive(Clone, Copy, Debug)]
struct TransitionEdge {
    next: CellState,
    leader_cell: usize,
    multiplicity: u128,
}

/// Compressed outgoing transitions for one residual cell state.
///
/// `overflowed` is set when at least one transition multiplicity cannot be
/// represented as a `u128`, scenario counting treats that as count overflow.
#[derive(Clone, Debug, Default)]
struct TransitionSet {
    edges: Vec<TransitionEdge>,
    overflowed: bool,
}

/// Recursively combines precomputed local cell refinements into compressed edges.
fn build_transitions(
    ranges: &[(usize, usize)],
    refinements_by_cell: &[Vec<LocalRefinement>],
    leader_cell: usize,
    cell_idx: usize,
    next_state: CellState,
    multiplicity: u128,
    transitions: &mut TransitionSet,
) {
    if cell_idx == ranges.len() {
        // Every current cell has chosen one local refinement. The accumulated
        // boundary mask identifies the residual state reached by this round,
        // and multiplicity is the product of the local role spaces that map to
        // that same residual state.
        transitions.edges.push(TransitionEdge {
            next: next_state,
            leader_cell,
            multiplicity,
        });
        return;
    }

    let (start, _) = ranges[cell_idx];
    let n = ranges
        .last()
        .map(|(start, len)| start + len)
        .expect("transition ranges must be non-empty");
    for refinement in refinements_by_cell[cell_idx].iter().copied() {
        // Choose one refinement for this cell and carry its contribution into
        // the DFS prefix. Different refinements may produce the same
        // `next_state`, keeping them as separate edges is intentional because
        // unranking uses each edge's multiplicity to reconstruct a concrete
        // round.
        let Some(multiplicity) = multiplicity.checked_mul(refinement.multiplicity) else {
            transitions.overflowed = true;
            continue;
        };
        let mut next_state = next_state;
        next_state.add_local_boundaries(start, n, refinement.parts());
        build_transitions(
            ranges,
            refinements_by_cell,
            leader_cell,
            cell_idx + 1,
            next_state,
            multiplicity,
            transitions,
        );
    }
}

/// Role of a non-leader contiguous part in a round refinement.
///
/// The declaration order is the canonical role order used when reconstructing
/// a concrete transition from its rank.
#[derive(Clone, Copy, Debug)]
enum Role {
    Outside,
    Both,
    Secondary,
    Primary,
}

/// Scenario generator for a fixed participant count.
///
/// The generator lazily memoizes the compressed transition DAG keyed by
/// residual symmetry state. Each transition set describes the canonical
/// one-round refinements reachable from a state, with edge multiplicities
/// carrying the number of concrete role choices represented by that edge.
/// Generation first builds exact suffix counts over this DAG, then enumerates
/// or samples scenario ranks and unpacks each selected rank into concrete
/// [`RoundScenario`] masks.
struct ScenarioGenerator {
    /// Number of participants in the generated scenarios.
    n: usize,
    /// Memoized outgoing transitions for each residual symmetry state.
    transition_memo: HashMap<CellState, TransitionSet>,
    /// Memoized cell-local refinements keyed by cell size and whether the cell
    /// contains the round leader.
    local_memo: HashMap<(usize, bool), Vec<LocalRefinement>>,
}

impl ScenarioGenerator {
    /// Creates a generator for `n` participants.
    fn new(n: usize) -> Self {
        Self {
            n,
            transition_memo: HashMap::new(),
            local_memo: HashMap::new(),
        }
    }

    /// Enumerates canonical scenarios with their residual symmetry cells.
    ///
    /// Returns all scenarios when the space fits within `max_scenarios`,
    /// otherwise samples `max_scenarios` without replacement.
    ///
    /// # Panics
    ///
    /// Panics if the scenario space overflows `u128`.
    fn generate(
        &mut self,
        rng: &mut impl Rng,
        rounds: usize,
        max_scenarios: usize,
    ) -> Vec<(Scenario, Cells)> {
        let initial = CellState::initial(self.n);

        // Count the complete canonical scenario space before choosing which
        // ranks to materialize. The same count table is reused by unranking so
        // each selected rank can skip whole compressed subtrees.
        let counts = self
            .counts(&initial, rounds)
            .expect("scenario space overflows u128; reduce rounds or participants");
        let total = counts[rounds][&initial];

        // If the requested budget covers the whole space, enumerate ranks in
        // canonical order. Otherwise sample distinct ranks uniformly and
        // materialize only those scenarios.
        if total <= max_scenarios as u128 {
            return (0..total)
                .map(|idx| self.scenario_from_rank(&initial, rounds, idx, &counts))
                .collect();
        }
        sample_unique_indices(rng, total, max_scenarios)
            .into_iter()
            .map(|idx| self.scenario_from_rank(&initial, rounds, idx, &counts))
            .collect()
    }

    /// Builds bottom-up scenario counts for paths starting at `initial`.
    ///
    /// `counts[r][state]` is the exact number of canonical scenario suffixes
    /// of length `r` reachable from `state`. `None` indicates arithmetic
    /// overflow while building transitions or summing suffix counts.
    fn counts(
        &mut self,
        initial: &CellState,
        rounds: usize,
    ) -> Option<Vec<HashMap<CellState, u128>>> {
        // First discover which residual states can appear at each depth from
        // the initial state. Later DP layers only need to account for these
        // reachable states.
        let mut layers = Vec::with_capacity(rounds + 1);
        layers.push(HashSet::from([*initial]));

        for depth in 0..rounds {
            let mut next_layer = HashSet::new();
            for state in layers[depth].iter() {
                self.ensure_transitions(state);
                let transitions = self.transitions(state);
                if transitions.overflowed {
                    return None;
                }
                for edge in &transitions.edges {
                    next_layer.insert(edge.next);
                }
            }
            layers.push(next_layer);
        }

        // A zero-round suffix has exactly one continuation: emit no more
        // rounds. Seed that base case for every state reachable after the full
        // scripted prefix.
        let mut counts = vec![HashMap::new(); rounds + 1];
        for state in layers[rounds].iter() {
            counts[0].insert(*state, 1);
        }

        // Fold suffix counts back toward the initial state. Each outgoing edge
        // contributes its round multiplicity times the number of suffixes from
        // the edge's next residual state.
        for remaining in 1..=rounds {
            let depth = rounds - remaining;
            for state in layers[depth].iter() {
                self.ensure_transitions(state);
                let transitions = self.transitions(state);
                if transitions.overflowed {
                    return None;
                }

                let mut total = 0u128;
                for edge in &transitions.edges {
                    let suffix = counts[remaining - 1]
                        .get(&edge.next)
                        .copied()
                        .expect("next state missing from count layer");
                    let edge_count = edge.multiplicity.checked_mul(suffix)?;
                    total = total.checked_add(edge_count)?;
                }
                counts[remaining].insert(*state, total);
            }
        }

        Some(counts)
    }

    /// Reconstructs the scenario at `rank` under the compressed ordering.
    fn scenario_from_rank(
        &mut self,
        initial: &CellState,
        rounds: usize,
        mut rank: u128,
        counts: &[HashMap<CellState, u128>],
    ) -> (Scenario, Cells) {
        let mut scenario = Vec::with_capacity(rounds);
        let mut state = *initial;

        // Walk the scenario prefix from left to right. At each residual state,
        // each edge owns a contiguous rank interval equal to its concrete
        // round multiplicity times the number of suffixes behind its next
        // state.
        for remaining in (1..=rounds).rev() {
            self.ensure_transitions(&state);
            let transitions = self.transitions(&state);
            assert!(
                !transitions.overflowed,
                "transition multiplicity should fit in u128"
            );

            let mut selected = false;
            for edge in &transitions.edges {
                let suffix = counts[remaining - 1]
                    .get(&edge.next)
                    .copied()
                    .expect("next state missing from count layer");
                let subtree_count = edge
                    .multiplicity
                    .checked_mul(suffix)
                    .expect("canonical scenario count should fit in u128");

                if rank < subtree_count {
                    // The high digit selects which concrete round within this
                    // edge to decode, the remainder is the suffix rank used at
                    // the next residual state.
                    let transition_rank = rank / suffix;
                    scenario.push(self.round_from_edge(&state, edge, transition_rank));
                    state = edge.next;
                    rank %= suffix;
                    selected = true;
                    break;
                }
                rank -= subtree_count;
            }
            assert!(selected, "canonical scenario rank out of bounds");
        }

        (Scenario { rounds: scenario }, state.to_cells(self.n))
    }

    /// Ensures compressed transition edges for `state` are memoized.
    fn ensure_transitions(&mut self, state: &CellState) {
        if self.transition_memo.contains_key(state) {
            return;
        }
        let ranges = state.ranges(self.n);
        let mut transitions = TransitionSet::default();

        // Try each residual cell as the canonical source of this round's
        // leader. The leader cell's local refinements reserve its final
        // participant as a singleton leader part.
        for leader_cell in 0..ranges.len() {
            let refinements: Vec<_> = ranges
                .iter()
                .enumerate()
                .map(|(cell_idx, (_, size))| {
                    self.local_refinements(*size, cell_idx == leader_cell)
                        .to_vec()
                })
                .collect();

            // Combine one local refinement per cell into complete outgoing
            // edges for this leader choice.
            build_transitions(
                &ranges,
                &refinements,
                leader_cell,
                0,
                CellState::initial(self.n),
                1,
                &mut transitions,
            );
        }
        self.transition_memo.insert(*state, transitions);
    }

    /// Returns memoized compressed transition edges for `state`.
    fn transitions(&self, state: &CellState) -> &TransitionSet {
        self.transition_memo
            .get(state)
            .expect("transition set should be memoized")
    }

    /// Returns all local refinements for a cell of `size`.
    fn local_refinements(&mut self, size: usize, is_leader: bool) -> &[LocalRefinement] {
        self.local_memo
            .entry((size, is_leader))
            .or_insert_with(|| build_local_refinements(size, is_leader))
            .as_slice()
    }

    /// Reconstructs one concrete round represented by `edge`.
    fn round_from_edge(
        &self,
        state: &CellState,
        edge: &TransitionEdge,
        mut rank: u128,
    ) -> RoundScenario {
        let mut primary_mask = 0u64;
        let mut secondary_mask = 0u64;
        let mut leader = None;

        // The edge fixes only the next residual cell boundaries. Decode the
        // edge-local rank in cell order: each low digit selects this cell's
        // role subset, and the leader cell has an extra digit for which half
        // sees the leader.
        for (cell_idx, (start, size)) in state.ranges(self.n).into_iter().enumerate() {
            // Recover this current cell's part sizes by slicing it with the
            // next state's residual boundaries.
            let parts = edge.next.parts_in_cell(start, size);
            if cell_idx == edge.leader_cell {
                let non_leader_len = parts.len() - 1;
                let role_count = role_subset_count(non_leader_len);
                let local_space = role_count * 3;
                let local_rank = rank % local_space;
                rank /= local_space;

                // The leader cell first consumes a role-subset digit for its
                // non-leader prefix, then one of three leader placements:
                // primary-only, secondary-only, or both halves.
                let roles = roles_from_rank(non_leader_len, local_rank / 3);
                apply_roles(
                    start,
                    &parts[..non_leader_len],
                    &roles[..non_leader_len],
                    &mut primary_mask,
                    &mut secondary_mask,
                );

                let leader_idx = start + size - 1;
                match local_rank % 3 {
                    0 => set_mask_bit(&mut primary_mask, leader_idx),
                    1 => set_mask_bit(&mut secondary_mask, leader_idx),
                    2 => {
                        set_mask_bit(&mut primary_mask, leader_idx);
                        set_mask_bit(&mut secondary_mask, leader_idx);
                    }
                    _ => unreachable!("leader placement rank out of bounds"),
                }
                leader = Some(leader_idx);
            } else {
                let role_count = role_subset_count(parts.len());
                let local_rank = rank % role_count;
                rank /= role_count;

                // Non-leader cells only consume a role-subset digit.
                let roles = roles_from_rank(parts.len(), local_rank);
                apply_roles(
                    start,
                    &parts,
                    &roles[..parts.len()],
                    &mut primary_mask,
                    &mut secondary_mask,
                );
            }
        }

        assert_eq!(rank, 0, "transition rank should be fully consumed");
        RoundScenario {
            leader: leader.expect("leader cell should assign a leader"),
            primary_mask,
            secondary_mask,
        }
    }
}

/// Number of ways to choose `len` non-empty role buckets from four ordered
/// non-leader roles.
const fn role_subset_count(len: usize) -> u128 {
    match len {
        0 => 1,
        1 => 4,
        2 => 6,
        3 => 4,
        4 => 1,
        _ => panic!("role bucket count exceeds role space"),
    }
}

/// Builds all local refinements for a cell.
///
/// A local refinement is the shape this cell can have after one round. The
/// shape only stores contiguous part sizes, `multiplicity` records how many
/// assignments of those parts to the ordered non-leader roles produce the same
/// shape.
fn build_local_refinements(size: usize, is_leader: bool) -> Vec<LocalRefinement> {
    let mut refinements = Vec::new();
    let available = if is_leader { size - 1 } else { size };
    let mut prefix = Vec::new();
    for_each_composition(available, 4, &mut prefix, &mut |parts| {
        // `parts` partitions the non-leader portion into the non-empty role
        // buckets that will remain distinguishable after this round.
        let role_choices = role_subset_count(parts.len());
        let mut local = Vec::with_capacity(parts.len() + usize::from(is_leader));
        local.extend_from_slice(parts);
        let multiplicity = if is_leader {
            // The leader is always the final participant in its current cell
            // and becomes a singleton residual cell. It can be visible to the
            // primary half, the secondary half, or both halves.
            local.push(1);
            role_choices * 3
        } else {
            role_choices
        };
        refinements.push(LocalRefinement::new(&local, multiplicity));
    });
    refinements
}

/// Enumerates positive compositions of `total` into at most `max_parts` parts.
///
/// The empty composition is emitted for `total == 0`, which is needed for a
/// singleton leader cell with no non-leader prefix.
fn for_each_composition<F: FnMut(&[usize])>(
    total: usize,
    max_parts: usize,
    current: &mut Parts,
    f: &mut F,
) {
    if total == 0 {
        f(current);
        return;
    }
    for parts in 1..=total.min(max_parts) {
        for_each_composition_with_len(total, parts, current, f);
    }
}

/// Enumerates positive compositions of `remaining` with exactly `parts_left`
/// parts.
fn for_each_composition_with_len<F: FnMut(&[usize])>(
    remaining: usize,
    parts_left: usize,
    current: &mut Parts,
    f: &mut F,
) {
    if parts_left == 0 {
        if remaining == 0 {
            f(current);
        }
        return;
    }
    if parts_left == 1 {
        current.push(remaining);
        f(current);
        current.pop();
        return;
    }

    let max_first = remaining - (parts_left - 1);
    for first in 1..=max_first {
        current.push(first);
        for_each_composition_with_len(remaining - first, parts_left - 1, current, f);
        current.pop();
    }
}

/// Returns the ranked subset of `len` roles from the four ordered non-leader
/// roles. The selected roles are returned in canonical role order.
///
/// A local refinement with `len` parts does not store which role bucket each
/// part came from. Its rank selects one of the `C(4, len)` non-empty subsets of
/// the ordered role list: outside, both, secondary-only, primary-only.
fn roles_from_rank(len: usize, rank: u128) -> [Role; 4] {
    let mut seen = 0u128;
    for mask in 0u8..16 {
        if mask.count_ones() as usize != len {
            continue;
        }
        if seen == rank {
            let mut roles = [Role::Outside; 4];
            let mut next = 0usize;
            // Scan masks in numeric order while assigning selected bits in the
            // canonical role order. This is the inverse of the
            // `role_subset_count(len)` space used by local refinements.
            for (bit, role) in [Role::Outside, Role::Both, Role::Secondary, Role::Primary]
                .into_iter()
                .enumerate()
            {
                if (mask & (1u8 << bit)) != 0 {
                    roles[next] = role;
                    next += 1;
                }
            }
            return roles;
        }
        seen += 1;
    }
    unreachable!("role subset rank out of bounds")
}

/// Applies `roles` to contiguous `parts` starting at `start`.
fn apply_roles(
    start: usize,
    parts: &[usize],
    roles: &[Role],
    primary_mask: &mut u64,
    secondary_mask: &mut u64,
) {
    let mut offset = 0usize;
    for (part, role) in parts.iter().zip(roles) {
        let range_start = start + offset;
        match role {
            Role::Outside => {}
            Role::Both => {
                set_mask_range(primary_mask, range_start, *part);
                set_mask_range(secondary_mask, range_start, *part);
            }
            Role::Secondary => set_mask_range(secondary_mask, range_start, *part),
            Role::Primary => set_mask_range(primary_mask, range_start, *part),
        }
        offset += part;
    }
}

/// Generates compromised-node assignments that are unique modulo residual
/// symmetry cells.
///
/// Two assignments are equivalent if one can be obtained from the other by
/// permuting participants within the same cell. This function emits exactly
/// one canonical representative per equivalence class by always picking the
/// first indices from each cell.
fn compromised_sets_for_cells(cells: &[usize], faults: usize) -> Vec<Compromised> {
    let ranges = cells_to_ranges(cells);
    let mut result = Vec::new();
    let mut current = Vec::new();
    allocate_faults(&ranges, 0, faults, &mut current, &mut result);
    result
}

/// Recursively allocates `remaining` faults across cells starting at
/// `cell_idx`, picking the first `take` indices from each cell as the
/// canonical representative.
fn allocate_faults(
    ranges: &[(usize, usize)],
    cell_idx: usize,
    remaining: usize,
    current: &mut Compromised,
    result: &mut Vec<Compromised>,
) {
    if remaining == 0 {
        result.push(current.clone());
        return;
    }
    if cell_idx >= ranges.len() {
        return;
    }
    let (start, size) = ranges[cell_idx];
    let remaining_capacity: usize = ranges[cell_idx..].iter().map(|(_, s)| *s).sum();
    if remaining > remaining_capacity {
        return;
    }
    let max_from_this = remaining.min(size);
    for take in 0..=max_from_this {
        for i in 0..take {
            current.push(start + i);
        }
        allocate_faults(ranges, cell_idx + 1, remaining - take, current, result);
        for _ in 0..take {
            current.pop();
        }
    }
}

/// Samples unique indices without replacement from `[0, total)`.
fn sample_unique_indices(rng: &mut impl Rng, total: u128, samples: usize) -> Vec<u128> {
    assert!(
        (samples as u128) <= total,
        "cannot sample more unique indices than total domain size"
    );
    if samples == 0 {
        return Vec::new();
    }

    // Floyd's algorithm: O(samples) time, no retry loop.
    let mut sampled = Vec::with_capacity(samples);
    let mut seen = HashSet::with_capacity(samples);
    for idx in (total - samples as u128)..total {
        let candidate = rng.random_range(0..=idx);
        if seen.insert(candidate) {
            sampled.push(candidate);
        } else {
            assert!(
                seen.insert(idx),
                "tail index should be unique in Floyd sampling"
            );
            sampled.push(idx);
        }
    }
    sampled
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{
        simplex::{
            elector::{Config as ElectorConfig, RoundRobin},
            scheme::ed25519,
        },
        types::Epoch,
    };
    use commonware_cryptography::{ed25519::PrivateKey, Sha256, Signer};
    use commonware_utils::{ordered::Set, test_rng, TestRng};
    use std::collections::HashSet;

    fn round(_: usize, leader: usize, primary_mask: u64, secondary_mask: u64) -> RoundScenario {
        RoundScenario {
            leader,
            primary_mask,
            secondary_mask,
        }
    }

    fn range_mask(start: usize, len: usize) -> u64 {
        if len == 0 {
            return 0;
        }
        (((1u128 << len) - 1) << start) as u64
    }

    // Independent reference enumerator for small test cases. The production
    // generator uses compressed transition edges, while this helper expands
    // next-round refinements directly so tests can compare the DAG counter and
    // unranker against a simpler implementation.
    fn reference_next_round_transitions(cells: &[usize]) -> Vec<(RoundScenario, Cells)> {
        let n = cells.iter().sum();

        #[derive(Clone, Copy)]
        struct PrimaryState {
            primary_mask: u64,
            leader: Option<usize>,
        }

        #[derive(Clone, Copy)]
        struct SecondaryState {
            primary_mask: u64,
            secondary_mask: u64,
            leader: Option<usize>,
        }

        const LEADER_PLACEMENTS: &[(bool, bool)] = &[(true, false), (false, true), (true, true)];

        fn push_nonzero_cells(next_cells: &mut Cells, sizes: [usize; 4]) {
            for size in sizes {
                if size > 0 {
                    next_cells.push(size);
                }
            }
        }

        fn no_secondary(
            n: usize,
            ranges: &[(usize, usize)],
            leader_cell: usize,
            cell_idx: usize,
            state: PrimaryState,
            next_cells: &mut Cells,
            out: &mut Vec<(RoundScenario, Cells)>,
        ) {
            if cell_idx == ranges.len() {
                out.push((
                    round(
                        n,
                        state.leader.expect("leader cell should assign a leader"),
                        state.primary_mask,
                        state.primary_mask,
                    ),
                    next_cells.clone(),
                ));
                return;
            }

            let (start, size) = ranges[cell_idx];
            let max_outside = if cell_idx == leader_cell {
                size - 1
            } else {
                size
            };
            for outside in 0..=max_outside {
                let both = if cell_idx == leader_cell {
                    size - outside - 1
                } else {
                    size - outside
                };

                if outside > 0 {
                    next_cells.push(outside);
                }
                if both > 0 {
                    next_cells.push(both);
                }

                let mut next_primary = state.primary_mask | range_mask(start + outside, both);
                let mut next_leader = state.leader;
                if cell_idx == leader_cell {
                    let leader_idx = start + outside + both;
                    next_primary |= 1u64 << leader_idx;
                    next_cells.push(1);
                    next_leader = Some(leader_idx);
                }

                no_secondary(
                    n,
                    ranges,
                    leader_cell,
                    cell_idx + 1,
                    PrimaryState {
                        primary_mask: next_primary,
                        leader: next_leader,
                    },
                    next_cells,
                    out,
                );

                if cell_idx == leader_cell {
                    next_cells.pop();
                }
                if both > 0 {
                    next_cells.pop();
                }
                if outside > 0 {
                    next_cells.pop();
                }
            }
        }

        fn with_secondary(
            n: usize,
            ranges: &[(usize, usize)],
            leader_cell: usize,
            cell_idx: usize,
            state: SecondaryState,
            next_cells: &mut Cells,
            out: &mut Vec<(RoundScenario, Cells)>,
        ) {
            if cell_idx == ranges.len() {
                if state.primary_mask == state.secondary_mask {
                    return;
                }
                out.push((
                    round(
                        n,
                        state.leader.expect("leader cell should assign a leader"),
                        state.primary_mask,
                        state.secondary_mask,
                    ),
                    next_cells.clone(),
                ));
                return;
            }

            let (start, size) = ranges[cell_idx];
            let has_leader = cell_idx == leader_cell;
            let available = if has_leader { size - 1 } else { size };

            for outside in 0..=available {
                let remaining_after_outside = available - outside;
                for both in 0..=remaining_after_outside {
                    let remaining = remaining_after_outside - both;
                    for secondary in 0..=remaining {
                        let primary = remaining - secondary;
                        let checkpoint = next_cells.len();
                        push_nonzero_cells(next_cells, [outside, both, secondary, primary]);

                        let shared_mask = range_mask(start + outside, both);
                        let next_secondary = state.secondary_mask
                            | shared_mask
                            | range_mask(start + outside + both, secondary);
                        let next_primary = state.primary_mask
                            | shared_mask
                            | range_mask(start + outside + both + secondary, primary);
                        if has_leader {
                            let leader_idx = start + outside + both + secondary + primary;
                            next_cells.push(1);
                            for &(leader_in_primary, leader_in_secondary) in LEADER_PLACEMENTS {
                                let leader_bit = 1u64 << leader_idx;
                                with_secondary(
                                    n,
                                    ranges,
                                    leader_cell,
                                    cell_idx + 1,
                                    SecondaryState {
                                        primary_mask: if leader_in_primary {
                                            next_primary | leader_bit
                                        } else {
                                            next_primary
                                        },
                                        secondary_mask: if leader_in_secondary {
                                            next_secondary | leader_bit
                                        } else {
                                            next_secondary
                                        },
                                        leader: Some(leader_idx),
                                    },
                                    next_cells,
                                    out,
                                );
                            }
                        } else {
                            with_secondary(
                                n,
                                ranges,
                                leader_cell,
                                cell_idx + 1,
                                SecondaryState {
                                    primary_mask: next_primary,
                                    secondary_mask: next_secondary,
                                    leader: state.leader,
                                },
                                next_cells,
                                out,
                            );
                        }
                        next_cells.truncate(checkpoint);
                    }
                }
            }
        }

        let ranges = cells_to_ranges(cells);
        let mut out = Vec::new();
        for leader_cell in 0..cells.len() {
            let mut next_cells = Vec::new();
            no_secondary(
                n,
                &ranges,
                leader_cell,
                0,
                PrimaryState {
                    primary_mask: 0,
                    leader: None,
                },
                &mut next_cells,
                &mut out,
            );
            let mut next_cells = Vec::new();
            with_secondary(
                n,
                &ranges,
                leader_cell,
                0,
                SecondaryState {
                    primary_mask: 0,
                    secondary_mask: 0,
                    leader: None,
                },
                &mut next_cells,
                &mut out,
            );
        }
        out
    }

    fn reference_scenario_count(
        cells: &[usize],
        rounds: usize,
        memo: &mut HashMap<(Cells, usize), Option<u128>>,
    ) -> Option<u128> {
        if rounds == 0 {
            return Some(1);
        }
        let key = (cells.to_vec(), rounds);
        if let Some(cached) = memo.get(&key) {
            return *cached;
        }

        let result = (|| {
            let mut total = 0u128;
            for (_, next_cells) in reference_next_round_transitions(cells) {
                let suffix = reference_scenario_count(&next_cells, rounds - 1, memo)?;
                total = total.checked_add(suffix)?;
            }
            Some(total)
        })();
        memo.insert(key, result);
        result
    }

    fn reference_scenarios(cells: &[usize], rounds: usize) -> HashSet<(Scenario, Cells)> {
        if rounds == 0 {
            return HashSet::from([(Scenario { rounds: Vec::new() }, cells.to_vec())]);
        }

        let mut out = HashSet::new();
        for (round, next_cells) in reference_next_round_transitions(cells) {
            for (mut scenario, residual) in reference_scenarios(&next_cells, rounds - 1) {
                scenario.rounds.insert(0, round);
                out.insert((scenario, residual));
            }
        }
        out
    }

    fn expected_single_round_transitions(n: usize) -> HashSet<(RoundScenario, Cells)> {
        let mut out = HashSet::new();

        for outside in 0..n {
            let both = n - outside - 1;
            let leader = outside + both;
            let leader_bit = 1u64 << leader;
            let mask = range_mask(outside, both) | leader_bit;

            let mut cells = Vec::new();
            if outside > 0 {
                cells.push(outside);
            }
            if both > 0 {
                cells.push(both);
            }
            cells.push(1);

            out.insert((round(n, leader, mask, mask), cells));
        }

        for outside in 0..n {
            let remaining_after_outside = n - outside - 1;
            for both in 0..=remaining_after_outside {
                let remaining = remaining_after_outside - both;
                for secondary in 0..=remaining {
                    let primary = remaining - secondary;
                    let leader = outside + both + secondary + primary;
                    let leader_bit = 1u64 << leader;
                    let shared_mask = range_mask(outside, both);
                    let secondary_mask = shared_mask | range_mask(outside + both, secondary);
                    let primary_mask =
                        shared_mask | range_mask(outside + both + secondary, primary);

                    let mut cells = Vec::new();
                    if outside > 0 {
                        cells.push(outside);
                    }
                    if both > 0 {
                        cells.push(both);
                    }
                    if secondary > 0 {
                        cells.push(secondary);
                    }
                    if primary > 0 {
                        cells.push(primary);
                    }
                    cells.push(1);

                    out.insert((
                        round(n, leader, primary_mask | leader_bit, secondary_mask),
                        cells.clone(),
                    ));
                    out.insert((
                        round(n, leader, primary_mask, secondary_mask | leader_bit),
                        cells.clone(),
                    ));
                    out.insert((
                        round(
                            n,
                            leader,
                            primary_mask | leader_bit,
                            secondary_mask | leader_bit,
                        ),
                        cells,
                    ));
                }
            }
        }

        out
    }

    #[test]
    fn generated_cases_are_deterministic() {
        let framework = Framework {
            participants: 5,
            faults: 1,
            rounds: 3,
            mode: Mode::Sampled,
            max_cases: 50,
        };
        let first = cases(&mut test_rng(), framework);
        let second = cases(&mut test_rng(), framework);
        assert_eq!(first.len(), second.len());
        for (a, b) in first.iter().zip(second.iter()) {
            assert_eq!(a.compromised, b.compromised);
            assert_eq!(a.scenario, b.scenario);
        }
    }

    #[test]
    fn sampled_cases_are_unique() {
        let framework = Framework {
            participants: 7,
            faults: 2,
            rounds: 3,
            mode: Mode::Sampled,
            max_cases: 64,
        };
        let generated = cases(&mut test_rng(), framework);
        assert_eq!(generated.len(), framework.max_cases);

        let mut seen = HashSet::new();
        for case in generated {
            assert!(
                seen.insert((case.scenario, case.compromised)),
                "duplicate generated case"
            );
        }
    }

    #[test]
    fn generated_single_round_scenarios_match_expected_canonical_space() {
        for n in 3..=5 {
            let mut generator = ScenarioGenerator::new(n);
            let generated: HashSet<_> = generator
                .generate(&mut test_rng(), 1, usize::MAX)
                .into_iter()
                .map(|(scenario, cells)| (scenario.rounds[0], cells))
                .collect();
            assert_eq!(generated, expected_single_round_transitions(n));
        }
    }

    #[test]
    fn cell_state_roundtrips_cell_partitions() {
        for cells in [
            vec![5],
            vec![1, 4],
            vec![2, 1, 2],
            vec![1, 1, 1, 1],
            vec![3, 2, 1],
        ] {
            let n = cells.iter().sum();
            let state = CellState::from_cells(&cells);
            let ranges = cells_to_ranges(&cells);

            assert_eq!(state.to_cells(n), cells);
            assert_eq!(state.ranges(n), ranges);
        }
    }

    #[test]
    fn compressed_counts_match_reference_enumeration() {
        for n in 2..=4 {
            for rounds in 1..=3 {
                let initial_cells = vec![n];
                let mut reference_memo = HashMap::new();
                let reference =
                    reference_scenario_count(&initial_cells, rounds, &mut reference_memo);

                let initial = CellState::from_cells(&initial_cells);
                let mut generator = ScenarioGenerator::new(n);
                let counts = generator.counts(&initial, rounds);
                let compressed = counts.as_ref().map(|counts| counts[rounds][&initial]);
                assert_eq!(
                    compressed, reference,
                    "count mismatch for n={n} rounds={rounds}"
                );
            }
        }
    }

    #[test]
    fn compressed_unranking_matches_reference_space() {
        for (n, rounds) in [(2usize, 3usize), (3, 2)] {
            let mut generator = ScenarioGenerator::new(n);
            let generated: HashSet<_> = generator
                .generate(&mut test_rng(), rounds, usize::MAX)
                .into_iter()
                .collect();
            assert_eq!(
                generated,
                reference_scenarios(&[n], rounds),
                "mismatch for n={n} rounds={rounds}"
            );
        }
    }

    #[test]
    fn scripted_partitions_preserve_participant_order_and_overlap() {
        let scenario = Scenario {
            rounds: vec![round(4, 3, 0b1011, 0b0110)],
        };
        let participants: Vec<u32> = (0..4).collect();
        let (primary, secondary) = scenario.partitions(View::new(1), &participants);

        assert_eq!(primary, vec![0, 1, 3]);
        assert_eq!(secondary, vec![1, 2]);
    }

    #[test]
    fn route_with_groups_covers_reachable_split_targets() {
        let primary = vec![1u32, 2];
        let secondary = vec![2u32, 3];

        assert_eq!(
            route_with_groups(&2, &primary, &secondary),
            SplitTarget::Both
        );
        assert_eq!(
            route_with_groups(&1, &primary, &secondary),
            SplitTarget::Primary
        );
        assert_eq!(
            route_with_groups(&3, &primary, &secondary),
            SplitTarget::Secondary
        );
    }

    #[test]
    #[should_panic(expected = "sender not in any partition")]
    fn route_with_groups_panics_when_sender_is_missing() {
        let primary = vec![1u32, 2];
        let secondary = vec![2u32, 3];

        let _ = route_with_groups(&0, &primary, &secondary);
    }

    #[test]
    fn view_helpers_follow_modulo_split() {
        let participants: Vec<u32> = (0..4).collect();

        assert_eq!(
            view_partitions(View::new(1), &participants),
            (vec![0], vec![1, 2, 3])
        );
        assert_eq!(
            view_partitions(View::new(4), &participants),
            (Vec::<u32>::new(), participants.clone())
        );
        assert_eq!(
            view_route(View::new(1), &0, &participants),
            SplitTarget::Primary
        );
        assert_eq!(
            view_route(View::new(1), &3, &participants),
            SplitTarget::Secondary
        );
        assert_eq!(
            view_route(View::new(4), &0, &participants),
            SplitTarget::Secondary
        );
    }

    #[test]
    fn route_supports_shared_non_leaders_in_split_rounds() {
        let scenario = Scenario {
            rounds: vec![round(5, 4, 0b11010, 0b00110)],
        };
        let participants: Vec<u32> = (0..5).collect();
        assert_eq!(
            scenario.route(View::new(1), &0, &participants),
            SplitTarget::None
        );
        assert_eq!(
            scenario.route(View::new(1), &1, &participants),
            SplitTarget::Both
        );
        assert_eq!(
            scenario.route(View::new(1), &2, &participants),
            SplitTarget::Secondary
        );
        assert_eq!(
            scenario.route(View::new(1), &3, &participants),
            SplitTarget::Primary
        );
        assert_eq!(
            scenario.route(View::new(1), &4, &participants),
            SplitTarget::Primary
        );
    }

    #[test]
    fn leader_visible_to_both_halves_preserves_partitions_and_route() {
        let scenario = Scenario {
            rounds: vec![round(4, 3, 0b1010, 0b1100)],
        };
        let participants: Vec<u32> = (0..4).collect();
        let (primary, secondary) = scenario.partitions(View::new(1), &participants);

        assert_eq!(primary, vec![1, 3]);
        assert_eq!(secondary, vec![2, 3]);
        assert_eq!(
            scenario.route(View::new(1), &3, &participants),
            SplitTarget::Both
        );
    }

    #[test]
    fn generated_split_round_leaders_belong_to_at_least_one_half() {
        let mut generator = ScenarioGenerator::new(4);
        let scenarios = generator.generate(&mut test_rng(), 2, usize::MAX);
        for (scenario, _) in scenarios {
            for round in scenario.rounds {
                if round.primary_mask == round.secondary_mask {
                    continue;
                }
                let leader_in_primary = mask_contains(round.primary_mask, round.leader);
                let leader_in_secondary = mask_contains(round.secondary_mask, round.leader);
                assert!(leader_in_primary || leader_in_secondary);
            }
        }
    }

    #[test]
    fn route_selects_correct_half() {
        let scenario = Scenario {
            rounds: vec![round(4, 0, 0b0011, 0b1100)],
        };
        let participants: Vec<u32> = (0..4).collect();
        assert_eq!(
            scenario.route(View::new(1), &0, &participants),
            SplitTarget::Primary
        );
        assert_eq!(
            scenario.route(View::new(1), &1, &participants),
            SplitTarget::Primary
        );
        assert_eq!(
            scenario.route(View::new(1), &2, &participants),
            SplitTarget::Secondary
        );
        assert_eq!(
            scenario.route(View::new(1), &3, &participants),
            SplitTarget::Secondary
        );
    }

    #[test]
    fn route_returns_both_after_scripted_rounds() {
        let scenario = Scenario {
            rounds: vec![round(4, 0, 0b0011, 0b1100)],
        };
        let participants: Vec<u32> = (0..4).collect();

        for participant in &participants {
            assert_eq!(
                scenario.route(View::new(2), participant, &participants),
                SplitTarget::Both
            );
        }
    }

    #[test]
    fn scenarios_fall_back_to_synchrony_after_attack_rounds() {
        let scenario = Scenario {
            rounds: vec![round(4, 0, 0b0011, 0b1100)],
        };
        let participants: Vec<u32> = (0..4).collect();
        let (primary, secondary) = scenario.partitions(View::new(2), &participants);
        assert_eq!(primary, participants);
        assert_eq!(secondary, participants);
    }

    #[test]
    fn canonical_scenario_count_reports_overflow() {
        let initial = CellState::from_cells(&[4]);
        let mut generator = ScenarioGenerator::new(4);
        assert_eq!(generator.counts(&initial, 40), None);
    }

    #[test]
    fn unique_index_sampling_handles_near_full_ranges() {
        let total = 100_000u128;
        let samples = 99_999usize;
        let mut rng = TestRng::new(9);
        let sampled = sample_unique_indices(&mut rng, total, samples);
        assert_eq!(sampled.len(), samples);
        assert_eq!(
            sampled.iter().copied().collect::<HashSet<_>>().len(),
            samples
        );
        assert!(sampled.into_iter().all(|idx| idx < total));
    }

    #[test]
    fn sustained_cases_repeat_single_round() {
        let framework = Framework {
            participants: 5,
            faults: 1,
            rounds: 3,
            mode: Mode::Sustained,
            max_cases: 50,
        };
        let all = cases(&mut test_rng(), framework);
        assert!(!all.is_empty());
        for case in &all {
            let rounds = case.scenario.rounds();
            assert_eq!(rounds.len(), 3);
            assert_eq!(rounds[0], rounds[1]);
            assert_eq!(rounds[1], rounds[2]);
        }
    }

    #[test]
    #[should_panic(expected = "scenario space overflows u128")]
    fn cases_panic_on_scenario_overflow() {
        let _ = cases(
            &mut test_rng(),
            Framework {
                participants: 4,
                faults: 1,
                rounds: 40,
                mode: Mode::Sampled,
                max_cases: 1,
            },
        );
    }

    #[test]
    fn pruned_scenarios_vary_across_round_positions() {
        let mut generator = ScenarioGenerator::new(5);
        let scenarios = generator.generate(&mut test_rng(), 3, 32);
        for round in 0..3 {
            let unique: HashSet<_> = scenarios
                .iter()
                .map(|(scenario, _)| scenario.rounds[round])
                .collect();
            assert!(
                unique.len() > 1,
                "round index {round} should vary under deterministic pruning"
            );
        }
    }

    #[test]
    fn compromised_sets_collapse_symmetry_cells() {
        // All 3 participants in one cell: only 1 unique compromised set for f=1
        assert_eq!(compromised_sets_for_cells(&[3], 1).len(), 1);
        assert_eq!(compromised_sets_for_cells(&[3], 1), vec![vec![0]]);

        // Cells [2, 1]: two unique compromised sets for f=1
        let sets = compromised_sets_for_cells(&[2, 1], 1);
        assert_eq!(sets, vec![vec![2], vec![0]]);

        // All singletons [1, 1, 1]: 3 unique compromised sets for f=1
        let sets = compromised_sets_for_cells(&[1, 1, 1], 1);
        assert_eq!(sets, vec![vec![2], vec![1], vec![0]]);
    }

    #[test]
    fn compromised_sets_multi_fault() {
        // Cells [3, 2], f=2: allocate across cells
        let sets = compromised_sets_for_cells(&[3, 2], 2);
        // take=0 from cell0, take=2 from cell1 -> [3, 4]
        // take=1 from cell0, take=1 from cell1 -> [0, 3]
        // take=2 from cell0, take=0 from cell1 -> [0, 1]
        assert_eq!(sets, vec![vec![3, 4], vec![0, 3], vec![0, 1]]);
    }

    #[test]
    fn cases_fewer_than_naive_cross_product() {
        let framework = Framework {
            participants: 5,
            faults: 1,
            rounds: 1,
            mode: Mode::Sampled,
            max_cases: usize::MAX,
        };
        let all_cases = cases(&mut test_rng(), framework);
        let mut generator = ScenarioGenerator::new(5);
        let scenarios = generator.generate(&mut test_rng(), 1, usize::MAX);
        // Naive cross-product would be scenarios * C(5,1) = scenarios * 5.
        // Symmetry-aware should produce fewer.
        let naive = scenarios.len() * 5;
        assert!(
            all_cases.len() < naive,
            "got {} cases but naive would be {naive}",
            all_cases.len()
        );
    }

    #[test]
    fn cases_reject_more_than_64_participants() {
        let result = std::panic::catch_unwind(|| {
            cases(
                &mut test_rng(),
                Framework {
                    participants: MAX_PARTICIPANTS + 1,
                    faults: 1,
                    rounds: 1,
                    mode: Mode::Sampled,
                    max_cases: 1,
                },
            )
        });
        assert!(result.is_err());
    }

    #[test]
    fn mask_helpers_set_boundary_ranges() {
        let mut mask = 0u64;
        set_mask_bit(&mut mask, 63);
        assert!(!mask_contains(mask, 62));
        assert!(mask_contains(mask, 63));

        let mut mask = 0u64;
        set_mask_range(&mut mask, 60, 4);
        for idx in 60..=63 {
            assert!(mask_contains(mask, idx), "bit {idx} should be set");
        }
        assert!(!mask_contains(mask, 59));
    }

    #[test]
    fn scenario_masks_route_within_one_word() {
        let mut primary_mask = 0u64;
        set_mask_range(&mut primary_mask, 60, 4);
        let mut secondary_mask = 0u64;
        set_mask_bit(&mut secondary_mask, 61);
        set_mask_bit(&mut secondary_mask, 63);
        let scenario = Scenario {
            rounds: vec![RoundScenario {
                leader: 63,
                primary_mask,
                secondary_mask,
            }],
        };
        let participants: Vec<_> = (0..64).collect();

        let (primary, secondary) = scenario.partitions(View::new(1), &participants);
        assert_eq!(primary, vec![60, 61, 62, 63]);
        assert_eq!(secondary, vec![61, 63]);
        assert_eq!(
            scenario.route(View::new(1), &60, &participants),
            SplitTarget::Primary
        );
        assert_eq!(
            scenario.route(View::new(1), &61, &participants),
            SplitTarget::Both
        );
        assert_eq!(
            scenario.route(View::new(1), &63, &participants),
            SplitTarget::Both
        );
        assert_eq!(
            scenario.route(View::new(1), &0, &participants),
            SplitTarget::None
        );
    }

    #[test]
    fn cases_support_64_participants() {
        let generated = cases(
            &mut test_rng(),
            Framework {
                participants: MAX_PARTICIPANTS,
                faults: 1,
                rounds: 1,
                mode: Mode::Sampled,
                max_cases: 1,
            },
        );
        assert_eq!(generated.len(), 1);
    }

    #[test]
    fn route_returns_none_for_participants_outside_selected_partitions() {
        let scenario = Scenario {
            rounds: vec![round(4, 0, 0b0001, 0b0010)],
        };
        let participants: Vec<u32> = (0..4).collect();
        assert_eq!(
            scenario.route(View::new(1), &2, &participants),
            SplitTarget::None
        );
    }

    #[test]
    #[should_panic(expected = "sender missing from runtime participant list")]
    fn route_panics_when_sender_is_missing_from_participants() {
        let scenario = Scenario {
            rounds: vec![round(4, 0, 0b0001, 0b0010)],
        };
        let participants: Vec<u32> = (0..4).collect();
        let _ = scenario.route(View::new(1), &99, &participants);
    }

    #[test]
    fn twins_elector_uses_scenario_leaders_then_fallback_suffix() {
        let framework = Framework {
            participants: 5,
            faults: 1,
            rounds: 3,
            mode: Mode::Sampled,
            max_cases: 1,
        };
        let case = cases(&mut test_rng(), framework)
            .into_iter()
            .next()
            .expect("expected at least one generated twins case");
        let participants: Vec<_> = (0..framework.participants as u64)
            .map(|seed| PrivateKey::from_seed(seed).public_key())
            .collect();
        let participants = Set::try_from(participants).expect("participants should be unique");
        let twins = <Elector<RoundRobin<Sha256>> as ElectorConfig<ed25519::Scheme>>::build(
            Elector::new(
                RoundRobin::<Sha256>::default(),
                &case.scenario,
                framework.participants,
            ),
            &participants,
        );
        let fallback = <RoundRobin<Sha256> as ElectorConfig<ed25519::Scheme>>::build(
            RoundRobin::<Sha256>::default(),
            &participants,
        );

        for (round_idx, round_scenario) in case.scenario.rounds().iter().enumerate() {
            let round = Round::new(Epoch::new(0), View::new((round_idx as u64) + 1));
            assert_eq!(
                twins.elect(round, None),
                Participant::from_usize(round_scenario.leader()),
                "unexpected leader in scripted attack round"
            );
        }

        for view in (framework.rounds as u64 + 1)..=20 {
            let round = Round::new(Epoch::new(333), View::new(view));
            assert_eq!(twins.elect(round, None), fallback.elect(round, None));
        }
    }
}