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 #include <mLib/alloc.h>
47 #include <mLib/dstr.h>
48 #include <mLib/mdwopt.h>
49 #include <mLib/quis.h>
50 #include <mLib/report.h>
60 #include "mpbarrett.h"
73 /*----- Options -----------------------------------------------------------*/
76 const char *name; /* Pre-configured named thing */
77 unsigned fbits; /* Field size bits */
78 unsigned gbits; /* Group size bits */
79 unsigned n; /* Number of factors */
80 unsigned i; /* Number of intervals (or zero) */
81 double t; /* Time for each interval (secs) */
82 unsigned f; /* Flags */
83 #define OF_NOCHECK 1u /* Don't do group checking */
86 /*----- Job switch --------------------------------------------------------*/
88 /* --- Barrett exponentiation --- */
90 typedef struct bar_ctx {
96 static void *bar_init(opts *o)
98 bar_ctx *c = CREATE(bar_ctx);
105 if (dh_parse(&qd, &gp))
106 die(1, "bad prime group: %s", qd.e);
108 if (!o->fbits) o->fbits = 1024;
109 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
111 mpbarrett_create(&c->b, gp.p);
114 c->e = xmalloc(c->n * sizeof(group_expfactor));
115 for (i = 0; i < c->n; i++) {
116 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
117 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
123 static void bar_run(void *cc)
126 mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
130 static void barsim_run(void *cc)
133 mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
137 /* --- Montgomery exponentiation --- */
139 typedef struct mont_ctx {
145 static void *mont_init(opts *o)
147 mont_ctx *c = CREATE(mont_ctx);
154 if (dh_parse(&qd, &gp))
155 die(1, "bad prime group: %s", qd.e);
157 if (!o->fbits) o->fbits = 1024;
158 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
160 mpmont_create(&c->m, gp.p);
163 c->e = xmalloc(c->n * sizeof(mp_expfactor));
164 for (i = 0; i < c->n; i++) {
165 c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
166 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
172 static void mont_run(void *cc)
175 mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
179 static void montsim_run(void *cc)
182 mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
186 /* --- Group exponentiation --- */
188 typedef struct gr_ctx {
194 static void *grp_init(opts *o)
196 gr_ctx *c = CREATE(gr_ctx);
204 if (dh_parse(&qd, &gp))
205 die(1, "bad prime group: %s", qd.e);
207 if (!o->fbits) o->fbits = 1024;
208 dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
210 c->g = group_prime(&gp);
211 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
212 die(1, "bad group: %s", e);
215 c->e = xmalloc(c->n * sizeof(group_expfactor));
216 for (i = 0; i < c->n; i++) {
217 c->e[i].base = G_CREATE(c->g);
218 G_FROMINT(c->g, c->e[i].base,
219 mprand_range(MP_NEW, gp.p, &rand_global, 0));
220 c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
226 static void *grec_init(opts *o)
228 gr_ctx *c = CREATE(gr_ctx);
235 die(1, "can't generate elliptic curves");
236 if ((e = ec_getinfo(&ei, o->name)) != 0)
237 die(1, "bad curve: %s", e);
238 c->g = group_ec(&ei);
239 if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
240 die(1, "bad group: %s", e);
243 c->e = xmalloc(c->n * sizeof(group_expfactor));
244 for (i = 0; i < c->n; i++) {
245 c->e[i].base = G_CREATE(c->g);
246 ec_rand(ei.c, &p, &rand_global);
247 G_FROMEC(c->g, c->e[i].base, &p);
248 c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
254 static void gr_run(void *cc)
257 ge *x = G_CREATE(c->g);
258 G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
262 static void grsim_run(void *cc)
265 ge *x = G_CREATE(c->g);
266 G_MEXP(c->g, x, c->e, c->n);
272 typedef struct rsapriv_ctx {
278 static void *rsapriv_init(opts *o)
280 rsapriv_ctx *c = CREATE(rsapriv_ctx);
282 if (!o->fbits) o->fbits = 1024;
283 rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
284 rsa_privcreate(&c->rpc, &c->rp, 0);
285 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
289 static void *rsaprivblind_init(opts *o)
291 rsapriv_ctx *c = CREATE(rsapriv_ctx);
293 if (!o->fbits) o->fbits = 1024;
294 rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
295 rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
296 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
300 static void rsapriv_run(void *cc)
303 mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
307 typedef struct rsapub_ctx {
313 static void *rsapub_init(opts *o)
315 rsapub_ctx *c = CREATE(rsapub_ctx);
318 if (!o->fbits) o->fbits = 1024;
319 rsa_gen(&rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
320 c->rp.n = MP_COPY(rp.n);
321 c->rp.e = MP_COPY(rp.e);
323 rsa_pubcreate(&c->rpc, &c->rp);
324 c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
328 static void rsapub_run(void *cc)
331 mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
335 /* --- Symmetric encryption --- */
337 typedef struct ksched_ctx {
343 static void *ksched_init(opts *o)
345 ksched_ctx *c = CREATE(ksched_ctx);
347 die(1, "must specify encryption scheme name");
348 if ((c->c = gcipher_byname(o->name)) == 0)
349 die(1, "encryption scheme `%s' not known", o->name);
350 c->ksz = keysz(o->gbits/8, c->c->keysz);
351 c->k = xmalloc(c->ksz);
352 rand_get(RAND_GLOBAL, c->k, c->ksz);
356 static void ksched_run(void *cc)
359 gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
363 typedef struct enc_ctx {
370 static void *enc_init(opts *o)
372 enc_ctx *c = CREATE(enc_ctx);
377 die(1, "must specify encryption scheme name");
378 if ((cc = gcipher_byname(o->name)) == 0)
379 die(1, "encryption scheme `%s' not known", o->name);
380 ksz = keysz(0, cc->keysz);
382 rand_get(RAND_GLOBAL, k, ksz);
383 c->c = GC_INIT(cc, k, ksz);
385 c->sz = o->gbits ? o->gbits : 65536;
386 c->n = o->n ? o->n : 16;
387 c->m = xmalloc(c->sz);
391 static void enc_run(void *cc)
395 for (i = 0; i < c->n; i++)
396 GC_ENCRYPT(c->c, c->m, c->m, c->sz);
399 /* --- Hashing --- */
401 typedef struct hash_ctx {
408 static void *hash_init(opts *o)
410 hash_ctx *c = CREATE(hash_ctx);
412 die(1, "must specify hash function name");
413 if ((c->h = ghash_byname(o->name)) == 0)
414 die(1, "hash function `%s' not known", o->name);
415 c->sz = o->gbits ? o->gbits : 65536;
416 c->n = o->n ? o->n : 16;
417 c->m = xmalloc(c->sz);
421 static void hash_run(void *cc)
425 ghash *h = GH_INIT(c->h);
426 for (i = 0; i < c->n; i++)
427 GH_HASH(h, c->m, c->sz);
432 /* --- Job table --- */
434 typedef struct jobops {
436 void *(*init)(opts *);
440 static const jobops jobtab[] = {
441 { "g-prime-exp", grp_init, gr_run },
442 { "g-ec-mul", grec_init, gr_run },
443 { "g-prime-exp-sim", grp_init, grsim_run },
444 { "g-ec-mul-sim", grec_init, grsim_run },
445 { "barrett-exp", bar_init, bar_run },
446 { "barrett-exp-sim", bar_init, barsim_run },
447 { "mont-exp", mont_init, mont_run },
448 { "mont-exp-sim", mont_init, montsim_run },
449 { "rsa-priv", rsapriv_init, rsapriv_run },
450 { "rsa-priv-blind", rsaprivblind_init, rsapriv_run },
451 { "rsa-pub", rsapub_init, rsapub_run },
452 { "ksched", ksched_init, ksched_run },
453 { "enc", enc_init, enc_run },
454 { "hash", hash_init, hash_run },
458 /*----- Main code ---------------------------------------------------------*/
460 void version(FILE *fp)
462 pquis(fp, "$, Catacomb " VERSION "\n");
465 static void usage(FILE *fp)
467 pquis(fp, "Usage: $ [-options] job\n");
470 static void help(FILE *fp)
476 Various performance tests.\n\
480 -h, --help Show this help text.\n\
481 -v, --version Show program version number.\n\
482 -u, --usage Show terse usage message.\n\
483 -l, --list [ITEM...] List all the various names of things.\n\
485 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
486 -b, --field-bits Field size for g-prime and rsa.\n\
487 -q, --no-check Don't check field/group for validity.\n\
488 -B, --group-bits Group size for g-prime; key size for ksched;\n\
489 data size for enc and hash.\n\
490 -n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
491 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
492 -t, --time=TIME Length of an interval in seconds. [1]\n\
498 listtab[i].name, listtab[i].name) \
500 jobtab[i].name, jobtab[i].name) \
501 LI("Elliptic curves", ec, \
502 ectab[i].name, ectab[i].name) \
503 LI("Diffie-Hellman groups", dh, \
504 ptab[i].name, ptab[i].name) \
505 LI("Encryption algorithms", cipher, \
506 gciphertab[i], gciphertab[i]->name) \
507 LI("Hash functions", hash, \
508 ghashtab[i], ghashtab[i]->name)
510 MAKELISTTAB(listtab, LISTS)
512 static unsigned uarg(const char *what, const char *p)
517 u = strtoul(p, &q, 0);
518 if (*q || u > UINT_MAX || q == p || errno)
519 die(1, "bad %s `%s'", what, p);
523 static double farg(const char *what, const char *p)
529 if (*q || q == p || errno)
530 die(1, "bad %s `%s'", what, p);
534 int main(int argc, char *argv[])
539 struct timeval tv_next, tv_now;
543 clock_t c_start, c_stop;
550 static const struct option opts[] = {
551 { "help", 0, 0, 'h' },
552 { "version", 0, 0, 'v' },
553 { "usage", 0, 0, 'u' },
554 { "list", 0, 0, 'l' },
555 { "name", OPTF_ARGREQ, 0, 'C' },
556 { "field-bits", OPTF_ARGREQ, 0, 'b' },
557 { "group-bits", OPTF_ARGREQ, 0, 'B' },
558 { "factors", OPTF_ARGREQ, 0, 'n' },
559 { "intervals", OPTF_ARGREQ, 0, 'i' },
560 { "time", OPTF_ARGREQ, 0, 't' },
561 { "no-check", 0, 0, 'q' },
565 i = mdwopt(argc, argv, "hvulC:b:B:n:i:t:q", opts, 0, 0, 0);
568 case 'h': help(stdout); exit(0);
569 case 'v': version(stdout); exit(0);
570 case 'u': usage(stdout); exit(0);
571 case 'l': exit(displaylists(listtab, argv + optind));
572 case 'C': o.name = optarg; break;
573 case 'b': o.fbits = uarg("field bits", optarg); break;
574 case 'B': o.gbits = uarg("subgroup bits", optarg); break;
575 case 'n': o.n = uarg("factor count", optarg); break;
576 case 'i': o.i = uarg("interval count", optarg); break;
577 case 't': o.t = farg("interval length", optarg); break;
578 case 'q': o.f |= OF_NOCHECK; break;
579 default: usage(stderr); exit(1);
582 if (optind + 1 != argc) { usage(stderr); exit(1); }
584 for (j = jobtab; j->name; j++)
585 if (strcmp(j->name, argv[optind]) == 0) break;
586 if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
591 gettimeofday(&tv_now, 0);
593 tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
599 gettimeofday(&tv_now, 0);
600 } while (TV_CMP(&tv_now, <, &tv_next));
602 t = (double)(c_stop - c_start)/CLOCKS_PER_SEC;
605 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
606 n, ii, ii/t, itot/ttot);
609 } while (!o.i || n < o.i);
614 /*----- That's all, folks -------------------------------------------------*/