chiark / gitweb /
progs/*.1: Mention the default hash for `ed25519',
[catacomb] / progs / perftest.c
1 /* -*-c-*-
2  *
3  * Measure performance of various operations (Unix-specific)
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #define _FILE_OFFSET_BITS 64
31
32 #include "config.h"
33
34 #include <errno.h>
35 #include <limits.h>
36 #include <math.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <time.h>
41
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45
46 #include <mLib/alloc.h>
47 #include <mLib/dstr.h>
48 #include <mLib/mdwopt.h>
49 #include <mLib/quis.h>
50 #include <mLib/report.h>
51 #include <mLib/sub.h>
52 #include <mLib/tv.h>
53
54 #include "rand.h"
55 #include "mp.h"
56 #include "mprand.h"
57 #include "fibrand.h"
58 #include "rsa.h"
59 #include "mpint.h"
60 #include "mptext.h"
61 #include "mpmont.h"
62 #include "mpbarrett.h"
63 #include "dh.h"
64 #include "pgen.h"
65 #include "ec.h"
66 #include "group.h"
67 #include "x25519.h"
68 #include "x448.h"
69 #include "ed25519.h"
70
71 #include "cc.h"
72 #include "gcipher.h"
73 #include "ghash.h"
74 #include "gmac.h"
75 #include "poly1305.h"
76
77 #include "ectab.h"
78 #include "ptab.h"
79
80 /*----- Options -----------------------------------------------------------*/
81
82 typedef struct opts {
83   const char *name;                     /* Pre-configured named thing */
84   unsigned fbits;                       /* Field size bits */
85   unsigned gbits;                       /* Group size bits */
86   unsigned n;                           /* Number of factors */
87   unsigned i;                           /* Number of intervals (or zero) */
88   double t;                             /* Time for each interval (secs) */
89   mp *e;                                /* Public exponent */
90   unsigned f;                           /* Flags */
91 #define OF_NOCHECK 1u                   /*   Don't do group checking */
92 } opts;
93
94 /*----- Job switch --------------------------------------------------------*/
95
96 /* --- Barrett exponentiation --- */
97
98 typedef struct bar_ctx {
99   size_t n;
100   mpbarrett b;
101   mp_expfactor *e;
102 } bar_ctx;
103
104 static void *bar_init(opts *o)
105 {
106   bar_ctx *c = CREATE(bar_ctx);
107   gprime_param gp;
108   qd_parse qd;
109   size_t i;
110
111   if (o->name) {
112     qd.p = o->name;
113     if (dh_parse(&qd, &gp))
114       die(1, "bad prime group: %s", qd.e);
115   } else {
116     if (!o->fbits) o->fbits = 1024;
117     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
118   }
119   mpbarrett_create(&c->b, gp.p);
120   if (!o->n) o->n = 1;
121   c->n = o->n;
122   c->e = xmalloc(c->n * sizeof(group_expfactor));
123   for (i = 0; i < c->n; i++) {
124     c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
125     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
126   }
127   dh_paramfree(&gp);
128   return (c);
129 }
130
131 static void bar_run(void *cc)
132 {
133   bar_ctx *c = cc;
134   mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
135   MP_DROP(d);
136 }
137
138 static void barsim_run(void *cc)
139 {
140   bar_ctx *c = cc;
141   mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
142   MP_DROP(d);
143 }
144
145 /* --- Montgomery exponentiation --- */
146
147 typedef struct mont_ctx {
148   size_t n;
149   mpmont m;
150   mp_expfactor *e;
151 } mont_ctx;
152
153 static void *mont_init(opts *o)
154 {
155   mont_ctx *c = CREATE(mont_ctx);
156   gprime_param gp;
157   qd_parse qd;
158   size_t i;
159
160   if (o->name) {
161     qd.p = o->name;
162     if (dh_parse(&qd, &gp))
163       die(1, "bad prime group: %s", qd.e);
164   } else {
165     if (!o->fbits) o->fbits = 1024;
166     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
167   }
168   mpmont_create(&c->m, gp.p);
169   if (!o->n) o->n = 1;
170   c->n = o->n;
171   c->e = xmalloc(c->n * sizeof(mp_expfactor));
172   for (i = 0; i < c->n; i++) {
173     c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
174     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
175   }
176   dh_paramfree(&gp);
177   return (c);
178 }
179
180 static void mont_run(void *cc)
181 {
182   mont_ctx *c = cc;
183   mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
184   MP_DROP(d);
185 }
186
187 static void montsim_run(void *cc)
188 {
189   mont_ctx *c = cc;
190   mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
191   MP_DROP(d);
192 }
193
194 /* --- Group exponentiation --- */
195
196 typedef struct gr_ctx {
197   size_t n;
198   group *g;
199   group_expfactor *e;
200 } gr_ctx;
201
202 static void *grp_init(opts *o)
203 {
204   gr_ctx *c = CREATE(gr_ctx);
205   const char *e;
206   gprime_param gp;
207   qd_parse qd;
208   size_t i;
209
210   if (o->name) {
211     qd.p = o->name;
212     if (dh_parse(&qd, &gp))
213       die(1, "bad prime group: %s", qd.e);
214   } else {
215     if (!o->fbits) o->fbits = 1024;
216     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
217   }
218   c->g = group_prime(&gp);
219   if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
220     die(1, "bad group: %s", e);
221   if (!o->n) o->n = 1;
222   c->n = o->n;
223   c->e = xmalloc(c->n * sizeof(group_expfactor));
224   for (i = 0; i < c->n; i++) {
225     c->e[i].base = G_CREATE(c->g);
226     G_FROMINT(c->g, c->e[i].base,
227               mprand_range(MP_NEW, gp.p, &rand_global, 0));
228     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
229   }
230   dh_paramfree(&gp);
231   return (c);
232 }
233
234 static void *grec_init(opts *o)
235 {
236   gr_ctx *c = CREATE(gr_ctx);
237   const char *e;
238   ec_info ei;
239   ec p = EC_INIT;
240   size_t i;
241
242   if (!o->name)
243     die(1, "can't generate elliptic curves");
244   if ((e = ec_getinfo(&ei, o->name)) != 0)
245     die(1, "bad curve: %s", e);
246   c->g = group_ec(&ei);
247   if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
248     die(1, "bad group: %s", e);
249   if (!o->n) o->n = 1;
250   c->n = o->n;
251   c->e = xmalloc(c->n * sizeof(group_expfactor));
252   for (i = 0; i < c->n; i++) {
253     c->e[i].base = G_CREATE(c->g);
254     ec_rand(ei.c, &p, &rand_global);
255     G_FROMEC(c->g, c->e[i].base, &p);
256     c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
257   }
258   EC_DESTROY(&p);
259   return (c);
260 }
261
262 static void gr_run(void *cc)
263 {
264   gr_ctx *c = cc;
265   ge *x = G_CREATE(c->g);
266   G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
267   G_DESTROY(c->g, x);
268 }
269
270 static void grsim_run(void *cc)
271 {
272   gr_ctx *c = cc;
273   ge *x = G_CREATE(c->g);
274   G_MEXP(c->g, x, c->e, c->n);
275   G_DESTROY(c->g, x);
276 }
277
278 /* --- x25519 --- */
279
280 typedef struct x25519_jobctx {
281   octet k[X25519_KEYSZ];
282   octet p[X25519_PUBSZ];
283 } x25519_jobctx;
284
285 static void *x25519_jobinit(opts *o)
286 {
287   x25519_jobctx *c = CREATE(x25519_jobctx);
288   rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
289   rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
290   return (c);
291 }
292
293 static void x25519_jobrun(void *cc)
294   { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); }
295
296 /* --- x448 --- */
297
298 typedef struct x448_jobctx {
299   octet k[X448_KEYSZ];
300   octet p[X448_PUBSZ];
301 } x448_jobctx;
302
303 static void *x448_jobinit(opts *o)
304 {
305   x448_jobctx *c = CREATE(x448_jobctx);
306   rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
307   rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
308   return (c);
309 }
310
311 static void x448_jobrun(void *cc)
312   { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); }
313
314 /* --- Ed25519 --- */
315
316 typedef struct ed25519_signctx {
317   octet k[ED25519_KEYSZ];
318   octet K[ED25519_PUBSZ];
319   octet m[64];
320 } ed25519_signctx;
321
322 typedef struct ed25519_vrfctx {
323   octet K[ED25519_PUBSZ];
324   octet m[64];
325   octet sig[ED25519_SIGSZ];
326 } ed25519_vrfctx;
327
328 static void *ed25519_signinit(opts *o)
329 {
330   ed25519_signctx *c = CREATE(ed25519_signctx);
331
332   rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
333   rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
334   ed25519_pubkey(c->K, c->k, sizeof(c->k));
335   return (c);
336 }
337
338 static void ed25519_signrun(void *cc)
339 {
340   ed25519_signctx *c = cc;
341   octet sig[ED25519_SIGSZ];
342
343   ed25519_sign(sig, c->k, sizeof(c->k), c->K, c->m, sizeof(c->m));
344 }
345
346 static void *ed25519_vrfinit(opts *o)
347 {
348   octet k[ED25519_KEYSZ];
349   ed25519_vrfctx *c = CREATE(ed25519_vrfctx);
350
351   rand_get(RAND_GLOBAL, k, sizeof(k));
352   rand_get(RAND_GLOBAL, c->m, sizeof(c->m));
353   ed25519_pubkey(c->K, k, sizeof(k));
354   ed25519_sign(c->sig, k, sizeof(k), c->K, c->m, sizeof(c->m));
355   return (c);
356 }
357
358 static void ed25519_vrfrun(void *cc)
359 {
360   ed25519_vrfctx *c = cc;
361   ed25519_verify(c->K, c->m, sizeof(c->m), c->sig);
362 }
363
364 /* --- RSA --- */
365
366 typedef struct rsapriv_ctx {
367   rsa_priv rp;
368   rsa_privctx rpc;
369   mp *m;
370 } rsapriv_ctx;
371
372 static void *rsapriv_init(opts *o)
373 {
374   rsapriv_ctx *c = CREATE(rsapriv_ctx);
375
376   if (!o->fbits) o->fbits = 1024;
377   if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
378   rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
379   rsa_privcreate(&c->rpc, &c->rp, 0);
380   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
381   return (c);
382 }
383
384 static void *rsaprivblind_init(opts *o)
385 {
386   rsapriv_ctx *c = CREATE(rsapriv_ctx);
387
388   if (!o->fbits) o->fbits = 1024;
389   if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
390   rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
391   rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
392   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
393   return (c);
394 }
395
396 static void rsapriv_run(void *cc)
397 {
398   rsapriv_ctx *c = cc;
399   mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
400   MP_DROP(d);
401 }
402
403 typedef struct rsapub_ctx {
404   rsa_pub rp;
405   rsa_pubctx rpc;
406   mp *m;
407 } rsapub_ctx;
408
409 static void *rsapub_init(opts *o)
410 {
411   rsapub_ctx *c = CREATE(rsapub_ctx);
412   rsa_priv rp;
413
414   if (!o->fbits) o->fbits = 1024;
415   if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
416   rsa_gen_e(&rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
417   c->rp.n = MP_COPY(rp.n);
418   c->rp.e = MP_COPY(rp.e);
419   rsa_privfree(&rp);
420   rsa_pubcreate(&c->rpc, &c->rp);
421   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
422   return (c);
423 }
424
425 static void rsapub_run(void *cc)
426 {
427   rsapub_ctx *c = cc;
428   mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
429   MP_DROP(d);
430 }
431
432 /* --- Symmetric encryption --- */
433
434 typedef struct ksched_ctx {
435   const gccipher *c;
436   octet *k;
437   size_t ksz;
438 } ksched_ctx;
439
440 static void *ksched_init(opts *o)
441 {
442   ksched_ctx *c = CREATE(ksched_ctx);
443   if (!o->name)
444     die(1, "must specify encryption scheme name");
445   if ((c->c = gcipher_byname(o->name)) == 0)
446     die(1, "encryption scheme `%s' not known", o->name);
447   c->ksz = keysz(o->gbits/8, c->c->keysz);
448   c->k = xmalloc(c->ksz);
449   rand_get(RAND_GLOBAL, c->k, c->ksz);
450   return (c);
451 }
452
453 static void ksched_run(void *cc)
454 {
455   ksched_ctx *c = cc;
456   gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
457   GC_DESTROY(gc);
458 }
459
460 typedef struct enc_ctx {
461   gcipher *c;
462   octet *m;
463   size_t sz;
464   size_t n;
465 } enc_ctx;
466
467 static void *enc_init(opts *o)
468 {
469   enc_ctx *c = CREATE(enc_ctx);
470   const gccipher *cc;
471   size_t ksz;
472   octet *k;
473   if (!o->name)
474     die(1, "must specify encryption scheme name");
475   if ((cc = gcipher_byname(o->name)) == 0)
476     die(1, "encryption scheme `%s' not known", o->name);
477   ksz = keysz(0, cc->keysz);
478   k = xmalloc(ksz);
479   rand_get(RAND_GLOBAL, k, ksz);
480   c->c = GC_INIT(cc, k, ksz);
481   xfree(k);
482   c->sz = o->gbits ? o->gbits : 65536;
483   c->n = o->n ? o->n : 16;
484   c->m = xmalloc(c->sz);
485   return (c);
486 }
487
488 static void enc_run(void *cc)
489 {
490   enc_ctx *c = cc;
491   size_t i;
492   for (i = 0; i < c->n; i++)
493     GC_ENCRYPT(c->c, c->m, c->m, c->sz);
494 }
495
496 /* --- Hashing --- */
497
498 typedef struct hash_ctx {
499   const gchash *h;
500   octet *m;
501   size_t sz;
502   size_t n;
503 } hash_ctx;
504
505 static void *hash_init(opts *o)
506 {
507   hash_ctx *c = CREATE(hash_ctx);
508   if (!o->name)
509     die(1, "must specify hash function name");
510   if ((c->h = ghash_byname(o->name)) == 0)
511     die(1, "hash function `%s' not known", o->name);
512   c->sz = o->gbits ? o->gbits : 65536;
513   c->n = o->n ? o->n : 16;
514   c->m = xmalloc(c->sz);
515   return (c);
516 }
517
518 static void hash_run(void *cc)
519 {
520   hash_ctx *c = cc;
521   size_t i;
522   ghash *h = GH_INIT(c->h);
523   for (i = 0; i < c->n; i++)
524     GH_HASH(h, c->m, c->sz);
525   GH_DONE(h, 0);
526   GH_DESTROY(h);
527 }
528
529 /* --- Poly1305 --- */
530
531 typedef struct poly1305_jobctx {
532   poly1305_key k;
533   octet s[POLY1305_MASKSZ];
534   octet *m;
535   size_t sz;
536   size_t n;
537 } poly1305_jobctx;
538
539 static void *poly1305_jobinit(opts *o)
540 {
541   octet k[POLY1305_KEYSZ];
542   poly1305_jobctx *c = CREATE(poly1305_jobctx);
543   rand_get(RAND_GLOBAL, k, sizeof(k));
544   poly1305_keyinit(&c->k, k, sizeof(k));
545   rand_get(RAND_GLOBAL, c->s, sizeof(c->s));
546   c->sz = o->gbits ? o->gbits : 65536;
547   c->n = o->n ? o->n : 16;
548   c->m = xmalloc(c->sz);
549   return (c);
550 }
551
552 static void poly1305_jobrun(void *cc)
553 {
554   poly1305_jobctx *c = cc;
555   poly1305_ctx ctx;
556   octet t[POLY1305_TAGSZ];
557   size_t i;
558   poly1305_macinit(&ctx, &c->k, c->s);
559   for (i = 0; i < c->n; i++) poly1305_hash(&ctx, c->m, c->sz);
560   poly1305_done(&ctx, t);
561 }
562
563 /* --- Job table --- */
564
565 typedef struct jobops {
566   const char *name;
567   void *(*init)(opts *);
568   void (*run)(void *);
569 } jobops;
570
571 static const jobops jobtab[] = {
572   { "g-prime-exp",              grp_init,               gr_run },
573   { "g-ec-mul",                 grec_init,              gr_run },
574   { "g-prime-exp-sim",          grp_init,               grsim_run },
575   { "g-ec-mul-sim",             grec_init,              grsim_run },
576   { "barrett-exp",              bar_init,               bar_run },
577   { "barrett-exp-sim",          bar_init,               barsim_run },
578   { "mont-exp",                 mont_init,              mont_run },
579   { "mont-exp-sim",             mont_init,              montsim_run },
580   { "rsa-priv",                 rsapriv_init,           rsapriv_run },
581   { "rsa-priv-blind",           rsaprivblind_init,      rsapriv_run },
582   { "rsa-pub",                  rsapub_init,            rsapub_run },
583   { "x25519",                   x25519_jobinit,         x25519_jobrun },
584   { "x448",                     x448_jobinit,           x448_jobrun },
585   { "ed25519-sign",             ed25519_signinit,       ed25519_signrun },
586   { "ed25519-vrf",              ed25519_vrfinit,        ed25519_vrfrun },
587   { "ksched",                   ksched_init,            ksched_run },
588   { "enc",                      enc_init,               enc_run },
589   { "hash",                     hash_init,              hash_run },
590   { "poly1305",                 poly1305_jobinit,       poly1305_jobrun },
591   { 0,                          0,                      0 }
592 };
593
594 /*----- Main code ---------------------------------------------------------*/
595
596 void version(FILE *fp)
597 {
598   pquis(fp, "$, Catacomb " VERSION "\n");
599 }
600
601 static void usage(FILE *fp)
602 {
603   pquis(fp, "Usage: $ [-options] job\n");
604 }
605
606 static void help(FILE *fp)
607 {
608   version(fp);
609   putc('\n', fp);
610   usage(fp);
611   pquis(fp, "\n\
612 Various performance tests.\n\
613 \n\
614 Options:\n\
615 \n\
616 -h, --help              Show this help text.\n\
617 -v, --version           Show program version number.\n\
618 -u, --usage             Show terse usage message.\n\
619 -l, --list [ITEM...]    List all the various names of things.\n\
620 \n\
621 -C, --name=NAME         Select curve/DH-group/enc/hash name.\n\
622 -b, --field-bits        Field size for g-prime and rsa.\n\
623 -q, --no-check          Don't check field/group for validity.\n\
624 -B, --group-bits        Group size for g-prime; key size for ksched;\n\
625                           data size for enc and hash.\n\
626 -n, --factors=COUNT     Number of factors for {exp,mul}-sim.\n\
627 -i, --intervals=COUNT   Number of intervals to run for.  [0; forever]\n\
628 -t, --time=TIME         Length of an interval in seconds.  [1]\n\
629 ");
630 }
631
632 #define LISTS(LI)                                                       \
633   LI("Lists", list,                                                     \
634      listtab[i].name, listtab[i].name)                                  \
635   LI("Jobs", job,                                                       \
636      jobtab[i].name, jobtab[i].name)                                    \
637   LI("Elliptic curves", ec,                                             \
638      ectab[i].name, ectab[i].name)                                      \
639   LI("Diffie-Hellman groups", dh,                                       \
640      ptab[i].name, ptab[i].name)                                        \
641   LI("Encryption algorithms", cipher,                                   \
642      gciphertab[i], gciphertab[i]->name)                                \
643   LI("Hash functions", hash,                                            \
644      ghashtab[i], ghashtab[i]->name)
645
646 MAKELISTTAB(listtab, LISTS)
647
648 static unsigned uarg(const char *what, const char *p)
649 {
650   char *q;
651   unsigned long u;
652   errno = 0;
653   u = strtoul(p, &q, 0);
654   if (*q || u > UINT_MAX || q == p || errno)
655     die(1, "bad %s `%s'", what, p);
656   return (u);
657 }
658
659 static mp *mparg(const char *what, const char *p)
660 {
661   char *q;
662   mp *x = mp_readstring(MP_NEW, p, &q, 0);
663   if (!x || *q) die(1, "bad %s `%s'", what, p);
664   return (x);
665 }
666
667 static double farg(const char *what, const char *p)
668 {
669   char *q;
670   double f;
671   errno = 0;
672   f = strtod(p, &q);
673   if (*q || q == p || errno)
674     die(1, "bad %s `%s'", what, p);
675   return (f);
676 }
677
678 int main(int argc, char *argv[])
679 {
680   int i;
681   opts o = { 0 };
682   const jobops *j;
683   struct timeval tv_next, tv_now;
684   double t, ttot;
685   unsigned n;
686   unsigned long ii;
687   clock_t c_start, c_stop;
688   double itot;
689   void *p;
690
691   ego(argv[0]);
692   o.t = 1;
693   for (;;) {
694     static const struct option opts[] = {
695       { "help",         0,              0,      'h' },
696       { "version",      0,              0,      'v' },
697       { "usage",        0,              0,      'u' },
698       { "list",         0,              0,      'l' },
699       { "name",         OPTF_ARGREQ,    0,      'C' },
700       { "field-bits",   OPTF_ARGREQ,    0,      'b' },
701       { "group-bits",   OPTF_ARGREQ,    0,      'B' },
702       { "factors",      OPTF_ARGREQ,    0,      'n' },
703       { "intervals",    OPTF_ARGREQ,    0,      'i' },
704       { "public-exponent", OPTF_ARGREQ, 0,      'e' },
705       { "time",         OPTF_ARGREQ,    0,      't' },
706       { "no-check",     0,              0,      'q' },
707       { 0,              0,              0,      0 }
708     };
709
710     i = mdwopt(argc, argv, "hvulC:b:B:n:i:e:t:q", opts, 0, 0, 0);
711     if (i < 0) break;
712     switch (i) {
713       case 'h': help(stdout); exit(0);
714       case 'v': version(stdout); exit(0);
715       case 'u': usage(stdout); exit(0);
716       case 'l': exit(displaylists(listtab, argv + optind));
717       case 'C': o.name = optarg; break;
718       case 'b': o.fbits = uarg("field bits", optarg); break;
719       case 'B': o.gbits = uarg("subgroup bits", optarg); break;
720       case 'n': o.n = uarg("factor count", optarg); break;
721       case 'e':
722         mp_drop(o.e); o.e = mparg("public exponent", optarg);
723         if (MP_CMP(o.e, <, MP_THREE) || MP_EVENP(o.e))
724           die(1, "invalid public exponent");
725         break;
726       case 'i': o.i = uarg("interval count", optarg); break;
727       case 't': o.t = farg("interval length", optarg); break;
728       case 'q': o.f |= OF_NOCHECK; break;
729       default: usage(stderr); exit(1);
730     }
731   }
732   if (optind + 1 != argc) { usage(stderr); exit(1); }
733
734   for (j = jobtab; j->name; j++)
735     if (strcmp(j->name, argv[optind]) == 0) break;
736   if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
737   p = j->init(&o);
738
739   n = 0;
740   ttot = itot =  0;
741   gettimeofday(&tv_now, 0);
742   do {
743     tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
744     ii = 0;
745     c_start = clock();
746     do {
747       j->run(p);
748       ii++;
749       gettimeofday(&tv_now, 0);
750     } while (TV_CMP(&tv_now, <, &tv_next));
751     c_stop = clock();
752     t = (double)(c_stop - c_start)/CLOCKS_PER_SEC;
753     itot += ii;
754     ttot += t;
755     printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
756            n, ii, ii/t, itot/ttot);
757     fflush(stdout);
758     n++;
759   } while (!o.i || n < o.i);
760
761   return (0);
762 }
763
764 /*----- That's all, folks -------------------------------------------------*/