p3-binary-pcs 0.8.0

Multilinear polynomial commitment over Plonky3 binary tower fields, via additive-domain proximity
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
use alloc::string::String;
use alloc::{format, vec};

use p3_binary_field::BinaryField128;
use p3_field::PrimeCharacteristicRing;
use p3_keccak::Keccak256Hash;
use p3_matrix::dense::RowMajorMatrix;
use p3_multilinear_util::poly::Poly;
use p3_sumcheck::ring_switch::bits::BitRingSwitchProofError;
use p3_sumcheck::{OpeningBatch, PrescribedPointPcs, TableSpec};
use p3_symmetric::CryptographicHasher;
use rand::rngs::SmallRng;
use rand::{RngExt, SeedableRng};

use super::evaluate::{BLOCKS_PER_TASK, packed_column_sums};
use super::gather::GATHER_GROUP;
use super::plan::{ClaimPlan, ColumnClaim, column_claims};
use super::*;
use crate::params::BinaryPcsParams;
use crate::test_util::{MyChallenger, MyMmcs, challenger, mmcs};

type EF = BinaryField128;

// The reduction error a rejection carries, so a test names the check that refused it.
fn reduction_error<E: core::fmt::Debug>(
    error: BooleanTraceError<EF, E>,
) -> BitRingSwitchProofError {
    match error {
        BooleanTraceCommitmentError::Boolean(BooleanPcsError::ReductionProof(inner)) => inner,
        other => panic!("{other:?}"),
    }
}

/// Columns every fixture table holds.
const FIXTURE_WIDTH: usize = 2;

/// A Boolean table of `2^log_height` rows and two columns.
fn table(seed: u64, log_height: usize) -> Table<EF> {
    let rows = 1usize << log_height;
    let mut rng = SmallRng::seed_from_u64(seed);
    let cells = (0..FIXTURE_WIDTH * rows)
        .map(|_| EF::from_bool(rng.random::<bool>()))
        .collect();
    Table::new(RowMajorMatrix::new(cells, rows))
}

/// A Boolean table with an explicitly chosen width.
fn table_with_width(seed: u64, log_height: usize, width: usize) -> Table<EF> {
    let rows = 1usize << log_height;
    let mut rng = SmallRng::seed_from_u64(seed);
    let cells = (0..width * rows)
        .map(|_| EF::from_bool(rng.random::<bool>()))
        .collect();
    Table::new(RowMajorMatrix::new(cells, rows))
}

fn packed_table(table: &Table<EF>) -> Table<EF> {
    let height = 1usize << table.num_variables();
    let words = (0..height.div_ceil(WORD_BITS))
        .flat_map(|block| {
            (0..table.num_polys()).map(move |column| {
                (0..WORD_BITS).fold(0u64, |word, lane| {
                    let row = block * WORD_BITS + lane;
                    if row < height && table.column(column).value(row) == EF::ONE {
                        word | (1u64 << lane)
                    } else {
                        word
                    }
                })
            })
        })
        .collect();
    Table::from_packed_bits(
        RowMajorMatrix::new(words, table.num_polys()),
        table.num_variables(),
    )
}

#[test]
fn packed_column_sums_match_a_per_bit_sum() {
    // Part of one word, one block, one partial task, and several tasks with a partial last one.
    let blocks_past_tasks = (3 * BLOCKS_PER_TASK + 1) * WORD_BITS;
    for (height, width, seed) in [
        (1, 1, 1),
        (4, 1, 2),
        (32, 5, 3),
        (64, 3, 4),
        (5 * WORD_BITS, 1, 5),
        (blocks_past_tasks, 7, 6),
    ] {
        let mut rng = SmallRng::seed_from_u64(seed);
        let used = if height < WORD_BITS {
            (1u64 << height) - 1
        } else {
            u64::MAX
        };
        let words = (0..height.div_ceil(WORD_BITS) * width)
            .map(|_| rng.random::<u64>() & used)
            .collect::<Vec<_>>();
        let row_weights = (0..height).map(|_| rng.random()).collect::<Vec<EF>>();

        let expected = (0..width)
            .map(|column| {
                (0..height)
                    .filter(|&row| {
                        (words[(row / WORD_BITS) * width + column] >> (row % WORD_BITS)) & 1 == 1
                    })
                    .map(|row| row_weights[row])
                    .sum::<EF>()
            })
            .collect::<Vec<_>>();
        let words = RowMajorMatrix::new(words, width);
        assert_eq!(
            packed_column_sums(&words, &row_weights),
            expected,
            "{height}x{width}"
        );
    }
}

#[test]
fn gathered_bits_place_every_cell_at_its_slot() {
    // Packed and dense, long and short, and packed tables spanning several column groups.
    let shapes = [
        TableShape::new(7, 2 * GATHER_GROUP + 3),
        TableShape::new(6, 5),
        TableShape::new(4, GATHER_GROUP + 1),
        TableShape::new(3, 3),
    ];
    let dense = shapes
        .iter()
        .enumerate()
        .map(|(seed, shape)| table_with_width(seed as u64, shape.num_variables(), shape.width()))
        .collect::<Vec<_>>();
    let tables = alloc::vec![
        packed_table(&dense[0]),
        dense[1].clone(),
        packed_table(&dense[2]),
        dense[3].clone(),
    ];
    let scheme = pcs(&shapes);

    let mut expected = alloc::vec![0u64; 1 << (scheme.num_variables() - 6)];
    for placement in scheme.placements(&shapes).unwrap() {
        let table = &dense[placement.idx()];
        let column_len = 1usize << table.num_variables();
        for (column, selector) in placement.selectors().iter().enumerate() {
            for row in 0..column_len {
                if table.column(column).value(row) == EF::ONE {
                    let index = selector.index() * column_len + row;
                    expected[index / WORD_BITS] |= 1 << (index % WORD_BITS);
                }
            }
        }
    }

    let gathered = scheme.gather_bits(&tables).unwrap();
    let gathered = gathered
        .iter()
        .map(|word| word.to_bits())
        .collect::<Vec<_>>();
    assert_eq!(gathered, expected);
}

#[test]
fn a_gather_refuses_the_first_non_boolean_column_in_placement_order() {
    // The taller table is placed first, so its columns are reached before the short table's.
    let shapes = [TableShape::new(3, 2), TableShape::new(7, 4)];
    let scheme = pcs(&shapes);
    let refused = |bad: &[(usize, usize)]| {
        let tables = shapes
            .iter()
            .enumerate()
            .map(|(index, shape)| {
                let rows = 1 << shape.num_variables();
                let table = table_with_width(index as u64, shape.num_variables(), shape.width());
                let mut cells = table.iter_polys().flatten().copied().collect::<Vec<_>>();
                for &(_, column) in bad.iter().filter(|&&(table, _)| table == index) {
                    cells[column * rows + rows / 2] = EF::GENERATOR;
                }
                Table::new(RowMajorMatrix::new(cells, rows))
            })
            .collect::<Vec<_>>();
        match scheme.gather_bits(&tables) {
            Err(BooleanTraceCommitmentError::NonBooleanCell { table, column }) => (table, column),
            _ => panic!("a non-Boolean cell must be refused"),
        }
    };
    assert_eq!(refused(&[(0, 1), (1, 3)]), (1, 3));
    assert_eq!(refused(&[(1, 3), (1, 1)]), (1, 1));
    assert_eq!(refused(&[(0, 1), (0, 0)]), (0, 0));
}

/// A commitment over the batch these shapes describe.
fn pcs(shapes: &[TableShape]) -> BooleanTracePcs<EF, MyMmcs, MyMmcs> {
    let params = BinaryPcsParams {
        log_inv_rate: 2,
        pow_bits: 0,
        security_level: 40,
    };
    let (arity, _) = plan_stacked_layout(shapes);
    // The packing absorbs seven variables, so the commitment holds the rest.
    let config = BinaryPcsConfig::try_new::<EF, EF>(arity - 7, params).unwrap();
    BooleanTracePcs::new(config, mmcs(), mmcs(), arity).unwrap()
}

/// Every column of every table, opened at one point per table.
fn protocol(shapes: &[TableShape]) -> OpeningProtocol {
    successor_protocol(shapes, &[])
}

/// Every column of every table at the current row, and the `next` columns one row ahead.
fn successor_protocol(shapes: &[TableShape], next: &[usize]) -> OpeningProtocol {
    OpeningProtocol::new(
        shapes
            .iter()
            .map(|shape| {
                TableSpec::new(
                    *shape,
                    alloc::vec![OpeningBatch::new(
                        (0..shape.width()).collect(),
                        next.to_vec()
                    )],
                )
            })
            .collect(),
    )
}

/// One table opened `num_batches` times, every column read at both rows each time.
fn both_views_protocol(shape: TableShape, num_batches: usize) -> OpeningProtocol {
    OpeningProtocol::new(vec![TableSpec::new(
        shape,
        (0..num_batches)
            .map(|_| OpeningBatch::new((0..shape.width()).collect(), (0..shape.width()).collect()))
            .collect(),
    )])
}

/// One complete-batch proof of both views whose value at `tampered` is moved by one.
///
/// The flow is the prover's own, so the moved value is bound before the column point is
/// drawn, exactly as an honest value would be.
fn forged_successor_proof(
    scheme: &BooleanTracePcs<EF, MyMmcs, MyMmcs>,
    data: BooleanTraceData<EF, MyMmcs>,
    shape: TableShape,
    points: &[Point<EF>],
    tampered: Option<usize>,
    challenger: &mut MyChallenger,
) -> BooleanTraceProof<EF, MyMmcs, MyMmcs> {
    type Scheme = BooleanTracePcs<EF, MyMmcs, MyMmcs>;
    let BooleanTraceCommitmentData { inner, tables } = data;
    let width = shape.width();
    let mut values = Vec::new();
    for point in points {
        let (current, successor) = Scheme::evaluate_views(&tables[0], point, true);
        values.extend(current);
        values.extend(successor);
    }
    if let Some(at) = tampered {
        values[at] += EF::ONE;
    }

    let batch_shape = ColumnBatchShape {
        table_variables: shape.num_variables(),
        width,
        num_batches: points.len(),
        next: true,
    };
    let mut transcript = ColumnBatchProverTranscript::new(challenger, batch_shape);
    let mut openings = Vec::with_capacity(points.len());
    for (point, batch_values) in points.iter().zip(values.chunks_exact(2 * width)) {
        let (current, successor) = batch_values.split_at(width);
        let column_point = transcript.batch(point, current, successor);
        let mut lifted_point = column_point;
        lifted_point.extend(point);
        openings.push(BitOpening {
            point: lifted_point,
            row_variables: shape.num_variables(),
            current: true,
            next: true,
        });
    }
    transcript.finish();

    let (_, opening) = scheme
        .inner
        .open_readings(inner, &openings, challenger)
        .unwrap();
    BooleanTraceCommitmentProof { values, opening }
}

/// A column read one row ahead: row `z` reads row `z + 1`, the last row itself.
fn successor_reading(column: &[EF], point: &Point<EF>) -> EF {
    let rows = column.len();
    let eq = Poly::new_from_point(point.as_slice(), EF::ONE);
    (0..rows)
        .map(|row| eq.as_slice()[row] * column[(row + 1).min(rows - 1)])
        .sum()
}

#[test]
fn an_opened_column_is_the_column_the_table_holds() {
    // Invariant: lifting a row point by a slot address reads that column and no other.
    //
    // Fixture state: three tables, so the slots are neither all one arity nor all aligned.
    //
    //     - table 0   2^10 rows, 2 columns
    //     - table 1   2^8  rows, 2 columns
    //     - table 2   2^4  rows, 2 columns
    //
    // The stack is log2_ceil(2048 + 512 + 32) = 12 variables wide.
    //
    // The third table is what puts a column shorter than one staging word in the batch.
    //
    //     - 2^10 and 2^8 rows  ->  whole words, written by the aligned run
    //     - 2^4 rows           ->  sixteen bits inside one word, set in place
    //
    // A wrong shift on that second path would place the column somewhere else.
    //
    // Every opened value is checked against the column's own multilinear.
    // That reference shares nothing with the packing or with the reduction.
    let shapes = [
        TableShape::new(10, FIXTURE_WIDTH),
        TableShape::new(8, FIXTURE_WIDTH),
        TableShape::new(4, FIXTURE_WIDTH),
    ];
    let tables = alloc::vec![table(0xB100, 10), table(0xB101, 8), table(0xB103, 4)];
    let scheme = pcs(&shapes);
    let protocol = protocol(&shapes);
    assert_eq!(scheme.num_variables(), 12);

    let mut rng = SmallRng::seed_from_u64(0xB102);
    let points = alloc::vec![
        Point::<EF>::rand(&mut rng, 10),
        Point::<EF>::rand(&mut rng, 8),
        Point::<EF>::rand(&mut rng, 4),
    ];

    let mut prover_chal = challenger();
    let (commitment, data) = scheme.commit(tables.clone(), &mut prover_chal).unwrap();
    let proof = scheme
        .open_at(data, &protocol, &points, &mut prover_chal)
        .unwrap();

    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let evals = scheme
        .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
        .unwrap();

    // One batch per table, each carrying that table's whole width and no successor value.
    assert_eq!(evals.len(), shapes.len());
    for (index, batch) in evals.iter().enumerate() {
        assert!(batch.next().is_empty());
        for (column, &value) in batch.current().iter().enumerate() {
            let reference = Poly::new(tables[index].poly(column).as_slice().to_vec());
            assert_eq!(
                value,
                reference.eval_base(&points[index]),
                "{index}/{column}"
            );
        }
    }
}

#[test]
fn packed_table_round_trip_matches_dense_openings() {
    for (log_height, width, seed) in [(5, 8, 0xB10A), (10, FIXTURE_WIDTH, 0xB10C)] {
        let shapes = [TableShape::new(log_height, width)];
        let dense = table_with_width(seed, log_height, width);
        let packed = packed_table(&dense);
        let scheme = pcs(&shapes);
        let protocol = protocol(&shapes);
        let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(seed + 1), log_height);

        let mut dense_challenger = challenger();
        let (dense_commitment, dense_data) =
            scheme.commit(vec![dense], &mut dense_challenger).unwrap();
        let dense_proof = scheme
            .open_at(
                dense_data,
                &protocol,
                core::slice::from_ref(&point),
                &mut dense_challenger,
            )
            .unwrap();
        let dense_after_open = p3_challenger::CanSample::<EF>::sample(&mut dense_challenger);
        let mut packed_challenger = challenger();
        let (packed_commitment, packed_data) =
            scheme.commit(vec![packed], &mut packed_challenger).unwrap();
        let packed_proof = scheme
            .open_at(
                packed_data,
                &protocol,
                core::slice::from_ref(&point),
                &mut packed_challenger,
            )
            .unwrap();
        let packed_after_open = p3_challenger::CanSample::<EF>::sample(&mut packed_challenger);

        assert_eq!(dense_commitment, packed_commitment);
        assert_eq!(
            postcard::to_allocvec(&dense_proof).unwrap(),
            postcard::to_allocvec(&packed_proof).unwrap()
        );
        assert_eq!(dense_after_open, packed_after_open);
        let mut verifier = challenger();
        scheme.observe_commitment(&packed_commitment, &mut verifier);
        scheme
            .verify_at(
                &packed_commitment,
                &packed_proof,
                &protocol,
                core::slice::from_ref(&point),
                &mut verifier,
            )
            .unwrap();
    }
}

#[test]
fn packed_mixed_heights_and_widths_match_dense_proof_bytes() {
    let shapes = [
        TableShape::new(6, 3),
        TableShape::new(4, 5),
        TableShape::new(7, 2),
    ];
    let dense = vec![
        table_with_width(0xB10C, 6, 3),
        table_with_width(0xB10D, 4, 5),
        table_with_width(0xB10E, 7, 2),
    ];
    let packed = dense.iter().map(packed_table).collect::<Vec<_>>();
    let scheme = pcs(&shapes);
    let protocol = protocol(&shapes);
    let mut rng = SmallRng::seed_from_u64(0xB10F);
    let points = shapes
        .iter()
        .map(|shape| Point::<EF>::rand(&mut rng, shape.num_variables()))
        .collect::<Vec<_>>();

    let mut dense_challenger = challenger();
    let (dense_commitment, dense_data) = scheme.commit(dense, &mut dense_challenger).unwrap();
    let dense_proof = scheme
        .open_at(dense_data, &protocol, &points, &mut dense_challenger)
        .unwrap();
    let dense_after_open = p3_challenger::CanSample::<EF>::sample(&mut dense_challenger);

    let mut packed_challenger = challenger();
    let (packed_commitment, packed_data) = scheme.commit(packed, &mut packed_challenger).unwrap();
    let packed_proof = scheme
        .open_at(packed_data, &protocol, &points, &mut packed_challenger)
        .unwrap();
    let packed_after_open = p3_challenger::CanSample::<EF>::sample(&mut packed_challenger);

    assert_eq!(dense_commitment, packed_commitment);
    assert_eq!(
        postcard::to_allocvec(&dense_proof).unwrap(),
        postcard::to_allocvec(&packed_proof).unwrap()
    );
    assert_eq!(dense_after_open, packed_after_open);
}

#[test]
fn the_sampled_path_draws_the_same_points_on_both_sides() {
    // Invariant: the sampled convention needs no point to cross the wire.
    // Each side binds the commitment itself.
    //
    //     commit  ->  binds the root, then the caller draws nothing of its own
    //     verify  ->  binds the root, then samples the points the prover sampled
    //
    // A caller that bound the root by hand would bind it twice and sample elsewhere.
    // Both sides therefore run on a fresh sponge, as every other scheme here expects.
    //
    // Fixture state: one table of 2^10 rows and two columns, so the stack has arity 11.
    let shapes = [TableShape::new(10, FIXTURE_WIDTH)];
    let scheme = pcs(&shapes);
    let protocol = protocol(&shapes);

    let mut prover_chal = challenger();
    let (commitment, data) = scheme
        .commit(alloc::vec![table(0xB200, 10)], &mut prover_chal)
        .unwrap();
    let proof = scheme
        .open(data, protocol.clone(), &mut prover_chal)
        .unwrap();

    let mut verifier_chal = challenger();
    scheme
        .verify(&commitment, &proof, &mut verifier_chal, protocol.clone())
        .unwrap();

    // Binding it a second time moves every later draw, so the two sides split.
    let mut double_bound = challenger();
    scheme.observe_commitment(&commitment, &mut double_bound);
    let refused = scheme
        .verify(&commitment, &proof, &mut double_bound, protocol)
        .unwrap_err();
    // The split transcript gives the replayed reduction a different column point.
    assert_eq!(
        reduction_error(refused),
        BitRingSwitchProofError::ClaimMismatch
    );
}

#[test]
fn a_disagreeing_shape_or_point_count_is_refused() {
    // Invariant: the shape agreement is checked before the transcript moves at all.
    let shapes = [TableShape::new(10, FIXTURE_WIDTH)];
    let scheme = pcs(&shapes);

    // Mutation: one column fewer changes the retained table shape.
    let narrow = [TableShape::new(10, 1)];
    let mut chal = challenger();
    let (_, data) = scheme
        .commit(alloc::vec![table(0xB300, 10)], &mut chal)
        .unwrap();
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB301), 10);
    let Err(error) = scheme.open_at(
        data,
        &protocol(&narrow),
        core::slice::from_ref(&point),
        &mut chal,
    ) else {
        panic!("a shape set stacking elsewhere describes another commitment")
    };
    let BooleanTraceCommitmentError::TableShapeMismatch {
        table: table_index,
        expected,
        actual,
    } = error
    else {
        panic!("the retained source shape must be checked before stacking")
    };
    assert_eq!(table_index, 0);
    assert_eq!(expected, TableShape::new(10, 1));
    assert_eq!(actual, TableShape::new(10, FIXTURE_WIDTH));

    // Mutation: no point at all, against a protocol scheduling one batch.
    let (_, data) = scheme
        .commit(alloc::vec![table(0xB300, 10)], &mut chal)
        .unwrap();
    let Err(error) = scheme.open_at(data, &protocol(&shapes), &[], &mut chal) else {
        panic!("a batch with no point is opened nowhere")
    };
    assert!(matches!(
        error,
        BooleanTraceCommitmentError::PointCount {
            expected: 1,
            actual: 0
        }
    ));
}

#[test]
fn a_proof_short_of_one_value_is_refused() {
    // Invariant: the value run must cover every opened column, one value each.
    //
    // Fixture state: one table of two columns, so an honest proof carries two values.
    //
    // Mutation: drop the last value, leaving one against two opened readings.
    let shapes = [TableShape::new(10, FIXTURE_WIDTH)];
    let scheme = pcs(&shapes);
    let protocol = protocol(&shapes);

    let mut prover_chal = challenger();
    let (commitment, data) = scheme
        .commit(alloc::vec![table(0xB400, 10)], &mut prover_chal)
        .unwrap();
    let points = alloc::vec![Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB401), 10)];
    let mut proof = scheme
        .open_at(data, &protocol, &points, &mut prover_chal)
        .unwrap();
    proof.values.pop();

    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let Err(error) = scheme.verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
    else {
        panic!("a proof answering for one column cannot answer for two")
    };
    assert!(matches!(
        error,
        BooleanTraceCommitmentError::ValueCount {
            expected: 2,
            actual: 1
        }
    ));
}

#[test]
fn the_claim_plan_writes_every_value_position_exactly_once() {
    // Invariant: the value run is laid out by the claim plan alone, so no position is
    // answered twice and none is left at the fill the run starts from.
    //
    //     current [0, 1, 2], next []        the whole width at the current row
    //     current [2, 0, 1], next [1, 2]    both sides, out of table order
    //     current [0, 1],    next [1, 0]    both sides, each reordering the other
    //     current [],        next [2, 0]    a successor view read nowhere else
    //     current [3, 1],    next [1, 3, 0] a successor side wider than the current one
    for (width, current, next) in [
        (3, vec![0, 1, 2], vec![]),
        (3, vec![2, 0, 1], vec![1, 2]),
        (2, vec![0, 1], vec![1, 0]),
        (3, vec![], vec![2, 0]),
        (4, vec![3, 1], vec![1, 3, 0]),
    ] {
        let protocol = OpeningProtocol::new(vec![TableSpec::new(
            TableShape::new(8, width),
            vec![OpeningBatch::new(current.clone(), next.clone())],
        )]);
        let len = value_count(&protocol);
        assert_eq!(len, current.len() + next.len());
        assert!(
            ClaimPlan::new(column_claims(&protocol), len).is_some(),
            "{current:?} / {next:?}"
        );
    }

    // Several batches over several tables share one run, so the cursor has to advance
    // past every batch's own two sides before the next batch writes.
    let protocol = OpeningProtocol::new(vec![
        TableSpec::new(
            TableShape::new(8, 3),
            vec![
                OpeningBatch::new(vec![2, 0, 1], vec![1, 2]),
                OpeningBatch::new(vec![0], vec![2, 0]),
            ],
        ),
        TableSpec::new(
            TableShape::new(6, 2),
            vec![OpeningBatch::new(vec![1, 0], vec![0])],
        ),
    ]);
    let len = value_count(&protocol);
    assert_eq!(len, 5 + 3 + 3);
    assert!(ClaimPlan::new(column_claims(&protocol), len).is_some());

    // Both ways a plan can miss are refused, so the checks above are not vacuous.
    //
    //     one position written twice   a claim answering both views at one position
    //     one position never written   no claim at all against a run of one value
    let collided = ColumnClaim {
        table: 0,
        column: 0,
        opening: 0,
        current_at: Some(0),
        next_at: Some(0),
    };
    assert!(ClaimPlan::new(vec![collided], 1).is_none());
    assert!(ClaimPlan::new(vec![], 1).is_none());
}

#[test]
fn a_single_table_batch_uses_one_reduction_for_all_columns() {
    // Invariant: one complete current-row batch for one table is discharged by one
    // column-point ring switch, regardless of the table width.
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![OpeningBatch::new(vec![0, 1, 2], Vec::new())],
    )]);
    let table = table_with_width(0xB500, 8, 3);
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB501), 8);

    let mut prover_chal = challenger();
    let (_, data) = scheme.commit(vec![table], &mut prover_chal).unwrap();
    let proof = scheme
        .open_at(data, &protocol, &[point], &mut prover_chal)
        .unwrap();

    assert_eq!(proof.values.len(), 3);
    assert_eq!(proof.opening.reduction.claims.len(), 1);
}

#[test]
fn each_route_emits_the_reductions_its_plan_counts() {
    // Invariant: the route a protocol resolves to counts exactly the reductions its proof
    // carries, so the security charge and the opening read one resolution.
    //
    //     every column, current row              batched      1 reduction
    //     every column, both rows, two batches   batched      2 reductions
    //     current [2, 0]                         per column   2 reductions
    //     current [0, 1, 2], next [1]            per column   3 reductions
    //     two tables, every column               per column   3 + 2 reductions
    let shape = TableShape::new(8, 3);
    let two_tables = [shape, TableShape::new(6, 2)];
    let reordered = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![OpeningBatch::new(vec![2, 0], Vec::new())],
    )]);
    for (shapes, protocol, batched, reductions, seed) in [
        (vec![shape], protocol(&[shape]), true, 1, 0xB700),
        (vec![shape], both_views_protocol(shape, 2), true, 2, 0xB710),
        (vec![shape], reordered, false, 2, 0xB720),
        (
            vec![shape],
            successor_protocol(&[shape], &[1]),
            false,
            3,
            0xB730,
        ),
        (two_tables.to_vec(), protocol(&two_tables), false, 5, 0xB740),
    ] {
        let route = OpeningRoute::new(&protocol);
        assert_eq!(
            matches!(route, OpeningRoute::Batched(_)),
            batched,
            "{seed:#x}"
        );
        assert_eq!(route.num_reductions(), reductions, "{seed:#x}");

        let scheme = pcs(&shapes);
        let tables = shapes
            .iter()
            .enumerate()
            .map(|(index, shape)| {
                table_with_width(seed + index as u64, shape.num_variables(), shape.width())
            })
            .collect();
        let mut rng = SmallRng::seed_from_u64(seed + 0xF);
        let points = protocol
            .iter_openings()
            .map(|(table, _)| Point::<EF>::rand(&mut rng, shapes[table].num_variables()))
            .collect::<Vec<_>>();

        let mut prover_chal = challenger();
        let (_, data) = scheme.commit(tables, &mut prover_chal).unwrap();
        let proof = scheme
            .open_at(data, &protocol, &points, &mut prover_chal)
            .unwrap();
        assert_eq!(
            proof.opening.reduction.claims.len(),
            route.num_reductions(),
            "{seed:#x}"
        );
    }
}

#[test]
fn batched_values_are_bound_before_the_column_point() {
    // Changing a claimed column value changes the verifier's column point and therefore
    // cannot be repaired by reusing the original one-reduction proof.
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![OpeningBatch::new(vec![0, 1, 2], Vec::new())],
    )]);
    let table = table_with_width(0xB502, 8, 3);
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB503), 8);

    let mut prover_chal = challenger();
    let (commitment, data) = scheme.commit(vec![table], &mut prover_chal).unwrap();
    let mut proof = scheme
        .open_at(
            data,
            &protocol,
            core::slice::from_ref(&point),
            &mut prover_chal,
        )
        .unwrap();
    proof.values[0] += EF::ONE;

    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let refused = scheme
        .verify_at(&commitment, &proof, &protocol, &[point], &mut verifier_chal)
        .unwrap_err();
    assert_eq!(
        reduction_error(refused),
        BitRingSwitchProofError::ClaimMismatch
    );
}

#[test]
fn complete_batches_support_multiple_points_and_width_one() {
    // Width one has no column-point coordinates; two opening batches still use two
    // independent inner reductions and preserve the ordinary value return format.
    let shape = TableShape::new(8, 1);
    let scheme = pcs(&[shape]);
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![
            OpeningBatch::new(vec![0], Vec::new()),
            OpeningBatch::new(vec![0], Vec::new()),
        ],
    )]);
    let table = table_with_width(0xB504, 8, 1);
    let mut rng = SmallRng::seed_from_u64(0xB505);
    let points = vec![Point::<EF>::rand(&mut rng, 8), Point::rand(&mut rng, 8)];

    let mut prover_chal = challenger();
    let (commitment, data) = scheme
        .commit(vec![table.clone()], &mut prover_chal)
        .unwrap();
    let proof = scheme
        .open_at(data, &protocol, &points, &mut prover_chal)
        .unwrap();
    assert_eq!(proof.values.len(), 2);
    assert_eq!(proof.opening.reduction.claims.len(), 2);

    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let evals = scheme
        .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
        .unwrap();
    for (point, eval) in points.iter().zip(evals) {
        let reference = Poly::new(table.poly(0).as_slice().to_vec());
        assert_eq!(eval.current()[0], reference.eval_base(point));
    }
}

#[test]
fn optimized_security_charges_batches_and_column_coordinates() {
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![
            OpeningBatch::new(vec![0, 1, 2], Vec::new()),
            OpeningBatch::new(vec![0, 1, 2], Vec::new()),
        ],
    )]);

    let security = <BooleanTracePcs<EF, MyMmcs, MyMmcs> as PrescribedPointPcs<
        EF,
        MyChallenger,
    >>::prescribed_security(&scheme, &protocol)
    .unwrap();
    let batching = security
        .terms
        .iter()
        .find(|term| term.label == "column-batching")
        .unwrap();
    assert!((batching.bits.bits() - 126.0).abs() < 1e-9);
    let ring_switch = security
        .terms
        .iter()
        .find(|term| term.label == "bit-ring-switch")
        .unwrap();
    assert!(ring_switch.bits.bits().is_finite());
}

#[test]
fn optimized_security_rejects_a_protocol_with_the_wrong_stacked_arity() {
    let committed_shape = TableShape::new(8, 3);
    let scheme = pcs(&[committed_shape]);
    let protocol_shape = TableShape::new(8, 2);
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        protocol_shape,
        vec![OpeningBatch::new(vec![0, 1], Vec::new())],
    )]);

    let security = <BooleanTracePcs<EF, MyMmcs, MyMmcs> as PrescribedPointPcs<
        EF,
        MyChallenger,
    >>::prescribed_security(&scheme, &protocol);
    assert!(security.is_none());
}

#[test]
fn non_power_two_complete_batches_round_trip_below_and_above_packing_width() {
    // Widths three and five exercise zero padding, while heights below and above 128
    // rows cover the short-column-in-word and whole-word packing layouts.
    for (width, log_height, num_batches, seed) in [
        (3, 6, 2, 0xB506),
        (3, 8, 2, 0xB507),
        (5, 6, 1, 0xB508),
        (5, 8, 1, 0xB509),
    ] {
        let shape = TableShape::new(log_height, width);
        let scheme = pcs(&[shape]);
        let protocol = OpeningProtocol::new(vec![TableSpec::new(
            shape,
            (0..num_batches)
                .map(|_| OpeningBatch::new((0..width).collect(), Vec::new()))
                .collect(),
        )]);
        let table = table_with_width(seed, log_height, width);
        let mut rng = SmallRng::seed_from_u64(seed + 1);
        let points = (0..num_batches)
            .map(|_| Point::<EF>::rand(&mut rng, log_height))
            .collect::<Vec<_>>();

        let mut prover_chal = challenger();
        let (commitment, data) = scheme
            .commit(vec![table.clone()], &mut prover_chal)
            .unwrap();
        let proof = scheme
            .open_at(data, &protocol, &points, &mut prover_chal)
            .unwrap();
        assert_eq!(proof.opening.reduction.claims.len(), num_batches);

        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        let evals = scheme
            .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
            .unwrap();
        for (batch, point) in points.iter().enumerate() {
            for column in 0..width {
                let reference = Poly::new(table.poly(column).as_slice().to_vec());
                assert_eq!(evals[batch].current()[column], reference.eval_base(point));
            }
        }
    }
}

#[test]
fn optimized_proofs_reject_each_column_tampering_reduction_tampering_and_point_reordering() {
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![
            OpeningBatch::new(vec![0, 1, 2], Vec::new()),
            OpeningBatch::new(vec![0, 1, 2], Vec::new()),
        ],
    )]);
    let table = table_with_width(0xB50A, 8, 3);
    let mut rng = SmallRng::seed_from_u64(0xB50B);
    let points = vec![Point::<EF>::rand(&mut rng, 8), Point::rand(&mut rng, 8)];
    let mut prover_chal = challenger();
    let (commitment, data) = scheme.commit(vec![table], &mut prover_chal).unwrap();
    let proof = scheme
        .open_at(data, &protocol, &points, &mut prover_chal)
        .unwrap();

    // Untampered control.
    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    scheme
        .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
        .unwrap();

    for index in 0..proof.values.len() {
        let mut tampered = proof.clone();
        tampered.values[index] += EF::ONE;
        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        let refused = scheme
            .verify_at(
                &commitment,
                &tampered,
                &protocol,
                &points,
                &mut verifier_chal,
            )
            .unwrap_err();
        // Every claim's readings are checked against its own element before the shared rounds.
        // So a value of either batch is caught reading its claim off the element.
        assert_eq!(
            reduction_error(refused),
            BitRingSwitchProofError::ClaimMismatch,
            "value {index}"
        );
    }

    let mut tampered = proof.clone();
    tampered.opening.reduction.claims.pop();
    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let refused = scheme
        .verify_at(
            &commitment,
            &tampered,
            &protocol,
            &points,
            &mut verifier_chal,
        )
        .unwrap_err();
    // A dropped reduction is a count disagreement, refused before the transcript moves.
    assert!(
        matches!(
            refused,
            BooleanTraceCommitmentError::Boolean(BooleanPcsError::ClaimCount {
                expected: 2,
                values: 2,
                reductions: 1
            })
        ),
        "{refused:?}"
    );

    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let reordered = vec![points[1].clone(), points[0].clone()];
    let refused = scheme
        .verify_at(
            &commitment,
            &proof,
            &protocol,
            &reordered,
            &mut verifier_chal,
        )
        .unwrap_err();
    // Swapping the points reduces each claim at the other one's point.
    assert_eq!(
        reduction_error(refused),
        BitRingSwitchProofError::ClaimMismatch
    );
}

#[test]
fn optimized_shape_errors_leave_the_prover_transcript_untouched() {
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    // A batch naming the two views differently takes the per-column route, whose own
    // shape checks run before it binds anything either.
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![OpeningBatch::new(vec![0, 1, 2], vec![0])],
    )]);
    let mut prover_chal = challenger();
    let (_, data) = scheme
        .commit(vec![table_with_width(0xB50C, 8, 3)], &mut prover_chal)
        .unwrap();
    let mut expected = prover_chal.clone();
    let short_point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB50D), 7);
    let error = match scheme.open_at(data, &protocol, &[short_point], &mut prover_chal) {
        Ok(_) => panic!("a point with the wrong arity must be rejected"),
        Err(error) => error,
    };
    assert!(matches!(
        error,
        BooleanTraceCommitmentError::PointArity { table: 0, .. }
    ));
    assert_eq!(
        p3_challenger::CanSample::<EF>::sample(&mut prover_chal),
        p3_challenger::CanSample::<EF>::sample(&mut expected),
    );

    let valid_protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![OpeningBatch::new(vec![0, 1, 2], Vec::new())],
    )]);
    let mut prover_chal = challenger();
    let (_, data) = scheme
        .commit(vec![table_with_width(0xB510, 8, 3)], &mut prover_chal)
        .unwrap();
    let mut expected = prover_chal.clone();
    let bad_point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB511), 7);
    let error = match scheme.open_at(data, &valid_protocol, &[bad_point], &mut prover_chal) {
        Ok(_) => panic!("a point with the wrong arity must be rejected"),
        Err(error) => error,
    };
    assert!(matches!(
        error,
        BooleanTraceCommitmentError::PointArity { table: 0, .. }
    ));
    assert_eq!(
        p3_challenger::CanSample::<EF>::sample(&mut prover_chal),
        p3_challenger::CanSample::<EF>::sample(&mut expected),
    );

    let narrow_shape = TableShape::new(8, 2);
    let narrow_protocol = OpeningProtocol::new(vec![TableSpec::new(
        narrow_shape,
        vec![OpeningBatch::new(vec![0, 1], Vec::new())],
    )]);
    let mut prover_chal = challenger();
    let (_, data) = scheme
        .commit(vec![table_with_width(0xB512, 8, 3)], &mut prover_chal)
        .unwrap();
    let mut expected = prover_chal.clone();
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB513), 8);
    let error = match scheme.open_at(data, &narrow_protocol, &[point], &mut prover_chal) {
        Ok(_) => panic!("a protocol with the wrong stacked arity must be rejected"),
        Err(error) => error,
    };
    assert!(matches!(
        error,
        BooleanTraceCommitmentError::TableShapeMismatch { .. }
    ));
    assert_eq!(
        p3_challenger::CanSample::<EF>::sample(&mut prover_chal),
        p3_challenger::CanSample::<EF>::sample(&mut expected),
    );
}

#[test]
fn source_shape_errors_precede_sampling_for_open_and_open_at() {
    for (committed_shapes, protocol_shapes, expected) in [
        (
            vec![TableShape::new(8, 3)],
            vec![TableShape::new(8, 4)],
            "shape",
        ),
        (
            vec![TableShape::new(8, 3)],
            vec![TableShape::new(9, 2)],
            "shape",
        ),
        (
            vec![TableShape::new(8, 2), TableShape::new(8, 2)],
            vec![TableShape::new(8, 4)],
            "count",
        ),
    ] {
        let scheme = pcs(&committed_shapes);
        let tables = committed_shapes
            .iter()
            .enumerate()
            .map(|(index, shape)| {
                table_with_width(0xB520 + index as u64, shape.num_variables(), shape.width())
            })
            .collect();
        let sampled_protocol = protocol(&protocol_shapes);
        let mut sampled_challenger = challenger();
        let (_, data) = scheme.commit(tables, &mut sampled_challenger).unwrap();
        let mut expected_challenger = sampled_challenger.clone();
        let error = match scheme.open(data, sampled_protocol, &mut sampled_challenger) {
            Ok(_) => panic!("a source shape mismatch must be rejected before sampling"),
            Err(error) => error,
        };
        match expected {
            "shape" => assert!(matches!(
                error,
                BooleanTraceCommitmentError::TableShapeMismatch { .. }
            )),
            "count" => assert!(matches!(
                error,
                BooleanTraceCommitmentError::TableCountMismatch { .. }
            )),
            _ => unreachable!(),
        }
        assert_eq!(
            p3_challenger::CanSample::<EF>::sample(&mut sampled_challenger),
            p3_challenger::CanSample::<EF>::sample(&mut expected_challenger),
        );

        let protocol = protocol(&protocol_shapes);
        let points = protocol_shapes
            .iter()
            .map(|shape| {
                Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB530), shape.num_variables())
            })
            .collect::<Vec<_>>();
        let mut challenger = challenger();
        let (_, data) = scheme
            .commit(
                committed_shapes
                    .iter()
                    .enumerate()
                    .map(|(index, shape)| {
                        table_with_width(
                            0xB540 + index as u64,
                            shape.num_variables(),
                            shape.width(),
                        )
                    })
                    .collect(),
                &mut challenger,
            )
            .unwrap();
        let mut expected_challenger = challenger.clone();
        let error = match scheme.open_at(data, &protocol, &points, &mut challenger) {
            Ok(_) => panic!("a source shape mismatch must be rejected before opening"),
            Err(error) => error,
        };
        assert!(matches!(
            (expected, error),
            (
                "shape",
                BooleanTraceCommitmentError::TableShapeMismatch { .. }
            ) | (
                "count",
                BooleanTraceCommitmentError::TableCountMismatch { .. }
            )
        ));
        assert_eq!(
            p3_challenger::CanSample::<EF>::sample(&mut challenger),
            p3_challenger::CanSample::<EF>::sample(&mut expected_challenger),
        );
    }
}

#[test]
fn reordered_subset_batches_use_the_fallback_column_route() {
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let protocol = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![OpeningBatch::new(vec![2, 0], Vec::new())],
    )]);
    let table = table_with_width(0xB50E, 8, 3);
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB50F), 8);

    let mut prover_chal = challenger();
    let (commitment, data) = scheme
        .commit(vec![table.clone()], &mut prover_chal)
        .unwrap();
    let proof = scheme
        .open_at(
            data,
            &protocol,
            core::slice::from_ref(&point),
            &mut prover_chal,
        )
        .unwrap();
    assert_eq!(proof.values.len(), 2);
    assert_eq!(proof.opening.reduction.claims.len(), 2);

    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let evals = scheme
        .verify_at(
            &commitment,
            &proof,
            &protocol,
            core::slice::from_ref(&point),
            &mut verifier_chal,
        )
        .unwrap();
    assert_eq!(
        evals[0].current(),
        &[
            Poly::new(table.poly(2).as_slice().to_vec()).eval_base(&point),
            Poly::new(table.poly(0).as_slice().to_vec()).eval_base(&point),
        ]
    );
}

#[test]
fn complete_successor_batches_round_trip_around_the_packing_width() {
    // Invariant: a batch claiming every column at both rows is one reduction, and each
    // claimed value is that column's own reading at the batch's point.
    //
    // Fixture state: table arities below, at and above the seven one element absorbs.
    //
    //     2^3 rows   the whole successor view sits inside one element
    //     2^7 rows   exactly one element
    //     2^9 rows   the view outruns one element, so the reduction sends carry and last
    //
    // Width one draws no column coordinate, and width three pads the column point.
    //
    // Every value is checked against a reference built from the source table alone.
    for (log_height, width, num_batches, seed) in [
        (3, 32, 2, 0xB600),
        (7, 8, 1, 0xB602),
        (9, 4, 3, 0xB604),
        (9, 1, 2, 0xB606),
        (8, 3, 2, 0xB608),
    ] {
        let shape = TableShape::new(log_height, width);
        let scheme = pcs(&[shape]);
        let protocol = both_views_protocol(shape, num_batches);
        let table = table_with_width(seed, log_height, width);
        let mut rng = SmallRng::seed_from_u64(seed + 1);
        let points = (0..num_batches)
            .map(|_| Point::<EF>::rand(&mut rng, log_height))
            .collect::<Vec<_>>();

        let mut prover_chal = challenger();
        let (commitment, data) = scheme
            .commit(vec![table.clone()], &mut prover_chal)
            .unwrap();
        let proof = scheme
            .open_at(data, &protocol, &points, &mut prover_chal)
            .unwrap();
        assert_eq!(proof.values.len(), 2 * width * num_batches);
        assert_eq!(proof.opening.reduction.claims.len(), num_batches);

        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        let evals = scheme
            .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
            .unwrap();
        for (batch, point) in points.iter().enumerate() {
            for column in 0..width {
                let cells = table.poly(column);
                let reference = Poly::new(cells.as_slice().to_vec());
                assert_eq!(
                    evals[batch].current()[column],
                    reference.eval_base(point),
                    "{batch}/{column}"
                );
                assert_eq!(
                    evals[batch].next()[column],
                    successor_reading(cells.as_slice(), point),
                    "{batch}/{column} next"
                );
            }
        }
    }
}

#[test]
fn packed_successor_tables_match_dense_proof_bytes() {
    // Invariant: a table held as words and the same table held as cells claim the same
    // readings at both rows, so their proofs are one byte run and one sponge state.
    for (log_height, width, seed) in [(5, 8, 0xB60A), (10, FIXTURE_WIDTH, 0xB60C)] {
        let shape = TableShape::new(log_height, width);
        let dense = table_with_width(seed, log_height, width);
        let packed = packed_table(&dense);
        let scheme = pcs(&[shape]);
        let protocol = both_views_protocol(shape, 1);
        let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(seed + 1), log_height);

        let mut dense_challenger = challenger();
        let (dense_commitment, dense_data) =
            scheme.commit(vec![dense], &mut dense_challenger).unwrap();
        let dense_proof = scheme
            .open_at(
                dense_data,
                &protocol,
                core::slice::from_ref(&point),
                &mut dense_challenger,
            )
            .unwrap();
        let dense_after_open = p3_challenger::CanSample::<EF>::sample(&mut dense_challenger);

        let mut packed_challenger = challenger();
        let (packed_commitment, packed_data) =
            scheme.commit(vec![packed], &mut packed_challenger).unwrap();
        let packed_proof = scheme
            .open_at(
                packed_data,
                &protocol,
                core::slice::from_ref(&point),
                &mut packed_challenger,
            )
            .unwrap();
        let packed_after_open = p3_challenger::CanSample::<EF>::sample(&mut packed_challenger);

        assert_eq!(dense_commitment, packed_commitment);
        assert_eq!(
            postcard::to_allocvec(&dense_proof).unwrap(),
            postcard::to_allocvec(&packed_proof).unwrap()
        );
        assert_eq!(dense_after_open, packed_after_open);

        let mut verifier = challenger();
        scheme.observe_commitment(&packed_commitment, &mut verifier);
        scheme
            .verify_at(
                &packed_commitment,
                &packed_proof,
                &protocol,
                core::slice::from_ref(&point),
                &mut verifier,
            )
            .unwrap();
    }
}

#[test]
fn partial_successor_views_use_the_fallback_column_route() {
    // Invariant: a batch naming the two views differently is answered column by column,
    // one claim per column either view names, a column in both sharing one claim.
    //
    //     current [0, 1, 2], next [1]   a subset read one row ahead
    //     current [0],       next [1]   a column read one row ahead and nowhere else
    //     current [0, 1],    next [1]   a column in both sets
    //     current [2, 0],    next [0]   columns out of their table order
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let table = table_with_width(0xB60E, 8, 3);
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB60F), 8);

    for (current, next, claims) in [
        (vec![0, 1, 2], vec![1], 3),
        (vec![0], vec![1], 2),
        (vec![0, 1], vec![1], 2),
        (vec![2, 0], vec![0], 2),
    ] {
        let protocol = OpeningProtocol::new(vec![TableSpec::new(
            shape,
            vec![OpeningBatch::new(current.clone(), next.clone())],
        )]);

        let mut prover_chal = challenger();
        let (commitment, data) = scheme
            .commit(vec![table.clone()], &mut prover_chal)
            .unwrap();
        let proof = scheme
            .open_at(
                data,
                &protocol,
                core::slice::from_ref(&point),
                &mut prover_chal,
            )
            .unwrap();
        assert_eq!(proof.values.len(), current.len() + next.len());
        assert_eq!(proof.opening.reduction.claims.len(), claims);

        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        let evals = scheme
            .verify_at(
                &commitment,
                &proof,
                &protocol,
                core::slice::from_ref(&point),
                &mut verifier_chal,
            )
            .unwrap();

        for (&column, &value) in current.iter().zip(evals[0].current()) {
            let reference = Poly::new(table.poly(column).as_slice().to_vec());
            assert_eq!(value, reference.eval_base(&point), "current {column}");
        }
        for (&column, &value) in next.iter().zip(evals[0].next()) {
            let cells = table.poly(column);
            assert_eq!(
                value,
                successor_reading(cells.as_slice(), &point),
                "next {column}"
            );
        }
    }
}

#[test]
fn reordered_successor_views_keep_each_side_in_its_own_order() {
    // Invariant: the two sides of a batch are laid out independently, so a column named
    // by both lands at the position its own side gives it.
    //
    //     current [0, 1],    next [1, 0]   each side reverses the other
    //     current [2, 0, 1], next [1, 2]   three columns, neither side in table order
    //
    // Both shapes take the per-column route, one claim per column either side names.
    //
    // Every returned value is checked against a reference built from the table alone.
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let table = table_with_width(0xB618, 8, 3);
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB619), 8);

    for (current, next, claims) in [(vec![0, 1], vec![1, 0], 2), (vec![2, 0, 1], vec![1, 2], 3)] {
        let protocol = OpeningProtocol::new(vec![TableSpec::new(
            shape,
            vec![OpeningBatch::new(current.clone(), next.clone())],
        )]);

        let mut prover_chal = challenger();
        let (commitment, data) = scheme
            .commit(vec![table.clone()], &mut prover_chal)
            .unwrap();
        let proof = scheme
            .open_at(
                data,
                &protocol,
                core::slice::from_ref(&point),
                &mut prover_chal,
            )
            .unwrap();
        assert_eq!(proof.values.len(), current.len() + next.len());
        assert_eq!(proof.opening.reduction.claims.len(), claims);

        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        let evals = scheme
            .verify_at(
                &commitment,
                &proof,
                &protocol,
                core::slice::from_ref(&point),
                &mut verifier_chal,
            )
            .unwrap();

        let current_reference = current
            .iter()
            .map(|&column| Poly::new(table.poly(column).as_slice().to_vec()).eval_base(&point))
            .collect::<Vec<_>>();
        let next_reference = next
            .iter()
            .map(|&column| successor_reading(table.poly(column).as_slice(), &point))
            .collect::<Vec<_>>();
        assert_eq!(evals[0].current(), current_reference, "current {current:?}");
        assert_eq!(evals[0].next(), next_reference, "next {next:?}");
    }
}

#[test]
fn a_second_table_is_read_one_row_ahead_at_its_own_point() {
    // Invariant: two tables in one commitment each read their own rows, so the successor
    // view of one never steps into the slot of the other.
    //
    // Fixture state: 2^10 and 2^8 rows of two columns each, stacking to arity 12.
    let shapes = [
        TableShape::new(10, FIXTURE_WIDTH),
        TableShape::new(8, FIXTURE_WIDTH),
    ];
    let tables = vec![table(0xB610, 10), table(0xB611, 8)];
    let scheme = pcs(&shapes);
    let protocol = successor_protocol(&shapes, &[1]);
    let mut rng = SmallRng::seed_from_u64(0xB612);
    let points = vec![
        Point::<EF>::rand(&mut rng, 10),
        Point::<EF>::rand(&mut rng, 8),
    ];

    let mut prover_chal = challenger();
    let (commitment, data) = scheme.commit(tables.clone(), &mut prover_chal).unwrap();
    let proof = scheme
        .open_at(data, &protocol, &points, &mut prover_chal)
        .unwrap();

    let mut verifier_chal = challenger();
    scheme.observe_commitment(&commitment, &mut verifier_chal);
    let evals = scheme
        .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
        .unwrap();

    for (index, batch) in evals.iter().enumerate() {
        for (column, &value) in batch.current().iter().enumerate() {
            let reference = Poly::new(tables[index].poly(column).as_slice().to_vec());
            assert_eq!(
                value,
                reference.eval_base(&points[index]),
                "{index}/{column}"
            );
        }
        let cells = tables[index].poly(1);
        assert_eq!(
            batch.next(),
            &[successor_reading(cells.as_slice(), &points[index])],
            "{index} next"
        );
    }
}

#[test]
fn a_tampered_successor_value_is_rejected_on_either_route() {
    // Invariant: every value of the run is bound before the challenges that combine it,
    // so moving any one of them, on either route, breaks the proof.
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let table = table_with_width(0xB614, 8, 3);
    let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB615), 8);

    // The first protocol combines both views of every column, the second names them apart.
    for (per_column, protocol) in [
        (false, both_views_protocol(shape, 1)),
        (
            true,
            OpeningProtocol::new(vec![TableSpec::new(
                shape,
                vec![OpeningBatch::new(vec![0, 1, 2], vec![1])],
            )]),
        ),
    ] {
        let mut prover_chal = challenger();
        let (commitment, data) = scheme
            .commit(vec![table.clone()], &mut prover_chal)
            .unwrap();
        let proof = scheme
            .open_at(
                data,
                &protocol,
                core::slice::from_ref(&point),
                &mut prover_chal,
            )
            .unwrap();

        // Untampered control.
        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        scheme
            .verify_at(
                &commitment,
                &proof,
                &protocol,
                core::slice::from_ref(&point),
                &mut verifier_chal,
            )
            .unwrap();

        for index in 0..proof.values.len() {
            let mut tampered = proof.clone();
            tampered.values[index] += EF::ONE;
            let mut verifier_chal = challenger();
            scheme.observe_commitment(&commitment, &mut verifier_chal);
            let refused = scheme
                .verify_at(
                    &commitment,
                    &tampered,
                    &protocol,
                    core::slice::from_ref(&point),
                    &mut verifier_chal,
                )
                .unwrap_err();
            // Combining feeds every value into one column point the current claim answers.
            // Named apart, the lone next value is the one the successor claim answers.
            let expected = if per_column && index >= shape.width() {
                BitRingSwitchProofError::SuccessorClaimMismatch
            } else {
                BitRingSwitchProofError::ClaimMismatch
            };
            assert_eq!(reduction_error(refused), expected, "value {index}");
        }
    }
}

#[test]
fn successor_security_charges_both_views_of_every_batch() {
    // Invariant: the two combined claims of a batch share one column point, so the
    // batching term charges each of them.
    //
    //     current only   two batches over two coordinates   ->  128 - log2(4) = 126
    //     both views     four claims over two coordinates   ->  128 - log2(8) = 125
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let security = <BooleanTracePcs<EF, MyMmcs, MyMmcs> as PrescribedPointPcs<
        EF,
        MyChallenger,
    >>::prescribed_security(&scheme, &both_views_protocol(shape, 2))
    .expect("a successor view is answered, so it is priced");

    let bits = |label: &str| {
        security
            .terms
            .iter()
            .find(|term| term.label == label)
            .unwrap_or_else(|| panic!("missing {label}"))
            .bits
            .bits()
    };
    assert!((bits("column-batching") - 125.0).abs() < 1e-9);
    assert!(bits("bit-ring-switch").is_finite());

    // The per-column route prices a successor protocol too, with no batching term.
    let fallback = OpeningProtocol::new(vec![TableSpec::new(
        shape,
        vec![OpeningBatch::new(vec![0, 1, 2], vec![1])],
    )]);
    let security = <BooleanTracePcs<EF, MyMmcs, MyMmcs> as PrescribedPointPcs<
        EF,
        MyChallenger,
    >>::prescribed_security(&scheme, &fallback)
    .expect("a per-column successor protocol is priced");
    assert!(
        security
            .terms
            .iter()
            .all(|term| term.label != "column-batching")
    );
}

/// Keccak-256 hex digest of a value's postcard encoding.
fn pinned_digest<T: Serialize>(value: &T) -> String {
    let bytes = postcard::to_allocvec(value).expect("postcard serialization must not fail");
    Keccak256Hash
        .hash_iter(bytes)
        .iter()
        .map(|b| format!("{b:02x}"))
        .collect()
}

/// The bits of the labelled term of a `prescribed_security` report, or a panic naming
/// the label that is missing.
fn term_bits(security: &PrescribedOpeningSecurity, label: &str) -> f64 {
    security
        .terms
        .iter()
        .find(|term| term.label == label)
        .unwrap_or_else(|| panic!("missing {label}"))
        .bits
        .bits()
}

/// Pins the opening proof bytes, the shared post-open/post-verify challenger state, and
/// the `prescribed_security` report, for all three opening routes: the batched route
/// reading both views, the batched route sampled through the transcript, and the
/// per-column route over several tables with a subset, a reordering and a partial
/// successor batch.
#[test]
fn opening_routes_and_their_security_are_pinned() {
    // (a) Batched, both views, prescribed points.
    {
        let shape = TableShape::new(8, 3);
        let scheme = pcs(&[shape]);
        let protocol = both_views_protocol(shape, 2);
        let table = table_with_width(0xB900, 8, 3);
        let mut rng = SmallRng::seed_from_u64(0xB901);
        let points = vec![Point::<EF>::rand(&mut rng, 8), Point::rand(&mut rng, 8)];

        let mut prover_chal = challenger();
        let (commitment, data) = scheme.commit(vec![table], &mut prover_chal).unwrap();
        let proof = scheme
            .open_at(data, &protocol, &points, &mut prover_chal)
            .unwrap();
        let after_open: EF = prover_chal.sample_algebra_element();

        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        scheme
            .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
            .unwrap();
        let after_verify: EF = verifier_chal.sample_algebra_element();
        assert_eq!(after_open, after_verify);
        assert_eq!(
            pinned_digest(&(&proof, after_open)),
            "95bb155239e73df6f128fb1eac4a8971107886a49fb194ef27c2ba01bc35962e"
        );

        let security = <BooleanTracePcs<EF, MyMmcs, MyMmcs> as PrescribedPointPcs<
            EF,
            MyChallenger,
        >>::prescribed_security(&scheme, &protocol)
        .unwrap();
        assert_eq!(security.terms.len(), 4);
        assert!((term_bits(&security, "column-batching") - 125.0).abs() < 1e-9);
        assert!((term_bits(&security, "binary-pcs-opening") - 121.97763218697155).abs() < 1e-9);
        assert!((term_bits(&security, "bit-ring-switch") - 124.09310940439148).abs() < 1e-9);
        assert!((term_bits(&security, "bit-ring-switch-claim-batching") - 128.0).abs() < 1e-9);
        assert!((security.log2_max_candidates - 0.0).abs() < 1e-9);
    }

    // (b) Batched, current only, sampled path.
    {
        let shapes = [TableShape::new(10, 2)];
        let scheme = pcs(&shapes);
        let protocol = protocol(&shapes);
        let table = table_with_width(0xB910, 10, 2);

        let mut prover_chal = challenger();
        let (commitment, data) = scheme.commit(vec![table], &mut prover_chal).unwrap();
        let proof = scheme
            .open(data, protocol.clone(), &mut prover_chal)
            .unwrap();
        let after_open: EF = prover_chal.sample_algebra_element();

        let mut verifier_chal = challenger();
        scheme
            .verify(&commitment, &proof, &mut verifier_chal, protocol.clone())
            .unwrap();
        let after_verify: EF = verifier_chal.sample_algebra_element();
        assert_eq!(after_open, after_verify);
        assert_eq!(
            pinned_digest(&(&proof, after_open)),
            "f53d7dbf97b73103bc07fc16aa2051e00e51b1edccabe4aea741275bcfb58394"
        );

        let security = <BooleanTracePcs<EF, MyMmcs, MyMmcs> as PrescribedPointPcs<
            EF,
            MyChallenger,
        >>::prescribed_security(&scheme, &protocol)
        .unwrap();
        assert_eq!(security.terms.len(), 3);
        assert!((term_bits(&security, "column-batching") - 128.0).abs() < 1e-9);
        assert!((term_bits(&security, "binary-pcs-opening") - 120.95560588064154).abs() < 1e-9);
        assert!((term_bits(&security, "bit-ring-switch") - 124.09310940439148).abs() < 1e-9);
        assert!((security.log2_max_candidates - 0.0).abs() < 1e-9);
    }

    // (c) Per-column: three tables, a reordered subset, a complete batch and a partial
    // successor batch.
    {
        let shapes = [
            TableShape::new(6, 3),
            TableShape::new(4, 5),
            TableShape::new(7, 2),
        ];
        let scheme = pcs(&shapes);
        let protocol = OpeningProtocol::new(vec![
            TableSpec::new(shapes[0], vec![OpeningBatch::new(vec![2, 0], vec![1])]),
            TableSpec::new(
                shapes[1],
                vec![OpeningBatch::new(
                    (0..shapes[1].width()).collect(),
                    Vec::new(),
                )],
            ),
            TableSpec::new(
                shapes[2],
                vec![
                    OpeningBatch::new(vec![1], vec![0, 1]),
                    OpeningBatch::new(Vec::new(), vec![1]),
                ],
            ),
        ]);
        let tables = vec![
            table_with_width(0xB920, 6, 3),
            table_with_width(0xB921, 4, 5),
            table_with_width(0xB922, 7, 2),
        ];
        let mut rng = SmallRng::seed_from_u64(0xB923);
        let points = vec![
            Point::<EF>::rand(&mut rng, 6),
            Point::<EF>::rand(&mut rng, 4),
            Point::<EF>::rand(&mut rng, 7),
            Point::<EF>::rand(&mut rng, 7),
        ];

        let mut prover_chal = challenger();
        let (commitment, data) = scheme.commit(tables, &mut prover_chal).unwrap();
        let proof = scheme
            .open_at(data, &protocol, &points, &mut prover_chal)
            .unwrap();
        let after_open: EF = prover_chal.sample_algebra_element();

        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        scheme
            .verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
            .unwrap();
        let after_verify: EF = verifier_chal.sample_algebra_element();
        assert_eq!(after_open, after_verify);
        assert_eq!(
            pinned_digest(&(&proof, after_open)),
            "7bff6b57739b0e69f24e7ece7dc4ced96212c9a1f0503262547f8d309a9484b5"
        );

        let security = <BooleanTracePcs<EF, MyMmcs, MyMmcs> as PrescribedPointPcs<
            EF,
            MyChallenger,
        >>::prescribed_security(&scheme, &protocol)
        .unwrap();
        assert_eq!(security.terms.len(), 3);
        assert!((term_bits(&security, "binary-pcs-opening") - 121.97763218697155).abs() < 1e-9);
        assert!((term_bits(&security, "bit-ring-switch") - 124.2995602818589).abs() < 1e-9);
        assert!(
            (term_bits(&security, "bit-ring-switch-claim-batching") - 124.67807190511263).abs()
                < 1e-9
        );
        assert!((security.log2_max_candidates - 0.0).abs() < 1e-9);
    }
}

#[test]
fn a_forged_next_value_is_rejected_at_every_position() {
    // Invariant: every next value is combined into the claim the reduction answers, so
    // a prover that binds a moved one and then opens honestly is still refused.
    //
    // Moving a value in a finished proof cannot show this. The column point moves with
    // it, so such a rejection would not say whether the combination reads that position
    // at all. This flow binds the moved value first, exactly as an honest run does.
    let shape = TableShape::new(8, 3);
    let scheme = pcs(&[shape]);
    let protocol = both_views_protocol(shape, 2);
    let mut rng = SmallRng::seed_from_u64(0xB617);
    let points = vec![Point::<EF>::rand(&mut rng, 8), Point::rand(&mut rng, 8)];

    let forge = |tampered: Option<usize>| {
        let mut prover_chal = challenger();
        let (commitment, data) = scheme
            .commit(vec![table_with_width(0xB616, 8, 3)], &mut prover_chal)
            .unwrap();
        let proof =
            forged_successor_proof(&scheme, data, shape, &points, tampered, &mut prover_chal);
        let mut verifier_chal = challenger();
        scheme.observe_commitment(&commitment, &mut verifier_chal);
        scheme.verify_at(&commitment, &proof, &protocol, &points, &mut verifier_chal)
    };

    // Untampered control: this flow is the one the prover plays, so it verifies.
    forge(None).unwrap();

    // Every next value of every batch, which trails that batch's current ones.
    for batch in 0..points.len() {
        for column in 0..shape.width() {
            let at = batch * 2 * shape.width() + shape.width() + column;
            assert_eq!(
                reduction_error(forge(Some(at)).unwrap_err()),
                BitRingSwitchProofError::SuccessorClaimMismatch,
                "next value {at}"
            );
        }
    }
}