5 * Measure performance of various operations (Unix-specific)
7 * (c) 2004 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of Catacomb.
14 * Catacomb is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU Library General Public License as
16 * published by the Free Software Foundation; either version 2 of the
17 * License, or (at your option) any later version.
19 * Catacomb is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Library General Public License for more details.
24 * You should have received a copy of the GNU Library General Public
25 * License along with Catacomb; if not, write to the Free
26 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
30 /*----- Header files ------------------------------------------------------*/
32 #define _FILE_OFFSET_BITS 64
44 #include <sys/types.h>
48 #include <mLib/alloc.h>
49 #include <mLib/dstr.h>
50 #include <mLib/mdwopt.h>
51 #include <mLib/quis.h>
52 #include <mLib/report.h>
62 #include "mpbarrett.h"
75 /*----- Options -----------------------------------------------------------*/
78 const char *name; /* Pre-configured named thing */
79 unsigned fbits; /* Field size bits */
80 unsigned gbits; /* Group size bits */
81 unsigned n; /* Number of factors */
82 unsigned i; /* Number of intervals (or zero) */
83 double t; /* Time for each interval (secs) */
84 unsigned f; /* Flags */
85 #define OF_NOCHECK 1u /* Don't do group checking */
88 /*----- Job switch --------------------------------------------------------*/
90 /* --- Barrett exponentiation --- */
92 typedef struct bar_ctx {
98 static void *bar_init(opts *o)
100 bar_ctx *c = CREATE(bar_ctx);
107 if (dh_parse(&qd, &gp))
108 die(1, "bad prime group: %s", qd.e);
110 if (!o->fbits) o->fbits = 1024;
111 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
113 mpbarrett_create(&c->b, gp.p);
116 c->e = xmalloc(c->n * sizeof(group_expfactor));
117 for (i = 0; i < c->n; i++) {
118 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
119 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
125 static void bar_run(void *cc)
128 mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
132 static void barsim_run(void *cc)
135 mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
139 /* --- Montgomery exponentiation --- */
141 typedef struct mont_ctx {
147 static void *mont_init(opts *o)
149 mont_ctx *c = CREATE(mont_ctx);
156 if (dh_parse(&qd, &gp))
157 die(1, "bad prime group: %s", qd.e);
159 if (!o->fbits) o->fbits = 1024;
160 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
162 mpmont_create(&c->m, gp.p);
165 c->e = xmalloc(c->n * sizeof(mp_expfactor));
166 for (i = 0; i < c->n; i++) {
167 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
168 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
174 static void mont_run(void *cc)
177 mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
181 static void montsim_run(void *cc)
184 mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
188 /* --- Group exponentiation --- */
190 typedef struct gr_ctx {
196 static void *grp_init(opts *o)
198 gr_ctx *c = CREATE(gr_ctx);
206 if (dh_parse(&qd, &gp))
207 die(1, "bad prime group: %s", qd.e);
209 if (!o->fbits) o->fbits = 1024;
210 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
212 c->g = group_prime(&gp);
213 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
214 die(1, "bad group: %s", e);
217 c->e = xmalloc(c->n * sizeof(group_expfactor));
218 for (i = 0; i < c->n; i++) {
219 c->e[i].base = G_CREATE(c->g);
220 G_FROMINT(c->g, c->e[i].base,
221 mprand_range(MP_NEW, gp.p, &rand_global, 0));
222 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
228 static void *grec_init(opts *o)
230 gr_ctx *c = CREATE(gr_ctx);
237 die(1, "can't generate elliptic curves");
238 if ((e = ec_getinfo(&ei, o->name)) != 0)
239 die(1, "bad curve: %s", e);
240 c->g = group_ec(&ei);
241 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
242 die(1, "bad group: %s", e);
245 c->e = xmalloc(c->n * sizeof(group_expfactor));
246 for (i = 0; i < c->n; i++) {
247 c->e[i].base = G_CREATE(c->g);
248 ec_rand(ei.c, &p, &rand_global);
249 G_FROMEC(c->g, c->e[i].base, &p);
250 c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
256 static void gr_run(void *cc)
259 ge *x = G_CREATE(c->g);
260 G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
264 static void grsim_run(void *cc)
267 ge *x = G_CREATE(c->g);
268 G_MEXP(c->g, x, c->e, c->n);
274 typedef struct rsapriv_ctx {
280 static void *rsapriv_init(opts *o)
282 rsapriv_ctx *c = CREATE(rsapriv_ctx);
284 if (!o->fbits) o->fbits = 1024;
285 rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
286 rsa_privcreate(&c->rpc, &c->rp, 0);
287 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
291 static void *rsaprivblind_init(opts *o)
293 rsapriv_ctx *c = CREATE(rsapriv_ctx);
295 if (!o->fbits) o->fbits = 1024;
296 rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
297 rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
298 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
302 static void rsapriv_run(void *cc)
305 mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
309 typedef struct rsapub_ctx {
315 static void *rsapub_init(opts *o)
317 rsapub_ctx *c = CREATE(rsapub_ctx);
320 if (!o->fbits) o->fbits = 1024;
321 rsa_gen(&rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
322 c->rp.n = MP_COPY(rp.n);
323 c->rp.e = MP_COPY(rp.e);
325 rsa_pubcreate(&c->rpc, &c->rp);
326 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
330 static void rsapub_run(void *cc)
333 mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
337 /* --- Symmetric encryption --- */
339 typedef struct ksched_ctx {
345 static void *ksched_init(opts *o)
347 ksched_ctx *c = CREATE(ksched_ctx);
349 die(1, "must specify encryption scheme name");
350 if ((c->c = gcipher_byname(o->name)) == 0)
351 die(1, "encryption scheme `%s' not known", o->name);
352 c->ksz = keysz(o->gbits/8, c->c->keysz);
353 c->k = xmalloc(c->ksz);
354 rand_get(RAND_GLOBAL, c->k, c->ksz);
358 static void ksched_run(void *cc)
361 gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
365 typedef struct enc_ctx {
372 static void *enc_init(opts *o)
374 enc_ctx *c = CREATE(enc_ctx);
379 die(1, "must specify encryption scheme name");
380 if ((cc = gcipher_byname(o->name)) == 0)
381 die(1, "encryption scheme `%s' not known", o->name);
382 ksz = keysz(0, cc->keysz);
384 rand_get(RAND_GLOBAL, k, ksz);
385 c->c = GC_INIT(cc, k, ksz);
387 c->sz = o->gbits ? o->gbits : 65536;
388 c->n = o->n ? o->n : 16;
389 c->m = xmalloc(c->sz);
393 static void enc_run(void *cc)
397 for (i = 0; i < c->n; i++)
398 GC_ENCRYPT(c->c, c->m, c->m, c->sz);
401 /* --- Hashing --- */
403 typedef struct hash_ctx {
410 static void *hash_init(opts *o)
412 hash_ctx *c = CREATE(hash_ctx);
414 die(1, "must specify hash function name");
415 if ((c->h = ghash_byname(o->name)) == 0)
416 die(1, "hash function `%s' not known", o->name);
417 c->sz = o->gbits ? o->gbits : 65536;
418 c->n = o->n ? o->n : 16;
419 c->m = xmalloc(c->sz);
423 static void hash_run(void *cc)
427 ghash *h = GH_INIT(c->h);
428 for (i = 0; i < c->n; i++)
429 GH_HASH(h, c->m, c->sz);
434 /* --- Job table --- */
436 typedef struct jobops {
438 void *(*init)(opts *);
442 static const jobops jobtab[] = {
443 { "g-prime-exp", grp_init, gr_run },
444 { "g-ec-mul", grec_init, gr_run },
445 { "g-prime-exp-sim", grp_init, grsim_run },
446 { "g-ec-mul-sim", grec_init, grsim_run },
447 { "barrett-exp", bar_init, bar_run },
448 { "barrett-exp-sim", bar_init, barsim_run },
449 { "mont-exp", mont_init, mont_run },
450 { "mont-exp-sim", mont_init, montsim_run },
451 { "rsa-priv", rsapriv_init, rsapriv_run },
452 { "rsa-priv-blind", rsaprivblind_init, rsapriv_run },
453 { "rsa-pub", rsapub_init, rsapub_run },
454 { "ksched", ksched_init, ksched_run },
455 { "enc", enc_init, enc_run },
456 { "hash", hash_init, hash_run },
460 /*----- Main code ---------------------------------------------------------*/
462 void version(FILE *fp)
464 pquis(fp, "$, Catacomb " VERSION "\n");
467 static void usage(FILE *fp)
469 pquis(fp, "Usage: $ [-options] job\n");
472 static void help(FILE *fp)
478 Various performance tests.\n\
482 -h, --help Show this help text.\n\
483 -v, --version Show program version number.\n\
484 -u, --usage Show terse usage message.\n\
485 -l, --list [ITEM...] List all the various names of things.\n\
487 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
488 -b, --field-bits Field size for g-prime and rsa.\n\
489 -q, --no-check Don't check field/group for validity.\n\
490 -B, --group-bits Group size for g-prime; key size for ksched;\n\
491 data size for enc and hash.\n\
492 -n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
493 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
494 -t, --time=TIME Length of an interval in seconds. [1]\n\
500 listtab[i].name, listtab[i].name) \
502 jobtab[i].name, jobtab[i].name) \
503 LI("Elliptic curves", ec, \
504 ectab[i].name, ectab[i].name) \
505 LI("Diffie-Hellman groups", dh, \
506 ptab[i].name, ptab[i].name) \
507 LI("Encryption algorithms", cipher, \
508 gciphertab[i], gciphertab[i]->name) \
509 LI("Hash functions", hash, \
510 ghashtab[i], ghashtab[i]->name)
512 MAKELISTTAB(listtab, LISTS)
514 static unsigned uarg(const char *what, const char *p)
519 u = strtoul(p, &q, 0);
520 if (*q || u > UINT_MAX || q == p || errno)
521 die(1, "bad %s `%s'", what, p);
525 static double farg(const char *what, const char *p)
531 if (*q || q == p || errno)
532 die(1, "bad %s `%s'", what, p);
536 int main(int argc, char *argv[])
541 struct timeval tv_next, tv_now;
545 clock_t c_start, c_stop;
552 static const struct option opts[] = {
553 { "help", 0, 0, 'h' },
554 { "version", 0, 0, 'v' },
555 { "usage", 0, 0, 'u' },
556 { "list", 0, 0, 'l' },
557 { "name", OPTF_ARGREQ, 0, 'C' },
558 { "field-bits", OPTF_ARGREQ, 0, 'b' },
559 { "group-bits", OPTF_ARGREQ, 0, 'B' },
560 { "factors", OPTF_ARGREQ, 0, 'n' },
561 { "intervals", OPTF_ARGREQ, 0, 'i' },
562 { "time", OPTF_ARGREQ, 0, 't' },
563 { "no-check", 0, 0, 'q' },
567 i = mdwopt(argc, argv, "hvulC:b:B:n:i:t:q", opts, 0, 0, 0);
570 case 'h': help(stdout); exit(0);
571 case 'v': version(stdout); exit(0);
572 case 'u': usage(stdout); exit(0);
573 case 'l': exit(displaylists(listtab, argv + optind));
574 case 'C': o.name = optarg; break;
575 case 'b': o.fbits = uarg("field bits", optarg); break;
576 case 'B': o.gbits = uarg("subgroup bits", optarg); break;
577 case 'n': o.n = uarg("factor count", optarg); break;
578 case 'i': o.i = uarg("interval count", optarg); break;
579 case 't': o.t = farg("interval length", optarg); break;
580 case 'q': o.f |= OF_NOCHECK; break;
581 default: usage(stderr); exit(1);
584 if (optind + 1 != argc) { usage(stderr); exit(1); }
586 for (j = jobtab; j->name; j++)
587 if (strcmp(j->name, argv[optind]) == 0) break;
588 if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
593 gettimeofday(&tv_now, 0);
595 tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
601 gettimeofday(&tv_now, 0);
602 } while (TV_CMP(&tv_now, <, &tv_next));
604 t = (double)(c_stop - c_start)/CLOCKS_PER_SEC;
607 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
608 n, ii, ii/t, itot/ttot);
611 } while (!o.i || n < o.i);
616 /*----- That's all, folks -------------------------------------------------*/