p3_fri/
config.rs

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