From 69d0f5c5baff8d0d76dbae153ca43c51ff85a2d9 Mon Sep 17 00:00:00 2001 From: arnaucube Date: Thu, 27 Jul 2023 23:30:34 +0200 Subject: [PATCH] add pow_i & pow_i_over_x methods --- src/lib.rs | 1 + src/protogalaxy.rs | 90 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 src/protogalaxy.rs diff --git a/src/lib.rs b/src/lib.rs index 609383e..e24a480 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,5 +4,6 @@ #![allow(dead_code)] // TMP pub mod pedersen; +pub mod protogalaxy; pub mod transcript; pub mod utils; diff --git a/src/protogalaxy.rs b/src/protogalaxy.rs new file mode 100644 index 0000000..280fc43 --- /dev/null +++ b/src/protogalaxy.rs @@ -0,0 +1,90 @@ +use ark_crypto_primitives::sponge::Absorb; +use ark_ec::{CurveGroup, Group}; +use ark_ff::fields::PrimeField; +use ark_std::log2; +use ark_std::{One, Zero}; +use std::marker::PhantomData; +use std::ops::Add; + +use ark_poly::{univariate::SparsePolynomial, Polynomial}; + +use crate::pedersen::{Commitment, Params as PedersenParams, Pedersen, Proof as PedersenProof}; +use crate::transcript::Transcript; +use crate::utils::*; + +// naive impl of pow_i for betas, assuming that betas=(b, b^2, b^4, ..., b^{2^{t-1}}) +fn pow_i(i: usize, betas: &Vec) -> F { + // WIP check if makes more sense to do it with ifs instead of arithmetic + + let n = 2_u64.pow(betas.len() as u32); + let b = bit_decompose(i as u64, n as usize); + + let mut r: F = F::one(); + for (j, beta_i) in betas.iter().enumerate() { + let mut b_j = F::zero(); + if b[j] { + b_j = F::one(); + } + r *= (F::one() - b_j) + b_j * betas[j]; + } + r +} + +fn pow_i_over_x(i: usize, betas: &Vec, deltas: &Vec) -> SparsePolynomial { + assert_eq!(betas.len(), deltas.len()); + + let n = 2_u64.pow(betas.len() as u32); + let b = bit_decompose(i as u64, n as usize); + + let mut r: SparsePolynomial = + SparsePolynomial::::from_coefficients_vec(vec![(0, F::one())]); // start with r(x) = 1 + for (j, beta_i) in betas.iter().enumerate() { + if b[j] { + let curr: SparsePolynomial = + SparsePolynomial::::from_coefficients_vec(vec![(0, betas[j]), (1, deltas[j])]); + r = r.mul(&curr); + } + } + r +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::pedersen::Pedersen; + use crate::transcript::poseidon_test_config; + use ark_bls12_381::{Fr, G1Projective}; + use ark_std::One; + use ark_std::UniformRand; + + #[test] + fn test_pow_i() { + let mut rng = ark_std::test_rng(); + let t = 4; + let n = 16; + let beta = Fr::rand(&mut rng); + let betas = powers_of_beta(beta, t); + let not_betas = all_powers(beta, n); + + for i in 0..n { + assert_eq!(pow_i(i, &betas), not_betas[i]); + } + } + + #[test] + fn test_pow_i_over_x() { + let mut rng = ark_std::test_rng(); + let t = 3; + let n = 8; + // let beta = Fr::rand(&mut rng); + // let delta = Fr::rand(&mut rng); + let beta = Fr::from(3); + let delta = Fr::from(5); + let betas = powers_of_beta(beta, t); + let deltas = powers_of_beta(delta, t); + + // for i in 0..n { + // dbg!(pow_i_over_x(i, &betas, &deltas)); + // } + } +}