use crate::{impl_bytes, *};
use core::ops::{Add, AddAssign};
use snarkos_errors::serialization::SerializationError;
use snarkos_models::curves::{AffineCurve, PairingCurve, PairingEngine, PrimeField, ProjectiveCurve, Zero};
use snarkos_utilities::{
bytes::ToBytes,
error,
serialize::{CanonicalDeserialize, CanonicalSerialize},
};
#[derive(Derivative)]
#[derivative(Default(bound = ""), Clone(bound = ""), Debug(bound = ""))]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct UniversalParams<E: PairingEngine> {
pub powers_of_g: Vec<E::G1Affine>,
pub powers_of_gamma_g: Vec<E::G1Affine>,
pub h: E::G2Affine,
pub beta_h: E::G2Affine,
pub prepared_neg_powers_of_h: Option<Vec<<E::G2Affine as PairingCurve>::Prepared>>,
#[derivative(Debug = "ignore")]
pub prepared_h: <E::G2Affine as PairingCurve>::Prepared,
#[derivative(Debug = "ignore")]
pub prepared_beta_h: <E::G2Affine as PairingCurve>::Prepared,
}
impl_bytes!(UniversalParams);
impl<E: PairingEngine> PCUniversalParams for UniversalParams<E> {
fn max_degree(&self) -> usize {
self.powers_of_g.len() - 1
}
}
#[derive(Derivative)]
#[derivative(Default(bound = ""), Hash(bound = ""), Clone(bound = ""), Debug(bound = ""))]
pub struct Powers<'a, E: PairingEngine> {
pub powers_of_g: Cow<'a, [E::G1Affine]>,
pub powers_of_gamma_g: Cow<'a, [E::G1Affine]>,
}
impl<E: PairingEngine> Powers<'_, E> {
pub fn size(&self) -> usize {
self.powers_of_g.len()
}
}
#[derive(Derivative)]
#[derivative(Default(bound = ""), Clone(bound = ""), Debug(bound = ""))]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct VerifierKey<E: PairingEngine> {
pub g: E::G1Affine,
pub gamma_g: E::G1Affine,
pub h: E::G2Affine,
pub beta_h: E::G2Affine,
#[derivative(Debug = "ignore")]
pub prepared_h: <E::G2Affine as PairingCurve>::Prepared,
#[derivative(Debug = "ignore")]
pub prepared_beta_h: <E::G2Affine as PairingCurve>::Prepared,
}
impl_bytes!(VerifierKey);
#[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 E::G1Affine,
);
impl_bytes!(Commitment);
impl<E: PairingEngine> PCCommitment for Commitment<E> {
#[inline]
fn empty() -> Self {
Commitment(E::G1Affine::zero())
}
fn has_degree_bound(&self) -> bool {
false
}
}
impl<'a, E: PairingEngine> AddAssign<(E::Fr, &'a Commitment<E>)> for Commitment<E> {
#[inline]
fn add_assign(&mut self, (f, other): (E::Fr, &'a Commitment<E>)) {
let mut other = other.0.mul(f.into_repr());
other.add_assign_mixed(&self.0);
self.0 = other.into();
}
}
#[derive(Derivative)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct Randomness<E: PairingEngine> {
pub blinding_polynomial: Polynomial<E::Fr>,
}
impl_bytes!(Randomness);
impl<E: PairingEngine> Randomness<E> {
#[inline]
pub fn is_hiding(&self) -> bool {
!self.blinding_polynomial.is_zero()
}
#[inline]
pub fn calculate_hiding_polynomial_degree(hiding_bound: usize) -> usize {
hiding_bound + 1
}
}
impl<E: PairingEngine> PCRandomness for Randomness<E> {
fn empty() -> Self {
Self {
blinding_polynomial: Polynomial::zero(),
}
}
fn rand<R: RngCore>(hiding_bound: usize, _: bool, rng: &mut R) -> Self {
let mut randomness = Randomness::empty();
let hiding_poly_degree = Self::calculate_hiding_polynomial_degree(hiding_bound);
randomness.blinding_polynomial = Polynomial::rand(hiding_poly_degree, rng);
randomness
}
}
impl<'a, E: PairingEngine> Add<&'a Randomness<E>> for Randomness<E> {
type Output = Self;
#[inline]
fn add(mut self, other: &'a Self) -> Self {
self.blinding_polynomial += &other.blinding_polynomial;
self
}
}
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<&'a Randomness<E>> for Randomness<E> {
#[inline]
fn add_assign(&mut self, other: &'a Self) {
self.blinding_polynomial += &other.blinding_polynomial;
}
}
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.blinding_polynomial += (f, &other.blinding_polynomial);
}
}
#[derive(Derivative)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Copy(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
#[derive(CanonicalSerialize, CanonicalDeserialize)]
pub struct Proof<E: PairingEngine> {
pub w: E::G1Affine,
pub random_v: Option<E::Fr>,
}
impl_bytes!(Proof);
impl<E: PairingEngine> PCProof for Proof<E> {}