p3-binary-pcs 0.9.0-rc.1

Multilinear polynomial commitment over Plonky3 binary tower fields, via additive-domain proximity
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
//! Supported parameter profiles, one per proximity regime, derived from the soundness analysis.

use alloc::vec::Vec;

use p3_challenger::{FieldChallenger, GrindingChallenger};
use p3_field::{ExtensionField, Field};
use p3_whir::{
    FoldingFactor, ProtocolParameters, SecurityAssumption, WhirConfig, WhirConfigError, WhirDomain,
};

use crate::whir::error::ProfileError;

/// Widest candidate space a grinding search enumerates, in bits.
///
/// The search counts with a sixty-four bit index, so no alphabet offers more than that.
const GRIND_SEARCH_BITS: usize = 64;

/// Headroom a grinding search keeps below the candidate space, so an exhaustive one rarely fails.
const GRIND_MARGIN_BITS: usize = 8;

/// Grinding one witness of the alphabet can carry.
fn grinding_ceiling<F: Field>() -> usize {
    F::bits()
        .min(GRIND_SEARCH_BITS)
        .saturating_sub(GRIND_MARGIN_BITS)
}

/// Raise the grinding allowance to a figure the analysis asks for.
///
/// # Errors
///
/// Returns an error when one witness of the alphabet cannot carry the figure.
fn raise_allowance<F: Field>(required: usize) -> Result<usize, ProfileError> {
    let ceiling = grinding_ceiling::<F>();
    if required > ceiling {
        return Err(ProfileError::Grinding { required, ceiling });
    }
    Ok(required)
}

/// A named parameter profile for one proximity regime.
///
/// The regime, the code rate and the folding width are chosen by the caller.
///
/// Query counts, samples and grinding are all derived from the soundness analysis.
///
/// Nothing here defaults to a regime: each one is a constructor of its own.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct BinaryWhirProfile {
    /// Proximity regime the derivation is graded under.
    assumption: SecurityAssumption,
    /// Target in bits for every error term the schedule charges.
    security_level: usize,
    /// Base-two logarithm of the inverse rate of the first codeword.
    starting_log_inv_rate: usize,
    /// Variables eliminated by each round.
    folding_factor: usize,
}

impl BinaryWhirProfile {
    /// The regime that assumes only unique decoding, and conjectures nothing.
    #[must_use]
    pub const fn unique_decoding(
        security_level: usize,
        starting_log_inv_rate: usize,
        folding_factor: usize,
    ) -> Self {
        Self {
            assumption: SecurityAssumption::UniqueDecoding,
            security_level,
            starting_log_inv_rate,
            folding_factor,
        }
    }

    /// The regime that decodes to the Johnson radius, where list decoding is proven.
    ///
    /// Mutual correlated agreement at this radius is a theorem, resting on no conjecture.
    #[must_use]
    pub const fn proven_list_decoding(
        security_level: usize,
        starting_log_inv_rate: usize,
        folding_factor: usize,
    ) -> Self {
        Self {
            assumption: SecurityAssumption::JohnsonBound,
            security_level,
            starting_log_inv_rate,
            folding_factor,
        }
    }

    /// The regime that decodes to capacity, which no theorem supports.
    ///
    /// Every additive binary domain refuses it, because the assumption is refuted there.
    #[must_use]
    pub const fn conjectural(
        security_level: usize,
        starting_log_inv_rate: usize,
        folding_factor: usize,
    ) -> Self {
        Self {
            assumption: SecurityAssumption::CapacityBound,
            security_level,
            starting_log_inv_rate,
            folding_factor,
        }
    }

    /// The proximity regime this profile is graded under.
    #[must_use]
    pub const fn assumption(&self) -> SecurityAssumption {
        self.assumption
    }

    /// The target in bits for every error term.
    #[must_use]
    pub const fn security_level(&self) -> usize {
        self.security_level
    }

    /// Derive the schedule this profile stands for over a concrete evaluation domain.
    ///
    /// Grinding is raised to exactly what the analysis demands, rather than guessed at.
    ///
    /// The domain vetoes a regime whose distance assumption fails for its code.
    ///
    /// # Errors
    ///
    /// Returns an error when the derived schedule opens no position.
    ///
    /// Returns an error when the domain refuses the regime.
    ///
    /// Returns an error when the analysis demands more grinding than one witness can carry.
    ///
    /// Returns an error when the schedule itself is infeasible.
    pub fn config<EF, F, Challenger, Domain>(
        &self,
        num_variables: usize,
        domain: &Domain,
    ) -> Result<WhirConfig<EF, F, Challenger>, ProfileError>
    where
        F: Field,
        EF: ExtensionField<F>,
        Challenger: FieldChallenger<F> + GrindingChallenger<Witness = F>,
        Domain: WhirDomain<F, EF>,
    {
        let mut pow_bits = 0;
        loop {
            let parameters = ProtocolParameters {
                starting_log_inv_rate: self.starting_log_inv_rate,
                round_log_inv_rates: Vec::new(),
                folding_factor: FoldingFactor::Constant(self.folding_factor),
                soundness_type: self.assumption,
                security_level: self.security_level,
                pow_bits,
            };
            match WhirConfig::new_with_domain(num_variables, parameters, domain) {
                Ok(config) => return Ok(config),
                // The analysis reports exactly how much grinding the gap needs.
                // Raising the allowance to that figure is the derivation, not a search.
                Err(WhirConfigError::PowBitsExceedBudget { required, .. })
                    if required > pow_bits =>
                {
                    pow_bits = raise_allowance::<F>(required)?;
                }
                // The schedule refuses a run that opens nothing, whichever route built it.
                //
                // Naming it here keeps callers from reading a bare scheduling failure.
                Err(WhirConfigError::ZeroQueries) => return Err(ProfileError::ZeroQueries),
                Err(error) => return Err(ProfileError::Schedule(error)),
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use p3_binary_field::{BinaryChallenger, BinaryField32, BinaryField64, BinaryField128};
    use p3_challenger::{GrindingChallenger, HashChallenger};
    use p3_keccak::Keccak256Hash;
    use p3_whir::{FoldingFactorError, SecurityAssumption, WhirConfigError};

    use super::{BinaryWhirProfile, grinding_ceiling, raise_allowance};
    use crate::test_util::MyChallenger;
    use crate::whir::error::ProfileError;
    use crate::whir::{BinaryWhirDomain, BooleanWhirDomain, ProofShape};

    type EF = BinaryField128;
    type NarrowChallenger = BinaryChallenger<BinaryField32, HashChallenger<u8, Keccak256Hash, 32>>;

    const NUM_VARIABLES: usize = 9;
    const SECURITY_LEVEL: usize = 100;
    const LOG_INV_RATE: usize = 2;
    const FOLDING: usize = 3;

    #[test]
    fn a_schedule_that_opens_no_position_is_refused() {
        // Invariant: a schedule must open at least one position.
        //
        // Queries alone test proximity, so a schedule opening none accepts any codeword.
        //
        // Crediting grinding with the whole target derives exactly that.
        //
        // This profile reaches it with a zero target, which credits the whole of nothing.
        let domain = BooleanWhirDomain::default();
        let profile = BinaryWhirProfile::proven_list_decoding(0, LOG_INV_RATE, FOLDING);
        assert!(matches!(
            profile
                .config::<EF, EF, MyChallenger, _>(NUM_VARIABLES, &domain)
                .err(),
            Some(ProfileError::ZeroQueries)
        ));

        // The floor is on the derived count, so a target that does buy queries is untouched.
        let real = BinaryWhirProfile::proven_list_decoding(SECURITY_LEVEL, LOG_INV_RATE, FOLDING)
            .config::<EF, EF, MyChallenger, _>(NUM_VARIABLES, &domain)
            .unwrap();
        assert!(real.terminal().num_queries > 0);
    }

    #[test]
    fn a_profile_that_folds_nothing_is_refused_by_the_schedule() {
        // Invariant: a round must eliminate at least one variable.
        //
        // This one needs no check of our own, since the schedule already refuses it.
        //
        // The test pins that refusal, so nobody adds a redundant guard here later.
        let domain = BooleanWhirDomain::default();
        let profile = BinaryWhirProfile::proven_list_decoding(SECURITY_LEVEL, LOG_INV_RATE, 0);
        assert!(matches!(
            profile
                .config::<EF, EF, MyChallenger, _>(NUM_VARIABLES, &domain)
                .err(),
            Some(ProfileError::Schedule(WhirConfigError::FoldingFactor(
                FoldingFactorError::ZeroFactor
            )))
        ));
    }

    #[test]
    fn each_regime_is_reached_by_its_own_constructor() {
        assert_eq!(
            BinaryWhirProfile::unique_decoding(SECURITY_LEVEL, LOG_INV_RATE, FOLDING).assumption(),
            SecurityAssumption::UniqueDecoding
        );
        assert_eq!(
            BinaryWhirProfile::proven_list_decoding(SECURITY_LEVEL, LOG_INV_RATE, FOLDING)
                .assumption(),
            SecurityAssumption::JohnsonBound
        );
        assert_eq!(
            BinaryWhirProfile::conjectural(SECURITY_LEVEL, LOG_INV_RATE, FOLDING).assumption(),
            SecurityAssumption::CapacityBound
        );
    }

    #[test]
    fn decoding_further_buys_fewer_opened_positions() {
        let domain = BooleanWhirDomain::default();
        let shape = |profile: BinaryWhirProfile| {
            ProofShape::of(
                &profile
                    .config::<EF, EF, MyChallenger, _>(NUM_VARIABLES, &domain)
                    .unwrap(),
                1,
            )
        };
        let unique = shape(BinaryWhirProfile::unique_decoding(
            SECURITY_LEVEL,
            LOG_INV_RATE,
            FOLDING,
        ));
        let proven = shape(BinaryWhirProfile::proven_list_decoding(
            SECURITY_LEVEL,
            LOG_INV_RATE,
            FOLDING,
        ));
        assert!(proven.stir_queries < unique.stir_queries);
    }

    #[test]
    fn the_conjectural_regime_is_refused_by_the_additive_domain() {
        let refused = BinaryWhirProfile::conjectural(SECURITY_LEVEL, LOG_INV_RATE, FOLDING)
            .config::<EF, EF, MyChallenger, _>(NUM_VARIABLES, &BooleanWhirDomain::default())
            .unwrap_err();
        // The schedule error carries no equality.
        //
        // So the variant is pinned whole, and the rendered message is checked as well.
        assert!(matches!(
            refused,
            ProfileError::Schedule(WhirConfigError::UnsupportedSecurityAssumption {
                assumption: SecurityAssumption::CapacityBound
            })
        ));
        assert_eq!(
            alloc::format!("{refused}"),
            "the evaluation domain does not support the CapacityBound soundness regime"
        );
    }

    // A schedule the challenger can grind, or a refusal naming a figure above what it can.
    fn inside_the_ceiling(derived: &Result<usize, ProfileError>, ceiling: usize) {
        match derived {
            Ok(pow_bits) => assert!(*pow_bits <= ceiling, "{pow_bits} bits past {ceiling}"),
            Err(ProfileError::Grinding {
                required,
                ceiling: named,
            }) => {
                assert_eq!(*named, ceiling);
                assert!(*required > ceiling, "{required} bits is not past {ceiling}");
            }
            // A derivation refused for another reason is not this test's business.
            Err(ProfileError::Schedule(_) | ProfileError::ZeroQueries) => {}
        }
    }

    #[test]
    fn the_ceiling_is_the_headroom_the_search_keeps_below_one_witness() {
        // Integer arithmetic over the alphabet's width, so every host reads the same figure.
        assert_eq!(grinding_ceiling::<BinaryField32>(), 24);
        assert_eq!(grinding_ceiling::<BinaryField64>(), 56);
        assert_eq!(grinding_ceiling::<EF>(), 56);
    }

    #[test]
    fn a_request_one_bit_past_the_ceiling_is_refused() {
        // The request is constructed rather than derived, so no analysis can move the premise.
        let refused = raise_allowance::<BinaryField32>(25).unwrap_err();
        assert!(
            matches!(
                refused,
                ProfileError::Grinding {
                    required: 25,
                    ceiling: 24
                }
            ),
            "{refused:?}"
        );
        assert_eq!(
            alloc::format!("{refused}"),
            "the schedule needs 25 grinding bits, one witness allows 24"
        );
        assert!(matches!(
            raise_allowance::<EF>(57).unwrap_err(),
            ProfileError::Grinding {
                required: 57,
                ceiling: 56
            }
        ));

        // The ceiling itself is allowed, so the refusal starts exactly one bit above it.
        assert_eq!(raise_allowance::<BinaryField32>(24).unwrap(), 24);
        assert_eq!(raise_allowance::<EF>(56).unwrap(), 56);
    }

    #[test]
    fn no_schedule_the_profile_returns_can_abort_the_prover() {
        // What must never come back is a schedule the challenger would refuse to grind.
        let narrow = BinaryWhirDomain::<BinaryField32>::default();
        let wide = BooleanWhirDomain::default();
        for security_level in [80, 100, 128, 160, 200, 256] {
            for log_inv_rate in [1, 2, 3, 4] {
                for num_variables in [8, 12, 16, 20] {
                    for folding in [2, 3, 4] {
                        let profile = BinaryWhirProfile::proven_list_decoding(
                            security_level,
                            log_inv_rate,
                            folding,
                        );
                        inside_the_ceiling(
                            &profile
                                .config::<EF, BinaryField32, NarrowChallenger, _>(
                                    num_variables,
                                    &narrow,
                                )
                                .map(|config| config.max_pow_bits()),
                            grinding_ceiling::<BinaryField32>(),
                        );
                        inside_the_ceiling(
                            &profile
                                .config::<EF, EF, MyChallenger, _>(num_variables, &wide)
                                .map(|config| config.max_pow_bits()),
                            grinding_ceiling::<EF>(),
                        );
                    }
                }
            }
        }
    }

    #[test]
    #[should_panic = "too small a margin"]
    fn the_narrow_ceiling_is_the_one_the_challenger_enforces() {
        // The refusal above is worth nothing unless one more bit really does abort a prover.
        let mut challenger = NarrowChallenger::from_hasher(alloc::vec::Vec::new(), Keccak256Hash);
        let _ = challenger.grind(grinding_ceiling::<BinaryField32>() + 1);
    }

    #[test]
    #[should_panic = "too small a margin"]
    fn the_wide_ceiling_is_the_one_the_challenger_enforces() {
        let mut challenger = MyChallenger::from_hasher(alloc::vec::Vec::new(), Keccak256Hash);
        let _ = challenger.grind(grinding_ceiling::<EF>() + 1);
    }
}