use crate::{impl_bytes, PCCommitment, PCCommitterKey, PCRandomness, PCVerifierKey, Vec};
use core::ops::{Add, AddAssign};
use rand_core::RngCore;
use snarkos_errors::serialization::SerializationError;
use snarkos_models::curves::PairingEngine;
use snarkos_utilities::{
bytes::{FromBytes, ToBytes},
error,
serialize::*,
};
use crate::kzg10;
pub type UniversalParams<E> = kzg10::UniversalParams<E>;
#[derive(Derivative)]
#[derivative(Default(bound = ""), Hash(bound = ""), Clone(bound = ""), Debug(bound = ""))]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct CommitterKey<E: PairingEngine> {
pub powers: Vec<E::G1Affine>,
pub shifted_powers: Option<Vec<E::G1Affine>>,
pub powers_of_gamma_g: Vec<E::G1Affine>,
pub enforced_degree_bounds: Option<Vec<usize>>,
pub max_degree: usize,
}
impl_bytes!(CommitterKey);
impl<E: PairingEngine> CommitterKey<E> {
pub fn powers<'a>(&'a self) -> kzg10::Powers<'a, E> {
kzg10::Powers {
powers_of_g: self.powers.as_slice().into(),
powers_of_gamma_g: self.powers_of_gamma_g.as_slice().into(),
}
}
pub fn shifted_powers<'a>(&'a self, degree_bound: impl Into<Option<usize>>) -> Option<kzg10::Powers<'a, E>> {
self.shifted_powers.as_ref().map(|shifted_powers| {
let powers_range = if let Some(degree_bound) = degree_bound.into() {
assert!(self.enforced_degree_bounds.as_ref().unwrap().contains(°ree_bound));
let max_bound = self.enforced_degree_bounds.as_ref().unwrap().last().unwrap();
(max_bound - degree_bound)..
} else {
0..
};
let ck = kzg10::Powers {
powers_of_g: (&shifted_powers[powers_range]).into(),
powers_of_gamma_g: self.powers_of_gamma_g.as_slice().into(),
};
ck
})
}
}
impl<E: PairingEngine> PCCommitterKey for CommitterKey<E> {
fn max_degree(&self) -> usize {
self.max_degree
}
fn supported_degree(&self) -> usize {
self.powers.len()
}
}
#[derive(Derivative)]
#[derivative(Default(bound = ""), Clone(bound = ""), Debug(bound = ""))]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct VerifierKey<E: PairingEngine> {
pub vk: kzg10::VerifierKey<E>,
pub degree_bounds_and_shift_powers: Option<Vec<(usize, E::G1Affine)>>,
pub max_degree: usize,
pub supported_degree: usize,
}
impl_bytes!(VerifierKey);
impl<E: PairingEngine> VerifierKey<E> {
pub fn get_shift_power(&self, bound: usize) -> Option<E::G1Affine> {
self.degree_bounds_and_shift_powers
.as_ref()
.and_then(|v| v.binary_search_by(|(d, _)| d.cmp(&bound)).ok().map(|i| v[i].1))
}
}
impl<E: PairingEngine> PCVerifierKey for VerifierKey<E> {
fn max_degree(&self) -> usize {
self.max_degree
}
fn supported_degree(&self) -> usize {
self.supported_degree
}
}
#[derive(Derivative)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Copy(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct Commitment<E: PairingEngine> {
pub(crate) comm: kzg10::Commitment<E>,
pub(crate) shifted_comm: Option<kzg10::Commitment<E>>,
}
impl_bytes!(Commitment);
impl<E: PairingEngine> PCCommitment for Commitment<E> {
#[inline]
fn empty() -> Self {
Self {
comm: kzg10::Commitment::empty(),
shifted_comm: Some(kzg10::Commitment::empty()),
}
}
fn has_degree_bound(&self) -> bool {
self.shifted_comm.is_some()
}
}
#[derive(Derivative)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct Randomness<E: PairingEngine> {
pub(crate) rand: kzg10::Randomness<E>,
pub(crate) shifted_rand: Option<kzg10::Randomness<E>>,
}
impl_bytes!(Randomness);
impl<'a, E: PairingEngine> Add<&'a Self> for Randomness<E> {
type Output = Self;
fn add(mut self, other: &'a Self) -> Self {
self += other;
self
}
}
impl<'a, E: PairingEngine> AddAssign<&'a Self> for Randomness<E> {
#[inline]
fn add_assign(&mut self, other: &'a Self) {
self.rand += &other.rand;
if let Some(r1) = &mut self.shifted_rand {
*r1 += other.shifted_rand.as_ref().unwrap_or(&kzg10::Randomness::empty());
} else {
self.shifted_rand = other.shifted_rand.as_ref().map(|r| r.clone());
}
}
}
impl<'a, E: PairingEngine> Add<(E::Fr, &'a Randomness<E>)> for Randomness<E> {
type Output = Self;
#[inline]
fn add(mut self, other: (E::Fr, &'a Randomness<E>)) -> Self {
self += other;
self
}
}
impl<'a, E: PairingEngine> AddAssign<(E::Fr, &'a Randomness<E>)> for Randomness<E> {
#[inline]
fn add_assign(&mut self, (f, other): (E::Fr, &'a Randomness<E>)) {
self.rand += (f, &other.rand);
let empty = kzg10::Randomness::empty();
if let Some(r1) = &mut self.shifted_rand {
*r1 += (f, other.shifted_rand.as_ref().unwrap_or(&empty));
} else {
self.shifted_rand = other.shifted_rand.as_ref().map(|r| empty + (f, r));
}
}
}
impl<E: PairingEngine> PCRandomness for Randomness<E> {
fn empty() -> Self {
Self {
rand: kzg10::Randomness::empty(),
shifted_rand: None,
}
}
fn rand<R: RngCore>(hiding_bound: usize, has_degree_bound: bool, rng: &mut R) -> Self {
let shifted_rand = if has_degree_bound {
Some(kzg10::Randomness::rand(hiding_bound, false, rng))
} else {
None
};
Self {
rand: kzg10::Randomness::rand(hiding_bound, false, rng),
shifted_rand,
}
}
}