3 * Measure performance of various operations (Unix-specific)
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 ------------------------------------------------------*/
30 #define _FILE_OFFSET_BITS 64
42 #include <sys/types.h>
46 #ifdef HAVE_LINUX_PERF_EVENT_H
47 # include <linux/perf_event.h>
48 # include <asm/unistd.h>
51 #include <mLib/alloc.h>
52 #include <mLib/bits.h>
53 #include <mLib/dstr.h>
54 #include <mLib/mdwopt.h>
55 #include <mLib/quis.h>
56 #include <mLib/report.h>
68 #include "mpbarrett.h"
88 /*----- Options -----------------------------------------------------------*/
91 const char *name; /* Pre-configured named thing */
92 const char *opwhat; /* What to call operations */
93 unsigned fbits; /* Field size bits */
94 unsigned gbits; /* Group size bits */
95 unsigned n; /* Number of factors */
96 unsigned i; /* Number of intervals (or zero) */
97 unsigned k; /* Main loop batch size */
98 unsigned long sc; /* Scale factor */
99 double t; /* Time for each interval (secs) */
100 mp *e; /* Public exponent */
101 unsigned f; /* Flags */
102 #define OF_NOCHECK 1u /* Don't do group checking */
105 /*----- Job switch --------------------------------------------------------*/
107 /* --- Barrett exponentiation --- */
109 typedef struct bar_ctx {
115 static void *bar_init(opts *o)
117 bar_ctx *c = CREATE(bar_ctx);
124 if (dh_parse(&qd, &gp))
125 die(1, "bad prime group: %s", qd.e);
127 if (!o->fbits) o->fbits = 1024;
128 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
130 mpbarrett_create(&c->b, gp.p);
133 c->e = xmalloc(c->n * sizeof(group_expfactor));
134 for (i = 0; i < c->n; i++) {
135 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
136 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
142 static void bar_run(void *cc)
145 mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
149 static void barsim_run(void *cc)
152 mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
156 /* --- Montgomery exponentiation --- */
158 typedef struct mont_ctx {
164 static void *mont_init(opts *o)
166 mont_ctx *c = CREATE(mont_ctx);
173 if (dh_parse(&qd, &gp))
174 die(1, "bad prime group: %s", qd.e);
176 if (!o->fbits) o->fbits = 1024;
177 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
179 mpmont_create(&c->m, gp.p);
182 c->e = xmalloc(c->n * sizeof(mp_expfactor));
183 for (i = 0; i < c->n; i++) {
184 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
185 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
191 static void mont_run(void *cc)
194 mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
198 static void montsim_run(void *cc)
201 mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
205 /* --- Group exponentiation --- */
207 typedef struct gr_ctx {
213 static void *grp_init(opts *o)
215 gr_ctx *c = CREATE(gr_ctx);
223 if (dh_parse(&qd, &gp))
224 die(1, "bad prime group: %s", qd.e);
226 if (!o->fbits) o->fbits = 1024;
227 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
229 c->g = group_prime(&gp);
230 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
231 die(1, "bad group: %s", e);
234 c->e = xmalloc(c->n * sizeof(group_expfactor));
235 for (i = 0; i < c->n; i++) {
236 c->e[i].base = G_CREATE(c->g);
237 G_FROMINT(c->g, c->e[i].base,
238 mprand_range(MP_NEW, gp.p, &rand_global, 0));
239 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
245 static void *grec_init(opts *o)
247 gr_ctx *c = CREATE(gr_ctx);
254 die(1, "can't generate elliptic curves");
255 if ((e = ec_getinfo(&ei, o->name)) != 0)
256 die(1, "bad curve: %s", e);
257 c->g = group_ec(&ei);
258 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
259 die(1, "bad group: %s", e);
262 c->e = xmalloc(c->n * sizeof(group_expfactor));
263 for (i = 0; i < c->n; i++) {
264 c->e[i].base = G_CREATE(c->g);
265 ec_rand(ei.c, &p, &rand_global);
266 G_FROMEC(c->g, c->e[i].base, &p);
267 c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
273 static void gr_run(void *cc)
276 ge *x = G_CREATE(c->g);
277 G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
281 static void grsim_run(void *cc)
284 ge *x = G_CREATE(c->g);
285 G_MEXP(c->g, x, c->e, c->n);
291 typedef struct x25519_jobctx {
292 octet k[X25519_KEYSZ];
293 octet p[X25519_PUBSZ];
296 static void *x25519_jobinit(opts *o)
298 x25519_jobctx *c = CREATE(x25519_jobctx);
299 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
300 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
304 static void x25519_jobrun(void *cc)
305 { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); }
309 typedef struct x448_jobctx {
314 static void *x448_jobinit(opts *o)
316 x448_jobctx *c = CREATE(x448_jobctx);
317 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
318 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
322 static void x448_jobrun(void *cc)
323 { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); }
325 /* --- Ed25519 --- */
327 typedef struct ed25519_signctx {
328 octet k[ED25519_KEYSZ];
329 octet K[ED25519_PUBSZ];
333 typedef struct ed25519_vrfctx {
334 octet K[ED25519_PUBSZ];
336 octet sig[ED25519_SIGSZ];
339 static void *ed25519_signinit(opts *o)
341 ed25519_signctx *c = CREATE(ed25519_signctx);
343 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
344 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
345 ed25519_pubkey(c->K, c->k, sizeof(c->k));
349 static void ed25519_signrun(void *cc)
351 ed25519_signctx *c = cc;
352 octet sig[ED25519_SIGSZ];
354 ed25519_sign(sig, c->k, sizeof(c->k), c->K, c->m, sizeof(c->m));
357 static void *ed25519_vrfinit(opts *o)
359 octet k[ED25519_KEYSZ];
360 ed25519_vrfctx *c = CREATE(ed25519_vrfctx);
362 rand_get(RAND_GLOBAL, k, sizeof(k));
363 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
364 ed25519_pubkey(c->K, k, sizeof(k));
365 ed25519_sign(c->sig, k, sizeof(k), c->K, c->m, sizeof(c->m));
369 static void ed25519_vrfrun(void *cc)
371 ed25519_vrfctx *c = cc;
372 ed25519_verify(c->K, c->m, sizeof(c->m), c->sig);
377 typedef struct ed448_signctx {
378 octet k[ED448_KEYSZ];
379 octet K[ED448_PUBSZ];
383 typedef struct ed448_vrfctx {
384 octet K[ED448_PUBSZ];
386 octet sig[ED448_SIGSZ];
389 static void *ed448_signinit(opts *o)
391 ed448_signctx *c = CREATE(ed448_signctx);
393 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
394 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
395 ed448_pubkey(c->K, c->k, sizeof(c->k));
399 static void ed448_signrun(void *cc)
401 ed448_signctx *c = cc;
402 octet sig[ED448_SIGSZ];
404 ed448_sign(sig, c->k, sizeof(c->k), c->K, 0, 0, 0, c->m, sizeof(c->m));
407 static void *ed448_vrfinit(opts *o)
409 octet k[ED448_KEYSZ];
410 ed448_vrfctx *c = CREATE(ed448_vrfctx);
412 rand_get(RAND_GLOBAL, k, sizeof(k));
413 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
414 ed448_pubkey(c->K, k, sizeof(k));
415 ed448_sign(c->sig, k, sizeof(k), c->K, 0, 0, 0, c->m, sizeof(c->m));
419 static void ed448_vrfrun(void *cc)
421 ed448_vrfctx *c = cc;
422 ed448_verify(c->K, 0, 0, 0, c->m, sizeof(c->m), c->sig);
427 typedef struct rsapriv_ctx {
433 static void *rsapriv_init(opts *o)
435 rsapriv_ctx *c = CREATE(rsapriv_ctx);
437 if (!o->fbits) o->fbits = 1024;
438 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
439 rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
440 rsa_privcreate(&c->rpc, &c->rp, 0);
441 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
445 static void *rsaprivblind_init(opts *o)
447 rsapriv_ctx *c = CREATE(rsapriv_ctx);
449 if (!o->fbits) o->fbits = 1024;
450 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
451 rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
452 rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
453 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
457 static void rsapriv_run(void *cc)
460 mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
464 typedef struct rsapub_ctx {
470 static void *rsapub_init(opts *o)
472 rsapub_ctx *c = CREATE(rsapub_ctx);
475 if (!o->fbits) o->fbits = 1024;
476 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
477 rsa_gen_e(&rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
478 c->rp.n = MP_COPY(rp.n);
479 c->rp.e = MP_COPY(rp.e);
481 rsa_pubcreate(&c->rpc, &c->rp);
482 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
486 static void rsapub_run(void *cc)
489 mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
493 /* --- Symmetric encryption --- */
495 typedef struct ksched_ctx {
501 static void *ksched_init(opts *o)
503 ksched_ctx *c = CREATE(ksched_ctx);
505 die(1, "must specify encryption scheme name");
506 if ((c->c = gcipher_byname(o->name)) == 0)
507 die(1, "encryption scheme `%s' not known", o->name);
508 c->ksz = keysz(o->fbits/8, c->c->keysz);
509 if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8))
510 die(1, "bad key size %u for %s", o->fbits, o->name);
511 c->k = xmalloc(c->ksz);
512 rand_get(RAND_GLOBAL, c->k, c->ksz);
516 static void ksched_run(void *cc)
519 gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
523 typedef struct enc_ctx {
530 static void *enc_init(opts *o)
532 enc_ctx *c = CREATE(enc_ctx);
537 die(1, "must specify encryption scheme name");
538 if ((cc = gcipher_byname(o->name)) == 0)
539 die(1, "encryption scheme `%s' not known", o->name);
540 ksz = keysz(o->fbits/8, cc->keysz);
541 if (o->fbits%8 || (o->fbits && ksz != o->fbits/8))
542 die(1, "bad key size %u for %s", o->fbits, o->name);
544 rand_get(RAND_GLOBAL, k, ksz);
545 c->c = GC_INIT(cc, k, ksz);
547 c->sz = o->gbits ? o->gbits : 65536;
548 c->n = o->n ? o->n : 16;
549 o->opwhat = "byte"; o->sc = c->n*c->sz;
550 c->m = xmalloc(c->sz);
554 static void enc_run(void *cc)
558 for (i = 0; i < c->n; i++)
559 GC_ENCRYPT(c->c, c->m, c->m, c->sz);
562 /* --- Authenticated encryption --- */
564 typedef struct aeadsetup_ctx {
566 octet *k; size_t ksz;
567 octet *n; size_t nsz;
571 static void *aeadsetup_init(opts *o)
573 aeadsetup_ctx *c = CREATE(aeadsetup_ctx);
575 die(1, "must specify encryption scheme name");
576 if ((c->aec = gaead_byname(o->name)) == 0)
577 die(1, "aead scheme `%s' not known", o->name);
578 c->ksz = keysz(o->fbits/8, c->aec->keysz);
579 c->nsz = keysz_pad(o->gbits/8, c->aec->noncesz);
580 c->tsz = keysz(0, c->aec->tagsz);
581 if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8))
582 die(1, "bad key size %u for %s", o->fbits, o->name);
583 if (o->gbits%8 || (o->gbits && c->nsz != o->gbits/8))
584 die(1, "bad nonce size %u for %s", o->gbits, o->name);
585 c->k = xmalloc(c->ksz); rand_get(RAND_GLOBAL, c->k, c->ksz);
586 c->n = xmalloc(c->nsz); rand_get(RAND_GLOBAL, c->n, c->nsz);
590 static void aeadsetup_run(void *cc)
592 aeadsetup_ctx *c = cc;
593 gaead_key *k = GAEAD_KEY(c->aec, c->k, c->ksz);
594 gaead_enc *e = GAEAD_ENC(k, c->n, c->nsz, 0, 0, c->tsz);
595 GAEAD_DESTROY(e); GAEAD_DESTROY(k);
598 typedef struct aeadenc_ctx {
600 octet *n; size_t nsz;
601 octet *p, *q; size_t sz; size_t nn;
605 static void *aeadenc_init(opts *o)
607 aeadenc_ctx *c = CREATE(aeadenc_ctx);
610 octet *k; size_t ksz;
613 die(1, "must specify encryption scheme name");
614 if ((aec = gaead_byname(o->name)) == 0)
615 die(1, "aead scheme `%s' not known", o->name);
616 c->sz = o->gbits ? o->gbits : 65536;
617 c->nn = o->n ? o->n : 16;
618 ksz = keysz(o->fbits/8, aec->keysz);
619 c->nsz = keysz(0, aec->noncesz);
620 c->tsz = keysz(0, aec->tagsz);
621 if (o->fbits%8 || (o->fbits && ksz != o->fbits/8))
622 die(1, "bad key size %u for %s", o->fbits, o->name);
624 k = xmalloc(ksz); rand_get(RAND_GLOBAL, k, ksz);
625 c->n = xmalloc(c->nsz); rand_get(RAND_GLOBAL, c->n, c->nsz);
626 c->p = xmalloc(c->sz); c->q = xmalloc(c->sz + aec->bufsz);
628 key = GAEAD_KEY(aec, k, ksz);
629 c->enc = GAEAD_ENC(key, c->n, c->nsz, 0, 0, c->tsz);
630 GAEAD_DESTROY(key); xfree(k);
632 o->opwhat = "byte"; o->sc = c->nn*c->sz;
636 static void aeadaad_run(void *cc)
642 GAEAD_REINIT(c->enc, c->n, c->nsz, c->nn*c->sz, 0, c->tsz);
643 a = GAEAD_AAD(c->enc);
644 for (i = 0; i < c->nn; i++) GAEAD_HASH(a, c->p, c->sz);
648 static void aeadenc_run(void *cc)
654 GAEAD_REINIT(c->enc, c->n, c->nsz, 0, c->nn*c->sz, c->tsz);
655 for (i = 0; i < c->nn; i++) {
656 buf_init(&b, c->q, c->sz + c->enc->ops->c->bufsz);
657 GAEAD_ENCRYPT(c->enc, c->p, c->sz, &b);
661 /* --- Hashing --- */
663 typedef struct hash_ctx {
670 static void *hash_init(opts *o)
672 hash_ctx *c = CREATE(hash_ctx);
674 die(1, "must specify hash function name");
675 if ((c->h = ghash_byname(o->name)) == 0)
676 die(1, "hash function `%s' not known", o->name);
677 c->sz = o->gbits ? o->gbits : 65536;
678 c->n = o->n ? o->n : 16;
679 o->opwhat = "byte"; o->sc = c->n*c->sz;
680 c->m = xmalloc(c->sz);
684 static void hash_run(void *cc)
688 ghash *h = GH_INIT(c->h);
689 for (i = 0; i < c->n; i++)
690 GH_HASH(h, c->m, c->sz);
695 /* --- Poly1305 --- */
697 typedef struct poly1305_jobctx {
699 octet s[POLY1305_MASKSZ];
705 static void *poly1305_jobinit(opts *o)
707 octet k[POLY1305_KEYSZ];
708 poly1305_jobctx *c = CREATE(poly1305_jobctx);
709 rand_get(RAND_GLOBAL, k, sizeof(k));
710 poly1305_keyinit(&c->k, k, sizeof(k));
711 rand_get(RAND_GLOBAL, c->s, sizeof(c->s));
712 c->sz = o->gbits ? o->gbits : 65536;
713 c->n = o->n ? o->n : 16;
714 o->opwhat = "byte"; o->sc = c->n*c->sz;
715 c->m = xmalloc(c->sz);
719 static void poly1305_jobrun(void *cc)
721 poly1305_jobctx *c = cc;
723 octet t[POLY1305_TAGSZ];
725 poly1305_macinit(&ctx, &c->k, c->s);
726 for (i = 0; i < c->n; i++) poly1305_hash(&ctx, c->m, c->sz);
727 poly1305_done(&ctx, t);
730 /* --- Job table --- */
732 typedef struct jobops {
734 void *(*init)(opts *);
738 static const jobops jobtab[] = {
739 { "g-prime-exp", grp_init, gr_run },
740 { "g-ec-mul", grec_init, gr_run },
741 { "g-prime-exp-sim", grp_init, grsim_run },
742 { "g-ec-mul-sim", grec_init, grsim_run },
743 { "barrett-exp", bar_init, bar_run },
744 { "barrett-exp-sim", bar_init, barsim_run },
745 { "mont-exp", mont_init, mont_run },
746 { "mont-exp-sim", mont_init, montsim_run },
747 { "rsa-priv", rsapriv_init, rsapriv_run },
748 { "rsa-priv-blind", rsaprivblind_init, rsapriv_run },
749 { "rsa-pub", rsapub_init, rsapub_run },
750 { "x25519", x25519_jobinit, x25519_jobrun },
751 { "x448", x448_jobinit, x448_jobrun },
752 { "ed25519-sign", ed25519_signinit, ed25519_signrun },
753 { "ed25519-vrf", ed25519_vrfinit, ed25519_vrfrun },
754 { "ed448-sign", ed448_signinit, ed448_signrun },
755 { "ed448-vrf", ed448_vrfinit, ed448_vrfrun },
756 { "ksched", ksched_init, ksched_run },
757 { "enc", enc_init, enc_run },
758 { "aead-setup", aeadsetup_init, aeadsetup_run },
759 { "aead-aad", aeadenc_init, aeadaad_run },
760 { "aead-enc", aeadenc_init, aeadenc_run },
761 { "hash", hash_init, hash_run },
762 { "poly1305", poly1305_jobinit, poly1305_jobrun },
766 /*----- Cycle counting ----------------------------------------------------*/
768 typedef kludge64 cycles;
769 static int cyclecount_active_p = 0;
771 #if defined(__GNUC__) && (CPUFAM_X86 || CPUFAM_AMD64)
773 static void init_cyclecount(void) { cyclecount_active_p = 1; }
775 static cycles cyclecount(void)
780 __asm__("rdtsc" : "=a"(lo), "=d"(hi));
785 #elif defined(HAVE_LINUX_PERF_EVENT_H) && defined(HAVE_UINT64)
787 static int perf_fd = -1;
789 static void init_cyclecount(void)
791 struct perf_event_attr attr = { 0 };
793 attr.type = PERF_TYPE_HARDWARE;
794 attr.size = sizeof(attr);
795 attr.config = PERF_COUNT_HW_CPU_CYCLES;
797 attr.exclude_kernel = 1;
800 if ((perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0)) < 0)
801 moan("failed to open perf event: %s", strerror(errno));
803 cyclecount_active_p = 1;
806 static cycles cyclecount(void)
811 if (!cyclecount_active_p)
813 else if ((n = read(perf_fd, &cy.i, sizeof(cy.i))) != sizeof(cy.i)) {
814 if (n < 0) moan("error reading perf event: %s", strerror(errno));
815 else moan("unexpected short read from perf event");
816 cyclecount_active_p = 0; close(perf_fd); perf_fd = -1;
828 static void init_cyclecount(void) { cyclecount_active_p = 0; }
829 static cycles cyclecount(void) { kludge64 cy; SET64(cy, 0, 0); return (cy); }
833 /*----- Main code ---------------------------------------------------------*/
835 void version(FILE *fp)
837 pquis(fp, "$, Catacomb " VERSION "\n");
840 static void usage(FILE *fp)
842 pquis(fp, "Usage: $ [-options] job\n");
845 static void help(FILE *fp)
851 Various performance tests.\n\
855 -h, --help Show this help text.\n\
856 -v, --version Show program version number.\n\
857 -u, --usage Show terse usage message.\n\
858 -l, --list [ITEM...] List all the various names of things.\n\
860 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
861 -b, --field-bits Field size for g-prime and rsa;\n\
862 key bits for ksched, enc, aead-setup, aead-enc.\n\
863 -q, --no-check Don't check field/group for validity.\n\
864 -B, --group-bits Group size for g-prime; nonce bits for aead-setup;\n\
865 data size for enc, aead-aad, aead-enc, and hash.\n\
866 -n, --factors=COUNT Number of factors for {exp,mul}-sim;\n\
867 inner iters for enc, aead-aad, aead-enc, hash.\n\
868 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
869 -k, --batch=COUNT Number of operations to batch between timer checks.\n\
870 -t, --time=TIME Length of an interval in seconds. [1]\n\
876 listtab[i].name, listtab[i].name) \
878 jobtab[i].name, jobtab[i].name) \
879 LI("Elliptic curves", ec, \
880 ectab[i].name, ectab[i].name) \
881 LI("Diffie-Hellman groups", dh, \
882 ptab[i].name, ptab[i].name) \
883 LI("Encryption algorithms", cipher, \
884 gciphertab[i], gciphertab[i]->name) \
885 LI("Authenticated encryption schemes", aead, \
886 gaeadtab[i], gaeadtab[i]->name) \
887 LI("Hash functions", hash, \
888 ghashtab[i], ghashtab[i]->name)
890 MAKELISTTAB(listtab, LISTS)
892 static unsigned uarg(const char *what, const char *p)
897 u = strtoul(p, &q, 0);
898 if (*q || u > UINT_MAX || q == p || errno)
899 die(1, "bad %s `%s'", what, p);
903 static mp *mparg(const char *what, const char *p)
906 mp *x = mp_readstring(MP_NEW, p, &q, 0);
907 if (!x || *q) die(1, "bad %s `%s'", what, p);
911 static double farg(const char *what, const char *p)
917 if (*q || q == p || errno)
918 die(1, "bad %s `%s'", what, p);
922 int main(int argc, char *argv[])
927 struct timeval tv_next, tv_now;
928 double t, ttot, cy, cytot;
932 kludge64 cy0, cy1, cydiff;
937 o.t = 1; o.k = 1; o.sc = 1; o.opwhat = "op";
939 static const struct option opts[] = {
940 { "help", 0, 0, 'h' },
941 { "version", 0, 0, 'v' },
942 { "usage", 0, 0, 'u' },
943 { "list", 0, 0, 'l' },
944 { "name", OPTF_ARGREQ, 0, 'C' },
945 { "field-bits", OPTF_ARGREQ, 0, 'b' },
946 { "group-bits", OPTF_ARGREQ, 0, 'B' },
947 { "factors", OPTF_ARGREQ, 0, 'n' },
948 { "intervals", OPTF_ARGREQ, 0, 'i' },
949 { "batch", OPTF_ARGREQ, 0, 'k' },
950 { "public-exponent", OPTF_ARGREQ, 0, 'e' },
951 { "time", OPTF_ARGREQ, 0, 't' },
952 { "no-check", 0, 0, 'q' },
956 i = mdwopt(argc, argv, "hvulC:b:B:n:i:k:e:t:q", opts, 0, 0, 0);
959 case 'h': help(stdout); exit(0);
960 case 'v': version(stdout); exit(0);
961 case 'u': usage(stdout); exit(0);
962 case 'l': exit(displaylists(listtab, argv + optind));
963 case 'C': o.name = optarg; break;
964 case 'b': o.fbits = uarg("field bits", optarg); break;
965 case 'B': o.gbits = uarg("subgroup bits", optarg); break;
966 case 'n': o.n = uarg("factor count", optarg); break;
968 mp_drop(o.e); o.e = mparg("public exponent", optarg);
969 if (MP_CMP(o.e, <, MP_THREE) || MP_EVENP(o.e))
970 die(1, "invalid public exponent");
972 case 'i': o.i = uarg("interval count", optarg); break;
973 case 't': o.t = farg("interval length", optarg); break;
974 case 'k': o.k = uarg("batch size", optarg); break;
975 case 'q': o.f |= OF_NOCHECK; break;
976 default: usage(stderr); exit(1);
979 if (optind + 1 != argc) { usage(stderr); exit(1); }
981 for (j = jobtab; j->name; j++)
982 if (strcmp(j->name, argv[optind]) == 0) break;
983 if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
987 ttot = itot = 0; cytot = 0; init_cyclecount();
988 gettimeofday(&tv_now, 0);
990 tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
992 c0 = clock(); cy0 = cyclecount();
994 for (k = 0; k < o.k; k++) { j->run(p); }
996 gettimeofday(&tv_now, 0);
997 } while (TV_CMP(&tv_now, <, &tv_next));
998 cy1 = cyclecount(); c1 = clock();
999 t = (double)(c1 - c0)/CLOCKS_PER_SEC;
1000 itot += ii; ttot += t;
1001 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f",
1002 n, ii, ii/t, itot/ttot);
1003 if (cyclecount_active_p) {
1004 SUB64(cydiff, cy1, cy0); cy = LO64(cydiff) + ldexp(HI64(cydiff), 32);
1006 printf(" (cy/%s = %3f; avg cy/%s = %3f)",
1007 o.opwhat, cy/ii/o.sc, o.opwhat, cytot/itot/o.sc);
1012 } while (!o.i || n < o.i);
1017 /*----- That's all, folks -------------------------------------------------*/