3 * $Id: ec-test.c,v 1.3 2004/03/27 17:54:11 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.3 2004/03/27 17:54:11 mdw
34 * Standard curves and curve checking.
36 * Revision 1.2 2004/03/27 00:04:46 mdw
37 * Implement efficient reduction for pleasant-looking primes.
39 * Revision 1.1 2004/03/23 15:19:32 mdw
40 * Test elliptic curves more thoroughly.
44 /*----- Header files ------------------------------------------------------*/
52 #include <mLib/alloc.h>
53 #include <mLib/testrig.h>
59 /*----- Cardboard cut-out elliptic curve ----------------------------------*/
61 typedef struct ecctx
{
68 #define MAGIC 0x3a1f0b07
70 static void ecDESTROY(ec_curve
*cc
)
72 ecctx
*c
= (ecctx
*)cc
;
74 ec_destroycurve(c
->real
);
79 static ec *ec##OP(ec_curve *cc, ec *d, const ec *p) { \
80 ecctx *c = (ecctx *)cc; \
81 return (EC_##OP(c->real, d, p)); \
85 static ec *ec##OP(ec_curve *cc, ec *d, const ec *p, const ec *q) { \
86 ecctx *c = (ecctx *)cc; \
87 return (EC_##OP(c->real, d, p, q)); \
101 static ec
*ecFIND(ec_curve
*cc
, ec
*d
, mp
*x
)
103 ecctx
*c
= (ecctx
*)cc
;
104 return (EC_FIND(c
->real
, d
, x
));
107 static int ecCHECK(ec_curve
*cc
, const ec
*p
)
109 ecctx
*c
= (ecctx
*)cc
;
110 return (EC_CHECK(c
->real
, p
));
113 static ec_ops ecops
= {
114 ecDESTROY
, ecIN
, ecOUT
, ecFIX
,
115 ecFIND
, ecNEG
, ecADD
, ecSUB
, ecDBL
, ecCHECK
118 static ec_curve
*ec_cutout(ec_curve
*real
, const char *name
)
120 ecctx
*c
= CREATE(ecctx
);
124 c
->name
= xstrdup(name
);
129 static const char *ec_name(ec_curve
*cc
)
131 ecctx
*c
= (ecctx
*)cc
;
132 assert(c
->magic
== MAGIC
);
136 /*----- Test field types --------------------------------------------------*/
138 static void ecvcvt(const char *buf
, dstr
*d
)
145 if ((v
= ec_curveparse(&qd
)) == 0) {
146 fprintf(stderr
, "bad curve `%.*s|%s': %s\n",
147 qd
.p
- buf
, buf
, qd
.p
, qd
.e
);
150 dstr_ensure(d
, sizeof(v
));
151 *(ec_curve
**)d
->buf
= ec_cutout(v
, buf
);
155 static void ecvdump(dstr
*d
, FILE *fp
)
157 ec_curve
*v
= *(ec_curve
**)d
->buf
;
158 fprintf(fp
, "%s", ec_name(v
));
161 test_type type_ecurve
= { ecvcvt
, ecvdump
};
163 static void eccvt(const char *p
, dstr
*d
)
170 dstr_ensure(d
, sizeof(ec
));
172 d
->len
+= sizeof(ec
);
174 if (!ec_ptparse(&qd
, a
)) {
175 fprintf(stderr
, "bad point `%.*s|%s': %s\n", qd
.p
- p
, p
, qd
.p
, qd
.e
);
180 static void ecdodump(ec
*a
, FILE *fp
)
186 mp_writefile(a
->x
, fp
, 16);
188 mp_writefile(a
->y
, fp
, 16);
192 static void ecdump(dstr
*d
, FILE *fp
)
194 ec
*a
= (ec
*)d
->buf
;
198 test_type type_ec
= { eccvt
, ecdump
};
200 /*----- Testing elliptic curve functionality ------------------------------*/
204 static void ecdestroy(ec_curve
*c
)
212 static int v##op(dstr v[]) \
214 ec_curve *e = *(ec_curve **)v[0].buf; \
215 ec *a = (ec *)v[1].buf; \
216 ec *r = (ec *)v[2].buf; \
220 if (!EC_EQ(r, &c)) { \
221 fprintf(stderr, #op "failed"); \
222 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
223 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
224 fprintf(stderr, "\n r = "); ecdodump(r, stderr); \
225 fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \
226 fprintf(stderr, "\n"); \
229 EC_DESTROY(a); EC_DESTROY(r); EC_DESTROY(&c); \
235 static int v##op(dstr v[]) \
237 ec_curve *e = *(ec_curve **)v[0].buf; \
238 ec *a = (ec *)v[1].buf; \
239 ec *b = (ec *)v[2].buf; \
240 ec *r = (ec *)v[3].buf; \
243 ec_##op(e, &c, a, b); \
244 if (!EC_EQ(r, &c)) { \
245 fprintf(stderr, #op "failed"); \
246 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
247 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
248 fprintf(stderr, "\n b = "); ecdodump(b, stderr); \
249 fprintf(stderr, "\n r = "); ecdodump(r, stderr); \
250 fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \
251 fprintf(stderr, "\n"); \
254 EC_DESTROY(a); EC_DESTROY(b); EC_DESTROY(r); EC_DESTROY(&c); \
264 static int vcheck(dstr v
[])
266 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
267 ec
*a
= (ec
*)v
[1].buf
;
268 int r
= *(int *)v
[2].buf
;
273 fprintf(stderr
, "check failed");
274 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
275 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
276 fprintf(stderr
, "\n r = %d", r
);
277 fprintf(stderr
, "\n c = %d", c
);
278 fprintf(stderr
, "\n");
286 static int vmul(dstr v
[])
288 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
289 ec
*a
= (ec
*)v
[1].buf
;
290 mp
*n
= *(mp
**)v
[2].buf
;
291 ec
*r
= (ec
*)v
[3].buf
;
296 fprintf(stderr
, "mul failed");
297 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
298 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
299 fprintf(stderr
, "\n n = "); mp_writefile(n
, stderr
, 10);
300 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
301 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
302 fprintf(stderr
, "\n");
305 EC_DESTROY(a
); EC_DESTROY(r
); EC_DESTROY(&c
); MP_DROP(n
);
310 static int vfind(dstr v
[])
312 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
313 mp
*x
= *(mp
**)v
[1].buf
;
314 ec
*r
= (ec
*)v
[2].buf
;
317 if (!ec_find(e
, &c
, x
)) EC_SETINF(&c
);
319 fprintf(stderr
, "find failed");
320 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
321 fprintf(stderr
, "\n x = "); mp_writefile(x
, stderr
, 16);
322 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
323 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
324 fprintf(stderr
, "\n");
327 MP_DROP(x
); EC_DESTROY(r
); EC_DESTROY(&c
);
332 static test_chunk tests
[] = {
333 { "neg", vneg
, { &type_ecurve
, &type_ec
, &type_ec
} },
334 { "dbl", vdbl
, { &type_ecurve
, &type_ec
, &type_ec
} },
335 { "add", vadd
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
336 { "sub", vsub
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
337 { "mul", vmul
, { &type_ecurve
, &type_ec
, &type_mp
, &type_ec
} },
338 { "check", vcheck
, { &type_ecurve
, &type_ec
, &type_int
} },
339 { "find", vfind
, { &type_ecurve
, &type_mp
, &type_ec
} },
343 int main(int argc
, char *argv
[])
346 test_run(argc
, argv
, tests
, SRCDIR
"/tests/ec");
352 /*----- That's all, folks -------------------------------------------------*/