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"
74 /*----- Options -----------------------------------------------------------*/
77 const char *name
; /* Pre-configured named thing */
78 unsigned fbits
; /* Field size bits */
79 unsigned gbits
; /* Group size bits */
80 unsigned n
; /* Number of factors */
81 unsigned i
; /* Number of intervals (or zero) */
82 double t
; /* Time for each interval (secs) */
83 unsigned f
; /* Flags */
84 #define OF_NOCHECK 1u /* Don't do group checking */
87 /*----- Job switch --------------------------------------------------------*/
89 /* --- Barrett exponentiation --- */
91 typedef struct bar_ctx
{
97 static void *bar_init(opts
*o
)
99 bar_ctx
*c
= CREATE(bar_ctx
);
106 if (dh_parse(&qd
, &gp
))
107 die(1, "bad prime group: %s", qd
.e
);
109 if (!o
->fbits
) o
->fbits
= 1024;
110 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
112 mpbarrett_create(&c
->b
, gp
.p
);
115 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
116 for (i
= 0; i
< c
->n
; i
++) {
117 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
118 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
124 static void bar_run(void *cc
)
127 mp
*d
= mpbarrett_exp(&c
->b
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
131 static void barsim_run(void *cc
)
134 mp
*d
= mpbarrett_mexp(&c
->b
, MP_NEW
, c
->e
, c
->n
);
138 /* --- Montgomery exponentiation --- */
140 typedef struct mont_ctx
{
146 static void *mont_init(opts
*o
)
148 mont_ctx
*c
= CREATE(mont_ctx
);
155 if (dh_parse(&qd
, &gp
))
156 die(1, "bad prime group: %s", qd
.e
);
158 if (!o
->fbits
) o
->fbits
= 1024;
159 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
161 mpmont_create(&c
->m
, gp
.p
);
164 c
->e
= xmalloc(c
->n
* sizeof(mp_expfactor
));
165 for (i
= 0; i
< c
->n
; i
++) {
166 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
167 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
173 static void mont_run(void *cc
)
176 mp
*d
= mpmont_expr(&c
->m
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
180 static void montsim_run(void *cc
)
183 mp
*d
= mpmont_mexpr(&c
->m
, MP_NEW
, c
->e
, c
->n
);
187 /* --- Group exponentiation --- */
189 typedef struct gr_ctx
{
195 static void *grp_init(opts
*o
)
197 gr_ctx
*c
= CREATE(gr_ctx
);
205 if (dh_parse(&qd
, &gp
))
206 die(1, "bad prime group: %s", qd
.e
);
208 if (!o
->fbits
) o
->fbits
= 1024;
209 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
211 c
->g
= group_prime(&gp
);
212 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
213 die(1, "bad group: %s", e
);
216 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
217 for (i
= 0; i
< c
->n
; i
++) {
218 c
->e
[i
].base
= G_CREATE(c
->g
);
219 G_FROMINT(c
->g
, c
->e
[i
].base
,
220 mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0));
221 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
227 static void *grec_init(opts
*o
)
229 gr_ctx
*c
= CREATE(gr_ctx
);
236 die(1, "can't generate elliptic curves");
237 if ((e
= ec_getinfo(&ei
, o
->name
)) != 0)
238 die(1, "bad curve: %s", e
);
239 c
->g
= group_ec(&ei
);
240 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
241 die(1, "bad group: %s", e
);
244 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
245 for (i
= 0; i
< c
->n
; i
++) {
246 c
->e
[i
].base
= G_CREATE(c
->g
);
247 ec_rand(ei
.c
, &p
, &rand_global
);
248 G_FROMEC(c
->g
, c
->e
[i
].base
, &p
);
249 c
->e
[i
].exp
= mprand_range(MP_NEW
, ei
.r
, &rand_global
, 0);
255 static void gr_run(void *cc
)
258 ge
*x
= G_CREATE(c
->g
);
259 G_EXP(c
->g
, x
, c
->e
[0].base
, c
->e
[0].exp
);
263 static void grsim_run(void *cc
)
266 ge
*x
= G_CREATE(c
->g
);
267 G_MEXP(c
->g
, x
, c
->e
, c
->n
);
273 typedef struct rsapriv_ctx
{
279 static void *rsapriv_init(opts
*o
)
281 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
283 if (!o
->fbits
) o
->fbits
= 1024;
284 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
285 rsa_privcreate(&c
->rpc
, &c
->rp
, 0);
286 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
290 static void *rsaprivblind_init(opts
*o
)
292 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
294 if (!o
->fbits
) o
->fbits
= 1024;
295 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
296 rsa_privcreate(&c
->rpc
, &c
->rp
, fibrand_create(0));
297 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
301 static void rsapriv_run(void *cc
)
304 mp
*d
= rsa_privop(&c
->rpc
, MP_NEW
, c
->m
);
308 typedef struct rsapub_ctx
{
314 static void *rsapub_init(opts
*o
)
316 rsapub_ctx
*c
= CREATE(rsapub_ctx
);
319 if (!o
->fbits
) o
->fbits
= 1024;
320 rsa_gen(&rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
321 c
->rp
.n
= MP_COPY(rp
.n
);
322 c
->rp
.e
= MP_COPY(rp
.e
);
324 rsa_pubcreate(&c
->rpc
, &c
->rp
);
325 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
329 static void rsapub_run(void *cc
)
332 mp
*d
= rsa_pubop(&c
->rpc
, MP_NEW
, c
->m
);
336 /* --- Symmetric encryption --- */
338 typedef struct ksched_ctx
{
344 static void *ksched_init(opts
*o
)
346 ksched_ctx
*c
= CREATE(ksched_ctx
);
348 die(1, "must specify encryption scheme name");
349 if ((c
->c
= gcipher_byname(o
->name
)) == 0)
350 die(1, "encryption scheme `%s' not known", o
->name
);
351 c
->ksz
= keysz(o
->gbits
/8, c
->c
->keysz
);
352 c
->k
= xmalloc(c
->ksz
);
353 rand_get(RAND_GLOBAL
, c
->k
, c
->ksz
);
357 static void ksched_run(void *cc
)
360 gcipher
*gc
= GC_INIT(c
->c
, c
->k
, c
->ksz
);
364 typedef struct enc_ctx
{
371 static void *enc_init(opts
*o
)
373 enc_ctx
*c
= CREATE(enc_ctx
);
378 die(1, "must specify encryption scheme name");
379 if ((cc
= gcipher_byname(o
->name
)) == 0)
380 die(1, "encryption scheme `%s' not known", o
->name
);
381 ksz
= keysz(0, cc
->keysz
);
383 rand_get(RAND_GLOBAL
, k
, ksz
);
384 c
->c
= GC_INIT(cc
, k
, ksz
);
386 c
->sz
= o
->gbits ? o
->gbits
: 65536;
387 c
->n
= o
->n ? o
->n
: 16;
388 c
->m
= xmalloc(c
->sz
);
392 static void enc_run(void *cc
)
396 for (i
= 0; i
< c
->n
; i
++)
397 GC_ENCRYPT(c
->c
, c
->m
, c
->m
, c
->sz
);
400 /* --- Hashing --- */
402 typedef struct hash_ctx
{
409 static void *hash_init(opts
*o
)
411 hash_ctx
*c
= CREATE(hash_ctx
);
413 die(1, "must specify hash function name");
414 if ((c
->h
= ghash_byname(o
->name
)) == 0)
415 die(1, "hash function `%s' not known", o
->name
);
416 c
->sz
= o
->gbits ? o
->gbits
: 65536;
417 c
->n
= o
->n ? o
->n
: 16;
418 c
->m
= xmalloc(c
->sz
);
422 static void hash_run(void *cc
)
426 ghash
*h
= GH_INIT(c
->h
);
427 for (i
= 0; i
< c
->n
; i
++)
428 GH_HASH(h
, c
->m
, c
->sz
);
433 /* --- Job table --- */
435 typedef struct jobops
{
437 void *(*init
)(opts
*);
441 static const jobops jobtab
[] = {
442 { "g-prime-exp", grp_init
, gr_run
},
443 { "g-ec-mul", grec_init
, gr_run
},
444 { "g-prime-exp-sim", grp_init
, grsim_run
},
445 { "g-ec-mul-sim", grec_init
, grsim_run
},
446 { "barrett-exp", bar_init
, bar_run
},
447 { "barrett-exp-sim", bar_init
, barsim_run
},
448 { "mont-exp", mont_init
, mont_run
},
449 { "mont-exp-sim", mont_init
, montsim_run
},
450 { "rsa-priv", rsapriv_init
, rsapriv_run
},
451 { "rsa-priv-blind", rsaprivblind_init
, rsapriv_run
},
452 { "rsa-pub", rsapub_init
, rsapub_run
},
453 { "ksched", ksched_init
, ksched_run
},
454 { "enc", enc_init
, enc_run
},
455 { "hash", hash_init
, hash_run
},
459 /*----- Main code ---------------------------------------------------------*/
461 void version(FILE *fp
)
463 pquis(fp
, "$, Catacomb " VERSION
"\n");
466 static void usage(FILE *fp
)
468 pquis(fp
, "Usage: $ [-options] job\n");
471 static void help(FILE *fp
)
477 Various performance tests.\n\
481 -h, --help Show this help text.\n\
482 -v, --version Show program version number.\n\
483 -u, --usage Show terse usage message.\n\
484 -l, --list [ITEM...] List all the various names of things.\n\
486 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
487 -b, --field-bits Field size for g-prime and rsa.\n\
488 -q, --no-check Don't check field/group for validity.\n\
489 -B, --group-bits Group size for g-prime; key size for ksched;\n\
490 data size for enc and hash.\n\
491 -n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
492 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
493 -t, --time=TIME Length of an interval in seconds. [1]\n\
499 listtab[i].name, listtab[i].name) \
501 jobtab[i].name, jobtab[i].name) \
502 LI("Elliptic curves", ec, \
503 ectab[i].name, ectab[i].name) \
504 LI("Diffie-Hellman groups", dh, \
505 ptab[i].name, ptab[i].name) \
506 LI("Encryption algorithms", cipher, \
507 gciphertab[i], gciphertab[i]->name) \
508 LI("Hash functions", hash, \
509 ghashtab[i], ghashtab[i]->name)
511 MAKELISTTAB(listtab
, LISTS
)
513 static unsigned uarg(const char *what
, const char *p
)
518 u
= strtoul(p
, &q
, 0);
519 if (*q
|| u
> UINT_MAX
|| q
== p
|| errno
)
520 die(1, "bad %s `%s'", what
, p
);
524 static double farg(const char *what
, const char *p
)
530 if (*q
|| q
== p
|| errno
)
531 die(1, "bad %s `%s'", what
, p
);
535 int main(int argc
, char *argv
[])
540 struct timeval tv_next
, tv_now
;
544 clock_t c_start
, c_stop
;
551 static const struct option opts
[] = {
552 { "help", 0, 0, 'h' },
553 { "version", 0, 0, 'v' },
554 { "usage", 0, 0, 'u' },
555 { "list", 0, 0, 'l' },
556 { "name", OPTF_ARGREQ
, 0, 'C' },
557 { "field-bits", OPTF_ARGREQ
, 0, 'b' },
558 { "group-bits", OPTF_ARGREQ
, 0, 'B' },
559 { "factors", OPTF_ARGREQ
, 0, 'n' },
560 { "intervals", OPTF_ARGREQ
, 0, 'i' },
561 { "time", OPTF_ARGREQ
, 0, 't' },
562 { "no-check", 0, 0, 'q' },
566 i
= mdwopt(argc
, argv
, "hvulC:b:B:n:i:t:q", opts
, 0, 0, 0);
569 case 'h': help(stdout
); exit(0);
570 case 'v': version(stdout
); exit(0);
571 case 'u': usage(stdout
); exit(0);
572 case 'l': exit(displaylists(listtab
, argv
+ optind
));
573 case 'C': o
.name
= optarg
; break;
574 case 'b': o
.fbits
= uarg("field bits", optarg
); break;
575 case 'B': o
.gbits
= uarg("subgroup bits", optarg
); break;
576 case 'n': o
.n
= uarg("factor count", optarg
); break;
577 case 'i': o
.i
= uarg("interval count", optarg
); break;
578 case 't': o
.t
= farg("interval length", optarg
); break;
579 case 'q': o
.f
|= OF_NOCHECK
; break;
580 default: usage(stderr
); exit(1);
583 if (optind
+ 1 != argc
) { usage(stderr
); exit(1); }
585 for (j
= jobtab
; j
->name
; j
++)
586 if (strcmp(j
->name
, argv
[optind
]) == 0) break;
587 if (!j
->name
) die(1, "unknown job type `%s'", argv
[optind
]);
592 gettimeofday(&tv_now
, 0);
594 tv_addl(&tv_next
, &tv_now
, o
.t
, fmod(o
.t
* MILLION
, MILLION
));
600 gettimeofday(&tv_now
, 0);
601 } while (TV_CMP(&tv_now
, <, &tv_next
));
603 t
= (double)(c_stop
- c_start
)/CLOCKS_PER_SEC
;
606 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
607 n
, ii
, ii
/t
, itot
/ttot
);
610 } while (!o
.i
|| n
< o
.i
);
615 /*----- That's all, folks -------------------------------------------------*/