4b8d1583fd9df80a7e79ffe1c6bd57ab6a44d848
3 * $Id: perftest.c,v 1.1 2004/04/21 00:37:32 mdw Exp $
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"
70 /*----- Options -----------------------------------------------------------*/
73 const char *name
; /* Pre-configured named thing */
74 unsigned fbits
; /* Field size bits */
75 unsigned gbits
; /* Group size bits */
76 unsigned n
; /* Number of factors */
77 unsigned i
; /* Number of intervals (or zero) */
78 double t
; /* Time for each interval (secs) */
81 /*----- Job switch --------------------------------------------------------*/
83 /* --- Barrett exponentiation --- */
85 typedef struct bar_ctx
{
91 static void *bar_init(opts
*o
)
93 bar_ctx
*c
= CREATE(bar_ctx
);
100 if (dh_parse(&qd
, &gp
))
101 die(1, "bad prime group: %s", qd
.e
);
103 if (!o
->fbits
) o
->fbits
= 1024;
104 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
106 mpbarrett_create(&c
->b
, gp
.p
);
109 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
110 for (i
= 0; i
< c
->n
; i
++) {
111 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
112 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
118 static void bar_run(void *cc
)
121 mp
*d
= mpbarrett_exp(&c
->b
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
125 static void barsim_run(void *cc
)
128 mp
*d
= mpbarrett_mexp(&c
->b
, MP_NEW
, c
->e
, c
->n
);
132 /* --- Montgomery exponentiation --- */
134 typedef struct mont_ctx
{
140 static void *mont_init(opts
*o
)
142 mont_ctx
*c
= CREATE(mont_ctx
);
149 if (dh_parse(&qd
, &gp
))
150 die(1, "bad prime group: %s", qd
.e
);
152 if (!o
->fbits
) o
->fbits
= 1024;
153 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
155 mpmont_create(&c
->m
, gp
.p
);
158 c
->e
= xmalloc(c
->n
* sizeof(mp_expfactor
));
159 for (i
= 0; i
< c
->n
; i
++) {
160 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
161 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
167 static void mont_run(void *cc
)
170 mp
*d
= mpmont_expr(&c
->m
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
174 static void montsim_run(void *cc
)
177 mp
*d
= mpmont_mexpr(&c
->m
, MP_NEW
, c
->e
, c
->n
);
181 /* --- Group exponentiation --- */
183 typedef struct gr_ctx
{
189 static void *grp_init(opts
*o
)
191 gr_ctx
*c
= CREATE(gr_ctx
);
199 if (dh_parse(&qd
, &gp
))
200 die(1, "bad prime group: %s", qd
.e
);
202 if (!o
->fbits
) o
->fbits
= 1024;
203 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
205 c
->g
= group_prime(&gp
);
206 if ((e
= G_CHECK(c
->g
, &rand_global
)) != 0)
207 die(1, "bad group: %s", e
);
210 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
211 for (i
= 0; i
< c
->n
; i
++) {
212 c
->e
[i
].base
= G_CREATE(c
->g
);
213 G_FROMINT(c
->g
, c
->e
[i
].base
,
214 mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0));
215 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
221 static void *grec_init(opts
*o
)
223 gr_ctx
*c
= CREATE(gr_ctx
);
230 die(1, "can't generate elliptic curves");
231 if ((e
= ec_getinfo(&ei
, o
->name
)) != 0)
232 die(1, "bad curve: %s", e
);
233 c
->g
= group_ec(&ei
);
234 if ((e
= G_CHECK(c
->g
, &rand_global
)) != 0)
235 die(1, "bad group: %s", e
);
238 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
239 for (i
= 0; i
< c
->n
; i
++) {
240 c
->e
[i
].base
= G_CREATE(c
->g
);
241 ec_rand(ei
.c
, &p
, &rand_global
);
242 G_FROMEC(c
->g
, c
->e
[i
].base
, &p
);
243 c
->e
[i
].exp
= mprand_range(MP_NEW
, ei
.r
, &rand_global
, 0);
249 static void gr_run(void *cc
)
252 ge
*x
= G_CREATE(c
->g
);
253 G_EXP(c
->g
, x
, c
->e
[0].base
, c
->e
[0].exp
);
257 static void grsim_run(void *cc
)
260 ge
*x
= G_CREATE(c
->g
);
261 G_MEXP(c
->g
, x
, c
->e
, c
->n
);
267 typedef struct rsapriv_ctx
{
273 static void *rsapriv_init(opts
*o
)
275 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
277 if (!o
->fbits
) o
->fbits
= 1024;
278 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
279 rsa_privcreate(&c
->rpc
, &c
->rp
, 0);
280 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
284 static void *rsaprivblind_init(opts
*o
)
286 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
288 if (!o
->fbits
) o
->fbits
= 1024;
289 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
290 rsa_privcreate(&c
->rpc
, &c
->rp
, fibrand_create(0));
291 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
295 static void rsapriv_run(void *cc
)
298 mp
*d
= rsa_privop(&c
->rpc
, MP_NEW
, c
->m
);
302 typedef struct rsapub_ctx
{
308 static void *rsapub_init(opts
*o
)
310 rsapub_ctx
*c
= CREATE(rsapub_ctx
);
313 if (!o
->fbits
) o
->fbits
= 1024;
314 rsa_gen(&rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
315 c
->rp
.n
= MP_COPY(rp
.n
);
316 c
->rp
.e
= MP_COPY(rp
.e
);
318 rsa_pubcreate(&c
->rpc
, &c
->rp
);
319 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
323 static void rsapub_run(void *cc
)
326 mp
*d
= rsa_pubop(&c
->rpc
, MP_NEW
, c
->m
);
330 /* --- Symmetric encryption --- */
332 typedef struct ksched_ctx
{
338 static void *ksched_init(opts
*o
)
340 ksched_ctx
*c
= CREATE(ksched_ctx
);
342 die(1, "must specify encryption scheme name");
343 if ((c
->c
= gcipher_byname(o
->name
)) == 0)
344 die(1, "encryption scheme `%s' not known", o
->name
);
345 c
->ksz
= keysz(o
->gbits
/8, c
->c
->keysz
);
346 c
->k
= xmalloc(c
->ksz
);
347 rand_get(RAND_GLOBAL
, c
->k
, c
->ksz
);
351 static void ksched_run(void *cc
)
354 gcipher
*gc
= GC_INIT(c
->c
, c
->k
, c
->ksz
);
358 typedef struct enc_ctx
{
365 static void *enc_init(opts
*o
)
367 enc_ctx
*c
= CREATE(enc_ctx
);
372 die(1, "must specify encryption scheme name");
373 if ((cc
= gcipher_byname(o
->name
)) == 0)
374 die(1, "encryption scheme `%s' not known", o
->name
);
375 ksz
= keysz(0, cc
->keysz
);
377 rand_get(RAND_GLOBAL
, k
, ksz
);
378 c
->c
= GC_INIT(cc
, k
, ksz
);
380 c
->sz
= o
->gbits ? o
->gbits
: 65536;
381 c
->n
= o
->n ? o
->n
: 16;
382 c
->m
= xmalloc(c
->sz
);
386 static void enc_run(void *cc
)
390 for (i
= 0; i
< c
->n
; i
++)
391 GC_ENCRYPT(c
->c
, c
->m
, c
->m
, c
->sz
);
394 /* --- Hashing --- */
396 typedef struct hash_ctx
{
403 static void *hash_init(opts
*o
)
405 hash_ctx
*c
= CREATE(hash_ctx
);
407 die(1, "must specify hash function name");
408 if ((c
->h
= ghash_byname(o
->name
)) == 0)
409 die(1, "hash function `%s' not known", o
->name
);
410 c
->sz
= o
->gbits ? o
->gbits
: 65536;
411 c
->n
= o
->n ? o
->n
: 16;
412 c
->m
= xmalloc(c
->sz
);
416 static void hash_run(void *cc
)
420 ghash
*h
= GH_INIT(c
->h
);
421 for (i
= 0; i
< c
->n
; i
++)
422 GH_HASH(h
, c
->m
, c
->sz
);
427 /* --- Job table --- */
429 typedef struct jobobs
{
431 void *(*init
)(opts
*);
435 static const jobops jobtab
[] = {
436 { "g-prime-exp", grp_init
, gr_run
},
437 { "g-ec-mul", grec_init
, gr_run
},
438 { "g-prime-exp-sim", grp_init
, grsim_run
},
439 { "g-ec-mul-sim", grec_init
, grsim_run
},
440 { "barrett-exp", bar_init
, bar_run
},
441 { "barrett-exp-sim", bar_init
, barsim_run
},
442 { "mont-exp", mont_init
, mont_run
},
443 { "mont-exp-sim", mont_init
, montsim_run
},
444 { "rsa-priv", rsapriv_init
, rsapriv_run
},
445 { "rsa-priv-blind", rsaprivblind_init
, rsapriv_run
},
446 { "rsa-pub", rsapub_init
, rsapub_run
},
447 { "ksched", ksched_init
, ksched_run
},
448 { "enc", enc_init
, enc_run
},
449 { "hash", hash_init
, hash_run
},
453 /*----- Main code ---------------------------------------------------------*/
455 static void version(FILE *fp
)
457 pquis(fp
, "$, Catacomb " VERSION
"\n");
460 static void usage(FILE *fp
)
462 pquis(fp
, "Usage: $ [-options] job\n");
465 static void help(FILE *fp
)
471 Various performance tests.\n\
475 static unsigned uarg(const char *what
, const char *p
)
480 u
= strtoul(p
, &q
, 0);
481 if (*q
|| u
> UINT_MAX
|| q
== p
|| errno
)
482 die(1, "bad %s `%s'", what
, p
);
486 static double farg(const char *what
, const char *p
)
492 if (*q
|| q
== p
|| errno
)
493 die(1, "bad %s `%s'", what
, p
);
497 int main(int argc
, char *argv
[])
502 struct timeval tv_next
, tv_now
;
506 clock_t c_start
, c_stop
;
513 static const struct option opts
[] = {
514 { "help", 0, 0, 'h' },
515 { "version", 0, 0, 'v' },
516 { "usage", 0, 0, 'u' },
517 { "name", OPTF_ARGREQ
, 0, 'C' },
518 { "field-bits", OPTF_ARGREQ
, 0, 'b' },
519 { "group-bits", OPTF_ARGREQ
, 0, 'B' },
520 { "factors", OPTF_ARGREQ
, 0, 'n' },
521 { "intervals", OPTF_ARGREQ
, 0, 'i' },
522 { "time", OPTF_ARGREQ
, 0, 't' },
526 i
= mdwopt(argc
, argv
, "hvuC:b:B:n:i:t:", opts
, 0, 0, 0);
529 case 'h': help(stdout
); exit(0);
530 case 'v': version(stdout
); exit(0);
531 case 'u': usage(stdout
); exit(0);
532 case 'C': o
.name
= optarg
; break;
533 case 'b': o
.fbits
= uarg("field bits", optarg
); break;
534 case 'B': o
.gbits
= uarg("subgroup bits", optarg
); break;
535 case 'n': o
.n
= uarg("factor count", optarg
); break;
536 case 'i': o
.i
= uarg("interval count", optarg
); break;
537 case 't': o
.t
= farg("interval length", optarg
); break;
538 default: usage(stderr
); exit(1);
541 if (optind
+ 1 != argc
) { usage(stderr
); exit(1); }
543 for (j
= jobtab
; j
->name
; j
++)
544 if (strcmp(j
->name
, argv
[optind
]) == 0) break;
545 if (!j
->name
) die(1, "unknown job type `%s'", argv
[optind
]);
550 gettimeofday(&tv_now
, 0);
552 tv_addl(&tv_next
, &tv_now
, o
.t
, fmod(o
.t
* MILLION
, MILLION
));
558 gettimeofday(&tv_now
, 0);
559 } while (TV_CMP(&tv_now
, <, &tv_next
));
561 t
= (double)(c_stop
- c_start
)/CLOCKS_PER_SEC
;
564 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
565 n
, ii
, ii
/t
, itot
/ttot
);
568 } while (!o
.i
|| n
< o
.i
);
573 /*----- That's all, folks -------------------------------------------------*/