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
//! Shared round execution logic for the WHIR protocol.
//!
//! These free functions implement the round body for rounds 1+ and the final
//! round. Both the base WHIR prover/verifier and the zkWHIR 2.0 prover/verifier
//! call these functions to avoid duplicating the round loop.

use ark_ff::FftField;
use ark_std::rand::{CryptoRng, RngCore};

use super::RoundConfig;
use crate::{
    algebra::{
        dot,
        embedding::Identity,
        linear_form::{Evaluate, UnivariateEvaluation},
        MultilinearPoint,
    },
    hash::Hash,
    protocols::{geometric_challenge::geometric_challenge, irs_commit, proof_of_work, sumcheck},
    transcript::{
        codecs::U64, Codec, Decoding, DuplexSpongeInterface, ProverMessage, ProverState,
        VerificationResult, VerifierState,
    },
    utils::zip_strict,
    verify,
};

/// Mutable sumcheck accumulation state threaded through WHIR prover rounds.
#[derive(Debug)]
pub struct SumcheckState<'a, F> {
    pub(crate) vector: &'a mut Vec<F>,
    pub(crate) covector: &'a mut Vec<F>,
    pub(crate) the_sum: &'a mut F,
}

/// Configuration for the final WHIR round (sumcheck + proof-of-work).
#[derive(Debug)]
pub struct FinalRoundConfig<'a, F: FftField> {
    pub(crate) sumcheck: &'a sumcheck::Config<F>,
    pub(crate) pow: &'a proof_of_work::Config,
}

/// Result of a single verifier round (rounds 1+).
#[must_use]
#[derive(Debug)]
pub struct VerifyRoundResult<F: FftField> {
    pub commitment: irs_commit::Commitment<F>,
    pub in_domain: irs_commit::Evaluations<F>,
    pub stir_rlc_coeffs: Vec<F>,
    pub stir_challenges: Vec<UnivariateEvaluation<F>>,
    pub folding_randomness: MultilinearPoint<F>,
}

/// Result of a single prover round (rounds 1+).
#[must_use]
#[derive(Debug)]
pub struct ProveRoundResult<F: FftField> {
    pub witness: irs_commit::Witness<F, F>,
    pub in_domain: irs_commit::Evaluations<F>,
    pub folding_randomness: MultilinearPoint<F>,
}

/// Single prover round body for rounds 1+ of the WHIR protocol.
///
/// Commits the current vector, opens the previous round's witness, accumulates
/// STIR constraints (OOD + in-domain), and runs the round's sumcheck.
fn prove_round<F, H, R>(
    round_config: &RoundConfig<F>,
    prev_round_config: &RoundConfig<F>,
    prover_state: &mut ProverState<H, R>,
    state: &mut SumcheckState<'_, F>,
    prev_witness: &irs_commit::Witness<F, F>,
    folding_randomness: &MultilinearPoint<F>,
) -> ProveRoundResult<F>
where
    F: FftField + Codec<[H::U]>,
    H: DuplexSpongeInterface,
    R: RngCore + CryptoRng,
    [u8; 32]: Decoding<[H::U]>,
    U64: Codec<[H::U]>,
    u8: Decoding<[H::U]>,
    Hash: ProverMessage<[H::U]>,
{
    let new_witness = round_config
        .irs_committer
        .commit(prover_state, &[state.vector.as_slice()]);
    round_config.pow.prove(prover_state);

    let in_domain = prev_round_config
        .irs_committer
        .open(prover_state, &[prev_witness]);

    let stir_challenges: Vec<_> = new_witness
        .out_of_domain()
        .evaluators(round_config.initial_size())
        .chain(in_domain.evaluators(round_config.initial_size()))
        .collect();
    let stir_evaluations: Vec<F> = new_witness
        .out_of_domain()
        .values(&[F::ONE])
        .chain(in_domain.values(&folding_randomness.eq_weights()))
        .collect();

    let stir_rlc_coeffs: Vec<F> = geometric_challenge(prover_state, stir_challenges.len());
    UnivariateEvaluation::accumulate_many(&stir_challenges, state.covector, &stir_rlc_coeffs);
    *state.the_sum += dot(&stir_rlc_coeffs, &stir_evaluations);
    debug_assert_eq!(dot(state.vector, state.covector), *state.the_sum);

    let new_folding =
        round_config
            .sumcheck
            .prove(prover_state, state.vector, state.covector, state.the_sum);
    debug_assert_eq!(dot(state.vector, state.covector), *state.the_sum);

    ProveRoundResult {
        witness: new_witness,
        in_domain,
        folding_randomness: new_folding,
    }
}

/// Final prover round.
///
/// Sends the (small) final folded vector directly, runs PoW, opens the last
/// commitment, and runs the final sumcheck.
fn prove_final_round<F, H, R>(
    final_config: &FinalRoundConfig<'_, F>,
    last_round_config: &RoundConfig<F>,
    prover_state: &mut ProverState<H, R>,
    state: &mut SumcheckState<'_, F>,
    prev_witness: &irs_commit::Witness<F, F>,
) -> (irs_commit::Evaluations<F>, MultilinearPoint<F>)
where
    F: FftField + Codec<[H::U]>,
    H: DuplexSpongeInterface,
    R: RngCore + CryptoRng,
    [u8; 32]: Decoding<[H::U]>,
    U64: Codec<[H::U]>,
    u8: Decoding<[H::U]>,
    Hash: ProverMessage<[H::U]>,
{
    assert_eq!(state.vector.len(), final_config.sumcheck.initial_size);
    for coeff in state.vector.iter() {
        prover_state.prover_message(coeff);
    }

    final_config.pow.prove(prover_state);

    let in_domain = last_round_config
        .irs_committer
        .open(prover_state, &[prev_witness]);

    let final_folding =
        final_config
            .sumcheck
            .prove(prover_state, state.vector, state.covector, state.the_sum);

    (in_domain, final_folding)
}

/// Single verifier round body for rounds 1+.
///
/// Receives commitment, verifies PoW, opens the previous round's commitment,
/// accumulates STIR constraints, and runs the round's sumcheck.
fn verify_round<F, H>(
    round_config: &RoundConfig<F>,
    prev_round_config: &RoundConfig<F>,
    verifier_state: &mut VerifierState<'_, H>,
    the_sum: &mut F,
    prev_commitment: &irs_commit::Commitment<F>,
    folding_randomness: &MultilinearPoint<F>,
) -> VerificationResult<VerifyRoundResult<F>>
where
    F: FftField + Codec<[H::U]>,
    H: DuplexSpongeInterface,
    u8: Decoding<[H::U]>,
    [u8; 32]: Decoding<[H::U]>,
    U64: Codec<[H::U]>,
    Hash: ProverMessage<[H::U]>,
{
    let commitment = round_config
        .irs_committer
        .receive_commitment(verifier_state)?;
    round_config.pow.verify(verifier_state)?;

    let in_domain = prev_round_config
        .irs_committer
        .verify(verifier_state, &[prev_commitment])?;

    let stir_challenges: Vec<UnivariateEvaluation<F>> = commitment
        .out_of_domain()
        .evaluators(round_config.initial_size())
        .chain(in_domain.evaluators(round_config.initial_size()))
        .collect();
    let stir_evaluations: Vec<F> = commitment
        .out_of_domain()
        .values(&[F::ONE])
        .chain(in_domain.values(&folding_randomness.eq_weights()))
        .collect();

    let stir_rlc_coeffs: Vec<F> = geometric_challenge(verifier_state, stir_challenges.len());
    *the_sum += dot(&stir_rlc_coeffs, &stir_evaluations);

    let folding_randomness = round_config.sumcheck.verify(verifier_state, the_sum)?;

    Ok(VerifyRoundResult {
        commitment,
        in_domain,
        stir_rlc_coeffs,
        stir_challenges,
        folding_randomness,
    })
}

/// Final verifier round.
///
/// Receives the final vector, verifies PoW, opens the last commitment, checks
/// in-domain evaluations directly, and runs the final sumcheck.
///
/// Returns `(final_vector, in_domain, final_folding_randomness)`.
fn verify_final_round<F, H>(
    final_config: &FinalRoundConfig<'_, F>,
    last_round_config: &RoundConfig<F>,
    verifier_state: &mut VerifierState<'_, H>,
    the_sum: &mut F,
    prev_commitment: &irs_commit::Commitment<F>,
    folding_randomness: &MultilinearPoint<F>,
) -> VerificationResult<(Vec<F>, irs_commit::Evaluations<F>, MultilinearPoint<F>)>
where
    F: FftField + Codec<[H::U]>,
    H: DuplexSpongeInterface,
    u8: Decoding<[H::U]>,
    [u8; 32]: Decoding<[H::U]>,
    U64: Codec<[H::U]>,
    Hash: ProverMessage<[H::U]>,
{
    let final_vector: Vec<F> =
        verifier_state.prover_messages_vec(final_config.sumcheck.initial_size)?;

    final_config.pow.verify(verifier_state)?;

    let in_domain = last_round_config
        .irs_committer
        .verify(verifier_state, &[prev_commitment])?;

    for (weight, eval) in zip_strict(
        in_domain.evaluators(final_vector.len()),
        in_domain.values(&folding_randomness.eq_weights()),
    ) {
        verify!(weight.evaluate(&Identity::<F>::new(), &final_vector) == eval);
    }

    let final_folding = final_config.sumcheck.verify(verifier_state, the_sum)?;

    Ok((final_vector, in_domain, final_folding))
}

/// Result of running remaining prover rounds (rounds 1..N + final round).
#[must_use]
#[derive(Debug)]
pub struct ProveRemainingResult<F> {
    /// In-domain points from the first opening after round 0.
    pub first_in_domain_points: Vec<F>,
    /// Folding randomness from each remaining round + final round.
    pub round_folding_randomness: Vec<MultilinearPoint<F>>,
}

/// Run the remaining prover rounds (1..N) plus the final round.
///
/// After the caller handles round 0 (which may differ between base WHIR and
/// zkWHIR 2.0), this function executes all subsequent fold-commit-sumcheck
/// rounds and the final direct-send round.
pub fn prove_remaining_rounds<F, H, R>(
    round_configs: &[RoundConfig<F>],
    final_config: &FinalRoundConfig<'_, F>,
    prover_state: &mut ProverState<H, R>,
    state: &mut SumcheckState<'_, F>,
    round0_witness: irs_commit::Witness<F, F>,
    round0_folding: &MultilinearPoint<F>,
) -> ProveRemainingResult<F>
where
    F: FftField + Codec<[H::U]>,
    H: DuplexSpongeInterface,
    R: RngCore + CryptoRng,
    [u8; 32]: Decoding<[H::U]>,
    U64: Codec<[H::U]>,
    u8: Decoding<[H::U]>,
    Hash: ProverMessage<[H::U]>,
{
    assert!(!round_configs.is_empty());
    let mut prev_witness = round0_witness;
    let mut folding_randomness = round0_folding.clone();
    let mut first_in_domain_points = Vec::new();
    let mut round_folding_randomness = Vec::new();

    for (i, window) in round_configs.windows(2).enumerate() {
        let (prev_rc, rc) = (&window[0], &window[1]);
        let ProveRoundResult {
            witness,
            in_domain,
            folding_randomness: new_folding,
        } = prove_round(
            rc,
            prev_rc,
            prover_state,
            state,
            &prev_witness,
            &folding_randomness,
        );
        if i == 0 {
            first_in_domain_points = in_domain.points;
        }
        folding_randomness = new_folding.clone();
        round_folding_randomness.push(new_folding);
        prev_witness = witness;
    }

    let last_rc = round_configs.last().unwrap();
    let (final_in_domain, final_folding) =
        prove_final_round(final_config, last_rc, prover_state, state, &prev_witness);
    if round_configs.len() == 1 {
        first_in_domain_points = final_in_domain.points;
    }
    round_folding_randomness.push(final_folding);

    ProveRemainingResult {
        first_in_domain_points,
        round_folding_randomness,
    }
}

/// Result of running remaining verifier rounds (rounds 1..N + final round).
#[must_use]
#[derive(Debug)]
pub struct VerifyRemainingResult<F: FftField> {
    /// The final folded vector sent by the prover.
    pub final_vector: Vec<F>,
    /// In-domain evaluations from the first opening after round 0.
    pub first_in_domain: irs_commit::Evaluations<F>,
    /// STIR constraints accumulated during rounds 1..N.
    pub round_constraints: Vec<(Vec<F>, Vec<UnivariateEvaluation<F>>)>,
    /// Folding randomness from rounds 1..N (excludes the final sumcheck).
    pub round_folding_randomness: Vec<MultilinearPoint<F>>,
    /// Folding randomness from the final sumcheck. Stored separately because
    /// callers need it both in the evaluation point AND for the MLE check.
    pub final_sumcheck_randomness: MultilinearPoint<F>,
}

/// Run the remaining verifier rounds (1..N) plus the final round.
///
/// After the caller handles round 0, this function verifies all subsequent
/// fold-commit-sumcheck rounds and the final direct-send round.
pub fn verify_remaining_rounds<F, H>(
    round_configs: &[RoundConfig<F>],
    final_config: &FinalRoundConfig<'_, F>,
    verifier_state: &mut VerifierState<'_, H>,
    the_sum: &mut F,
    round0_commitment: &irs_commit::Commitment<F>,
    round0_folding: &MultilinearPoint<F>,
) -> VerificationResult<VerifyRemainingResult<F>>
where
    F: FftField + Codec<[H::U]>,
    H: DuplexSpongeInterface,
    u8: Decoding<[H::U]>,
    [u8; 32]: Decoding<[H::U]>,
    U64: Codec<[H::U]>,
    Hash: ProverMessage<[H::U]>,
{
    assert!(!round_configs.is_empty());
    let mut prev_commitment = round0_commitment.clone();
    let mut folding_randomness = round0_folding.clone();
    let mut first_in_domain = None;
    let mut round_constraints = Vec::new();
    let mut round_folding_randomness = Vec::new();

    for (i, window) in round_configs.windows(2).enumerate() {
        let (prev_rc, rc) = (&window[0], &window[1]);
        let result = verify_round(
            rc,
            prev_rc,
            verifier_state,
            the_sum,
            &prev_commitment,
            &folding_randomness,
        )?;
        if i == 0 {
            first_in_domain = Some(result.in_domain);
        }
        round_constraints.push((result.stir_rlc_coeffs, result.stir_challenges));
        let new_folding = result.folding_randomness;
        folding_randomness = new_folding.clone();
        round_folding_randomness.push(new_folding);
        prev_commitment = result.commitment;
    }

    let last_rc = round_configs.last().unwrap();
    let (final_vector, final_in_domain, final_sumcheck_randomness) = verify_final_round(
        final_config,
        last_rc,
        verifier_state,
        the_sum,
        &prev_commitment,
        &folding_randomness,
    )?;

    let first_in_domain = first_in_domain.unwrap_or(final_in_domain);

    Ok(VerifyRemainingResult {
        final_vector,
        first_in_domain,
        round_constraints,
        round_folding_randomness,
        final_sumcheck_randomness,
    })
}