Skip to main content

w3f_pcs/pcs/
mod.rs

1use ark_ff::PrimeField;
2use ark_poly::Evaluations;
3use ark_serialize::*;
4use ark_std::fmt::Debug;
5use ark_std::iter::Sum;
6use ark_std::ops::{Add, Mul, Sub};
7use ark_std::rand::Rng;
8use ark_std::vec::Vec;
9
10pub use id::IdentityCommitment;
11
12use crate::Poly;
13
14pub mod commitment;
15pub mod id;
16pub mod kzg;
17
18pub trait Commitment<F: PrimeField>:
19    Eq
20    + Sized
21    + Clone
22    + Debug
23    + Add<Self, Output = Self>
24    + Mul<F, Output = Self>
25    + Sub<Self, Output = Self>
26    + Sum<Self>
27    + CanonicalSerialize
28    + CanonicalDeserialize
29{
30    fn mul(&self, by: F) -> Self;
31    fn combine(coeffs: &[F], commitments: &[Self]) -> Self;
32}
33
34/// Can be used to commit and open commitments to DensePolynomial<F> of degree up to max_degree.
35pub trait CommitterKey: Clone + Debug + CanonicalSerialize + CanonicalDeserialize {
36    /// Maximal degree of a polynomial supported.
37    fn max_degree(&self) -> usize;
38
39    /// Maximal number of evaluations supported when committing in the Lagrangian base.
40    fn max_evals(&self) -> usize {
41        self.max_degree() + 1
42    }
43}
44
45/// Can be used to verify openings to commitments.
46pub trait VerifierKey: Clone + Debug {
47    /// Maximal number of openings that can be verified.
48    fn max_points(&self) -> usize {
49        1
50    }
51}
52
53/// Generates a `VerifierKey`, serializable
54pub trait RawVerifierKey:
55    Clone + Debug + Eq + PartialEq + CanonicalSerialize + CanonicalDeserialize
56{
57    type VK: VerifierKey;
58
59    fn prepare(&self) -> Self::VK;
60}
61
62pub trait PcsParams {
63    type CK: CommitterKey;
64    type VK: VerifierKey;
65    type RVK: RawVerifierKey<VK = Self::VK>;
66
67    fn ck(&self) -> Self::CK;
68    fn vk(&self) -> Self::VK;
69    fn raw_vk(&self) -> Self::RVK;
70
71    fn ck_with_lagrangian(&self, _domain_size: usize) -> Self::CK {
72        unimplemented!();
73    }
74}
75
76/// Polynomial commitment scheme.
77pub trait PCS<F: PrimeField> {
78    type C: Commitment<F>;
79
80    type Proof: Clone + CanonicalSerialize + CanonicalDeserialize;
81
82    type CK: CommitterKey;
83
84    // vk needs to be convertible to a ck that is only required to commit to the p=1 constant polynomial,
85    // see https://eprint.iacr.org/archive/2020/1536/1629188090.pdf, section 4.2
86    type VK: VerifierKey + Into<Self::CK>;
87
88    type Params: PcsParams<CK = Self::CK, VK = Self::VK>;
89
90    fn setup<R: Rng>(max_degree: usize, rng: &mut R) -> Self::Params;
91
92    fn commit(ck: &Self::CK, p: &Poly<F>) -> Result<Self::C, ()>;
93
94    fn commit_evals(ck: &Self::CK, evals: &Evaluations<F>) -> Result<Self::C, ()> {
95        let poly = evals.interpolate_by_ref();
96        Self::commit(ck, &poly)
97    }
98
99    fn open(ck: &Self::CK, p: &Poly<F>, x: F) -> Result<Self::Proof, ()>;
100
101    fn verify(vk: &Self::VK, c: Self::C, x: F, z: F, proof: Self::Proof) -> Result<(), ()>;
102
103    // TODO: is the default implementation useful?
104    fn batch_verify<R: Rng>(
105        vk: &Self::VK,
106        c: Vec<Self::C>,
107        x: Vec<F>,
108        y: Vec<F>,
109        proof: Vec<Self::Proof>,
110        _rng: &mut R,
111    ) -> Result<(), ()> {
112        assert_eq!(c.len(), x.len());
113        assert_eq!(c.len(), y.len());
114        c.into_iter()
115            .zip(x.into_iter())
116            .zip(y.into_iter())
117            .zip(proof.into_iter())
118            .all(|(((c, x), y), proof)| Self::verify(vk, c, x, y, proof).is_ok())
119            .then(|| ())
120            .ok_or(())
121    }
122}