Skip to main content

ps_ecc/polynomial/methods/
is_zero.rs

1use crate::Polynomial;
2
3impl Polynomial {
4    /// Returns `true` if this is the zero polynomial.
5    ///
6    /// A polynomial is zero if all its coefficients are zero. Due to internal
7    /// normalization, this is equivalent to having degree 0 and a zero constant term.
8    #[must_use]
9    #[inline]
10    pub const fn is_zero(&self) -> bool {
11        self.degree == 0 && self.coefficients[0] == 0
12    }
13}
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[test]
20    fn zero_polynomial_is_zero() {
21        let zero = Polynomial::default();
22
23        assert!(zero.is_zero());
24    }
25
26    #[test]
27    fn constant_nonzero_is_not_zero() {
28        let mut p = Polynomial::default();
29
30        p.set(0, 1);
31
32        assert!(!p.is_zero());
33    }
34
35    #[test]
36    fn polynomial_with_higher_degree_is_not_zero() {
37        let mut p = Polynomial::default();
38
39        p.set(2, 1);
40
41        assert!(!p.is_zero());
42    }
43
44    #[test]
45    fn collected_zeros_is_zero() {
46        let p: Polynomial = [0u8; 4].into_iter().collect();
47
48        assert!(p.is_zero());
49    }
50
51    #[test]
52    fn collected_nonzero_is_not_zero() {
53        let p: Polynomial = [1u8, 0, 0, 0].into_iter().collect();
54
55        assert!(!p.is_zero());
56    }
57
58    const _: () = {
59        // Compile-time verification that is_zero is const
60        const ZERO: Polynomial = Polynomial {
61            coefficients: [0; 255],
62            degree: 0,
63        };
64
65        assert!(ZERO.is_zero());
66    };
67}