provekit-whir 0.1.1

An implementation of the WHIR polynomial commitment scheme
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
#![allow(type_alias_bounds)] // We need the bound to reference F::BasePrimeField.

mod config;
mod prover;
pub(crate) mod rounds;
mod verifier;

use std::fmt::Debug;

use ark_ff::{FftField, Field};
use ark_std::rand::{CryptoRng, RngCore};
use serde::{Deserialize, Serialize};
#[cfg(feature = "tracing")]
use tracing::instrument;

use crate::{
    algebra::{
        embedding::{Embedding, Identity},
        linear_form::LinearForm,
    },
    hash::Hash,
    protocols::{irs_commit, proof_of_work, sumcheck},
    transcript::{
        Codec, DuplexSpongeInterface, ProverMessage, ProverState, VerificationResult, VerifierState,
    },
    utils::zip_strict,
    verify,
};

#[derive(Clone, PartialEq, Eq, Serialize, Deserialize, Debug)]
#[serde(bound = "M: Embedding, M::Source: FftField, M::Target: FftField")]
pub struct Config<M>
where
    M: Embedding,
    M::Source: FftField,
    M::Target: FftField,
{
    pub initial_committer: irs_commit::Config<M>,
    pub initial_sumcheck: sumcheck::Config<M::Target>,
    pub initial_skip_pow: proof_of_work::Config,
    pub round_configs: Vec<RoundConfig<M::Target>>,
    pub final_sumcheck: sumcheck::Config<M::Target>,
    pub final_pow: proof_of_work::Config,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(bound = "F: FftField")]
pub struct RoundConfig<F>
where
    F: FftField,
{
    pub irs_committer: irs_commit::Config<Identity<F>>,
    pub sumcheck: sumcheck::Config<F>,
    pub pow: proof_of_work::Config,
}

pub type Witness<F: FftField, M: Embedding<Target = F>> = irs_commit::Witness<M::Source, F>;
pub type Commitment<F: Field> = irs_commit::Commitment<F>;

#[must_use = "The final claim must be checked if there where any linear forms."]
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct FinalClaim<F: Field> {
    /// Multinlinear extension evaluation point.
    pub evaluation_point: Vec<F>,
    /// The random linear combination coefficients.
    pub rlc_coefficients: Vec<F>,
    /// Claimed value of the rlc of the mle of the linears forms in the point.
    /// Note: not computed on the prover side, set to zero instead.
    pub linear_form_rlc: F,
}

impl<F: Field> FinalClaim<F> {
    pub fn verify<'a>(
        &'a self,
        linear_forms: impl IntoIterator<Item = &'a dyn LinearForm<F>>,
    ) -> VerificationResult<()> {
        let rlc = zip_strict(&self.rlc_coefficients, linear_forms)
            .map(|(&c, l)| c * l.mle_evaluate(&self.evaluation_point))
            .sum::<F>();
        verify!(rlc == self.linear_form_rlc);
        Ok(())
    }
}

impl<M> Config<M>
where
    M: Embedding,
    M::Source: FftField,
    M::Target: FftField,
{
    /// Commit to one or more vectors.
    #[cfg_attr(feature = "tracing", instrument(skip_all, fields(size = vectors.first().unwrap().len())))]
    pub fn commit<H, R>(
        &self,
        prover_state: &mut ProverState<H, R>,
        vectors: &[&[M::Source]],
    ) -> Witness<M::Target, M>
    where
        H: DuplexSpongeInterface,
        R: RngCore + CryptoRng,
        M::Target: Codec<[H::U]>,
        Hash: ProverMessage<[H::U]>,
    {
        self.initial_committer.commit(prover_state, vectors)
    }

    /// Receive a commitment to vectors.
    pub fn receive_commitment<H>(
        &self,
        verifier_state: &mut VerifierState<H>,
    ) -> VerificationResult<Commitment<M::Target>>
    where
        H: DuplexSpongeInterface,
        M::Target: Codec<[H::U]>,
        Hash: ProverMessage<[H::U]>,
    {
        self.initial_committer.receive_commitment(verifier_state)
    }

    /// Disable proof-of-work for test.
    #[cfg(test)]
    pub(crate) fn disable_pow(&mut self) {
        self.initial_sumcheck.round_pow.threshold = u64::MAX;
        self.initial_skip_pow.threshold = u64::MAX;
        for round in &mut self.round_configs {
            round.sumcheck.round_pow.threshold = u64::MAX;
            round.pow.threshold = u64::MAX;
        }
        self.final_sumcheck.round_pow.threshold = u64::MAX;
        self.final_pow.threshold = u64::MAX;
    }
}

#[cfg(test)]
mod tests {
    use std::borrow::Cow;

    use ark_ff::{Field, UniformRand};

    use super::*;
    use crate::{
        algebra::{
            embedding::Basefield,
            fields::{Field64, Field64_3},
            linear_form::{Covector, Evaluate, LinearForm, MultilinearExtension},
            MultilinearPoint,
        },
        hash,
        parameters::ProtocolParameters,
        transcript::{codecs::Empty, DomainSeparator, ProverState, VerifierState},
        utils::test_serde,
    };

    /// Field type used in the tests.
    type F = Field64;

    /// Extension field type used in the tests.
    type EF = Field64_3;

    /// Build owned linear forms for `prove()` (which consumes them).
    fn build_prove_forms<F: Field>(
        points: &[MultilinearPoint<F>],
        num_variables: usize,
        include_covector: bool,
    ) -> Vec<Box<dyn LinearForm<F>>> {
        let mut forms: Vec<Box<dyn LinearForm<F>>> = Vec::new();
        for point in points {
            forms.push(Box::new(MultilinearExtension {
                point: point.0.clone(),
            }));
        }
        if include_covector {
            forms.push(Box::new(Covector {
                vector: (0..1 << num_variables).map(F::from).collect(),
            }));
        }
        forms
    }

    /// Run a complete WHIR proof lifecycle: commit, prove, and verify.
    ///
    /// This function:
    /// - builds a multilinear polynomial with a specified number of variables,
    /// - constructs a statement with constraints based on evaluations and linear relations,
    /// - commits to the polynomial using a Merkle-based commitment scheme,
    /// - generates a proof using the WHIR prover,
    /// - verifies the proof using the WHIR verifier.
    fn make_whir_things(
        num_variables: usize,
        initial_folding_factor: usize,
        folding_factor: usize,
        num_points: usize,
        unique_decoding: bool,
        pow_bits: usize,
    ) {
        // Number of coefficients in the multilinear polynomial (2^num_variables)
        let num_coeffs = 1 << num_variables;

        // Randomness source
        let mut rng = ark_std::test_rng();

        // Configure the WHIR protocol parameters
        let whir_params = ProtocolParameters {
            security_level: 32,
            pow_bits,
            initial_folding_factor,
            folding_factor,
            unique_decoding,
            starting_log_inv_rate: 1,
            batch_size: 1,
            hash_id: hash::SHA2,
        };

        // Build global configuration from protocol parameters
        let mut params = Config::<Basefield<EF>>::new(1 << num_variables, &whir_params);
        params.disable_pow();
        eprintln!("{params}");

        // Test that the config is serializable
        test_serde(&params);

        // Our test vector is all ones in the basefield.
        let vector = vec![F::ONE; num_coeffs];

        // Generate `num_points` random points in the multilinear domain
        let points: Vec<_> = (0..num_points)
            .map(|_| MultilinearPoint::rand(&mut rng, num_variables))
            .collect();

        let mut linear_forms: Vec<Box<dyn LinearForm<EF>>> = Vec::new();
        let mut evaluations = Vec::new();

        for point in &points {
            let linear_form = MultilinearExtension {
                point: point.0.clone(),
            };
            evaluations.push(linear_form.evaluate(params.embedding(), &vector));
            linear_forms.push(Box::new(linear_form));
        }

        let covector = Covector {
            vector: (0..1 << num_variables).map(EF::from).collect(),
        };
        let sum = covector.evaluate(params.embedding(), &vector);
        linear_forms.push(Box::new(covector));
        evaluations.push(sum);

        // Define the Fiat-Shamir domain separator for committing and proving
        let ds = DomainSeparator::protocol(&params)
            .session(&format!("Test at {}:{}", file!(), line!()))
            .instance(&Empty);

        // Initialize the Merlin transcript from the domain separator
        let mut prover_state = ProverState::new_std(&ds);

        // Commit to the polynomial and generate auxiliary witness data
        let witness = params.commit(&mut prover_state, &[&vector]);

        let prove_linear_forms = build_prove_forms(&points, num_variables, true);

        // Generate a proof for the given statement and witness
        let _ = params.prove(
            &mut prover_state,
            vec![Cow::from(vector)],
            vec![Cow::Owned(witness)],
            prove_linear_forms,
            Cow::Borrowed(evaluations.as_slice()),
        );

        // Reconstruct verifier's view of the transcript
        let proof = prover_state.proof();
        let mut verifier_state = VerifierState::new_std(&ds, &proof);
        let commitment = params.receive_commitment(&mut verifier_state).unwrap();

        // Verify the proof
        let final_claim = params
            .verify(&mut verifier_state, &[&commitment], &evaluations)
            .unwrap();
        final_claim
            .verify(
                linear_forms
                    .iter()
                    .map(|l| l.as_ref() as &dyn LinearForm<EF>),
            )
            .unwrap();
    }

    #[test]
    fn test_whir_1() {
        for folding_factor in [1, 2, 3, 4] {
            let num_variables = folding_factor..=3 * folding_factor;
            for num_variable in num_variables {
                for num_points in [0, 1, 2] {
                    for unique_decoding in [true, false] {
                        for pow_bits in [0, 5, 10] {
                            eprintln!();
                            dbg!(
                                folding_factor,
                                num_variable,
                                num_points,
                                unique_decoding,
                                pow_bits
                            );

                            make_whir_things(
                                num_variable,
                                folding_factor,
                                folding_factor,
                                num_points,
                                unique_decoding,
                                pow_bits,
                            );
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn test_fail() {
        make_whir_things(3, 2, 2, 0, false, 0);
    }

    #[test]
    fn test_whir_mixed_folding_factors() {
        let folding_factors = [1, 2, 3, 4];
        let num_points = [0, 1, 2];

        for initial_folding_factor in folding_factors {
            for folding_factor in folding_factors {
                if initial_folding_factor == folding_factor {
                    continue;
                }
                let n = std::cmp::max(initial_folding_factor, folding_factor);
                let num_variables = n..=3 * n;
                for num_variable in num_variables {
                    for num_points in num_points {
                        eprintln!();
                        dbg!(
                            initial_folding_factor,
                            folding_factor,
                            num_variable,
                            num_points,
                        );

                        make_whir_things(
                            num_variable,
                            initial_folding_factor,
                            folding_factor,
                            num_points,
                            false,
                            5,
                        );
                    }
                }
            }
        }
    }

    /// Test batch proving with multiple independent polynomials and statements.
    ///
    /// Creates N separate polynomials, commits to each independently, and uses RLC to batch-prove
    /// them together. This verifies the full lifecycle: commitment, batch proving, and verification.
    fn make_whir_batch_things(
        num_variables: usize,
        initial_folding_factor: usize,
        folding_factor: usize,
        num_points_per_poly: usize,
        num_vectors: usize,
        unique_decoding: bool,
        pow_bits: usize,
    ) {
        let num_coeffs = 1 << num_variables;
        let mut rng = ark_std::test_rng();

        let whir_params = ProtocolParameters {
            security_level: 32,
            pow_bits,
            initial_folding_factor,
            folding_factor,
            unique_decoding,
            starting_log_inv_rate: 1,
            batch_size: 1,
            hash_id: hash::SHA2,
        };

        let mut params = Config::new(1 << num_variables, &whir_params);
        params.disable_pow();
        eprintln!("{params}");

        // Create N different vectors
        let vectors: Vec<_> = (0..num_vectors)
            .map(|i| {
                // Different vectors: first is all 1s, second is all 2s, etc.
                vec![F::from((i + 1) as u64); num_coeffs]
            })
            .collect();
        let vec_refs = vectors.iter().map(|v| v.as_slice()).collect::<Vec<_>>();

        let points: Vec<_> = (0..num_points_per_poly)
            .map(|_| MultilinearPoint::rand(&mut rng, num_variables))
            .collect();

        let mut linear_forms: Vec<Box<dyn Evaluate<Basefield<EF>>>> = Vec::new();
        for point in &points {
            linear_forms.push(Box::new(MultilinearExtension {
                point: point.0.clone(),
            }));
        }
        linear_forms.push(Box::new(Covector {
            vector: ((0..1 << num_variables).map(EF::from).collect()),
        }));

        let evaluations = linear_forms
            .iter()
            .flat_map(|linear_form| {
                vec_refs
                    .iter()
                    .map(|vec| linear_form.evaluate(params.embedding(), vec))
            })
            .collect::<Vec<_>>();

        // Set up domain separator for batch proving
        let ds = DomainSeparator::protocol(&params)
            .session(&format!("Test at {}:{}", file!(), line!()))
            .instance(&Empty);
        let mut prover_state = ProverState::new_std(&ds);

        // Commit to each polynomial and generate witnesses
        let mut witnesses = Vec::new();
        for &vec in &vec_refs {
            let witness = params.commit(&mut prover_state, &[vec]);
            witnesses.push(witness);
        }

        let prove_linear_forms = build_prove_forms(&points, num_variables, true);

        // Batch prove all polynomials together
        let _ = params.prove(
            &mut prover_state,
            vectors
                .iter()
                .map(|v| Cow::Borrowed(v.as_slice()))
                .collect(),
            witnesses.into_iter().map(Cow::Owned).collect(),
            prove_linear_forms,
            Cow::Borrowed(evaluations.as_slice()),
        );

        // Reconstruct verifier's transcript view
        let proof = prover_state.proof();
        let mut verifier_state = VerifierState::new_std(&ds, &proof);

        let mut commitments = Vec::new();
        for _ in 0..num_vectors {
            let commitment = params.receive_commitment(&mut verifier_state).unwrap();
            commitments.push(commitment);
        }
        let commitment_refs = commitments.iter().collect::<Vec<_>>();

        // Verify the batched proof
        let final_claim = params
            .verify(&mut verifier_state, &commitment_refs, &evaluations)
            .unwrap();
        final_claim
            .verify(
                linear_forms
                    .iter()
                    .map(|l| l.as_ref() as &dyn LinearForm<EF>),
            )
            .unwrap();
    }

    #[test]
    fn test_whir_batch_1() {
        // Test with different configurations
        let folding_factors = [1, 2, 3, 4];
        let num_polynomials = [2, 3, 4];
        let num_points = [0, 1, 2];

        for initial_folding_factor in folding_factors {
            for folding_factor in folding_factors {
                let n = std::cmp::max(initial_folding_factor, folding_factor);
                // TODO: Batching with small number of variables..
                for num_variables in (initial_folding_factor + folding_factor)..=3 * n {
                    for num_polys in num_polynomials {
                        for num_points_per_poly in num_points {
                            eprintln!();
                            dbg!(
                                initial_folding_factor,
                                folding_factor,
                                num_variables,
                                num_polys,
                                num_points_per_poly,
                            );
                            make_whir_batch_things(
                                num_variables,
                                initial_folding_factor,
                                folding_factor,
                                num_points_per_poly,
                                num_polys,
                                false,
                                0, // pow_bits
                            );
                        }
                    }
                }
            }
        }
    }

    #[test]
    fn test_whir_batch_single_polynomial() {
        // Edge case: batch proving with just one polynomial should also work
        make_whir_batch_things(
            6, // num_variables
            2, // initial_folding_factor
            2, // folding_factor
            2, // num_points_per_poly
            1, // num_polynomials (single!)
            false, 0,
        );
    }

    /// Test that batch verification rejects proofs with mismatched polynomials.
    ///
    /// This security test verifies that the cross-term commitment prevents the prover from
    /// using a different polynomial than what was committed. The prover commits to poly2 but
    /// attempts to use poly_wrong for evaluation, which should cause verification to fail.
    #[test]
    #[cfg_attr(feature = "verifier_panics", should_panic)]
    #[cfg_attr(
        debug_assertions,
        ignore = "debug_assert in prover panics on intentionally invalid input"
    )]
    fn test_whir_batch_rejects_invalid_constraint() {
        // Setup parameters
        let num_variables = 4;
        let initial_folding_factor = 2;
        let folding_factor = 2;
        let num_polynomials = 2;
        let num_coeffs = 1 << num_variables;
        let mut rng = ark_std::test_rng();

        let whir_params = ProtocolParameters {
            security_level: 32,
            pow_bits: 0,
            initial_folding_factor,
            folding_factor,
            unique_decoding: false,
            starting_log_inv_rate: 1,
            batch_size: 1,
            hash_id: hash::SHA2,
        };

        let mut params = Config::<Basefield<EF>>::new(1 << num_variables, &whir_params);
        params.disable_pow();

        // Create test vectors
        let vec1 = vec![F::ONE; num_coeffs];
        let vec2 = vec![F::from(2u64); num_coeffs];
        let vec_wrong = vec![F::from(999u64); num_coeffs];

        let constraint_points: Vec<_> = (0..2)
            .map(|_| MultilinearPoint::rand(&mut rng, num_variables))
            .collect();

        let linear_forms: [Box<dyn Evaluate<Basefield<EF>>>; 2] = [
            Box::new(MultilinearExtension {
                point: constraint_points[0].0.clone(),
            }),
            Box::new(MultilinearExtension {
                point: constraint_points[1].0.clone(),
            }),
        ];
        let evaluations = linear_forms
            .iter()
            .flat_map(|weights| {
                [&vec1, &vec_wrong].map(|v| weights.evaluate(params.embedding(), v))
            })
            .collect::<Vec<_>>();

        let ds = DomainSeparator::protocol(&params)
            .session(&format!("Test at {}:{}", file!(), line!()))
            .instance(&Empty);
        let mut prover_state = ProverState::new_std(&ds);

        let witness1 = params.commit(&mut prover_state, &[&vec1]);
        let witness2 = params.commit(&mut prover_state, &[&vec2]);

        let prove_linear_forms = build_prove_forms(&constraint_points, num_variables, false);

        // Generate proof with mismatched polynomials
        let _ = params.prove(
            &mut prover_state,
            vec![Cow::Borrowed(vec1.as_slice()), Cow::from(vec_wrong)],
            vec![Cow::Owned(witness1), Cow::Owned(witness2)],
            prove_linear_forms,
            Cow::Borrowed(evaluations.as_slice()),
        );

        // Verification should fail because the cross-terms don't match the commitment
        let proof = prover_state.proof();
        let mut verifier_state = VerifierState::new_std(&ds, &proof);

        let mut commitments = Vec::new();
        for _ in 0..num_polynomials {
            let parsed_commitment = params.receive_commitment(&mut verifier_state).unwrap();
            commitments.push(parsed_commitment);
        }

        let final_claim = params
            .verify(
                &mut verifier_state,
                &[&commitments[0], &commitments[1]],
                &evaluations,
            )
            .unwrap();
        let verifier_result = final_claim.verify(
            linear_forms
                .iter()
                .map(|l| l.as_ref() as &dyn LinearForm<EF>),
        );
        assert!(
            verifier_result.is_err(),
            "Verifier should reject mismatched polynomial"
        );
    }

    /// Test batch proving with batch_size > 1 (multiple polynomials per commitment).
    ///
    /// This tests the case where each commitment contains multiple stacked polynomials
    /// (e.g., masked witness + random blinding for ZK), and we batch-prove multiple
    /// such commitments together.
    ///
    /// This was a regression test for a bug where the RLC combination of stacked
    /// leaf answers was incorrect when batch_size > 1.
    #[allow(clippy::too_many_arguments)]
    fn make_whir_batch_with_batch_size(
        num_variables: usize,
        initial_folding_factor: usize,
        folding_factor: usize,
        num_points_per_poly: usize,
        num_witnesses: usize,
        batch_size: usize,
        unique_decoding: bool,
        pow_bits: usize,
    ) {
        let num_coeffs = 1 << num_variables;
        let mut rng = ark_std::test_rng();

        let whir_params = ProtocolParameters {
            security_level: 32,
            pow_bits,
            initial_folding_factor,
            folding_factor,
            unique_decoding,
            starting_log_inv_rate: 1,
            batch_size, // KEY: batch_size > 1
            hash_id: hash::SHA2,
        };

        let mut params = Config::<Basefield<EF>>::new(1 << num_variables, &whir_params);
        params.disable_pow();

        // Create polynomials for each witness
        // Each witness will contain batch_size polynomials committed together
        let all_vectors: Vec<Vec<F>> = (0..num_witnesses * batch_size)
            .map(|i| vec![F::from((i + 1) as u64); num_coeffs])
            .collect::<Vec<_>>();
        let vec_refs = all_vectors.iter().map(|p| p.as_slice()).collect::<Vec<_>>();

        let points: Vec<_> = (0..num_points_per_poly)
            .map(|_| MultilinearPoint::rand(&mut rng, num_variables))
            .collect();

        let mut linear_forms: Vec<Box<dyn Evaluate<Basefield<EF>>>> = Vec::new();
        for point in &points {
            linear_forms.push(Box::new(MultilinearExtension {
                point: point.0.clone(),
            }));
        }
        linear_forms.push(Box::new(Covector {
            vector: (0..1 << num_variables).map(EF::from).collect(),
        }));

        let evaluations = linear_forms
            .iter()
            .flat_map(|linear_form| {
                vec_refs
                    .iter()
                    .map(|vec| linear_form.evaluate(params.embedding(), vec))
            })
            .collect::<Vec<_>>();

        // Set up domain separator
        let ds = DomainSeparator::protocol(&params)
            .session(&format!("Test at {}:{}", file!(), line!()))
            .instance(&Empty);
        let mut prover_state = ProverState::new_std(&ds);

        // Commit using commit_batch (stacks batch_size polynomials per witness)
        let mut witnesses = Vec::new();
        for witness_polys in vec_refs.chunks(batch_size) {
            let witness = params.commit(&mut prover_state, witness_polys);
            witnesses.push(witness);
        }

        let prove_linear_forms = build_prove_forms(&points, num_variables, true);

        // Batch prove all witnesses together
        let _ = params.prove(
            &mut prover_state,
            all_vectors
                .iter()
                .map(|v| Cow::Borrowed(v.as_slice()))
                .collect(),
            witnesses.into_iter().map(Cow::Owned).collect(),
            prove_linear_forms,
            Cow::Borrowed(evaluations.as_slice()),
        );

        // Verify
        let proof = prover_state.proof();
        let mut verifier_state = VerifierState::new_std(&ds, &proof);

        let mut commitments = Vec::new();
        for _ in 0..num_witnesses {
            let commitment = params.receive_commitment(&mut verifier_state).unwrap();
            commitments.push(commitment);
        }
        let commitment_refs = commitments.iter().collect::<Vec<_>>();

        let final_claim = params
            .verify(&mut verifier_state, &commitment_refs, &evaluations)
            .unwrap();
        final_claim
            .verify(
                linear_forms
                    .iter()
                    .map(|l| l.as_ref() as &dyn LinearForm<EF>),
            )
            .unwrap();
    }

    #[test]
    fn test_whir_batch_with_batch_size_2() {
        // This is the key regression test for the batch_size > 1 bug
        let batch_sizes = [2, 3];
        let num_witnesses = [2, 3];
        let folding_factors = [2, 3];

        for batch_size in batch_sizes {
            for num_witness in num_witnesses {
                for folding_factor in folding_factors {
                    make_whir_batch_with_batch_size(
                        folding_factor * 2, // num_variables
                        folding_factor,
                        folding_factor,
                        1, // num_points_per_poly
                        num_witness,
                        batch_size,
                        false,
                        0, // pow_bits
                    );
                }
            }
        }
    }

    fn random_vector(num_coefficients: usize) -> Vec<F> {
        let mut store = Vec::<F>::with_capacity(num_coefficients);
        let mut rng = ark_std::rand::thread_rng();
        (0..num_coefficients).for_each(|_| store.push(F::rand(&mut rng)));
        store
    }

    /// Run a complete WHIR proof lifecycle: commit, prove, and verify.
    fn make_batched_whir_things(
        batch_size: usize,
        num_variables: usize,
        initial_folding_factor: usize,
        folding_factor: usize,
        num_points: usize,
        unique_decoding: bool,
        pow_bits: usize,
    ) {
        eprintln!("\n---------------------");
        eprintln!("Test parameters: ");
        eprintln!("  num_vectors     : {batch_size}");
        eprintln!("  num_variables   : {num_variables}");
        eprintln!("  initial_folding : {initial_folding_factor}");
        eprintln!("  folding_factor  : {folding_factor}");
        eprintln!("  num_points      : {num_points:?}");
        eprintln!("  unique_decoding : {unique_decoding:?}");
        eprintln!("  pow_bits        : {pow_bits}");

        // Number of coefficients in the multilinear polynomial (2^num_variables)
        let num_coeffs = 1 << num_variables;

        // Randomness source
        let mut rng = ark_std::test_rng();

        // Configure the WHIR protocol parameters
        let whir_params = ProtocolParameters {
            security_level: 32,
            pow_bits,
            initial_folding_factor,
            folding_factor,
            unique_decoding,
            starting_log_inv_rate: 1,
            batch_size,
            hash_id: hash::SHA2,
        };

        // Build global configuration from multivariate + protocol parameters
        let mut params = Config::new(1 << num_variables, &whir_params);
        params.disable_pow();

        let vectors: Vec<Vec<F>> = (0..batch_size).map(|_| random_vector(num_coeffs)).collect();
        let vec_refs = vectors.iter().map(|v| v.as_slice()).collect::<Vec<_>>();

        // Generate `num_points` random points in the multilinear domain
        let points: Vec<_> = (0..num_points)
            .map(|_| MultilinearPoint::rand(&mut rng, num_variables))
            .collect();

        // Define the Fiat-Shamir IOPattern for committing and proving
        let ds = DomainSeparator::protocol(&params)
            .session(&format!("Test at {}:{}", file!(), line!()))
            .instance(&Empty);

        // Initialize the Merlin transcript from the domain separator
        let mut prover_state = ProverState::new_std(&ds);

        // Create a commitment to the polynomial and generate auxiliary witness data
        let batched_witness = params.commit(&mut prover_state, &vec_refs);

        // Create a weights matrix and evaluations for each polynomial
        let mut linear_forms: Vec<Box<dyn Evaluate<Basefield<F>>>> = Vec::new();
        for point in &points {
            linear_forms.push(Box::new(MultilinearExtension {
                point: point.0.clone(),
            }));
        }
        linear_forms.push(Box::new(Covector {
            vector: (0..1 << num_variables).map(F::from).collect(),
        }));
        let values = linear_forms
            .iter()
            .flat_map(|linear_form| {
                vec_refs
                    .iter()
                    .map(|vec| linear_form.evaluate(params.embedding(), vec))
            })
            .collect::<Vec<_>>();

        let prove_linear_forms = build_prove_forms(&points, num_variables, true);

        // Generate a proof for the given statement and witness
        let weights_dyn_refs = linear_forms
            .iter()
            .map(|w| w.as_ref() as &dyn LinearForm<F>)
            .collect::<Vec<_>>();
        let _ = params.prove(
            &mut prover_state,
            vectors
                .iter()
                .map(|v| Cow::Borrowed(v.as_slice()))
                .collect(),
            vec![Cow::Owned(batched_witness)],
            prove_linear_forms,
            Cow::Borrowed(values.as_slice()),
        );

        // Reconstruct verifier's view of the transcript using the IOPattern and prover's data
        let proof = prover_state.proof();
        let mut verifier_state = VerifierState::new_std(&ds, &proof);

        let commitment = params.receive_commitment(&mut verifier_state).unwrap();

        // Verify that the generated proof satisfies the statement
        params
            .verify(&mut verifier_state, &[&commitment], &values)
            .unwrap()
            .verify(weights_dyn_refs)
            .unwrap();
    }

    #[test]
    fn test_batched_whir() {
        let folding_factors = [1, 4];
        let unique_decoding_options = [false, true];
        let num_points = [0, 2];
        let pow_bits = [0, 10];

        for folding_factor in folding_factors {
            let num_variables = (2 * folding_factor)..=3 * folding_factor;
            for num_variable in num_variables {
                for num_points in num_points {
                    for unique_decoding in unique_decoding_options {
                        for pow_bits in pow_bits {
                            for batch_size in 1..=4 {
                                make_batched_whir_things(
                                    batch_size,
                                    num_variable,
                                    folding_factor,
                                    folding_factor,
                                    num_points,
                                    unique_decoding,
                                    pow_bits,
                                );
                            }
                        }
                    }
                }
            }
        }
    }
}