ps_ecc/polynomial/methods/
eval_at.rs1use crate::Polynomial;
2
3impl Polynomial {
4 #[must_use]
6 pub fn eval_at(&self, x: u8) -> u8 {
7 Self::eval_coefficients_at(self.coefficients(), x)
8 }
9}
10
11#[cfg(test)]
12mod tests {
13 use crate::Polynomial;
14
15 fn assert_exhaustive(p: &Polynomial) {
17 for x in 0..=255u8 {
18 assert_eq!(
19 p.eval_at(x),
20 Polynomial::eval_coefficients_at(p.coefficients(), x)
21 );
22 }
23 }
24
25 #[test]
26 fn zero_polynomial() {
27 assert_exhaustive(&Polynomial::default());
28 }
29
30 #[test]
31 fn constant_polynomial() {
32 let mut p = Polynomial::default();
33
34 p.set(0, 42);
35
36 assert_exhaustive(&p);
37
38 let mut p = Polynomial::default();
39
40 p.set(0, 255);
41
42 assert_exhaustive(&p);
43
44 let mut p = Polynomial::default();
45
46 p.set(0, 1);
47
48 assert_exhaustive(&p);
49 }
50
51 #[test]
52 fn linear_polynomial() {
53 let mut p = Polynomial::default();
54
55 p.set(0, 5);
56 p.set(1, 3);
57
58 assert_exhaustive(&p);
59
60 let mut p = Polynomial::default();
61
62 p.set(0, 0);
63 p.set(1, 1);
64
65 assert_exhaustive(&p);
66
67 let mut p = Polynomial::default();
68
69 p.set(0, 255);
70 p.set(1, 255);
71
72 assert_exhaustive(&p);
73 }
74
75 #[test]
76 fn quadratic_polynomial() {
77 let mut p = Polynomial::default();
78
79 p.set(0, 1);
80 p.set(1, 0);
81 p.set(2, 1);
82
83 assert_exhaustive(&p);
84
85 let mut p = Polynomial::default();
86
87 p.set(0, 0);
88 p.set(1, 0);
89 p.set(2, 1);
90
91 assert_exhaustive(&p);
92
93 let mut p = Polynomial::default();
94
95 p.set(0, 255);
96 p.set(1, 128);
97 p.set(2, 64);
98
99 assert_exhaustive(&p);
100 }
101
102 #[test]
103 fn cubic_polynomial() {
104 let mut p = Polynomial::default();
105
106 p.set(0, 0x12);
107 p.set(1, 0x34);
108 p.set(2, 0x56);
109 p.set(3, 0x78);
110
111 assert_exhaustive(&p);
112
113 let mut p = Polynomial::default();
114
115 p.set(0, 1);
116 p.set(1, 1);
117 p.set(2, 1);
118 p.set(3, 1);
119
120 assert_exhaustive(&p);
121
122 let mut p = Polynomial::default();
123
124 p.set(3, 1);
125
126 assert_exhaustive(&p);
127 }
128
129 #[test]
130 fn high_degree_polynomial() {
131 let mut p = Polynomial::default();
132
133 for i in 0..8 {
134 p.set(i, i + 1);
135 }
136
137 assert_exhaustive(&p);
138
139 let mut p = Polynomial::default();
140
141 p.set(7, 1);
142
143 assert_exhaustive(&p);
144 }
145
146 #[test]
147 fn sparse_polynomial() {
148 let mut p = Polynomial::default();
149
150 p.set(0, 1);
151 p.set(4, 1);
152
153 assert_exhaustive(&p);
154
155 let mut p = Polynomial::default();
156
157 p.set(1, 1);
158 p.set(3, 1);
159
160 assert_exhaustive(&p);
161 }
162
163 #[test]
164 fn all_coefficients_max() {
165 let mut p = Polynomial::default();
166
167 p.set(0, 255);
168 p.set(1, 255);
169 p.set(2, 255);
170 p.set(3, 255);
171
172 assert_exhaustive(&p);
173 }
174
175 #[test]
176 fn max_degree_monomial() {
177 let mut p = Polynomial::default();
178
179 p.set(Polynomial::MAX_DEGREE, 1);
180
181 assert_exhaustive(&p);
182 }
183
184 #[test]
185 fn max_degree_all_ones() {
186 let mut p = Polynomial::default();
187
188 for i in 0..=Polynomial::MAX_DEGREE {
189 p.set(i, 1);
190 }
191
192 assert_exhaustive(&p);
193 }
194
195 #[test]
196 fn max_degree_all_max() {
197 let mut p = Polynomial::default();
198
199 for i in 0..=Polynomial::MAX_DEGREE {
200 p.set(i, 255);
201 }
202
203 assert_exhaustive(&p);
204 }
205}