Skip to main content

w3f_pcs/pcs/
commitment.rs

1use ark_ec::CurveGroup;
2use ark_serialize::*;
3use ark_std::iter::Sum;
4use ark_std::ops::{Add, Mul, Sub};
5use ark_std::vec::Vec;
6
7use crate::pcs::Commitment;
8use crate::utils::ec::small_multiexp_affine;
9
10/// KZG commitment to G1 represented in affine coordinates.
11#[derive(Clone, Debug, PartialEq, Eq, CanonicalSerialize, CanonicalDeserialize)]
12pub struct WrappedAffine<C: CurveGroup>(pub C::Affine);
13
14impl<C: CurveGroup> Mul<C::ScalarField> for WrappedAffine<C> {
15    type Output = Self;
16
17    fn mul(self, by: C::ScalarField) -> Self {
18        (&self).mul(by)
19    }
20}
21
22impl<C: CurveGroup> Commitment<C::ScalarField> for WrappedAffine<C> {
23    fn mul(&self, by: C::ScalarField) -> WrappedAffine<C> {
24        WrappedAffine(self.0.mul(by).into_affine())
25    }
26
27    fn combine(coeffs: &[C::ScalarField], commitments: &[Self]) -> Self {
28        let bases = commitments.iter().map(|c| c.0).collect::<Vec<_>>();
29        let prod = small_multiexp_affine(coeffs, &bases);
30        WrappedAffine(prod.into_affine())
31    }
32}
33
34impl<C: CurveGroup> Add<Self> for WrappedAffine<C> {
35    type Output = WrappedAffine<C>;
36
37    fn add(self, other: WrappedAffine<C>) -> WrappedAffine<C> {
38        WrappedAffine((self.0 + other.0).into_affine())
39    }
40}
41
42impl<C: CurveGroup> Sub<Self> for WrappedAffine<C> {
43    type Output = WrappedAffine<C>;
44
45    fn sub(self, other: WrappedAffine<C>) -> WrappedAffine<C> {
46        WrappedAffine((self.0 - other.0).into_affine())
47    }
48}
49
50impl<C: CurveGroup> Sum<Self> for WrappedAffine<C> {
51    fn sum<I: Iterator<Item = Self>>(iter: I) -> WrappedAffine<C> {
52        let sum: C = iter.map(|c| c.0).sum();
53        WrappedAffine(sum.into_affine())
54    }
55}