Neptune-Crypto / twenty-first

Collection of mathematics routines and cryptography for the twenty-first century
GNU General Public License v2.0
74 stars 22 forks source link

introduce type `PolynomialDegree` #213

Open jan-ferdinand opened 6 months ago

jan-ferdinand commented 6 months ago

I find the amount of polynomial.degree() as usize in our codebase concerning, especially since .degree() returns an isize. This can cause some very funny behavior. I suggest we express a polynomial's degree as an

enum Degree {
    Positive(usize), // placeholder name, suggestions welcome
    NegativeInfinity,
}

and provide convenience methods as we see fit. For example, a common pattern is to panic! if the degree is NegativeInfinity (currently -1), which could be expressed through:

impl Polynomial {
    /// # Panics
    ///
    /// Panics if `self` is the zero polynomial.
    fn positive_degree(&self) -> usize {
        let Degree::Positive(degree) = self.degree() else {
            panic!("you messed up");
        }
        degree
    }
}