p3-sumcheck 0.8.0

Sumcheck protocol engine (product polynomials, SVO, HVZK masks)
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
//! Several bit-alphabet claims about one packing, reduced by one sumcheck.
//!
//! One reduction per claim leaves one surviving claim per point:
//!
//! ```text
//!     k claims  ->  k sumchecks of l' rounds  ->  k points the commitment opens
//! ```
//!
//! Every claim's rounds prove a sum of the same shape against the same packing:
//!
//! ```text
//!     sum_w t'(w) * A_i(w) = sigma_i
//! ```
//!
//! So a random combination of the sums is one sum of the same shape:
//!
//! ```text
//!     sum_w t'(w) * ( sum_i lambda^i * A_i(w) ) = sum_i lambda^i * sigma_i
//! ```
//!
//! One sumcheck proves it, and leaves one point the commitment opens:
//!
//! ```text
//!     k claims  ->  one sumcheck of l' rounds  ->  one point
//! ```
//!
//! # What each claim still sends
//!
//! Its tensor element, and its successor elements when it has them.
//! Each claim's readings are still checked against its own element's columns.
//!
//! Only the rounds and the surviving claim are shared.
//!
//! # The order the protocol fixes
//!
//! ```text
//!     bind      every claim's point, then every claim's elements
//!     draw      r'' once, [alpha once,] lambda
//!     prove     one sumcheck against sum_i lambda^i * A_i
//! ```
//!
//! Every draw comes after every element is bound.
//! A draw taken between two claims would let the later element be chosen to suit it.
//!
//! # Soundness
//!
//! Write `D_i(u)` for the row error of claim `i`'s elements, already folded under `alpha`.
//! A wrong element leaves the batched starting sum off by
//!
//! ```text
//!     sum_i lambda^i * sum_u eq(r'', u) * D_i(u)
//! ```
//!
//! That is a nonzero polynomial in `(r'', alpha, lambda)` of total degree at most
//!
//! ```text
//!     d_log  +  (K - 1)  +  (k - 1)
//! ```
//!
//! The rounds then add `2 l'` once, not once per claim:
//!
//! ```text
//!     batched      (d_log + K - 1 + (k - 1) + 2 l') / |EF|
//!     per claim    k * (d_log + K - 1 + 2 l') / |EF|
//! ```
//!
//! The added source is the `(k - 1) / |EF|` of `lambda` (Schwartz-Zippel).
//! The commitment then pays for one opened point rather than `k`.
//!
//! Every term is per-attempt, as for the one-claim reduction: nothing here grinds.
//!
//! # A batch of one
//!
//! A single claim plays the one-claim reduction, transcript and all.
//! Its batch draws no `lambda` and its proof carries the same messages.

use alloc::vec::Vec;
use core::borrow::Borrow;

use p3_binary_field::TowerLevel;
use p3_challenger::fs::TranscriptField;
use p3_challenger::{FieldChallenger, GrindingChallenger};
use p3_field::Field;
use p3_maybe_rayon::prelude::*;
use p3_multilinear_util::point::Point;
use p3_multilinear_util::poly::Poly;
use serde::{Deserialize, Serialize};

use super::packing::BitPacking;
use super::reduction::{
    BitRingSwitch, BitRingSwitchBatch, BitRingSwitchError, BitRingSwitchProof,
    BitRingSwitchProofError, SuccessorTensors, check_no_grinding, slot_packing,
};
use super::tensor::BitTensor;
use super::transcript::{
    BitRingSwitchClaimsProverTranscript, BitRingSwitchClaimsShape,
    BitRingSwitchClaimsVerifierTranscript, ClaimStatement, ClaimsDraws,
};
use crate::data::SumcheckData;
use crate::strategy::{Basis, IntoTranscriptField, ReprSumcheckProver, VariableOrder};

/// Several claims about one packing, each at a point of its own, reduced together.
///
/// Every claim is a [`BitRingSwitch`] set up at its own point.
/// All of them name the same number of variables, since they read the same packing.
#[derive(Clone, Debug)]
pub struct BitRingSwitchClaims<EF> {
    /// One reduction per claim, in the order the claims are bound.
    reductions: Vec<BitRingSwitch<EF>>,
}

/// What one claim of a batch sends: its tensor element, and its successor elements.
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound(serialize = "EF: TowerLevel", deserialize = "EF: TowerLevel"))]
pub struct ClaimElements<EF> {
    /// The tensor element both checks read, by rows and by columns.
    pub tensor: BitTensor<EF>,
    /// The carry and last elements, present exactly when the claim's setup sends them.
    pub successor: Option<SuccessorTensors<EF>>,
}

/// The messages a batch of claims puts on the wire.
///
/// ```text
///     per claim   its elements
///     once        the rounds, and the surviving value
/// ```
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(bound(serialize = "EF: TowerLevel", deserialize = "EF: TowerLevel"))]
pub struct BitRingSwitchClaimsProof<EF> {
    /// Every claim's elements, in the order the claims are bound.
    pub claims: Vec<ClaimElements<EF>>,
    /// The one degree-two sumcheck every claim shares.
    pub sumcheck: SumcheckData<EF, EF>,
    /// The value of the one surviving claim.
    pub final_eval: EF,
}

impl<EF> BitRingSwitchClaimsProof<EF> {
    /// Repackage a one-claim proof as a batch of one.
    fn of_single(proof: BitRingSwitchProof<EF>) -> Self {
        Self {
            claims: alloc::vec![ClaimElements {
                tensor: proof.tensor,
                successor: proof.successor,
            }],
            sumcheck: proof.sumcheck,
            final_eval: proof.final_eval,
        }
    }
}

impl<EF: TowerLevel> BitRingSwitchClaims<EF> {
    /// Gather claims to be reduced together.
    ///
    /// # Errors
    ///
    /// - No claim is given.
    /// - Two claims name different numbers of variables.
    pub fn new(reductions: Vec<BitRingSwitch<EF>>) -> Result<Self, BitRingSwitchError> {
        let first = reductions.first().ok_or(BitRingSwitchError::NoClaims)?;
        let expected = first.num_variables();
        if let Some(other) = reductions
            .iter()
            .find(|reduction| reduction.num_variables() != expected)
        {
            return Err(BitRingSwitchError::WidthMismatch {
                expected,
                actual: other.num_variables(),
            });
        }
        Ok(Self { reductions })
    }

    /// The claims, in the order they are bound.
    #[must_use]
    pub fn reductions(&self) -> &[BitRingSwitch<EF>] {
        &self.reductions
    }

    /// Variables the packed polynomial every claim reads must have.
    #[must_use]
    pub fn num_variables(&self) -> usize {
        self.reductions[0].num_variables()
    }

    /// The transcript description a batch of at least two claims plays.
    fn shape(&self) -> BitRingSwitchClaimsShape {
        BitRingSwitchClaimsShape {
            claims: self.reductions.iter().map(BitRingSwitch::shape).collect(),
        }
    }

    /// The leading Boolean coordinates every claim shares, and the slot address they spell.
    ///
    /// # Algorithm
    ///
    /// Each claim fixes its own Boolean prefix, which selects one slot of the packing.
    /// The shared sumcheck can skip only the coordinates every claim fixes the same way:
    ///
    /// ```text
    ///     claim 0   1 0 1 z ..
    ///     claim 1   1 0 0 z ..
    ///     shared    1 0          ->  two rounds skipped, slot address 0b10
    /// ```
    ///
    /// Inside that slot each claim's weight lives on its own sub-slot.
    fn common_prefix(&self) -> (usize, usize) {
        // No claim may lose a coordinate its own prefix does not fix.
        let limit = self
            .reductions
            .iter()
            .map(|reduction| reduction.fixed_prefix().0)
            .min()
            .expect("a batch holds at least one claim");
        let lead = &self.reductions[0].high()[..limit];
        let prefix = (0..limit)
            .find(|&j| {
                self.reductions[1..]
                    .iter()
                    .any(|other| other.high()[j] != lead[j])
            })
            .unwrap_or(limit);
        // Read the address from most significant bit to least significant bit.
        let address = lead[..prefix].iter().fold(0usize, |address, &bit| {
            (address << 1) | usize::from(bit == EF::ONE)
        });
        (prefix, address)
    }
}

/// `lambda^0, lambda^1, ..`, one weight per claim.
fn claim_weights<EF: Field>(lambda: EF, num_claims: usize) -> Vec<EF> {
    lambda.powers().take(num_claims).collect()
}

/// `eq(bits, r)` for Boolean `bits`: one factor `r_j` or `1 - r_j` per coordinate.
fn boolean_equality<EF: Field>(bits: &[EF], r: &[EF]) -> EF {
    bits.iter()
        .zip(r)
        .map(|(&bit, &r)| if bit == EF::ONE { r } else { EF::ONE - r })
        .product()
}

impl<EF: TranscriptField + TowerLevel> BitRingSwitchClaims<EF> {
    /// Each claim's second stage, over the challenges the batch drew.
    fn batches(&self, draws: &ClaimsDraws<EF>) -> Vec<BitRingSwitchBatch<'_, EF>> {
        self.reductions
            .iter()
            .map(|reduction| {
                // Only a claim sending successor elements weighs them under the shared alpha.
                let alpha = draws.alpha.filter(|_| reduction.sends_successor_tensors());
                reduction
                    .batch_drawn(&draws.batching_point, alpha)
                    .expect("the transcript draws what every claim's kind batches with")
            })
            .collect()
    }

    /// Reduce every claim to one claim about the packing, at one point.
    ///
    /// # The field the rounds run in
    ///
    /// As for [`BitRingSwitch::prove`]: `R` holds the packing, the weights and the rounds.
    ///
    /// # Returns
    ///
    /// The proof, the point the rounds ended at, and the surviving claim's value.
    /// A Boolean slot prefix every claim shares is restored in front of the random coordinates.
    ///
    /// # Panics
    ///
    /// Panics unless the packing has the variables the evaluation points leave.
    pub fn prove<R, Challenger, S>(
        &self,
        packing: &BitPacking<EF, S>,
        challenger: &mut Challenger,
    ) -> (BitRingSwitchClaimsProof<EF>, Point<EF>, EF)
    where
        EF: Send + Sync,
        R: IntoTranscriptField<EF> + Sync,
        Challenger: FieldChallenger<EF> + GrindingChallenger<Witness = EF>,
        S: Borrow<[EF]>,
    {
        // A batch of one is the one-claim reduction, its compact rounds included.
        if let [reduction] = self.reductions.as_slice() {
            let (proof, point, value) = reduction.prove::<R, _, _>(packing, challenger);
            return (BitRingSwitchClaimsProof::of_single(proof), point, value);
        }

        let num_variables = self.num_variables();
        assert_eq!(
            packing.num_variables(),
            num_variables,
            "the packing must have the {num_variables} variables the evaluation points leave"
        );

        // Every claim's elements are functions of its own point alone.
        // They are all ready before the transcript binds the first of them.
        let supports = self
            .reductions
            .iter()
            .map(BitRingSwitch::support)
            .collect::<Vec<_>>();
        let claims = self
            .reductions
            .iter()
            .zip(&supports)
            .map(|(reduction, (_, offset, equality))| ClaimElements {
                tensor: BitRingSwitch::tensor_over(packing, *offset, equality),
                successor: reduction.successor_tensors_over(packing, *offset, equality),
            })
            .collect::<Vec<_>>();

        let mut transcript =
            BitRingSwitchClaimsProverTranscript::<Challenger, EF>::new(challenger, self.shape());
        let statements = self.statements(&claims);
        let draws = transcript.statement(&statements);
        let batches = self.batches(&draws);
        let weights = claim_weights(draws.lambda, self.reductions.len());

        // The shared rounds run over the slot every claim's prefix agrees on.
        let (prefix, address) = self.common_prefix();
        let rounds = num_variables - prefix;
        let slot = 1usize << rounds;
        let slot_offset = address * slot;

        // sum_i lambda^i * A_i, each A_i written into the sub-slot its own prefix selects.
        let mut table = Poly::<R>::zero(rounds);
        for ((batch, (_, offset, equality)), &weight) in batches.iter().zip(&supports).zip(&weights)
        {
            let claim_weights = batch.weights_over::<R>(equality);
            let start = offset - slot_offset;
            let weight = R::from(weight);
            table.as_mut_slice()[start..start + claim_weights.num_evals()]
                .par_iter_mut()
                .zip(claim_weights.as_slice().par_iter())
                .for_each(|(entry, &value)| *entry += weight * value);
        }
        let initial_sum = Self::batched_sum(&batches, &claims, &weights);

        let mut prover = ReprSumcheckProver::<EF, EF, R>::from_repr_tables(
            VariableOrder::Prefix,
            slot_packing(packing, slot_offset, slot),
            table,
            initial_sum,
        );
        let mut sumcheck = SumcheckData::default();
        let r_prime = transcript.batched_sumcheck(|challenger| {
            prover.compute_sumcheck_polynomials(&mut sumcheck, challenger, rounds, 0)
        });

        // The rounds folded the packing down to its value at the point they ended on.
        let final_eval = prover.evals().as_slice()[0].into_transcript();
        transcript.surviving_claim(final_eval);
        transcript.finish();

        (
            BitRingSwitchClaimsProof {
                claims,
                sumcheck,
                final_eval,
            },
            self.restore_prefix(prefix, &r_prime),
            final_eval,
        )
    }

    /// Replay the batch and return the one claim it leaves behind, as a point and a value.
    ///
    /// Discharging that pair against a commitment to the packing is the caller's business.
    ///
    /// # Arguments
    ///
    /// - The proof.
    /// - Per claim, its current and successor readings, each when it is to be checked.
    /// - The challenger, advanced only once every structural check passes.
    ///
    /// # Errors
    ///
    /// Before the challenger is touched:
    ///
    /// - Elements or readings for a number of claims other than the setup's.
    /// - Any claim's structural rejection, as in [`BitRingSwitch::verify_readings`].
    ///
    /// After it:
    ///
    /// - A reading disagreeing with its claim's elements.
    /// - A failed round, or a surviving value that does not close the batched sum.
    pub fn verify_readings<Challenger>(
        &self,
        proof: &BitRingSwitchClaimsProof<EF>,
        readings: &[(Option<EF>, Option<EF>)],
        challenger: &mut Challenger,
    ) -> Result<(Point<EF>, EF), BitRingSwitchProofError>
    where
        Challenger: FieldChallenger<EF> + GrindingChallenger<Witness = EF>,
    {
        let expected = self.reductions.len();
        if let Some(actual) = [proof.claims.len(), readings.len()]
            .into_iter()
            .find(|&count| count != expected)
        {
            return Err(BitRingSwitchProofError::ClaimCount { expected, actual });
        }

        // A batch of one replays the one-claim reduction it was proved with.
        if let ([reduction], [claim], [(current, next)]) = (
            self.reductions.as_slice(),
            proof.claims.as_slice(),
            readings,
        ) {
            let single = BitRingSwitchProof {
                tensor: claim.tensor.clone(),
                successor: claim.successor.clone(),
                sumcheck: proof.sumcheck.clone(),
                final_eval: proof.final_eval,
            };
            return reduction.verify_readings(&single, *current, *next, challenger);
        }

        // Every structural rejection runs before the challenger is touched.
        for ((reduction, claim), &(current, next)) in
            self.reductions.iter().zip(&proof.claims).zip(readings)
        {
            reduction.check_elements(&claim.tensor, claim.successor.as_ref(), current, next)?;
        }
        check_no_grinding(&proof.sumcheck)?;

        // The verifier derives the shared slot from the public points alone.
        let (prefix, _) = self.common_prefix();
        let rounds = self.num_variables() - prefix;
        let mut transcript =
            BitRingSwitchClaimsVerifierTranscript::<Challenger, EF>::new(challenger, self.shape());
        let draws = transcript.statement(&self.statements(&proof.claims))?;

        // Each reading is checked against its own claim's columns, before any round.
        for ((reduction, claim), &(current, next)) in
            self.reductions.iter().zip(&proof.claims).zip(readings)
        {
            if let Err(error) = reduction.check_element_readings(
                &claim.tensor,
                claim.successor.as_ref(),
                current,
                next,
            ) {
                transcript.abort();
                return Err(error);
            }
        }

        let batches = self.batches(&draws);
        let weights = claim_weights(draws.lambda, self.reductions.len());

        // The starting sum is derived from the elements' rows, never taken from the prover.
        let mut sum = Self::batched_sum(&batches, &proof.claims, &weights);
        let replay = transcript.batched_sumcheck(|challenger| {
            proof
                .sumcheck
                .verify_rounds(challenger, &mut sum, rounds, 0, Basis::Evaluation)
        });
        let r_prime = match replay {
            Ok(point) => point,
            Err(error) => {
                transcript.abort();
                return Err(error.into());
            }
        };
        transcript.surviving_claim(proof.final_eval);
        transcript.finish();

        // The rounds close on sum_i lambda^i * A_i(r'), times the surviving value.
        let closing = self.closing_weight(&batches, &weights, prefix, &r_prime)?;
        if sum != closing * proof.final_eval {
            return Err(BitRingSwitchProofError::FinalCheck);
        }

        Ok((self.restore_prefix(prefix, &r_prime), proof.final_eval))
    }

    /// What every claim binds, borrowed from the claims and their elements.
    fn statements<'a>(&'a self, claims: &'a [ClaimElements<EF>]) -> Vec<ClaimStatement<'a, EF>> {
        self.reductions
            .iter()
            .zip(claims)
            .map(|(reduction, claim)| ClaimStatement {
                point: reduction.point(),
                rows: claim.tensor.rows(),
                successor: claim
                    .successor
                    .as_ref()
                    .map(|elements| (elements.carry.rows(), elements.last.rows())),
            })
            .collect()
    }

    /// `sum_i lambda^i * sigma_i`, the sum the shared rounds start from.
    fn batched_sum(
        batches: &[BitRingSwitchBatch<'_, EF>],
        claims: &[ClaimElements<EF>],
        weights: &[EF],
    ) -> EF {
        batches
            .iter()
            .zip(claims)
            .zip(weights)
            .map(|((batch, claim), &weight)| {
                weight * batch.initial_sum(&claim.tensor, claim.successor.as_ref())
            })
            .sum()
    }

    /// `sum_i lambda^i * A_i(r')`, the combined weight where the shared rounds ended.
    ///
    /// # Algorithm
    ///
    /// Claim `i` fixes `p_i >= p` leading bits, of which the batch skips the shared `p`.
    /// Its weight vanishes off its sub-slot and factors on the rest:
    ///
    /// ```text
    ///     A_i(r') = eq(bits_i[p..p_i], r'[..p_i - p]) * A_i|slot(r'[p_i - p..])
    /// ```
    fn closing_weight(
        &self,
        batches: &[BitRingSwitchBatch<'_, EF>],
        weights: &[EF],
        prefix: usize,
        r_prime: &Point<EF>,
    ) -> Result<EF, BitRingSwitchError> {
        let mut closing = EF::ZERO;
        for ((reduction, batch), &weight) in self.reductions.iter().zip(batches).zip(weights) {
            let own = reduction.fixed_prefix().0 - prefix;
            let (selected, rest) = r_prime.as_slice().split_at(own);
            let gate = boolean_equality(&reduction.high()[prefix..prefix + own], selected);
            let inside = batch.closing_weight(&Point::new(rest.to_vec()))?;
            closing += weight * gate * inside;
        }
        Ok(closing)
    }

    /// Restore the shared Boolean slot address in front of a point inside that slot.
    fn restore_prefix(&self, prefix: usize, point: &Point<EF>) -> Point<EF> {
        let mut coordinates = self.reductions[0].high()[..prefix].to_vec();
        coordinates.extend_from_slice(point.as_slice());
        Point::new(coordinates)
    }
}

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

    use p3_binary_field::{BinaryChallenger, BinaryField16};
    use p3_challenger::{CanSample, HashChallenger};
    use p3_field::PrimeCharacteristicRing;
    use p3_keccak::Keccak256Hash;
    use proptest::prelude::*;
    use rand::rngs::SmallRng;
    use rand::{RngExt, SeedableRng};

    use super::*;

    type EF = BinaryField16;
    type Chal = BinaryChallenger<EF, HashChallenger<u8, Keccak256Hash, 32>>;

    /// Coordinates a 16-bit element absorbs.
    const ABSORBED: usize = BitRingSwitch::<EF>::ABSORBED;

    fn challenger() -> Chal {
        Chal::from_hasher(Vec::new(), Keccak256Hash)
    }

    fn bits(seed: u64, bytes: usize) -> Vec<u8> {
        let mut rng = SmallRng::seed_from_u64(seed);
        (0..bytes).map(|_| rng.random::<u8>()).collect()
    }

    /// The bit witness, one field element per bit.
    fn embedded(witness: &[u8]) -> Poly<EF> {
        Poly::new(
            (0..witness.len() * 8)
                .map(|cell| EF::from_bool((witness[cell / 8] >> (cell % 8)) & 1 == 1))
                .collect(),
        )
    }

    /// The witness one row further on, the last row repeating, straight from the definition.
    fn dense_successor(witness: &[u8], point: &Point<EF>, row_variables: usize) -> EF {
        let cells = embedded(witness);
        let (selector, rho) = point.split_at(point.num_variables() - row_variables);
        let eq_selector = Poly::<EF>::new_from_point(selector.as_slice(), EF::ONE);
        let eq_rho = Poly::<EF>::new_from_point(rho.as_slice(), EF::ONE);
        let rows = 1usize << row_variables;
        let mut claim = EF::ZERO;
        for (c, &gate) in eq_selector.as_slice().iter().enumerate() {
            for (z, &weight) in eq_rho.as_slice().iter().enumerate() {
                claim += gate * weight * cells.as_slice()[c * rows + (z + 1).min(rows - 1)];
            }
        }
        claim
    }

    /// One claim's setup: its point, and the successor rows when it reads the next row too.
    #[derive(Clone, Debug)]
    struct Claim {
        point: Point<EF>,
        successor: Option<usize>,
    }

    impl Claim {
        fn reduction(&self) -> BitRingSwitch<EF> {
            self.successor
                .map_or_else(
                    || BitRingSwitch::new(&self.point),
                    |rows| BitRingSwitch::with_successor(&self.point, rows),
                )
                .unwrap()
        }

        /// The honest readings, straight from the witness.
        fn readings(&self, witness: &[u8]) -> (Option<EF>, Option<EF>) {
            let current = embedded(witness).eval_base(&self.point);
            let next = self
                .successor
                .map(|rows| dense_successor(witness, &self.point, rows));
            (Some(current), next)
        }
    }

    fn batch(claims: &[Claim]) -> BitRingSwitchClaims<EF> {
        BitRingSwitchClaims::new(claims.iter().map(Claim::reduction).collect()).unwrap()
    }

    fn honest_readings(claims: &[Claim], witness: &[u8]) -> Vec<(Option<EF>, Option<EF>)> {
        claims.iter().map(|claim| claim.readings(witness)).collect()
    }

    /// A point whose leading coordinates spell `prefix`, the rest drawn at random.
    fn point_with_prefix(rng: &mut SmallRng, prefix: &[bool], num_variables: usize) -> Point<EF> {
        Point::new(
            (0..num_variables)
                .map(|i| match prefix.get(i) {
                    Some(&bit) => EF::from_bool(bit),
                    None => rng.random(),
                })
                .collect(),
        )
    }

    /// Prove and verify one batch, then check the surviving claim against the packing.
    fn round_trip(witness: &[u8], claims: &[Claim]) -> BitRingSwitchClaimsProof<EF> {
        let packing = BitPacking::<EF>::new(witness).unwrap();
        let setup = batch(claims);
        let mut prover = challenger();
        let (proof, point_p, value_p) = setup.prove::<EF, _, _>(&packing, &mut prover);
        let mut verifier = challenger();
        let (point, value) = setup
            .verify_readings(&proof, &honest_readings(claims, witness), &mut verifier)
            .unwrap();

        // Both sides agree on the one surviving claim, and it is true of the packing.
        assert_eq!((&point, value), (&point_p, value_p));
        assert_eq!(value, packing.poly().eval_base(&point));
        // The two sponges end in the same state.
        assert_eq!(
            CanSample::<EF>::sample(&mut prover),
            CanSample::<EF>::sample(&mut verifier)
        );
        proof
    }

    #[test]
    fn an_empty_or_ragged_batch_is_refused() {
        // No claim at all.
        assert_eq!(
            BitRingSwitchClaims::<EF>::new(Vec::new()).unwrap_err(),
            BitRingSwitchError::NoClaims
        );

        // Two claims over packings of different widths.
        let mut rng = SmallRng::seed_from_u64(1);
        let narrow = BitRingSwitch::new(&Point::<EF>::rand(&mut rng, 8)).unwrap();
        let wide = BitRingSwitch::new(&Point::<EF>::rand(&mut rng, 9)).unwrap();
        assert_eq!(
            BitRingSwitchClaims::new(vec![narrow, wide]).unwrap_err(),
            BitRingSwitchError::WidthMismatch {
                expected: 4,
                actual: 5
            }
        );
    }

    #[test]
    fn a_batch_of_one_is_the_one_claim_reduction() {
        // One claim, proved alone and as a batch of one.
        let witness = bits(0xB1, 64);
        let packing = BitPacking::<EF>::new(&witness).unwrap();
        let point = Point::<EF>::rand(&mut SmallRng::seed_from_u64(0xB2), 9);
        let reduction = BitRingSwitch::new(&point).unwrap();
        let (single, single_point, _) = reduction.prove::<EF, _, _>(&packing, &mut challenger());
        let setup = BitRingSwitchClaims::new(vec![reduction]).unwrap();
        let (batched, batched_point, _) = setup.prove::<EF, _, _>(&packing, &mut challenger());

        // The same messages, so the same transcript and the same surviving point.
        assert_eq!(batched.claims[0].tensor, single.tensor);
        assert_eq!(
            format!("{:?}", batched.sumcheck),
            format!("{:?}", single.sumcheck)
        );
        assert_eq!(batched_point, single_point);
    }

    #[test]
    fn several_random_points_leave_one_true_claim() {
        // Four claims at unrelated points: no shared prefix, every round runs.
        let witness = bits(0xC1, 64);
        let mut rng = SmallRng::seed_from_u64(0xC2);
        let claims = (0..4)
            .map(|_| Claim {
                point: Point::rand(&mut rng, 9),
                successor: None,
            })
            .collect::<Vec<_>>();
        let proof = round_trip(&witness, &claims);

        // One element per claim, and one sumcheck of l' = 9 - 4 rounds for all of them.
        assert_eq!(proof.claims.len(), 4);
        assert_eq!(proof.sumcheck.num_rounds(), 9 - ABSORBED);
    }

    #[test]
    fn claims_mixing_successor_views_share_one_alpha() {
        // Invariant: alpha is drawn once, and weighs only the claims that send successor elements.
        //
        //     claim 0   current reading only                 K = 1
        //     claim 1   successor rows outrun one element    K = 3
        //     claim 2   successor rows inside one element    K = 1
        let witness = bits(0xD1, 64);
        let mut rng = SmallRng::seed_from_u64(0xD2);
        let claims = [None, Some(7), Some(2)].map(|successor| Claim {
            point: Point::rand(&mut rng, 9),
            successor,
        });
        let proof = round_trip(&witness, &claims);
        assert!(proof.claims[1].successor.is_some());
        assert!(proof.claims[0].successor.is_none() && proof.claims[2].successor.is_none());
    }

    #[test]
    fn a_shared_boolean_prefix_skips_its_rounds() {
        // Both claims read column 0b10 of a stacked trace, at different rows.
        let witness = bits(0xE1, 128);
        let mut rng = SmallRng::seed_from_u64(0xE2);
        let claims = [0, 1].map(|_| Claim {
            point: point_with_prefix(&mut rng, &[true, false], 10),
            successor: None,
        });
        let proof = round_trip(&witness, &claims);

        // The two shared bits select the slot, so two of the six rounds never run.
        assert_eq!(proof.sumcheck.num_rounds(), 10 - ABSORBED - 2);
    }

    #[test]
    fn prefixes_that_part_keep_each_claim_in_its_own_sub_slot() {
        // Invariant: only the leading bits every claim agrees on are skipped.
        //
        //     claim 0   1 0 1 ..    its weight on sub-slot 0b01 of slot 0b1
        //     claim 1   1 1 ..      its weight on sub-slot 0b1  of slot 0b1
        //     claim 2   1 z ..      spread over all of slot 0b1
        //     claim 3   1 0 1 ..    the same sub-slot as claim 0, another row
        let witness = bits(0xF1, 128);
        let mut rng = SmallRng::seed_from_u64(0xF2);
        let claims = [
            vec![true, false, true],
            vec![true, true],
            vec![true],
            vec![true, false, true],
        ]
        .map(|prefix| Claim {
            point: point_with_prefix(&mut rng, &prefix, 10),
            successor: None,
        });
        let proof = round_trip(&witness, &claims);
        assert_eq!(proof.sumcheck.num_rounds(), 10 - ABSORBED - 1);

        // Claims in disjoint slots share nothing, and every round runs.
        let apart = [vec![false], vec![true]].map(|prefix| Claim {
            point: point_with_prefix(&mut rng, &prefix, 10),
            successor: Some(3),
        });
        let proof = round_trip(&witness, &apart);
        assert_eq!(proof.sumcheck.num_rounds(), 10 - ABSORBED);
    }

    /// Three honest claims, their batch, and the proof it produced.
    fn honest_batch() -> (Vec<u8>, Vec<Claim>, BitRingSwitchClaimsProof<EF>) {
        let witness = bits(0xA1, 64);
        let mut rng = SmallRng::seed_from_u64(0xA2);
        let claims = [None, Some(6), None]
            .map(|successor| Claim {
                point: Point::rand(&mut rng, 9),
                successor,
            })
            .to_vec();
        let packing = BitPacking::<EF>::new(&witness).unwrap();
        let (proof, _, _) = batch(&claims).prove::<EF, _, _>(&packing, &mut challenger());
        (witness, claims, proof)
    }

    #[test]
    fn a_false_reading_at_one_point_is_rejected() {
        let (witness, claims, proof) = honest_batch();
        let setup = batch(&claims);

        // Move the current reading of the middle claim alone.
        let mut readings = honest_readings(&claims, &witness);
        readings[1].0 = readings[1].0.map(|value| value + EF::ONE);
        assert_eq!(
            setup
                .verify_readings(&proof, &readings, &mut challenger())
                .unwrap_err(),
            BitRingSwitchProofError::ClaimMismatch
        );

        // Move its successor reading instead.
        let mut readings = honest_readings(&claims, &witness);
        readings[1].1 = readings[1].1.map(|value| value + EF::ONE);
        assert_eq!(
            setup
                .verify_readings(&proof, &readings, &mut challenger())
                .unwrap_err(),
            BitRingSwitchProofError::SuccessorClaimMismatch
        );
    }

    /// A prover that tampers with the elements before binding them, then plays the rounds honestly.
    ///
    /// ```text
    ///     honest elements  ->  tamper  ->  bind  ->  r'', alpha, lambda
    ///     rounds           ->  measured on the honest tables, under those draws
    /// ```
    ///
    /// Each round message is read off the tables alone.
    /// The verifier derives the starting sum from the forged rows.
    /// So the rounds prove the forged sum exactly when it equals the honest one.
    fn forge(
        setup: &BitRingSwitchClaims<EF>,
        packing: &BitPacking<EF>,
        tamper: impl FnOnce(&mut [ClaimElements<EF>]),
    ) -> BitRingSwitchClaimsProof<EF> {
        let supports = setup
            .reductions()
            .iter()
            .map(BitRingSwitch::support)
            .collect::<Vec<_>>();
        let honest = setup
            .reductions()
            .iter()
            .zip(&supports)
            .map(|(reduction, (_, offset, equality))| ClaimElements {
                tensor: BitRingSwitch::tensor_over(packing, *offset, equality),
                successor: reduction.successor_tensors_over(packing, *offset, equality),
            })
            .collect::<Vec<_>>();
        let mut claims = honest.clone();
        tamper(&mut claims);

        // The forged elements are what the transcript binds.
        let mut challenger = challenger();
        let mut transcript =
            BitRingSwitchClaimsProverTranscript::<Chal, EF>::new(&mut challenger, setup.shape());
        let draws = transcript.statement(&setup.statements(&claims));
        let batches = setup.batches(&draws);
        let weights = claim_weights(draws.lambda, claims.len());

        // The combined weight table, exactly as the honest prover builds it.
        let (prefix, address) = setup.common_prefix();
        let rounds = setup.num_variables() - prefix;
        let slot = 1usize << rounds;
        let mut table = Poly::<EF>::zero(rounds);
        for ((batch, (_, offset, equality)), &weight) in batches.iter().zip(&supports).zip(&weights)
        {
            let start = offset - address * slot;
            let claim_weights = batch.weights_over::<EF>(equality);
            for (entry, &value) in table.as_mut_slice()[start..]
                .iter_mut()
                .zip(claim_weights.as_slice())
            {
                *entry += weight * value;
            }
        }

        // The prover is seeded with the sum the tables really have, which only its debug checks read.
        let mut prover = ReprSumcheckProver::<EF, EF, EF>::from_repr_tables(
            VariableOrder::Prefix,
            slot_packing(packing, address * slot, slot),
            table,
            BitRingSwitchClaims::batched_sum(&batches, &honest, &weights),
        );
        let mut sumcheck = SumcheckData::default();
        let _ = transcript.batched_sumcheck(|challenger| {
            prover.compute_sumcheck_polynomials(&mut sumcheck, challenger, rounds, 0)
        });
        let final_eval = prover.evals().as_slice()[0];
        transcript.surviving_claim(final_eval);
        transcript.finish();

        BitRingSwitchClaimsProof {
            claims,
            sumcheck,
            final_eval,
        }
    }

    /// The readings a proof's elements imply, so no column check can object to them.
    fn implied_readings(
        setup: &BitRingSwitchClaims<EF>,
        proof: &BitRingSwitchClaimsProof<EF>,
        claims: &[Claim],
    ) -> Vec<(Option<EF>, Option<EF>)> {
        setup
            .reductions()
            .iter()
            .zip(&proof.claims)
            .zip(claims)
            .map(|((reduction, elements), claim)| {
                let current = reduction.incoming_claim(&elements.tensor);
                let next = claim.successor.map(|_| {
                    reduction
                        .successor_claim(&elements.tensor, elements.successor.as_ref())
                        .unwrap()
                });
                (Some(current), next)
            })
            .collect()
    }

    /// Add one to row 5 of the tensor element of every listed claim.
    fn shift_row(claims: &mut [ClaimElements<EF>], indices: &[usize]) {
        for &index in indices {
            let mut rows = claims[index].tensor.rows().to_vec();
            rows[5] += EF::ONE;
            claims[index].tensor = BitTensor::try_from(rows).unwrap();
        }
    }

    #[test]
    fn a_forged_element_at_one_point_moves_the_starting_sum() {
        // Invariant: the rows of every claim's element reach the batched starting sum.
        //
        //     columns  ->  the forged readings, which the column check accepts
        //     rows     ->  sum_i lambda^i * sigma_i, off by lambda^index * D(r'')
        let (witness, claims, _) = honest_batch();
        let packing = BitPacking::<EF>::new(&witness).unwrap();
        let setup = batch(&claims);
        for index in 0..claims.len() {
            // Forge one claim's element, current-only or successor alike.
            let forged = forge(&setup, &packing, |elements| shift_row(elements, &[index]));
            let readings = implied_readings(&setup, &forged, &claims);

            // The rounds were played for the forged transcript, yet the sum they close on is wrong.
            assert_eq!(
                setup
                    .verify_readings(&forged, &readings, &mut challenger())
                    .unwrap_err(),
                BitRingSwitchProofError::FinalCheck,
                "claim {index}"
            );
        }
    }

    #[test]
    fn one_row_error_in_two_claims_does_not_cancel() {
        // Invariant: the powers of lambda keep two equal row errors apart.
        //
        //     weights 1, 1          D(r'') + D(r'') = 0 in characteristic 2  ->  accepted
        //     weights 1, lambda     (1 + lambda) * D(r'') != 0               ->  refused
        let witness = bits(0xA1, 64);
        let packing = BitPacking::<EF>::new(&witness).unwrap();
        let mut rng = SmallRng::seed_from_u64(0xA3);
        let point = Point::<EF>::rand(&mut rng, 9);
        let distinct = [point.clone(), Point::rand(&mut rng, 9)];
        let repeated = [point.clone(), point];

        // Two plain claims at distinct points, then at one point used twice.
        for points in [distinct, repeated] {
            let claims = points.map(|point| Claim {
                point,
                successor: None,
            });
            let setup = batch(&claims);

            // The same row error in both elements.
            let forged = forge(&setup, &packing, |elements| shift_row(elements, &[0, 1]));
            let readings = implied_readings(&setup, &forged, &claims);

            // Both readings moved, and each still agrees with its own element's columns.
            assert_ne!(readings, honest_readings(&claims, &witness));
            assert_eq!(
                setup
                    .verify_readings(&forged, &readings, &mut challenger())
                    .unwrap_err(),
                BitRingSwitchProofError::FinalCheck
            );
        }
    }

    #[test]
    fn a_tampered_surviving_value_does_not_close_the_rounds() {
        let (witness, claims, mut proof) = honest_batch();
        proof.final_eval += EF::ONE;
        assert_eq!(
            batch(&claims)
                .verify_readings(
                    &proof,
                    &honest_readings(&claims, &witness),
                    &mut challenger()
                )
                .unwrap_err(),
            BitRingSwitchProofError::FinalCheck
        );
    }

    #[test]
    fn a_proof_does_not_verify_with_its_claims_reordered() {
        // Swap the first and last claims, and their elements with them.
        let (witness, mut claims, mut proof) = honest_batch();
        claims.swap(0, 2);
        proof.claims.swap(0, 2);
        let setup = batch(&claims);
        let readings = honest_readings(&claims, &witness);

        // Every element still sits beside its own claim, so every column check passes.
        for ((reduction, elements), &(current, next)) in
            setup.reductions().iter().zip(&proof.claims).zip(&readings)
        {
            reduction
                .check_element_readings(
                    &elements.tensor,
                    elements.successor.as_ref(),
                    current,
                    next,
                )
                .unwrap();
        }

        // The swapped statement draws other challenges, which the recorded rounds do not answer.
        let error = setup
            .verify_readings(&proof, &readings, &mut challenger())
            .unwrap_err();
        assert!(
            matches!(
                error,
                BitRingSwitchProofError::Sumcheck(_) | BitRingSwitchProofError::FinalCheck
            ),
            "{error:?}"
        );
    }

    #[test]
    fn a_claim_count_other_than_the_setup_is_refused() {
        let (witness, claims, proof) = honest_batch();
        let setup = batch(&claims);
        let readings = honest_readings(&claims, &witness);

        // One element short.
        let mut short = proof.clone();
        short.claims.pop();
        assert_eq!(
            setup
                .verify_readings(&short, &readings, &mut challenger())
                .unwrap_err(),
            BitRingSwitchProofError::ClaimCount {
                expected: 3,
                actual: 2
            }
        );

        // One reading short.
        assert_eq!(
            setup
                .verify_readings(&proof, &readings[..2], &mut challenger())
                .unwrap_err(),
            BitRingSwitchProofError::ClaimCount {
                expected: 3,
                actual: 2
            }
        );
    }

    #[test]
    fn a_stray_grinding_witness_is_refused() {
        let (witness, claims, mut proof) = honest_batch();
        proof.sumcheck.pow_witnesses.push(EF::ZERO);
        assert_eq!(
            batch(&claims)
                .verify_readings(
                    &proof,
                    &honest_readings(&claims, &witness),
                    &mut challenger()
                )
                .unwrap_err(),
            BitRingSwitchProofError::NonEmptyPowWitnesses { actual: 1 }
        );
    }

    proptest! {
        #![proptest_config(ProptestConfig { cases: 24, ..ProptestConfig::default() })]

        /// The batch against one reduction per point, over random witnesses, points and views.
        #[test]
        fn the_batch_agrees_with_one_reduction_per_point(
            log_bytes in 1usize..=5,
            num_claims in 1usize..=4,
            seed: u64,
            successor_mask in 0u8..16,
            prefix_bits in 0usize..=2,
        ) {
            let witness = bits(seed, 1 << log_bytes);
            let packing = BitPacking::<EF>::new(&witness).unwrap();
            let num_variables = log_bytes + 3;
            let mut rng = SmallRng::seed_from_u64(seed ^ 0x5EED);
            // A shared Boolean prefix, never longer than the packing's own coordinates.
            let prefix = (0..prefix_bits.min(num_variables - ABSORBED))
                .map(|_| rng.random::<bool>())
                .collect::<Vec<_>>();
            let claims = (0..num_claims)
                .map(|i| Claim {
                    point: point_with_prefix(&mut rng, &prefix, num_variables),
                    successor: ((successor_mask >> i) & 1 == 1)
                        .then(|| rng.random_range(0..=num_variables)),
                })
                .collect::<Vec<_>>();

            // Reference: one reduction per point, each on its own sponge.
            let reference = claims
                .iter()
                .map(|claim| {
                    let (proof, _, _) =
                        claim.reduction().prove::<EF, _, _>(&packing, &mut challenger());
                    ClaimElements { tensor: proof.tensor, successor: proof.successor }
                })
                .collect::<Vec<_>>();

            let proof = round_trip(&witness, &claims);

            // Every claim sends the elements its own reduction would.
            prop_assert_eq!(&proof.claims, &reference);
            // And those elements carry the readings the witness defines.
            let setup = batch(&claims);
            for ((reduction, elements), claim) in
                setup.reductions().iter().zip(&proof.claims).zip(&claims)
            {
                let (current, next) = claim.readings(&witness);
                prop_assert_eq!(Some(reduction.incoming_claim(&elements.tensor)), current);
                if next.is_some() {
                    let successor = reduction
                        .successor_claim(&elements.tensor, elements.successor.as_ref())
                        .unwrap();
                    prop_assert_eq!(Some(successor), next);
                }
            }
        }
    }
}