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