3 * Testing group operations
5 * (c) 2004 Straylight/Edgeware
8 /*----- Licensing notice --------------------------------------------------*
10 * This file is part of Catacomb.
12 * Catacomb is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU Library General Public License as
14 * published by the Free Software Foundation; either version 2 of the
15 * License, or (at your option) any later version.
17 * Catacomb is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Library General Public License for more details.
22 * You should have received a copy of the GNU Library General Public
23 * License along with Catacomb; if not, write to the Free
24 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
28 /*----- Header files ------------------------------------------------------*/
32 #include <mLib/testrig.h>
39 /*----- Main code ---------------------------------------------------------*/
41 static group *getgroup(const char *p) {
42 group *g; qd_parse qd;
43 qd.p = p; qd.e = 0; g = group_parse(&qd);
44 if (g && !qd_eofp(&qd)) { G_DESTROYGROUP(g); g = 0; qd.e = "junk at eof"; }
45 if (!g) { fprintf(stderr, "bad group string `%.*s|%s': %s\n", qd.p - p,
46 p, qd.p, qd.e); exit(1); }
50 static ge *getge(group *g, const char *p) {
52 if (group_readstring(g, x, p, 0)) {
53 fprintf(stderr, "bad group element `%s'\n", p);
59 static void show(group *g, const char *p, ge *x) {
60 fprintf(stderr, "*** %s = ", p); group_writefile(g, x, stderr);
64 static void showec(const char *p, ec *q) {
65 fprintf(stderr, "*** %s = ", p);
66 if (EC_ATINF(q)) fprintf(stderr, "inf\n");
68 mp_writefile(q->x, stderr, 16); fputs(", ", stderr);
69 mp_writefile(q->x, stderr, 16); putchar('\n');
73 static void showmp(const char *p, mp *x, int r) {
74 fprintf(stderr, "*** %s = ", p); mp_writefile(x, stderr, r);
78 static int check(const char *op, const char *gd, group *g,
82 if (G_EQ(g, r, c)) return (1);
83 fprintf(stderr, "\n*** %s failed\n", op);
84 fprintf(stderr, "*** group: %s\n", gd);
88 p = va_arg(ap, const char *); if (!p) break;
89 x = va_arg(ap, ge *); show(g, p, x);
91 show(g, "expected", r);
92 show(g, "computed", c);
96 /*----- Actual tests ------------------------------------------------------*/
98 static int vcheck(dstr *v)
100 group *g = getgroup(v[0].buf);
101 grand *gr = fibrand_create(0);
102 const char *e = G_CHECK(g, gr);
104 gr->ops->destroy(gr);
107 if (strcmp(e, v[1].buf)) {
109 fprintf(stderr, "*** check failed\n");
110 fprintf(stderr, "*** group: %s\n", v[0].buf);
111 fprintf(stderr, "*** expected: %s\n", v[1].buf);
112 fprintf(stderr, "*** returned: %s\n", e);
114 assert(mparena_count(MPARENA_GLOBAL) == 0);
118 static int vcheckelt(dstr *v)
120 group *g = getgroup(v[0].buf);
121 ge *x = getge(g, v[1].buf);
122 int ir = *(int *)v[2].buf;
123 int ic = group_check(g, x);
127 fprintf(stderr, "*** check failed\n");
128 fprintf(stderr, "*** group: %s\n", v[0].buf);
130 fprintf(stderr, "*** expected %s\n", ir ? "failure" : "success");
134 assert(mparena_count(MPARENA_GLOBAL) == 0);
138 static int vmul(dstr *v)
140 group *g = getgroup(v[0].buf);
141 ge *x = getge(g, v[1].buf);
142 ge *y = getge(g, v[2].buf);
143 ge *r = getge(g, v[3].buf);
147 ok &= check("mul", v[0].buf, g, r, c, "x", x, "y", y, (char *)0);
148 G_DESTROY(g, x); G_DESTROY(g, y); G_DESTROY(g, r); G_DESTROY(g, c);
150 assert(mparena_count(MPARENA_GLOBAL) == 0);
154 static int vsqr(dstr *v)
156 group *g = getgroup(v[0].buf);
157 ge *x = getge(g, v[1].buf);
158 ge *r = getge(g, v[2].buf);
162 ok &= check("sqr", v[0].buf, g, r, c, "x", x, (char *)0);
163 G_DESTROY(g, x); G_DESTROY(g, r); G_DESTROY(g, c);
165 assert(mparena_count(MPARENA_GLOBAL) == 0);
169 static int vinv(dstr *v)
171 group *g = getgroup(v[0].buf);
172 ge *x = getge(g, v[1].buf);
173 ge *r = getge(g, v[2].buf);
177 ok &= check("inv", v[0].buf, g, r, c, "x", x, (char *)0);
178 G_DESTROY(g, x); G_DESTROY(g, r); G_DESTROY(g, c);
180 assert(mparena_count(MPARENA_GLOBAL) == 0);
184 static int vdiv(dstr *v)
186 group *g = getgroup(v[0].buf);
187 ge *x = getge(g, v[1].buf);
188 ge *y = getge(g, v[2].buf);
189 ge *r = getge(g, v[3].buf);
193 ok &= check("div", v[0].buf, g, r, c, "x", x, "y", y, (char *)0);
194 group_stddiv(g, c, x, y);
195 ok &= check("stddiv", v[0].buf, g, r, c, "x", x, "y", y, (char *)0);
196 G_DESTROY(g, x); G_DESTROY(g, y); G_DESTROY(g, r); G_DESTROY(g, c);
198 assert(mparena_count(MPARENA_GLOBAL) == 0);
202 static int vexp(dstr *v)
204 group *g = getgroup(v[0].buf);
205 ge *x = getge(g, v[1].buf);
206 mp *n = *(mp **)v[2].buf;
207 ge *r = getge(g, v[3].buf);
211 if (!G_EQ(g, r, c)) {
213 fprintf(stderr, "\n*** exp failed\n");
214 fprintf(stderr, "*** group: %s\n", v[0].buf);
215 show(g, "x", x); showmp("n", n, 10);
216 show(g, "expected", r); show(g, "computed", c);
218 group_stdexp(g, c, x, n);
219 if (!G_EQ(g, r, c)) {
221 fprintf(stderr, "\n*** stdexp failed\n");
222 fprintf(stderr, "*** group: %s\n", v[0].buf);
223 show(g, "x", x); showmp("n", n, 10);
224 show(g, "expected", r); show(g, "computed", c);
226 G_DESTROY(g, x); MP_DROP(n); G_DESTROY(g, r); G_DESTROY(g, c);
228 assert(mparena_count(MPARENA_GLOBAL) == 0);
232 static int vmexp(size_t n, dstr *v)
234 group *g = getgroup(v[0].buf);
236 group_expfactor *f = xmalloc(n * sizeof(group_expfactor));
239 for (i = 0; i < n; i++) {
240 f[i].base = getge(g, v[1 + 2 * i].buf);
241 f[i].exp = *(mp **)v[2 + 2 * i].buf;
243 r = getge(g, v[1 + 2 * n].buf);
246 if (!G_EQ(g, r, c)) {
248 fprintf(stderr, "\n*** mexp failed\n");
249 fprintf(stderr, "*** group: %s\n", v[0].buf);
250 for (i = 0; i < n; i++) {
251 show(g, "base", f[i].base);
252 showmp("exp", f[i].exp, 10);
254 show(g, "expected", r); show(g, "computed", c);
256 group_stdmexp(g, c, f, n);
257 if (!G_EQ(g, r, c)) {
259 fprintf(stderr, "\n*** stdmexp failed\n");
260 fprintf(stderr, "*** group: %s\n", v[0].buf);
261 for (i = 0; i < n; i++) {
262 show(g, "base", f[i].base);
263 showmp("exp", f[i].exp, 10);
265 show(g, "expected", r); show(g, "computed", c);
267 for (i = 0; i < n; i++) { G_DESTROY(g, f[i].base); MP_DROP(f[i].exp); }
268 G_DESTROY(g, r); G_DESTROY(g, c);
270 assert(mparena_count(MPARENA_GLOBAL) == 0);
274 static int vmexp1(dstr *v) { return vmexp(1, v); }
275 static int vmexp2(dstr *v) { return vmexp(2, v); }
276 static int vmexp3(dstr *v) { return vmexp(3, v); }
277 static int vmexp4(dstr *v) { return vmexp(4, v); }
279 static int vtoint(dstr *v)
281 group *g = getgroup(v[0].buf);
282 ge *x = getge(g, v[1].buf);
283 int ir = *(int *)v[2].buf;
284 mp *r = *(mp **)v[3].buf;
288 c = G_TOINT(g, MP_NEW, x);
290 if (ir != ic || (!ic && !MP_EQ(r, c))) {
292 fprintf(stderr, "\n*** toint failed\n");
293 fprintf(stderr, "*** group: %s\n", v[0].buf);
294 if (ir) fprintf(stderr, "*** expected failure\n");
295 else { show(g, "x", x); showmp("expected", r, 16);
296 showmp("computed", c, 16); }
298 G_DESTROY(g, x); mp_drop(r); mp_drop(c);
300 assert(mparena_count(MPARENA_GLOBAL) == 0);
304 static int vfromint(dstr *v)
306 group *g = getgroup(v[0].buf);
307 mp *x = *(mp **)v[1].buf;
308 int ir = *(int *)v[2].buf;
309 ge *r = getge(g, v[3].buf);
313 ic = G_FROMINT(g, c, x);
314 if (ir != ic || (!ic && !G_EQ(g, r, c))) {
316 fprintf(stderr, "\n*** fromint failed\n");
317 fprintf(stderr, "*** group: %s\n", v[0].buf);
318 showmp("x", x, 16); if (ir) fprintf(stderr, "*** should have failed\n");
319 else { show(g, "expected", r); show(g, "computed", c); }
321 MP_DROP(x); G_DESTROY(g, r); G_DESTROY(g, c);
323 assert(mparena_count(MPARENA_GLOBAL) == 0);
327 static int vtoec(dstr *v)
329 group *g = getgroup(v[0].buf);
330 ge *x = getge(g, v[1].buf);
331 int ir = *(int *)v[2].buf;
332 ec *r = (ec *)v[3].buf;
336 ic = G_TOEC(g, &c, x);
337 if (ir != ic || (!ic && !EC_EQ(r, &c))) {
339 fprintf(stderr, "\n*** toec failed\n");
340 fprintf(stderr, "*** group: %s\n", v[0].buf);
342 if (ir) fprintf(stderr, "*** should have failed\n");
343 else { showec("expected", r); showec("computed", &c); }
345 G_DESTROY(g, x); EC_DESTROY(&c); EC_DESTROY(r);
347 assert(mparena_count(MPARENA_GLOBAL) == 0);
351 static int vfromec(dstr *v)
353 group *g = getgroup(v[0].buf);
354 ec *p = (ec *)v[1].buf;
355 int ir = *(int *)v[2].buf;
356 ge *r = getge(g, v[3].buf);
360 ic = G_FROMEC(g, c, p);
361 if (ir != ic || (!ic && !G_EQ(g, r, c))) {
363 fprintf(stderr, "\n*** fromec failed\n");
364 fprintf(stderr, "*** group: %s\n", v[0].buf);
365 showec("p", p); if (ir) fprintf(stderr, "*** should have failed\n");
366 else { show(g, "expected", r); show(g, "computed", c); }
368 EC_DESTROY(p); G_DESTROY(g, r); G_DESTROY(g, c);
370 assert(mparena_count(MPARENA_GLOBAL) == 0);
374 static int vtobuf(dstr *v)
376 group *g = getgroup(v[0].buf);
377 ge *x = getge(g, v[1].buf);
378 int ir = *(int *)v[2].buf;
384 dstr_ensure(&c, v[3].len);
385 buf_init(&b, c.buf, v[3].len);
386 ic = G_TOBUF(g, &b, x);
388 if (ic != ir || (!ic && (c.len != v[3].len ||
389 memcmp(c.buf, v[3].buf, c.len)))) {
391 fprintf(stderr, "*** tobuf failed\n");
392 fprintf(stderr, "*** group: %s\n", v[0].buf);
394 if (ir) fprintf(stderr, "*** expected failure\n");
396 fprintf(stderr, "*** expected: "); type_hex.dump(&v[3], stderr);
397 fprintf(stderr, "\n*** computed: "); type_hex.dump(&c, stderr);
401 G_DESTROY(g, x); dstr_destroy(&c);
403 assert(mparena_count(MPARENA_GLOBAL) == 0);
407 static int vfrombuf(dstr *v)
409 group *g = getgroup(v[0].buf);
410 int ir = *(int *)v[2].buf;
411 ge *r = getge(g, v[3].buf);
417 buf_init(&b, v[1].buf, v[1].len);
418 ic = G_FROMBUF(g, &b, c);
419 if ((ic < 0) != (ir < 0) || (ir >= 0 &&
420 (ir != BLEN(&b) || !G_EQ(g, r, c)))) {
422 fprintf(stderr, "*** frombuf failed\n");
423 fprintf(stderr, "*** group: %s\n", v[0].buf);
424 fprintf(stderr, "*** input string: "); type_hex.dump(&v[1], stderr);
426 if (ir < 0) fprintf(stderr, "*** expected failure\n");
428 show(g, "expected", r); show(g, "computed", c);
429 fprintf(stderr, "*** expected used = %d\n", ir);
430 fprintf(stderr, "*** computed used = %lu\n", (unsigned long)BLEN(&b));
433 G_DESTROY(g, r); G_DESTROY(g, c);
435 assert(mparena_count(MPARENA_GLOBAL) == 0);
439 static int vtoraw(dstr *v)
441 group *g = getgroup(v[0].buf);
442 ge *x = getge(g, v[1].buf);
443 int ir = *(int *)v[2].buf;
449 dstr_ensure(&c, v[3].len);
450 buf_init(&b, c.buf, v[3].len);
451 ic = G_TORAW(g, &b, x);
453 if (ic != ir || (!ic && (c.len != v[3].len ||
454 memcmp(c.buf, v[3].buf, c.len)))) {
456 fprintf(stderr, "*** toraw failed\n");
457 fprintf(stderr, "*** group: %s\n", v[0].buf);
459 if (ir) fprintf(stderr, "*** expected failure\n");
461 fprintf(stderr, "*** expected: "); type_hex.dump(&v[3], stderr);
462 fprintf(stderr, "\n*** computed: "); type_hex.dump(&c, stderr);
466 G_DESTROY(g, x); dstr_destroy(&c);
468 assert(mparena_count(MPARENA_GLOBAL) == 0);
472 static int vfromraw(dstr *v)
474 group *g = getgroup(v[0].buf);
475 int ir = *(int *)v[2].buf;
476 ge *r = getge(g, v[3].buf);
482 buf_init(&b, v[1].buf, v[1].len);
483 ic = G_FROMRAW(g, &b, c);
484 if ((ic < 0) != (ir < 0) || (ir >= 0 &&
485 (ir != BLEN(&b) || !G_EQ(g, r, c)))) {
487 fprintf(stderr, "*** fromraw failed\n");
488 fprintf(stderr, "*** group: %s\n", v[0].buf);
489 fprintf(stderr, "*** input string: "); type_hex.dump(&v[1], stderr);
491 if (ir < 0) fprintf(stderr, "*** expected failure\n");
493 show(g, "expected", r); show(g, "computed", c);
494 fprintf(stderr, "*** expected used = %d\n", ir);
495 fprintf(stderr, "*** computed used = %lu\n", (unsigned long)BLEN(&b));
498 G_DESTROY(g, r); G_DESTROY(g, c);
500 assert(mparena_count(MPARENA_GLOBAL) == 0);
504 static const test_chunk tests[] = {
505 { "check", vcheck, { &type_string, &type_string } },
506 { "checkelt", vcheckelt, { &type_string, &type_string, &type_int } },
507 { "mul", vmul, { &type_string, &type_string,
508 &type_string, &type_string } },
509 { "sqr", vsqr, { &type_string, &type_string,
511 { "inv", vinv, { &type_string, &type_string,
513 { "div", vdiv, { &type_string, &type_string,
514 &type_string, &type_string } },
515 { "exp", vexp, { &type_string, &type_string,
516 &type_mp, &type_string } },
517 { "mexp-1", vmexp1, { &type_string,
518 &type_string, &type_mp,
520 { "mexp-2", vmexp2, { &type_string,
521 &type_string, &type_mp,
522 &type_string, &type_mp,
524 { "mexp-3", vmexp3, { &type_string,
525 &type_string, &type_mp,
526 &type_string, &type_mp,
527 &type_string, &type_mp,
529 { "mexp-4", vmexp4, { &type_string,
530 &type_string, &type_mp,
531 &type_string, &type_mp,
532 &type_string, &type_mp,
533 &type_string, &type_mp,
535 { "toint", vtoint, { &type_string, &type_string,
536 &type_int, &type_mp } },
537 { "fromint", vfromint, { &type_string, &type_mp,
538 &type_int, &type_string } },
539 { "toec", vtoec, { &type_string, &type_string,
540 &type_int, &type_ec } },
541 { "fromec", vfromec, { &type_string, &type_ec,
542 &type_int, &type_string } },
543 { "tobuf", vtobuf, { &type_string, &type_string,
544 &type_int, &type_hex } },
545 { "frombuf", vfrombuf, { &type_string, &type_hex,
546 &type_int, &type_string } },
547 { "toraw", vtoraw, { &type_string, &type_string,
548 &type_int, &type_hex } },
549 { "fromraw", vfromraw, { &type_string, &type_hex,
550 &type_int, &type_string } },
554 int main(int argc, char *argv[])
557 test_run(argc, argv, tests, SRCDIR "/t/group");
561 /*----- That's all, folks -------------------------------------------------*/