-/*----- Test rig ----------------------------------------------------------*/
-
-#ifdef TEST_RIG
-
-#include <mLib/report.h>
-#include <mLib/str.h>
-#include <mLib/testrig.h>
-
-static void fixdstr(dstr *d)
-{
- if (d->len > 56)
- die(1, "invalid length for fgoldi");
- else if (d->len < 56) {
- dstr_ensure(d, 56);
- memset(d->buf + d->len, 0, 56 - d->len);
- d->len = 56;
- }
-}
-
-static void cvt_fgoldi(const char *buf, dstr *d)
-{
- dstr dd = DSTR_INIT;
-
- type_hex.cvt(buf, &dd); fixdstr(&dd);
- dstr_ensure(d, sizeof(fgoldi)); d->len = sizeof(fgoldi);
- fgoldi_load((fgoldi *)d->buf, (const octet *)dd.buf);
- dstr_destroy(&dd);
-}
-
-static void dump_fgoldi(dstr *d, FILE *fp)
- { fdump(stderr, "???", (const piece *)d->buf); }
-
-static void cvt_fgoldi_ref(const char *buf, dstr *d)
- { type_hex.cvt(buf, d); fixdstr(d); }
-
-static void dump_fgoldi_ref(dstr *d, FILE *fp)
-{
- fgoldi x;
-
- fgoldi_load(&x, (const octet *)d->buf);
- fdump(stderr, "???", x.P);
-}
-
-static int eq(const fgoldi *x, dstr *d)
- { octet b[56]; fgoldi_store(b, x); return (memcmp(b, d->buf, 56) == 0); }
-
-static const test_type
- type_fgoldi = { cvt_fgoldi, dump_fgoldi },
- type_fgoldi_ref = { cvt_fgoldi_ref, dump_fgoldi_ref };
-
-#define TEST_UNOP(op) \
- static int vrf_##op(dstr dv[]) \
- { \
- fgoldi *x = (fgoldi *)dv[0].buf; \
- fgoldi z, zz; \
- int ok = 1; \
- \
- fgoldi_##op(&z, x); \
- if (!eq(&z, &dv[1])) { \
- ok = 0; \
- fprintf(stderr, "failed!\n"); \
- fdump(stderr, "x", x->P); \
- fdump(stderr, "calc", z.P); \
- fgoldi_load(&zz, (const octet *)dv[1].buf); \
- fdump(stderr, "z", zz.P); \
- } \
- \
- return (ok); \
- }
-
-TEST_UNOP(sqr)
-TEST_UNOP(inv)
-
-#define TEST_BINOP(op) \
- static int vrf_##op(dstr dv[]) \
- { \
- fgoldi *x = (fgoldi *)dv[0].buf, *y = (fgoldi *)dv[1].buf; \
- fgoldi z, zz; \
- int ok = 1; \
- \
- fgoldi_##op(&z, x, y); \
- if (!eq(&z, &dv[2])) { \
- ok = 0; \
- fprintf(stderr, "failed!\n"); \
- fdump(stderr, "x", x->P); \
- fdump(stderr, "y", y->P); \
- fdump(stderr, "calc", z.P); \
- fgoldi_load(&zz, (const octet *)dv[2].buf); \
- fdump(stderr, "z", zz.P); \
- } \
- \
- return (ok); \
- }
-
-TEST_BINOP(add)
-TEST_BINOP(sub)
-TEST_BINOP(mul)
-
-static int vrf_mulc(dstr dv[])
-{
- fgoldi *x = (fgoldi *)dv[0].buf;
- long a = *(const long *)dv[1].buf;
- fgoldi z, zz;
- int ok = 1;
-
- fgoldi_mulconst(&z, x, a);
- if (!eq(&z, &dv[2])) {
- ok = 0;
- fprintf(stderr, "failed!\n");
- fdump(stderr, "x", x->P);
- fprintf(stderr, "a = %ld\n", a);
- fdump(stderr, "calc", z.P);
- fgoldi_load(&zz, (const octet *)dv[2].buf);
- fdump(stderr, "z", zz.P);
- }
-
- return (ok);
-}
-
-static int vrf_condswap(dstr dv[])
-{
- fgoldi *x = (fgoldi *)dv[0].buf, *y = (fgoldi *)dv[1].buf;
- fgoldi xx = *x, yy = *y;
- uint32 m = *(uint32 *)dv[2].buf;
- int ok = 1;
-
- fgoldi_condswap(&xx, &yy, m);
- if (!eq(&xx, &dv[3]) || !eq(&yy, &dv[4])) {
- ok = 0;
- fprintf(stderr, "failed!\n");
- fdump(stderr, "x", x->P);
- fdump(stderr, "y", y->P);
- fprintf(stderr, "m = 0x%08lx\n", (unsigned long)m);
- fdump(stderr, "calc xx", xx.P);
- fdump(stderr, "calc yy", yy.P);
- fgoldi_load(&xx, (const octet *)dv[3].buf);
- fgoldi_load(&yy, (const octet *)dv[4].buf);
- fdump(stderr, "want xx", xx.P);
- fdump(stderr, "want yy", yy.P);
- }
-
- return (ok);
-}
-
-static int vrf_sub_mulc_add_sub_mul(dstr dv[])
-{
- fgoldi *u = (fgoldi *)dv[0].buf, *v = (fgoldi *)dv[1].buf,
- *w = (fgoldi *)dv[3].buf, *x = (fgoldi *)dv[4].buf,
- *y = (fgoldi *)dv[5].buf;
- long a = *(const long *)dv[2].buf;
- fgoldi umv, aumv, wpaumv, xmy, z, zz;
- int ok = 1;
-
- fgoldi_sub(&umv, u, v);
- fgoldi_mulconst(&aumv, &umv, a);
- fgoldi_add(&wpaumv, w, &aumv);
- fgoldi_sub(&xmy, x, y);
- fgoldi_mul(&z, &wpaumv, &xmy);
-
- if (!eq(&z, &dv[6])) {
- ok = 0;
- fprintf(stderr, "failed!\n");
- fdump(stderr, "u", u->P);
- fdump(stderr, "v", v->P);
- fdump(stderr, "u - v", umv.P);
- fprintf(stderr, "a = %ld\n", a);
- fdump(stderr, "a (u - v)", aumv.P);
- fdump(stderr, "w + a (u - v)", wpaumv.P);
- fdump(stderr, "x", x->P);
- fdump(stderr, "y", y->P);
- fdump(stderr, "x - y", xmy.P);
- fdump(stderr, "(x - y) (w + a (u - v))", z.P);
- fgoldi_load(&zz, (const octet *)dv[6].buf); fdump(stderr, "z", zz.P);
- }
-
- return (ok);
-}
-
-static test_chunk tests[] = {
- { "add", vrf_add, { &type_fgoldi, &type_fgoldi, &type_fgoldi_ref } },
- { "sub", vrf_sub, { &type_fgoldi, &type_fgoldi, &type_fgoldi_ref } },
- { "mul", vrf_mul, { &type_fgoldi, &type_fgoldi, &type_fgoldi_ref } },
- { "mulconst", vrf_mulc, { &type_fgoldi, &type_long, &type_fgoldi_ref } },
- { "condswap", vrf_condswap,
- { &type_fgoldi, &type_fgoldi, &type_uint32,
- &type_fgoldi_ref, &type_fgoldi_ref } },
- { "sqr", vrf_sqr, { &type_fgoldi, &type_fgoldi_ref } },
- { "inv", vrf_inv, { &type_fgoldi, &type_fgoldi_ref } },
- { "sub-mulc-add-sub-mul", vrf_sub_mulc_add_sub_mul,
- { &type_fgoldi, &type_fgoldi, &type_long, &type_fgoldi,
- &type_fgoldi, &type_fgoldi, &type_fgoldi_ref } },
- { 0, 0, { 0 } }
-};
-
-int main(int argc, char *argv[])
-{
- test_run(argc, argv, tests, SRCDIR "/t/fgoldi");
- return (0);
-}
-
-#endif
-