use crate::*;
use crate::{PCCommitterKey, PCVerifierKey, Vec};
use ark_ec::AffineRepr;
use ark_ff::{Field, UniformRand, Zero};
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::rand::RngCore;
use ark_std::vec;
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(Default(bound = ""), Clone(bound = ""), Debug(bound = ""))]
pub struct UniversalParams<G: AffineRepr> {
pub comm_key: Vec<G>,
pub h: G,
pub s: G,
}
impl<G: AffineRepr> PCUniversalParams for UniversalParams<G> {
fn max_degree(&self) -> usize {
self.comm_key.len() - 1
}
}
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Debug(bound = "")
)]
pub struct CommitterKey<G: AffineRepr> {
pub comm_key: Vec<G>,
pub h: G,
pub s: G,
pub max_degree: usize,
}
impl<G: AffineRepr> PCCommitterKey for CommitterKey<G> {
fn max_degree(&self) -> usize {
self.max_degree
}
fn supported_degree(&self) -> usize {
self.comm_key.len() - 1
}
}
pub type VerifierKey<G> = CommitterKey<G>;
impl<G: AffineRepr> PCVerifierKey for VerifierKey<G> {
fn max_degree(&self) -> usize {
self.max_degree
}
fn supported_degree(&self) -> usize {
self.comm_key.len() - 1
}
}
pub type PreparedVerifierKey<G> = VerifierKey<G>;
impl<G: AffineRepr> PCPreparedVerifierKey<VerifierKey<G>> for PreparedVerifierKey<G> {
fn prepare(vk: &VerifierKey<G>) -> Self {
vk.clone()
}
}
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Copy(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
pub struct Commitment<G: AffineRepr> {
pub comm: G,
pub shifted_comm: Option<G>,
}
impl<G: AffineRepr> PCCommitment for Commitment<G> {
#[inline]
fn empty() -> Self {
Commitment {
comm: G::zero(),
shifted_comm: None,
}
}
fn has_degree_bound(&self) -> bool {
false
}
}
pub type PreparedCommitment<E> = Commitment<E>;
impl<G: AffineRepr> PCPreparedCommitment<Commitment<G>> for PreparedCommitment<G> {
fn prepare(vk: &Commitment<G>) -> Self {
vk.clone()
}
}
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Debug(bound = ""),
PartialEq(bound = ""),
Eq(bound = "")
)]
pub struct Randomness<G: AffineRepr> {
pub rand: G::ScalarField,
pub shifted_rand: Option<G::ScalarField>,
}
impl<G: AffineRepr> PCRandomness for Randomness<G> {
fn empty() -> Self {
Self {
rand: G::ScalarField::zero(),
shifted_rand: None,
}
}
fn rand<R: RngCore>(_: usize, has_degree_bound: bool, _: Option<usize>, rng: &mut R) -> Self {
let rand = G::ScalarField::rand(rng);
let shifted_rand = if has_degree_bound {
Some(G::ScalarField::rand(rng))
} else {
None
};
Self { rand, shifted_rand }
}
}
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(
Default(bound = ""),
Hash(bound = ""),
Clone(bound = ""),
Debug(bound = "")
)]
pub struct Proof<G: AffineRepr> {
pub l_vec: Vec<G>,
pub r_vec: Vec<G>,
pub final_comm_key: G,
pub c: G::ScalarField,
pub hiding_comm: Option<G>,
pub rand: Option<G::ScalarField>,
}
pub struct SuccinctCheckPolynomial<F: Field>(pub Vec<F>);
impl<F: Field> SuccinctCheckPolynomial<F> {
pub fn compute_coeffs(&self) -> Vec<F> {
let challenges = &self.0;
let log_d = challenges.len();
let mut coeffs = vec![F::one(); 1 << log_d];
for (i, challenge) in challenges.iter().enumerate() {
let i = i + 1;
let elem_degree = 1 << (log_d - i);
for start in (elem_degree..coeffs.len()).step_by(elem_degree * 2) {
for offset in 0..elem_degree {
coeffs[start + offset] *= challenge;
}
}
}
coeffs
}
pub fn evaluate(&self, point: F) -> F {
let challenges = &self.0;
let log_d = challenges.len();
let mut product = F::one();
for (i, challenge) in challenges.iter().enumerate() {
let i = i + 1;
let elem_degree: u64 = (1 << (log_d - i)) as u64;
let elem = point.pow([elem_degree]);
product *= &(F::one() + &(elem * challenge));
}
product
}
}