p3_fri/
config.rs

1use alloc::vec::Vec;
2use core::fmt::Debug;
3
4use p3_field::Field;
5use p3_matrix::Matrix;
6
7#[derive(Debug)]
8pub struct FriConfig<M> {
9    pub log_blowup: usize,
10    // TODO: This parameter and FRI early stopping are not yet implemented in `CirclePcs`.
11    pub log_final_poly_len: usize,
12    pub num_queries: usize,
13    pub proof_of_work_bits: usize,
14    pub mmcs: M,
15}
16
17impl<M> FriConfig<M> {
18    pub const fn blowup(&self) -> usize {
19        1 << self.log_blowup
20    }
21
22    pub const fn final_poly_len(&self) -> usize {
23        1 << self.log_final_poly_len
24    }
25
26    /// Returns the soundness bits of this FRI instance based on the
27    /// [ethSTARK](https://eprint.iacr.org/2021/582) conjecture.
28    ///
29    /// Certain users may instead want to look at proven soundness, a more complex calculation which
30    /// isn't currently supported by this crate.
31    pub const fn conjectured_soundness_bits(&self) -> usize {
32        self.log_blowup * self.num_queries + self.proof_of_work_bits
33    }
34}
35
36/// Whereas `FriConfig` encompasses parameters the end user can set, `FriGenericConfig` is
37/// set by the PCS calling FRI, and abstracts over implementation details of the PCS.
38pub trait FriGenericConfig<F: Field> {
39    type InputProof;
40    type InputError: Debug;
41
42    /// We can ask FRI to sample extra query bits (LSB) for our own purposes.
43    /// They will be passed to our callbacks, but ignored (shifted off) by FRI.
44    fn extra_query_index_bits(&self) -> usize;
45
46    /// Fold a row, returning a single column.
47    /// Right now the input row will always be 2 columns wide,
48    /// but we may support higher folding arity in the future.
49    fn fold_row(
50        &self,
51        index: usize,
52        log_height: usize,
53        beta: F,
54        evals: impl Iterator<Item = F>,
55    ) -> F;
56
57    /// Same as applying fold_row to every row, possibly faster.
58    fn fold_matrix<M: Matrix<F>>(&self, beta: F, m: M) -> Vec<F>;
59}
60
61/// Creates a minimal `FriConfig` for testing purposes.
62/// This configuration is designed to reduce computational cost during tests.
63pub const fn create_test_fri_config<Mmcs>(
64    mmcs: Mmcs,
65    log_final_poly_len: usize,
66) -> FriConfig<Mmcs> {
67    FriConfig {
68        log_blowup: 2,
69        log_final_poly_len,
70        num_queries: 2,
71        proof_of_work_bits: 1,
72        mmcs,
73    }
74}
75
76/// Creates a `FriConfig` suitable for benchmarking.
77/// This configuration represents typical settings used in production-like scenarios.
78pub const fn create_benchmark_fri_config<Mmcs>(mmcs: Mmcs) -> FriConfig<Mmcs> {
79    FriConfig {
80        log_blowup: 1,
81        log_final_poly_len: 0,
82        num_queries: 100,
83        proof_of_work_bits: 16,
84        mmcs,
85    }
86}