Skip to main content

ps_ecc/polynomial/methods/
coefficients.rs

1use crate::Polynomial;
2
3impl Polynomial {
4    /// Returns the coefficients of this polynomial, from degree 0 to the leading term.
5    ///
6    /// # Panics
7    ///
8    /// Panics if the stored degree is 255, which exceeds
9    /// [`Polynomial::MAX_DEGREE`] and cannot occur for a valid polynomial.
10    #[must_use]
11    pub fn coefficients(&self) -> &[u8] {
12        #[allow(clippy::expect_used)]
13        self.coefficients.get(..self.degree() as usize + 1).expect(
14            "BUG: Polynomial::coefficients called on an invalid GF(256) polynomial of degree 255",
15        )
16    }
17}
18
19#[cfg(test)]
20mod tests {
21    use crate::Polynomial;
22
23    #[test]
24    fn default_polynomial_has_one_coefficient() {
25        let p = Polynomial::default();
26
27        assert_eq!(p.coefficients(), &[0]);
28    }
29
30    #[test]
31    fn coefficients_match_set_values() {
32        let mut p = Polynomial::default();
33
34        p.set(0, 5);
35        p.set(1, 3);
36        p.set(2, 7);
37
38        assert_eq!(p.coefficients(), &[5, 3, 7]);
39    }
40
41    #[test]
42    fn sparse_polynomial_includes_zeros() {
43        let mut p = Polynomial::default();
44
45        p.set(0, 1);
46        p.set(3, 2);
47
48        assert_eq!(p.coefficients(), &[1, 0, 0, 2]);
49    }
50
51    #[test]
52    fn coefficients_length_is_degree_plus_one() {
53        let mut p = Polynomial::default();
54
55        p.set(5, 1);
56
57        assert_eq!(p.coefficients().len(), 6);
58    }
59
60    #[test]
61    fn coefficients_after_trim() {
62        let mut p = Polynomial::default();
63
64        p.set(4, 1);
65        p.set(4, 0);
66
67        assert_eq!(p.coefficients(), &[0]);
68    }
69}