6d42a704aaf03d5bbf5c7a95177b5fc6b9761887
3 * $Id: ec-test.c,v 1.1 2004/03/23 15:19:32 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.1 2004/03/23 15:19:32 mdw
34 * Test elliptic curves more thoroughly.
38 /*----- Header files ------------------------------------------------------*/
46 #include <mLib/alloc.h>
47 #include <mLib/testrig.h>
53 /*----- Cardboard cut-out elliptic curve ----------------------------------*/
55 typedef struct ecctx
{
62 #define MAGIC 0x3a1f0b07
64 static void ecDESTROY(ec_curve
*cc
)
66 ecctx
*c
= (ecctx
*)cc
;
68 ec_destroycurve(c
->real
);
73 static ec *ec##OP(ec_curve *cc, ec *d, const ec *p) { \
74 ecctx *c = (ecctx *)cc; \
75 return (EC_##OP(c->real, d, p)); \
79 static ec *ec##OP(ec_curve *cc, ec *d, const ec *p, const ec *q) { \
80 ecctx *c = (ecctx *)cc; \
81 return (EC_##OP(c->real, d, p, q)); \
95 static ec
*ecFIND(ec_curve
*cc
, ec
*d
, mp
*x
)
97 ecctx
*c
= (ecctx
*)cc
;
98 return (EC_FIND(c
->real
, d
, x
));
101 static int ecCHECK(ec_curve
*cc
, const ec
*p
)
103 ecctx
*c
= (ecctx
*)cc
;
104 return (EC_CHECK(c
->real
, p
));
107 static ec_ops ecops
= {
108 ecDESTROY
, ecIN
, ecOUT
, ecFIX
,
109 ecFIND
, ecNEG
, ecADD
, ecSUB
, ecDBL
, ecCHECK
112 static ec_curve
*ec_cutout(ec_curve
*real
, const char *name
)
114 ecctx
*c
= CREATE(ecctx
);
118 c
->name
= xstrdup(name
);
123 static const char *ec_name(ec_curve
*cc
)
125 ecctx
*c
= (ecctx
*)cc
;
126 assert(c
->magic
== MAGIC
);
130 /*----- Test field types --------------------------------------------------*
132 * Really lazy parser. Sorry.
135 static void skipws(const char **p
)
137 while (isspace((unsigned char)**p
)) (*p
)++;
140 static void ckchar(const char **p
, int ch
)
141 { skipws(p
); if (**p
== ch
) (*p
)++; }
143 static void ckend(const char **p
)
147 fprintf(stderr
, "syntax error: junk at end of line\n");
152 static int ckstring(const char **p
, const char **s
)
158 for (i
= 0; s
[i
]; i
++) {
160 if (strncmp(*p
, s
[i
], n
) == 0 && !isalnum((unsigned char)(*p
)[n
])) {
165 fprintf(stderr
, "syntax error: couldn't recognize keyword\n");
169 static mp
*getmp(const char **p
)
174 m
= mp_readstring(MP_NEW
, *p
, &q
, 0);
175 if (!m
|| isalnum((unsigned char)*q
)) {
176 fprintf(stderr
, "syntax error: bad number\n");
183 static void ecvcvt(const char *buf
, dstr
*d
)
191 static const char *fnames
[] = {
192 "prime", "binpoly", 0
194 static const char *ecnames
[] = {
195 "prime", "primeproj", "bin", "binproj", 0
198 switch (i
= ckstring(&p
, fnames
), ckchar(&p
, ':'), i
) {
199 case 0: m
= getmp(&p
); f
= field_prime(m
); mp_drop(m
); break;
200 case 1: m
= getmp(&p
); f
= field_binpoly(m
); mp_drop(m
); break;
205 switch (i
= ckstring(&p
, ecnames
), ckchar(&p
, ':'), i
) {
206 case 0: m
= getmp(&p
); ckchar(&p
, ','); n
= getmp(&p
);
207 v
= ec_prime(f
, m
, n
); mp_drop(m
); mp_drop(n
); break;
208 case 1: m
= getmp(&p
); ckchar(&p
, ','); n
= getmp(&p
);
209 v
= ec_primeproj(f
, m
, n
); mp_drop(m
); mp_drop(n
); break;
210 case 2: m
= getmp(&p
); ckchar(&p
, ','); n
= getmp(&p
);
211 v
= ec_bin(f
, m
, n
); mp_drop(m
); mp_drop(n
); break;
212 case 3: m
= getmp(&p
); ckchar(&p
, ','); n
= getmp(&p
);
213 v
= ec_binproj(f
, m
, n
); mp_drop(m
); mp_drop(n
); break;
218 dstr_ensure(d
, sizeof(v
));
219 *(ec_curve
**)d
->buf
= ec_cutout(v
, buf
);
223 static void ecvdump(dstr
*d
, FILE *fp
)
225 ec_curve
*v
= *(ec_curve
**)d
->buf
;
226 fprintf(fp
, "%s", ec_name(v
));
229 test_type type_ecurve
= { ecvcvt
, ecvdump
};
231 static void eccvt(const char *p
, dstr
*d
)
235 dstr_ensure(d
, sizeof(ec
));
237 d
->len
+= sizeof(ec
);
240 if (strcmp(p
, "inf") == 0)
243 { a
->x
= getmp(&p
); ckchar(&p
, ','); a
->y
= getmp(&p
); ckend(&p
); }
246 static void ecdodump(ec
*a
, FILE *fp
)
252 mp_writefile(a
->x
, fp
, 16);
254 mp_writefile(a
->y
, fp
, 16);
258 static void ecdump(dstr
*d
, FILE *fp
)
260 ec
*a
= (ec
*)d
->buf
;
264 test_type type_ec
= { eccvt
, ecdump
};
266 /*----- Testing elliptic curve functionality ------------------------------*/
270 static void ecdestroy(ec_curve
*c
)
278 static int v##op(dstr v[]) \
280 ec_curve *e = *(ec_curve **)v[0].buf; \
281 ec *a = (ec *)v[1].buf; \
282 ec *r = (ec *)v[2].buf; \
286 if (!EC_EQ(r, &c)) { \
287 fprintf(stderr, #op "failed"); \
288 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
289 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
290 fprintf(stderr, "\n r = "); ecdodump(r, stderr); \
291 fprintf(stderr, "\n c = "); ecdodump(&c, stderr); \
292 fprintf(stderr, "\n"); \
295 EC_DESTROY(a); EC_DESTROY(r); EC_DESTROY(&c); \
301 static int v##op(dstr v[]) \
303 ec_curve *e = *(ec_curve **)v[0].buf; \
304 ec *a = (ec *)v[1].buf; \
305 ec *b = (ec *)v[2].buf; \
306 ec *r = (ec *)v[3].buf; \
309 ec_##op(e, &c, a, b); \
310 if (!EC_EQ(r, &c)) { \
311 fprintf(stderr, #op "failed"); \
312 fprintf(stderr, "\ncurve = "); type_ecurve.dump(v, stderr); \
313 fprintf(stderr, "\n a = "); ecdodump(a, stderr); \
314 fprintf(stderr, "\n b = "); ecdodump(b, stderr); \
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(b); EC_DESTROY(r); EC_DESTROY(&c); \
330 static int vcheck(dstr v
[])
332 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
333 ec
*a
= (ec
*)v
[1].buf
;
334 int r
= *(int *)v
[2].buf
;
339 fprintf(stderr
, "check failed");
340 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
341 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
342 fprintf(stderr
, "\n r = %d", r
);
343 fprintf(stderr
, "\n c = %d", c
);
344 fprintf(stderr
, "\n");
352 static int vmul(dstr v
[])
354 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
355 ec
*a
= (ec
*)v
[1].buf
;
356 mp
*n
= *(mp
**)v
[2].buf
;
357 ec
*r
= (ec
*)v
[3].buf
;
362 fprintf(stderr
, "mul failed");
363 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
364 fprintf(stderr
, "\n a = "); ecdodump(a
, stderr
);
365 fprintf(stderr
, "\n n = "); mp_writefile(n
, stderr
, 10);
366 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
367 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
368 fprintf(stderr
, "\n");
371 EC_DESTROY(a
); EC_DESTROY(r
); EC_DESTROY(&c
); MP_DROP(n
);
376 static int vfind(dstr v
[])
378 ec_curve
*e
= *(ec_curve
**)v
[0].buf
;
379 mp
*x
= *(mp
**)v
[1].buf
;
380 ec
*r
= (ec
*)v
[2].buf
;
383 if (!ec_find(e
, &c
, x
)) EC_SETINF(&c
);
385 fprintf(stderr
, "find failed");
386 fprintf(stderr
, "\ncurve = "); type_ecurve
.dump(v
, stderr
);
387 fprintf(stderr
, "\n x = "); mp_writefile(x
, stderr
, 16);
388 fprintf(stderr
, "\n r = "); ecdodump(r
, stderr
);
389 fprintf(stderr
, "\n c = "); ecdodump(&c
, stderr
);
390 fprintf(stderr
, "\n");
393 MP_DROP(x
); EC_DESTROY(r
); EC_DESTROY(&c
);
398 static test_chunk tests
[] = {
399 { "neg", vneg
, { &type_ecurve
, &type_ec
, &type_ec
} },
400 { "dbl", vdbl
, { &type_ecurve
, &type_ec
, &type_ec
} },
401 { "add", vadd
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
402 { "sub", vsub
, { &type_ecurve
, &type_ec
, &type_ec
, &type_ec
} },
403 { "mul", vmul
, { &type_ecurve
, &type_ec
, &type_mp
, &type_ec
} },
404 { "check", vcheck
, { &type_ecurve
, &type_ec
, &type_int
} },
405 { "find", vfind
, { &type_ecurve
, &type_mp
, &type_ec
} },
409 int main(int argc
, char *argv
[])
412 test_run(argc
, argv
, tests
, SRCDIR
"/tests/ec");
418 /*----- That's all, folks -------------------------------------------------*/