Skip to content

Commit

Permalink
Merge branch 'master' into new-group-infra
Browse files Browse the repository at this point in the history
  • Loading branch information
weikengchen authored Aug 4, 2023
2 parents 04b4106 + 1e8257f commit 8a6f8e8
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 9 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ members = [
"test-templates",
]

resolver = "2"

[profile.release]
opt-level = 3
lto = "thin"
Expand Down
2 changes: 1 addition & 1 deletion ff/src/fields/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ mod no_std_tests {
let felt2 = Fr::one() + Fr::one();
let felt16 = felt2 * felt2 * felt2 * felt2;

assert_eq!(Fr::from(1u8), Fr::one());
assert_eq!(Fr::from(1u8), Fr::one());
assert_eq!(Fr::from(1u16), Fr::one());
assert_eq!(Fr::from(1u32), Fr::one());
assert_eq!(Fr::from(1u64), Fr::one());
Expand Down
10 changes: 5 additions & 5 deletions poly/benches/dense_uv_polynomial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,23 +100,23 @@ fn bench_div_by_vanishing_poly<F: FftField>(b: &mut Bencher, degree: &usize) {

fn poly_benches<F: FftField>(c: &mut Criterion, name: &'static str) {
if ENABLE_ADD_BENCH {
let cur_name = format!("{:?} - add_polynomial", name.clone());
let cur_name = format!("{:?} - add_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_poly_add::<F>);
}
if ENABLE_ADD_ASSIGN_BENCH {
let cur_name = format!("{:?} - add_assign_polynomial", name.clone());
let cur_name = format!("{:?} - add_assign_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_poly_add_assign::<F>);
}
if ENABLE_EVALUATE_BENCH {
let cur_name = format!("{:?} - evaluate_polynomial", name.clone());
let cur_name = format!("{:?} - evaluate_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_poly_evaluate::<F>);
}
if ENABLE_SPARSE_EVALUATE_BENCH {
let cur_name = format!("{:?} - evaluate_sparse_polynomial", name.clone());
let cur_name = format!("{:?} - evaluate_sparse_polynomial", name);
setup_bench::<F>(c, &cur_name, bench_sparse_poly_evaluate::<F>);
}
if ENABLE_DIV_BY_VANISHING_POLY_BENCH {
let cur_name = format!("{:?} - evaluate_div_by_vanishing_poly", name.clone());
let cur_name = format!("{:?} - evaluate_div_by_vanishing_poly", name);
setup_bench::<F>(c, &cur_name, bench_div_by_vanishing_poly::<F>);
}
}
Expand Down
37 changes: 36 additions & 1 deletion poly/src/domain/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

use ark_ff::FftField;
use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
use ark_std::{fmt, hash, rand::Rng, vec::Vec};
use ark_std::{fmt, hash, rand::Rng, vec::Vec, Zero};

#[cfg(feature = "parallel")]
use rayon::prelude::*;
Expand Down Expand Up @@ -234,6 +234,41 @@ pub trait EvaluationDomain<F: FftField>:
tau.pow([self.size() as u64]) - self.coset_offset_pow_size()
}

/// Return the filter polynomial of `self` with respect to the subdomain `subdomain`.
/// Assumes that `subdomain` is contained within `self`.
///
/// # Panics
///
/// Panics if `subdomain` is not contained within `self`.
fn filter_polynomial(&self, subdomain: &Self) -> crate::univariate::DensePolynomial<F> {
use crate::univariate::DenseOrSparsePolynomial;
let self_vanishing_poly = DenseOrSparsePolynomial::from(
&self.vanishing_polynomial()
* (subdomain.size_as_field_element()
* subdomain.coset_offset().pow([subdomain.size() as u64])),
);
let subdomain_vanishing_poly = DenseOrSparsePolynomial::from(
&subdomain.vanishing_polynomial() * self.size_as_field_element(),
);
let (quotient, remainder) = self_vanishing_poly
.divide_with_q_and_r(&subdomain_vanishing_poly)
.unwrap();
assert!(remainder.is_zero());
quotient
}

/// This evaluates at `tau` the filter polynomial for `self` with respect
/// to the subdomain `subdomain`.
fn evaluate_filter_polynomial(&self, subdomain: &Self, tau: F) -> F {
let v_subdomain_of_tau = subdomain.evaluate_vanishing_polynomial(tau);
if v_subdomain_of_tau.is_zero() {
F::one()
} else {
subdomain.size_as_field_element() * self.evaluate_vanishing_polynomial(tau)
/ (self.size_as_field_element() * v_subdomain_of_tau)
}
}

/// Returns the `i`-th element of the domain.
fn element(&self, i: usize) -> F {
let mut result = self.group_gen().pow([i as u64]);
Expand Down
47 changes: 46 additions & 1 deletion poly/src/domain/radix2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod tests {
EvaluationDomain, Radix2EvaluationDomain,
};
use ark_ff::{FftField, Field, One, UniformRand, Zero};
use ark_std::{rand::Rng, test_rng};
use ark_std::{collections::BTreeSet, rand::Rng, test_rng};
use ark_test_curves::bls12_381::Fr;

#[test]
Expand Down Expand Up @@ -220,6 +220,51 @@ mod tests {
}
}

#[test]
fn filter_polynomial_test() {
for log_domain_size in 1..=4 {
let domain_size = 1 << log_domain_size;
let domain = Radix2EvaluationDomain::<Fr>::new(domain_size).unwrap();
for log_subdomain_size in 1..=log_domain_size {
let subdomain_size = 1 << log_subdomain_size;
let subdomain = Radix2EvaluationDomain::<Fr>::new(subdomain_size).unwrap();

// Obtain all possible offsets of `subdomain` within `domain`.
let mut possible_offsets = vec![Fr::one()];
let domain_generator = domain.group_gen();

let mut offset = domain_generator;
let subdomain_generator = subdomain.group_gen();
while offset != subdomain_generator {
possible_offsets.push(offset);
offset *= domain_generator;
}

assert_eq!(possible_offsets.len(), domain_size / subdomain_size);

// Get all possible cosets of `subdomain` within `domain`.
let cosets = possible_offsets
.iter()
.map(|offset| subdomain.get_coset(*offset).unwrap());

for coset in cosets {
let coset_elements = coset.elements().collect::<BTreeSet<_>>();
let filter_poly = domain.filter_polynomial(&coset);
assert_eq!(filter_poly.degree(), domain_size - subdomain_size);
for element in domain.elements() {
let evaluation = domain.evaluate_filter_polynomial(&coset, element);
assert_eq!(evaluation, filter_poly.evaluate(&element));
if coset_elements.contains(&element) {
assert_eq!(evaluation, Fr::one())
} else {
assert_eq!(evaluation, Fr::zero())
}
}
}
}
}
}

#[test]
fn size_of_elements() {
for coeffs in 1..10 {
Expand Down
9 changes: 8 additions & 1 deletion test-curves/src/bls12_381/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@ pub mod g2;
#[cfg(feature = "bls12_381_curve")]
pub mod g2_swu_iso;
#[cfg(feature = "bls12_381_curve")]
pub use {fq::*, fq12::*, fq2::*, fq6::*, g1::*, g1_swu_iso::*, g2::*, g2_swu_iso::*};
pub use {
fq::*,
fq12::*,
fq2::*,
fq6::*,
g1::{G1Affine, G1Projective},
g2::{G2Affine, G2Projective},
};

#[cfg(test)]
mod tests;
Expand Down

0 comments on commit 8a6f8e8

Please sign in to comment.