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"
77 /*----- Options -----------------------------------------------------------*/
80 const char *name
; /* Pre-configured named thing */
81 unsigned fbits
; /* Field size bits */
82 unsigned gbits
; /* Group size bits */
83 unsigned n
; /* Number of factors */
84 unsigned i
; /* Number of intervals (or zero) */
85 double t
; /* Time for each interval (secs) */
86 unsigned f
; /* Flags */
87 #define OF_NOCHECK 1u /* Don't do group checking */
90 /*----- Job switch --------------------------------------------------------*/
92 /* --- Barrett exponentiation --- */
94 typedef struct bar_ctx
{
100 static void *bar_init(opts
*o
)
102 bar_ctx
*c
= CREATE(bar_ctx
);
109 if (dh_parse(&qd
, &gp
))
110 die(1, "bad prime group: %s", qd
.e
);
112 if (!o
->fbits
) o
->fbits
= 1024;
113 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
115 mpbarrett_create(&c
->b
, gp
.p
);
118 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
119 for (i
= 0; i
< c
->n
; i
++) {
120 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
121 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
127 static void bar_run(void *cc
)
130 mp
*d
= mpbarrett_exp(&c
->b
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
134 static void barsim_run(void *cc
)
137 mp
*d
= mpbarrett_mexp(&c
->b
, MP_NEW
, c
->e
, c
->n
);
141 /* --- Montgomery exponentiation --- */
143 typedef struct mont_ctx
{
149 static void *mont_init(opts
*o
)
151 mont_ctx
*c
= CREATE(mont_ctx
);
158 if (dh_parse(&qd
, &gp
))
159 die(1, "bad prime group: %s", qd
.e
);
161 if (!o
->fbits
) o
->fbits
= 1024;
162 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
164 mpmont_create(&c
->m
, gp
.p
);
167 c
->e
= xmalloc(c
->n
* sizeof(mp_expfactor
));
168 for (i
= 0; i
< c
->n
; i
++) {
169 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
170 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
176 static void mont_run(void *cc
)
179 mp
*d
= mpmont_expr(&c
->m
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
183 static void montsim_run(void *cc
)
186 mp
*d
= mpmont_mexpr(&c
->m
, MP_NEW
, c
->e
, c
->n
);
190 /* --- Group exponentiation --- */
192 typedef struct gr_ctx
{
198 static void *grp_init(opts
*o
)
200 gr_ctx
*c
= CREATE(gr_ctx
);
208 if (dh_parse(&qd
, &gp
))
209 die(1, "bad prime group: %s", qd
.e
);
211 if (!o
->fbits
) o
->fbits
= 1024;
212 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
214 c
->g
= group_prime(&gp
);
215 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
216 die(1, "bad group: %s", e
);
219 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
220 for (i
= 0; i
< c
->n
; i
++) {
221 c
->e
[i
].base
= G_CREATE(c
->g
);
222 G_FROMINT(c
->g
, c
->e
[i
].base
,
223 mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0));
224 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
230 static void *grec_init(opts
*o
)
232 gr_ctx
*c
= CREATE(gr_ctx
);
239 die(1, "can't generate elliptic curves");
240 if ((e
= ec_getinfo(&ei
, o
->name
)) != 0)
241 die(1, "bad curve: %s", e
);
242 c
->g
= group_ec(&ei
);
243 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
244 die(1, "bad group: %s", e
);
247 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
248 for (i
= 0; i
< c
->n
; i
++) {
249 c
->e
[i
].base
= G_CREATE(c
->g
);
250 ec_rand(ei
.c
, &p
, &rand_global
);
251 G_FROMEC(c
->g
, c
->e
[i
].base
, &p
);
252 c
->e
[i
].exp
= mprand_range(MP_NEW
, ei
.r
, &rand_global
, 0);
258 static void gr_run(void *cc
)
261 ge
*x
= G_CREATE(c
->g
);
262 G_EXP(c
->g
, x
, c
->e
[0].base
, c
->e
[0].exp
);
266 static void grsim_run(void *cc
)
269 ge
*x
= G_CREATE(c
->g
);
270 G_MEXP(c
->g
, x
, c
->e
, c
->n
);
276 typedef struct x25519_jobctx
{
277 octet k
[X25519_KEYSZ
];
278 octet p
[X25519_PUBSZ
];
281 static void *x25519_jobinit(opts
*o
)
283 x25519_jobctx
*c
= CREATE(x25519_jobctx
);
284 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
285 rand_get(RAND_GLOBAL
, c
->p
, sizeof(c
->p
));
289 static void x25519_jobrun(void *cc
)
290 { x25519_jobctx
*c
= cc
; octet z
[X25519_OUTSZ
]; x25519(z
, c
->k
, c
->p
); }
292 /* --- Ed25519 --- */
294 typedef struct ed25519_signctx
{
295 octet k
[ED25519_KEYSZ
];
296 octet K
[ED25519_PUBSZ
];
300 typedef struct ed25519_vrfctx
{
301 octet K
[ED25519_PUBSZ
];
303 octet sig
[ED25519_SIGSZ
];
306 static void *ed25519_signinit(opts
*o
)
308 ed25519_signctx
*c
= CREATE(ed25519_signctx
);
310 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
311 rand_get(RAND_GLOBAL
, c
->m
, sizeof(c
->m
));
312 ed25519_pubkey(c
->K
, c
->k
, sizeof(c
->k
));
316 static void ed25519_signrun(void *cc
)
318 ed25519_signctx
*c
= cc
;
319 octet sig
[ED25519_SIGSZ
];
321 ed25519_sign(sig
, c
->k
, sizeof(c
->k
), c
->K
, c
->m
, sizeof(c
->m
));
324 static void *ed25519_vrfinit(opts
*o
)
326 octet k
[ED25519_KEYSZ
];
327 ed25519_vrfctx
*c
= CREATE(ed25519_vrfctx
);
329 rand_get(RAND_GLOBAL
, k
, sizeof(k
));
330 rand_get(RAND_GLOBAL
, c
->m
, sizeof(c
->m
));
331 ed25519_pubkey(c
->K
, k
, sizeof(k
));
332 ed25519_sign(c
->sig
, k
, sizeof(k
), c
->K
, c
->m
, sizeof(c
->m
));
336 static void ed25519_vrfrun(void *cc
)
338 ed25519_vrfctx
*c
= cc
;
339 ed25519_verify(c
->K
, c
->m
, sizeof(c
->m
), c
->sig
);
344 typedef struct rsapriv_ctx
{
350 static void *rsapriv_init(opts
*o
)
352 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
354 if (!o
->fbits
) o
->fbits
= 1024;
355 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
356 rsa_privcreate(&c
->rpc
, &c
->rp
, 0);
357 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
361 static void *rsaprivblind_init(opts
*o
)
363 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
365 if (!o
->fbits
) o
->fbits
= 1024;
366 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
367 rsa_privcreate(&c
->rpc
, &c
->rp
, fibrand_create(0));
368 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
372 static void rsapriv_run(void *cc
)
375 mp
*d
= rsa_privop(&c
->rpc
, MP_NEW
, c
->m
);
379 typedef struct rsapub_ctx
{
385 static void *rsapub_init(opts
*o
)
387 rsapub_ctx
*c
= CREATE(rsapub_ctx
);
390 if (!o
->fbits
) o
->fbits
= 1024;
391 rsa_gen(&rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
392 c
->rp
.n
= MP_COPY(rp
.n
);
393 c
->rp
.e
= MP_COPY(rp
.e
);
395 rsa_pubcreate(&c
->rpc
, &c
->rp
);
396 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
400 static void rsapub_run(void *cc
)
403 mp
*d
= rsa_pubop(&c
->rpc
, MP_NEW
, c
->m
);
407 /* --- Symmetric encryption --- */
409 typedef struct ksched_ctx
{
415 static void *ksched_init(opts
*o
)
417 ksched_ctx
*c
= CREATE(ksched_ctx
);
419 die(1, "must specify encryption scheme name");
420 if ((c
->c
= gcipher_byname(o
->name
)) == 0)
421 die(1, "encryption scheme `%s' not known", o
->name
);
422 c
->ksz
= keysz(o
->gbits
/8, c
->c
->keysz
);
423 c
->k
= xmalloc(c
->ksz
);
424 rand_get(RAND_GLOBAL
, c
->k
, c
->ksz
);
428 static void ksched_run(void *cc
)
431 gcipher
*gc
= GC_INIT(c
->c
, c
->k
, c
->ksz
);
435 typedef struct enc_ctx
{
442 static void *enc_init(opts
*o
)
444 enc_ctx
*c
= CREATE(enc_ctx
);
449 die(1, "must specify encryption scheme name");
450 if ((cc
= gcipher_byname(o
->name
)) == 0)
451 die(1, "encryption scheme `%s' not known", o
->name
);
452 ksz
= keysz(0, cc
->keysz
);
454 rand_get(RAND_GLOBAL
, k
, ksz
);
455 c
->c
= GC_INIT(cc
, k
, ksz
);
457 c
->sz
= o
->gbits ? o
->gbits
: 65536;
458 c
->n
= o
->n ? o
->n
: 16;
459 c
->m
= xmalloc(c
->sz
);
463 static void enc_run(void *cc
)
467 for (i
= 0; i
< c
->n
; i
++)
468 GC_ENCRYPT(c
->c
, c
->m
, c
->m
, c
->sz
);
471 /* --- Hashing --- */
473 typedef struct hash_ctx
{
480 static void *hash_init(opts
*o
)
482 hash_ctx
*c
= CREATE(hash_ctx
);
484 die(1, "must specify hash function name");
485 if ((c
->h
= ghash_byname(o
->name
)) == 0)
486 die(1, "hash function `%s' not known", o
->name
);
487 c
->sz
= o
->gbits ? o
->gbits
: 65536;
488 c
->n
= o
->n ? o
->n
: 16;
489 c
->m
= xmalloc(c
->sz
);
493 static void hash_run(void *cc
)
497 ghash
*h
= GH_INIT(c
->h
);
498 for (i
= 0; i
< c
->n
; i
++)
499 GH_HASH(h
, c
->m
, c
->sz
);
504 /* --- Poly1305 --- */
506 typedef struct poly1305_jobctx
{
508 octet s
[POLY1305_MASKSZ
];
514 static void *poly1305_jobinit(opts
*o
)
516 octet k
[POLY1305_KEYSZ
];
517 poly1305_jobctx
*c
= CREATE(poly1305_jobctx
);
518 rand_get(RAND_GLOBAL
, k
, sizeof(k
));
519 poly1305_keyinit(&c
->k
, k
, sizeof(k
));
520 rand_get(RAND_GLOBAL
, c
->s
, sizeof(c
->s
));
521 c
->sz
= o
->gbits ? o
->gbits
: 65536;
522 c
->n
= o
->n ? o
->n
: 16;
523 c
->m
= xmalloc(c
->sz
);
527 static void poly1305_jobrun(void *cc
)
529 poly1305_jobctx
*c
= cc
;
531 octet t
[POLY1305_TAGSZ
];
533 poly1305_macinit(&ctx
, &c
->k
, c
->s
);
534 for (i
= 0; i
< c
->n
; i
++) poly1305_hash(&ctx
, c
->m
, c
->sz
);
535 poly1305_done(&ctx
, t
);
538 /* --- Job table --- */
540 typedef struct jobops
{
542 void *(*init
)(opts
*);
546 static const jobops jobtab
[] = {
547 { "g-prime-exp", grp_init
, gr_run
},
548 { "g-ec-mul", grec_init
, gr_run
},
549 { "g-prime-exp-sim", grp_init
, grsim_run
},
550 { "g-ec-mul-sim", grec_init
, grsim_run
},
551 { "barrett-exp", bar_init
, bar_run
},
552 { "barrett-exp-sim", bar_init
, barsim_run
},
553 { "mont-exp", mont_init
, mont_run
},
554 { "mont-exp-sim", mont_init
, montsim_run
},
555 { "rsa-priv", rsapriv_init
, rsapriv_run
},
556 { "rsa-priv-blind", rsaprivblind_init
, rsapriv_run
},
557 { "rsa-pub", rsapub_init
, rsapub_run
},
558 { "x25519", x25519_jobinit
, x25519_jobrun
},
559 { "ed25519-sign", ed25519_signinit
, ed25519_signrun
},
560 { "ed25519-vrf", ed25519_vrfinit
, ed25519_vrfrun
},
561 { "ksched", ksched_init
, ksched_run
},
562 { "enc", enc_init
, enc_run
},
563 { "hash", hash_init
, hash_run
},
564 { "poly1305", poly1305_jobinit
, poly1305_jobrun
},
568 /*----- Main code ---------------------------------------------------------*/
570 void version(FILE *fp
)
572 pquis(fp
, "$, Catacomb " VERSION
"\n");
575 static void usage(FILE *fp
)
577 pquis(fp
, "Usage: $ [-options] job\n");
580 static void help(FILE *fp
)
586 Various performance tests.\n\
590 -h, --help Show this help text.\n\
591 -v, --version Show program version number.\n\
592 -u, --usage Show terse usage message.\n\
593 -l, --list [ITEM...] List all the various names of things.\n\
595 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
596 -b, --field-bits Field size for g-prime and rsa.\n\
597 -q, --no-check Don't check field/group for validity.\n\
598 -B, --group-bits Group size for g-prime; key size for ksched;\n\
599 data size for enc and hash.\n\
600 -n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
601 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
602 -t, --time=TIME Length of an interval in seconds. [1]\n\
608 listtab[i].name, listtab[i].name) \
610 jobtab[i].name, jobtab[i].name) \
611 LI("Elliptic curves", ec, \
612 ectab[i].name, ectab[i].name) \
613 LI("Diffie-Hellman groups", dh, \
614 ptab[i].name, ptab[i].name) \
615 LI("Encryption algorithms", cipher, \
616 gciphertab[i], gciphertab[i]->name) \
617 LI("Hash functions", hash, \
618 ghashtab[i], ghashtab[i]->name)
620 MAKELISTTAB(listtab
, LISTS
)
622 static unsigned uarg(const char *what
, const char *p
)
627 u
= strtoul(p
, &q
, 0);
628 if (*q
|| u
> UINT_MAX
|| q
== p
|| errno
)
629 die(1, "bad %s `%s'", what
, p
);
633 static double farg(const char *what
, const char *p
)
639 if (*q
|| q
== p
|| errno
)
640 die(1, "bad %s `%s'", what
, p
);
644 int main(int argc
, char *argv
[])
649 struct timeval tv_next
, tv_now
;
653 clock_t c_start
, c_stop
;
660 static const struct option opts
[] = {
661 { "help", 0, 0, 'h' },
662 { "version", 0, 0, 'v' },
663 { "usage", 0, 0, 'u' },
664 { "list", 0, 0, 'l' },
665 { "name", OPTF_ARGREQ
, 0, 'C' },
666 { "field-bits", OPTF_ARGREQ
, 0, 'b' },
667 { "group-bits", OPTF_ARGREQ
, 0, 'B' },
668 { "factors", OPTF_ARGREQ
, 0, 'n' },
669 { "intervals", OPTF_ARGREQ
, 0, 'i' },
670 { "time", OPTF_ARGREQ
, 0, 't' },
671 { "no-check", 0, 0, 'q' },
675 i
= mdwopt(argc
, argv
, "hvulC:b:B:n:i:t:q", opts
, 0, 0, 0);
678 case 'h': help(stdout
); exit(0);
679 case 'v': version(stdout
); exit(0);
680 case 'u': usage(stdout
); exit(0);
681 case 'l': exit(displaylists(listtab
, argv
+ optind
));
682 case 'C': o
.name
= optarg
; break;
683 case 'b': o
.fbits
= uarg("field bits", optarg
); break;
684 case 'B': o
.gbits
= uarg("subgroup bits", optarg
); break;
685 case 'n': o
.n
= uarg("factor count", optarg
); break;
686 case 'i': o
.i
= uarg("interval count", optarg
); break;
687 case 't': o
.t
= farg("interval length", optarg
); break;
688 case 'q': o
.f
|= OF_NOCHECK
; break;
689 default: usage(stderr
); exit(1);
692 if (optind
+ 1 != argc
) { usage(stderr
); exit(1); }
694 for (j
= jobtab
; j
->name
; j
++)
695 if (strcmp(j
->name
, argv
[optind
]) == 0) break;
696 if (!j
->name
) die(1, "unknown job type `%s'", argv
[optind
]);
701 gettimeofday(&tv_now
, 0);
703 tv_addl(&tv_next
, &tv_now
, o
.t
, fmod(o
.t
* MILLION
, MILLION
));
709 gettimeofday(&tv_now
, 0);
710 } while (TV_CMP(&tv_now
, <, &tv_next
));
712 t
= (double)(c_stop
- c_start
)/CLOCKS_PER_SEC
;
715 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
716 n
, ii
, ii
/t
, itot
/ttot
);
719 } while (!o
.i
|| n
< o
.i
);
724 /*----- That's all, folks -------------------------------------------------*/