use ark_ec::{pairing::Pairing, AffineRepr, CurveGroup};
use ark_ff::PrimeField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{cfg_iter, fmt::Debug, vec::Vec};
#[cfg(feature = "parallel")]
use rayon::prelude::*;
use super::error::AggregationError;
#[derive(Clone, Debug, PartialEq, CanonicalSerialize, CanonicalDeserialize)]
pub struct Key<G: AffineRepr> {
pub a: Vec<G>,
pub b: Vec<G>,
}
pub type VKey<E> = Key<<E as Pairing>::G2Affine>;
pub type WKey<E> = Key<<E as Pairing>::G1Affine>;
#[derive(Clone, Debug, CanonicalSerialize, CanonicalDeserialize)]
pub struct PreparedVKey<E: Pairing> {
pub a: Vec<<E as Pairing>::G2Prepared>,
pub b: Vec<<E as Pairing>::G2Prepared>,
}
impl<E: Pairing> From<&VKey<E>> for PreparedVKey<E> {
fn from(other: &VKey<E>) -> Self {
let a = cfg_iter!(other.a)
.map(|e| E::G2Prepared::from(*e))
.collect::<Vec<_>>();
let b = cfg_iter!(other.b)
.map(|e| E::G2Prepared::from(*e))
.collect::<Vec<_>>();
Self { a, b }
}
}
impl<E: Pairing> PreparedVKey<E> {
pub fn len(&self) -> usize {
self.a.len()
}
pub fn ensure_sufficient_len<K>(&self, m: &[K]) -> Result<(), AggregationError> {
if self.a.len() < m.len() {
return Err(AggregationError::InsufficientKeyLength(self.len()));
}
return Ok(());
}
}
impl<G> Key<G>
where
G: AffineRepr,
{
pub fn has_correct_len(&self, n: usize) -> bool {
self.a.len() == n && self.b.len() == n
}
pub fn ensure_sufficient_len<K>(&self, m: &[K]) -> Result<(), AggregationError> {
if self.a.len() < m.len() {
return Err(AggregationError::InsufficientKeyLength(self.len()));
}
return Ok(());
}
pub fn len(&self) -> usize {
self.a.len()
}
pub fn scale(&self, s_vec: &[G::ScalarField]) -> Result<Self, AggregationError> {
if self.a.len() != s_vec.len() {
return Err(AggregationError::InvalidKeyLength);
}
let (a, b): (Vec<G::Group>, Vec<G::Group>) = cfg_iter!(self.a)
.zip(cfg_iter!(self.b))
.zip(cfg_iter!(s_vec))
.map(|((ap, bp), si)| {
let s_repr = si.into_bigint();
let v1s = ap.mul_bigint(s_repr);
let v2s = bp.mul_bigint(s_repr);
(v1s, v2s)
})
.unzip();
Ok(Self {
a: G::Group::normalize_batch(&a),
b: G::Group::normalize_batch(&b),
})
}
pub fn split(mut self, at: usize) -> (Self, Self) {
let a_right = self.a.split_off(at);
let b_right = self.b.split_off(at);
(
Self {
a: self.a,
b: self.b,
},
Self {
a: a_right,
b: b_right,
},
)
}
pub fn compress(&self, right: &Self, scale: &G::ScalarField) -> Result<Self, AggregationError> {
let left = self;
if left.a.len() != right.a.len() {
return Err(AggregationError::InvalidKeyLength);
}
let (a, b): (Vec<G::Group>, Vec<G::Group>) = cfg_iter!(left.a)
.zip(cfg_iter!(left.b))
.zip(cfg_iter!(right.a))
.zip(cfg_iter!(right.b))
.map(|(((left_a, left_b), right_a), right_b)| {
let s_repr = scale.into_bigint();
let mut ra = right_a.mul_bigint(s_repr);
let mut rb = right_b.mul_bigint(s_repr);
ra += left_a;
rb += left_b;
(ra, rb)
})
.unzip();
Ok(Self {
a: G::Group::normalize_batch(&a),
b: G::Group::normalize_batch(&b),
})
}
pub fn first(&self) -> (G, G) {
(self.a[0], self.b[0])
}
}