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 <sys/syscall.h>
48 # include <linux/perf_event.h>
51 #include <mLib/alloc.h>
52 #include <mLib/bits.h>
53 #include <mLib/dstr.h>
54 #include <mLib/macros.h>
55 #include <mLib/mdwopt.h>
56 #include <mLib/quis.h>
57 #include <mLib/report.h>
69 #include "mpbarrett.h"
89 /*----- Options -----------------------------------------------------------*/
92 const char *name; /* Pre-configured named thing */
93 const char *opwhat; /* What to call operations */
94 unsigned fbits; /* Field size bits */
95 unsigned gbits; /* Group size bits */
96 unsigned n; /* Number of factors */
97 unsigned i; /* Number of intervals (or zero) */
98 unsigned k; /* Main loop batch size */
99 unsigned long sc; /* Scale factor */
100 double t; /* Time for each interval (secs) */
101 mp *e; /* Public exponent */
102 unsigned f; /* Flags */
103 #define OF_NOCHECK 1u /* Don't do group checking */
106 /*----- Job switch --------------------------------------------------------*/
108 /* --- Barrett exponentiation --- */
110 typedef struct bar_ctx {
116 static void *bar_init(opts *o)
118 bar_ctx *c = CREATE(bar_ctx);
125 if (dh_parse(&qd, &gp))
126 die(1, "bad prime group: %s", qd.e);
128 if (!o->fbits) o->fbits = 1024;
129 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
131 mpbarrett_create(&c->b, gp.p);
134 c->e = xmalloc(c->n * sizeof(group_expfactor));
135 for (i = 0; i < c->n; i++) {
136 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
137 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
143 static void bar_run(void *cc)
146 mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
150 static void barsim_run(void *cc)
153 mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
157 /* --- Montgomery exponentiation --- */
159 typedef struct mont_ctx {
165 static void *mont_init(opts *o)
167 mont_ctx *c = CREATE(mont_ctx);
174 if (dh_parse(&qd, &gp))
175 die(1, "bad prime group: %s", qd.e);
177 if (!o->fbits) o->fbits = 1024;
178 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
180 mpmont_create(&c->m, gp.p);
183 c->e = xmalloc(c->n * sizeof(mp_expfactor));
184 for (i = 0; i < c->n; i++) {
185 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
186 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
192 static void mont_run(void *cc)
195 mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
199 static void montsim_run(void *cc)
202 mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
206 /* --- Group exponentiation --- */
208 typedef struct gr_ctx {
214 static void *grp_init(opts *o)
216 gr_ctx *c = CREATE(gr_ctx);
224 if (dh_parse(&qd, &gp))
225 die(1, "bad prime group: %s", qd.e);
227 if (!o->fbits) o->fbits = 1024;
228 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
230 c->g = group_prime(&gp);
231 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
232 die(1, "bad group: %s", e);
235 c->e = xmalloc(c->n * sizeof(group_expfactor));
236 for (i = 0; i < c->n; i++) {
237 c->e[i].base = G_CREATE(c->g);
238 G_FROMINT(c->g, c->e[i].base,
239 mprand_range(MP_NEW, gp.p, &rand_global, 0));
240 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
246 static void *grec_init(opts *o)
248 gr_ctx *c = CREATE(gr_ctx);
255 die(1, "can't generate elliptic curves");
256 if ((e = ec_getinfo(&ei, o->name)) != 0)
257 die(1, "bad curve: %s", e);
258 c->g = group_ec(&ei);
259 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
260 die(1, "bad group: %s", e);
263 c->e = xmalloc(c->n * sizeof(group_expfactor));
264 for (i = 0; i < c->n; i++) {
265 c->e[i].base = G_CREATE(c->g);
266 ec_rand(ei.c, &p, &rand_global);
267 G_FROMEC(c->g, c->e[i].base, &p);
268 c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
274 static void gr_run(void *cc)
277 ge *x = G_CREATE(c->g);
278 G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
282 static void grsim_run(void *cc)
285 ge *x = G_CREATE(c->g);
286 G_MEXP(c->g, x, c->e, c->n);
292 typedef struct x25519_jobctx {
293 octet k[X25519_KEYSZ];
294 octet p[X25519_PUBSZ];
297 static void *x25519_jobinit(opts *o)
299 x25519_jobctx *c = CREATE(x25519_jobctx);
300 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
301 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
305 static void x25519_jobrun(void *cc)
306 { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); }
310 typedef struct x448_jobctx {
315 static void *x448_jobinit(opts *o)
317 x448_jobctx *c = CREATE(x448_jobctx);
318 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
319 rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
323 static void x448_jobrun(void *cc)
324 { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); }
326 /* --- Ed25519 --- */
328 typedef struct ed25519_signctx {
329 octet k[ED25519_KEYSZ];
330 octet K[ED25519_PUBSZ];
334 typedef struct ed25519_vrfctx {
335 octet K[ED25519_PUBSZ];
337 octet sig[ED25519_SIGSZ];
340 static void *ed25519_signinit(opts *o)
342 ed25519_signctx *c = CREATE(ed25519_signctx);
344 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
345 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
346 ed25519_pubkey(c->K, c->k, sizeof(c->k));
350 static void ed25519_signrun(void *cc)
352 ed25519_signctx *c = cc;
353 octet sig[ED25519_SIGSZ];
355 ed25519_sign(sig, c->k, sizeof(c->k), c->K, c->m, sizeof(c->m));
358 static void *ed25519_vrfinit(opts *o)
360 octet k[ED25519_KEYSZ];
361 ed25519_vrfctx *c = CREATE(ed25519_vrfctx);
363 rand_get(RAND_GLOBAL, k, sizeof(k));
364 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
365 ed25519_pubkey(c->K, k, sizeof(k));
366 ed25519_sign(c->sig, k, sizeof(k), c->K, c->m, sizeof(c->m));
370 static void ed25519_vrfrun(void *cc)
372 ed25519_vrfctx *c = cc;
373 ed25519_verify(c->K, c->m, sizeof(c->m), c->sig);
378 typedef struct ed448_signctx {
379 octet k[ED448_KEYSZ];
380 octet K[ED448_PUBSZ];
384 typedef struct ed448_vrfctx {
385 octet K[ED448_PUBSZ];
387 octet sig[ED448_SIGSZ];
390 static void *ed448_signinit(opts *o)
392 ed448_signctx *c = CREATE(ed448_signctx);
394 rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
395 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
396 ed448_pubkey(c->K, c->k, sizeof(c->k));
400 static void ed448_signrun(void *cc)
402 ed448_signctx *c = cc;
403 octet sig[ED448_SIGSZ];
405 ed448_sign(sig, c->k, sizeof(c->k), c->K, 0, 0, 0, c->m, sizeof(c->m));
408 static void *ed448_vrfinit(opts *o)
410 octet k[ED448_KEYSZ];
411 ed448_vrfctx *c = CREATE(ed448_vrfctx);
413 rand_get(RAND_GLOBAL, k, sizeof(k));
414 rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
415 ed448_pubkey(c->K, k, sizeof(k));
416 ed448_sign(c->sig, k, sizeof(k), c->K, 0, 0, 0, c->m, sizeof(c->m));
420 static void ed448_vrfrun(void *cc)
422 ed448_vrfctx *c = cc;
423 ed448_verify(c->K, 0, 0, 0, c->m, sizeof(c->m), c->sig);
428 typedef struct rsapriv_ctx {
434 static void *rsapriv_init(opts *o)
436 rsapriv_ctx *c = CREATE(rsapriv_ctx);
438 if (!o->fbits) o->fbits = 1024;
439 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
440 rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
441 rsa_privcreate(&c->rpc, &c->rp, 0);
442 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
446 static void *rsaprivblind_init(opts *o)
448 rsapriv_ctx *c = CREATE(rsapriv_ctx);
450 if (!o->fbits) o->fbits = 1024;
451 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
452 rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
453 rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
454 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
458 static void rsapriv_run(void *cc)
461 mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
465 typedef struct rsapub_ctx {
471 static void *rsapub_init(opts *o)
473 rsapub_ctx *c = CREATE(rsapub_ctx);
476 if (!o->fbits) o->fbits = 1024;
477 if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
478 rsa_gen_e(&rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
479 c->rp.n = MP_COPY(rp.n);
480 c->rp.e = MP_COPY(rp.e);
482 rsa_pubcreate(&c->rpc, &c->rp);
483 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
487 static void rsapub_run(void *cc)
490 mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
494 /* --- Symmetric encryption --- */
496 typedef struct ksched_ctx {
502 static void *ksched_init(opts *o)
504 ksched_ctx *c = CREATE(ksched_ctx);
506 die(1, "must specify encryption scheme name");
507 if ((c->c = gcipher_byname(o->name)) == 0)
508 die(1, "encryption scheme `%s' not known", o->name);
509 c->ksz = keysz(o->fbits/8, c->c->keysz);
510 if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8))
511 die(1, "bad key size %u for %s", o->fbits, o->name);
512 c->k = xmalloc(c->ksz);
513 rand_get(RAND_GLOBAL, c->k, c->ksz);
517 static void ksched_run(void *cc)
520 gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
524 typedef struct enc_ctx {
531 static void *enc_init(opts *o)
533 enc_ctx *c = CREATE(enc_ctx);
538 die(1, "must specify encryption scheme name");
539 if ((cc = gcipher_byname(o->name)) == 0)
540 die(1, "encryption scheme `%s' not known", o->name);
541 ksz = keysz(o->fbits/8, cc->keysz);
542 if (o->fbits%8 || (o->fbits && ksz != o->fbits/8))
543 die(1, "bad key size %u for %s", o->fbits, o->name);
545 rand_get(RAND_GLOBAL, k, ksz);
546 c->c = GC_INIT(cc, k, ksz);
548 c->sz = o->gbits ? o->gbits : 65536;
549 c->n = o->n ? o->n : 16;
550 o->opwhat = "byte"; o->sc = c->n*c->sz;
551 c->m = xmalloc(c->sz);
555 static void enc_run(void *cc)
559 for (i = 0; i < c->n; i++)
560 GC_ENCRYPT(c->c, c->m, c->m, c->sz);
563 /* --- Authenticated encryption --- */
565 typedef struct aeadsetup_ctx {
567 octet *k; size_t ksz;
568 octet *n; size_t nsz;
572 static void *aeadsetup_init(opts *o)
574 aeadsetup_ctx *c = CREATE(aeadsetup_ctx);
576 die(1, "must specify encryption scheme name");
577 if ((c->aec = gaead_byname(o->name)) == 0)
578 die(1, "aead scheme `%s' not known", o->name);
579 c->ksz = keysz(o->fbits/8, c->aec->keysz);
580 c->nsz = keysz_pad(o->gbits/8, c->aec->noncesz);
581 c->tsz = keysz(0, c->aec->tagsz);
582 if (o->fbits%8 || (o->fbits && c->ksz != o->fbits/8))
583 die(1, "bad key size %u for %s", o->fbits, o->name);
584 if (o->gbits%8 || (o->gbits && c->nsz != o->gbits/8))
585 die(1, "bad nonce size %u for %s", o->gbits, o->name);
586 c->k = xmalloc(c->ksz); rand_get(RAND_GLOBAL, c->k, c->ksz);
587 c->n = xmalloc(c->nsz); rand_get(RAND_GLOBAL, c->n, c->nsz);
591 static void aeadsetup_run(void *cc)
593 aeadsetup_ctx *c = cc;
594 gaead_key *k = GAEAD_KEY(c->aec, c->k, c->ksz);
595 gaead_enc *e = GAEAD_ENC(k, c->n, c->nsz, 0, 0, c->tsz);
596 GAEAD_DESTROY(e); GAEAD_DESTROY(k);
599 typedef struct aeadenc_ctx {
601 octet *n; size_t nsz;
602 octet *p, *q; size_t sz; size_t nn;
606 static void *aeadenc_init(opts *o)
608 aeadenc_ctx *c = CREATE(aeadenc_ctx);
611 octet *k; size_t ksz;
614 die(1, "must specify encryption scheme name");
615 if ((aec = gaead_byname(o->name)) == 0)
616 die(1, "aead scheme `%s' not known", o->name);
617 c->sz = o->gbits ? o->gbits : 65536;
618 c->nn = o->n ? o->n : 16;
619 ksz = keysz(o->fbits/8, aec->keysz);
620 c->nsz = keysz(0, aec->noncesz);
621 c->tsz = keysz(0, aec->tagsz);
622 if (o->fbits%8 || (o->fbits && ksz != o->fbits/8))
623 die(1, "bad key size %u for %s", o->fbits, o->name);
625 k = xmalloc(ksz); rand_get(RAND_GLOBAL, k, ksz);
626 c->n = xmalloc(c->nsz); rand_get(RAND_GLOBAL, c->n, c->nsz);
627 c->p = xmalloc(c->sz); c->q = xmalloc(c->sz + aec->bufsz);
629 key = GAEAD_KEY(aec, k, ksz);
630 c->enc = GAEAD_ENC(key, c->n, c->nsz, 0, 0, c->tsz);
631 GAEAD_DESTROY(key); xfree(k);
633 o->opwhat = "byte"; o->sc = c->nn*c->sz;
637 static void aeadaad_run(void *cc)
643 GAEAD_REINIT(c->enc, c->n, c->nsz, c->nn*c->sz, 0, c->tsz);
644 a = GAEAD_AAD(c->enc);
645 for (i = 0; i < c->nn; i++) GAEAD_HASH(a, c->p, c->sz);
649 static void aeadenc_run(void *cc)
655 GAEAD_REINIT(c->enc, c->n, c->nsz, 0, c->nn*c->sz, c->tsz);
656 for (i = 0; i < c->nn; i++) {
657 buf_init(&b, c->q, c->sz + c->enc->ops->c->bufsz);
658 GAEAD_ENCRYPT(c->enc, c->p, c->sz, &b);
662 /* --- Hashing --- */
664 typedef struct hash_ctx {
671 static void *hash_init(opts *o)
673 hash_ctx *c = CREATE(hash_ctx);
675 die(1, "must specify hash function name");
676 if ((c->h = ghash_byname(o->name)) == 0)
677 die(1, "hash function `%s' not known", o->name);
678 c->sz = o->gbits ? o->gbits : 65536;
679 c->n = o->n ? o->n : 16;
680 o->opwhat = "byte"; o->sc = c->n*c->sz;
681 c->m = xmalloc(c->sz);
685 static void hash_run(void *cc)
689 ghash *h = GH_INIT(c->h);
690 for (i = 0; i < c->n; i++)
691 GH_HASH(h, c->m, c->sz);
696 /* --- Poly1305 --- */
698 typedef struct poly1305_jobctx {
700 octet s[POLY1305_MASKSZ];
706 static void *poly1305_jobinit(opts *o)
708 octet k[POLY1305_KEYSZ];
709 poly1305_jobctx *c = CREATE(poly1305_jobctx);
710 rand_get(RAND_GLOBAL, k, sizeof(k));
711 poly1305_keyinit(&c->k, k, sizeof(k));
712 rand_get(RAND_GLOBAL, c->s, sizeof(c->s));
713 c->sz = o->gbits ? o->gbits : 65536;
714 c->n = o->n ? o->n : 16;
715 o->opwhat = "byte"; o->sc = c->n*c->sz;
716 c->m = xmalloc(c->sz);
720 static void poly1305_jobrun(void *cc)
722 poly1305_jobctx *c = cc;
724 octet t[POLY1305_TAGSZ];
726 poly1305_macinit(&ctx, &c->k, c->s);
727 for (i = 0; i < c->n; i++) poly1305_hash(&ctx, c->m, c->sz);
728 poly1305_done(&ctx, t);
731 /* --- Job table --- */
733 typedef struct jobops {
735 void *(*init)(opts *);
739 static const jobops jobtab[] = {
740 { "g-prime-exp", grp_init, gr_run },
741 { "g-ec-mul", grec_init, gr_run },
742 { "g-prime-exp-sim", grp_init, grsim_run },
743 { "g-ec-mul-sim", grec_init, grsim_run },
744 { "barrett-exp", bar_init, bar_run },
745 { "barrett-exp-sim", bar_init, barsim_run },
746 { "mont-exp", mont_init, mont_run },
747 { "mont-exp-sim", mont_init, montsim_run },
748 { "rsa-priv", rsapriv_init, rsapriv_run },
749 { "rsa-priv-blind", rsaprivblind_init, rsapriv_run },
750 { "rsa-pub", rsapub_init, rsapub_run },
751 { "x25519", x25519_jobinit, x25519_jobrun },
752 { "x448", x448_jobinit, x448_jobrun },
753 { "ed25519-sign", ed25519_signinit, ed25519_signrun },
754 { "ed25519-vrf", ed25519_vrfinit, ed25519_vrfrun },
755 { "ed448-sign", ed448_signinit, ed448_signrun },
756 { "ed448-vrf", ed448_vrfinit, ed448_vrfrun },
757 { "ksched", ksched_init, ksched_run },
758 { "enc", enc_init, enc_run },
759 { "aead-setup", aeadsetup_init, aeadsetup_run },
760 { "aead-aad", aeadenc_init, aeadaad_run },
761 { "aead-enc", aeadenc_init, aeadenc_run },
762 { "hash", hash_init, hash_run },
763 { "poly1305", poly1305_jobinit, poly1305_jobrun },
767 /*----- Cycle counting ----------------------------------------------------*/
769 typedef kludge64 cycles;
770 static int cyclecount_active_p = 0;
772 #if defined(__GNUC__) && (CPUFAM_X86 || CPUFAM_AMD64)
774 static void init_cyclecount(void) { cyclecount_active_p = 1; }
776 static cycles cyclecount(void)
781 __asm__("rdtsc" : "=a"(lo), "=d"(hi));
786 #elif defined(HAVE_LINUX_PERF_EVENT_H) && defined(HAVE_UINT64)
788 static int perf_fd = -1;
790 static void init_cyclecount(void)
792 struct perf_event_attr attr = { 0 };
794 attr.type = PERF_TYPE_HARDWARE;
795 attr.size = sizeof(attr);
796 attr.config = PERF_COUNT_HW_CPU_CYCLES;
798 attr.exclude_kernel = 1;
801 if ((perf_fd = syscall(SYS_perf_event_open, &attr, 0, -1, -1, 0)) < 0)
802 moan("failed to open perf event: %s", strerror(errno));
804 cyclecount_active_p = 1;
807 static cycles cyclecount(void)
812 if (!cyclecount_active_p)
814 else if ((n = read(perf_fd, &cy.i, sizeof(cy.i))) != sizeof(cy.i)) {
815 if (n < 0) moan("error reading perf event: %s", strerror(errno));
816 else moan("unexpected short read from perf event");
817 cyclecount_active_p = 0; close(perf_fd); perf_fd = -1;
829 static void init_cyclecount(void) { cyclecount_active_p = 0; }
830 static cycles cyclecount(void) { kludge64 cy; SET64(cy, 0, 0); return (cy); }
834 /*----- Main code ---------------------------------------------------------*/
836 void version(FILE *fp)
838 pquis(fp, "$, Catacomb " VERSION "\n");
841 static void usage(FILE *fp)
843 pquis(fp, "Usage: $ [-options] job\n");
846 static void help(FILE *fp)
852 Various performance tests.\n\
856 -h, --help Show this help text.\n\
857 -v, --version Show program version number.\n\
858 -u, --usage Show terse usage message.\n\
859 -l, --list [ITEM...] List all the various names of things.\n\
861 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
862 -b, --field-bits Field size for g-prime and rsa;\n\
863 key bits for ksched, enc, aead-setup, aead-enc.\n\
864 -q, --no-check Don't check field/group for validity.\n\
865 -B, --group-bits Group size for g-prime; nonce bits for aead-setup;\n\
866 data size for enc, aead-aad, aead-enc, and hash.\n\
867 -n, --factors=COUNT Number of factors for {exp,mul}-sim;\n\
868 inner iters for enc, aead-aad, aead-enc, hash.\n\
869 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
870 -k, --batch=COUNT Number of operations to batch between timer checks.\n\
871 -t, --time=TIME Length of an interval in seconds. [1]\n\
877 listtab[i].name, listtab[i].name) \
879 jobtab[i].name, jobtab[i].name) \
880 LI("Elliptic curves", ec, \
881 ectab[i].name, ectab[i].name) \
882 LI("Diffie-Hellman groups", dh, \
883 ptab[i].name, ptab[i].name) \
884 LI("Encryption algorithms", cipher, \
885 gciphertab[i], gciphertab[i]->name) \
886 LI("Authenticated encryption schemes", aead, \
887 gaeadtab[i], gaeadtab[i]->name) \
888 LI("Hash functions", hash, \
889 ghashtab[i], ghashtab[i]->name)
891 MAKELISTTAB(listtab, LISTS)
893 static unsigned uarg(const char *what, const char *p)
898 u = strtoul(p, &q, 0);
899 if (*q || u > UINT_MAX || q == p || errno)
900 die(1, "bad %s `%s'", what, p);
904 static mp *mparg(const char *what, const char *p)
907 mp *x = mp_readstring(MP_NEW, p, &q, 0);
908 if (!x || *q) die(1, "bad %s `%s'", what, p);
912 static double farg(const char *what, const char *p)
918 if (*q || q == p || errno)
919 die(1, "bad %s `%s'", what, p);
923 int main(int argc, char *argv[])
928 struct timeval tv_next, tv_now;
929 double t, ttot, cy, cytot;
933 kludge64 cy0, cy1, cydiff;
938 o.t = 1; o.k = 1; o.sc = 1; o.opwhat = "op";
940 static const struct option opts[] = {
941 { "help", 0, 0, 'h' },
942 { "version", 0, 0, 'v' },
943 { "usage", 0, 0, 'u' },
944 { "list", 0, 0, 'l' },
945 { "name", OPTF_ARGREQ, 0, 'C' },
946 { "field-bits", OPTF_ARGREQ, 0, 'b' },
947 { "group-bits", OPTF_ARGREQ, 0, 'B' },
948 { "factors", OPTF_ARGREQ, 0, 'n' },
949 { "intervals", OPTF_ARGREQ, 0, 'i' },
950 { "batch", OPTF_ARGREQ, 0, 'k' },
951 { "public-exponent", OPTF_ARGREQ, 0, 'e' },
952 { "time", OPTF_ARGREQ, 0, 't' },
953 { "no-check", 0, 0, 'q' },
957 i = mdwopt(argc, argv, "hvulC:b:B:n:i:k:e:t:q", opts, 0, 0, 0);
960 case 'h': help(stdout); exit(0);
961 case 'v': version(stdout); exit(0);
962 case 'u': usage(stdout); exit(0);
963 case 'l': exit(displaylists(listtab, argv + optind));
964 case 'C': o.name = optarg; break;
965 case 'b': o.fbits = uarg("field bits", optarg); break;
966 case 'B': o.gbits = uarg("subgroup bits", optarg); break;
967 case 'n': o.n = uarg("factor count", optarg); break;
969 mp_drop(o.e); o.e = mparg("public exponent", optarg);
970 if (MP_CMP(o.e, <, MP_THREE) || MP_EVENP(o.e))
971 die(1, "invalid public exponent");
973 case 'i': o.i = uarg("interval count", optarg); break;
974 case 't': o.t = farg("interval length", optarg); break;
975 case 'k': o.k = uarg("batch size", optarg); break;
976 case 'q': o.f |= OF_NOCHECK; break;
977 default: usage(stderr); exit(1);
980 if (optind + 1 != argc) { usage(stderr); exit(1); }
982 for (j = jobtab; j->name; j++)
983 if (STRCMP(j->name, ==, argv[optind])) break;
984 if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
988 ttot = itot = 0; cytot = 0; init_cyclecount();
989 gettimeofday(&tv_now, 0);
991 tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
993 c0 = clock(); cy0 = cyclecount();
995 for (k = 0; k < o.k; k++) { j->run(p); }
997 gettimeofday(&tv_now, 0);
998 } while (TV_CMP(&tv_now, <, &tv_next));
999 cy1 = cyclecount(); c1 = clock();
1000 t = (double)(c1 - c0)/CLOCKS_PER_SEC;
1001 itot += ii; ttot += t;
1002 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f",
1003 n, ii, ii/t, itot/ttot);
1004 if (cyclecount_active_p) {
1005 SUB64(cydiff, cy1, cy0); cy = LO64(cydiff) + ldexp(HI64(cydiff), 32);
1007 printf(" (cy/%s = %3f; avg cy/%s = %3f)",
1008 o.opwhat, cy/ii/o.sc, o.opwhat, cytot/itot/o.sc);
1013 } while (!o.i || n < o.i);
1018 /*----- That's all, folks -------------------------------------------------*/