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 /*----- Header files ------------------------------------------------------*/
38 #include <mLib/alloc.h>
39 #include <mLib/testrig.h>
45 /*----- Cardboard cut-out elliptic curve ----------------------------------*/
47 typedef struct ecctx
{
54 #define MAGIC 0x3a1f0b07
56 static void ecDESTROY(ec_curve
*cc
)
58 ecctx
*c
= (ecctx
*)cc
;
60 ec_destroycurve(c
->real
);
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)); \
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)); \
87 static ec
*ecFIND(ec_curve
*cc
, ec
*d
, mp
*x
)
89 ecctx
*c
= (ecctx
*)cc
;
90 return (EC_FIND(c
->real
, d
, x
));
93 static int ecCHECK(ec_curve
*cc
, const ec
*p
)
95 ecctx
*c
= (ecctx
*)cc
;
96 return (EC_CHECK(c
->real
, p
));
99 static int ecSAMEP(ec_curve
*cc
, ec_curve
*dd
)
101 ecctx
*c
= (ecctx
*)cc
, *d
= (ecctx
*)dd
;
102 return (ec_samep(c
->real
, d
->real
));
105 static const ec_ops ecops
= {
107 ecDESTROY
, ecSAMEP
, ecIN
, ecOUT
, ecFIX
,
108 ecFIND
, ecNEG
, ecADD
, ecSUB
, ecDBL
, ecCHECK
111 static ec_curve
*ec_cutout(ec_curve
*real
, const char *name
)
113 ecctx
*c
= CREATE(ecctx
);
119 c
->name
= xstrdup(name
);
124 static const char *ec_name(ec_curve
*cc
)
126 ecctx
*c
= (ecctx
*)cc
;
127 assert(c
->magic
== MAGIC
);
131 /*----- Test field types --------------------------------------------------*/
133 static void ecvcvt(const char *buf
, dstr
*d
)
140 if ((v
= ec_curveparse(&qd
)) == 0) {
141 fprintf(stderr
, "bad curve `%.*s|%s': %s\n",
142 qd
.p
- buf
, buf
, qd
.p
, qd
.e
);
145 dstr_ensure(d
, sizeof(v
));
146 *(ec_curve
**)d
->buf
= ec_cutout(v
, buf
);
150 static void ecvdump(dstr
*d
, FILE *fp
)
152 ec_curve
*v
= *(ec_curve
**)d
->buf
;
153 fprintf(fp
, "%s", ec_name(v
));
156 const test_type type_ecurve
= { ecvcvt
, ecvdump
};
158 static void eccvt(const char *p
, dstr
*d
)
165 dstr_ensure(d
, sizeof(ec
));
167 d
->len
+= sizeof(ec
);
169 if (!ec_ptparse(&qd
, a
)) {
170 fprintf(stderr
, "bad point `%.*s|%s': %s\n", qd
.p
- p
, p
, qd
.p
, qd
.e
);
175 static void ecdodump(ec
*a
, FILE *fp
)
181 mp_writefile(a
->x
, fp
, 16);
183 mp_writefile(a
->y
, fp
, 16);
187 static void ecdump(dstr
*d
, FILE *fp
)
189 ec
*a
= (ec
*)d
->buf
;
193 const test_type type_ec
= { eccvt
, ecdump
};
195 /*----- Testing elliptic curve functionality ------------------------------*/
199 static void ecdestroy(ec_curve
*c
)
207 static int v##op(dstr v[]) \
209 ec_curve *e = *(ec_curve **)v[0].buf; \
210 ec *a = (ec *)v[1].buf; \
211 ec *r = (ec *)v[2].buf; \
215 if (!EC_EQ(r, &c)) { \
216 fprintf(stderr, #op "failed"); \
217 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
218 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
219 fprintf(stderr, "\n r = "); ecdodump(r, stderr); \
220 fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \
221 fprintf(stderr, "\n"); \
224 EC_DESTROY(a); EC_DESTROY(r); EC_DESTROY(&c); \
230 static int v##op(dstr v[]) \
232 ec_curve *e = *(ec_curve **)v[0].buf; \
233 ec *a = (ec *)v[1].buf; \
234 ec *b = (ec *)v[2].buf; \
235 ec *r = (ec *)v[3].buf; \
238 ec_##op(e, &c, a, b); \
239 if (!EC_EQ(r, &c)) { \
240 fprintf(stderr, #op "failed"); \
241 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
242 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
243 fprintf(stderr, "\n b = "); ecdodump(b, stderr); \
244 fprintf(stderr, "\n r = "); ecdodump(r, stderr); \
245 fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \
246 fprintf(stderr, "\n"); \
249 EC_DESTROY(a); EC_DESTROY(b); EC_DESTROY(r); EC_DESTROY(&c); \
259 static int vcheck(dstr v
[])
261 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
262 ec
*a
= (ec
*)v
[1].buf
;
263 int r
= *(int *)v
[2].buf
;
268 fprintf(stderr
, "check failed");
269 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
270 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
271 fprintf(stderr
, "\n r = %d", r
);
272 fprintf(stderr
, "\n c = %d", c
);
273 fprintf(stderr
, "\n");
281 static int vmul(dstr v
[])
283 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
284 ec
*a
= (ec
*)v
[1].buf
;
285 mp
*n
= *(mp
**)v
[2].buf
;
286 ec
*r
= (ec
*)v
[3].buf
;
291 fprintf(stderr
, "mul failed");
292 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
293 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
294 fprintf(stderr
, "\n n = "); mp_writefile(n
, stderr
, 10);
295 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
296 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
297 fprintf(stderr
, "\n");
300 EC_DESTROY(a
); EC_DESTROY(r
); EC_DESTROY(&c
); MP_DROP(n
);
305 static int vfind(dstr v
[])
307 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
308 mp
*x
= *(mp
**)v
[1].buf
;
309 ec
*r
= (ec
*)v
[2].buf
;
312 if (!ec_find(e
, &c
, x
)) EC_SETINF(&c
);
314 fprintf(stderr
, "find failed");
315 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
316 fprintf(stderr
, "\n x = "); mp_writefile(x
, stderr
, 16);
317 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
318 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
319 fprintf(stderr
, "\n");
322 MP_DROP(x
); EC_DESTROY(r
); EC_DESTROY(&c
);
327 static test_chunk tests
[] = {
328 { "neg", vneg
, { &type_ecurve
, &type_ec
, &type_ec
} },
329 { "dbl", vdbl
, { &type_ecurve
, &type_ec
, &type_ec
} },
330 { "add", vadd
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
331 { "sub", vsub
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
332 { "mul", vmul
, { &type_ecurve
, &type_ec
, &type_mp
, &type_ec
} },
333 { "check", vcheck
, { &type_ecurve
, &type_ec
, &type_int
} },
334 { "find", vfind
, { &type_ecurve
, &type_mp
, &type_ec
} },
338 int main(int argc
, char *argv
[])
341 test_run(argc
, argv
, tests
, SRCDIR
"/tests/ec");
347 /*----- That's all, folks -------------------------------------------------*/