Debian Codegraph @

src/test/test_ed448.c

raw symbols SCIP
1 #include "ed448.h"
2 #include "mont.h"
3 #include <assert.h>
4
5 const uint8_t Gx[56] = {0x4F, 0x19, 0x70, 0xC6, 0x6B, 0xED, 0x0D, 0xED, 0x22, 0x1D, 0x15, 0xA6, 0x22, 0xBF, 0x36, 0xDA, 0x9E, 0x14, 0x65, 0x70, 0x47, 0x0F, 0x17, 0x67, 0xEA, 0x6D, 0xE3, 0x24, 0xA3, 0xD3, 0xA4, 0x64, 0x12, 0xAE, 0x1A, 0xF7, 0x2A, 0xB6, 0x65, 0x11, 0x43, 0x3B, 0x80, 0xE1, 0x8B, 0x00, 0x93, 0x8E, 0x26, 0x26, 0xA8, 0x2B, 0xC7, 0x0C, 0xC0, 0x5E};
6 const uint8_t Gy[56] = {0x69, 0x3F, 0x46, 0x71, 0x6E, 0xB6, 0xBC, 0x24, 0x88, 0x76, 0x20, 0x37, 0x56, 0xC9, 0xC7, 0x62, 0x4B, 0xEA, 0x73, 0x73, 0x6C, 0xA3, 0x98, 0x40, 0x87, 0x78, 0x9C, 0x1E, 0x05, 0xA0, 0xC2, 0xD7, 0x3A, 0xD3, 0xFF, 0x1C, 0xE6, 0x7C, 0x39, 0xC4, 0xFD, 0xBD, 0x13, 0x2C, 0x4E, 0xD7, 0xC8, 0xAD, 0x98, 0x08, 0x79, 0x5B, 0xF2, 0x30, 0xFA, 0x14};
7 const uint8_t PAIx[56] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
8 const uint8_t PAIy[56] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01};
9 const uint8_t G2x[56] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xA9, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55, 0x55};
10 const uint8_t G2y[56] = {0xAE, 0x05, 0xE9, 0x63, 0x4A, 0xD7, 0x04, 0x8D, 0xB3, 0x59, 0xD6, 0x20, 0x50, 0x86, 0xC2, 0xB0, 0x03, 0x6E, 0xD7, 0xA0, 0x35, 0x88, 0x4D, 0xD7, 0xB7, 0xE3, 0x6D, 0x72, 0x8A, 0xD8, 0xC4, 0xB8, 0x0D, 0x65, 0x65, 0x83, 0x3A, 0x2A, 0x30, 0x98, 0xBB, 0xBC, 0xB2, 0xBE, 0xD1, 0xCD, 0xA0, 0x6B, 0xDA, 0xEA, 0xFB, 0xCD, 0xEA, 0x93, 0x86, 0xED};
11 const uint8_t G3x[56] = {0x08, 0x65, 0x88, 0x6B, 0x91, 0x08, 0xAF, 0x64, 0x55, 0xBD, 0x64, 0x31, 0x6C, 0xB6, 0x94, 0x33, 0x32, 0x24, 0x1B, 0x8B, 0x8C, 0xDA, 0x82, 0xC7, 0xE2, 0xBA, 0x07, 0x7A, 0x4A, 0x3F, 0xCF, 0xE8, 0xDA, 0xA9, 0xCB, 0xF7, 0xF6, 0x27, 0x1F, 0xD6, 0xE8, 0x62, 0xB7, 0x69, 0x46, 0x5D, 0xA8, 0x57, 0x57, 0x28, 0x17, 0x32, 0x86, 0xFF, 0x2F, 0x8F};
12 const uint8_t G3y[56] = {0xE0, 0x05, 0xA8, 0xDB, 0xD5, 0x12, 0x5C, 0xF7, 0x06, 0xCB, 0xDA, 0x7A, 0xD4, 0x3A, 0xA6, 0x44, 0x9A, 0x4A, 0x8D, 0x95, 0x23, 0x56, 0xC3, 0xB9, 0xFC, 0xE4, 0x3C, 0x82, 0xEC, 0x4E, 0x1D, 0x58, 0xBB, 0x3A, 0x33, 0x1B, 0xDB, 0x67, 0x67, 0xF0, 0xBF, 0xFA, 0x9A, 0x68, 0xFE, 0xD0, 0x2D, 0xAF, 0xB8, 0x22, 0xAC, 0x13, 0x58, 0x8E, 0xD6, 0xFC};
13
14 /** Double the generator **/
15 void test_point_double_1(void)
16 {
17 uint8_t outx[56], outy[56];
18 int res;
19
20 EcContext *ec_ctx;
21 PointEd448 *G, *G2;
22
23 memset(outx, 0xFF, sizeof outx);
24 memset(outy, 0xFF, sizeof outy);
25
26 ed448_new_context(&ec_ctx);
27 res = ed448_new_point(&G, Gx, Gy, 56, ec_ctx);
28 assert(res == 0);
29
30 res = ed448_clone(&G2, G);
31 assert(res == 0);
32
33 res = ed448_double(G2);
34 assert(res == 0);
35
36 res = ed448_get_xy(outx, outy, 56, G2);
37 assert(res == 0);
38
39 /* Check */
40 assert(0 == memcmp(outx, G2x, 56));
41 assert(0 == memcmp(outy, G2y, 56));
42
43 ed448_free_point(G);
44 ed448_free_point(G2);
45 ed448_free_context(ec_ctx);
46 }
47
48 /** Double the PAI **/
49 void test_point_double_2(void)
50 {
51 uint8_t outx[56], outy[56];
52 int res;
53
54 EcContext *ec_ctx;
55 PointEd448 *PAI;
56
57 memset(outx, 0xFF, sizeof outx);
58 memset(outy, 0xFF, sizeof outy);
59
60 ed448_new_context(&ec_ctx);
61 res = ed448_new_point(&PAI, PAIx, PAIy, 56, ec_ctx);
62 assert(res == 0);
63
64 res = ed448_double(PAI);
65 assert(res == 0);
66
67 res = ed448_get_xy(outx, outy, 56, PAI);
68 assert(res == 0);
69
70 /* Check */
71 assert(0 == memcmp(outx, PAIx, 56));
72 assert(0 == memcmp(outy, PAIy, 56));
73
74 ed448_free_point(PAI);
75 ed448_free_context(ec_ctx);
76 }
77
78 /** Add the generator to PAI **/
79 void test_point_add_1(void)
80 {
81 uint8_t outx[56], outy[56];
82 int res;
83
84 EcContext *ec_ctx;
85 PointEd448 *G, *PAI, *P;
86
87 memset(outx, 0xFF, sizeof outx);
88 memset(outy, 0xFF, sizeof outy);
89
90 ed448_new_context(&ec_ctx);
91 res = ed448_new_point(&G, Gx, Gy, 56, ec_ctx);
92 assert(res == 0);
93 res = ed448_new_point(&PAI, PAIx, PAIy, 56, ec_ctx);
94 assert(res == 0);
95
96 res = ed448_clone(&P, G);
97 assert(res == 0);
98
99 res = ed448_add(P, PAI);
100 assert(res == 0);
101
102 res = ed448_get_xy(outx, outy, 56, P);
103 assert(res == 0);
104
105 /* Check */
106 assert(0 == memcmp(outx, Gx, 56));
107 assert(0 == memcmp(outy, Gy, 56));
108
109 ed448_free_point(G);
110 ed448_free_point(PAI);
111 ed448_free_point(P);
112 ed448_free_context(ec_ctx);
113 }
114
115 /** Add the generator to itself **/
116 void test_point_add_2(void)
117 {
118 uint8_t outx[56], outy[56];
119 int res;
120
121 EcContext *ec_ctx;
122 PointEd448 *G, *G2;
123
124 memset(outx, 0xFF, sizeof outx);
125 memset(outy, 0xFF, sizeof outy);
126
127 ed448_new_context(&ec_ctx);
128 res = ed448_new_point(&G, Gx, Gy, 56, ec_ctx);
129 assert(res == 0);
130
131 res = ed448_clone(&G2, G);
132 assert(res == 0);
133
134 res = ed448_add(G2, G);
135 assert(res == 0);
136
137 res = ed448_get_xy(outx, outy, 56, G2);
138 assert(res == 0);
139
140 /* Check */
141 assert(0 == memcmp(outx, G2x, 56));
142 assert(0 == memcmp(outy, G2y, 56));
143
144 ed448_free_point(G);
145 ed448_free_point(G2);
146 ed448_free_context(ec_ctx);
147 }
148
149 static void scale(PointEd448 *P, uint64_t *scale, EcContext *ec_ctx)
150 {
151 uint64_t *scratch = P->wp->scratch;
152
153 mont_mult(P->x, P->x, scale, scratch, ec_ctx->mont_ctx);
154 mont_mult(P->y, P->y, scale, scratch, ec_ctx->mont_ctx);
155 mont_mult(P->z, P->z, scale, scratch, ec_ctx->mont_ctx);
156 }
157
158 /** Add G to 2G **/
159 void test_point_add_3(void)
160 {
161 uint8_t outx[56], outy[56];
162 int res;
163
164 EcContext *ec_ctx;
165 PointEd448 *G, *G2, *G3;
166
167 uint64_t *scale1, *scale2;
168
169 memset(outx, 0xFF, sizeof outx);
170 memset(outy, 0xFF, sizeof outy);
171
172 ed448_new_context(&ec_ctx);
173 res = ed448_new_point(&G, Gx, Gy, 56, ec_ctx);
174 assert(res == 0);
175 res = ed448_new_point(&G2, G2x, G2y, 56, ec_ctx);
176 assert(res == 0);
177
178 /** Let's scale Z coords by different factors, so that they are not equal nor 1 **/
179 mont_number(&scale1, 1, ec_ctx->mont_ctx);
180 mont_number(&scale2, 1, ec_ctx->mont_ctx);
181 mont_set(scale1, 0xAABBCCDDEEFF, ec_ctx->mont_ctx);
182 mont_set(scale2, 0xFFEEDDCCBBAA, ec_ctx->mont_ctx);
183 scale(G, scale1, ec_ctx);
184 scale(G2, scale2, ec_ctx);
185
186 res = ed448_clone(&G3, G);
187 assert(res == 0);
188
189 res = ed448_add(G3, G2);
190 assert(res == 0);
191
192 res = ed448_get_xy(outx, outy, 56, G3);
193 assert(res == 0);
194
195 /* Check */
196 assert(0 == memcmp(outx, G3x, 56));
197 assert(0 == memcmp(outy, G3y, 56));
198
199 ed448_free_point(G);
200 ed448_free_point(G2);
201 ed448_free_point(G3);
202 ed448_free_context(ec_ctx);
203 free(scale1);
204 free(scale2);
205 }
206
207 void test_scalar_1(void)
208 {
209 EcContext *ec_ctx;
210 PointEd448 *G, *P, *G259;
211
212 uint8_t outx[56], outy[56];
213 int res;
214
215 ed448_new_context(&ec_ctx);
216 res = ed448_new_point(&G, Gx, Gy, 56, ec_ctx);
217 assert(res == 0);
218 res = ed448_clone(&P, G);
219 assert(res == 0);
220
221 /* Multiply by zero */
222 res = ed448_scalar(P, (uint8_t*)"\x00", 1, 0);
223 assert(res == 0);
224 res = ed448_get_xy(outx, outy, 56, P);
225 assert(res == 0);
226 assert(0 == memcmp(outx, PAIx, 56));
227 assert(0 == memcmp(outy, PAIy, 56));
228
229 /* Multiply by one */
230 res = ed448_copy(P, G);
231 assert(res == 0);
232 res = ed448_scalar(P, (uint8_t*)"\x01", 1, 0);
233 assert(res == 0);
234 res = ed448_get_xy(outx, outy, 56, P);
235 assert(res == 0);
236 assert(0 == memcmp(outx, Gx, 56));
237 assert(0 == memcmp(outy, Gy, 56));
238
239 /* Multiply by three */
240 ed448_copy(P, G);
241 res = ed448_scalar(P, (uint8_t*)"\x03", 1, 0);
242 assert(res == 0);
243 res = ed448_get_xy(outx, outy, 56, P);
244 assert(res == 0);
245 assert(0 == memcmp(outx, G3x, 56));
246 assert(0 == memcmp(outy, G3y, 56));
247
248 /* Multiply by 259 */
249 ed448_new_point(&G259, Gx, Gy, 56, ec_ctx);
250 for (unsigned i=1; i<259; i++)
251 ed448_add(G259, G);
252 ed448_copy(P, G);
253 res = ed448_scalar(P, (uint8_t*)"\x01\x03", 2, 0);
254 assert(res == 0);
255 res = ed448_cmp(P, G259);
256 assert(res == 0);
257
258 ed448_free_point(G);
259 ed448_free_point(G259);
260 ed448_free_point(P);
261 ed448_free_context(ec_ctx);
262 }
263
264 void test_cmp_1(void)
265 {
266 EcContext *ec_ctx;
267 PointEd448 *G, *G2, *P;
268 uint64_t *scale1, *scale2;
269 int res;
270
271 ed448_new_context(&ec_ctx);
272 res = ed448_new_point(&G, Gx, Gy, 56, ec_ctx);
273 assert(res == 0);
274 res = ed448_clone(&P, G);
275 assert(res == 0);
276 res = ed448_new_point(&G2, G2x, G2y, 56, ec_ctx);
277 assert(res == 0);
278
279 mont_number(&scale1, 1, ec_ctx->mont_ctx);
280 mont_number(&scale2, 1, ec_ctx->mont_ctx);
281 mont_set(scale1, 0xAABBCCDDEEFF, ec_ctx->mont_ctx);
282 mont_set(scale2, 0xFFEEDDCCBBAA, ec_ctx->mont_ctx);
283 scale(G, scale1, ec_ctx);
284 scale(P, scale2, ec_ctx);
285
286 res = ed448_cmp(P, G);
287 assert(res == 0);
288
289 res = ed448_cmp(G2, G);
290 assert(res != 0);
291
292 ed448_free_point(G);
293 ed448_free_point(G2);
294 ed448_free_point(P);
295 ed448_free_context(ec_ctx);
296 free(scale1);
297 free(scale2);
298 }
299
300 void test_neg_1(void)
301 {
302 EcContext *ec_ctx;
303 PointEd448 *G, *P, *PAI;
304 int res;
305
306 ed448_new_context(&ec_ctx);
307 res = ed448_new_point(&G, Gx, Gy, 56, ec_ctx);
308 assert(res == 0);
309 res = ed448_clone(&P, G);
310 assert(res == 0);
311 res = ed448_new_point(&PAI, PAIx, PAIy, 56, ec_ctx);
312 assert(res == 0);
313
314 ed448_neg(P);
315 ed448_add(P, G);
316 res = ed448_cmp(P, PAI);
317 assert(res == 0);
318
319 ed448_free_point(G);
320 ed448_free_point(P);
321 ed448_free_point(PAI);
322 ed448_free_context(ec_ctx);
323 }
324
325 int main(void)
326 {
327 test_point_double_1();
328 test_point_double_2();
329 test_point_add_1();
330 test_point_add_2();
331 test_point_add_3();
332 test_scalar_1();
333 test_cmp_1();
334 test_neg_1();
335 return 0;
336 }