Skip to content

Commit

Permalink
Merge pull request #2529 from o1-labs/dw/is-homogeneous-mvpoly
Browse files Browse the repository at this point in the history
MVPoly: move is_homogeneous in trait
  • Loading branch information
dannywillems authored Sep 7, 2024
2 parents b82f789 + 4754761 commit a804af5
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions mvpoly/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,9 @@ pub trait MVPoly<F: PrimeField, const N: usize, const D: usize>:
/// In the near future, the "expression framework" should be moved also into
/// this library.
fn from_expr<Column: Into<usize>>(expr: Expr<ConstantExpr<F>, Column>) -> Self;

/// Returns true if the polynomial is homogeneous (of degree `D`).
/// As a reminder, a polynomial is homogeneous if all its monomials have the
/// same degree.
fn is_homogeneous(&self) -> bool;
}
6 changes: 6 additions & 0 deletions mvpoly/src/monomials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,12 @@ impl<const N: usize, const D: usize, F: PrimeField> MVPoly<F, N, D> for Sparse<F
fn from_expr<Column: Into<usize>>(expr: Expr<ConstantExpr<F>, Column>) -> Self {
prime::Dense::from_expr(expr).into()
}

fn is_homogeneous(&self) -> bool {
self.monomials
.iter()
.all(|(exponents, _)| exponents.iter().sum::<usize>() == D)
}
}

impl<const N: usize, const D: usize, F: PrimeField> From<prime::Dense<F, N, D>>
Expand Down
29 changes: 14 additions & 15 deletions mvpoly/src/prime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,20 @@ impl<F: PrimeField, const N: usize, const D: usize> MVPoly<F, N, D> for Dense<F,
}
}
}

fn is_homogeneous(&self) -> bool {
let normalized_indices = self.normalized_indices.clone();
let mut prime_gen = PrimeNumberGenerator::new();
let is_homogeneous = normalized_indices
.iter()
.zip(self.coeff.clone())
.all(|(idx, c)| {
let decomposition_of_i = naive_prime_factors(*idx, &mut prime_gen);
let monomial_degree = decomposition_of_i.iter().fold(0, |acc, (_, d)| acc + d);
monomial_degree == D || c == F::zero()
});
is_homogeneous
}
}

impl<F: PrimeField, const N: usize, const D: usize> Dense<F, N, D> {
Expand Down Expand Up @@ -471,21 +485,6 @@ impl<F: PrimeField, const N: usize, const D: usize> Dense<F, N, D> {
normalized_indices
}

/// Returns `true` if the polynomial is homoegenous of degree `d`
pub fn is_homogeneous(&self) -> bool {
let normalized_indices = self.normalized_indices.clone();
let mut prime_gen = PrimeNumberGenerator::new();
let is_homogeneous = normalized_indices
.iter()
.zip(self.coeff.clone())
.all(|(idx, c)| {
let decomposition_of_i = naive_prime_factors(*idx, &mut prime_gen);
let monomial_degree = decomposition_of_i.iter().fold(0, |acc, (_, d)| acc + d);
monomial_degree == D || c == F::zero()
});
is_homogeneous
}

pub fn increase_degree<const D_PRIME: usize>(&self) -> Dense<F, N, D_PRIME> {
assert!(D <= D_PRIME, "The degree of the target polynomial must be greater or equal to the degree of the source polynomial");
let mut result: Dense<F, N, D_PRIME> = Dense::zero();
Expand Down

0 comments on commit a804af5

Please sign in to comment.