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"
78 /*----- Options -----------------------------------------------------------*/
81 const char *name
; /* Pre-configured named thing */
82 unsigned fbits
; /* Field size bits */
83 unsigned gbits
; /* Group size bits */
84 unsigned n
; /* Number of factors */
85 unsigned i
; /* Number of intervals (or zero) */
86 double t
; /* Time for each interval (secs) */
87 unsigned f
; /* Flags */
88 #define OF_NOCHECK 1u /* Don't do group checking */
91 /*----- Job switch --------------------------------------------------------*/
93 /* --- Barrett exponentiation --- */
95 typedef struct bar_ctx
{
101 static void *bar_init(opts
*o
)
103 bar_ctx
*c
= CREATE(bar_ctx
);
110 if (dh_parse(&qd
, &gp
))
111 die(1, "bad prime group: %s", qd
.e
);
113 if (!o
->fbits
) o
->fbits
= 1024;
114 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
116 mpbarrett_create(&c
->b
, gp
.p
);
119 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
120 for (i
= 0; i
< c
->n
; i
++) {
121 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
122 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
128 static void bar_run(void *cc
)
131 mp
*d
= mpbarrett_exp(&c
->b
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
135 static void barsim_run(void *cc
)
138 mp
*d
= mpbarrett_mexp(&c
->b
, MP_NEW
, c
->e
, c
->n
);
142 /* --- Montgomery exponentiation --- */
144 typedef struct mont_ctx
{
150 static void *mont_init(opts
*o
)
152 mont_ctx
*c
= CREATE(mont_ctx
);
159 if (dh_parse(&qd
, &gp
))
160 die(1, "bad prime group: %s", qd
.e
);
162 if (!o
->fbits
) o
->fbits
= 1024;
163 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
165 mpmont_create(&c
->m
, gp
.p
);
168 c
->e
= xmalloc(c
->n
* sizeof(mp_expfactor
));
169 for (i
= 0; i
< c
->n
; i
++) {
170 c
->e
[i
].base
= mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0);
171 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
177 static void mont_run(void *cc
)
180 mp
*d
= mpmont_expr(&c
->m
, MP_NEW
, c
->e
[0].base
, c
->e
[0].exp
);
184 static void montsim_run(void *cc
)
187 mp
*d
= mpmont_mexpr(&c
->m
, MP_NEW
, c
->e
, c
->n
);
191 /* --- Group exponentiation --- */
193 typedef struct gr_ctx
{
199 static void *grp_init(opts
*o
)
201 gr_ctx
*c
= CREATE(gr_ctx
);
209 if (dh_parse(&qd
, &gp
))
210 die(1, "bad prime group: %s", qd
.e
);
212 if (!o
->fbits
) o
->fbits
= 1024;
213 dh_gen(&gp
, o
->gbits
, o
->fbits
, 0, &rand_global
, pgen_evspin
, 0);
215 c
->g
= group_prime(&gp
);
216 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
217 die(1, "bad group: %s", e
);
220 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
221 for (i
= 0; i
< c
->n
; i
++) {
222 c
->e
[i
].base
= G_CREATE(c
->g
);
223 G_FROMINT(c
->g
, c
->e
[i
].base
,
224 mprand_range(MP_NEW
, gp
.p
, &rand_global
, 0));
225 c
->e
[i
].exp
= mprand_range(MP_NEW
, gp
.q
, &rand_global
, 0);
231 static void *grec_init(opts
*o
)
233 gr_ctx
*c
= CREATE(gr_ctx
);
240 die(1, "can't generate elliptic curves");
241 if ((e
= ec_getinfo(&ei
, o
->name
)) != 0)
242 die(1, "bad curve: %s", e
);
243 c
->g
= group_ec(&ei
);
244 if (!(o
->f
& OF_NOCHECK
) && (e
= G_CHECK(c
->g
, &rand_global
)) != 0)
245 die(1, "bad group: %s", e
);
248 c
->e
= xmalloc(c
->n
* sizeof(group_expfactor
));
249 for (i
= 0; i
< c
->n
; i
++) {
250 c
->e
[i
].base
= G_CREATE(c
->g
);
251 ec_rand(ei
.c
, &p
, &rand_global
);
252 G_FROMEC(c
->g
, c
->e
[i
].base
, &p
);
253 c
->e
[i
].exp
= mprand_range(MP_NEW
, ei
.r
, &rand_global
, 0);
259 static void gr_run(void *cc
)
262 ge
*x
= G_CREATE(c
->g
);
263 G_EXP(c
->g
, x
, c
->e
[0].base
, c
->e
[0].exp
);
267 static void grsim_run(void *cc
)
270 ge
*x
= G_CREATE(c
->g
);
271 G_MEXP(c
->g
, x
, c
->e
, c
->n
);
277 typedef struct x25519_jobctx
{
278 octet k
[X25519_KEYSZ
];
279 octet p
[X25519_PUBSZ
];
282 static void *x25519_jobinit(opts
*o
)
284 x25519_jobctx
*c
= CREATE(x25519_jobctx
);
285 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
286 rand_get(RAND_GLOBAL
, c
->p
, sizeof(c
->p
));
290 static void x25519_jobrun(void *cc
)
291 { x25519_jobctx
*c
= cc
; octet z
[X25519_OUTSZ
]; x25519(z
, c
->k
, c
->p
); }
295 typedef struct x448_jobctx
{
300 static void *x448_jobinit(opts
*o
)
302 x448_jobctx
*c
= CREATE(x448_jobctx
);
303 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
304 rand_get(RAND_GLOBAL
, c
->p
, sizeof(c
->p
));
308 static void x448_jobrun(void *cc
)
309 { x448_jobctx
*c
= cc
; octet z
[X448_OUTSZ
]; x448(z
, c
->k
, c
->p
); }
311 /* --- Ed25519 --- */
313 typedef struct ed25519_signctx
{
314 octet k
[ED25519_KEYSZ
];
315 octet K
[ED25519_PUBSZ
];
319 typedef struct ed25519_vrfctx
{
320 octet K
[ED25519_PUBSZ
];
322 octet sig
[ED25519_SIGSZ
];
325 static void *ed25519_signinit(opts
*o
)
327 ed25519_signctx
*c
= CREATE(ed25519_signctx
);
329 rand_get(RAND_GLOBAL
, c
->k
, sizeof(c
->k
));
330 rand_get(RAND_GLOBAL
, c
->m
, sizeof(c
->m
));
331 ed25519_pubkey(c
->K
, c
->k
, sizeof(c
->k
));
335 static void ed25519_signrun(void *cc
)
337 ed25519_signctx
*c
= cc
;
338 octet sig
[ED25519_SIGSZ
];
340 ed25519_sign(sig
, c
->k
, sizeof(c
->k
), c
->K
, c
->m
, sizeof(c
->m
));
343 static void *ed25519_vrfinit(opts
*o
)
345 octet k
[ED25519_KEYSZ
];
346 ed25519_vrfctx
*c
= CREATE(ed25519_vrfctx
);
348 rand_get(RAND_GLOBAL
, k
, sizeof(k
));
349 rand_get(RAND_GLOBAL
, c
->m
, sizeof(c
->m
));
350 ed25519_pubkey(c
->K
, k
, sizeof(k
));
351 ed25519_sign(c
->sig
, k
, sizeof(k
), c
->K
, c
->m
, sizeof(c
->m
));
355 static void ed25519_vrfrun(void *cc
)
357 ed25519_vrfctx
*c
= cc
;
358 ed25519_verify(c
->K
, c
->m
, sizeof(c
->m
), c
->sig
);
363 typedef struct rsapriv_ctx
{
369 static void *rsapriv_init(opts
*o
)
371 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
373 if (!o
->fbits
) o
->fbits
= 1024;
374 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
375 rsa_privcreate(&c
->rpc
, &c
->rp
, 0);
376 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
380 static void *rsaprivblind_init(opts
*o
)
382 rsapriv_ctx
*c
= CREATE(rsapriv_ctx
);
384 if (!o
->fbits
) o
->fbits
= 1024;
385 rsa_gen(&c
->rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
386 rsa_privcreate(&c
->rpc
, &c
->rp
, fibrand_create(0));
387 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
391 static void rsapriv_run(void *cc
)
394 mp
*d
= rsa_privop(&c
->rpc
, MP_NEW
, c
->m
);
398 typedef struct rsapub_ctx
{
404 static void *rsapub_init(opts
*o
)
406 rsapub_ctx
*c
= CREATE(rsapub_ctx
);
409 if (!o
->fbits
) o
->fbits
= 1024;
410 rsa_gen(&rp
, o
->fbits
, &rand_global
, 0, pgen_evspin
, 0);
411 c
->rp
.n
= MP_COPY(rp
.n
);
412 c
->rp
.e
= MP_COPY(rp
.e
);
414 rsa_pubcreate(&c
->rpc
, &c
->rp
);
415 c
->m
= mprand_range(MP_NEW
, c
->rp
.n
, &rand_global
, 0);
419 static void rsapub_run(void *cc
)
422 mp
*d
= rsa_pubop(&c
->rpc
, MP_NEW
, c
->m
);
426 /* --- Symmetric encryption --- */
428 typedef struct ksched_ctx
{
434 static void *ksched_init(opts
*o
)
436 ksched_ctx
*c
= CREATE(ksched_ctx
);
438 die(1, "must specify encryption scheme name");
439 if ((c
->c
= gcipher_byname(o
->name
)) == 0)
440 die(1, "encryption scheme `%s' not known", o
->name
);
441 c
->ksz
= keysz(o
->gbits
/8, c
->c
->keysz
);
442 c
->k
= xmalloc(c
->ksz
);
443 rand_get(RAND_GLOBAL
, c
->k
, c
->ksz
);
447 static void ksched_run(void *cc
)
450 gcipher
*gc
= GC_INIT(c
->c
, c
->k
, c
->ksz
);
454 typedef struct enc_ctx
{
461 static void *enc_init(opts
*o
)
463 enc_ctx
*c
= CREATE(enc_ctx
);
468 die(1, "must specify encryption scheme name");
469 if ((cc
= gcipher_byname(o
->name
)) == 0)
470 die(1, "encryption scheme `%s' not known", o
->name
);
471 ksz
= keysz(0, cc
->keysz
);
473 rand_get(RAND_GLOBAL
, k
, ksz
);
474 c
->c
= GC_INIT(cc
, k
, ksz
);
476 c
->sz
= o
->gbits ? o
->gbits
: 65536;
477 c
->n
= o
->n ? o
->n
: 16;
478 c
->m
= xmalloc(c
->sz
);
482 static void enc_run(void *cc
)
486 for (i
= 0; i
< c
->n
; i
++)
487 GC_ENCRYPT(c
->c
, c
->m
, c
->m
, c
->sz
);
490 /* --- Hashing --- */
492 typedef struct hash_ctx
{
499 static void *hash_init(opts
*o
)
501 hash_ctx
*c
= CREATE(hash_ctx
);
503 die(1, "must specify hash function name");
504 if ((c
->h
= ghash_byname(o
->name
)) == 0)
505 die(1, "hash function `%s' not known", o
->name
);
506 c
->sz
= o
->gbits ? o
->gbits
: 65536;
507 c
->n
= o
->n ? o
->n
: 16;
508 c
->m
= xmalloc(c
->sz
);
512 static void hash_run(void *cc
)
516 ghash
*h
= GH_INIT(c
->h
);
517 for (i
= 0; i
< c
->n
; i
++)
518 GH_HASH(h
, c
->m
, c
->sz
);
523 /* --- Poly1305 --- */
525 typedef struct poly1305_jobctx
{
527 octet s
[POLY1305_MASKSZ
];
533 static void *poly1305_jobinit(opts
*o
)
535 octet k
[POLY1305_KEYSZ
];
536 poly1305_jobctx
*c
= CREATE(poly1305_jobctx
);
537 rand_get(RAND_GLOBAL
, k
, sizeof(k
));
538 poly1305_keyinit(&c
->k
, k
, sizeof(k
));
539 rand_get(RAND_GLOBAL
, c
->s
, sizeof(c
->s
));
540 c
->sz
= o
->gbits ? o
->gbits
: 65536;
541 c
->n
= o
->n ? o
->n
: 16;
542 c
->m
= xmalloc(c
->sz
);
546 static void poly1305_jobrun(void *cc
)
548 poly1305_jobctx
*c
= cc
;
550 octet t
[POLY1305_TAGSZ
];
552 poly1305_macinit(&ctx
, &c
->k
, c
->s
);
553 for (i
= 0; i
< c
->n
; i
++) poly1305_hash(&ctx
, c
->m
, c
->sz
);
554 poly1305_done(&ctx
, t
);
557 /* --- Job table --- */
559 typedef struct jobops
{
561 void *(*init
)(opts
*);
565 static const jobops jobtab
[] = {
566 { "g-prime-exp", grp_init
, gr_run
},
567 { "g-ec-mul", grec_init
, gr_run
},
568 { "g-prime-exp-sim", grp_init
, grsim_run
},
569 { "g-ec-mul-sim", grec_init
, grsim_run
},
570 { "barrett-exp", bar_init
, bar_run
},
571 { "barrett-exp-sim", bar_init
, barsim_run
},
572 { "mont-exp", mont_init
, mont_run
},
573 { "mont-exp-sim", mont_init
, montsim_run
},
574 { "rsa-priv", rsapriv_init
, rsapriv_run
},
575 { "rsa-priv-blind", rsaprivblind_init
, rsapriv_run
},
576 { "rsa-pub", rsapub_init
, rsapub_run
},
577 { "x25519", x25519_jobinit
, x25519_jobrun
},
578 { "x448", x448_jobinit
, x448_jobrun
},
579 { "ed25519-sign", ed25519_signinit
, ed25519_signrun
},
580 { "ed25519-vrf", ed25519_vrfinit
, ed25519_vrfrun
},
581 { "ksched", ksched_init
, ksched_run
},
582 { "enc", enc_init
, enc_run
},
583 { "hash", hash_init
, hash_run
},
584 { "poly1305", poly1305_jobinit
, poly1305_jobrun
},
588 /*----- Main code ---------------------------------------------------------*/
590 void version(FILE *fp
)
592 pquis(fp
, "$, Catacomb " VERSION
"\n");
595 static void usage(FILE *fp
)
597 pquis(fp
, "Usage: $ [-options] job\n");
600 static void help(FILE *fp
)
606 Various performance tests.\n\
610 -h, --help Show this help text.\n\
611 -v, --version Show program version number.\n\
612 -u, --usage Show terse usage message.\n\
613 -l, --list [ITEM...] List all the various names of things.\n\
615 -C, --name=NAME Select curve/DH-group/enc/hash name.\n\
616 -b, --field-bits Field size for g-prime and rsa.\n\
617 -q, --no-check Don't check field/group for validity.\n\
618 -B, --group-bits Group size for g-prime; key size for ksched;\n\
619 data size for enc and hash.\n\
620 -n, --factors=COUNT Number of factors for {exp,mul}-sim.\n\
621 -i, --intervals=COUNT Number of intervals to run for. [0; forever]\n\
622 -t, --time=TIME Length of an interval in seconds. [1]\n\
628 listtab[i].name, listtab[i].name) \
630 jobtab[i].name, jobtab[i].name) \
631 LI("Elliptic curves", ec, \
632 ectab[i].name, ectab[i].name) \
633 LI("Diffie-Hellman groups", dh, \
634 ptab[i].name, ptab[i].name) \
635 LI("Encryption algorithms", cipher, \
636 gciphertab[i], gciphertab[i]->name) \
637 LI("Hash functions", hash, \
638 ghashtab[i], ghashtab[i]->name)
640 MAKELISTTAB(listtab
, LISTS
)
642 static unsigned uarg(const char *what
, const char *p
)
647 u
= strtoul(p
, &q
, 0);
648 if (*q
|| u
> UINT_MAX
|| q
== p
|| errno
)
649 die(1, "bad %s `%s'", what
, p
);
653 static double farg(const char *what
, const char *p
)
659 if (*q
|| q
== p
|| errno
)
660 die(1, "bad %s `%s'", what
, p
);
664 int main(int argc
, char *argv
[])
669 struct timeval tv_next
, tv_now
;
673 clock_t c_start
, c_stop
;
680 static const struct option opts
[] = {
681 { "help", 0, 0, 'h' },
682 { "version", 0, 0, 'v' },
683 { "usage", 0, 0, 'u' },
684 { "list", 0, 0, 'l' },
685 { "name", OPTF_ARGREQ
, 0, 'C' },
686 { "field-bits", OPTF_ARGREQ
, 0, 'b' },
687 { "group-bits", OPTF_ARGREQ
, 0, 'B' },
688 { "factors", OPTF_ARGREQ
, 0, 'n' },
689 { "intervals", OPTF_ARGREQ
, 0, 'i' },
690 { "time", OPTF_ARGREQ
, 0, 't' },
691 { "no-check", 0, 0, 'q' },
695 i
= mdwopt(argc
, argv
, "hvulC:b:B:n:i:t:q", opts
, 0, 0, 0);
698 case 'h': help(stdout
); exit(0);
699 case 'v': version(stdout
); exit(0);
700 case 'u': usage(stdout
); exit(0);
701 case 'l': exit(displaylists(listtab
, argv
+ optind
));
702 case 'C': o
.name
= optarg
; break;
703 case 'b': o
.fbits
= uarg("field bits", optarg
); break;
704 case 'B': o
.gbits
= uarg("subgroup bits", optarg
); break;
705 case 'n': o
.n
= uarg("factor count", optarg
); break;
706 case 'i': o
.i
= uarg("interval count", optarg
); break;
707 case 't': o
.t
= farg("interval length", optarg
); break;
708 case 'q': o
.f
|= OF_NOCHECK
; break;
709 default: usage(stderr
); exit(1);
712 if (optind
+ 1 != argc
) { usage(stderr
); exit(1); }
714 for (j
= jobtab
; j
->name
; j
++)
715 if (strcmp(j
->name
, argv
[optind
]) == 0) break;
716 if (!j
->name
) die(1, "unknown job type `%s'", argv
[optind
]);
721 gettimeofday(&tv_now
, 0);
723 tv_addl(&tv_next
, &tv_now
, o
.t
, fmod(o
.t
* MILLION
, MILLION
));
729 gettimeofday(&tv_now
, 0);
730 } while (TV_CMP(&tv_now
, <, &tv_next
));
732 t
= (double)(c_stop
- c_start
)/CLOCKS_PER_SEC
;
735 printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
736 n
, ii
, ii
/t
, itot
/ttot
);
739 } while (!o
.i
|| n
< o
.i
);
744 /*----- That's all, folks -------------------------------------------------*/