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    /// This should return a domain such that `Domain::next_point` returns `Some`.
53    fn natural_domain_for_degree(&self, degree: usize) -> Self::Domain;
54
55    /// Given a collection of evaluation matrices, produce a binding commitment to
56    /// the polynomials defined by those evaluations. If `zk` is enabled, the evaluations are
57    /// first randomized as explained in Section 3 of https://eprint.iacr.org/2024/1037.pdf .
58    ///
59    /// Returns both the commitment which should be sent to the verifier
60    /// and the prover data which can be used to produce opening proofs.
61    #[allow(clippy::type_complexity)]
62    fn commit(
63        &self,
64        evaluations: impl IntoIterator<Item = (Self::Domain, RowMajorMatrix<Val<Self::Domain>>)>,
65    ) -> (Self::Commitment, Self::ProverData);
66
67    /// Commit to the quotient polynomial. We first decompose the quotient polynomial into
68    /// `num_chunks` many smaller polynomials each of degree `degree / num_chunks`.
69    /// This can have minor performance benefits, but is not strictly necessary in the non `zk` case.
70    /// When `zk` is enabled, this commitment will additionally include some randomization process
71    /// to hide the inputs.
72    ///
73    /// ### Arguments
74    /// - `quotient_domain` the domain of the quotient polynomial.
75    /// - `quotient_evaluations` the evaluations of the quotient polynomial over the domain. This should be in
76    ///   standard (not bit-reversed) order.
77    /// - `num_chunks` the number of smaller polynomials to decompose the quotient polynomial into.
78    #[allow(clippy::type_complexity)]
79    fn commit_quotient(
80        &self,
81        quotient_domain: Self::Domain,
82        quotient_evaluations: RowMajorMatrix<Val<Self::Domain>>,
83        num_chunks: usize,
84    ) -> (Self::Commitment, Self::ProverData) {
85        // Given the evaluation vector of `Q_i(x)` over a domain, split it into evaluation vectors
86        // of `q_{i0}(x), ...` over subdomains and commit to these `q`'s.
87        // TODO: Currently, split_evals involves copying the data to a new matrix.
88        //       We may be able to avoid this copy making use of bit-reversals.
89        let quotient_sub_evaluations =
90            quotient_domain.split_evals(num_chunks, quotient_evaluations);
91        let quotient_sub_domains = quotient_domain.split_domains(num_chunks);
92
93        self.commit(
94            quotient_sub_domains
95                .into_iter()
96                .zip(quotient_sub_evaluations),
97        )
98    }
99
100    /// Given prover data corresponding to a commitment to a collection of evaluation matrices,
101    /// return the evaluations of those matrices on the given domain.
102    ///
103    /// This is essentially a no-op when called with a `domain` which is a subset of the evaluation domain
104    /// on which the evaluation matrices are defined.
105    fn get_evaluations_on_domain<'a>(
106        &self,
107        prover_data: &'a Self::ProverData,
108        idx: usize,
109        domain: Self::Domain,
110    ) -> Self::EvaluationsOnDomain<'a>;
111
112    /// Open a collection of polynomial commitments at a set of points. Produce the values at those points along with a proof
113    /// of correctness.
114    ///
115    /// Arguments:
116    /// - `commitment_data_with_opening_points`: A vector whose elements are a pair:
117    ///     - `data`: The prover data corresponding to a multi-matrix commitment.
118    ///     - `opening_points`: A vector containing, for each matrix committed to, a vector of opening points.
119    /// - `fiat_shamir_challenger`: The challenger that will be used to generate the proof.
120    ///
121    /// Unwrapping the arguments further, each `data` contains a vector of the committed matrices (`matrices = Vec<M>`).
122    /// If the length of `matrices` is not equal to the length of `opening_points` the function will error. Otherwise, for
123    /// each index `i`, the matrix `M = matrices[i]` will be opened at the points `opening_points[i]`.
124    ///
125    /// This means that each column of `M` will be interpreted as the evaluation vector of some polynomial
126    /// and we will compute the value of all of those polynomials at `opening_points[i]`.
127    ///
128    /// The domains on which the evaluation vectors are defined is not part of the arguments here
129    /// but should be public information known to both the prover and verifier.
130    fn open(
131        &self,
132        // For each multi-matrix commitment,
133        commitment_data_with_opening_points: Vec<(
134            // The matrices and auxiliary prover data
135            &Self::ProverData,
136            // for each matrix,
137            Vec<
138                // the points to open
139                Vec<Challenge>,
140            >,
141        )>,
142        fiat_shamir_challenger: &mut Challenger,
143    ) -> (OpenedValues<Challenge>, Self::Proof);
144
145    /// Verify that a collection of opened values is correct.
146    ///
147    /// Arguments:
148    /// - `commitments_with_opening_points`: A vector whose elements are a pair:
149    ///     - `commitment`: A multi matrix commitment.
150    ///     - `opening_points`: A vector containing, for each matrix committed to, a vector of opening points and claimed evaluations.
151    /// - `proof`: A claimed proof of correctness for the opened values.
152    /// - `fiat_shamir_challenger`: The challenger that will be used to generate the proof.
153    #[allow(clippy::type_complexity)]
154    fn verify(
155        &self,
156        // For each commitment:
157        commitments_with_opening_points: Vec<(
158            // The commitment
159            Self::Commitment,
160            // for each matrix in the commitment:
161            Vec<(
162                // its domain,
163                Self::Domain,
164                // A vector of (point, claimed_evaluation) pairs
165                Vec<(
166                    // the point the matrix was opened at,
167                    Challenge,
168                    // the claimed evaluations at that point
169                    Vec<Challenge>,
170                )>,
171            )>,
172        )>,
173        // The opening proof for all claimed evaluations.
174        proof: &Self::Proof,
175        fiat_shamir_challenger: &mut Challenger,
176    ) -> Result<(), Self::Error>;
177
178    fn get_opt_randomization_poly_commitment(
179        &self,
180        _domain: Self::Domain,
181    ) -> Option<(Self::Commitment, Self::ProverData)> {
182        None
183    }
184}
185
186pub type OpenedValues<F> = Vec<OpenedValuesForRound<F>>;
187pub type OpenedValuesForRound<F> = Vec<OpenedValuesForMatrix<F>>;
188pub type OpenedValuesForMatrix<F> = Vec<OpenedValuesForPoint<F>>;
189pub type OpenedValuesForPoint<F> = Vec<F>;