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>
62 #include "mpbarrett.h"
80 /*----- Options -----------------------------------------------------------*/
83 const char *name
; /* Pre-configured named thing */
84 unsigned fbits
; /* Field size bits */
85 unsigned gbits
; /* Group size bits */
86 unsigned n
; /* Number of factors */
87 unsigned i
; /* Number of intervals (or zero) */
88 double t
; /* Time for each interval (secs) */
89 mp
*e
; /* Public exponent */
90 unsigned f
; /* Flags */
91 #define OF_NOCHECK 1u /* Don't do group checking */
94 /*----- Job switch --------------------------------------------------------*/
96 /* --- Barrett exponentiation --- */
98 typedef struct bar_ctx
{
104 static void *bar_init(opts
*o
)
106 bar_ctx
*c
= CREATE(bar_ctx
);
113 if (dh_parse(&qd
, &gp
))
114 die(1, "bad prime group: %s", qd
.e
);
116 if (!o
->fbits
) o
->fbits
= 1024;
117 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
119 mpbarrett_create(&c
->b
, gp
.p
);
122 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
123 for (i
= 0; i
< c
->n
; i
++) {
124 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
125 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
131 static void bar_run(void *cc
)
134 mp
*d
= mpbarrett_exp(&c
->b
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
138 static void barsim_run(void *cc
)
141 mp
*d
= mpbarrett_mexp(&c
->b
, MP_NEW
, c
->e
, c
->n
);
145 /* --- Montgomery exponentiation --- */
147 typedef struct mont_ctx
{
153 static void *mont_init(opts
*o
)
155 mont_ctx
*c
= CREATE(mont_ctx
);
162 if (dh_parse(&qd
, &gp
))
163 die(1, "bad prime group: %s", qd
.e
);
165 if (!o
->fbits
) o
->fbits
= 1024;
166 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
168 mpmont_create(&c
->m
, gp
.p
);
171 c
->e
= xmalloc(c
->n
* sizeof(mp_expfactor
));
172 for (i
= 0; i
< c
->n
; i
++) {
173 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
174 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
180 static void mont_run(void *cc
)
183 mp
*d
= mpmont_expr(&c
->m
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
187 static void montsim_run(void *cc
)
190 mp
*d
= mpmont_mexpr(&c
->m
, MP_NEW
, c
->e
, c
->n
);
194 /* --- Group exponentiation --- */
196 typedef struct gr_ctx
{
202 static void *grp_init(opts
*o
)
204 gr_ctx
*c
= CREATE(gr_ctx
);
212 if (dh_parse(&qd
, &gp
))
213 die(1, "bad prime group: %s", qd
.e
);
215 if (!o
->fbits
) o
->fbits
= 1024;
216 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
218 c
->g
= group_prime(&gp
);
219 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
220 die(1, "bad group: %s", e
);
223 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
224 for (i
= 0; i
< c
->n
; i
++) {
225 c
->e
[i
].base
= G_CREATE(c
->g
);
226 G_FROMINT(c
->g
, c
->e
[i
].base
,
227 mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0));
228 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
234 static void *grec_init(opts
*o
)
236 gr_ctx
*c
= CREATE(gr_ctx
);
243 die(1, "can't generate elliptic curves");
244 if ((e
= ec_getinfo(&ei
, o
->name
)) != 0)
245 die(1, "bad curve: %s", e
);
246 c
->g
= group_ec(&ei
);
247 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
248 die(1, "bad group: %s", e
);
251 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
252 for (i
= 0; i
< c
->n
; i
++) {
253 c
->e
[i
].base
= G_CREATE(c
->g
);
254 ec_rand(ei
.c
, &p
, &rand_global
);
255 G_FROMEC(c
->g
, c
->e
[i
].base
, &p
);
256 c
->e
[i
].exp
= mprand_range(MP_NEW
, ei
.r
, &rand_global
, 0);
262 static void gr_run(void *cc
)
265 ge
*x
= G_CREATE(c
->g
);
266 G_EXP(c
->g
, x
, c
->e
[0].base
, c
->e
[0].exp
);
270 static void grsim_run(void *cc
)
273 ge
*x
= G_CREATE(c
->g
);
274 G_MEXP(c
->g
, x
, c
->e
, c
->n
);
280 typedef struct x25519_jobctx
{
281 octet k
[X25519_KEYSZ
];
282 octet p
[X25519_PUBSZ
];
285 static void *x25519_jobinit(opts
*o
)
287 x25519_jobctx
*c
= CREATE(x25519_jobctx
);
288 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
289 rand_get(RAND_GLOBAL
, c
->p
, sizeof(c
->p
));
293 static void x25519_jobrun(void *cc
)
294 { x25519_jobctx
*c
= cc
; octet z
[X25519_OUTSZ
]; x25519(z
, c
->k
, c
->p
); }
298 typedef struct x448_jobctx
{
303 static void *x448_jobinit(opts
*o
)
305 x448_jobctx
*c
= CREATE(x448_jobctx
);
306 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
307 rand_get(RAND_GLOBAL
, c
->p
, sizeof(c
->p
));
311 static void x448_jobrun(void *cc
)
312 { x448_jobctx
*c
= cc
; octet z
[X448_OUTSZ
]; x448(z
, c
->k
, c
->p
); }
314 /* --- Ed25519 --- */
316 typedef struct ed25519_signctx
{
317 octet k
[ED25519_KEYSZ
];
318 octet K
[ED25519_PUBSZ
];
322 typedef struct ed25519_vrfctx
{
323 octet K
[ED25519_PUBSZ
];
325 octet sig
[ED25519_SIGSZ
];
328 static void *ed25519_signinit(opts
*o
)
330 ed25519_signctx
*c
= CREATE(ed25519_signctx
);
332 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
333 rand_get(RAND_GLOBAL
, c
->m
, sizeof(c
->m
));
334 ed25519_pubkey(c
->K
, c
->k
, sizeof(c
->k
));
338 static void ed25519_signrun(void *cc
)
340 ed25519_signctx
*c
= cc
;
341 octet sig
[ED25519_SIGSZ
];
343 ed25519_sign(sig
, c
->k
, sizeof(c
->k
), c
->K
, c
->m
, sizeof(c
->m
));
346 static void *ed25519_vrfinit(opts
*o
)
348 octet k
[ED25519_KEYSZ
];
349 ed25519_vrfctx
*c
= CREATE(ed25519_vrfctx
);
351 rand_get(RAND_GLOBAL
, k
, sizeof(k
));
352 rand_get(RAND_GLOBAL
, c
->m
, sizeof(c
->m
));
353 ed25519_pubkey(c
->K
, k
, sizeof(k
));
354 ed25519_sign(c
->sig
, k
, sizeof(k
), c
->K
, c
->m
, sizeof(c
->m
));
358 static void ed25519_vrfrun(void *cc
)
360 ed25519_vrfctx
*c
= cc
;
361 ed25519_verify(c
->K
, c
->m
, sizeof(c
->m
), c
->sig
);
366 typedef struct rsapriv_ctx
{
372 static void *rsapriv_init(opts
*o
)
374 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
376 if (!o
->fbits
) o
->fbits
= 1024;
377 if (!o
->e
) o
->e
= mp_fromulong(MP_NEW
, 65537);
378 rsa_gen_e(&c
->rp
, o
->fbits
, o
->e
, &rand_global
, 0, pgen_evspin
, 0);
379 rsa_privcreate(&c
->rpc
, &c
->rp
, 0);
380 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
384 static void *rsaprivblind_init(opts
*o
)
386 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
388 if (!o
->fbits
) o
->fbits
= 1024;
389 if (!o
->e
) o
->e
= mp_fromulong(MP_NEW
, 65537);
390 rsa_gen_e(&c
->rp
, o
->fbits
, o
->e
, &rand_global
, 0, pgen_evspin
, 0);
391 rsa_privcreate(&c
->rpc
, &c
->rp
, fibrand_create(0));
392 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
396 static void rsapriv_run(void *cc
)
399 mp
*d
= rsa_privop(&c
->rpc
, MP_NEW
, c
->m
);
403 typedef struct rsapub_ctx
{
409 static void *rsapub_init(opts
*o
)
411 rsapub_ctx
*c
= CREATE(rsapub_ctx
);
414 if (!o
->fbits
) o
->fbits
= 1024;
415 if (!o
->e
) o
->e
= mp_fromulong(MP_NEW
, 65537);
416 rsa_gen_e(&rp
, o
->fbits
, o
->e
, &rand_global
, 0, pgen_evspin
, 0);
417 c
->rp
.n
= MP_COPY(rp
.n
);
418 c
->rp
.e
= MP_COPY(rp
.e
);
420 rsa_pubcreate(&c
->rpc
, &c
->rp
);
421 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
425 static void rsapub_run(void *cc
)
428 mp
*d
= rsa_pubop(&c
->rpc
, MP_NEW
, c
->m
);
432 /* --- Symmetric encryption --- */
434 typedef struct ksched_ctx
{
440 static void *ksched_init(opts
*o
)
442 ksched_ctx
*c
= CREATE(ksched_ctx
);
444 die(1, "must specify encryption scheme name");
445 if ((c
->c
= gcipher_byname(o
->name
)) == 0)
446 die(1, "encryption scheme `%s' not known", o
->name
);
447 c
->ksz
= keysz(o
->gbits
/8, c
->c
->keysz
);
448 c
->k
= xmalloc(c
->ksz
);
449 rand_get(RAND_GLOBAL
, c
->k
, c
->ksz
);
453 static void ksched_run(void *cc
)
456 gcipher
*gc
= GC_INIT(c
->c
, c
->k
, c
->ksz
);
460 typedef struct enc_ctx
{
467 static void *enc_init(opts
*o
)
469 enc_ctx
*c
= CREATE(enc_ctx
);
474 die(1, "must specify encryption scheme name");
475 if ((cc
= gcipher_byname(o
->name
)) == 0)
476 die(1, "encryption scheme `%s' not known", o
->name
);
477 ksz
= keysz(0, cc
->keysz
);
479 rand_get(RAND_GLOBAL
, k
, ksz
);
480 c
->c
= GC_INIT(cc
, k
, ksz
);
482 c
->sz
= o
->gbits ? o
->gbits
: 65536;
483 c
->n
= o
->n ? o
->n
: 16;
484 c
->m
= xmalloc(c
->sz
);
488 static void enc_run(void *cc
)
492 for (i
= 0; i
< c
->n
; i
++)
493 GC_ENCRYPT(c
->c
, c
->m
, c
->m
, c
->sz
);
496 /* --- Hashing --- */
498 typedef struct hash_ctx
{
505 static void *hash_init(opts
*o
)
507 hash_ctx
*c
= CREATE(hash_ctx
);
509 die(1, "must specify hash function name");
510 if ((c
->h
= ghash_byname(o
->name
)) == 0)
511 die(1, "hash function `%s' not known", o
->name
);
512 c
->sz
= o
->gbits ? o
->gbits
: 65536;
513 c
->n
= o
->n ? o
->n
: 16;
514 c
->m
= xmalloc(c
->sz
);
518 static void hash_run(void *cc
)
522 ghash
*h
= GH_INIT(c
->h
);
523 for (i
= 0; i
< c
->n
; i
++)
524 GH_HASH(h
, c
->m
, c
->sz
);
529 /* --- Poly1305 --- */
531 typedef struct poly1305_jobctx
{
533 octet s
[POLY1305_MASKSZ
];
539 static void *poly1305_jobinit(opts
*o
)
541 octet k
[POLY1305_KEYSZ
];
542 poly1305_jobctx
*c
= CREATE(poly1305_jobctx
);
543 rand_get(RAND_GLOBAL
, k
, sizeof(k
));
544 poly1305_keyinit(&c
->k
, k
, sizeof(k
));
545 rand_get(RAND_GLOBAL
, c
->s
, sizeof(c
->s
));
546 c
->sz
= o
->gbits ? o
->gbits
: 65536;
547 c
->n
= o
->n ? o
->n
: 16;
548 c
->m
= xmalloc(c
->sz
);
552 static void poly1305_jobrun(void *cc
)
554 poly1305_jobctx
*c
= cc
;
556 octet t
[POLY1305_TAGSZ
];
558 poly1305_macinit(&ctx
, &c
->k
, c
->s
);
559 for (i
= 0; i
< c
->n
; i
++) poly1305_hash(&ctx
, c
->m
, c
->sz
);
560 poly1305_done(&ctx
, t
);
563 /* --- Job table --- */
565 typedef struct jobops
{
567 void *(*init
)(opts
*);
571 static const jobops jobtab
[] = {
572 { "g-prime-exp", grp_init
, gr_run
},
573 { "g-ec-mul", grec_init
, gr_run
},
574 { "g-prime-exp-sim", grp_init
, grsim_run
},
575 { "g-ec-mul-sim", grec_init
, grsim_run
},
576 { "barrett-exp", bar_init
, bar_run
},
577 { "barrett-exp-sim", bar_init
, barsim_run
},
578 { "mont-exp", mont_init
, mont_run
},
579 { "mont-exp-sim", mont_init
, montsim_run
},
580 { "rsa-priv", rsapriv_init
, rsapriv_run
},
581 { "rsa-priv-blind", rsaprivblind_init
, rsapriv_run
},
582 { "rsa-pub", rsapub_init
, rsapub_run
},
583 { "x25519", x25519_jobinit
, x25519_jobrun
},
584 { "x448", x448_jobinit
, x448_jobrun
},
585 { "ed25519-sign", ed25519_signinit
, ed25519_signrun
},
586 { "ed25519-vrf", ed25519_vrfinit
, ed25519_vrfrun
},
587 { "ksched", ksched_init
, ksched_run
},
588 { "enc", enc_init
, enc_run
},
589 { "hash", hash_init
, hash_run
},
590 { "poly1305", poly1305_jobinit
, poly1305_jobrun
},
594 /*----- Main code ---------------------------------------------------------*/
596 void version(FILE *fp
)
598 pquis(fp
, "$, Catacomb " VERSION
"\n");
601 static void usage(FILE *fp
)
603 pquis(fp
, "Usage: $ [-options] job\n");
606 static void help(FILE *fp
)
612 Various performance tests.\n\
616 -h, --help Show this help text.\n\
617 -v, --version Show program version number.\n\
618 -u, --usage Show terse usage message.\n\
619 -l, --list [ITEM...] List all the various names of things.\n\
621 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
622 -b, --field-bits Field size for g-prime and rsa.\n\
623 -q, --no-check Don't check field/group for validity.\n\
624 -B, --group-bits Group size for g-prime; key size for ksched;\n\
625 data size for enc and hash.\n\
626 -n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
627 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
628 -t, --time=TIME Length of an interval in seconds. [1]\n\
634 listtab[i].name, listtab[i].name) \
636 jobtab[i].name, jobtab[i].name) \
637 LI("Elliptic curves", ec, \
638 ectab[i].name, ectab[i].name) \
639 LI("Diffie-Hellman groups", dh, \
640 ptab[i].name, ptab[i].name) \
641 LI("Encryption algorithms", cipher, \
642 gciphertab[i], gciphertab[i]->name) \
643 LI("Hash functions", hash, \
644 ghashtab[i], ghashtab[i]->name)
646 MAKELISTTAB(listtab
, LISTS
)
648 static unsigned uarg(const char *what
, const char *p
)
653 u
= strtoul(p
, &q
, 0);
654 if (*q
|| u
> UINT_MAX
|| q
== p
|| errno
)
655 die(1, "bad %s `%s'", what
, p
);
659 static mp
*mparg(const char *what
, const char *p
)
662 mp
*x
= mp_readstring(MP_NEW
, p
, &q
, 0);
663 if (!x
|| *q
) die(1, "bad %s `%s'", what
, p
);
667 static double farg(const char *what
, const char *p
)
673 if (*q
|| q
== p
|| errno
)
674 die(1, "bad %s `%s'", what
, p
);
678 int main(int argc
, char *argv
[])
683 struct timeval tv_next
, tv_now
;
687 clock_t c_start
, c_stop
;
694 static const struct option opts
[] = {
695 { "help", 0, 0, 'h' },
696 { "version", 0, 0, 'v' },
697 { "usage", 0, 0, 'u' },
698 { "list", 0, 0, 'l' },
699 { "name", OPTF_ARGREQ
, 0, 'C' },
700 { "field-bits", OPTF_ARGREQ
, 0, 'b' },
701 { "group-bits", OPTF_ARGREQ
, 0, 'B' },
702 { "factors", OPTF_ARGREQ
, 0, 'n' },
703 { "intervals", OPTF_ARGREQ
, 0, 'i' },
704 { "public-exponent", OPTF_ARGREQ
, 0, 'e' },
705 { "time", OPTF_ARGREQ
, 0, 't' },
706 { "no-check", 0, 0, 'q' },
710 i
= mdwopt(argc
, argv
, "hvulC:b:B:n:i:e:t:q", opts
, 0, 0, 0);
713 case 'h': help(stdout
); exit(0);
714 case 'v': version(stdout
); exit(0);
715 case 'u': usage(stdout
); exit(0);
716 case 'l': exit(displaylists(listtab
, argv
+ optind
));
717 case 'C': o
.name
= optarg
; break;
718 case 'b': o
.fbits
= uarg("field bits", optarg
); break;
719 case 'B': o
.gbits
= uarg("subgroup bits", optarg
); break;
720 case 'n': o
.n
= uarg("factor count", optarg
); break;
722 mp_drop(o
.e
); o
.e
= mparg("public exponent", optarg
);
723 if (MP_CMP(o
.e
, <, MP_THREE
) || MP_EVENP(o
.e
))
724 die(1, "invalid public exponent");
726 case 'i': o
.i
= uarg("interval count", optarg
); break;
727 case 't': o
.t
= farg("interval length", optarg
); break;
728 case 'q': o
.f
|= OF_NOCHECK
; break;
729 default: usage(stderr
); exit(1);
732 if (optind
+ 1 != argc
) { usage(stderr
); exit(1); }
734 for (j
= jobtab
; j
->name
; j
++)
735 if (strcmp(j
->name
, argv
[optind
]) == 0) break;
736 if (!j
->name
) die(1, "unknown job type `%s'", argv
[optind
]);
741 gettimeofday(&tv_now
, 0);
743 tv_addl(&tv_next
, &tv_now
, o
.t
, fmod(o
.t
* MILLION
, MILLION
));
749 gettimeofday(&tv_now
, 0);
750 } while (TV_CMP(&tv_now
, <, &tv_next
));
752 t
= (double)(c_stop
- c_start
)/CLOCKS_PER_SEC
;
755 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
756 n
, ii
, ii
/t
, itot
/ttot
);
759 } while (!o
.i
|| n
< o
.i
);
764 /*----- That's all, folks -------------------------------------------------*/