Skip to content

Commit

Permalink
fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
PatStiles committed Nov 28, 2023
1 parent 8ce6f57 commit 8444a6e
Show file tree
Hide file tree
Showing 5 changed files with 407 additions and 381 deletions.
5 changes: 2 additions & 3 deletions src/poly/unipoly.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,7 @@ mod tests {
fn interpolate_poly() {
let n = 250;
let mut rng = test_rng();
let poly =
UniPoly::from_coeff((0..n).map(|_| Fr::rand(&mut rng)).collect::<Vec<_>>());
let poly = UniPoly::from_coeff((0..n).map(|_| Fr::rand(&mut rng)).collect::<Vec<_>>());
let mut src = Vec::with_capacity(n);
let mut x = Vec::with_capacity(n);

Expand All @@ -267,7 +266,7 @@ mod tests {
src.push(poly.evaluate(&val));
}
let res = interpolate(&src, &x);

for i in 0..poly.len() {
assert_eq!(res[i], poly[i]);
}
Expand Down
2 changes: 1 addition & 1 deletion src/subprotocols/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ mod zk;
pub mod dot_product;
pub mod grand_product;
pub mod sumcheck;
pub mod traits;
pub mod zeromorph;
pub mod traits;
58 changes: 29 additions & 29 deletions src/subprotocols/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,36 @@ use merlin::Transcript;
use crate::utils::transcript;

pub trait CommitmentScheme {
type Commitment;
type Evaluation;
type Polynomial;
type Challenge;
type Proof;
type Error;
type Commitment;
type Evaluation;
type Polynomial;
type Challenge;
type Proof;
type Error;

type ProverKey;
type VerifierKey;
type ProverKey;
type VerifierKey;

//TODO: convert to impl IntoIterator<Item = Self::Polynomial>
fn commit(
polys: &[Self::Polynomial],
ck: &Self::ProverKey
) -> Result<Vec<Self::Commitment>, Self::Error>;
//TODO: convert to impl IntoIterator<Item = Self::Polynomial>
fn commit(
polys: &[Self::Polynomial],
ck: &Self::ProverKey,
) -> Result<Vec<Self::Commitment>, Self::Error>;

fn prove(
polys: &[Self::Polynomial],
evals: &[Self::Evaluation],
challenges: &[Self::Challenge],
pk: impl Borrow<Self::ProverKey>,
transcript: &mut Transcript
) -> Result<Self::Proof, Self::Error>;
fn prove(
polys: &[Self::Polynomial],
evals: &[Self::Evaluation],
challenges: &[Self::Challenge],
pk: impl Borrow<Self::ProverKey>,
transcript: &mut Transcript,
) -> Result<Self::Proof, Self::Error>;

fn verify(
commitments: &[Self::Commitment],
evals: &[Self::Evaluation],
challenges: &[Self::Challenge],
vk: impl Borrow<Self::VerifierKey>,
transcript: &mut Transcript,
proof: Self::Proof,
) -> Result<bool, Self::Error>;
}
fn verify(
commitments: &[Self::Commitment],
evals: &[Self::Evaluation],
challenges: &[Self::Challenge],
vk: impl Borrow<Self::VerifierKey>,
transcript: &mut Transcript,
proof: Self::Proof,
) -> Result<bool, Self::Error>;
}
191 changes: 100 additions & 91 deletions src/subprotocols/zeromorph/data_structures.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use ark_bn254::Bn254;
use ark_ec::{pairing::Pairing, CurveGroup};
use ark_std::rand::rngs::StdRng;
use ark_ff::UniformRand;
use ark_std::rand::rngs::StdRng;
use lazy_static::lazy_static;
use rand_chacha::rand_core::SeedableRng;
use std::sync::{Arc, Mutex};
Expand All @@ -11,61 +11,71 @@ use crate::utils::math::Math;
//TODO: The SRS is set with a default value of ____ if this is to be changed (extended) use the cli arg and change it manually.
//TODO: add input specifiying monomial or lagrange basis
lazy_static! {
pub static ref ZEROMORPH_SRS: Arc<Mutex<ZeromorphSRS<20, Bn254>>> =
pub static ref ZEROMORPH_SRS: Arc<Mutex<ZeromorphSRS<20, Bn254>>> =
Arc::new(Mutex::new(ZeromorphSRS::setup(None)));
}

#[derive(Debug, Clone, Default)]
pub struct ZeromorphSRS<const N_MAX: usize, P: Pairing> {
g1_powers: Vec<P::G1Affine>,
g2_powers: Vec<P::G2Affine>,
g1_powers: Vec<P::G1Affine>,
g2_powers: Vec<P::G2Affine>,
}

impl<const N_MAX: usize, P: Pairing> ZeromorphSRS<N_MAX, P> {

fn compute_g_powers<G: CurveGroup>(tau: G::ScalarField) -> Vec<G::Affine> {
let g_srs = vec![G::zero(); N_MAX - 1];

let g_srs: Vec<G> = std::iter::once(G::generator())
.chain(g_srs.iter().scan(G::generator(), |state, _| {
*state *= &tau;
Some(*state)
}))
.collect();

G::normalize_batch(&g_srs)
fn compute_g_powers<G: CurveGroup>(tau: G::ScalarField) -> Vec<G::Affine> {
let g_srs = vec![G::zero(); N_MAX - 1];

let g_srs: Vec<G> = std::iter::once(G::generator())
.chain(g_srs.iter().scan(G::generator(), |state, _| {
*state *= &tau;
Some(*state)
}))
.collect();

G::normalize_batch(&g_srs)
}

pub fn setup(toxic_waste: Option<&[u8]>) -> ZeromorphSRS<N_MAX, P> {
let tau: &[u8];
if toxic_waste.is_none() {
tau = b"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
} else {
tau = toxic_waste.unwrap()
}

pub fn setup(toxic_waste: Option<&[u8]>) -> ZeromorphSRS<N_MAX,P> {
let tau: &[u8];
if toxic_waste.is_none() {
tau = b"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
} else {
tau = toxic_waste.unwrap()
}
/*
if ENV_VAR_NOT_PASSED_IN
*/
let mut bytes = [0u8; 32];
let len = tau.len();
bytes[..len].copy_from_slice(&tau[..len]);
let rng = &mut StdRng::from_seed(bytes);

let tau = P::ScalarField::rand(rng);
let g1_powers = Self::compute_g_powers::<P::G1>(tau);
let g2_powers = Self::compute_g_powers::<P::G2>(tau);
ZeromorphSRS { g1_powers, g2_powers }
/*
if ENV_VAR_NOT_PASSED_IN
*/
let mut bytes = [0u8; 32];
let len = tau.len();
bytes[..len].copy_from_slice(&tau[..len]);
let rng = &mut StdRng::from_seed(bytes);

let tau = P::ScalarField::rand(rng);
let g1_powers = Self::compute_g_powers::<P::G1>(tau);
let g2_powers = Self::compute_g_powers::<P::G2>(tau);
ZeromorphSRS {
g1_powers,
g2_powers,
}
}

pub fn get_prover_key(&self) -> ZeromorphProverKey<P> {
ZeromorphProverKey { g1: self.g1_powers[0], tau_1: self.g1_powers[1], g1_powers: self.g1_powers.clone() }
pub fn get_prover_key(&self) -> ZeromorphProverKey<P> {
ZeromorphProverKey {
g1: self.g1_powers[0],
tau_1: self.g1_powers[1],
g1_powers: self.g1_powers.clone(),
}

pub fn get_verifier_key(&self) -> ZeromorphVerifierKey<P> {
let idx = N_MAX - (2_usize.pow(N_MAX.log_2() as u32) - 1);
ZeromorphVerifierKey { g1: self.g1_powers[0], g2: self.g2_powers[0], tau_2: self.g2_powers[1], tau_N_max_sub_2_N: self.g2_powers[idx] }
}

pub fn get_verifier_key(&self) -> ZeromorphVerifierKey<P> {
let idx = N_MAX - (2_usize.pow(N_MAX.log_2() as u32) - 1);
ZeromorphVerifierKey {
g1: self.g1_powers[0],
g2: self.g2_powers[0],
tau_2: self.g2_powers[1],
tau_N_max_sub_2_N: self.g2_powers[idx],
}

}
}

#[derive(Clone, Debug)]
Expand Down Expand Up @@ -94,51 +104,50 @@ pub struct ZeromorphProof<P: Pairing> {

#[cfg(test)]
mod test {
use ark_bn254::Bn254;
use ark_ec::{pairing::Pairing, AffineRepr};
use ark_ff::One;
use std::ops::Mul;
use super::*;

fn expected_srs<E: Pairing>(n: usize, seed: &[u8]) -> (Vec<E::G1Affine>, Vec<E::G2Affine>) {

let mut bytes = [0u8; 32];
let len = seed.len();
bytes[..len].copy_from_slice(&seed[..len]);
let rng = &mut StdRng::from_seed(bytes);

let tau = E::ScalarField::rand(rng);

let powers_of_tau: Vec<E::ScalarField> =
std::iter::successors(Some(E::ScalarField::one()), |p| Some(*p * tau))
.take(n)
.collect();

let g1_gen = E::G1Affine::generator();
let g2_gen = E::G2Affine::generator();

let srs_g1: Vec<E::G1Affine> = powers_of_tau
.iter()
.map(|tp| g1_gen.mul(tp).into())
.collect();
let srs_g2: Vec<E::G2Affine> = powers_of_tau
.iter()
.map(|tp| g2_gen.mul(tp).into())
.collect();

(srs_g1, srs_g2)
}

#[test]
fn test_srs() {
const K: i32 = 1;
const N: usize = 1 << K;
let seed = b"111111111111111111111111111";

let (g1_srs_expected, g2_srs_expected) = expected_srs::<Bn254>(N, seed);

let srs = ZeromorphSRS::<N, Bn254>::setup(Some(seed));
assert_eq!(g1_srs_expected, srs.g1_powers);
assert_eq!(g2_srs_expected, srs.g2_powers);
}
}
use super::*;
use ark_bn254::Bn254;
use ark_ec::{pairing::Pairing, AffineRepr};
use ark_ff::One;
use std::ops::Mul;

fn expected_srs<E: Pairing>(n: usize, seed: &[u8]) -> (Vec<E::G1Affine>, Vec<E::G2Affine>) {
let mut bytes = [0u8; 32];
let len = seed.len();
bytes[..len].copy_from_slice(&seed[..len]);
let rng = &mut StdRng::from_seed(bytes);

let tau = E::ScalarField::rand(rng);

let powers_of_tau: Vec<E::ScalarField> =
std::iter::successors(Some(E::ScalarField::one()), |p| Some(*p * tau))
.take(n)
.collect();

let g1_gen = E::G1Affine::generator();
let g2_gen = E::G2Affine::generator();

let srs_g1: Vec<E::G1Affine> = powers_of_tau
.iter()
.map(|tp| g1_gen.mul(tp).into())
.collect();
let srs_g2: Vec<E::G2Affine> = powers_of_tau
.iter()
.map(|tp| g2_gen.mul(tp).into())
.collect();

(srs_g1, srs_g2)
}

#[test]
fn test_srs() {
const K: i32 = 1;
const N: usize = 1 << K;
let seed = b"111111111111111111111111111";

let (g1_srs_expected, g2_srs_expected) = expected_srs::<Bn254>(N, seed);

let srs = ZeromorphSRS::<N, Bn254>::setup(Some(seed));
assert_eq!(g1_srs_expected, srs.g1_powers);
assert_eq!(g2_srs_expected, srs.g2_powers);
}
}
Loading

0 comments on commit 8444a6e

Please sign in to comment.