arnaucube
arnaucube/hyperplonkPublic Self-hosted repository
hyperplonk/pcs/src/lib.rs
pcs/src/lib.rsRaw
  1mod errors;
  2mod multilinear_kzg;
  3mod structs;
  4mod univariate_kzg;
  5
  6pub mod prelude;
  7
  8use ark_ec::PairingEngine;
  9use ark_ff::Field;
 10use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
 11use ark_std::rand::{CryptoRng, RngCore};
 12use errors::PCSError;
 13use std::{borrow::Borrow, fmt::Debug, hash::Hash};
 14
 15/// This trait defines APIs for polynomial commitment schemes.
 16/// Note that for our usage of PCS, we do not require the hiding property.
 17pub trait PolynomialCommitmentScheme<E: PairingEngine> {
 18    /// Prover parameters
 19    type ProverParam: Clone;
 20    /// Verifier parameters
 21    type VerifierParam: Clone + CanonicalSerialize + CanonicalDeserialize;
 22    /// Structured reference string
 23    type SRS: Clone + Debug;
 24    /// Polynomial and its associated types
 25    type Polynomial: Clone
 26        + Debug
 27        + Hash
 28        + PartialEq
 29        + Eq
 30        + CanonicalSerialize
 31        + CanonicalDeserialize;
 32    /// Polynomial input domain
 33    type Point: Clone + Ord + Debug + Sync + Hash + PartialEq + Eq;
 34    /// Polynomial Evaluation
 35    type Evaluation: Field;
 36    /// Commitments
 37    type Commitment: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;
 38    /// Batch commitments
 39    type BatchCommitment: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;
 40    /// Proofs
 41    type Proof: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;
 42    /// Batch proofs
 43    type BatchProof: Clone + CanonicalSerialize + CanonicalDeserialize + Debug + PartialEq + Eq;
 44
 45    /// Build SRS for testing.
 46    ///
 47    /// - For univariate polynomials, `supported_size` is the maximum degree.
 48    /// - For multilinear polynomials, `supported_size` is the number of
 49    ///   variables.
 50    ///
 51    /// WARNING: THIS FUNCTION IS FOR TESTING PURPOSE ONLY.
 52    /// THE OUTPUT SRS SHOULD NOT BE USED IN PRODUCTION.
 53    fn gen_srs_for_testing<R: RngCore + CryptoRng>(
 54        rng: &mut R,
 55        supported_size: usize,
 56    ) -> Result<Self::SRS, PCSError>;
 57
 58    /// Trim the universal parameters to specialize the public parameters.
 59    /// Input both `supported_degree` for univariate and
 60    /// `supported_num_vars` for multilinear.
 61    /// ## Note on function signature
 62    /// Usually, data structure like SRS and ProverParam are huge and users
 63    /// might wish to keep them in heap using different kinds of smart pointers
 64    /// (instead of only in stack) therefore our `impl Borrow<_>` interface
 65    /// allows for passing in any pointer type, e.g.: `trim(srs: &Self::SRS,
 66    /// ..)` or `trim(srs: Box<Self::SRS>, ..)` or `trim(srs: Arc<Self::SRS>,
 67    /// ..)` etc.
 68    fn trim(
 69        srs: impl Borrow<Self::SRS>,
 70        supported_degree: usize,
 71        supported_num_vars: Option<usize>,
 72    ) -> Result<(Self::ProverParam, Self::VerifierParam), PCSError>;
 73
 74    /// Generate a commitment for a polynomial
 75    /// ## Note on function signature
 76    /// Usually, data structure like SRS and ProverParam are huge and users
 77    /// might wish to keep them in heap using different kinds of smart pointers
 78    /// (instead of only in stack) therefore our `impl Borrow<_>` interface
 79    /// allows for passing in any pointer type, e.g.: `commit(prover_param:
 80    /// &Self::ProverParam, ..)` or `commit(prover_param:
 81    /// Box<Self::ProverParam>, ..)` or `commit(prover_param:
 82    /// Arc<Self::ProverParam>, ..)` etc.
 83    fn commit(
 84        prover_param: impl Borrow<Self::ProverParam>,
 85        poly: &Self::Polynomial,
 86    ) -> Result<Self::Commitment, PCSError>;
 87
 88    /// Generate a commitment for a list of polynomials
 89    fn multi_commit(
 90        prover_param: impl Borrow<Self::ProverParam>,
 91        polys: &[Self::Polynomial],
 92    ) -> Result<Self::BatchCommitment, PCSError>;
 93
 94    /// On input a polynomial `p` and a point `point`, outputs a proof for the
 95    /// same.
 96    fn open(
 97        prover_param: impl Borrow<Self::ProverParam>,
 98        polynomial: &Self::Polynomial,
 99        point: &Self::Point,
100    ) -> Result<(Self::Proof, Self::Evaluation), PCSError>;
101
102    /// Input a list of multilinear extensions, and a same number of points, and
103    /// a transcript, compute a multi-opening for all the polynomials.
104    fn multi_open(
105        prover_param: impl Borrow<Self::ProverParam>,
106        multi_commitment: &Self::BatchCommitment,
107        polynomials: &[Self::Polynomial],
108        points: &[Self::Point],
109    ) -> Result<(Self::BatchProof, Vec<Self::Evaluation>), PCSError>;
110
111    /// Input a multilinear extension, and a number of points, and
112    /// a transcript, compute a multi-opening for all the polynomials.
113    fn multi_open_single_poly(
114        prover_param: impl Borrow<Self::ProverParam>,
115        commitment: &Self::Commitment,
116        polynomials: &Self::Polynomial,
117        points: &[Self::Point],
118    ) -> Result<(Self::BatchProof, Vec<Self::Evaluation>), PCSError>;
119
120    /// Verifies that `value` is the evaluation at `x` of the polynomial
121    /// committed inside `comm`.
122    fn verify(
123        verifier_param: &Self::VerifierParam,
124        commitment: &Self::Commitment,
125        point: &Self::Point,
126        value: &E::Fr,
127        proof: &Self::Proof,
128    ) -> Result<bool, PCSError>;
129
130    /// Verifies that `value_i` is the evaluation at `x_i` of the polynomial
131    /// `poly_i` committed inside `comm`.
132    fn batch_verify<R: RngCore + CryptoRng>(
133        verifier_param: &Self::VerifierParam,
134        multi_commitment: &Self::BatchCommitment,
135        points: &[Self::Point],
136        values: &[E::Fr],
137        batch_proof: &Self::BatchProof,
138        rng: &mut R,
139    ) -> Result<bool, PCSError>;
140
141    /// Verifies that `value_i` is the evaluation at `x_i` of the polynomial
142    /// `poly` committed inside `comm`.
143    fn batch_verify_single_poly(
144        verifier_param: &Self::VerifierParam,
145        commitment: &Self::Commitment,
146        points: &[Self::Point],
147        values: &[E::Fr],
148        batch_proof: &Self::BatchProof,
149    ) -> Result<bool, PCSError>;
150}
151
152/// API definitions for structured reference string
153pub trait StructuredReferenceString<E: PairingEngine>: Sized {
154    /// Prover parameters
155    type ProverParam;
156    /// Verifier parameters
157    type VerifierParam;
158
159    /// Extract the prover parameters from the public parameters.
160    fn extract_prover_param(&self, supported_size: usize) -> Self::ProverParam;
161    /// Extract the verifier parameters from the public parameters.
162    fn extract_verifier_param(&self, supported_size: usize) -> Self::VerifierParam;
163
164    /// Trim the universal parameters to specialize the public parameters
165    /// for polynomials to the given `supported_size`, and
166    /// returns committer key and verifier key.
167    ///
168    /// - For univariate polynomials, `supported_size` is the maximum degree.
169    /// - For multilinear polynomials, `supported_size` is 2 to the number of
170    ///   variables.
171    ///
172    /// `supported_log_size` should be in range `1..=params.log_size`
173    fn trim(
174        &self,
175        supported_size: usize,
176    ) -> Result<(Self::ProverParam, Self::VerifierParam), PCSError>;
177
178    /// Build SRS for testing.
179    ///
180    /// - For univariate polynomials, `supported_size` is the maximum degree.
181    /// - For multilinear polynomials, `supported_size` is the number of
182    ///   variables.
183    ///
184    /// WARNING: THIS FUNCTION IS FOR TESTING PURPOSE ONLY.
185    /// THE OUTPUT SRS SHOULD NOT BE USED IN PRODUCTION.
186    fn gen_srs_for_testing<R: RngCore + CryptoRng>(
187        rng: &mut R,
188        supported_size: usize,
189    ) -> Result<Self, PCSError>;
190}