p3-air 0.8.0

Defines the Algebraic Intermediate Representation (AIR) interface and supporting utilities for STARK provers.
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
use alloc::format;
use alloc::string::{String, ToString};
use alloc::vec::Vec;
use core::fmt;

use p3_field::{ExtensionField, Field};
use p3_matrix::Matrix;
use p3_matrix::dense::{RowMajorMatrix, RowMajorMatrixView};
use p3_matrix::stack::ViewPair;

use crate::boundary::{self, BoundaryPublic};
use crate::{
    Air, AirBuilder, AirBuilderWithContext, BaseAir, ExtensionBuilder, Name, NamedAirBuilder,
    NamedExtensionBuilder, PermutationAirBuilder, RowWindow,
};

/// A single constraint violation captured during debug evaluation.
///
/// During constraint checking the builder evaluates every AIR constraint
/// on every trace row.
///
/// When a constraint evaluates to a non-zero value, the violation is
/// recorded here **instead of panicking**.
/// This allows the caller to inspect all failures at once and produce
/// comprehensive diagnostic output.
///
/// # Label support
///
/// Constraints asserted with the labeled variant carry a human-readable
/// string identifying them by purpose (e.g. `"range_check"`).
///
/// Labels are zero-cost in production builders because:
/// - The default trait implementation discards them
/// - Only the debug builder overrides it to capture them
/// - `&'static str` avoids any heap allocation
#[derive(Debug, Clone)]
pub struct ConstraintFailure {
    /// Zero-based index of the trace row where the violation occurred.
    pub row: usize,

    /// Zero-based position of the constraint within one evaluation pass.
    ///
    /// The counter increments after every assertion, **pass or fail**.
    /// This gives each constraint a stable index regardless of which
    /// other constraints fail.
    pub constraint: usize,

    /// Human-readable label for this constraint.
    ///
    /// - Set when the constraint was asserted via [`NamedAirBuilder`].
    /// - `None` when the standard unlabeled assertion was used.
    pub label: Option<String>,
}

impl fmt::Display for ConstraintFailure {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "#{}", self.constraint)?;
        if let Some(label) = &self.label {
            write!(f, " {label:?}")?;
        }
        Ok(())
    }
}

/// Summary of all constraint violations across a full trace evaluation.
///
/// Collects **every** violation in a single pass, giving the developer
/// a complete picture of what went wrong.
///
/// # Why
///
/// Wide AIRs can have 50+ constraints per row.
/// Without a full report, debugging becomes a tedious loop:
///
/// 1. Run the checker
/// 2. Fix the one reported failure
/// 3. Re-run, discover the next failure
/// 4. Repeat
///
/// With a full report, **all** failures are visible at once.
///
/// # Memory safety
///
/// Use the `max_failures` parameter in [`check_all_constraints`] to cap
/// the number of recorded violations.
/// Without a cap, a fully-broken trace could allocate unbounded memory.
#[derive(Debug, Clone)]
pub struct ConstraintReport {
    /// Every constraint violation found during the evaluation pass.
    ///
    /// Row-ordered for the AIR's own constraints.
    /// Listed public boundary cells are compared after the row scan and appended last,
    /// so they are not in row order with respect to the rest.
    pub failures: Vec<ConstraintFailure>,

    /// Total number of rows in the evaluated trace.
    pub total_rows: usize,

    /// Number of constraints evaluated per row.
    ///
    /// Captured from the first row's evaluation.
    /// If the AIR dynamically varies its constraint count per row,
    /// this reflects only the first.
    ///
    /// A listed public boundary cell is not one of these.
    /// Its failure carries an index at or past this count, matching where the backend
    /// injects the pin.
    pub total_constraints_per_row: usize,
}

impl ConstraintReport {
    /// Returns `true` when no constraint violations were found.
    pub const fn is_ok(&self) -> bool {
        self.failures.is_empty()
    }
}

/// Debug-mode constraint builder that evaluates an AIR over concrete field
/// values and collects every violation rather than panicking immediately.
///
/// This is the single, canonical builder used by both uni-stark and
/// batch-stark for debug constraint checking.
///
/// The second type parameter defaults to the base field, so callers that
/// do not need extension-field support can simply write
/// `DebugConstraintBuilder<'a, F>`. When permutation or lookup arguments
/// are involved, the full form `DebugConstraintBuilder<'a, F, EF>` is used.
#[derive(Debug)]
pub struct DebugConstraintBuilder<'a, F: Field, EF: ExtensionField<F> = F> {
    /// Zero-based index of the trace row currently under evaluation.
    row_index: usize,

    /// Counter incremented after every constraint assertion, giving each
    /// constraint a stable index within a single evaluation pass.
    constraint_index: usize,

    /// All constraint violations recorded so far for the current row.
    failures: Vec<ConstraintFailure>,

    /// Vertical pair giving access to the current and next witness rows.
    main: ViewPair<'a, F>,

    /// Window over the current and next preprocessed rows.
    /// When the AIR has no preprocessed trace this is a zero-width window.
    preprocessed: RowWindow<'a, F>,

    /// Slice of public values made available to the AIR during evaluation.
    public_values: &'a [F],

    /// Selector that equals one on the first row and zero elsewhere.
    is_first_row: F,

    /// Selector that equals one on the last row and zero elsewhere.
    is_last_row: F,

    /// Selector that equals one on every row except the last.
    is_transition: F,

    /// Vertical pair for the current and next permutation rows, present
    /// only when the AIR uses a permutation argument.
    permutation: Option<ViewPair<'a, EF>>,

    /// Challenge values for the permutation argument, empty when unused.
    permutation_challenges: &'a [EF],

    /// Expected cumulated values for global lookup arguments.
    permutation_values: &'a [EF],

    /// Values of each periodic column at [`Self::row_index`], in column order.
    ///
    /// Empty when the AIR has no periodic columns.
    periodic_row: &'a [F],
}

impl<'a, F: Field> DebugConstraintBuilder<'a, F> {
    /// Build a constraint checker for AIRs that do not use permutations.
    ///
    /// Permutation-related fields are set to `None` / empty so that the
    /// builder can still satisfy trait bounds that require extension-field
    /// support, but calling permutation accessors will panic.
    ///
    /// `periodic_row` must hold the slice returned by `BaseAir::periodic_values` for
    /// the current row (or be empty when the AIR has no periodic columns).
    #[allow(clippy::too_many_arguments)]
    pub fn new(
        row_index: usize,
        main: ViewPair<'a, F>,
        preprocessed: ViewPair<'a, F>,
        public_values: &'a [F],
        is_first_row: F,
        is_last_row: F,
        is_transition: F,
        periodic_row: &'a [F],
    ) -> Self {
        Self {
            row_index,
            constraint_index: 0,
            failures: Vec::new(),
            main,
            preprocessed: RowWindow::from_two_rows(
                preprocessed.top.values,
                preprocessed.bottom.values,
            ),
            public_values,
            is_first_row,
            is_last_row,
            is_transition,
            permutation: None,
            permutation_challenges: &[],
            permutation_values: &[],
            periodic_row,
        }
    }
}

impl<'a, F: Field, EF: ExtensionField<F>> DebugConstraintBuilder<'a, F, EF> {
    /// Build a constraint checker that also carries permutation data.
    ///
    /// Use this when the AIR declares lookup or permutation arguments
    /// that require access to the permutation trace and challenges.
    ///
    /// `periodic_row` must hold the slice returned by `BaseAir::periodic_values` for
    /// the current row (or be empty when the AIR has no periodic columns).
    #[allow(clippy::too_many_arguments)]
    pub fn new_with_permutation(
        row_index: usize,
        main: ViewPair<'a, F>,
        preprocessed: ViewPair<'a, F>,
        public_values: &'a [F],
        is_first_row: F,
        is_last_row: F,
        is_transition: F,
        permutation: ViewPair<'a, EF>,
        permutation_challenges: &'a [EF],
        permutation_values: &'a [EF],
        periodic_row: &'a [F],
    ) -> Self {
        Self {
            row_index,
            constraint_index: 0,
            failures: Vec::new(),
            main,
            preprocessed: RowWindow::from_two_rows(
                preprocessed.top.values,
                preprocessed.bottom.values,
            ),
            public_values,
            is_first_row,
            is_last_row,
            is_transition,
            permutation: Some(permutation),
            permutation_challenges,
            permutation_values,
            periodic_row,
        }
    }

    /// Whether at least one constraint violation has been recorded.
    pub const fn has_failures(&self) -> bool {
        !self.failures.is_empty()
    }

    /// Borrow the list of recorded constraint violations.
    pub fn failures(&self) -> &[ConstraintFailure] {
        &self.failures
    }

    /// Render the recorded violations as a bracketed, comma-separated list.
    ///
    /// Output form: `[#0, #1 "col_1_must_be_zero", #4]`. Every entry starts
    /// with `#`, so mixed labeled and unlabeled entries stay unambiguous.
    pub fn formatted_failures(&self) -> String {
        let entries: Vec<String> = self.failures.iter().map(ToString::to_string).collect();
        format!("[{}]", entries.join(", "))
    }

    /// Consume the builder and return all recorded constraint violations.
    pub fn into_failures(self) -> Vec<ConstraintFailure> {
        self.failures
    }
}

impl<'a, F, EF> AirBuilder for DebugConstraintBuilder<'a, F, EF>
where
    F: Field,
    EF: ExtensionField<F>,
{
    type F = F;
    type Expr = F;
    type Var = F;
    type PreprocessedWindow = RowWindow<'a, F>;
    type MainWindow = RowWindow<'a, F>;
    type PublicVar = F;
    type PeriodicVar = F;

    fn main(&self) -> Self::MainWindow {
        RowWindow::from_two_rows(self.main.top.values, self.main.bottom.values)
    }

    fn preprocessed(&self) -> &Self::PreprocessedWindow {
        &self.preprocessed
    }

    fn is_first_row(&self) -> Self::Expr {
        self.is_first_row
    }

    fn is_last_row(&self) -> Self::Expr {
        self.is_last_row
    }

    fn is_transition(&self) -> Self::Expr {
        self.is_transition
    }

    /// Delegates to the named variant with an empty label.
    fn assert_zero<I: Into<Self::Expr>>(&mut self, x: I) {
        self.assert_zero_named(x, "");
    }

    fn public_values(&self) -> &[Self::PublicVar] {
        self.public_values
    }

    fn periodic_values(&self) -> &[Self::PeriodicVar] {
        self.periodic_row
    }
}

impl<F: Field, EF: ExtensionField<F>> AirBuilderWithContext for DebugConstraintBuilder<'_, F, EF> {
    /// No extra context is needed during debug evaluation.
    type EvalContext = ();

    fn eval_context(&self) -> &Self::EvalContext {
        &()
    }
}

impl<F: Field, EF: ExtensionField<F>> ExtensionBuilder for DebugConstraintBuilder<'_, F, EF> {
    type EF = EF;
    type ExprEF = EF;
    type VarEF = EF;

    /// Delegates to the named variant with an empty label.
    fn assert_zero_ext<I>(&mut self, x: I)
    where
        I: Into<Self::ExprEF>,
    {
        self.assert_zero_ext_named(x, "");
    }
}

impl<'a, F: Field, EF: ExtensionField<F>> PermutationAirBuilder
    for DebugConstraintBuilder<'a, F, EF>
{
    type MP = RowWindow<'a, EF>;
    type RandomVar = EF;
    type PermutationVar = EF;

    /// # Panics
    ///
    /// Panics when the builder was created without permutation data.
    fn permutation(&self) -> Self::MP {
        let p = self.permutation
            .expect("permutation() called on a builder created without permutation data; use new_with_permutation()");
        RowWindow::from_two_rows(p.top.values, p.bottom.values)
    }

    fn permutation_randomness(&self) -> &[Self::RandomVar] {
        self.permutation_challenges
    }

    fn permutation_values(&self) -> &[Self::PermutationVar] {
        self.permutation_values
    }
}

impl<F: Field, EF: ExtensionField<F>> NamedAirBuilder for DebugConstraintBuilder<'_, F, EF> {
    /// Primary constraint implementation for the debug builder.
    ///
    /// Evaluates the name and captures the label on failure.
    /// The unlabeled path delegates here with an empty string.
    fn assert_zero_named<I, N>(&mut self, x: I, name: N)
    where
        I: Into<Self::Expr>,
        N: Name,
    {
        if x.into() != F::ZERO {
            let label = name.evaluate().to_string();
            self.failures.push(ConstraintFailure {
                row: self.row_index,
                constraint: self.constraint_index,
                label: if label.is_empty() { None } else { Some(label) },
            });
        }
        self.constraint_index += 1;
    }
}

impl<F: Field, EF: ExtensionField<F>> NamedExtensionBuilder for DebugConstraintBuilder<'_, F, EF> {
    /// Primary extension-field constraint implementation for the debug
    /// builder.
    ///
    /// Same pattern: unlabeled path delegates here with an empty string.
    fn assert_zero_ext_named<I, N>(&mut self, x: I, name: N)
    where
        I: Into<Self::ExprEF>,
        N: Name,
    {
        if x.into() != EF::ZERO {
            let label = name.evaluate().to_string();
            self.failures.push(ConstraintFailure {
                row: self.row_index,
                constraint: self.constraint_index,
                label: if label.is_empty() { None } else { Some(label) },
            });
        }
        self.constraint_index += 1;
    }
}

/// Find every cell an AIR lists as a public boundary input that does not hold its value.
///
/// A listed cell carries no AIR constraint of its own:
///
/// ```text
///     proving backend : binds the cell to the public value
///     this checker    : compares the two directly
/// ```
///
/// # Returns
///
/// One entry per mismatch, as `(index in declaration order, trace row)`.
///
/// # Panics
///
/// Panics when the declaration names a column or a public value that does not exist.
/// That is a bug in the AIR rather than a failure of the witness.
fn boundary_io_mismatches<F, A>(
    air: &A,
    main: &RowMajorMatrix<F>,
    public_values: &[F],
) -> Vec<(usize, usize)>
where
    F: Field,
    A: BaseAir<F>,
{
    let cells = air.public_boundary_io();

    // Check the declaration against what is actually on hand, not against what the AIR declares.
    boundary::validate(cells, main.width(), public_values.len())
        .unwrap_or_else(|error| panic!("{error}"));

    // Both trace ends are addressed relative to the row count.
    let height = main.height();

    cells
        .iter()
        .enumerate()
        .filter_map(|(index, cell)| {
            let row = cell.row(height);
            let value = main
                .get(row, cell.column)
                .expect("row and column checked in range");
            (value != public_values[cell.public_value]).then_some((index, row))
        })
        .collect()
}

/// Human-readable name of one listed public boundary cell.
fn boundary_io_label(cell: &BoundaryPublic) -> String {
    format!(
        "boundary-IO cell in column {} does not hold public value {}",
        cell.column, cell.public_value
    )
}

/// Reject a periodic column whose shape the trace cannot carry.
///
/// A column of length `p` holds the evaluations of one polynomial over a subgroup of order `p`.
///
/// - Such a subgroup exists only when `p` is a power of two.
/// - It tiles the rows only when `p` divides the height.
///
/// ```text
///     height 8,  length 2   [0,1][0,1][0,1][0,1]       tiles
///     height 8,  length 16  [0,1,...,7|8,...,15]       truncated
///     height 12, length 8   [0,...,7][0,1,2,3|4,...]   partial repeat
/// ```
///
/// The debug run reads periodic values through a wrapping row lookup.
///
/// That lookup accepts a column of any non-zero length, so it never exposes the mistake.
/// The commitment layer does, but only once a proof is being built or verified.
///
/// Screening here turns that late failure into one an AIR author sees on the first debug run.
///
/// This is a diagnostic, not a precondition.
/// The commitment layer screens for itself and does not rely on this.
///
/// # Panics
///
/// Panics when a declared column cannot be laid over the given number of rows.
fn assert_periodic_column_shapes<F: Clone, A: BaseAir<F>>(air: &A, height: usize) {
    for (index, column) in air.periodic_columns().iter().enumerate() {
        // The length is how many values the column lists before repeating.
        let length = column.len();

        // Powers of two are the orders for which a two-adic subgroup exists.
        // Zero fails here, ahead of the row lookup that would divide by it.
        assert!(
            length.is_power_of_two(),
            "debug constraint check rejected the AIR: \
             periodic column {index} has length {length}, which is not a power of two"
        );

        // Divisibility lands every repetition on a whole copy of that subgroup.
        assert!(
            height.is_multiple_of(length),
            "debug constraint check rejected the AIR: \
             periodic column {index} has length {length}, \
             which does not divide the trace height {height}"
        );
    }
}

/// Evaluate every AIR constraint against a concrete trace and panic on failure.
///
/// The function walks the trace row by row. For each row it:
///
/// 1. Builds a vertical pair of the current and next rows (wrapping around
///    at the end).
/// 2. Sets the first-row, last-row and transition selectors.
/// 3. Evaluates the AIR, collecting all violated constraints
///    (index with optional label).
/// 4. Stops at the first row that has at least one violation and panics
///    with a summary of every violated constraint on that row.
///
/// Cells the AIR lists as public inputs are compared before the loop.
/// No AIR constraint asserts them.
///
/// This is the simple variant that does not involve permutation or lookup
/// arguments. Batch-stark provides its own wrapper that additionally
/// supplies permutation data and lookup inputs.
#[allow(unused)] // Suppresses warnings in release mode where this is dead code.
pub fn check_constraints<F, A>(air: &A, main: &RowMajorMatrix<F>, public_values: &[F])
where
    F: Field,
    A: for<'a> Air<DebugConstraintBuilder<'a, F>>,
{
    let height = main.height();
    let preprocessed = air.preprocessed_trace();
    if let Some(prep) = preprocessed.as_ref() {
        assert_eq!(
            prep.height(),
            height,
            "debug constraint check requires preprocessed trace height ({}) to match main trace height ({})",
            prep.height(),
            height
        );
    }
    assert_periodic_column_shapes(air, height);

    // A listed cell has no AIR constraint, so the row loop below would never see it.
    if let Some(&(index, row)) = boundary_io_mismatches(air, main, public_values).first() {
        panic!(
            "row {row}: {}",
            boundary_io_label(&air.public_boundary_io()[index])
        );
    }

    for row_index in 0..height {
        let row_index_next = (row_index + 1) % height;

        // SAFETY: both indices are strictly less than `height`.
        let local = unsafe { main.row_slice_unchecked(row_index) };
        let next = unsafe { main.row_slice_unchecked(row_index_next) };

        // Pair the current and next witness rows into a vertical view.
        let main_pair = ViewPair::new(
            RowMajorMatrixView::new_row(&*local),
            RowMajorMatrixView::new_row(&*next),
        );

        // Pair the preprocessed rows. When the AIR has no preprocessed
        // trace we build a zero-width pair so the builder always has a
        // valid (possibly empty) preprocessed matrix.
        let (prep_local, prep_next) = preprocessed.as_ref().map_or((None, None), |prep| unsafe {
            // SAFETY: same index range as the main trace.
            (
                Some(prep.row_slice_unchecked(row_index)),
                Some(prep.row_slice_unchecked(row_index_next)),
            )
        });
        let preprocessed_pair = match (prep_local.as_ref(), prep_next.as_ref()) {
            (Some(l), Some(n)) => ViewPair::new(
                RowMajorMatrixView::new_row(&**l),
                RowMajorMatrixView::new_row(&**n),
            ),
            _ => ViewPair::new(
                RowMajorMatrixView::new(&[], 0),
                RowMajorMatrixView::new(&[], 0),
            ),
        };

        // Construct the builder with row selectors derived from the position.
        let periodic_row = air.periodic_values(row_index);
        let mut builder = DebugConstraintBuilder::new(
            row_index,
            main_pair,
            preprocessed_pair,
            public_values,
            F::from_bool(row_index == 0),
            F::from_bool(row_index == height - 1),
            F::from_bool(row_index != height - 1),
            &periodic_row,
        );

        // Run every AIR constraint on this row.
        air.eval(&mut builder);

        // Stop at the first failing row and report all violations at once.
        if builder.has_failures() {
            let rendered = builder.formatted_failures();
            panic!(
                "constraints not satisfied on row {row_index}: \
                 failed constraints = {rendered}"
            );
        }
    }
}

/// Evaluate every AIR constraint against a concrete trace and collect
/// **all** violations, returning a [`ConstraintReport`].
///
/// Unlike [`check_constraints`] which panics at the first failing row,
/// this function continues through the **entire** trace.
/// Invaluable for debugging wide AIRs where multiple rows fail
/// independently.
///
/// # Failure cap
///
/// The optional `max_failures` parameter prevents unbounded memory on
/// large traces with many violations.
///
/// The cap is checked **between** rows, so the final count may slightly
/// exceed it (by up to one row's worth of failures).
///
/// # Permutation arguments
///
/// This is the simple variant — no permutation or lookup arguments.
/// Batch-stark provides its own wrapper for those.
///
/// # Public boundary cells
///
/// A cell an AIR lists as a public input is compared after the row scan.
/// Each mismatch is reported at an index past the AIR's own constraints,
/// which is where the proving backend injects its pin.
///
/// These count against `max_failures` like any other violation.
#[allow(unused)] // Suppresses warnings in release mode where this is dead code.
pub fn check_all_constraints<F, A>(
    air: &A,
    main: &RowMajorMatrix<F>,
    public_values: &[F],
    max_failures: Option<usize>,
) -> ConstraintReport
where
    F: Field,
    A: for<'a> Air<DebugConstraintBuilder<'a, F>>,
{
    let height = main.height();
    let preprocessed = air.preprocessed_trace();
    if let Some(prep) = preprocessed.as_ref() {
        assert_eq!(
            prep.height(),
            height,
            "debug constraint check requires preprocessed trace height ({}) to match main trace height ({})",
            prep.height(),
            height
        );
    }
    assert_periodic_column_shapes(air, height);

    // Accumulate violations across all rows.
    let mut all_failures = Vec::new();

    // Capture how many constraints the AIR asserts per row.
    let mut total_constraints_per_row = 0;

    for row_index in 0..height {
        // Early exit when the failure cap is reached.
        if let Some(cap) = max_failures
            && all_failures.len() >= cap
        {
            break;
        }

        // Wrap around to row 0 after the last row.
        let row_index_next = (row_index + 1) % height;

        // SAFETY: both indices are strictly less than `height`.
        let local = unsafe { main.row_slice_unchecked(row_index) };
        let next = unsafe { main.row_slice_unchecked(row_index_next) };

        // Pair the current and next witness rows into a vertical view.
        let main_pair = ViewPair::new(
            RowMajorMatrixView::new_row(&*local),
            RowMajorMatrixView::new_row(&*next),
        );

        // Build the preprocessed pair, falling back to zero-width when
        // the AIR has no preprocessed trace.
        let (prep_local, prep_next) = preprocessed.as_ref().map_or((None, None), |prep| unsafe {
            // SAFETY: same index range as the main trace.
            (
                Some(prep.row_slice_unchecked(row_index)),
                Some(prep.row_slice_unchecked(row_index_next)),
            )
        });
        let preprocessed_pair = match (prep_local.as_ref(), prep_next.as_ref()) {
            (Some(l), Some(n)) => ViewPair::new(
                RowMajorMatrixView::new_row(&**l),
                RowMajorMatrixView::new_row(&**n),
            ),
            _ => ViewPair::new(
                RowMajorMatrixView::new(&[], 0),
                RowMajorMatrixView::new(&[], 0),
            ),
        };

        // Derive the row selectors from the current position.
        let periodic_row = air.periodic_values(row_index);
        let mut builder = DebugConstraintBuilder::new(
            row_index,
            main_pair,
            preprocessed_pair,
            public_values,
            F::from_bool(row_index == 0),
            F::from_bool(row_index == height - 1),
            F::from_bool(row_index != height - 1),
            &periodic_row,
        );

        // Run every AIR constraint on this row.
        air.eval(&mut builder);

        // Record the constraint count from the first row only.
        if row_index == 0 {
            total_constraints_per_row = builder.constraint_index;
        }

        // Collect any violations from this row.
        all_failures.extend(builder.into_failures());
    }

    // A listed cell has no AIR constraint, so it takes an index after the AIR's own.
    // That is where the proving backend injects its pin.
    //
    // The cap bounds these like any other failure, since the row loop may have used it up.
    let cells = air.public_boundary_io();
    let remaining = max_failures.map_or(usize::MAX, |cap| cap.saturating_sub(all_failures.len()));
    all_failures.extend(
        boundary_io_mismatches(air, main, public_values)
            .into_iter()
            .take(remaining)
            .map(|(index, row)| ConstraintFailure {
                row,
                constraint: total_constraints_per_row + index,
                label: Some(boundary_io_label(&cells[index])),
            }),
    );

    ConstraintReport {
        failures: all_failures,
        total_rows: height,
        total_constraints_per_row,
    }
}

#[cfg(test)]
mod tests {
    use alloc::borrow::Cow;
    use alloc::{format, vec};

    use p3_baby_bear::BabyBear;
    use p3_field::PrimeCharacteristicRing;

    use super::*;
    use crate::{BoundaryEnd, BoundaryPublic, WindowAccess};

    /// Minimal AIR for testing transition and boundary constraints.
    ///
    /// Enforces two rules:
    /// - **Transition**: every column increments by one between consecutive rows.
    /// - **Boundary**: the last row must equal the provided public values.
    #[derive(Debug)]
    struct RowLogicAir<const W: usize>;

    impl<F: Field, const W: usize> BaseAir<F> for RowLogicAir<W> {
        fn width(&self) -> usize {
            W
        }
    }

    impl<F: Field, const W: usize> Air<DebugConstraintBuilder<'_, F>> for RowLogicAir<W> {
        fn eval(&self, builder: &mut DebugConstraintBuilder<'_, F>) {
            let main = builder.main();

            // Transition constraint: next == current + 1 for every column.
            for col in 0..W {
                let current = main.current(col).unwrap();
                let next = main.next(col).unwrap();
                builder.when_transition().assert_eq(next, current + F::ONE);
            }

            // Boundary constraint: last row matches public values.
            let public_values = builder.public_values;
            let mut when_last = builder.when(builder.is_last_row);
            for (i, &pv) in public_values.iter().enumerate().take(W) {
                when_last.assert_eq(main.current(i).unwrap(), pv);
            }
        }
    }

    #[test]
    fn test_incremental_rows_with_last_row_check() {
        let air = RowLogicAir::<2>;
        let values = vec![
            BabyBear::ONE,
            BabyBear::ONE, // Row 0
            BabyBear::new(2),
            BabyBear::new(2), // Row 1
            BabyBear::new(3),
            BabyBear::new(3), // Row 2
            BabyBear::new(4),
            BabyBear::new(4), // Row 3 (last)
        ];
        let main = RowMajorMatrix::new(values, 2);
        check_constraints(&air, &main, &[BabyBear::new(4); 2]);
    }

    #[test]
    #[should_panic]
    fn test_incorrect_increment_logic() {
        let air = RowLogicAir::<2>;
        let values = vec![
            BabyBear::ONE,
            BabyBear::ONE, // Row 0
            BabyBear::new(2),
            BabyBear::new(2), // Row 1
            BabyBear::new(5),
            BabyBear::new(5), // Row 2 (wrong)
            BabyBear::new(6),
            BabyBear::new(6), // Row 3
        ];
        let main = RowMajorMatrix::new(values, 2);
        check_constraints(&air, &main, &[BabyBear::new(6); 2]);
    }

    #[test]
    #[should_panic]
    fn test_wrong_last_row_public_value() {
        let air = RowLogicAir::<2>;
        let values = vec![
            BabyBear::ONE,
            BabyBear::ONE, // Row 0
            BabyBear::new(2),
            BabyBear::new(2), // Row 1
            BabyBear::new(3),
            BabyBear::new(3), // Row 2
            BabyBear::new(4),
            BabyBear::new(4), // Row 3
        ];
        let main = RowMajorMatrix::new(values, 2);
        check_constraints(&air, &main, &[BabyBear::new(4), BabyBear::new(5)]);
    }

    #[test]
    fn test_single_row_wraparound_logic() {
        let air = RowLogicAir::<2>;
        let values = vec![
            BabyBear::new(99),
            BabyBear::new(77), // Row 0
        ];
        let main = RowMajorMatrix::new(values, 2);
        check_constraints(&air, &main, &[BabyBear::new(99), BabyBear::new(77)]);
    }

    /// Helper: build a single-row builder with the given main row values
    /// and evaluate an AIR, returning the builder for inspection.
    fn eval_single_row<const W: usize, A>(
        air: &A,
        row: [BabyBear; W],
    ) -> DebugConstraintBuilder<'static, BabyBear>
    where
        A: for<'a> Air<DebugConstraintBuilder<'a, BabyBear>>,
    {
        // Leak the row so we get a 'static borrow (fine in tests).
        let row: &'static [BabyBear] = Vec::from(row).leak();
        let view = RowMajorMatrixView::new_row(row);
        let main = ViewPair::new(view, view);

        let empty_view = RowMajorMatrixView::new(&[], 0);
        let mut builder = DebugConstraintBuilder::new(
            0,
            main,
            ViewPair::new(empty_view, empty_view),
            &[],
            BabyBear::ONE,  // is_first_row
            BabyBear::ONE,  // is_last_row (single row)
            BabyBear::ZERO, // is_transition
            &[],
        );
        air.eval(&mut builder);
        builder
    }

    /// AIR that asserts every column is zero. With W columns this produces
    /// W independent constraints, useful for testing multi-failure collection.
    #[derive(Debug)]
    struct AllZeroAir<const W: usize>;

    impl<F: Field, const W: usize> BaseAir<F> for AllZeroAir<W> {
        fn width(&self) -> usize {
            W
        }
    }

    impl<F: Field, const W: usize> Air<DebugConstraintBuilder<'_, F>> for AllZeroAir<W> {
        fn eval(&self, builder: &mut DebugConstraintBuilder<'_, F>) {
            let main = builder.main();
            for col in 0..W {
                builder.assert_zero(main.current(col).unwrap());
            }
        }
    }

    #[test]
    fn test_no_failures_when_all_constraints_pass() {
        let builder = eval_single_row(&AllZeroAir::<3>, [BabyBear::ZERO; 3]);
        assert!(!builder.has_failures());
        assert!(builder.failures().is_empty());
    }

    #[test]
    fn test_multiple_failures_collected() {
        // Columns 0 and 2 are non-zero, column 1 is zero.
        let builder = eval_single_row(
            &AllZeroAir::<3>,
            [BabyBear::ONE, BabyBear::ZERO, BabyBear::new(42)],
        );

        assert!(builder.has_failures());

        let failures = builder.failures();
        assert_eq!(failures.len(), 2);

        // Constraint 0 (column 0) failed.
        assert_eq!(failures[0].row, 0);
        assert_eq!(failures[0].constraint, 0);

        // Constraint 2 (column 2) failed; constraint 1 passed but still
        // advanced the counter.
        assert_eq!(failures[1].row, 0);
        assert_eq!(failures[1].constraint, 2);
    }

    #[test]
    fn test_into_failures() {
        let builder = eval_single_row(&AllZeroAir::<2>, [BabyBear::ONE, BabyBear::ONE]);

        let failures = builder.into_failures();
        assert_eq!(failures.len(), 2);
        assert_eq!(failures[0].constraint, 0);
        assert_eq!(failures[1].constraint, 1);
    }

    #[test]
    #[should_panic(expected = "failed constraints = [#0, #2]")]
    fn test_panic_message_lists_all_failed_indices() {
        let air = AllZeroAir::<3>;
        let values = vec![BabyBear::ONE, BabyBear::ZERO, BabyBear::new(7)];
        let main = RowMajorMatrix::new(values, 3);
        check_constraints(&air, &main, &[]);
    }

    #[test]
    #[should_panic(expected = "failed constraints = [#0, #1 \"col_1_must_be_zero\"]")]
    fn test_panic_message_includes_label_when_available() {
        let air = NamedConstraintAir;
        let values = vec![BabyBear::ONE, BabyBear::ONE];
        let main = RowMajorMatrix::new(values, 2);
        check_constraints(&air, &main, &[]);
    }

    #[test]
    fn test_check_all_constraints_no_failures() {
        let air = AllZeroAir::<2>;
        let values = BabyBear::zero_vec(4); // 2 rows × 2 cols, all zero
        let main = RowMajorMatrix::new(values, 2);
        let report = check_all_constraints(&air, &main, &[], None);
        assert!(report.is_ok());
        assert_eq!(report.total_rows, 2);
        assert_eq!(report.total_constraints_per_row, 2);
    }

    #[test]
    fn test_check_all_constraints_multiple_rows_fail() {
        let air = AllZeroAir::<2>;
        // Row 0: [1, 0] → constraint 0 fails
        // Row 1: [0, 1] → constraint 1 fails
        let values = vec![BabyBear::ONE, BabyBear::ZERO, BabyBear::ZERO, BabyBear::ONE];
        let main = RowMajorMatrix::new(values, 2);
        let report = check_all_constraints(&air, &main, &[], None);
        assert!(!report.is_ok());
        assert_eq!(report.failures.len(), 2);

        assert_eq!(report.failures[0].row, 0);
        assert_eq!(report.failures[0].constraint, 0);

        assert_eq!(report.failures[1].row, 1);
        assert_eq!(report.failures[1].constraint, 1);
    }

    #[test]
    fn test_check_all_constraints_max_failures_cap() {
        let air = AllZeroAir::<2>;
        // 4 rows, all non-zero → 8 failures total, but cap at 3.
        let values = vec![BabyBear::ONE; 8];
        let main = RowMajorMatrix::new(values, 2);
        let report = check_all_constraints(&air, &main, &[], Some(3));
        // With cap=3, we stop after collecting at least 3 failures.
        // Row 0 produces 2 failures, row 1 produces 2 more → we get 4
        // because the cap is checked *between* rows.
        assert!(report.failures.len() >= 3);
        assert!(report.failures.len() <= 4);
    }

    /// AIR that uses `assert_zero_named` for labeled constraints.
    #[derive(Debug)]
    struct NamedConstraintAir;

    impl<F: Field> BaseAir<F> for NamedConstraintAir {
        fn width(&self) -> usize {
            2
        }
    }

    impl<F: Field> Air<DebugConstraintBuilder<'_, F>> for NamedConstraintAir {
        fn eval(&self, builder: &mut DebugConstraintBuilder<'_, F>) {
            let main = builder.main();
            // Constraint 0: unlabeled
            builder.assert_zero(main.current(0).unwrap());
            // Constraint 1: labeled via NamedAirBuilder
            builder.assert_zero_named(main.current(1).unwrap(), "col_1_must_be_zero");
        }
    }

    #[test]
    fn test_named_constraint_label_captured() {
        let builder = eval_single_row(
            &NamedConstraintAir,
            [BabyBear::ONE, BabyBear::ONE], // both fail
        );
        let failures = builder.failures();
        assert_eq!(failures.len(), 2);

        // First failure: unlabeled.
        assert_eq!(failures[0].constraint, 0);
        assert!(failures[0].label.is_none());

        // Second failure: labeled.
        assert_eq!(failures[1].constraint, 1);
        assert_eq!(failures[1].label.as_deref(), Some("col_1_must_be_zero"));
    }

    #[test]
    fn test_named_constraint_no_label_when_passing() {
        let builder = eval_single_row(
            &NamedConstraintAir,
            [BabyBear::ZERO, BabyBear::ZERO], // both pass
        );
        assert!(!builder.has_failures());
    }

    #[test]
    fn test_named_constraint_in_full_report() {
        let air = NamedConstraintAir;
        // Two rows, both fail on both columns.
        let values = vec![BabyBear::ONE; 4];
        let main = RowMajorMatrix::new(values, 2);
        let report = check_all_constraints(&air, &main, &[], None);
        assert_eq!(report.failures.len(), 4);

        // Row 0, constraint 1 should have the label.
        let labeled: Vec<_> = report
            .failures
            .iter()
            .filter(|f| f.label.is_some())
            .collect();
        assert_eq!(labeled.len(), 2); // one per row
        assert!(
            labeled
                .iter()
                .all(|f| f.label.as_deref() == Some("col_1_must_be_zero"))
        );
    }

    /// AIR that exercises closure-based names and namespace composition.
    #[derive(Debug)]
    struct NamespacedAir;

    impl<F: Field> BaseAir<F> for NamespacedAir {
        fn width(&self) -> usize {
            3
        }
    }

    impl<F: Field> Air<DebugConstraintBuilder<'_, F>> for NamespacedAir {
        fn eval(&self, builder: &mut DebugConstraintBuilder<'_, F>) {
            use crate::NamespaceExt;

            let main = builder.main();
            let ns = "range_check";

            // Static namespace joined with static name.
            builder.assert_zero_named(main.current(0).unwrap(), ns.join("limb_0"));

            // Namespace joined with a closure name.
            let i = 1;
            builder.assert_zero_named(main.current(1).unwrap(), ns.name(|| format!("limb_{i}")));

            // Plain closure name (not a namespace).
            builder.assert_zero_named(main.current(2).unwrap(), || format!("col_{}", 2));
        }
    }

    #[test]
    fn test_namespace_join_labels() {
        let builder = eval_single_row(
            &NamespacedAir,
            [BabyBear::ONE, BabyBear::ONE, BabyBear::ONE],
        );
        let failures = builder.into_failures();
        assert_eq!(failures.len(), 3);

        assert_eq!(failures[0].label.as_deref(), Some("range_check::limb_0"));
        assert_eq!(failures[1].label.as_deref(), Some("range_check::limb_1"));
        assert_eq!(failures[2].label.as_deref(), Some("col_2"));
    }

    /// No-constraint AIR with a configurable preprocessed trace.
    ///
    /// Lets a test force a preprocessed shape independent of the main trace.
    #[derive(Debug)]
    struct ShapeProbeAir {
        /// Rows advertised in the preprocessed trace. `0` reports `None`.
        prep_height: usize,
        /// Columns of the advertised preprocessed trace. Ignored when height is `0`.
        prep_width: usize,
    }

    impl<F: Field> BaseAir<F> for ShapeProbeAir {
        fn width(&self) -> usize {
            // Single column; every fixture is `F::zero_vec(height)`.
            1
        }

        fn preprocessed_trace(&self) -> Option<RowMajorMatrix<F>> {
            // Height == 0 is the sentinel for "AIR has no preprocessed trace".
            if self.prep_height == 0 {
                return None;
            }

            // Row-major flat buffer: height * width zero elements.
            //
            //     layout (prep_height = 2, prep_width = 3):
            //       row 0: [ 0, 0, 0 ]
            //       row 1: [ 0, 0, 0 ]
            //       flat : [ 0, 0, 0, 0, 0, 0 ]
            let total = self.prep_height * self.prep_width;
            Some(RowMajorMatrix::new(F::zero_vec(total), self.prep_width))
        }
    }

    impl<F: Field> Air<DebugConstraintBuilder<'_, F>> for ShapeProbeAir {
        fn eval(&self, _builder: &mut DebugConstraintBuilder<'_, F>) {
            // Empty: every panic must come from a pre-loop guard, not from eval.
        }
    }

    #[test]
    fn test_preprocessed_height_matches_main_passes() {
        // Invariant: matching heights → guard accepts → empty eval loop runs cleanly.
        //
        // Fixture state:
        //   main         : 4 rows × 1 col
        //   preprocessed : 4 rows × 1 col  (advertised by the AIR)
        //
        //     main rows : [0, 1, 2, 3]
        //     prep rows : [0, 1, 2, 3]
        //                 → 4 == 4 → guard passes
        let air = ShapeProbeAir {
            prep_height: 4,
            prep_width: 1,
        };

        // Zero-valued rows; content is irrelevant because no constraint reads it.
        let main = RowMajorMatrix::new(BabyBear::zero_vec(4), 1);

        // Must return cleanly. A panic here would mean the guard rejected a well-shaped input.
        check_constraints(&air, &main, &[]);
    }

    #[test]
    #[should_panic(expected = "preprocessed trace height")]
    fn test_preprocessed_height_mismatch_panics_in_check_constraints() {
        // Invariant: a taller preprocessed trace must trip the guard before
        // any `unsafe` row-indexing on the oversized matrix runs.
        //
        // Fixture state:
        //   main         : 4 rows × 1 col
        //   preprocessed : 8 rows × 1 col  (AIR advertises an oversized shape)
        //
        //     main rows : [0, 1, 2, 3]
        //     prep rows : [0, 1, 2, 3, 4, 5, 6, 7]
        //                 → 8 != 4 → guard panics on entry
        let air = ShapeProbeAir {
            prep_height: 8,
            prep_width: 1,
        };

        // Main deliberately shorter than the advertised preprocessed trace.
        let main = RowMajorMatrix::new(BabyBear::zero_vec(4), 1);

        // Expected: panic before row 0 is ever dereferenced.
        check_constraints(&air, &main, &[]);
    }

    #[test]
    fn test_preprocessed_height_matches_main_passes_in_check_all_constraints() {
        // Invariant: collect-all carries the same guard; matching heights also succeed.
        //
        // Fixture state:
        //   main         : 4 rows × 1 col
        //   preprocessed : 4 rows × 1 col  → empty report returned
        let air = ShapeProbeAir {
            prep_height: 4,
            prep_width: 1,
        };
        let main = RowMajorMatrix::new(BabyBear::zero_vec(4), 1);

        // No failure cap; we expect no failures anyway.
        let report = check_all_constraints(&air, &main, &[], None);

        // Empty AIR → no failures recorded.
        assert!(report.is_ok());

        // Loop still walked every row.
        assert_eq!(report.total_rows, 4);
    }

    /// The one cell the constraint-free AIR below lists as a public input.
    ///
    /// ```text
    ///     column 0, last row, carrying public value 0
    /// ```
    const BOUNDARY_IO_CELLS: [BoundaryPublic; 1] = [BoundaryPublic::new(0, BoundaryEnd::Last, 0)];

    /// AIR that binds its only public input by position, beside one constraint of its own.
    ///
    /// No constraint touches the cell.
    /// Only the boundary check can catch a mismatch.
    ///
    /// The one transition constraint is what gives the injected pin a nonzero index.
    #[derive(Debug)]
    struct BoundaryIoAir;

    impl<F: Field> BaseAir<F> for BoundaryIoAir {
        fn width(&self) -> usize {
            1
        }

        fn num_public_values(&self) -> usize {
            1
        }

        fn public_boundary_io(&self) -> &[BoundaryPublic] {
            &BOUNDARY_IO_CELLS
        }
    }

    impl<F: Field> Air<DebugConstraintBuilder<'_, F>> for BoundaryIoAir {
        fn eval(&self, builder: &mut DebugConstraintBuilder<'_, F>) {
            // The column counts up by one, which every fixture below satisfies.
            // The listed cell is bound by position, not by this or any other constraint.
            let main = builder.main();
            let local = main.current_slice()[0];
            let next = main.next_slice()[0];
            builder.when_transition().assert_eq(next, local + F::ONE);
        }
    }

    #[test]
    fn test_boundary_io_cell_matching_its_public_value_passes() {
        // Invariant: a listed cell holding its public value is accepted.
        //
        // Fixture state: 4 rows, 1 column, last row 7.
        //
        //     rows          : [4, 5, 6, 7]
        //     public values : [7]
        //                     → 7 == 7 → accepted
        let main = RowMajorMatrix::new([4u32, 5, 6, 7].map(BabyBear::new).to_vec(), 1);

        // Returns cleanly.
        // A panic would mean a well-formed fixture was rejected.
        check_constraints(&BoundaryIoAir, &main, &[BabyBear::new(7)]);
    }

    #[test]
    #[should_panic(expected = "does not hold public value")]
    fn test_boundary_io_cell_disagreeing_with_its_public_value_panics() {
        // Invariant: the check fires even though the AIR asserts nothing at all.
        //
        // Mutation: claim a public value the last row does not carry.
        //
        //     rows          : [4, 5, 6, 7]
        //     public values : [8]
        //                     → 7 != 8 → panic
        let main = RowMajorMatrix::new([4u32, 5, 6, 7].map(BabyBear::new).to_vec(), 1);

        // Expected: panic before the row loop starts.
        check_constraints(&BoundaryIoAir, &main, &[BabyBear::new(8)]);
    }

    #[test]
    fn test_boundary_io_mismatch_is_collected_rather_than_panicking() {
        // Invariant: the collecting variant reports a cell mismatch like any other failure.
        //
        // Mutation: claim a public value the last row does not carry.
        //
        //     rows          : [4, 5, 6, 7]
        //     public values : [8]
        //                     → one failure on row 3
        let main = RowMajorMatrix::new([4u32, 5, 6, 7].map(BabyBear::new).to_vec(), 1);

        let report = check_all_constraints(&BoundaryIoAir, &main, &[BabyBear::new(8)], None);

        // The pin takes the index after the AIR's own constraint, which is where the
        // backend injects it. Reporting it at `index` alone would put it at 0.
        assert_eq!(report.total_constraints_per_row, 1);
        assert_eq!(report.failures.len(), 1);
        assert_eq!(report.failures[0].row, 3);
        assert_eq!(report.failures[0].constraint, 1);
        assert!(
            report.failures[0]
                .label
                .as_deref()
                .is_some_and(|label| label.contains("public value 0"))
        );
    }

    #[test]
    fn test_boundary_io_mismatch_counts_against_the_failure_cap() {
        // Invariant: a cell failure is capped like any other, even though it is found
        //   after the row scan rather than inside it.
        //
        // Fixture state: the same wrong claim, with no room left to record it.
        let main = RowMajorMatrix::new([4u32, 5, 6, 7].map(BabyBear::new).to_vec(), 1);

        let report = check_all_constraints(&BoundaryIoAir, &main, &[BabyBear::new(8)], Some(0));

        assert!(report.failures.is_empty());
    }

    #[test]
    #[should_panic(expected = "preprocessed trace height")]
    fn test_preprocessed_height_mismatch_panics_in_check_all_constraints() {
        // Invariant: same mismatch as the single-pass case → collect-all panics the same way.
        //
        // Fixture state:
        //   main         : 4 rows × 1 col
        //   preprocessed : 8 rows × 1 col  → 8 != 4 → guard panics on entry
        let air = ShapeProbeAir {
            prep_height: 8,
            prep_width: 1,
        };
        let main = RowMajorMatrix::new(BabyBear::zero_vec(4), 1);

        // Expected: panic on entry. The would-be report is unreachable → bound to `_`.
        let _ = check_all_constraints(&air, &main, &[], None);
    }

    /// Single column pinned to a periodic column listing `0, 1, ..., period - 1`.
    #[derive(Debug)]
    struct PeriodicCopyAir {
        period: usize,
    }

    impl<F: Field> BaseAir<F> for PeriodicCopyAir {
        fn width(&self) -> usize {
            1
        }

        fn num_periodic_columns(&self) -> usize {
            1
        }

        fn periodic_columns(&self) -> Cow<'_, [Vec<F>]> {
            Cow::Owned(vec![(0..self.period).map(F::from_usize).collect()])
        }
    }

    impl<F: Field> Air<DebugConstraintBuilder<'_, F>> for PeriodicCopyAir {
        fn eval(&self, builder: &mut DebugConstraintBuilder<'_, F>) {
            let main = builder.main();
            let periodic = builder.periodic_values()[0];
            builder.assert_eq(main.current(0).unwrap(), periodic);
        }
    }

    // Trace holding `row mod period`, which is exactly what the AIR reads off the
    // periodic column, so every rejection below can only come from the shape check.
    fn periodic_copy_trace(period: usize, height: usize) -> RowMajorMatrix<BabyBear> {
        RowMajorMatrix::new_col(
            (0..height)
                .map(|i| BabyBear::from_usize(i % period))
                .collect(),
        )
    }

    #[test]
    fn test_periodic_column_with_valid_shape_passes() {
        // Three shapes that tile an 8-row trace, spanning the accepted range:
        //
        //     period 1 : [0][0][0][0][0][0][0][0]   constant column
        //     period 2 : [0,1][0,1][0,1][0,1]       the common case
        //     period 8 : [0,1,2,3,4,5,6,7]          period equal to the height
        for period in [1, 2, 8] {
            let air = PeriodicCopyAir { period };
            let trace = periodic_copy_trace(period, 8);

            check_constraints(&air, &trace, &[]);

            let report = check_all_constraints(&air, &trace, &[], None);
            assert!(report.failures.is_empty(), "period {period} was rejected");
        }
    }

    #[test]
    #[should_panic(expected = "periodic column 0 has length 3, which is not a power of two")]
    fn test_periodic_column_length_not_power_of_two_is_rejected() {
        // Period 3 sits inside the 8 rows but has no subgroup of order 3 to interpolate over.
        let air = PeriodicCopyAir { period: 3 };
        check_constraints(&air, &periodic_copy_trace(3, 8), &[]);
    }

    #[test]
    #[should_panic(expected = "periodic column 0 has length 0, which is not a power of two")]
    fn test_empty_periodic_column_is_rejected() {
        // An empty column would send the row lookup into `row mod 0`.
        // Zero is not a power of two, so the shape check names the column first.
        let air = PeriodicCopyAir { period: 0 };
        let trace = RowMajorMatrix::new_col(BabyBear::zero_vec(8));
        let _ = check_all_constraints(&air, &trace, &[], None);
    }

    #[test]
    #[should_panic(
        expected = "periodic column 0 has length 16, which does not divide the trace height 8"
    )]
    fn test_periodic_column_longer_than_trace_is_rejected() {
        // Only the first 8 of the 16 values are ever read, so the debug run would
        // score a different AIR than the one the prover builds.
        let air = PeriodicCopyAir { period: 16 };
        let _ = check_all_constraints(&air, &periodic_copy_trace(16, 8), &[], None);
    }

    #[test]
    #[should_panic(
        expected = "periodic column 0 has length 8, which does not divide the trace height 12"
    )]
    fn test_periodic_column_not_dividing_trace_is_rejected() {
        // Period 8 is a power of two and fits inside 12 rows, yet 12 = 8 + 4 leaves a
        // partial repetition, so fitting is not the relation that matters.
        let air = PeriodicCopyAir { period: 8 };
        check_constraints(&air, &periodic_copy_trace(8, 12), &[]);
    }
}