3 * $Id: ec-test.c,v 1.4 2004/04/01 12:50:09 mdw Exp $
5 * Code for testing elliptic-curve stuff
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
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.
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.
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,
30 /*----- Revision history --------------------------------------------------*
33 * Revision 1.4 2004/04/01 12:50:09 mdw
34 * Add cyclic group abstraction, with test code. Separate off exponentation
35 * functions for better static linking. Fix a buttload of bugs on the way.
36 * Generally ensure that negative exponents do inversion correctly. Add
37 * table of standard prime-field subgroups. (Binary field subgroups are
38 * currently unimplemented but easy to add if anyone ever finds a good one.)
40 * Revision 1.3 2004/03/27 17:54:11 mdw
41 * Standard curves and curve checking.
43 * Revision 1.2 2004/03/27 00:04:46 mdw
44 * Implement efficient reduction for pleasant-looking primes.
46 * Revision 1.1 2004/03/23 15:19:32 mdw
47 * Test elliptic curves more thoroughly.
51 /*----- Header files ------------------------------------------------------*/
59 #include <mLib/alloc.h>
60 #include <mLib/testrig.h>
66 /*----- Cardboard cut-out elliptic curve ----------------------------------*/
68 typedef struct ecctx
{
75 #define MAGIC 0x3a1f0b07
77 static void ecDESTROY(ec_curve
*cc
)
79 ecctx
*c
= (ecctx
*)cc
;
81 ec_destroycurve(c
->real
);
86 static ec *ec##OP(ec_curve *cc, ec *d, const ec *p) { \
87 ecctx *c = (ecctx *)cc; \
88 return (EC_##OP(c->real, d, p)); \
92 static ec *ec##OP(ec_curve *cc, ec *d, const ec *p, const ec *q) { \
93 ecctx *c = (ecctx *)cc; \
94 return (EC_##OP(c->real, d, p, q)); \
108 static ec
*ecFIND(ec_curve
*cc
, ec
*d
, mp
*x
)
110 ecctx
*c
= (ecctx
*)cc
;
111 return (EC_FIND(c
->real
, d
, x
));
114 static int ecCHECK(ec_curve
*cc
, const ec
*p
)
116 ecctx
*c
= (ecctx
*)cc
;
117 return (EC_CHECK(c
->real
, p
));
120 static int ecSAMEP(ec_curve
*cc
, ec_curve
*dd
)
122 ecctx
*c
= (ecctx
*)cc
, *d
= (ecctx
*)dd
;
123 return (ec_samep(c
->real
, d
->real
));
126 static ec_ops ecops
= {
127 ecDESTROY
, ecSAMEP
, ecIN
, ecOUT
, ecFIX
,
128 ecFIND
, ecNEG
, ecADD
, ecSUB
, ecDBL
, ecCHECK
131 static ec_curve
*ec_cutout(ec_curve
*real
, const char *name
)
133 ecctx
*c
= CREATE(ecctx
);
139 c
->name
= xstrdup(name
);
144 static const char *ec_name(ec_curve
*cc
)
146 ecctx
*c
= (ecctx
*)cc
;
147 assert(c
->magic
== MAGIC
);
151 /*----- Test field types --------------------------------------------------*/
153 static void ecvcvt(const char *buf
, dstr
*d
)
160 if ((v
= ec_curveparse(&qd
)) == 0) {
161 fprintf(stderr
, "bad curve `%.*s|%s': %s\n",
162 qd
.p
- buf
, buf
, qd
.p
, qd
.e
);
165 dstr_ensure(d
, sizeof(v
));
166 *(ec_curve
**)d
->buf
= ec_cutout(v
, buf
);
170 static void ecvdump(dstr
*d
, FILE *fp
)
172 ec_curve
*v
= *(ec_curve
**)d
->buf
;
173 fprintf(fp
, "%s", ec_name(v
));
176 test_type type_ecurve
= { ecvcvt
, ecvdump
};
178 static void eccvt(const char *p
, dstr
*d
)
185 dstr_ensure(d
, sizeof(ec
));
187 d
->len
+= sizeof(ec
);
189 if (!ec_ptparse(&qd
, a
)) {
190 fprintf(stderr
, "bad point `%.*s|%s': %s\n", qd
.p
- p
, p
, qd
.p
, qd
.e
);
195 static void ecdodump(ec
*a
, FILE *fp
)
201 mp_writefile(a
->x
, fp
, 16);
203 mp_writefile(a
->y
, fp
, 16);
207 static void ecdump(dstr
*d
, FILE *fp
)
209 ec
*a
= (ec
*)d
->buf
;
213 test_type type_ec
= { eccvt
, ecdump
};
215 /*----- Testing elliptic curve functionality ------------------------------*/
219 static void ecdestroy(ec_curve
*c
)
227 static int v##op(dstr v[]) \
229 ec_curve *e = *(ec_curve **)v[0].buf; \
230 ec *a = (ec *)v[1].buf; \
231 ec *r = (ec *)v[2].buf; \
235 if (!EC_EQ(r, &c)) { \
236 fprintf(stderr, #op "failed"); \
237 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
238 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
239 fprintf(stderr, "\n r = "); ecdodump(r, stderr); \
240 fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \
241 fprintf(stderr, "\n"); \
244 EC_DESTROY(a); EC_DESTROY(r); EC_DESTROY(&c); \
250 static int v##op(dstr v[]) \
252 ec_curve *e = *(ec_curve **)v[0].buf; \
253 ec *a = (ec *)v[1].buf; \
254 ec *b = (ec *)v[2].buf; \
255 ec *r = (ec *)v[3].buf; \
258 ec_##op(e, &c, a, b); \
259 if (!EC_EQ(r, &c)) { \
260 fprintf(stderr, #op "failed"); \
261 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
262 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
263 fprintf(stderr, "\n b = "); ecdodump(b, stderr); \
264 fprintf(stderr, "\n r = "); ecdodump(r, stderr); \
265 fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \
266 fprintf(stderr, "\n"); \
269 EC_DESTROY(a); EC_DESTROY(b); EC_DESTROY(r); EC_DESTROY(&c); \
279 static int vcheck(dstr v
[])
281 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
282 ec
*a
= (ec
*)v
[1].buf
;
283 int r
= *(int *)v
[2].buf
;
288 fprintf(stderr
, "check failed");
289 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
290 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
291 fprintf(stderr
, "\n r = %d", r
);
292 fprintf(stderr
, "\n c = %d", c
);
293 fprintf(stderr
, "\n");
301 static int vmul(dstr v
[])
303 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
304 ec
*a
= (ec
*)v
[1].buf
;
305 mp
*n
= *(mp
**)v
[2].buf
;
306 ec
*r
= (ec
*)v
[3].buf
;
311 fprintf(stderr
, "mul failed");
312 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
313 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
314 fprintf(stderr
, "\n n = "); mp_writefile(n
, stderr
, 10);
315 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
316 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
317 fprintf(stderr
, "\n");
320 EC_DESTROY(a
); EC_DESTROY(r
); EC_DESTROY(&c
); MP_DROP(n
);
325 static int vfind(dstr v
[])
327 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
328 mp
*x
= *(mp
**)v
[1].buf
;
329 ec
*r
= (ec
*)v
[2].buf
;
332 if (!ec_find(e
, &c
, x
)) EC_SETINF(&c
);
334 fprintf(stderr
, "find failed");
335 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
336 fprintf(stderr
, "\n x = "); mp_writefile(x
, stderr
, 16);
337 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
338 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
339 fprintf(stderr
, "\n");
342 MP_DROP(x
); EC_DESTROY(r
); EC_DESTROY(&c
);
347 static test_chunk tests
[] = {
348 { "neg", vneg
, { &type_ecurve
, &type_ec
, &type_ec
} },
349 { "dbl", vdbl
, { &type_ecurve
, &type_ec
, &type_ec
} },
350 { "add", vadd
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
351 { "sub", vsub
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
352 { "mul", vmul
, { &type_ecurve
, &type_ec
, &type_mp
, &type_ec
} },
353 { "check", vcheck
, { &type_ecurve
, &type_ec
, &type_int
} },
354 { "find", vfind
, { &type_ecurve
, &type_mp
, &type_ec
} },
358 int main(int argc
, char *argv
[])
361 test_run(argc
, argv
, tests
, SRCDIR
"/tests/ec");
367 /*----- That's all, folks -------------------------------------------------*/