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