progs/*.1: Mention the default hash for `ed25519',
[catacomb] / progs / perftest.c
CommitLineData
e2edda68 1/* -*-c-*-
2 *
e2edda68 3 * Measure performance of various operations (Unix-specific)
4 *
5 * (c) 2004 Straylight/Edgeware
6 */
7
45c0fd36 8/*----- Licensing notice --------------------------------------------------*
e2edda68 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.
45c0fd36 16 *
e2edda68 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.
45c0fd36 21 *
e2edda68 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
cd6eca43
MW
30#define _FILE_OFFSET_BITS 64
31
e2edda68 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"
57fe52c7
MW
59#include "mpint.h"
60#include "mptext.h"
e2edda68 61#include "mpmont.h"
62#include "mpbarrett.h"
63#include "dh.h"
64#include "pgen.h"
65#include "ec.h"
66#include "group.h"
fc2d44af 67#include "x25519.h"
643eb1bb 68#include "x448.h"
d56fd9d1 69#include "ed25519.h"
e2edda68 70
c65df279 71#include "cc.h"
e2edda68 72#include "gcipher.h"
73#include "ghash.h"
74#include "gmac.h"
ef7cf21d 75#include "poly1305.h"
19e6e199 76
c65df279 77#include "ectab.h"
78#include "ptab.h"
e2edda68 79
80/*----- Options -----------------------------------------------------------*/
81
82typedef 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) */
57fe52c7 89 mp *e; /* Public exponent */
e74e12bc
MW
90 unsigned f; /* Flags */
91#define OF_NOCHECK 1u /* Don't do group checking */
e2edda68 92} opts;
93
94/*----- Job switch --------------------------------------------------------*/
95
96/* --- Barrett exponentiation --- */
97
98typedef struct bar_ctx {
99 size_t n;
100 mpbarrett b;
101 mp_expfactor *e;
102} bar_ctx;
103
104static 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
131static 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
138static 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
147typedef struct mont_ctx {
148 size_t n;
149 mpmont m;
150 mp_expfactor *e;
151} mont_ctx;
152
153static 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
180static 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
187static 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
196typedef struct gr_ctx {
197 size_t n;
198 group *g;
199 group_expfactor *e;
200} gr_ctx;
201
202static 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);
e74e12bc 219 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
e2edda68 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
234static 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);
e74e12bc 247 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
e2edda68 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
262static 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
270static 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
fc2d44af
MW
278/* --- x25519 --- */
279
280typedef struct x25519_jobctx {
281 octet k[X25519_KEYSZ];
282 octet p[X25519_PUBSZ];
283} x25519_jobctx;
284
285static 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
293static void x25519_jobrun(void *cc)
294 { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); }
295
643eb1bb
MW
296/* --- x448 --- */
297
298typedef struct x448_jobctx {
299 octet k[X448_KEYSZ];
300 octet p[X448_PUBSZ];
301} x448_jobctx;
302
303static 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
311static void x448_jobrun(void *cc)
312 { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); }
313
d56fd9d1
MW
314/* --- Ed25519 --- */
315
316typedef struct ed25519_signctx {
317 octet k[ED25519_KEYSZ];
318 octet K[ED25519_PUBSZ];
319 octet m[64];
320} ed25519_signctx;
321
322typedef struct ed25519_vrfctx {
323 octet K[ED25519_PUBSZ];
324 octet m[64];
325 octet sig[ED25519_SIGSZ];
326} ed25519_vrfctx;
327
328static 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
338static 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
346static 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
358static 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
e2edda68 364/* --- RSA --- */
365
366typedef struct rsapriv_ctx {
367 rsa_priv rp;
368 rsa_privctx rpc;
369 mp *m;
370} rsapriv_ctx;
371
372static void *rsapriv_init(opts *o)
373{
374 rsapriv_ctx *c = CREATE(rsapriv_ctx);
375
376 if (!o->fbits) o->fbits = 1024;
57fe52c7
MW
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);
e2edda68 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
384static void *rsaprivblind_init(opts *o)
385{
386 rsapriv_ctx *c = CREATE(rsapriv_ctx);
387
388 if (!o->fbits) o->fbits = 1024;
57fe52c7
MW
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);
e2edda68 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
396static 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
403typedef struct rsapub_ctx {
404 rsa_pub rp;
405 rsa_pubctx rpc;
406 mp *m;
407} rsapub_ctx;
408
409static 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;
57fe52c7
MW
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);
e2edda68 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
425static 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
434typedef struct ksched_ctx {
435 const gccipher *c;
436 octet *k;
437 size_t ksz;
438} ksched_ctx;
439
440static 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
453static 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
460typedef struct enc_ctx {
461 gcipher *c;
462 octet *m;
463 size_t sz;
464 size_t n;
465} enc_ctx;
466
467static 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
488static 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
498typedef struct hash_ctx {
499 const gchash *h;
500 octet *m;
501 size_t sz;
502 size_t n;
503} hash_ctx;
504
505static 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
518static 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
ef7cf21d
MW
529/* --- Poly1305 --- */
530
531typedef 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
539static 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
552static 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
e2edda68 563/* --- Job table --- */
564
c65df279 565typedef struct jobops {
e2edda68 566 const char *name;
567 void *(*init)(opts *);
568 void (*run)(void *);
569} jobops;
570
571static const jobops jobtab[] = {
45c0fd36 572 { "g-prime-exp", grp_init, gr_run },
e2edda68 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 },
fc2d44af 583 { "x25519", x25519_jobinit, x25519_jobrun },
643eb1bb 584 { "x448", x448_jobinit, x448_jobrun },
d56fd9d1
MW
585 { "ed25519-sign", ed25519_signinit, ed25519_signrun },
586 { "ed25519-vrf", ed25519_vrfinit, ed25519_vrfrun },
e2edda68 587 { "ksched", ksched_init, ksched_run },
588 { "enc", enc_init, enc_run },
589 { "hash", hash_init, hash_run },
ef7cf21d 590 { "poly1305", poly1305_jobinit, poly1305_jobrun },
e2edda68 591 { 0, 0, 0 }
592};
593
594/*----- Main code ---------------------------------------------------------*/
595
c65df279 596void version(FILE *fp)
e2edda68 597{
598 pquis(fp, "$, Catacomb " VERSION "\n");
599}
600
601static void usage(FILE *fp)
602{
603 pquis(fp, "Usage: $ [-options] job\n");
604}
605
606static void help(FILE *fp)
607{
608 version(fp);
609 putc('\n', fp);
610 usage(fp);
611 pquis(fp, "\n\
612Various performance tests.\n\
c65df279 613\n\
614Options:\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\
78614e02 623-q, --no-check Don't check field/group for validity.\n\
c65df279 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\
e2edda68 629");
630}
631
c65df279 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
646MAKELISTTAB(listtab, LISTS)
647
e2edda68 648static 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
57fe52c7
MW
659static 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
e2edda68 667static 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
678int 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' },
c65df279 698 { "list", 0, 0, 'l' },
e2edda68 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' },
57fe52c7 704 { "public-exponent", OPTF_ARGREQ, 0, 'e' },
e2edda68 705 { "time", OPTF_ARGREQ, 0, 't' },
e74e12bc 706 { "no-check", 0, 0, 'q' },
e2edda68 707 { 0, 0, 0, 0 }
708 };
709
57fe52c7 710 i = mdwopt(argc, argv, "hvulC:b:B:n:i:e:t:q", opts, 0, 0, 0);
e2edda68 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);
c65df279 716 case 'l': exit(displaylists(listtab, argv + optind));
e2edda68 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;
57fe52c7
MW
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;
e2edda68 726 case 'i': o.i = uarg("interval count", optarg); break;
727 case 't': o.t = farg("interval length", optarg); break;
e74e12bc 728 case 'q': o.f |= OF_NOCHECK; break;
e2edda68 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;
45c0fd36 740 ttot = itot = 0;
e2edda68 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 -------------------------------------------------*/