7c1936b5a46874b273a124ba7d1ade72bc812045
3 * $Id: group-test.c,v 1.2 2004/04/04 19:04:11 mdw Exp $
5 * Testing group operations
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 --------------------------------------------------*
32 * $Log: group-test.c,v $
33 * Revision 1.2 2004/04/04 19:04:11 mdw
34 * Raw I/O of elliptic curve points and group elements.
36 * Revision 1.1 2004/04/01 12:50:09 mdw
37 * Add cyclic group abstraction, with test code. Separate off exponentation
38 * functions for better static linking. Fix a buttload of bugs on the way.
39 * Generally ensure that negative exponents do inversion correctly. Add
40 * table of standard prime-field subgroups. (Binary field subgroups are
41 * currently unimplemented but easy to add if anyone ever finds a good one.)
45 /*----- Header files ------------------------------------------------------*/
49 #include <mLib/testrig.h>
56 /*----- Main code ---------------------------------------------------------*/
58 static group
*getgroup(const char *p
) {
59 group
*g
; qd_parse qd
;
60 qd
.p
= p
; qd
.e
= 0; g
= group_parse(&qd
);
61 if (g
&& !qd_eofp(&qd
)) { G_DESTROYGROUP(g
); g
= 0; qd
.e
= "junk at eof"; }
62 if (!g
) { fprintf(stderr
, "bad group string `%.*s|%s': %s\n", qd
.p
- p
,
63 p
, qd
.p
, qd
.e
); exit(1); }
67 static ge
*getge(group
*g
, const char *p
) {
69 if (group_readstring(g
, x
, p
, 0)) {
70 fprintf(stderr
, "bad group element `%s'\n", p
);
76 static void show(group
*g
, const char *p
, ge
*x
) {
77 fprintf(stderr
, "*** %s = ", p
); group_writefile(g
, x
, stderr
);
81 static void showec(const char *p
, ec
*q
) {
82 fprintf(stderr
, "*** %s = ", p
);
83 if (EC_ATINF(q
)) fprintf(stderr
, "inf\n");
85 mp_writefile(q
->x
, stderr
, 16); fputs(", ", stderr
);
86 mp_writefile(q
->x
, stderr
, 16); putchar('\n');
90 static void showmp(const char *p
, mp
*x
, int r
) {
91 fprintf(stderr
, "*** %s = ", p
); mp_writefile(x
, stderr
, r
);
95 static int check(const char *op
, const char *gd
, group
*g
,
99 if (G_EQ(g
, r
, c
)) return (1);
100 fprintf(stderr
, "\n*** %s failed\n", op
);
101 fprintf(stderr
, "*** group: %s\n", gd
);
104 const char *p
; ge
*x
;
105 p
= va_arg(ap
, const char *); if (!p
) break;
106 x
= va_arg(ap
, ge
*); show(g
, p
, x
);
108 show(g
, "expected", r
);
109 show(g
, "computed", c
);
113 /*----- Actual tests ------------------------------------------------------*/
115 static int vcheck(dstr
*v
)
117 group
*g
= getgroup(v
[0].buf
);
118 grand
*gr
= fibrand_create(0);
119 const char *e
= G_CHECK(g
, gr
);
121 gr
->ops
->destroy(gr
);
124 if (strcmp(e
, v
[1].buf
)) {
126 fprintf(stderr
, "*** check failed\n");
127 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
128 fprintf(stderr
, "*** expected: %s\n", v
[1].buf
);
129 fprintf(stderr
, "*** returned: %s\n", e
);
131 assert(mparena_count(MPARENA_GLOBAL
) == 0);
135 static int vcheckelt(dstr
*v
)
137 group
*g
= getgroup(v
[0].buf
);
138 ge
*x
= getge(g
, v
[1].buf
);
139 int ir
= *(int *)v
[2].buf
;
140 int ic
= group_check(g
, x
);
144 fprintf(stderr
, "*** check failed\n");
145 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
147 fprintf(stderr
, "*** expected %s\n", ir ?
"failure" : "success");
151 assert(mparena_count(MPARENA_GLOBAL
) == 0);
155 static int vmul(dstr
*v
)
157 group
*g
= getgroup(v
[0].buf
);
158 ge
*x
= getge(g
, v
[1].buf
);
159 ge
*y
= getge(g
, v
[2].buf
);
160 ge
*r
= getge(g
, v
[3].buf
);
164 ok
&= check("mul", v
[0].buf
, g
, r
, c
, "x", x
, "y", y
, (char *)0);
165 G_DESTROY(g
, x
); G_DESTROY(g
, y
); G_DESTROY(g
, r
); G_DESTROY(g
, c
);
167 assert(mparena_count(MPARENA_GLOBAL
) == 0);
171 static int vsqr(dstr
*v
)
173 group
*g
= getgroup(v
[0].buf
);
174 ge
*x
= getge(g
, v
[1].buf
);
175 ge
*r
= getge(g
, v
[2].buf
);
179 ok
&= check("sqr", v
[0].buf
, g
, r
, c
, "x", x
, (char *)0);
180 G_DESTROY(g
, x
); G_DESTROY(g
, r
); G_DESTROY(g
, c
);
182 assert(mparena_count(MPARENA_GLOBAL
) == 0);
186 static int vinv(dstr
*v
)
188 group
*g
= getgroup(v
[0].buf
);
189 ge
*x
= getge(g
, v
[1].buf
);
190 ge
*r
= getge(g
, v
[2].buf
);
194 ok
&= check("inv", v
[0].buf
, g
, r
, c
, "x", x
, (char *)0);
195 G_DESTROY(g
, x
); G_DESTROY(g
, r
); G_DESTROY(g
, c
);
197 assert(mparena_count(MPARENA_GLOBAL
) == 0);
201 static int vdiv(dstr
*v
)
203 group
*g
= getgroup(v
[0].buf
);
204 ge
*x
= getge(g
, v
[1].buf
);
205 ge
*y
= getge(g
, v
[2].buf
);
206 ge
*r
= getge(g
, v
[3].buf
);
210 ok
&= check("div", v
[0].buf
, g
, r
, c
, "x", x
, "y", y
, (char *)0);
211 group_stddiv(g
, c
, x
, y
);
212 ok
&= check("stddiv", v
[0].buf
, g
, r
, c
, "x", x
, "y", y
, (char *)0);
213 G_DESTROY(g
, x
); G_DESTROY(g
, y
); G_DESTROY(g
, r
); G_DESTROY(g
, c
);
215 assert(mparena_count(MPARENA_GLOBAL
) == 0);
219 static int vexp(dstr
*v
)
221 group
*g
= getgroup(v
[0].buf
);
222 ge
*x
= getge(g
, v
[1].buf
);
223 mp
*n
= *(mp
**)v
[2].buf
;
224 ge
*r
= getge(g
, v
[3].buf
);
228 if (!G_EQ(g
, r
, c
)) {
230 fprintf(stderr
, "\n*** exp failed\n");
231 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
232 show(g
, "x", x
); showmp("n", n
, 10);
233 show(g
, "expected", r
); show(g
, "computed", c
);
235 group_stdexp(g
, c
, x
, n
);
236 if (!G_EQ(g
, r
, c
)) {
238 fprintf(stderr
, "\n*** stdexp failed\n");
239 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
240 show(g
, "x", x
); showmp("n", n
, 10);
241 show(g
, "expected", r
); show(g
, "computed", c
);
243 G_DESTROY(g
, x
); MP_DROP(n
); G_DESTROY(g
, r
); G_DESTROY(g
, c
);
245 assert(mparena_count(MPARENA_GLOBAL
) == 0);
249 static int vmexp(size_t n
, dstr
*v
)
251 group
*g
= getgroup(v
[0].buf
);
253 group_expfactor
*f
= xmalloc(n
* sizeof(group_expfactor
));
256 for (i
= 0; i
< n
; i
++) {
257 f
[i
].base
= getge(g
, v
[1 + 2 * i
].buf
);
258 f
[i
].exp
= *(mp
**)v
[2 + 2 * i
].buf
;
260 r
= getge(g
, v
[1 + 2 * n
].buf
);
263 if (!G_EQ(g
, r
, c
)) {
265 fprintf(stderr
, "\n*** mexp failed\n");
266 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
267 for (i
= 0; i
< n
; i
++) {
268 show(g
, "base", f
[i
].base
);
269 showmp("exp", f
[i
].exp
, 10);
271 show(g
, "expected", r
); show(g
, "computed", c
);
273 group_stdmexp(g
, c
, f
, n
);
274 if (!G_EQ(g
, r
, c
)) {
276 fprintf(stderr
, "\n*** stdmexp failed\n");
277 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
278 for (i
= 0; i
< n
; i
++) {
279 show(g
, "base", f
[i
].base
);
280 showmp("exp", f
[i
].exp
, 10);
282 show(g
, "expected", r
); show(g
, "computed", c
);
284 for (i
= 0; i
< n
; i
++) { G_DESTROY(g
, f
[i
].base
); MP_DROP(f
[i
].exp
); }
285 G_DESTROY(g
, r
); G_DESTROY(g
, c
);
287 assert(mparena_count(MPARENA_GLOBAL
) == 0);
291 static int vmexp1(dstr
*v
) { return vmexp(1, v
); }
292 static int vmexp2(dstr
*v
) { return vmexp(2, v
); }
293 static int vmexp3(dstr
*v
) { return vmexp(3, v
); }
294 static int vmexp4(dstr
*v
) { return vmexp(4, v
); }
296 static int vtoint(dstr
*v
)
298 group
*g
= getgroup(v
[0].buf
);
299 ge
*x
= getge(g
, v
[1].buf
);
300 int ir
= *(int *)v
[2].buf
;
301 mp
*r
= *(mp
**)v
[3].buf
;
305 c
= G_TOINT(g
, MP_NEW
, x
);
307 if (ir
!= ic
|| (!ic
&& !MP_EQ(r
, c
))) {
309 fprintf(stderr
, "\n*** toint failed\n");
310 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
311 if (ir
) fprintf(stderr
, "*** expected failure\n");
312 else { show(g
, "x", x
); showmp("expected", r
, 16);
313 showmp("computed", c
, 16); }
315 G_DESTROY(g
, x
); mp_drop(r
); mp_drop(c
);
317 assert(mparena_count(MPARENA_GLOBAL
) == 0);
321 static int vfromint(dstr
*v
)
323 group
*g
= getgroup(v
[0].buf
);
324 mp
*x
= *(mp
**)v
[1].buf
;
325 int ir
= *(int *)v
[2].buf
;
326 ge
*r
= getge(g
, v
[3].buf
);
330 ic
= G_FROMINT(g
, c
, x
);
331 if (ir
!= ic
|| (!ic
&& !G_EQ(g
, r
, c
))) {
333 fprintf(stderr
, "\n*** fromint failed\n");
334 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
335 showmp("x", x
, 16); if (ir
) fprintf(stderr
, "*** should have failed\n");
336 else { show(g
, "expected", r
); show(g
, "computed", c
); }
338 MP_DROP(x
); G_DESTROY(g
, r
); G_DESTROY(g
, c
);
340 assert(mparena_count(MPARENA_GLOBAL
) == 0);
344 static int vtoec(dstr
*v
)
346 group
*g
= getgroup(v
[0].buf
);
347 ge
*x
= getge(g
, v
[1].buf
);
348 int ir
= *(int *)v
[2].buf
;
349 ec
*r
= (ec
*)v
[3].buf
;
353 ic
= G_TOEC(g
, &c
, x
);
354 if (ir
!= ic
|| (!ic
&& !EC_EQ(r
, &c
))) {
356 fprintf(stderr
, "\n*** toec failed\n");
357 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
359 if (ir
) fprintf(stderr
, "*** should have failed\n");
360 else { showec("expected", r
); showec("computed", &c
); }
362 G_DESTROY(g
, x
); EC_DESTROY(&c
); EC_DESTROY(r
);
364 assert(mparena_count(MPARENA_GLOBAL
) == 0);
368 static int vfromec(dstr
*v
)
370 group
*g
= getgroup(v
[0].buf
);
371 ec
*p
= (ec
*)v
[1].buf
;
372 int ir
= *(int *)v
[2].buf
;
373 ge
*r
= getge(g
, v
[3].buf
);
377 ic
= G_FROMEC(g
, c
, p
);
378 if (ir
!= ic
|| (!ic
&& !G_EQ(g
, r
, c
))) {
380 fprintf(stderr
, "\n*** fromec failed\n");
381 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
382 showec("p", p
); if (ir
) fprintf(stderr
, "*** should have failed\n");
383 else { show(g
, "expected", r
); show(g
, "computed", c
); }
385 EC_DESTROY(p
); G_DESTROY(g
, r
); G_DESTROY(g
, c
);
387 assert(mparena_count(MPARENA_GLOBAL
) == 0);
391 static int vtobuf(dstr
*v
)
393 group
*g
= getgroup(v
[0].buf
);
394 ge
*x
= getge(g
, v
[1].buf
);
395 int ir
= *(int *)v
[2].buf
;
401 dstr_ensure(&c
, v
[3].len
);
402 buf_init(&b
, c
.buf
, v
[3].len
);
403 ic
= G_TOBUF(g
, &b
, x
);
405 if (ic
!= ir
|| (!ic
&& (c
.len
!= v
[3].len
||
406 memcmp(c
.buf
, v
[3].buf
, c
.len
)))) {
408 fprintf(stderr
, "*** tobuf failed\n");
409 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
411 if (ir
) fprintf(stderr
, "*** expected failure\n");
413 fprintf(stderr
, "*** expected: "); type_hex
.dump(&v
[3], stderr
);
414 fprintf(stderr
, "\n*** computed: "); type_hex
.dump(&c
, stderr
);
418 G_DESTROY(g
, x
); dstr_destroy(&c
);
420 assert(mparena_count(MPARENA_GLOBAL
) == 0);
424 static int vfrombuf(dstr
*v
)
426 group
*g
= getgroup(v
[0].buf
);
427 int ir
= *(int *)v
[2].buf
;
428 ge
*r
= getge(g
, v
[3].buf
);
434 buf_init(&b
, v
[1].buf
, v
[1].len
);
435 ic
= G_FROMBUF(g
, &b
, c
);
436 if ((ic
< 0) != (ir
< 0) || (ir
>= 0 &&
437 (ir
!= BLEN(&b
) || !G_EQ(g
, r
, c
)))) {
439 fprintf(stderr
, "*** frombuf failed\n");
440 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
441 fprintf(stderr
, "*** input string: "); type_hex
.dump(&v
[1], stderr
);
443 if (ir
< 0) fprintf(stderr
, "*** expected failure\n");
445 show(g
, "expected", r
); show(g
, "computed", c
);
446 fprintf(stderr
, "*** expected used = %d\n", ir
);
447 fprintf(stderr
, "*** computed used = %lu\n", (unsigned long)BLEN(&b
));
450 G_DESTROY(g
, r
); G_DESTROY(g
, c
);
452 assert(mparena_count(MPARENA_GLOBAL
) == 0);
456 static int vtoraw(dstr
*v
)
458 group
*g
= getgroup(v
[0].buf
);
459 ge
*x
= getge(g
, v
[1].buf
);
460 int ir
= *(int *)v
[2].buf
;
466 dstr_ensure(&c
, v
[3].len
);
467 buf_init(&b
, c
.buf
, v
[3].len
);
468 ic
= G_TORAW(g
, &b
, x
);
470 if (ic
!= ir
|| (!ic
&& (c
.len
!= v
[3].len
||
471 memcmp(c
.buf
, v
[3].buf
, c
.len
)))) {
473 fprintf(stderr
, "*** toraw failed\n");
474 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
476 if (ir
) fprintf(stderr
, "*** expected failure\n");
478 fprintf(stderr
, "*** expected: "); type_hex
.dump(&v
[3], stderr
);
479 fprintf(stderr
, "\n*** computed: "); type_hex
.dump(&c
, stderr
);
483 G_DESTROY(g
, x
); dstr_destroy(&c
);
485 assert(mparena_count(MPARENA_GLOBAL
) == 0);
489 static int vfromraw(dstr
*v
)
491 group
*g
= getgroup(v
[0].buf
);
492 int ir
= *(int *)v
[2].buf
;
493 ge
*r
= getge(g
, v
[3].buf
);
499 buf_init(&b
, v
[1].buf
, v
[1].len
);
500 ic
= G_FROMRAW(g
, &b
, c
);
501 if ((ic
< 0) != (ir
< 0) || (ir
>= 0 &&
502 (ir
!= BLEN(&b
) || !G_EQ(g
, r
, c
)))) {
504 fprintf(stderr
, "*** fromraw failed\n");
505 fprintf(stderr
, "*** group: %s\n", v
[0].buf
);
506 fprintf(stderr
, "*** input string: "); type_hex
.dump(&v
[1], stderr
);
508 if (ir
< 0) fprintf(stderr
, "*** expected failure\n");
510 show(g
, "expected", r
); show(g
, "computed", c
);
511 fprintf(stderr
, "*** expected used = %d\n", ir
);
512 fprintf(stderr
, "*** computed used = %lu\n", (unsigned long)BLEN(&b
));
515 G_DESTROY(g
, r
); G_DESTROY(g
, c
);
517 assert(mparena_count(MPARENA_GLOBAL
) == 0);
521 static const test_chunk tests
[] = {
522 { "check", vcheck
, { &type_string
, &type_string
} },
523 { "checkelt", vcheckelt
, { &type_string
, &type_string
, &type_int
} },
524 { "mul", vmul
, { &type_string
, &type_string
,
525 &type_string
, &type_string
} },
526 { "sqr", vsqr
, { &type_string
, &type_string
,
528 { "inv", vinv
, { &type_string
, &type_string
,
530 { "div", vdiv
, { &type_string
, &type_string
,
531 &type_string
, &type_string
} },
532 { "exp", vexp
, { &type_string
, &type_string
,
533 &type_mp
, &type_string
} },
534 { "mexp-1", vmexp1
, { &type_string
,
535 &type_string
, &type_mp
,
537 { "mexp-2", vmexp2
, { &type_string
,
538 &type_string
, &type_mp
,
539 &type_string
, &type_mp
,
541 { "mexp-3", vmexp3
, { &type_string
,
542 &type_string
, &type_mp
,
543 &type_string
, &type_mp
,
544 &type_string
, &type_mp
,
546 { "mexp-4", vmexp4
, { &type_string
,
547 &type_string
, &type_mp
,
548 &type_string
, &type_mp
,
549 &type_string
, &type_mp
,
550 &type_string
, &type_mp
,
552 { "toint", vtoint
, { &type_string
, &type_string
,
553 &type_int
, &type_mp
} },
554 { "fromint", vfromint
, { &type_string
, &type_mp
,
555 &type_int
, &type_string
} },
556 { "toec", vtoec
, { &type_string
, &type_string
,
557 &type_int
, &type_ec
} },
558 { "fromec", vfromec
, { &type_string
, &type_ec
,
559 &type_int
, &type_string
} },
560 { "tobuf", vtobuf
, { &type_string
, &type_string
,
561 &type_int
, &type_hex
} },
562 { "frombuf", vfrombuf
, { &type_string
, &type_hex
,
563 &type_int
, &type_string
} },
564 { "toraw", vtoraw
, { &type_string
, &type_string
,
565 &type_int
, &type_hex
} },
566 { "fromraw", vfromraw
, { &type_string
, &type_hex
,
567 &type_int
, &type_string
} },
571 int main(int argc
, char *argv
[])
574 test_run(argc
, argv
, tests
, SRCDIR
"/tests/group");
578 /*----- That's all, folks -------------------------------------------------*/