math/fgoldi.[ch]: Implement the extra operations needed for Ed448.
[catacomb] / progs / perftest.c
index 3377487..80f060a 100644 (file)
@@ -56,6 +56,8 @@
 #include "mprand.h"
 #include "fibrand.h"
 #include "rsa.h"
+#include "mpint.h"
+#include "mptext.h"
 #include "mpmont.h"
 #include "mpbarrett.h"
 #include "dh.h"
@@ -63,6 +65,7 @@
 #include "ec.h"
 #include "group.h"
 #include "x25519.h"
+#include "x448.h"
 #include "ed25519.h"
 
 #include "cc.h"
@@ -83,6 +86,7 @@ typedef struct opts {
   unsigned n;                          /* Number of factors */
   unsigned i;                          /* Number of intervals (or zero) */
   double t;                            /* Time for each interval (secs) */
+  mp *e;                               /* Public exponent */
   unsigned f;                          /* Flags */
 #define OF_NOCHECK 1u                  /*   Don't do group checking */
 } opts;
@@ -289,6 +293,24 @@ static void *x25519_jobinit(opts *o)
 static void x25519_jobrun(void *cc)
   { x25519_jobctx *c = cc; octet z[X25519_OUTSZ]; x25519(z, c->k, c->p); }
 
+/* --- x448 --- */
+
+typedef struct x448_jobctx {
+  octet k[X448_KEYSZ];
+  octet p[X448_PUBSZ];
+} x448_jobctx;
+
+static void *x448_jobinit(opts *o)
+{
+  x448_jobctx *c = CREATE(x448_jobctx);
+  rand_get(RAND_GLOBAL, c->k, sizeof(c->k));
+  rand_get(RAND_GLOBAL, c->p, sizeof(c->p));
+  return (c);
+}
+
+static void x448_jobrun(void *cc)
+  { x448_jobctx *c = cc; octet z[X448_OUTSZ]; x448(z, c->k, c->p); }
+
 /* --- Ed25519 --- */
 
 typedef struct ed25519_signctx {
@@ -352,7 +374,8 @@ static void *rsapriv_init(opts *o)
   rsapriv_ctx *c = CREATE(rsapriv_ctx);
 
   if (!o->fbits) o->fbits = 1024;
-  rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
+  if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
+  rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
   rsa_privcreate(&c->rpc, &c->rp, 0);
   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
   return (c);
@@ -363,7 +386,8 @@ static void *rsaprivblind_init(opts *o)
   rsapriv_ctx *c = CREATE(rsapriv_ctx);
 
   if (!o->fbits) o->fbits = 1024;
-  rsa_gen(&c->rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
+  if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
+  rsa_gen_e(&c->rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
   rsa_privcreate(&c->rpc, &c->rp, fibrand_create(0));
   c->m = mprand_range(MP_NEW, c->rp.n, &rand_global, 0);
   return (c);
@@ -388,7 +412,8 @@ static void *rsapub_init(opts *o)
   rsa_priv rp;
 
   if (!o->fbits) o->fbits = 1024;
-  rsa_gen(&rp, o->fbits, &rand_global, 0, pgen_evspin, 0);
+  if (!o->e) o->e = mp_fromulong(MP_NEW, 65537);
+  rsa_gen_e(&rp, o->fbits, o->e, &rand_global, 0, pgen_evspin, 0);
   c->rp.n = MP_COPY(rp.n);
   c->rp.e = MP_COPY(rp.e);
   rsa_privfree(&rp);
@@ -556,6 +581,7 @@ static const jobops jobtab[] = {
   { "rsa-priv-blind",          rsaprivblind_init,      rsapriv_run },
   { "rsa-pub",                 rsapub_init,            rsapub_run },
   { "x25519",                  x25519_jobinit,         x25519_jobrun },
+  { "x448",                    x448_jobinit,           x448_jobrun },
   { "ed25519-sign",            ed25519_signinit,       ed25519_signrun },
   { "ed25519-vrf",             ed25519_vrfinit,        ed25519_vrfrun },
   { "ksched",                  ksched_init,            ksched_run },
@@ -630,6 +656,14 @@ static unsigned uarg(const char *what, const char *p)
   return (u);
 }
 
+static mp *mparg(const char *what, const char *p)
+{
+  char *q;
+  mp *x = mp_readstring(MP_NEW, p, &q, 0);
+  if (!x || *q) die(1, "bad %s `%s'", what, p);
+  return (x);
+}
+
 static double farg(const char *what, const char *p)
 {
   char *q;
@@ -667,12 +701,13 @@ int main(int argc, char *argv[])
       { "group-bits",  OPTF_ARGREQ,    0,      'B' },
       { "factors",     OPTF_ARGREQ,    0,      'n' },
       { "intervals",   OPTF_ARGREQ,    0,      'i' },
+      { "public-exponent", OPTF_ARGREQ, 0,     'e' },
       { "time",                OPTF_ARGREQ,    0,      't' },
       { "no-check",    0,              0,      'q' },
       { 0,             0,              0,      0 }
     };
 
-    i = mdwopt(argc, argv, "hvulC:b:B:n:i:t:q", opts, 0, 0, 0);
+    i = mdwopt(argc, argv, "hvulC:b:B:n:i:e:t:q", opts, 0, 0, 0);
     if (i < 0) break;
     switch (i) {
       case 'h': help(stdout); exit(0);
@@ -683,6 +718,11 @@ int main(int argc, char *argv[])
       case 'b': o.fbits = uarg("field bits", optarg); break;
       case 'B': o.gbits = uarg("subgroup bits", optarg); break;
       case 'n': o.n = uarg("factor count", optarg); break;
+      case 'e':
+       mp_drop(o.e); o.e = mparg("public exponent", optarg);
+       if (MP_CMP(o.e, <, MP_THREE) || MP_EVENP(o.e))
+         die(1, "invalid public exponent");
+       break;
       case 'i': o.i = uarg("interval count", optarg); break;
       case 't': o.t = farg("interval length", optarg); break;
       case 'q': o.f |= OF_NOCHECK; break;