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 ------------------------------------------------------*/
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) */
84 /*----- Job switch --------------------------------------------------------*/
86 /* --- Barrett exponentiation --- */
88 typedef struct bar_ctx
{
94 static void *bar_init(opts
*o
)
96 bar_ctx
*c
= CREATE(bar_ctx
);
103 if (dh_parse(&qd
, &gp
))
104 die(1, "bad prime group: %s", qd
.e
);
106 if (!o
->fbits
) o
->fbits
= 1024;
107 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
109 mpbarrett_create(&c
->b
, gp
.p
);
112 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
113 for (i
= 0; i
< c
->n
; i
++) {
114 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
115 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
121 static void bar_run(void *cc
)
124 mp
*d
= mpbarrett_exp(&c
->b
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
128 static void barsim_run(void *cc
)
131 mp
*d
= mpbarrett_mexp(&c
->b
, MP_NEW
, c
->e
, c
->n
);
135 /* --- Montgomery exponentiation --- */
137 typedef struct mont_ctx
{
143 static void *mont_init(opts
*o
)
145 mont_ctx
*c
= CREATE(mont_ctx
);
152 if (dh_parse(&qd
, &gp
))
153 die(1, "bad prime group: %s", qd
.e
);
155 if (!o
->fbits
) o
->fbits
= 1024;
156 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
158 mpmont_create(&c
->m
, gp
.p
);
161 c
->e
= xmalloc(c
->n
* sizeof(mp_expfactor
));
162 for (i
= 0; i
< c
->n
; i
++) {
163 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
164 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
170 static void mont_run(void *cc
)
173 mp
*d
= mpmont_expr(&c
->m
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
177 static void montsim_run(void *cc
)
180 mp
*d
= mpmont_mexpr(&c
->m
, MP_NEW
, c
->e
, c
->n
);
184 /* --- Group exponentiation --- */
186 typedef struct gr_ctx
{
192 static void *grp_init(opts
*o
)
194 gr_ctx
*c
= CREATE(gr_ctx
);
202 if (dh_parse(&qd
, &gp
))
203 die(1, "bad prime group: %s", qd
.e
);
205 if (!o
->fbits
) o
->fbits
= 1024;
206 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
208 c
->g
= group_prime(&gp
);
209 if ((e
= G_CHECK(c
->g
, &rand_global
)) != 0)
210 die(1, "bad group: %s", e
);
213 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
214 for (i
= 0; i
< c
->n
; i
++) {
215 c
->e
[i
].base
= G_CREATE(c
->g
);
216 G_FROMINT(c
->g
, c
->e
[i
].base
,
217 mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0));
218 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
224 static void *grec_init(opts
*o
)
226 gr_ctx
*c
= CREATE(gr_ctx
);
233 die(1, "can't generate elliptic curves");
234 if ((e
= ec_getinfo(&ei
, o
->name
)) != 0)
235 die(1, "bad curve: %s", e
);
236 c
->g
= group_ec(&ei
);
237 if ((e
= G_CHECK(c
->g
, &rand_global
)) != 0)
238 die(1, "bad group: %s", e
);
241 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
242 for (i
= 0; i
< c
->n
; i
++) {
243 c
->e
[i
].base
= G_CREATE(c
->g
);
244 ec_rand(ei
.c
, &p
, &rand_global
);
245 G_FROMEC(c
->g
, c
->e
[i
].base
, &p
);
246 c
->e
[i
].exp
= mprand_range(MP_NEW
, ei
.r
, &rand_global
, 0);
252 static void gr_run(void *cc
)
255 ge
*x
= G_CREATE(c
->g
);
256 G_EXP(c
->g
, x
, c
->e
[0].base
, c
->e
[0].exp
);
260 static void grsim_run(void *cc
)
263 ge
*x
= G_CREATE(c
->g
);
264 G_MEXP(c
->g
, x
, c
->e
, c
->n
);
270 typedef struct rsapriv_ctx
{
276 static void *rsapriv_init(opts
*o
)
278 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
280 if (!o
->fbits
) o
->fbits
= 1024;
281 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
282 rsa_privcreate(&c
->rpc
, &c
->rp
, 0);
283 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
287 static void *rsaprivblind_init(opts
*o
)
289 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
291 if (!o
->fbits
) o
->fbits
= 1024;
292 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
293 rsa_privcreate(&c
->rpc
, &c
->rp
, fibrand_create(0));
294 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
298 static void rsapriv_run(void *cc
)
301 mp
*d
= rsa_privop(&c
->rpc
, MP_NEW
, c
->m
);
305 typedef struct rsapub_ctx
{
311 static void *rsapub_init(opts
*o
)
313 rsapub_ctx
*c
= CREATE(rsapub_ctx
);
316 if (!o
->fbits
) o
->fbits
= 1024;
317 rsa_gen(&rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
318 c
->rp
.n
= MP_COPY(rp
.n
);
319 c
->rp
.e
= MP_COPY(rp
.e
);
321 rsa_pubcreate(&c
->rpc
, &c
->rp
);
322 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
326 static void rsapub_run(void *cc
)
329 mp
*d
= rsa_pubop(&c
->rpc
, MP_NEW
, c
->m
);
333 /* --- Symmetric encryption --- */
335 typedef struct ksched_ctx
{
341 static void *ksched_init(opts
*o
)
343 ksched_ctx
*c
= CREATE(ksched_ctx
);
345 die(1, "must specify encryption scheme name");
346 if ((c
->c
= gcipher_byname(o
->name
)) == 0)
347 die(1, "encryption scheme `%s' not known", o
->name
);
348 c
->ksz
= keysz(o
->gbits
/8, c
->c
->keysz
);
349 c
->k
= xmalloc(c
->ksz
);
350 rand_get(RAND_GLOBAL
, c
->k
, c
->ksz
);
354 static void ksched_run(void *cc
)
357 gcipher
*gc
= GC_INIT(c
->c
, c
->k
, c
->ksz
);
361 typedef struct enc_ctx
{
368 static void *enc_init(opts
*o
)
370 enc_ctx
*c
= CREATE(enc_ctx
);
375 die(1, "must specify encryption scheme name");
376 if ((cc
= gcipher_byname(o
->name
)) == 0)
377 die(1, "encryption scheme `%s' not known", o
->name
);
378 ksz
= keysz(0, cc
->keysz
);
380 rand_get(RAND_GLOBAL
, k
, ksz
);
381 c
->c
= GC_INIT(cc
, k
, ksz
);
383 c
->sz
= o
->gbits ? o
->gbits
: 65536;
384 c
->n
= o
->n ? o
->n
: 16;
385 c
->m
= xmalloc(c
->sz
);
389 static void enc_run(void *cc
)
393 for (i
= 0; i
< c
->n
; i
++)
394 GC_ENCRYPT(c
->c
, c
->m
, c
->m
, c
->sz
);
397 /* --- Hashing --- */
399 typedef struct hash_ctx
{
406 static void *hash_init(opts
*o
)
408 hash_ctx
*c
= CREATE(hash_ctx
);
410 die(1, "must specify hash function name");
411 if ((c
->h
= ghash_byname(o
->name
)) == 0)
412 die(1, "hash function `%s' not known", o
->name
);
413 c
->sz
= o
->gbits ? o
->gbits
: 65536;
414 c
->n
= o
->n ? o
->n
: 16;
415 c
->m
= xmalloc(c
->sz
);
419 static void hash_run(void *cc
)
423 ghash
*h
= GH_INIT(c
->h
);
424 for (i
= 0; i
< c
->n
; i
++)
425 GH_HASH(h
, c
->m
, c
->sz
);
430 /* --- Job table --- */
432 typedef struct jobops
{
434 void *(*init
)(opts
*);
438 static const jobops jobtab
[] = {
439 { "g-prime-exp", grp_init
, gr_run
},
440 { "g-ec-mul", grec_init
, gr_run
},
441 { "g-prime-exp-sim", grp_init
, grsim_run
},
442 { "g-ec-mul-sim", grec_init
, grsim_run
},
443 { "barrett-exp", bar_init
, bar_run
},
444 { "barrett-exp-sim", bar_init
, barsim_run
},
445 { "mont-exp", mont_init
, mont_run
},
446 { "mont-exp-sim", mont_init
, montsim_run
},
447 { "rsa-priv", rsapriv_init
, rsapriv_run
},
448 { "rsa-priv-blind", rsaprivblind_init
, rsapriv_run
},
449 { "rsa-pub", rsapub_init
, rsapub_run
},
450 { "ksched", ksched_init
, ksched_run
},
451 { "enc", enc_init
, enc_run
},
452 { "hash", hash_init
, hash_run
},
456 /*----- Main code ---------------------------------------------------------*/
458 void version(FILE *fp
)
460 pquis(fp
, "$, Catacomb " VERSION
"\n");
463 static void usage(FILE *fp
)
465 pquis(fp
, "Usage: $ [-options] job\n");
468 static void help(FILE *fp
)
474 Various performance tests.\n\
478 -h, --help Show this help text.\n\
479 -v, --version Show program version number.\n\
480 -u, --usage Show terse usage message.\n\
481 -l, --list [ITEM...] List all the various names of things.\n\
483 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
484 -b, --field-bits Field size for g-prime and rsa.\n\
485 -B, --group-bits Group size for g-prime; key size for ksched;\n\
486 data size for enc and hash.\n\
487 -n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
488 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
489 -t, --time=TIME Length of an interval in seconds. [1]\n\
495 listtab[i].name, listtab[i].name) \
497 jobtab[i].name, jobtab[i].name) \
498 LI("Elliptic curves", ec, \
499 ectab[i].name, ectab[i].name) \
500 LI("Diffie-Hellman groups", dh, \
501 ptab[i].name, ptab[i].name) \
502 LI("Encryption algorithms", cipher, \
503 gciphertab[i], gciphertab[i]->name) \
504 LI("Hash functions", hash, \
505 ghashtab[i], ghashtab[i]->name)
507 MAKELISTTAB(listtab
, LISTS
)
509 static unsigned uarg(const char *what
, const char *p
)
514 u
= strtoul(p
, &q
, 0);
515 if (*q
|| u
> UINT_MAX
|| q
== p
|| errno
)
516 die(1, "bad %s `%s'", what
, p
);
520 static double farg(const char *what
, const char *p
)
526 if (*q
|| q
== p
|| errno
)
527 die(1, "bad %s `%s'", what
, p
);
531 int main(int argc
, char *argv
[])
536 struct timeval tv_next
, tv_now
;
540 clock_t c_start
, c_stop
;
547 static const struct option opts
[] = {
548 { "help", 0, 0, 'h' },
549 { "version", 0, 0, 'v' },
550 { "usage", 0, 0, 'u' },
551 { "list", 0, 0, 'l' },
552 { "name", OPTF_ARGREQ
, 0, 'C' },
553 { "field-bits", OPTF_ARGREQ
, 0, 'b' },
554 { "group-bits", OPTF_ARGREQ
, 0, 'B' },
555 { "factors", OPTF_ARGREQ
, 0, 'n' },
556 { "intervals", OPTF_ARGREQ
, 0, 'i' },
557 { "time", OPTF_ARGREQ
, 0, 't' },
561 i
= mdwopt(argc
, argv
, "hvulC:b:B:n:i:t:", opts
, 0, 0, 0);
564 case 'h': help(stdout
); exit(0);
565 case 'v': version(stdout
); exit(0);
566 case 'u': usage(stdout
); exit(0);
567 case 'l': exit(displaylists(listtab
, argv
+ optind
));
568 case 'C': o
.name
= optarg
; break;
569 case 'b': o
.fbits
= uarg("field bits", optarg
); break;
570 case 'B': o
.gbits
= uarg("subgroup bits", optarg
); break;
571 case 'n': o
.n
= uarg("factor count", optarg
); break;
572 case 'i': o
.i
= uarg("interval count", optarg
); break;
573 case 't': o
.t
= farg("interval length", optarg
); break;
574 default: usage(stderr
); exit(1);
577 if (optind
+ 1 != argc
) { usage(stderr
); exit(1); }
579 for (j
= jobtab
; j
->name
; j
++)
580 if (strcmp(j
->name
, argv
[optind
]) == 0) break;
581 if (!j
->name
) die(1, "unknown job type `%s'", argv
[optind
]);
586 gettimeofday(&tv_now
, 0);
588 tv_addl(&tv_next
, &tv_now
, o
.t
, fmod(o
.t
* MILLION
, MILLION
));
594 gettimeofday(&tv_now
, 0);
595 } while (TV_CMP(&tv_now
, <, &tv_next
));
597 t
= (double)(c_stop
- c_start
)/CLOCKS_PER_SEC
;
600 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
601 n
, ii
, ii
/t
, itot
/ttot
);
604 } while (!o
.i
|| n
< o
.i
);
609 /*----- That's all, folks -------------------------------------------------*/