p3_commit/pcs.rs
1//! Traits for polynomial commitment schemes.
2
3use alloc::vec::Vec;
4use core::fmt::Debug;
5
6use p3_field::ExtensionField;
7use p3_matrix::Matrix;
8use p3_matrix::dense::RowMajorMatrix;
9use serde::Serialize;
10use serde::de::DeserializeOwned;
11
12use crate::PolynomialSpace;
13
14pub type Val<D> = <D as PolynomialSpace>::Val;
15
16/// A polynomial commitment scheme, for committing to batches of polynomials defined by their evaluations
17/// over some domain.
18///
19/// In general this does not have to be a hiding commitment scheme but it might be for some implementations.
20// TODO: Should we have a super-trait for weakly-binding PCSs, like FRI outside unique decoding radius?
21pub trait Pcs<Challenge, Challenger>
22where
23 Challenge: ExtensionField<Val<Self::Domain>>,
24{
25 /// The class of evaluation domains that this commitment scheme works over.
26 type Domain: PolynomialSpace;
27
28 /// The commitment that's sent to the verifier.
29 type Commitment: Clone + Serialize + DeserializeOwned;
30
31 /// Data that the prover stores for committed polynomials, to help the prover with opening.
32 type ProverData;
33
34 /// Type of the output of `get_evaluations_on_domain`.
35 type EvaluationsOnDomain<'a>: Matrix<Val<Self::Domain>> + 'a;
36
37 /// The opening argument.
38 type Proof: Clone + Serialize + DeserializeOwned;
39
40 /// The type of a proof verification error.
41 type Error: Debug;
42
43 /// Set to true to activate randomization and achieve zero-knowledge.
44 const ZK: bool;
45
46 /// Index of the trace commitment in the computed opened values.
47 const TRACE_IDX: usize = Self::ZK as usize;
48
49 /// Index of the quotient commitments in the computed opened values.
50 const QUOTIENT_IDX: usize = Self::TRACE_IDX + 1;
51
52 /// Index of the preprocessed trace commitment in the computed opened values.
53 const PREPROCESSED_TRACE_IDX: usize = Self::QUOTIENT_IDX + 1; // Note: not always present
54
55 /// This should return a domain such that `Domain::next_point` returns `Some`.
56 fn natural_domain_for_degree(&self, degree: usize) -> Self::Domain;
57
58 /// Given a collection of evaluation matrices, produce a binding commitment to
59 /// the polynomials defined by those evaluations. If `zk` is enabled, the evaluations are
60 /// first randomized as explained in Section 3 of https://eprint.iacr.org/2024/1037.pdf .
61 ///
62 /// Returns both the commitment which should be sent to the verifier
63 /// and the prover data which can be used to produce opening proofs.
64 #[allow(clippy::type_complexity)]
65 fn commit(
66 &self,
67 evaluations: impl IntoIterator<Item = (Self::Domain, RowMajorMatrix<Val<Self::Domain>>)>,
68 ) -> (Self::Commitment, Self::ProverData);
69
70 /// Commit to the quotient polynomial. We first decompose the quotient polynomial into
71 /// `num_chunks` many smaller polynomials each of degree `degree / num_chunks`.
72 /// This can have minor performance benefits, but is not strictly necessary in the non `zk` case.
73 /// When `zk` is enabled, this commitment will additionally include some randomization process
74 /// to hide the inputs.
75 ///
76 /// ### Arguments
77 /// - `quotient_domain` the domain of the quotient polynomial.
78 /// - `quotient_evaluations` the evaluations of the quotient polynomial over the domain. This should be in
79 /// standard (not bit-reversed) order.
80 /// - `num_chunks` the number of smaller polynomials to decompose the quotient polynomial into.
81 #[allow(clippy::type_complexity)]
82 fn commit_quotient(
83 &self,
84 quotient_domain: Self::Domain,
85 quotient_evaluations: RowMajorMatrix<Val<Self::Domain>>,
86 num_chunks: usize,
87 ) -> (Self::Commitment, Self::ProverData) {
88 // Given the evaluation vector of `Q_i(x)` over a domain, split it into evaluation vectors
89 // of `q_{i0}(x), ...` over subdomains and commit to these `q`'s.
90 // TODO: Currently, split_evals involves copying the data to a new matrix.
91 // We may be able to avoid this copy making use of bit-reversals.
92 let quotient_sub_evaluations =
93 quotient_domain.split_evals(num_chunks, quotient_evaluations);
94 let quotient_sub_domains = quotient_domain.split_domains(num_chunks);
95
96 self.commit(
97 quotient_sub_domains
98 .into_iter()
99 .zip(quotient_sub_evaluations),
100 )
101 }
102
103 /// Given prover data corresponding to a commitment to a collection of evaluation matrices,
104 /// return the evaluations of those matrices on the given domain.
105 ///
106 /// This is essentially a no-op when called with a `domain` which is a subset of the evaluation domain
107 /// on which the evaluation matrices are defined.
108 fn get_evaluations_on_domain<'a>(
109 &self,
110 prover_data: &'a Self::ProverData,
111 idx: usize,
112 domain: Self::Domain,
113 ) -> Self::EvaluationsOnDomain<'a>;
114
115 /// Open a collection of polynomial commitments at a set of points. Produce the values at those points along with a proof
116 /// of correctness.
117 ///
118 /// Arguments:
119 /// - `commitment_data_with_opening_points`: A vector whose elements are a pair:
120 /// - `data`: The prover data corresponding to a multi-matrix commitment.
121 /// - `opening_points`: A vector containing, for each matrix committed to, a vector of opening points.
122 /// - `fiat_shamir_challenger`: The challenger that will be used to generate the proof.
123 ///
124 /// Unwrapping the arguments further, each `data` contains a vector of the committed matrices (`matrices = Vec<M>`).
125 /// If the length of `matrices` is not equal to the length of `opening_points` the function will error. Otherwise, for
126 /// each index `i`, the matrix `M = matrices[i]` will be opened at the points `opening_points[i]`.
127 ///
128 /// This means that each column of `M` will be interpreted as the evaluation vector of some polynomial
129 /// and we will compute the value of all of those polynomials at `opening_points[i]`.
130 ///
131 /// The domains on which the evaluation vectors are defined is not part of the arguments here
132 /// but should be public information known to both the prover and verifier.
133 fn open(
134 &self,
135 // For each multi-matrix commitment,
136 commitment_data_with_opening_points: Vec<(
137 // The matrices and auxiliary prover data
138 &Self::ProverData,
139 // for each matrix,
140 Vec<
141 // the points to open
142 Vec<Challenge>,
143 >,
144 )>,
145 fiat_shamir_challenger: &mut Challenger,
146 ) -> (OpenedValues<Challenge>, Self::Proof);
147
148 /// Verify that a collection of opened values is correct.
149 ///
150 /// Arguments:
151 /// - `commitments_with_opening_points`: A vector whose elements are a pair:
152 /// - `commitment`: A multi matrix commitment.
153 /// - `opening_points`: A vector containing, for each matrix committed to, a vector of opening points and claimed evaluations.
154 /// - `proof`: A claimed proof of correctness for the opened values.
155 /// - `fiat_shamir_challenger`: The challenger that will be used to generate the proof.
156 #[allow(clippy::type_complexity)]
157 fn verify(
158 &self,
159 // For each commitment:
160 commitments_with_opening_points: Vec<(
161 // The commitment
162 Self::Commitment,
163 // for each matrix in the commitment:
164 Vec<(
165 // its domain,
166 Self::Domain,
167 // A vector of (point, claimed_evaluation) pairs
168 Vec<(
169 // the point the matrix was opened at,
170 Challenge,
171 // the claimed evaluations at that point
172 Vec<Challenge>,
173 )>,
174 )>,
175 )>,
176 // The opening proof for all claimed evaluations.
177 proof: &Self::Proof,
178 fiat_shamir_challenger: &mut Challenger,
179 ) -> Result<(), Self::Error>;
180
181 fn get_opt_randomization_poly_commitment(
182 &self,
183 _domain: Self::Domain,
184 ) -> Option<(Self::Commitment, Self::ProverData)> {
185 None
186 }
187}
188
189pub type OpenedValues<F> = Vec<OpenedValuesForRound<F>>;
190pub type OpenedValuesForRound<F> = Vec<OpenedValuesForMatrix<F>>;
191pub type OpenedValuesForMatrix<F> = Vec<OpenedValuesForPoint<F>>;
192pub type OpenedValuesForPoint<F> = Vec<F>;