Skip to main content

ps_ecc/polynomial/implementations/mul/
byte_array.rs

1use std::ops::Mul;
2
3use crate::{error::PolynomialMulError, Polynomial};
4
5impl<const N: usize> Mul<[u8; N]> for &Polynomial {
6    type Output = Result<Polynomial, PolynomialMulError>;
7
8    fn mul(self, rhs: [u8; N]) -> Self::Output {
9        self.mul(rhs.as_slice())
10    }
11}
12
13impl<const N: usize> Mul<[u8; N]> for Polynomial {
14    type Output = Result<Self, PolynomialMulError>;
15
16    fn mul(self, rhs: [u8; N]) -> Self::Output {
17        (&self).mul(rhs.as_slice())
18    }
19}
20
21impl<const N: usize> Mul<&[u8; N]> for &Polynomial {
22    type Output = Result<Polynomial, PolynomialMulError>;
23
24    fn mul(self, rhs: &[u8; N]) -> Self::Output {
25        self.mul(rhs.as_slice())
26    }
27}
28
29impl<const N: usize> Mul<&[u8; N]> for Polynomial {
30    type Output = Result<Self, PolynomialMulError>;
31
32    fn mul(self, rhs: &[u8; N]) -> Self::Output {
33        (&self).mul(rhs.as_slice())
34    }
35}
36
37#[cfg(test)]
38#[allow(clippy::expect_used)]
39mod tests {
40    use std::ops::Mul;
41
42    use crate::Polynomial;
43
44    #[test]
45    fn mul_by_array() {
46        let a = Polynomial::try_from(&[1u8, 2][..]).expect("valid polynomial");
47
48        let result = a.mul([3u8, 4]).expect("degree fits");
49
50        assert_eq!(result.coefficients(), &[3, 2, 8]);
51    }
52
53    #[test]
54    fn mul_by_array_ref() {
55        let a = Polynomial::try_from(&[1u8, 2][..]).expect("valid polynomial");
56
57        let result = a.mul(&[3u8, 4]).expect("degree fits");
58
59        assert_eq!(result.coefficients(), &[3, 2, 8]);
60    }
61}