bc985cef |
1 | /* -*-c-*- |
2 | * |
b817bfc6 |
3 | * $Id: ec-test.c,v 1.6 2004/04/08 01:36:15 mdw Exp $ |
bc985cef |
4 | * |
5 | * Code for testing elliptic-curve stuff |
6 | * |
7 | * (c) 2004 Straylight/Edgeware |
8 | */ |
9 | |
10 | /*----- Licensing notice --------------------------------------------------* |
11 | * |
12 | * This file is part of Catacomb. |
13 | * |
14 | * Catacomb is free software; you can redistribute it and/or modify |
15 | * it under the terms of the GNU Library General Public License as |
16 | * published by the Free Software Foundation; either version 2 of the |
17 | * License, or (at your option) any later version. |
18 | * |
19 | * Catacomb is distributed in the hope that it will be useful, |
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22 | * GNU Library General Public License for more details. |
23 | * |
24 | * You should have received a copy of the GNU Library General Public |
25 | * License along with Catacomb; if not, write to the Free |
26 | * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
27 | * MA 02111-1307, USA. |
28 | */ |
29 | |
bc985cef |
30 | /*----- Header files ------------------------------------------------------*/ |
31 | |
32 | #include <assert.h> |
33 | #include <ctype.h> |
34 | #include <stdio.h> |
35 | #include <stdlib.h> |
36 | #include <string.h> |
37 | |
38 | #include <mLib/alloc.h> |
39 | #include <mLib/testrig.h> |
40 | #include <mLib/sub.h> |
41 | |
42 | #include "ec.h" |
43 | #include "ec-test.h" |
44 | |
45 | /*----- Cardboard cut-out elliptic curve ----------------------------------*/ |
46 | |
47 | typedef struct ecctx { |
48 | ec_curve c; |
49 | unsigned long magic; |
50 | char *name; |
51 | ec_curve *real; |
52 | } ecctx; |
53 | |
54 | #define MAGIC 0x3a1f0b07 |
55 | |
56 | static void ecDESTROY(ec_curve *cc) |
57 | { |
58 | ecctx *c = (ecctx *)cc; |
59 | xfree(c->name); |
60 | ec_destroycurve(c->real); |
61 | DESTROY(c); |
62 | } |
63 | |
64 | #define UNOP(OP) \ |
65 | static ec *ec##OP(ec_curve *cc, ec *d, const ec *p) { \ |
66 | ecctx *c = (ecctx *)cc; \ |
67 | return (EC_##OP(c->real, d, p)); \ |
68 | } |
69 | |
70 | #define BINOP(OP) \ |
71 | static ec *ec##OP(ec_curve *cc, ec *d, const ec *p, const ec *q) { \ |
72 | ecctx *c = (ecctx *)cc; \ |
73 | return (EC_##OP(c->real, d, p, q)); \ |
74 | } |
75 | |
76 | UNOP(IN) |
77 | UNOP(OUT) |
78 | UNOP(FIX) |
79 | UNOP(NEG) |
80 | UNOP(DBL) |
81 | BINOP(ADD) |
82 | BINOP(SUB) |
83 | |
84 | #undef UNOP |
85 | #undef BINOP |
86 | |
87 | static ec *ecFIND(ec_curve *cc, ec *d, mp *x) |
88 | { |
89 | ecctx *c = (ecctx *)cc; |
90 | return (EC_FIND(c->real, d, x)); |
91 | } |
92 | |
93 | static int ecCHECK(ec_curve *cc, const ec *p) |
94 | { |
95 | ecctx *c = (ecctx *)cc; |
96 | return (EC_CHECK(c->real, p)); |
97 | } |
98 | |
34e4f738 |
99 | static int ecSAMEP(ec_curve *cc, ec_curve *dd) |
100 | { |
101 | ecctx *c = (ecctx *)cc, *d = (ecctx *)dd; |
102 | return (ec_samep(c->real, d->real)); |
103 | } |
104 | |
4e66da02 |
105 | static const ec_ops ecops = { |
34e4f738 |
106 | ecDESTROY, ecSAMEP, ecIN, ecOUT, ecFIX, |
bc985cef |
107 | ecFIND, ecNEG, ecADD, ecSUB, ecDBL, ecCHECK |
108 | }; |
109 | |
110 | static ec_curve *ec_cutout(ec_curve *real, const char *name) |
111 | { |
112 | ecctx *c = CREATE(ecctx); |
113 | c->c.f = real->f; |
114 | c->c.ops = &ecops; |
34e4f738 |
115 | c->c.a = real->a; |
116 | c->c.b = real->b; |
bc985cef |
117 | c->magic = MAGIC; |
118 | c->name = xstrdup(name); |
119 | c->real = real; |
120 | return (&c->c); |
121 | } |
122 | |
123 | static const char *ec_name(ec_curve *cc) |
124 | { |
125 | ecctx *c = (ecctx *)cc; |
126 | assert(c->magic == MAGIC); |
127 | return (c->name); |
128 | } |
129 | |
432c4e18 |
130 | /*----- Test field types --------------------------------------------------*/ |
bc985cef |
131 | |
132 | static void ecvcvt(const char *buf, dstr *d) |
133 | { |
bc985cef |
134 | ec_curve *v; |
432c4e18 |
135 | qd_parse qd; |
136 | |
137 | qd.p = buf; |
138 | qd.e = 0; |
139 | if ((v = ec_curveparse(&qd)) == 0) { |
140 | fprintf(stderr, "bad curve `%.*s|%s': %s\n", |
141 | qd.p - buf, buf, qd.p, qd.e); |
142 | exit(1); |
bc985cef |
143 | } |
bc985cef |
144 | dstr_ensure(d, sizeof(v)); |
145 | *(ec_curve **)d->buf = ec_cutout(v, buf); |
146 | d->len += sizeof(v); |
147 | } |
148 | |
149 | static void ecvdump(dstr *d, FILE *fp) |
150 | { |
151 | ec_curve *v = *(ec_curve **)d->buf; |
152 | fprintf(fp, "%s", ec_name(v)); |
153 | } |
154 | |
4e66da02 |
155 | const test_type type_ecurve = { ecvcvt, ecvdump }; |
bc985cef |
156 | |
157 | static void eccvt(const char *p, dstr *d) |
158 | { |
159 | ec *a; |
432c4e18 |
160 | qd_parse qd; |
bc985cef |
161 | |
432c4e18 |
162 | qd.p = p; |
163 | qd.e = 0; |
bc985cef |
164 | dstr_ensure(d, sizeof(ec)); |
165 | a = (ec *)d->buf; |
166 | d->len += sizeof(ec); |
167 | ec_create(a); |
432c4e18 |
168 | if (!ec_ptparse(&qd, a)) { |
169 | fprintf(stderr, "bad point `%.*s|%s': %s\n", qd.p - p, p, qd.p, qd.e); |
170 | exit(1); |
171 | } |
bc985cef |
172 | } |
173 | |
174 | static void ecdodump(ec *a, FILE *fp) |
175 | { |
176 | if (EC_ATINF(a)) |
177 | fputs("inf", fp); |
178 | else { |
179 | fputs("0x", fp); |
180 | mp_writefile(a->x, fp, 16); |
181 | fputs(", 0x", fp); |
182 | mp_writefile(a->y, fp, 16); |
183 | } |
184 | } |
185 | |
186 | static void ecdump(dstr *d, FILE *fp) |
187 | { |
188 | ec *a = (ec *)d->buf; |
189 | ecdodump(a, fp); |
190 | } |
191 | |
192 | test_type type_ec = { eccvt, ecdump }; |
193 | |
194 | /*----- Testing elliptic curve functionality ------------------------------*/ |
195 | |
196 | #ifdef TEST_RIG |
197 | |
198 | static void ecdestroy(ec_curve *c) |
199 | { |
200 | field *f = c->f; |
201 | ec_destroycurve(c); |
202 | F_DESTROY(f); |
203 | } |
204 | |
205 | #define UNOP(op) \ |
206 | static int v##op(dstr v[]) \ |
207 | { \ |
208 | ec_curve *e = *(ec_curve **)v[0].buf; \ |
209 | ec *a = (ec *)v[1].buf; \ |
210 | ec *r = (ec *)v[2].buf; \ |
211 | ec c = EC_INIT; \ |
212 | int ok = 1; \ |
213 | ec_##op(e, &c, a); \ |
214 | if (!EC_EQ(r, &c)) { \ |
215 | fprintf(stderr, #op "failed"); \ |
216 | fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \ |
217 | fprintf(stderr, "\n a = "); ecdodump(a, stderr); \ |
218 | fprintf(stderr, "\n r = "); ecdodump(r, stderr); \ |
219 | fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \ |
220 | fprintf(stderr, "\n"); \ |
221 | ok = 0; \ |
222 | } \ |
223 | EC_DESTROY(a); EC_DESTROY(r); EC_DESTROY(&c); \ |
224 | ecdestroy(e); \ |
225 | return (ok); \ |
226 | } |
227 | |
228 | #define BINOP(op) \ |
229 | static int v##op(dstr v[]) \ |
230 | { \ |
231 | ec_curve *e = *(ec_curve **)v[0].buf; \ |
232 | ec *a = (ec *)v[1].buf; \ |
233 | ec *b = (ec *)v[2].buf; \ |
234 | ec *r = (ec *)v[3].buf; \ |
235 | ec c = EC_INIT; \ |
236 | int ok = 1; \ |
237 | ec_##op(e, &c, a, b); \ |
238 | if (!EC_EQ(r, &c)) { \ |
239 | fprintf(stderr, #op "failed"); \ |
240 | fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \ |
241 | fprintf(stderr, "\n a = "); ecdodump(a, stderr); \ |
242 | fprintf(stderr, "\n b = "); ecdodump(b, stderr); \ |
243 | fprintf(stderr, "\n r = "); ecdodump(r, stderr); \ |
244 | fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \ |
245 | fprintf(stderr, "\n"); \ |
246 | ok = 0; \ |
247 | } \ |
248 | EC_DESTROY(a); EC_DESTROY(b); EC_DESTROY(r); EC_DESTROY(&c); \ |
249 | ecdestroy(e); \ |
250 | return (ok); \ |
251 | } |
252 | |
253 | UNOP(neg) |
254 | UNOP(dbl) |
255 | BINOP(add) |
256 | BINOP(sub) |
257 | |
258 | static int vcheck(dstr v[]) |
259 | { |
260 | ec_curve *e = *(ec_curve **)v[0].buf; |
261 | ec *a = (ec *)v[1].buf; |
262 | int r = *(int *)v[2].buf; |
263 | int c; |
264 | int ok = 1; |
265 | c = ec_check(e, a); |
266 | if (r != c) { |
267 | fprintf(stderr, "check failed"); |
268 | fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); |
269 | fprintf(stderr, "\n a = "); ecdodump(a, stderr); |
270 | fprintf(stderr, "\n r = %d", r); |
271 | fprintf(stderr, "\n c = %d", c); |
272 | fprintf(stderr, "\n"); |
273 | ok = 0; |
274 | } |
275 | EC_DESTROY(a); |
276 | ecdestroy(e); |
277 | return (ok); |
278 | } |
279 | |
280 | static int vmul(dstr v[]) |
281 | { |
282 | ec_curve *e = *(ec_curve **)v[0].buf; |
283 | ec *a = (ec *)v[1].buf; |
284 | mp *n = *(mp **)v[2].buf; |
285 | ec *r = (ec *)v[3].buf; |
286 | ec c = EC_INIT; |
287 | int ok = 1; |
288 | ec_mul(e, &c, a, n); |
289 | if (!EC_EQ(r, &c)) { |
290 | fprintf(stderr, "mul failed"); |
291 | fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); |
292 | fprintf(stderr, "\n a = "); ecdodump(a, stderr); |
293 | fprintf(stderr, "\n n = "); mp_writefile(n, stderr, 10); |
294 | fprintf(stderr, "\n r = "); ecdodump(r, stderr); |
295 | fprintf(stderr, "\n c = "); ecdodump(&c, stderr); |
296 | fprintf(stderr, "\n"); |
297 | ok = 0; |
298 | } |
299 | EC_DESTROY(a); EC_DESTROY(r); EC_DESTROY(&c); MP_DROP(n); |
300 | ecdestroy(e); |
301 | return (ok); |
302 | } |
303 | |
304 | static int vfind(dstr v[]) |
305 | { |
306 | ec_curve *e = *(ec_curve **)v[0].buf; |
307 | mp *x = *(mp **)v[1].buf; |
308 | ec *r = (ec *)v[2].buf; |
309 | ec c = EC_INIT; |
310 | int ok = 1; |
311 | if (!ec_find(e, &c, x)) EC_SETINF(&c); |
312 | if (!EC_EQ(r, &c)) { |
313 | fprintf(stderr, "find failed"); |
314 | fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); |
315 | fprintf(stderr, "\n x = "); mp_writefile(x, stderr, 16); |
316 | fprintf(stderr, "\n r = "); ecdodump(r, stderr); |
317 | fprintf(stderr, "\n c = "); ecdodump(&c, stderr); |
318 | fprintf(stderr, "\n"); |
319 | ok = 0; |
320 | } |
321 | MP_DROP(x); EC_DESTROY(r); EC_DESTROY(&c); |
322 | ecdestroy(e); |
323 | return (ok); |
324 | } |
325 | |
326 | static test_chunk tests[] = { |
327 | { "neg", vneg, { &type_ecurve, &type_ec, &type_ec } }, |
328 | { "dbl", vdbl, { &type_ecurve, &type_ec, &type_ec } }, |
329 | { "add", vadd, { &type_ecurve, &type_ec, &type_ec, &type_ec } }, |
330 | { "sub", vsub, { &type_ecurve, &type_ec, &type_ec, &type_ec } }, |
331 | { "mul", vmul, { &type_ecurve, &type_ec, &type_mp, &type_ec } }, |
332 | { "check", vcheck, { &type_ecurve, &type_ec, &type_int } }, |
333 | { "find", vfind, { &type_ecurve, &type_mp, &type_ec } }, |
334 | { 0, 0, { 0 } } |
335 | }; |
336 | |
337 | int main(int argc, char *argv[]) |
338 | { |
339 | sub_init(); |
340 | test_run(argc, argv, tests, SRCDIR "/tests/ec"); |
341 | return (0); |
342 | } |
343 | |
344 | #endif |
345 | |
346 | /*----- That's all, folks -------------------------------------------------*/ |