chiark / gitweb /
math/mp-arith.c: New function `mp_leastcongruent'.
[catacomb] / progs / perftest.c
1 /* -*-c-*-
2  *
3  * Measure performance of various operations (Unix-specific)
4  *
5  * (c) 2004 Straylight/Edgeware
6  */
7
8 /*----- Licensing notice --------------------------------------------------*
9  *
10  * This file is part of Catacomb.
11  *
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.
16  *
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.
21  *
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,
25  * MA 02111-1307, USA.
26  */
27
28 /*----- Header files ------------------------------------------------------*/
29
30 #define _FILE_OFFSET_BITS 64
31
32 #include "config.h"
33
34 #include <errno.h>
35 #include <limits.h>
36 #include <math.h>
37 #include <stdio.h>
38 #include <string.h>
39 #include <stdlib.h>
40 #include <time.h>
41
42 #include <sys/types.h>
43 #include <sys/time.h>
44 #include <unistd.h>
45
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>
51 #include <mLib/sub.h>
52 #include <mLib/tv.h>
53
54 #include "rand.h"
55 #include "mp.h"
56 #include "mprand.h"
57 #include "fibrand.h"
58 #include "rsa.h"
59 #include "mpmont.h"
60 #include "mpbarrett.h"
61 #include "dh.h"
62 #include "pgen.h"
63 #include "ec.h"
64 #include "group.h"
65
66 #include "cc.h"
67 #include "gcipher.h"
68 #include "ghash.h"
69 #include "gmac.h"
70 #include "ectab.h"
71 #include "ptab.h"
72
73 /*----- Options -----------------------------------------------------------*/
74
75 typedef struct opts {
76   const char *name;                     /* Pre-configured named thing */
77   unsigned fbits;                       /* Field size bits */
78   unsigned gbits;                       /* Group size bits */
79   unsigned n;                           /* Number of factors */
80   unsigned i;                           /* Number of intervals (or zero) */
81   double t;                             /* Time for each interval (secs) */
82   unsigned f;                           /* Flags */
83 #define OF_NOCHECK 1u                   /*   Don't do group checking */
84 } opts;
85
86 /*----- Job switch --------------------------------------------------------*/
87
88 /* --- Barrett exponentiation --- */
89
90 typedef struct bar_ctx {
91   size_t n;
92   mpbarrett b;
93   mp_expfactor *e;
94 } bar_ctx;
95
96 static void *bar_init(opts *o)
97 {
98   bar_ctx *c = CREATE(bar_ctx);
99   gprime_param gp;
100   qd_parse qd;
101   size_t i;
102
103   if (o->name) {
104     qd.p = o->name;
105     if (dh_parse(&qd, &gp))
106       die(1, "bad prime group: %s", qd.e);
107   } else {
108     if (!o->fbits) o->fbits = 1024;
109     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
110   }
111   mpbarrett_create(&c->b, gp.p);
112   if (!o->n) o->n = 1;
113   c->n = o->n;
114   c->e = xmalloc(c->n * sizeof(group_expfactor));
115   for (i = 0; i < c->n; i++) {
116     c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
117     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
118   }
119   dh_paramfree(&gp);
120   return (c);
121 }
122
123 static void bar_run(void *cc)
124 {
125   bar_ctx *c = cc;
126   mp *d = mpbarrett_exp(&c->b, MP_NEW, c->e[0].base, c->e[0].exp);
127   MP_DROP(d);
128 }
129
130 static void barsim_run(void *cc)
131 {
132   bar_ctx *c = cc;
133   mp *d = mpbarrett_mexp(&c->b, MP_NEW, c->e, c->n);
134   MP_DROP(d);
135 }
136
137 /* --- Montgomery exponentiation --- */
138
139 typedef struct mont_ctx {
140   size_t n;
141   mpmont m;
142   mp_expfactor *e;
143 } mont_ctx;
144
145 static void *mont_init(opts *o)
146 {
147   mont_ctx *c = CREATE(mont_ctx);
148   gprime_param gp;
149   qd_parse qd;
150   size_t i;
151
152   if (o->name) {
153     qd.p = o->name;
154     if (dh_parse(&qd, &gp))
155       die(1, "bad prime group: %s", qd.e);
156   } else {
157     if (!o->fbits) o->fbits = 1024;
158     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
159   }
160   mpmont_create(&c->m, gp.p);
161   if (!o->n) o->n = 1;
162   c->n = o->n;
163   c->e = xmalloc(c->n * sizeof(mp_expfactor));
164   for (i = 0; i < c->n; i++) {
165     c->e[i].base = mprand_range(MP_NEW, gp.p, &rand_global, 0);
166     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
167   }
168   dh_paramfree(&gp);
169   return (c);
170 }
171
172 static void mont_run(void *cc)
173 {
174   mont_ctx *c = cc;
175   mp *d = mpmont_expr(&c->m, MP_NEW, c->e[0].base, c->e[0].exp);
176   MP_DROP(d);
177 }
178
179 static void montsim_run(void *cc)
180 {
181   mont_ctx *c = cc;
182   mp *d = mpmont_mexpr(&c->m, MP_NEW, c->e, c->n);
183   MP_DROP(d);
184 }
185
186 /* --- Group exponentiation --- */
187
188 typedef struct gr_ctx {
189   size_t n;
190   group *g;
191   group_expfactor *e;
192 } gr_ctx;
193
194 static void *grp_init(opts *o)
195 {
196   gr_ctx *c = CREATE(gr_ctx);
197   const char *e;
198   gprime_param gp;
199   qd_parse qd;
200   size_t i;
201
202   if (o->name) {
203     qd.p = o->name;
204     if (dh_parse(&qd, &gp))
205       die(1, "bad prime group: %s", qd.e);
206   } else {
207     if (!o->fbits) o->fbits = 1024;
208     dh_gen(&gp, o->gbits, o->fbits, 0, &rand_global, pgen_evspin, 0);
209   }
210   c->g = group_prime(&gp);
211   if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
212     die(1, "bad group: %s", e);
213   if (!o->n) o->n = 1;
214   c->n = o->n;
215   c->e = xmalloc(c->n * sizeof(group_expfactor));
216   for (i = 0; i < c->n; i++) {
217     c->e[i].base = G_CREATE(c->g);
218     G_FROMINT(c->g, c->e[i].base,
219               mprand_range(MP_NEW, gp.p, &rand_global, 0));
220     c->e[i].exp = mprand_range(MP_NEW, gp.q, &rand_global, 0);
221   }
222   dh_paramfree(&gp);
223   return (c);
224 }
225
226 static void *grec_init(opts *o)
227 {
228   gr_ctx *c = CREATE(gr_ctx);
229   const char *e;
230   ec_info ei;
231   ec p = EC_INIT;
232   size_t i;
233
234   if (!o->name)
235     die(1, "can't generate elliptic curves");
236   if ((e = ec_getinfo(&ei, o->name)) != 0)
237     die(1, "bad curve: %s", e);
238   c->g = group_ec(&ei);
239   if (!(o->f & OF_NOCHECK) && (e = G_CHECK(c->g, &rand_global)) != 0)
240     die(1, "bad group: %s", e);
241   if (!o->n) o->n = 1;
242   c->n = o->n;
243   c->e = xmalloc(c->n * sizeof(group_expfactor));
244   for (i = 0; i < c->n; i++) {
245     c->e[i].base = G_CREATE(c->g);
246     ec_rand(ei.c, &p, &rand_global);
247     G_FROMEC(c->g, c->e[i].base, &p);
248     c->e[i].exp = mprand_range(MP_NEW, ei.r, &rand_global, 0);
249   }
250   EC_DESTROY(&p);
251   return (c);
252 }
253
254 static void gr_run(void *cc)
255 {
256   gr_ctx *c = cc;
257   ge *x = G_CREATE(c->g);
258   G_EXP(c->g, x, c->e[0].base, c->e[0].exp);
259   G_DESTROY(c->g, x);
260 }
261
262 static void grsim_run(void *cc)
263 {
264   gr_ctx *c = cc;
265   ge *x = G_CREATE(c->g);
266   G_MEXP(c->g, x, c->e, c->n);
267   G_DESTROY(c->g, x);
268 }
269
270 /* --- RSA --- */
271
272 typedef struct rsapriv_ctx {
273   rsa_priv rp;
274   rsa_privctx rpc;
275   mp *m;
276 } rsapriv_ctx;
277
278 static void *rsapriv_init(opts *o)
279 {
280   rsapriv_ctx *c = CREATE(rsapriv_ctx);
281
282   if (!o->fbits) o->fbits = 1024;
283   rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
284   rsa_privcreate(&c->rpc, &c->rp, 0);
285   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
286   return (c);
287 }
288
289 static void *rsaprivblind_init(opts *o)
290 {
291   rsapriv_ctx *c = CREATE(rsapriv_ctx);
292
293   if (!o->fbits) o->fbits = 1024;
294   rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
295   rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
296   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
297   return (c);
298 }
299
300 static void rsapriv_run(void *cc)
301 {
302   rsapriv_ctx *c = cc;
303   mp *d = rsa_privop(&c->rpc, MP_NEW, c->m);
304   MP_DROP(d);
305 }
306
307 typedef struct rsapub_ctx {
308   rsa_pub rp;
309   rsa_pubctx rpc;
310   mp *m;
311 } rsapub_ctx;
312
313 static void *rsapub_init(opts *o)
314 {
315   rsapub_ctx *c = CREATE(rsapub_ctx);
316   rsa_priv rp;
317
318   if (!o->fbits) o->fbits = 1024;
319   rsa_gen(&rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
320   c->rp.n = MP_COPY(rp.n);
321   c->rp.e = MP_COPY(rp.e);
322   rsa_privfree(&rp);
323   rsa_pubcreate(&c->rpc, &c->rp);
324   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
325   return (c);
326 }
327
328 static void rsapub_run(void *cc)
329 {
330   rsapub_ctx *c = cc;
331   mp *d = rsa_pubop(&c->rpc, MP_NEW, c->m);
332   MP_DROP(d);
333 }
334
335 /* --- Symmetric encryption --- */
336
337 typedef struct ksched_ctx {
338   const gccipher *c;
339   octet *k;
340   size_t ksz;
341 } ksched_ctx;
342
343 static void *ksched_init(opts *o)
344 {
345   ksched_ctx *c = CREATE(ksched_ctx);
346   if (!o->name)
347     die(1, "must specify encryption scheme name");
348   if ((c->c = gcipher_byname(o->name)) == 0)
349     die(1, "encryption scheme `%s' not known", o->name);
350   c->ksz = keysz(o->gbits/8, c->c->keysz);
351   c->k = xmalloc(c->ksz);
352   rand_get(RAND_GLOBAL, c->k, c->ksz);
353   return (c);
354 }
355
356 static void ksched_run(void *cc)
357 {
358   ksched_ctx *c = cc;
359   gcipher *gc = GC_INIT(c->c, c->k, c->ksz);
360   GC_DESTROY(gc);
361 }
362
363 typedef struct enc_ctx {
364   gcipher *c;
365   octet *m;
366   size_t sz;
367   size_t n;
368 } enc_ctx;
369
370 static void *enc_init(opts *o)
371 {
372   enc_ctx *c = CREATE(enc_ctx);
373   const gccipher *cc;
374   size_t ksz;
375   octet *k;
376   if (!o->name)
377     die(1, "must specify encryption scheme name");
378   if ((cc = gcipher_byname(o->name)) == 0)
379     die(1, "encryption scheme `%s' not known", o->name);
380   ksz = keysz(0, cc->keysz);
381   k = xmalloc(ksz);
382   rand_get(RAND_GLOBAL, k, ksz);
383   c->c = GC_INIT(cc, k, ksz);
384   xfree(k);
385   c->sz = o->gbits ? o->gbits : 65536;
386   c->n = o->n ? o->n : 16;
387   c->m = xmalloc(c->sz);
388   return (c);
389 }
390
391 static void enc_run(void *cc)
392 {
393   enc_ctx *c = cc;
394   size_t i;
395   for (i = 0; i < c->n; i++)
396     GC_ENCRYPT(c->c, c->m, c->m, c->sz);
397 }
398
399 /* --- Hashing --- */
400
401 typedef struct hash_ctx {
402   const gchash *h;
403   octet *m;
404   size_t sz;
405   size_t n;
406 } hash_ctx;
407
408 static void *hash_init(opts *o)
409 {
410   hash_ctx *c = CREATE(hash_ctx);
411   if (!o->name)
412     die(1, "must specify hash function name");
413   if ((c->h = ghash_byname(o->name)) == 0)
414     die(1, "hash function `%s' not known", o->name);
415   c->sz = o->gbits ? o->gbits : 65536;
416   c->n = o->n ? o->n : 16;
417   c->m = xmalloc(c->sz);
418   return (c);
419 }
420
421 static void hash_run(void *cc)
422 {
423   hash_ctx *c = cc;
424   size_t i;
425   ghash *h = GH_INIT(c->h);
426   for (i = 0; i < c->n; i++)
427     GH_HASH(h, c->m, c->sz);
428   GH_DONE(h, 0);
429   GH_DESTROY(h);
430 }
431
432 /* --- Job table --- */
433
434 typedef struct jobops {
435   const char *name;
436   void *(*init)(opts *);
437   void (*run)(void *);
438 } jobops;
439
440 static const jobops jobtab[] = {
441   { "g-prime-exp",              grp_init,               gr_run },
442   { "g-ec-mul",                 grec_init,              gr_run },
443   { "g-prime-exp-sim",          grp_init,               grsim_run },
444   { "g-ec-mul-sim",             grec_init,              grsim_run },
445   { "barrett-exp",              bar_init,               bar_run },
446   { "barrett-exp-sim",          bar_init,               barsim_run },
447   { "mont-exp",                 mont_init,              mont_run },
448   { "mont-exp-sim",             mont_init,              montsim_run },
449   { "rsa-priv",                 rsapriv_init,           rsapriv_run },
450   { "rsa-priv-blind",           rsaprivblind_init,      rsapriv_run },
451   { "rsa-pub",                  rsapub_init,            rsapub_run },
452   { "ksched",                   ksched_init,            ksched_run },
453   { "enc",                      enc_init,               enc_run },
454   { "hash",                     hash_init,              hash_run },
455   { 0,                          0,                      0 }
456 };
457
458 /*----- Main code ---------------------------------------------------------*/
459
460 void version(FILE *fp)
461 {
462   pquis(fp, "$, Catacomb " VERSION "\n");
463 }
464
465 static void usage(FILE *fp)
466 {
467   pquis(fp, "Usage: $ [-options] job\n");
468 }
469
470 static void help(FILE *fp)
471 {
472   version(fp);
473   putc('\n', fp);
474   usage(fp);
475   pquis(fp, "\n\
476 Various performance tests.\n\
477 \n\
478 Options:\n\
479 \n\
480 -h, --help              Show this help text.\n\
481 -v, --version           Show program version number.\n\
482 -u, --usage             Show terse usage message.\n\
483 -l, --list [ITEM...]    List all the various names of things.\n\
484 \n\
485 -C, --name=NAME         Select curve/DH-group/enc/hash name.\n\
486 -b, --field-bits        Field size for g-prime and rsa.\n\
487 -q, --no-check          Don't check field/group for validity.\n\
488 -B, --group-bits        Group size for g-prime; key size for ksched;\n\
489                           data size for enc and hash.\n\
490 -n, --factors=COUNT     Number of factors for {exp,mul}-sim.\n\
491 -i, --intervals=COUNT   Number of intervals to run for.  [0; forever]\n\
492 -t, --time=TIME         Length of an interval in seconds.  [1]\n\
493 ");
494 }
495
496 #define LISTS(LI)                                                       \
497   LI("Lists", list,                                                     \
498      listtab[i].name, listtab[i].name)                                  \
499   LI("Jobs", job,                                                       \
500      jobtab[i].name, jobtab[i].name)                                    \
501   LI("Elliptic curves", ec,                                             \
502      ectab[i].name, ectab[i].name)                                      \
503   LI("Diffie-Hellman groups", dh,                                       \
504      ptab[i].name, ptab[i].name)                                        \
505   LI("Encryption algorithms", cipher,                                   \
506      gciphertab[i], gciphertab[i]->name)                                \
507   LI("Hash functions", hash,                                            \
508      ghashtab[i], ghashtab[i]->name)
509
510 MAKELISTTAB(listtab, LISTS)
511
512 static unsigned uarg(const char *what, const char *p)
513 {
514   char *q;
515   unsigned long u;
516   errno = 0;
517   u = strtoul(p, &q, 0);
518   if (*q || u > UINT_MAX || q == p || errno)
519     die(1, "bad %s `%s'", what, p);
520   return (u);
521 }
522
523 static double farg(const char *what, const char *p)
524 {
525   char *q;
526   double f;
527   errno = 0;
528   f = strtod(p, &q);
529   if (*q || q == p || errno)
530     die(1, "bad %s `%s'", what, p);
531   return (f);
532 }
533
534 int main(int argc, char *argv[])
535 {
536   int i;
537   opts o = { 0 };
538   const jobops *j;
539   struct timeval tv_next, tv_now;
540   double t, ttot;
541   unsigned n;
542   unsigned long ii;
543   clock_t c_start, c_stop;
544   double itot;
545   void *p;
546
547   ego(argv[0]);
548   o.t = 1;
549   for (;;) {
550     static const struct option opts[] = {
551       { "help",         0,              0,      'h' },
552       { "version",      0,              0,      'v' },
553       { "usage",        0,              0,      'u' },
554       { "list",         0,              0,      'l' },
555       { "name",         OPTF_ARGREQ,    0,      'C' },
556       { "field-bits",   OPTF_ARGREQ,    0,      'b' },
557       { "group-bits",   OPTF_ARGREQ,    0,      'B' },
558       { "factors",      OPTF_ARGREQ,    0,      'n' },
559       { "intervals",    OPTF_ARGREQ,    0,      'i' },
560       { "time",         OPTF_ARGREQ,    0,      't' },
561       { "no-check",     0,              0,      'q' },
562       { 0,              0,              0,      0 }
563     };
564
565     i = mdwopt(argc, argv, "hvulC:b:B:n:i:t:q", opts, 0, 0, 0);
566     if (i < 0) break;
567     switch (i) {
568       case 'h': help(stdout); exit(0);
569       case 'v': version(stdout); exit(0);
570       case 'u': usage(stdout); exit(0);
571       case 'l': exit(displaylists(listtab, argv + optind));
572       case 'C': o.name = optarg; break;
573       case 'b': o.fbits = uarg("field bits", optarg); break;
574       case 'B': o.gbits = uarg("subgroup bits", optarg); break;
575       case 'n': o.n = uarg("factor count", optarg); break;
576       case 'i': o.i = uarg("interval count", optarg); break;
577       case 't': o.t = farg("interval length", optarg); break;
578       case 'q': o.f |= OF_NOCHECK; break;
579       default: usage(stderr); exit(1);
580     }
581   }
582   if (optind + 1 != argc) { usage(stderr); exit(1); }
583
584   for (j = jobtab; j->name; j++)
585     if (strcmp(j->name, argv[optind]) == 0) break;
586   if (!j->name) die(1, "unknown job type `%s'", argv[optind]);
587   p = j->init(&o);
588
589   n = 0;
590   ttot = itot =  0;
591   gettimeofday(&tv_now, 0);
592   do {
593     tv_addl(&tv_next, &tv_now, o.t, fmod(o.t * MILLION, MILLION));
594     ii = 0;
595     c_start = clock();
596     do {
597       j->run(p);
598       ii++;
599       gettimeofday(&tv_now, 0);
600     } while (TV_CMP(&tv_now, <, &tv_next));
601     c_stop = clock();
602     t = (double)(c_stop - c_start)/CLOCKS_PER_SEC;
603     itot += ii;
604     ttot += t;
605     printf("%5u: did = %5lu; /sec = %5f; avg /sec = %5f\n",
606            n, ii, ii/t, itot/ttot);
607     fflush(stdout);
608     n++;
609   } while (!o.i || n < o.i);
610
611   return (0);
612 }
613
614 /*----- That's all, folks -------------------------------------------------*/