Debian Codegraph @

src/test/test_ec_ws.c

raw symbols SCIP
1 #include <assert.h>
2 #include "common.h"
3 #include "mont.h"
4 #include "ec.h"
5 #include "endianess.h"
6 #include "modexp_utils.h"
7
8 void print_x(const char *s, const uint64_t *number, const MontContext *ctx);
9 Workplace *new_workplace(const MontContext *ctx);
10 void free_workplace(Workplace *wp);
11
12 void ec_projective_to_affine(uint64_t *x3, uint64_t *y3,
13 const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
14 Workplace *tmp,
15 const MontContext *ctx);
16
17 void ec_full_double(uint64_t *x3, uint64_t *y3, uint64_t *z3,
18 const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
19 const uint64_t *b,
20 Workplace *tmp, const MontContext *ctx);
21 void ec_mix_add(uint64_t *x3, uint64_t *y3, uint64_t *z3,
22 const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
23 const uint64_t *x2, const uint64_t *y2,
24 const uint64_t *b,
25 Workplace *tmp,
26 const MontContext *ctx);
27 void ec_full_add(uint64_t *x3, uint64_t *y3, uint64_t *z3,
28 const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
29 const uint64_t *x2, const uint64_t *y2, const uint64_t *z2,
30 const uint64_t *b,
31 Workplace *tmp,
32 const MontContext *ctx);
33 void ec_scalar(uint64_t *x3, uint64_t *y3, uint64_t *z3,
34 const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
35 uint64_t *b,
36 const uint8_t *exp, size_t exp_size, uint64_t seed,
37 Workplace *wp1,
38 Workplace *wp2,
39 const MontContext *ctx);
40
41 int ec_scalar_g_p256(uint64_t *x3, uint64_t *y3, uint64_t *z3,
42 const uint64_t *b,
43 const uint8_t *exp, size_t exp_size,
44 uint64_t seed,
45 Workplace *wp1,
46 Workplace *wp2,
47 ProtMemory **prot_g,
48 const MontContext *ctx);
49 ProtMemory** ec_scramble_g_p256(const MontContext *ctx, uint64_t seed);
50 void free_g_p256(ProtMemory **prot_g);
51
52 static int matches(const uint64_t *x1, const uint64_t *y1, const uint64_t *z1,
53 const uint64_t *x2, const uint64_t *y2, const uint64_t *z2,
54 Workplace *wp,
55 const MontContext *ctx)
56 {
57 uint64_t *xa, *ya, *xb, *yb;
58 int result;
59
60 mont_number(&xa, 1, ctx);
61 mont_number(&ya, 1, ctx);
62 mont_number(&xb, 1, ctx);
63 mont_number(&yb, 1, ctx);
64
65 ec_projective_to_affine(xa, ya, x1, y1, z1, wp, ctx);
66 ec_projective_to_affine(xb, yb, x2, y2, z2, wp, ctx);
67
68 result = mont_is_equal(xa, xb, ctx);
69 result |= mont_is_equal(ya, yb, ctx);
70
71 free(xa);
72 free(ya);
73 free(xb);
74 free(yb);
75
76 return (result != 0);
77 }
78
79 void test_ec_projective_to_affine(void)
80 {
81 Workplace *wp;
82 MontContext *ctx;
83 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
84 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
85 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
86 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
87 uint64_t *x, *y, *z;
88 uint8_t buffer[32];
89 uint64_t zero[4] = { 0 };
90
91 mont_context_init(&ctx, modulus, sizeof(modulus));
92 wp = new_workplace(ctx);
93
94 /** Arbitrary point on the curve with Z=10 **/
95 mont_from_bytes(&x, (uint8_t*)"\xc6\x4c\x90\xad\x8d\x5c\x1d\x96\xd6\x4b\x63\x46\x4a\x8b\x57\x91\xbf\x48\xa6\xb4\xb9\xbc\xd6\xad\x79\xc6\x3a\x13\xbf\xb7\x78\x5b", 32, ctx);
96 mont_from_bytes(&y, (uint8_t*)"\xe4\x98\x64\xd0\x22\x85\x75\x8a\x11\x79\x68\x2e\x06\x92\x3d\xf7\x62\xa8\x85\xea\xda\xe6\xd9\xb0\x5a\x4f\x0c\x43\x1d\x51\x77\xe4", 32, ctx);
97 mont_from_bytes(&z, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
98 ec_projective_to_affine(x, y, x, y, z, wp, ctx);
99 mont_to_bytes(buffer, 32, x, ctx);
100 assert(0 == memcmp(buffer, (uint8_t*)"\xfa\x3a\xdb\x43\xa7\xbc\x69\x5c\xc8\xa1\x23\x87\x07\x74\x55\x8e\x93\x20\xdd\x79\x5f\x5f\xaf\x11\x58\xfa\x39\x01\xf9\x92\x58\xd5", 32));
101 mont_to_bytes(buffer, 32, y, ctx);
102 assert(0 == memcmp(buffer, (uint8_t*)"\xe3\xa8\xd6\xe0\xd0\x40\x8b\xc1\xce\x8c\x24\x04\x9a\x41\xd2\xff\x23\x77\x40\x98\x49\x17\x15\xc4\xd5\xd4\xb4\x6d\x1c\x88\x25\x96", 32));
103
104 /** Point-at-infinity **/
105 memset(x, 0xFF, 32);
106 memset(y, 0xFF, 32);
107 memset(z, 0, 32);
108 ec_projective_to_affine(x, y, x, y, z, wp, ctx);
109 assert(0 == memcmp(x, zero, 32));
110 assert(0 == memcmp(y, zero, 32));
111
112 free(x);
113 free(y);
114 free(z);
115
116 free_workplace(wp);
117 mont_context_free(ctx);
118 }
119
120 void test_ec_full_double(void)
121 {
122 Workplace *wp;
123 MontContext *ctx;
124 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
125 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
126 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
127 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
128 uint64_t *x, *y, *z;
129 uint64_t *b;
130 uint8_t buffer[32];
131 uint64_t zero[4] = { 0 };
132
133 mont_context_init(&ctx, modulus, sizeof(modulus));
134 wp = new_workplace(ctx);
135 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
136
137 /** Arbitrary point on the curve with Z=10 **/
138 mont_from_bytes(&x, (uint8_t*)"\xc6\x4c\x90\xad\x8d\x5c\x1d\x96\xd6\x4b\x63\x46\x4a\x8b\x57\x91\xbf\x48\xa6\xb4\xb9\xbc\xd6\xad\x79\xc6\x3a\x13\xbf\xb7\x78\x5b", 32, ctx);
139 mont_from_bytes(&y, (uint8_t*)"\xe4\x98\x64\xd0\x22\x85\x75\x8a\x11\x79\x68\x2e\x06\x92\x3d\xf7\x62\xa8\x85\xea\xda\xe6\xd9\xb0\x5a\x4f\x0c\x43\x1d\x51\x77\xe4", 32, ctx);
140 mont_from_bytes(&z, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
141 ec_full_double(x, y, z, x, y, z, b, wp, ctx);
142
143 mont_to_bytes(buffer, 32, x, ctx);
144 assert(0 == memcmp(buffer, "\x9e\x0e\xcb\x70\xd6\x15\x88\x5e\x6a\xce\x5a\x03\x41\x89\xd5\xe5\xf8\xb1\x6f\x38\xe5\xc0\x1e\x59\xf5\xcc\xe6\xdf\xb4\xf9\xdd\x02", 32));
145 mont_to_bytes(buffer, 32, y, ctx);
146 assert(0 == memcmp(buffer, "\xda\x30\xad\x21\x7a\x5d\xe2\x3a\xd6\x86\x12\xd2\x61\xa0\x7b\x51\xff\x05\x3c\x73\xa6\x63\x88\x4b\xa4\xe6\xb6\x84\x71\x9a\xe0\xb4", 32));
147 mont_to_bytes(buffer, 32, z, ctx);
148 assert(0 == memcmp(buffer, "\x62\x60\x97\xCF\xE5\x64\xFC\xD1\x02\x41\xD7\xD1\x63\xBE\xF2\x41\x3D\xA9\xA8\xD6\x60\x5B\x7B\xB5\x7C\x4E\x4A\x21\x69\xA5\xFA\xC2", 32));
149
150 /** Point-at-infinity **/
151 mont_set(x, 0, ctx);
152 mont_set(y, 1, ctx);
153 mont_set(z, 0, ctx);
154 ec_full_double(x, y, z, x, y, z, b, wp, ctx);
155 assert(0 == memcmp(z, zero, 32));
156
157 /* Points with Y=0; for P-256:
158 X = 0x512aecbfc85c47596a7fb7b1285159e35f22b92edfb04634ea63c40cb6134872
159 X = 0xaed5133f37a3b8a79580484ed7aea61ca0dd46d2204fb9cb159c3bf349ecb790
160 */
161 free(x);
162 free(z);
163 mont_from_bytes(&x, (uint8_t*)"\x2b\xad\x3f\x80\xd3\x9a\xc9\x7b\x28\xfd\x2c\xeb\x93\x2d\x82\xe1\xb7\x5b\x3b\xd1\xbc\xe2\xbe\x11\x27\xe5\xa8\x7f\x1c\xc0\xd4\x77", 32, ctx);
164 mont_set(y, 0, ctx);
165 mont_from_bytes(&z, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
166 ec_full_double(x, y, z, x, y, z, b, wp, ctx);
167 assert(0 == memcmp(z, zero, 32));
168
169 free(x);
170 free(y);
171 free(z);
172
173 free(b);
174 free_workplace(wp);
175 mont_context_free(ctx);
176 }
177
178 void test_ec_mix_add_1(void)
179 {
180 Workplace *wp;
181 MontContext *ctx;
182 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
183 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
184 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
185 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
186 uint64_t *x1, *y1, *z1;
187 uint64_t *x2, *y2;
188 uint64_t *b;
189 uint8_t buffer[32];
190
191 mont_context_init(&ctx, modulus, sizeof(modulus));
192 wp = new_workplace(ctx);
193 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
194
195 /* Arbitrary points */
196 mont_from_bytes(&x1, (uint8_t*)"\xc6\x4c\x90\xad\x8d\x5c\x1d\x96\xd6\x4b\x63\x46\x4a\x8b\x57\x91\xbf\x48\xa6\xb4\xb9\xbc\xd6\xad\x79\xc6\x3a\x13\xbf\xb7\x78\x5b", 32, ctx);
197 mont_from_bytes(&y1, (uint8_t*)"\xe4\x98\x64\xd0\x22\x85\x75\x8a\x11\x79\x68\x2e\x06\x92\x3d\xf7\x62\xa8\x85\xea\xda\xe6\xd9\xb0\x5a\x4f\x0c\x43\x1d\x51\x77\xe4", 32, ctx);
198 mont_from_bytes(&z1, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
199 mont_from_bytes(&x2, (uint8_t*)"\xf2\x49\x10\x4d\x0e\x6f\x8f\x29\xe6\x01\x62\x77\x78\x0c\xda\x84\xdc\x84\xb8\x3b\xc3\xd8\x99\xdf\xb7\x36\xca\x08\x31\xfb\xe8\xcf", 32, ctx);
200 mont_from_bytes(&y2, (uint8_t*)"\xb5\x7e\x12\xfc\xdb\x03\x1f\x59\xca\xb8\x1b\x1c\x6b\x1e\x1c\x07\xe4\x51\x2e\x52\xce\x83\x2f\x1a\x0c\xed\xef\xff\x8b\x43\x40\xe9", 32, ctx);
201 ec_mix_add(x1, y1, z1, x1, y1, z1, x2, y2, b, wp, ctx);
202
203 mont_to_bytes(buffer, 32, x1, ctx);
204 assert(0 == memcmp(buffer, "\x13\x6c\x0e\x59\xbe\x5f\xb3\x6f\x98\xce\x1e\xa8\xc3\x14\xf2\xef\x9f\x9e\x53\x99\x8f\xd7\x14\x3a\x98\x86\xeb\x74\xc8\x5a\xf1\xaf", 32));
205 mont_to_bytes(buffer, 32, y1, ctx);
206 assert(0 == memcmp(buffer, "\xcd\x8f\xd0\xf2\xb2\x1f\xcc\x70\xae\x5a\x7f\xa8\x89\xac\xb8\xa2\x01\xc7\x70\xf9\xa3\xaf\x47\x4a\xc1\xb9\xf1\xc5\x62\xf7\x73\x9a", 32));
207 mont_to_bytes(buffer, 32, z1, ctx);
208 assert(0 == memcmp(buffer, "\x7f\x3f\xac\x7e\x49\x3e\x61\x4b\x52\xd8\x49\x31\x8b\x57\xa7\xec\x89\x50\x27\xdb\x75\xbe\xa6\x61\x3c\x54\x42\x89\xb3\x9f\x31\x46", 32));
209
210 /* Affine input point is point-at-infinity */
211 memset(x2, 0, 32);
212 memset(y2, 0, 32);
213 ec_mix_add(x1, y1, z1, x1, y1, z1, x2, y2, b, wp, ctx);
214
215 mont_to_bytes(buffer, 32, x1, ctx);
216 assert(0 == memcmp(buffer, "\x13\x6c\x0e\x59\xbe\x5f\xb3\x6f\x98\xce\x1e\xa8\xc3\x14\xf2\xef\x9f\x9e\x53\x99\x8f\xd7\x14\x3a\x98\x86\xeb\x74\xc8\x5a\xf1\xaf", 32));
217 mont_to_bytes(buffer, 32, y1, ctx);
218 assert(0 == memcmp(buffer, "\xcd\x8f\xd0\xf2\xb2\x1f\xcc\x70\xae\x5a\x7f\xa8\x89\xac\xb8\xa2\x01\xc7\x70\xf9\xa3\xaf\x47\x4a\xc1\xb9\xf1\xc5\x62\xf7\x73\x9a", 32));
219 mont_to_bytes(buffer, 32, z1, ctx);
220 assert(0 == memcmp(buffer, "\x7f\x3f\xac\x7e\x49\x3e\x61\x4b\x52\xd8\x49\x31\x8b\x57\xa7\xec\x89\x50\x27\xdb\x75\xbe\xa6\x61\x3c\x54\x42\x89\xb3\x9f\x31\x46", 32));
221
222 free(x1);
223 free(y1);
224 free(z1);
225 free(x2);
226 free(y2);
227
228 free(b);
229 free_workplace(wp);
230 mont_context_free(ctx);
231 }
232
233 void test_ec_mix_add_2(void)
234 {
235 Workplace *wp;
236 MontContext *ctx;
237 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
238 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
239 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
240 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
241 uint64_t *x1, *y1, *z1;
242 uint64_t *x2, *y2;
243 uint64_t *b;
244 uint8_t buffer[32];
245
246 mont_context_init(&ctx, modulus, sizeof(modulus));
247 wp = new_workplace(ctx);
248 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
249
250 /* Projective input point is point-at-infinity */
251 mont_number(&x1, 1, ctx);
252 mont_set(x1, 0, ctx);
253 mont_number(&y1, 1, ctx);
254 mont_set(y1, 1, ctx);
255 mont_number(&z1, 1, ctx);
256 mont_set(z1, 0, ctx);
257
258 mont_from_bytes(&x2, (uint8_t*)"\xf2\x49\x10\x4d\x0e\x6f\x8f\x29\xe6\x01\x62\x77\x78\x0c\xda\x84\xdc\x84\xb8\x3b\xc3\xd8\x99\xdf\xb7\x36\xca\x08\x31\xfb\xe8\xcf", 32, ctx);
259 mont_from_bytes(&y2, (uint8_t*)"\xb5\x7e\x12\xfc\xdb\x03\x1f\x59\xca\xb8\x1b\x1c\x6b\x1e\x1c\x07\xe4\x51\x2e\x52\xce\x83\x2f\x1a\x0c\xed\xef\xff\x8b\x43\x40\xe9", 32, ctx);
260
261 ec_mix_add(x1, y1, z1, x1, y1, z1, x2, y2, b, wp, ctx);
262
263 ec_projective_to_affine(x1, y1, x1, y1, z1, wp, ctx);
264 mont_to_bytes(buffer, 32, x1, ctx);
265 assert(0 == memcmp(buffer, (uint8_t*)"\xf2\x49\x10\x4d\x0e\x6f\x8f\x29\xe6\x01\x62\x77\x78\x0c\xda\x84\xdc\x84\xb8\x3b\xc3\xd8\x99\xdf\xb7\x36\xca\x08\x31\xfb\xe8\xcf", 32));
266 mont_to_bytes(buffer, 32, y1, ctx);
267 assert(0 == memcmp(buffer, (uint8_t*)"\xb5\x7e\x12\xfc\xdb\x03\x1f\x59\xca\xb8\x1b\x1c\x6b\x1e\x1c\x07\xe4\x51\x2e\x52\xce\x83\x2f\x1a\x0c\xed\xef\xff\x8b\x43\x40\xe9", 32));
268
269 free(x1);
270 free(y1);
271 free(z1);
272 free(x2);
273 free(y2);
274
275 free(b);
276 free_workplace(wp);
277 mont_context_free(ctx);
278 }
279
280 void test_ec_mix_add_3(void)
281 {
282 Workplace *wp;
283 MontContext *ctx;
284 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
285 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
286 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
287 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
288 uint64_t *x1, *y1, *z1;
289 uint64_t *x2, *y2;
290 uint64_t *b;
291 uint8_t buffer[32];
292
293 mont_context_init(&ctx, modulus, sizeof(modulus));
294 wp = new_workplace(ctx);
295 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
296
297 /* Affine and projective are actually the same point (doubling) */
298 mont_from_bytes(&x1, (uint8_t*)"\xc6\x4c\x90\xad\x8d\x5c\x1d\x96\xd6\x4b\x63\x46\x4a\x8b\x57\x91\xbf\x48\xa6\xb4\xb9\xbc\xd6\xad\x79\xc6\x3a\x13\xbf\xb7\x78\x5b", 32, ctx);
299 mont_from_bytes(&y1, (uint8_t*)"\xe4\x98\x64\xd0\x22\x85\x75\x8a\x11\x79\x68\x2e\x06\x92\x3d\xf7\x62\xa8\x85\xea\xda\xe6\xd9\xb0\x5a\x4f\x0c\x43\x1d\x51\x77\xe4", 32, ctx);
300 mont_from_bytes(&z1, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
301
302 mont_from_bytes(&x2, (uint8_t*)"\xfa\x3a\xdb\x43\xa7\xbc\x69\x5c\xc8\xa1\x23\x87\x07\x74\x55\x8e\x93\x20\xdd\x79\x5f\x5f\xaf\x11\x58\xfa\x39\x01\xf9\x92\x58\xd5", 32, ctx);
303 mont_from_bytes(&y2, (uint8_t*)"\xe3\xa8\xd6\xe0\xd0\x40\x8b\xc1\xce\x8c\x24\x04\x9a\x41\xd2\xff\x23\x77\x40\x98\x49\x17\x15\xc4\xd5\xd4\xb4\x6d\x1c\x88\x25\x96", 32, ctx);
304
305 ec_mix_add(x1, y1, z1, x1, y1, z1, x2, y2, b, wp, ctx);
306
307 mont_to_bytes(buffer, 32, x1, ctx);
308 assert(0 == memcmp(buffer, "\x96\x0f\x82\x08\x3a\x75\xf9\xaf\x9a\xab\x06\x05\x27\x0e\x2d\xa8\xb3\x20\x7e\x8d\xf2\xf0\x00\x4d\xb3\x19\x16\xc9\xea\xc5\x0f\x02", 32));
309 mont_to_bytes(buffer, 32, y1, ctx);
310 assert(0 == memcmp(buffer, "\x20\xe6\xe3\x02\xc6\x57\xfa\x95\x30\x39\xa9\x25\xf1\x9d\xc3\xcb\x0f\x59\xa7\x01\x46\xc8\xac\xe2\x09\x54\x3a\x25\x2a\x18\x96\xba", 32));
311 mont_to_bytes(buffer, 32, z1, ctx);
312 assert(0 == memcmp(buffer, "\xb4\x2f\x0b\xc1\x61\x03\x91\xe4\x11\xf1\x4c\x65\xef\x13\xd4\x57\xb1\x41\xb2\x54\xc3\x86\x08\xea\xc6\x5c\xf1\x61\x9d\x37\x6b\x77", 32));
313
314 free(x1);
315 free(y1);
316 free(z1);
317 free(x2);
318 free(y2);
319
320 free(b);
321 free_workplace(wp);
322 mont_context_free(ctx);
323 }
324
325 void test_ec_mix_add_4(void)
326 {
327 Workplace *wp;
328 MontContext *ctx;
329 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
330 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
331 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
332 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
333 uint64_t *x1, *y1, *z1;
334 uint64_t *x2, *y2;
335 uint64_t *b;
336 uint8_t buffer[32];
337
338 mont_context_init(&ctx, modulus, sizeof(modulus));
339 wp = new_workplace(ctx);
340 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
341
342 /* Affine and projective are actually the same point (doubling) */
343 mont_from_bytes(&x1, (uint8_t*)"\xc6\x4c\x90\xad\x8d\x5c\x1d\x96\xd6\x4b\x63\x46\x4a\x8b\x57\x91\xbf\x48\xa6\xb4\xb9\xbc\xd6\xad\x79\xc6\x3a\x13\xbf\xb7\x78\x5b", 32, ctx);
344 mont_from_bytes(&y1, (uint8_t*)"\xe4\x98\x64\xd0\x22\x85\x75\x8a\x11\x79\x68\x2e\x06\x92\x3d\xf7\x62\xa8\x85\xea\xda\xe6\xd9\xb0\x5a\x4f\x0c\x43\x1d\x51\x77\xe4", 32, ctx);
345 mont_from_bytes(&z1, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
346
347 mont_from_bytes(&x2, (uint8_t*)"\xfa\x3a\xdb\x43\xa7\xbc\x69\x5c\xc8\xa1\x23\x87\x07\x74\x55\x8e\x93\x20\xdd\x79\x5f\x5f\xaf\x11\x58\xfa\x39\x01\xf9\x92\x58\xd5", 32, ctx);
348 mont_from_bytes(&y2, (uint8_t*)"\xe3\xa8\xd6\xe0\xd0\x40\x8b\xc1\xce\x8c\x24\x04\x9a\x41\xd2\xff\x23\x77\x40\x98\x49\x17\x15\xc4\xd5\xd4\xb4\x6d\x1c\x88\x25\x96", 32, ctx);
349
350 ec_mix_add(x1, y1, z1, x1, y1, z1, x2, y2, b, wp, ctx);
351
352 mont_to_bytes(buffer, 32, x1, ctx);
353 assert(0 == memcmp(buffer, "\x96\x0f\x82\x08\x3a\x75\xf9\xaf\x9a\xab\x06\x05\x27\x0e\x2d\xa8\xb3\x20\x7e\x8d\xf2\xf0\x00\x4d\xb3\x19\x16\xc9\xea\xc5\x0f\x02", 32));
354 mont_to_bytes(buffer, 32, y1, ctx);
355 assert(0 == memcmp(buffer, "\x20\xe6\xe3\x02\xc6\x57\xfa\x95\x30\x39\xa9\x25\xf1\x9d\xc3\xcb\x0f\x59\xa7\x01\x46\xc8\xac\xe2\x09\x54\x3a\x25\x2a\x18\x96\xba", 32));
356 mont_to_bytes(buffer, 32, z1, ctx);
357 assert(0 == memcmp(buffer, "\xb4\x2f\x0b\xc1\x61\x03\x91\xe4\x11\xf1\x4c\x65\xef\x13\xd4\x57\xb1\x41\xb2\x54\xc3\x86\x08\xea\xc6\x5c\xf1\x61\x9d\x37\x6b\x77", 32));
358
359 free(x1);
360 free(y1);
361 free(z1);
362 free(x2);
363 free(y2);
364
365 free(b);
366 free_workplace(wp);
367 mont_context_free(ctx);
368 }
369
370 void test_ec_full_add(void)
371 {
372 Workplace *wp;
373 MontContext *ctx;
374 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
375 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
376 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
377 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
378 uint64_t *x1, *y1, *z1;
379 uint64_t *x2, *y2, *z2;
380 uint64_t *x3, *y3, *z3;
381 uint64_t *b;
382 uint8_t buffer[32];
383
384 mont_context_init(&ctx, modulus, sizeof(modulus));
385 wp = new_workplace(ctx);
386 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
387
388 /* Arbitrary points */
389 mont_from_bytes(&x1, (uint8_t*)"\xc6\x4c\x90\xad\x8d\x5c\x1d\x96\xd6\x4b\x63\x46\x4a\x8b\x57\x91\xbf\x48\xa6\xb4\xb9\xbc\xd6\xad\x79\xc6\x3a\x13\xbf\xb7\x78\x5b", 32, ctx);
390 mont_from_bytes(&y1, (uint8_t*)"\xe4\x98\x64\xd0\x22\x85\x75\x8a\x11\x79\x68\x2e\x06\x92\x3d\xf7\x62\xa8\x85\xea\xda\xe6\xd9\xb0\x5a\x4f\x0c\x43\x1d\x51\x77\xe4", 32, ctx);
391 mont_from_bytes(&z1, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
392 mont_from_bytes(&x2, (uint8_t*)"\x15\xa0\x46\x37\xa6\x49\xfc\x67\x7a\xb4\xd0\x33\x25\x56\x7d\x14\xb9\xe8\x3a\xbf\x1a\xd1\xe4\x4e\xfa\x1c\x41\xc8\x2f\xb6\x76\x7e", 32, ctx);
393 mont_from_bytes(&y2, (uint8_t*)"\x4b\x3f\xda\x5a\xa0\xaa\xd1\x9f\x4c\xb6\x60\xa8\x24\x50\xf8\xa3\x7a\x8b\x43\x9e\xf0\x93\x35\x53\xe6\x0f\x54\xa6\xae\xd6\x4a\x83", 32, ctx);
394 mont_from_bytes(&z2, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
395 ec_full_add(x1, y1, z1, x1, y1, z1, x2, y2, z2, b, wp, ctx);
396
397 mont_to_bytes(buffer, 32, x1, ctx);
398 assert(0 == memcmp(buffer, "\x0a\x49\xc8\x32\x3a\x4f\x13\x47\x40\x5a\x25\x43\xd4\xc1\xc6\xc8\xf7\x74\x51\xc5\x83\x5c\x82\x20\x9b\x39\x9c\x23\xee\xf6\x29\x2a", 32));
399 mont_to_bytes(buffer, 32, y1, ctx);
400 assert(0 == memcmp(buffer, "\xaa\xb2\xaa\x93\x13\x26\xc2\xfe\x0f\x44\xd3\x2d\xfe\x19\x57\x6e\x6b\xa9\x9a\xc9\x50\xff\x6f\x73\xb5\x8d\xa4\x39\x4c\xf9\x29\xc2", 32));
401 mont_to_bytes(buffer, 32, z1, ctx);
402 assert(0 == memcmp(buffer, "\x7e\x70\xd6\x49\x38\x7c\xde\xe8\x7d\xbc\xe8\x58\x88\xce\xe4\xd9\x33\x50\x9b\xff\x02\xc6\x4f\x0f\x83\x30\xde\x9c\xf6\x38\x4e\xd4", 32));
403
404 /* Same point (doubling) */
405 free(x1); free(y1); free(z1); free(x2); free(y2); free(z2);
406 mont_from_bytes(&x1, (uint8_t*)"\xc6\x4c\x90\xad\x8d\x5c\x1d\x96\xd6\x4b\x63\x46\x4a\x8b\x57\x91\xbf\x48\xa6\xb4\xb9\xbc\xd6\xad\x79\xc6\x3a\x13\xbf\xb7\x78\x5b", 32, ctx);
407 mont_from_bytes(&y1, (uint8_t*)"\xe4\x98\x64\xd0\x22\x85\x75\x8a\x11\x79\x68\x2e\x06\x92\x3d\xf7\x62\xa8\x85\xea\xda\xe6\xd9\xb0\x5a\x4f\x0c\x43\x1d\x51\x77\xe4", 32, ctx);
408 mont_from_bytes(&z1, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
409 mont_from_bytes(&x2, (uint8_t*)"\xfa\x3a\xdb\x43\xa7\xbc\x69\x5c\xc8\xa1\x23\x87\x07\x74\x55\x8e\x93\x20\xdd\x79\x5f\x5f\xaf\x11\x58\xfa\x39\x01\xf9\x92\x58\xd5", 32, ctx);
410 mont_from_bytes(&y2, (uint8_t*)"\xe3\xa8\xd6\xe0\xd0\x40\x8b\xc1\xce\x8c\x24\x04\x9a\x41\xd2\xff\x23\x77\x40\x98\x49\x17\x15\xc4\xd5\xd4\xb4\x6d\x1c\x88\x25\x96", 32, ctx);
411 mont_from_bytes(&z2, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x01", 32, ctx);
412
413 ec_full_add(x1, y1, z1, x1, y1, z1, x2, y2, z2, b, wp, ctx);
414
415 mont_to_bytes(buffer, 32, x1, ctx);
416 assert(0 == memcmp(buffer, "\x96\x0f\x82\x08\x3a\x75\xf9\xaf\x9a\xab\x06\x05\x27\x0e\x2d\xa8\xb3\x20\x7e\x8d\xf2\xf0\x00\x4d\xb3\x19\x16\xc9\xea\xc5\x0f\x02", 32));
417 mont_to_bytes(buffer, 32, y1, ctx);
418 assert(0 == memcmp(buffer, "\x20\xe6\xe3\x02\xc6\x57\xfa\x95\x30\x39\xa9\x25\xf1\x9d\xc3\xcb\x0f\x59\xa7\x01\x46\xc8\xac\xe2\x09\x54\x3a\x25\x2a\x18\x96\xba", 32));
419 mont_to_bytes(buffer, 32, z1, ctx);
420 assert(0 == memcmp(buffer, "\xb4\x2f\x0b\xc1\x61\x03\x91\xe4\x11\xf1\x4c\x65\xef\x13\xd4\x57\xb1\x41\xb2\x54\xc3\x86\x08\xea\xc6\x5c\xf1\x61\x9d\x37\x6b\x77", 32));
421
422 /* Opposite points */
423 mont_set(y2, 0, ctx);
424 mont_sub(y2, y2, y1, wp->scratch, ctx);
425 ec_full_add(x1, y1, z1, x1, y1, z1, x1, y2, z1, b, wp, ctx);
426 assert(mont_is_zero(z1, ctx));
427
428 /* Point at infinity (first term) */
429 free(x1); free(y1); free(z1);
430 mont_from_bytes(&x1, (uint8_t*)"\xf3\x91\x4a\x3a\xf2\x1b\x11\x44\x58\x3e\xf2\xf8\x54\x01\x4b\x72\xfa\x94\x05\x8d\xf9\x7c\x32\x4f\x1a\xef\x49\x37\x3c\xe8\x5b\xef", 32, ctx);
431 mont_from_bytes(&y1, (uint8_t*)"\x23\xaa\x65\x85\x4c\xc5\xbc\x53\x0d\x4f\xe7\x3e\xd9\x58\x95\x67\xb2\xea\x79\x1a\x7c\x9b\xe5\xf6\x78\x8c\xd5\xbe\xd8\x55\x0d\xe7", 32, ctx);
432 mont_from_bytes(&z1, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
433 mont_set(x2, 0, ctx);
434 mont_set(y2, 1, ctx);
435 mont_set(z2, 0, ctx);
436
437 ec_full_add(x2, y2, z2, x2, y2, z2, x1, y1, z1, b, wp, ctx);
438
439 ec_projective_to_affine(x1, y1, x1, y1, z1, wp, ctx);
440 ec_projective_to_affine(x2, y2, x2, y2, z2, wp, ctx);
441
442 assert(mont_is_equal(x1, x2, ctx));
443 assert(mont_is_equal(y1, y2, ctx));
444
445 free(x1); free(y1); free(z1);
446 mont_from_bytes(&x1, (uint8_t*)"\xf3\x91\x4a\x3a\xf2\x1b\x11\x44\x58\x3e\xf2\xf8\x54\x01\x4b\x72\xfa\x94\x05\x8d\xf9\x7c\x32\x4f\x1a\xef\x49\x37\x3c\xe8\x5b\xef", 32, ctx);
447 mont_from_bytes(&y1, (uint8_t*)"\x23\xaa\x65\x85\x4c\xc5\xbc\x53\x0d\x4f\xe7\x3e\xd9\x58\x95\x67\xb2\xea\x79\x1a\x7c\x9b\xe5\xf6\x78\x8c\xd5\xbe\xd8\x55\x0d\xe7", 32, ctx);
448 mont_from_bytes(&z1, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
449 mont_set(x2, 0, ctx);
450 mont_set(y2, 1, ctx);
451 mont_set(z2, 0, ctx);
452 mont_number(&x3, 1, ctx);
453 mont_number(&y3, 1, ctx);
454 mont_number(&z3, 1, ctx);
455
456 ec_full_add(x3, y3, z3, x1, y1, z1, x2, y2, z2, b, wp, ctx);
457
458 ec_projective_to_affine(x1, y1, x1, y1, z1, wp, ctx);
459 ec_projective_to_affine(x3, y3, x3, y3, z3, wp, ctx);
460
461 assert(mont_is_equal(x1, x3, ctx));
462 assert(mont_is_equal(y1, y3, ctx));
463
464 free(x1);
465 free(y1);
466 free(z1);
467 free(x2);
468 free(y2);
469 free(z2);
470 free(x3);
471 free(y3);
472 free(z3);
473
474 free(b);
475 free_workplace(wp);
476 mont_context_free(ctx);
477 }
478
479 void test_ec_scalar(void)
480 {
481 Workplace *wp1, *wp2;
482 MontContext *ctx;
483 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
484 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
485 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
486 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
487 uint64_t *x1, *y1, *z1;
488 uint64_t *x2, *y2, *z2;
489 uint64_t *b;
490 uint8_t buffer[32];
491
492 mont_context_init(&ctx, modulus, sizeof(modulus));
493 wp1 = new_workplace(ctx);
494 wp2 = new_workplace(ctx);
495 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
496
497 /* 1*G */
498 mont_number(&x1, 1, ctx);
499 mont_number(&y1, 1, ctx);
500 mont_number(&z1, 1, ctx);
501 mont_from_bytes(&x2, (uint8_t*)"\x2e\xee\x33\x80\xcb\xba\x96\xcb\xb7\x61\x04\xf5\xe4\x6a\x89\x78\xa6\x22\xe7\x07\xcb\x30\x04\x49\x8e\x4c\x3c\xba\x75\xf7\x99\xe0", 32, ctx);
502 mont_from_bytes(&y2, (uint8_t*)"\x1e\xe0\x9c\xe0\xed\x08\xfc\x10\x95\x0f\x30\xe8\xd8\x9c\x2c\xdd\xb6\x0e\x01\x67\x2f\xed\xb4\x13\xf5\x1e\x84\x12\x2d\x79\x33\x95", 32, ctx);
503 mont_from_bytes(&z2, (uint8_t*)"\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0a", 32, ctx);
504
505 ec_scalar(x1, y1, z1, x2, y2, z2, b, (uint8_t*)"\x01", 1, 0x4545, wp1, wp2, ctx);
506
507 assert(matches(x1, y1, z1, x2, y2, z2, wp1, ctx));
508
509 ec_scalar(x1, y1, z1, x2, y2, z2, b, (uint8_t*)"\x00\x01", 2, 0x4545, wp1, wp2, ctx);
510
511 assert(matches(x1, y1, z1, x2, y2, z2, wp1, ctx));
512
513 /* (order+1)*G */
514 ec_scalar(x1, y1, z1, x2, y2, z2, b, (uint8_t*)"\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xbc\xe6\xfa\xad\xa7\x17\x9e\x84\xf3\xb9\xca\xc2\xfc\x63\x25\x52", 32, 0x4545, wp1, wp2, ctx);
515 ec_projective_to_affine(x1, y1, x1, y1, z1, wp1, ctx);
516 mont_to_bytes(buffer, 32, x1, ctx);
517 assert(0 == memcmp(buffer, "\x6b\x17\xd1\xf2\xe1\x2c\x42\x47\xf8\xbc\xe6\xe5\x63\xa4\x40\xf2\x77\x03\x7d\x81\x2d\xeb\x33\xa0\xf4\xa1\x39\x45\xd8\x98\xc2\x96", 32));
518 mont_to_bytes(buffer, 32, y1, ctx);
519 assert(0 == memcmp(buffer, "\x4f\xe3\x42\xe2\xfe\x1a\x7f\x9b\x8e\xe7\xeb\x4a\x7c\x0f\x9e\x16\x2b\xce\x33\x57\x6b\x31\x5e\xce\xcb\xb6\x40\x68\x37\xbf\x51\xf5", 32));
520
521 /* 0*G */
522 ec_scalar(x1, y1, z1, x2, y2, z2, b, (uint8_t*)"\x00", 1, 0x4545, wp1, wp2, ctx);
523 assert(mont_is_zero(z1, ctx));
524
525 /* order*G */
526 ec_scalar(x1, y1, z1, x2, y2, z2, b, (uint8_t*)"\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xbc\xe6\xfa\xad\xa7\x17\x9e\x84\xf3\xb9\xca\xc2\xfc\x63\x25\x51", 32, 0x4545, wp1, wp2, ctx);
527 assert(mont_is_zero(z1, ctx));
528
529 /* 255*O */
530 ec_scalar(x1, y1, z1, x1, y1, z1, b, (uint8_t*)"\x00\xFF", 2, 0x4545, wp1, wp2, ctx);
531 assert(mont_is_zero(z1, ctx));
532
533 /* arb */
534 free(x2);
535 free(y2);
536 mont_from_bytes(&x2, (uint8_t*)"\xde\x24\x44\xbe\xbc\x8d\x36\xe6\x82\xed\xd2\x7e\x0f\x27\x15\x08\x61\x75\x19\xb3\x22\x1a\x8f\xa0\xb7\x7c\xab\x39\x89\xda\x97\xc9", 32, ctx);
537 mont_from_bytes(&y2, (uint8_t*)"\xc0\x93\xae\x7f\xf3\x6e\x53\x80\xfc\x01\xa5\xaa\xd1\xe6\x66\x59\x70\x2d\xe8\x0f\x53\xce\xc5\x76\xb6\x35\x0b\x24\x30\x42\xa2\x56", 32, ctx);
538 mont_set(z2, 1, ctx);
539 ec_scalar(x1, y1, z1, x2, y2, z2, b, (uint8_t*)"\xc5\x1e\x47\x53\xaf\xde\xc1\xe6\xb6\xc6\xa5\xb9\x92\xf4\x3f\x8d\xd0\xc7\xa8\x93\x30\x72\x70\x8b\x65\x22\x46\x8b\x2f\xfb\x06\xfd", 32, 0x4545, wp1, wp2, ctx);
540 ec_projective_to_affine(x1, y1, x1, y1, z1, wp1, ctx);
541 mont_to_bytes(buffer, 32, x1, ctx);
542 assert(0 == memcmp(buffer, "\x51\xd0\x8d\x5f\x2d\x42\x78\x88\x29\x46\xd8\x8d\x83\xc9\x7d\x11\xe6\x2b\xec\xc3\xcf\xc1\x8b\xed\xac\xc8\x9b\xa3\x4e\xec\xa0\x3f", 32));
543 mont_to_bytes(buffer, 32, y1, ctx);
544 assert(0 == memcmp(buffer, "\x75\xee\x68\xeb\x8b\xf6\x26\xaa\x5b\x67\x3a\xb5\x1f\x6e\x74\x4e\x06\xf8\xfc\xf8\xa6\xc0\xcf\x30\x35\xbe\xca\x95\x6a\x7b\x41\xd5", 32));
545
546 free(x1);
547 free(y1);
548 free(z1);
549 free(x2);
550 free(y2);
551 free(z2);
552
553 free(b);
554 free_workplace(wp1);
555 free_workplace(wp2);
556 mont_context_free(ctx);
557 }
558
559 void test_ec_scalar_g_p256(void)
560 {
561 Workplace *wp1, *wp2;
562 MontContext *ctx;
563 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
564 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
565 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
566 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
567 const uint8_t Gx[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
568 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
569 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
570 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96};
571 const uint8_t Gy[32] = {0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
572 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
573 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
574 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5};
575 uint64_t *b;
576 int res;
577
578 uint64_t *x1, *y1, *z1;
579 uint64_t *xw, *yw;
580 uint64_t *Gx_mont, *Gy_mont;
581 uint8_t buffer[32];
582 ProtMemory **prot_g;
583
584 mont_context_init(&ctx, modulus, sizeof(modulus));
585 wp1 = new_workplace(ctx);
586 wp2 = new_workplace(ctx);
587 mont_from_bytes(&b, (uint8_t*)"\x5a\xc6\x35\xd8\xaa\x3a\x93\xe7\xb3\xeb\xbd\x55\x76\x98\x86\xbc\x65\x1d\x06\xb0\xcc\x53\xb0\xf6\x3b\xce\x3c\x3e\x27\xd2\x60\x4b", 32, ctx);
588 prot_g = ec_scramble_g_p256(ctx, 0x1010);
589
590 mont_from_bytes(&Gx_mont, Gx, sizeof Gx, ctx);
591 mont_from_bytes(&Gy_mont, Gy, sizeof Gy, ctx);
592 mont_number(&xw, 1, ctx);
593 mont_number(&yw, 1, ctx);
594
595 /* 1*G */
596 mont_number(&x1, 1, ctx);
597 mont_number(&y1, 1, ctx);
598 mont_number(&z1, 1, ctx);
599 res = ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\x01", 1, 0x4545, wp1, wp2, prot_g, ctx);
600 assert(res == 0);
601 ec_projective_to_affine(xw, yw, x1, y1, z1, wp1, ctx);
602 assert(mont_is_equal(xw, Gx_mont, ctx));
603 assert(mont_is_equal(yw, Gy_mont, ctx));
604
605 ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\x00\x01", 2, 0x4545, wp1, wp2, prot_g, ctx);
606 ec_projective_to_affine(xw, yw, x1, y1, z1, wp1, ctx);
607 assert(mont_is_equal(xw, Gx_mont, ctx));
608 assert(mont_is_equal(yw, Gy_mont, ctx));
609
610 /* 0*G */
611 ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\x00", 1, 0x4545, wp1, wp2, prot_g, ctx);
612 assert(mont_is_zero(z1, ctx));
613
614 /* 31*G */
615 ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\x1F", 1, 0x4545, wp1, wp2, prot_g, ctx);
616 ec_projective_to_affine(xw, yw, x1, y1, z1, wp1, ctx);
617 mont_to_bytes(buffer, 32, xw, ctx);
618 assert(0 == memcmp(buffer, "\x30\x1d\x9e\x50\x2d\xc7\xe0\x5d\xa8\x5d\xa0\x26\xa7\xae\x9a\xa0\xfa\xc9\xdb\x7d\x52\xa9\x5b\x3e\x3e\x3f\x9a\xa0\xa1\xb4\x5b\x8b", 32));
619 mont_to_bytes(buffer, 32, yw, ctx);
620 assert(0 == memcmp(buffer, "\x65\x51\xb6\xf6\xb3\x06\x12\x23\xe0\xd2\x3c\x02\x6b\x01\x7d\x72\x29\x8d\x9a\xe4\x68\x87\xca\x61\xd5\x8d\xb6\xae\xa1\x7e\xe2\x67", 32));
621
622 /* 32*G */
623 ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\x20", 1, 0x4545, wp1, wp2, prot_g, ctx);
624 ec_projective_to_affine(xw, yw, x1, y1, z1, wp1, ctx);
625 mont_to_bytes(buffer, 32, xw, ctx);
626 assert(0 == memcmp(buffer, "\x23\x77\xc7\xd6\x90\xa2\x42\xca\x6c\x45\x07\x4e\x8e\xa5\xbe\xef\xaa\x55\x7f\xd5\xb6\x83\x71\xd9\xd1\x47\x5b\xd5\x2a\x7e\xd0\xe1", 32));
627 mont_to_bytes(buffer, 32, yw, ctx);
628 assert(0 == memcmp(buffer, "\x47\xa1\x3f\xb9\x84\x13\xa4\x39\x3f\x8d\x90\xe9\xbf\x90\x1b\x7e\x66\x58\xa6\xcd\xec\xf4\x67\x16\xe7\xc0\x67\xb1\xdd\xb8\xd2\xb2", 32));
629
630 /* (order+1)*G */
631 ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xbc\xe6\xfa\xad\xa7\x17\x9e\x84\xf3\xb9\xca\xc2\xfc\x63\x25\x52", 32, 0x4545, wp1, wp2, prot_g, ctx);
632 ec_projective_to_affine(x1, y1, x1, y1, z1, wp1, ctx);
633 mont_to_bytes(buffer, 32, x1, ctx);
634 assert(0 == memcmp(buffer, "\x6b\x17\xd1\xf2\xe1\x2c\x42\x47\xf8\xbc\xe6\xe5\x63\xa4\x40\xf2\x77\x03\x7d\x81\x2d\xeb\x33\xa0\xf4\xa1\x39\x45\xd8\x98\xc2\x96", 32));
635 mont_to_bytes(buffer, 32, y1, ctx);
636 assert(0 == memcmp(buffer, "\x4f\xe3\x42\xe2\xfe\x1a\x7f\x9b\x8e\xe7\xeb\x4a\x7c\x0f\x9e\x16\x2b\xce\x33\x57\x6b\x31\x5e\xce\xcb\xb6\x40\x68\x37\xbf\x51\xf5", 32));
637
638 /* order*G */
639 ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xbc\xe6\xfa\xad\xa7\x17\x9e\x84\xf3\xb9\xca\xc2\xfc\x63\x25\x51", 32, 0x4545, wp1, wp2, prot_g, ctx);
640 assert(mont_is_zero(z1, ctx));
641
642 /* arbirtrary */
643 ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\x73\x87\x34\x34\x3F\xF8\x93\x87", 8, 0x6776, wp1, wp2, prot_g, ctx);
644 ec_projective_to_affine(x1, y1, x1, y1, z1, wp1, ctx);
645 mont_to_bytes(buffer, 32, x1, ctx);
646 assert(0 == memcmp(buffer, "\xfc\x85\x6a\x26\x35\x51\x2a\x83\x44\x35\x55\x97\xbd\xbf\xa9\x3d\x33\x70\x2a\x48\xb0\x9d\x02\xbd\x1d\xc4\xfd\x4b\x5a\x4c\x6c\x09", 32));
647 mont_to_bytes(buffer, 32, y1, ctx);
648 assert(0 == memcmp(buffer, "\xcf\x0d\xc7\x68\x18\x61\xa0\xb7\x29\x22\xa9\xce\x17\xf1\x58\x22\x31\x1a\xab\x2a\x14\xc4\xbd\xb0\xc4\x32\xea\xfe\x93\x9a\x4a\x47", 32));
649
650 /* exponent is too long */
651 res = ec_scalar_g_p256(x1, y1, z1, b, (uint8_t*)"\xff\xff\xff\xff\x00\x00\x00\x00\xff\xff\xff\xff\xff\xff\xff\xff\xbc\xe6\xfa\xad\xa7\x17\x9e\x84\xf3\xb9\xca\xc2\xfc\x63\x25\x52\xFF", 33, 0x4545, wp1, wp2, prot_g, ctx);
652 assert(res == ERR_VALUE);
653
654 free_g_p256(prot_g);
655 free(b);
656 free(x1);
657 free(y1);
658 free(z1);
659 free(xw);
660 free(yw);
661 free(Gx_mont);
662 free(Gy_mont);
663 free_workplace(wp1);
664 free_workplace(wp2);
665 mont_context_free(ctx);
666
667 }
668
669
670 void test_ec_ws_new_point(void)
671 {
672 EcContext *ec_ctx;
673 EcPoint *ecp;
674 int res;
675 const uint8_t Gx[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
676 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
677 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
678 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96};
679 const uint8_t Gx_wrong[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
680 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
681 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
682 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x97};
683 const uint8_t Gy[32] = {0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
684 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
685 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
686 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5};
687 const uint8_t b[32] = {0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7,
688 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC,
689 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
690 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B};
691 const uint8_t order[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
692 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
693 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
694 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51};
695 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
696 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
697 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
698 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
699 uint8_t zero[32] = { 0 };
700
701 res = ec_ws_new_context(&ec_ctx, modulus, b, order, 32, 0);
702 assert(res == 0);
703 res = ec_ws_new_point(NULL, Gx, Gy, 32, ec_ctx);
704 assert(res == ERR_NULL);
705 res = ec_ws_new_point(&ecp, NULL, Gy, 32, ec_ctx);
706 assert(res == ERR_NULL);
707 res = ec_ws_new_point(&ecp, Gx, NULL, 32, ec_ctx);
708 assert(res == ERR_NULL);
709 res = ec_ws_new_point(&ecp, Gx, Gy, 32, NULL);
710 assert(res == ERR_NULL);
711
712 res = ec_ws_new_point(&ecp, Gx, Gy, 0, ec_ctx);
713 assert(res == ERR_NOT_ENOUGH_DATA);
714
715 res = ec_ws_new_point(&ecp, Gx_wrong, Gy, 32, ec_ctx);
716 assert(res == ERR_EC_POINT);
717
718 res = ec_ws_new_point(&ecp, Gx, Gy, 32, ec_ctx);
719 assert(res == 0);
720
721 ec_ws_free_point(ecp);
722 res = ec_ws_new_point(&ecp, zero, zero, 32, ec_ctx);
723 assert(res == 0);
724
725 ec_ws_free_point(ecp);
726 ec_free_context(ec_ctx);
727 }
728
729 void test_ec_ws_get_xy(void)
730 {
731 EcContext *ec_ctx;
732 EcPoint *ecp;
733 int res;
734 const uint8_t Gx[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
735 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
736 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
737 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96};
738 const uint8_t Gy[32] = {0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
739 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
740 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
741 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5};
742 const uint8_t b[32] = {0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7,
743 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC,
744 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
745 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B};
746 const uint8_t order[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
747 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
748 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
749 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51};
750 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
751 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
752 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
753 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
754 uint8_t bufx[32], bufy[32];
755
756 res = ec_ws_new_context(&ec_ctx, modulus, b, order, 32, 0);
757 assert(res == 0);
758 res = ec_ws_new_point(&ecp, Gx, Gy, 32, ec_ctx);
759 assert(res == 0);
760 assert(ecp != NULL);
761
762 res = ec_ws_get_xy(NULL, bufy, 32, ecp);
763 assert(res == ERR_NULL);
764 res = ec_ws_get_xy(bufx, NULL, 32, ecp);
765 assert(res == ERR_NULL);
766 res = ec_ws_get_xy(bufx, bufy, 32, NULL);
767 assert(res == ERR_NULL);
768
769 res = ec_ws_get_xy(bufx, bufy, 31, ecp);
770 assert(res == ERR_NOT_ENOUGH_DATA);
771
772 res = ec_ws_get_xy(bufx, bufy, 32, ecp);
773 assert(res == 0);
774
775 assert(0 == memcmp(bufx, Gx, 32));
776 assert(0 == memcmp(bufy, Gy, 32));
777
778 ec_ws_free_point(ecp);
779 ec_free_context(ec_ctx);
780 }
781
782 void test_ec_ws_double_p256(void)
783 {
784 EcContext *ec_ctx;
785 EcPoint *ecp;
786 int res;
787 const uint8_t Gx[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
788 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
789 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
790 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96};
791 const uint8_t Gy[32] = {0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
792 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
793 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
794 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5};
795 const uint8_t b[32] = {0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7,
796 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC,
797 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
798 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B};
799 const uint8_t order[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
800 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
801 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
802 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51};
803 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
804 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
805 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
806 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
807 uint8_t bufx[32], bufy[32];
808
809 ec_ws_new_context(&ec_ctx, modulus, b, order, 32, 0);
810 ec_ws_new_point(&ecp, Gx, Gy, 32, ec_ctx);
811
812 res = ec_ws_double(NULL);
813 assert(res == ERR_NULL);
814
815 res = ec_ws_double(ecp);
816 assert(res == 0);
817 ec_ws_get_xy(bufx, bufy, 32, ecp);
818 assert(0 == memcmp(bufx, "\x7c\xf2\x7b\x18\x8d\x03\x4f\x7e\x8a\x52\x38\x03\x04\xb5\x1a\xc3\xc0\x89\x69\xe2\x77\xf2\x1b\x35\xa6\x0b\x48\xfc\x47\x66\x99\x78", 32));
819 assert(0 == memcmp(bufy, "\x07\x77\x55\x10\xdb\x8e\xd0\x40\x29\x3d\x9a\xc6\x9f\x74\x30\xdb\xba\x7d\xad\xe6\x3c\xe9\x82\x29\x9e\x04\xb7\x9d\x22\x78\x73\xd1", 32));
820
821 ec_ws_free_point(ecp);
822 ec_free_context(ec_ctx);
823 }
824
825 void test_ec_ws_double_p521(void)
826 {
827 EcContext *ec_ctx;
828 EcPoint *ecp;
829 int res;
830 const uint8_t Px[66] = {0x01, 0xD5, 0xC6, 0x93, 0xF6, 0x6C, 0x08, 0xED,
831 0x03, 0xAD, 0x0F, 0x03, 0x1F, 0x93, 0x74, 0x43,
832 0x45, 0x8F, 0x60, 0x1F, 0xD0, 0x98, 0xD3, 0xD0,
833 0x22, 0x7B, 0x4B, 0xF6, 0x28, 0x73, 0xAF, 0x50,
834 0x74, 0x0B, 0x0B, 0xB8, 0x4A, 0xA1, 0x57, 0xFC,
835 0x84, 0x7B, 0xCF, 0x8D, 0xC1, 0x6A, 0x8B, 0x2B,
836 0x8B, 0xFD, 0x8E, 0x2D, 0x0A, 0x7D, 0x39, 0xAF,
837 0x04, 0xB0, 0x89, 0x93, 0x0E, 0xF6, 0xDA, 0xD5,
838 0xC1, 0xB4};
839 const uint8_t Py[66] = {0x01, 0x44, 0xB7, 0x77, 0x09, 0x63, 0xC6, 0x3A,
840 0x39, 0x24, 0x88, 0x65, 0xFF, 0x36, 0xB0, 0x74,
841 0x15, 0x1E, 0xAC, 0x33, 0x54, 0x9B, 0x22, 0x4A,
842 0xF5, 0xC8, 0x66, 0x4C, 0x54, 0x01, 0x2B, 0x81,
843 0x8E, 0xD0, 0x37, 0xB2, 0xB7, 0xC1, 0xA6, 0x3A,
844 0xC8, 0x9E, 0xBA, 0xA1, 0x1E, 0x07, 0xDB, 0x89,
845 0xFC, 0xEE, 0x5B, 0x55, 0x6E, 0x49, 0x76, 0x4E,
846 0xE3, 0xFA, 0x66, 0xEA, 0x7A, 0xE6, 0x1A, 0xC0,
847 0x18, 0x23};
848 const uint8_t b[66] = {0x00, 0x51, 0x95, 0x3E, 0xB9, 0x61, 0x8E, 0x1C,
849 0x9A, 0x1F, 0x92, 0x9A, 0x21, 0xA0, 0xB6, 0x85,
850 0x40, 0xEE, 0xA2, 0xDA, 0x72, 0x5B, 0x99, 0xB3,
851 0x15, 0xF3, 0xB8, 0xB4, 0x89, 0x91, 0x8E, 0xF1,
852 0x09, 0xE1, 0x56, 0x19, 0x39, 0x51, 0xEC, 0x7E,
853 0x93, 0x7B, 0x16, 0x52, 0xC0, 0xBD, 0x3B, 0xB1,
854 0xBF, 0x07, 0x35, 0x73, 0xDF, 0x88, 0x3D, 0x2C,
855 0x34, 0xF1, 0xEF, 0x45, 0x1F, 0xD4, 0x6B, 0x50,
856 0x3F, 0x00};
857 const uint8_t order[66] = {0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
858 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
859 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
860 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
861 0xFF, 0xFA, 0x51, 0x86, 0x87, 0x83, 0xBF, 0x2F,
862 0x96, 0x6B, 0x7F, 0xCC, 0x01, 0x48, 0xF7, 0x09,
863 0xA5, 0xD0, 0x3B, 0xB5, 0xC9, 0xB8, 0x89, 0x9C,
864 0x47, 0xAE, 0xBB, 0x6F, 0xB7, 0x1E, 0x91, 0x38,
865 0x64, 0x09};
866 const uint8_t modulus[66] = {0x01, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
867 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
868 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
869 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
870 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
871 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
872 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
873 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
874 0xFF, 0xFF};
875 uint8_t bufx[66], bufy[66];
876
877 ec_ws_new_context(&ec_ctx, modulus, b, order, 66, 0);
878 ec_ws_new_point(&ecp, Px, Py, 66, ec_ctx);
879
880 res = ec_ws_double(NULL);
881 assert(res == ERR_NULL);
882
883 res = ec_ws_double(ecp);
884 assert(res == 0);
885 ec_ws_get_xy(bufx, bufy, 66, ecp);
886 assert(0 == memcmp(bufx, "\x01\x28\x79\x44\x2F\x24\x50\xC1\x19\xE7\x11\x9A\x5F\x73\x8B\xE1\xF1\xEB\xA9\xE9\xD7\xC6\xCF\x41\xB3\x25\xD9\xCE\x6D\x64\x31\x06\xE9\xD6\x11\x24\xA9\x1A\x96\xBC\xF2\x01\x30\x5A\x9D\xEE\x55\xFA\x79\x13\x6D\xC7\x00\x83\x1E\x54\xC3\xCA\x4F\xF2\x64\x6B\xD3\xC3\x6B\xC6", 66));
887 assert(0 == memcmp(bufy, "\x01\x98\x64\xA8\xB8\x85\x5C\x24\x79\xCB\xEF\xE3\x75\xAE\x55\x3E\x23\x93\x27\x1E\xD3\x6F\xAD\xFC\x44\x94\xFC\x05\x83\xF6\xBD\x03\x59\x88\x96\xF3\x98\x54\xAB\xEA\xE5\xF9\xA6\x51\x5A\x02\x1E\x2C\x0E\xEF\x13\x9E\x71\xDE\x61\x01\x43\xF5\x33\x82\xF4\x10\x4D\xCC\xB5\x43", 66));
888
889 ec_ws_free_point(ecp);
890 ec_free_context(ec_ctx);
891 }
892
893 void test_ec_ws_add(void)
894 {
895 EcContext *ec_ctx;
896 EcPoint *ecp, *ecp2;
897 int res;
898 const uint8_t Gx[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
899 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
900 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
901 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96};
902 const uint8_t Gy[32] = {0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
903 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
904 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
905 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5};
906 const uint8_t b[32] = {0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7,
907 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC,
908 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
909 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B};
910 const uint8_t order[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
911 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
912 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
913 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51};
914 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
915 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
916 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
917 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
918 uint8_t bufx[32], bufy[32];
919
920 ec_ws_new_context(&ec_ctx, modulus, b, order, 32, 0);
921 ec_ws_new_point(&ecp, Gx, Gy, 32, ec_ctx);
922 ec_ws_new_point(&ecp2, Gx, Gy, 32, ec_ctx);
923 ec_ws_double(ecp2);
924
925 res = ec_ws_add(NULL, ecp);
926 assert(res == ERR_NULL);
927 res = ec_ws_add(ecp, NULL);
928 assert(res == ERR_NULL);
929
930 res = ec_ws_add(ecp, ecp2);
931 assert(res == 0);
932 ec_ws_get_xy(bufx, bufy, 32, ecp);
933 assert(0 == memcmp(bufx, "\x5e\xcb\xe4\xd1\xa6\x33\x0a\x44\xc8\xf7\xef\x95\x1d\x4b\xf1\x65\xe6\xc6\xb7\x21\xef\xad\xa9\x85\xfb\x41\x66\x1b\xc6\xe7\xfd\x6c", 32));
934 assert(0 == memcmp(bufy, "\x87\x34\x64\x0c\x49\x98\xff\x7e\x37\x4b\x06\xce\x1a\x64\xa2\xec\xd8\x2a\xb0\x36\x38\x4f\xb8\x3d\x9a\x79\xb1\x27\xa2\x7d\x50\x32", 32));
935
936 ec_ws_free_point(ecp);
937 ec_ws_free_point(ecp2);
938 ec_free_context(ec_ctx);
939 }
940
941 void test_ec_ws_scalar(void)
942 {
943 EcContext *ec_ctx;
944 EcPoint *ecp;
945 int res;
946 const uint8_t Gx[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
947 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
948 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
949 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96};
950 const uint8_t Gy[32] = {0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
951 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
952 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
953 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5};
954 const uint8_t b[32] = {0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7,
955 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC,
956 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
957 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B};
958 const uint8_t order[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
959 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
960 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
961 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51};
962 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
963 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
964 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
965 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
966 uint8_t bufx[32], bufy[32];
967
968 ec_ws_new_context(&ec_ctx, modulus, b, order, 32, 0x5EED);
969 ec_ws_new_point(&ecp, Gx, Gy, 32, ec_ctx);
970
971 res = ec_ws_scalar(NULL, (uint8_t*)"\xFF\xFF", 2, 0xFFFF);
972 assert(res == ERR_NULL);
973 res = ec_ws_scalar(ecp, NULL, 2, 0xFFFF);
974 assert(res == ERR_NULL);
975
976 res = ec_ws_scalar(ecp, (uint8_t*)"\xFF\xFF", 2, 0xFFFF);
977 assert(res == 0);
978 ec_ws_get_xy(bufx, bufy, 32, ecp);
979 assert(0 == memcmp(bufx, "\xf2\x49\x10\x4d\x0e\x6f\x8f\x29\xe6\x01\x62\x77\x78\x0c\xda\x84\xdc\x84\xb8\x3b\xc3\xd8\x99\xdf\xb7\x36\xca\x08\x31\xfb\xe8\xcf", 32));
980 assert(0 == memcmp(bufy, "\xb5\x7e\x12\xfc\xdb\x03\x1f\x59\xca\xb8\x1b\x1c\x6b\x1e\x1c\x07\xe4\x51\x2e\x52\xce\x83\x2f\x1a\x0c\xed\xef\xff\x8b\x43\x40\xe9", 32));
981
982 ec_ws_free_point(ecp);
983 ec_free_context(ec_ctx);
984 }
985
986 void test_ec_ws_neg(void)
987 {
988 EcContext *ec_ctx;
989 EcPoint *ecp;
990 int res;
991 const uint8_t Gx[32] = {0x6B, 0x17, 0xD1, 0xF2, 0xE1, 0x2C, 0x42, 0x47,
992 0xF8, 0xBC, 0xE6, 0xE5, 0x63, 0xA4, 0x40, 0xF2,
993 0x77, 0x03, 0x7D, 0x81, 0x2D, 0xEB, 0x33, 0xA0,
994 0xF4, 0xA1, 0x39, 0x45, 0xD8, 0x98, 0xC2, 0x96};
995 const uint8_t Gy[32] = {0x4F, 0xE3, 0x42, 0xE2, 0xFE, 0x1A, 0x7F, 0x9B,
996 0x8E, 0xE7, 0xEB, 0x4A, 0x7C, 0x0F, 0x9E, 0x16,
997 0x2B, 0xCE, 0x33, 0x57, 0x6B, 0x31, 0x5E, 0xCE,
998 0xCB, 0xB6, 0x40, 0x68, 0x37, 0xBF, 0x51, 0xF5};
999 const uint8_t b[32] = {0x5A, 0xC6, 0x35, 0xD8, 0xAA, 0x3A, 0x93, 0xE7,
1000 0xB3, 0xEB, 0xBD, 0x55, 0x76, 0x98, 0x86, 0xBC,
1001 0x65, 0x1D, 0x06, 0xB0, 0xCC, 0x53, 0xB0, 0xF6,
1002 0x3B, 0xCE, 0x3C, 0x3E, 0x27, 0xD2, 0x60, 0x4B};
1003 const uint8_t order[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
1004 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
1005 0xBC, 0xE6, 0xFA, 0xAD, 0xA7, 0x17, 0x9E, 0x84,
1006 0xF3, 0xB9, 0xCA, 0xC2, 0xFC, 0x63, 0x25, 0x51};
1007 const uint8_t modulus[32] = {0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x01,
1008 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1009 0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF, 0xFF, 0xFF,
1010 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
1011 uint8_t bufx[32], bufy[32];
1012
1013 ec_ws_new_context(&ec_ctx, modulus, b, order, 32, 0);
1014 ec_ws_new_point(&ecp, Gx, Gy, 32, ec_ctx);
1015
1016 res = ec_ws_neg(NULL);
1017 assert(res == ERR_NULL);
1018
1019 res = ec_ws_neg(ecp);
1020 assert(res == 0);
1021 ec_ws_get_xy(bufx, bufy, 32, ecp);
1022 assert(0 == memcmp(bufx, "\x6b\x17\xd1\xf2\xe1\x2c\x42\x47\xf8\xbc\xe6\xe5\x63\xa4\x40\xf2\x77\x03\x7d\x81\x2d\xeb\x33\xa0\xf4\xa1\x39\x45\xd8\x98\xc2\x96", 32));
1023 assert(0 == memcmp(bufy, "\xb0\x1c\xbd\x1c\x01\xe5\x80\x65\x71\x18\x14\xb5\x83\xf0\x61\xe9\xd4\x31\xcc\xa9\x94\xce\xa1\x31\x34\x49\xbf\x97\xc8\x40\xae\x0a", 32));
1024
1025 ec_ws_free_point(ecp);
1026 ec_free_context(ec_ctx);
1027 }
1028
1029
1030 int main(void) {
1031 test_ec_projective_to_affine();
1032 test_ec_full_double();
1033 test_ec_mix_add_1();
1034 test_ec_mix_add_2();
1035 test_ec_mix_add_3();
1036 test_ec_mix_add_4();
1037 test_ec_full_add();
1038 test_ec_scalar();
1039 test_ec_scalar_g_p256();
1040 test_ec_ws_new_point();
1041 test_ec_ws_get_xy();
1042 test_ec_ws_double_p256();
1043 test_ec_ws_double_p521();
1044 test_ec_ws_add();
1045 test_ec_ws_scalar();
1046 test_ec_ws_neg();
1047 return 0;
1048 }