Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
tcoratger committed Nov 13, 2024
1 parent edd468d commit df8c32b
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 76 deletions.
22 changes: 11 additions & 11 deletions poly/src/domain/mixed_radix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ impl<F: FftField> EvaluationDomain<F> for MixedRadixEvaluationDomain<F> {
coeffs,
self.group_gen,
self.log_size_of_group,
serial_mixed_radix_fft::<T, F>,
serial_mixed_radix_fft,
)
}

Expand All @@ -190,7 +190,7 @@ impl<F: FftField> EvaluationDomain<F> for MixedRadixEvaluationDomain<F> {
evals,
self.group_gen_inv,
self.log_size_of_group,
serial_mixed_radix_fft::<T, F>,
serial_mixed_radix_fft,
);
if self.offset.is_one() {
ark_std::cfg_iter_mut!(evals).for_each(|val| *val *= self.size_inv);
Expand Down Expand Up @@ -400,7 +400,7 @@ mod tests {
fn vanishing_polynomial_evaluation() {
let rng = &mut test_rng();
for coeffs in 0..12 {
let domain = MixedRadixEvaluationDomain::<Fr>::new(coeffs).unwrap();
let domain = MixedRadixEvaluationDomain::new(coeffs).unwrap();
let coset_domain = domain.get_coset(Fr::GENERATOR).unwrap();
let z = domain.vanishing_polynomial();
let coset_z = coset_domain.vanishing_polynomial();
Expand All @@ -421,7 +421,7 @@ mod tests {
#[test]
fn vanishing_polynomial_vanishes_on_domain() {
for coeffs in 0..1000 {
let domain = MixedRadixEvaluationDomain::<Fr>::new(coeffs).unwrap();
let domain = MixedRadixEvaluationDomain::new(coeffs).unwrap();
let z = domain.vanishing_polynomial();
for point in domain.elements() {
assert!(z.evaluate(&point).is_zero())
Expand All @@ -441,7 +441,7 @@ mod tests {
fn non_systematic_lagrange_coefficients_test() {
for domain_dim in 1..10 {
let domain_size = 1 << domain_dim;
let domain = MixedRadixEvaluationDomain::<Fr>::new(domain_size).unwrap();
let domain = MixedRadixEvaluationDomain::new(domain_size).unwrap();
let coset_domain = domain.get_coset(Fr::GENERATOR).unwrap();
// Get random pt + lagrange coefficients
let rand_pt = Fr::rand(&mut test_rng());
Expand All @@ -450,7 +450,7 @@ mod tests {

// Sample the random polynomial, evaluate it over the domain and the random
// point.
let rand_poly = DensePolynomial::<Fr>::rand(domain_size - 1, &mut test_rng());
let rand_poly = DensePolynomial::rand(domain_size - 1, &mut test_rng());
let poly_evals = domain.fft(rand_poly.coeffs());
let coset_poly_evals = coset_domain.fft(rand_poly.coeffs());
let actual_eval = rand_poly.evaluate(&rand_pt);
Expand All @@ -474,7 +474,7 @@ mod tests {
// low. We generate lagrange coefficients for each element in the domain.
for domain_dim in 1..5 {
let domain_size = 1 << domain_dim;
let domain = MixedRadixEvaluationDomain::<Fr>::new(domain_size).unwrap();
let domain = MixedRadixEvaluationDomain::new(domain_size).unwrap();
let coset_domain = domain.get_coset(Fr::GENERATOR).unwrap();
for (i, (x, coset_x)) in domain.elements().zip(coset_domain.elements()).enumerate() {
let lagrange_coeffs = domain.evaluate_all_lagrange_coefficients(x);
Expand Down Expand Up @@ -502,7 +502,7 @@ mod tests {
fn size_of_elements() {
for coeffs in 1..12 {
let size = 1 << coeffs;
let domain = MixedRadixEvaluationDomain::<Fr>::new(size).unwrap();
let domain = MixedRadixEvaluationDomain::new(size).unwrap();
let domain_size = domain.size();
assert_eq!(domain_size, domain.elements().count());
}
Expand All @@ -512,7 +512,7 @@ mod tests {
fn elements_contents() {
for coeffs in 1..12 {
let size = 1 << coeffs;
let domain = MixedRadixEvaluationDomain::<Fr>::new(size).unwrap();
let domain = MixedRadixEvaluationDomain::new(size).unwrap();
let offset = Fr::GENERATOR;
let coset_domain = domain.get_coset(offset).unwrap();
for (i, (x, coset_x)) in domain.elements().zip(coset_domain.elements()).enumerate() {
Expand Down Expand Up @@ -545,12 +545,12 @@ mod tests {
let domain = MixedRadixEvaluationDomain::new(v1.len()).unwrap();

for log_cpus in log_d..min(log_d + 1, 3) {
parallel_fft::<F, F>(
parallel_fft(
&mut v1,
domain.group_gen,
log_d,
log_cpus,
serial_mixed_radix_fft::<F, F>,
serial_mixed_radix_fft,
);
serial_mixed_radix_fft::<F, F>(&mut v2, domain.group_gen, log_d);

Expand Down
74 changes: 31 additions & 43 deletions poly/src/polynomial/univariate/dense.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,8 +209,8 @@ impl<F: FftField> DensePolynomial<F> {
.zip(&quotient_vec)
.for_each(|(s, c)| *s += c);

let quotient = DensePolynomial::<F>::from_coefficients_vec(quotient_vec);
let remainder = DensePolynomial::<F>::from_coefficients_vec(remainder_vec);
let quotient = DensePolynomial::from_coefficients_vec(quotient_vec);
let remainder = DensePolynomial::from_coefficients_vec(remainder_vec);
(quotient, remainder)
}
}
Expand Down Expand Up @@ -246,13 +246,13 @@ impl<F: FftField> DensePolynomial<F> {
domain: D,
) -> Evaluations<F, D> {
let poly: DenseOrSparsePolynomial<'_, F> = self.into();
DenseOrSparsePolynomial::<F>::evaluate_over_domain(poly, domain)
DenseOrSparsePolynomial::evaluate_over_domain(poly, domain)
}

/// Evaluate `self` over `domain`.
pub fn evaluate_over_domain<D: EvaluationDomain<F>>(self, domain: D) -> Evaluations<F, D> {
let poly: DenseOrSparsePolynomial<'_, F> = self.into();
DenseOrSparsePolynomial::<F>::evaluate_over_domain(poly, domain)
DenseOrSparsePolynomial::evaluate_over_domain(poly, domain)
}
}

Expand Down Expand Up @@ -708,15 +708,15 @@ mod tests {
// if the leading coefficient were uniformly sampled from all of F, this
// test would fail with high probability ~99.9%
for i in 1..=30 {
assert_eq!(DensePolynomial::<F5>::rand(i, rng).degree(), i);
assert_eq!(DensePolynomial::rand(i, rng).degree(), i);
}
}

#[test]
fn double_polynomials_random() {
let rng = &mut test_rng();
for degree in 0..70 {
let p = DensePolynomial::<Fr>::rand(degree, rng);
let p = DensePolynomial::rand(degree, rng);
let p_double = &p + &p;
let p_quad = &p_double + &p_double;
assert_eq!(&(&(&p + &p) + &p) + &p, p_quad);
Expand All @@ -728,8 +728,8 @@ mod tests {
let rng = &mut test_rng();
for a_degree in 0..70 {
for b_degree in 0..70 {
let p1 = DensePolynomial::<Fr>::rand(a_degree, rng);
let p2 = DensePolynomial::<Fr>::rand(b_degree, rng);
let p1 = DensePolynomial::rand(a_degree, rng);
let p2 = DensePolynomial::rand(b_degree, rng);
let res1 = &p1 + &p2;
let res2 = &p2 + &p1;
assert_eq!(res1, res2);
Expand All @@ -742,7 +742,7 @@ mod tests {
let rng = &mut test_rng();
for a_degree in 0..70 {
for b_degree in 0..70 {
let p1 = DensePolynomial::<Fr>::rand(a_degree, rng);
let p1 = DensePolynomial::rand(a_degree, rng);
let p2 = rand_sparse_poly(b_degree, rng);
let res = &p1 + &p2;
assert_eq!(res, &p1 + &Into::<DensePolynomial<Fr>>::into(p2));
Expand All @@ -755,7 +755,7 @@ mod tests {
let rng = &mut test_rng();
for a_degree in 0..70 {
for b_degree in 0..70 {
let p1 = DensePolynomial::<Fr>::rand(a_degree, rng);
let p1 = DensePolynomial::rand(a_degree, rng);
let p2 = rand_sparse_poly(b_degree, rng);

let mut res = p1.clone();
Expand Down Expand Up @@ -789,8 +789,8 @@ mod tests {
let rng = &mut test_rng();
for a_degree in 0..70 {
for b_degree in 0..70 {
let p1 = DensePolynomial::<Fr>::rand(a_degree, rng);
let p2 = DensePolynomial::<Fr>::rand(b_degree, rng);
let p1 = DensePolynomial::rand(a_degree, rng);
let p2 = DensePolynomial::rand(b_degree, rng);
let res1 = &p1 - &p2;
let res2 = &p2 - &p1;
assert_eq!(&res1 + &p2, p1);
Expand All @@ -804,7 +804,7 @@ mod tests {
let rng = &mut test_rng();
for a_degree in 0..70 {
for b_degree in 0..70 {
let p1 = DensePolynomial::<Fr>::rand(a_degree, rng);
let p1 = DensePolynomial::rand(a_degree, rng);
let p2 = rand_sparse_poly(b_degree, rng);
let res = &p1 - &p2;
assert_eq!(res, &p1 - &Into::<DensePolynomial<Fr>>::into(p2));
Expand All @@ -817,7 +817,7 @@ mod tests {
let rng = &mut test_rng();
for a_degree in 0..70 {
for b_degree in 0..70 {
let p1 = DensePolynomial::<Fr>::rand(a_degree, rng);
let p1 = DensePolynomial::rand(a_degree, rng);
let p2 = rand_sparse_poly(b_degree, rng);

let mut res = p1.clone();
Expand All @@ -832,14 +832,14 @@ mod tests {
// Test adding polynomials with its negative equals 0
let mut rng = test_rng();
for degree in 0..70 {
let poly = DensePolynomial::<Fr>::rand(degree, &mut rng);
let poly = DensePolynomial::rand(degree, &mut rng);
let neg = -poly.clone();
let result = poly + neg;
assert!(result.is_zero());
assert_eq!(result.degree(), 0);

// Test with SubAssign trait
let poly = DensePolynomial::<Fr>::rand(degree, &mut rng);
let poly = DensePolynomial::rand(degree, &mut rng);
let mut result = poly.clone();
result -= &poly;
assert!(result.is_zero());
Expand Down Expand Up @@ -871,8 +871,8 @@ mod tests {

for a_degree in 0..50 {
for b_degree in 0..50 {
let dividend = DensePolynomial::<Fr>::rand(a_degree, rng);
let divisor = DensePolynomial::<Fr>::rand(b_degree, rng);
let dividend = DensePolynomial::rand(a_degree, rng);
let divisor = DensePolynomial::rand(b_degree, rng);
if let Some((quotient, remainder)) = DenseOrSparsePolynomial::divide_with_q_and_r(
&(&dividend).into(),
&(&divisor).into(),
Expand Down Expand Up @@ -901,7 +901,7 @@ mod tests {
fn mul_random_element() {
let rng = &mut test_rng();
for degree in 0..70 {
let a = DensePolynomial::<Fr>::rand(degree, rng);
let a = DensePolynomial::rand(degree, rng);
let e = Fr::rand(rng);
assert_eq!(
&a * e,
Expand All @@ -915,8 +915,8 @@ mod tests {
let rng = &mut test_rng();
for a_degree in 0..70 {
for b_degree in 0..70 {
let a = DensePolynomial::<Fr>::rand(a_degree, rng);
let b = DensePolynomial::<Fr>::rand(b_degree, rng);
let a = DensePolynomial::rand(a_degree, rng);
let b = DensePolynomial::rand(b_degree, rng);
assert_eq!(&a * &b, a.naive_mul(&b))
}
}
Expand All @@ -928,7 +928,7 @@ mod tests {
for size in 1..10 {
let domain = GeneralEvaluationDomain::new(1 << size).unwrap();
for degree in 0..70 {
let p = DensePolynomial::<Fr>::rand(degree, rng);
let p = DensePolynomial::rand(degree, rng);
let ans1 = p.mul_by_vanishing_poly(domain);
let ans2 = &p * &domain.vanishing_polynomial().into();
assert_eq!(ans1, ans2);
Expand All @@ -942,7 +942,7 @@ mod tests {
for size in 1..10 {
let domain = GeneralEvaluationDomain::new(1 << size).unwrap();
for degree in 0..12 {
let p = DensePolynomial::<Fr>::rand(degree * 100, rng);
let p = DensePolynomial::rand(degree * 100, rng);
let (quotient, remainder) = p.divide_by_vanishing_poly(domain);
let p_recovered = quotient.mul_by_vanishing_poly(domain) + remainder;
assert_eq!(p, p_recovered);
Expand Down Expand Up @@ -981,27 +981,18 @@ mod tests {
#[test]
fn evaluate_over_domain_test() {
let rng = &mut ark_std::test_rng();
let domain = crate::domain::Radix2EvaluationDomain::<Fr>::new(1 << 10).unwrap();
let domain = crate::domain::Radix2EvaluationDomain::new(1 << 10).unwrap();
let offset = Fr::GENERATOR;
let coset = domain.get_coset(offset).unwrap();
for _ in 0..100 {
let poly = DensePolynomial::<Fr>::rand(1 << 11, rng);
let evaluations = domain
.elements()
.map(|e| poly.evaluate(&e))
.collect::<Vec<_>>();
let poly = DensePolynomial::rand(1 << 11, rng);
let evaluations = domain.elements().map(|e| poly.evaluate(&e)).collect();
assert_eq!(evaluations, poly.evaluate_over_domain_by_ref(domain).evals);
let evaluations = coset
.elements()
.map(|e| poly.evaluate(&e))
.collect::<Vec<_>>();
let evaluations = coset.elements().map(|e| poly.evaluate(&e)).collect();
assert_eq!(evaluations, poly.evaluate_over_domain(coset).evals);
}
let zero = DensePolynomial::zero();
let evaluations = domain
.elements()
.map(|e| zero.evaluate(&e))
.collect::<Vec<_>>();
let evaluations = domain.elements().map(|e| zero.evaluate(&e)).collect();
assert_eq!(evaluations, zero.evaluate_over_domain(domain).evals);
}

Expand Down Expand Up @@ -1030,18 +1021,15 @@ mod tests {
let query_points: Vec<_> = domain.elements().collect();

let eval1 = poly.evaluate_over_domain_by_ref(domain).evals;
let eval2 = query_points
.iter()
.map(|x| poly.evaluate(x))
.collect::<Vec<_>>();
let eval2 = query_points.iter().map(|x| poly.evaluate(x)).collect();

assert_eq!(eval1, eval2);
}

#[test]
fn test_add_assign_with_zero_self() {
// Create a polynomial poly1 which is a zero polynomial
let mut poly1 = DensePolynomial::<Fr> { coeffs: Vec::new() };
let mut poly1 = DensePolynomial { coeffs: Vec::new() };

// Create another polynomial poly2, which is: 2 + 3x (coefficients [2, 3])
let poly2 = DensePolynomial {
Expand All @@ -1064,7 +1052,7 @@ mod tests {
};

// Create an empty polynomial poly2 (zero polynomial)
let poly2 = DensePolynomial::<Fr> { coeffs: Vec::new() };
let poly2 = DensePolynomial { coeffs: Vec::new() };

// Add zero polynomial poly2 to poly1.
// Since poly2 is zero, poly1 should remain unchanged.
Expand Down
16 changes: 8 additions & 8 deletions poly/src/polynomial/univariate/sparse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl<'a, 'b, F: Field> Add<&'a SparsePolynomial<F>> for &'b SparsePolynomial<F>
return self.clone();
}
// Single pass add algorithm (merging two sorted sets)
let mut result = SparsePolynomial::<F>::zero();
let mut result = SparsePolynomial::zero();
// our current index in each vector
let mut self_index = 0;
let mut other_index = 0;
Expand Down Expand Up @@ -262,7 +262,7 @@ impl<F: Field> SparsePolynomial<F> {
*cur_coeff += &(*self_coeff * other_coeff);
}
}
let result = result.into_iter().collect::<Vec<_>>();
let result = result.into_iter().collect();
SparsePolynomial::from_coefficients_vec(result)
}
}
Expand All @@ -285,13 +285,13 @@ impl<F: FftField> SparsePolynomial<F> {
domain: D,
) -> Evaluations<F, D> {
let poly: DenseOrSparsePolynomial<'_, F> = self.into();
DenseOrSparsePolynomial::<F>::evaluate_over_domain(poly, domain)
DenseOrSparsePolynomial::evaluate_over_domain(poly, domain)
}

/// Evaluate `self` over `domain`.
pub fn evaluate_over_domain<D: EvaluationDomain<F>>(self, domain: D) -> Evaluations<F, D> {
let poly: DenseOrSparsePolynomial<'_, F> = self.into();
DenseOrSparsePolynomial::<F>::evaluate_over_domain(poly, domain)
DenseOrSparsePolynomial::evaluate_over_domain(poly, domain)
}
}

Expand All @@ -308,7 +308,7 @@ impl<F: Field> From<SparsePolynomial<F>> for DensePolynomial<F> {
impl<F: Field> From<DensePolynomial<F>> for SparsePolynomial<F> {
fn from(dense_poly: DensePolynomial<F>) -> SparsePolynomial<F> {
let coeffs = dense_poly.coeffs();
let mut sparse_coeffs = Vec::<(usize, F)>::new();
let mut sparse_coeffs = Vec::new();
for (i, coeff) in coeffs.iter().enumerate() {
if !coeff.is_zero() {
sparse_coeffs.push((i, *coeff));
Expand Down Expand Up @@ -452,7 +452,7 @@ mod tests {
assert_eq!(sparse_prod.degree(), dense_prod.degree());
assert_eq!(
sparse_prod,
SparsePolynomial::<Fr>::from(dense_prod),
SparsePolynomial::from(dense_prod),
"degree_a = {}, degree_b = {}",
degree_a,
degree_b
Expand Down Expand Up @@ -541,7 +541,7 @@ mod tests {
.divide_by_vanishing_poly(domain);
assert_eq!(
r,
DensePolynomial::<Fr>::zero(),
DensePolynomial::zero(),
"poly_degree_dim = {}, domain_dim = {}",
poly_degree_dim,
domain_dim
Expand All @@ -552,7 +552,7 @@ mod tests {
.divide_by_vanishing_poly(domain);
assert_eq!(
r,
DensePolynomial::<Fr>::zero(),
DensePolynomial::zero(),
"poly_degree_dim = {}, domain_dim = {}",
poly_degree_dim,
domain_dim
Expand Down
Loading

0 comments on commit df8c32b

Please sign in to comment.