]> git.ipfire.org Git - thirdparty/nettle.git/commitdiff
Add benchmarking of slh-dsa, change unit to sign/s. slh-dsa-shake-128s
authorNiels Möller <nisse@lysator.liu.se>
Mon, 17 Feb 2025 19:25:42 +0000 (20:25 +0100)
committerNiels Möller <nisse@lysator.liu.se>
Mon, 17 Feb 2025 19:25:42 +0000 (20:25 +0100)
examples/hogweed-benchmark.c

index 3f8588331989e87a67108665ad1924df71abd8f9..c8d67f741cf6f5291805af4cfb84297ff28d53df 100644 (file)
@@ -51,7 +51,7 @@
 #include "gostdsa.h"
 #include "curve25519.h"
 #include "curve448.h"
-
+#include "slh-dsa.h"
 #include "nettle-meta.h"
 #include "sexp.h"
 #include "knuth-lfib.h"
@@ -159,8 +159,12 @@ bench_alg (const struct alg *alg)
 
   alg->clear (ctx);
 
-  printf("%16s %4d %9.4f %9.4f\n",
-        alg->name, alg->size, 1e-3/sign, 1e-3/verify);
+  if (sign < 0.02)
+    printf("%16s %4d %9.1f %9.1f\n",
+          alg->name, alg->size, 1.0/sign, 1.0/verify);
+  else
+    printf("%16s %4d %9.2f %9.2f\n",
+          alg->name, alg->size, 1.0/sign, 1.0/verify);
 }
 
 struct rsa_ctx
@@ -851,6 +855,50 @@ bench_curve_clear (void *p)
   free (p);
 }
 
+struct slh_dsa_ctx {
+  uint8_t pub[SLH_DSA_SHAKE_128S_KEY_SIZE];
+  uint8_t key[SLH_DSA_SHAKE_128S_KEY_SIZE];
+  uint8_t msg[10];
+  uint8_t sig[SLH_DSA_SHAKE_128S_SIGNATURE_SIZE];
+};
+
+static void *
+bench_slh_dsa_init (unsigned size)
+{
+  struct slh_dsa_ctx *ctx;
+  assert (size == 128);
+
+  ctx = xalloc (sizeof(*ctx));
+  memset (ctx->key, 1, SLH_DSA_SHAKE_128S_KEY_SIZE);
+  memset (ctx->pub, 2, SLH_DSA_SHAKE_128S_SEED_SIZE);
+  slh_dsa_shake_128s_root (ctx->pub, ctx->key, ctx->pub + SLH_DSA_SHAKE_128S_SEED_SIZE);
+  memset (ctx->msg, 3, sizeof (ctx->msg));
+  slh_dsa_shake_128s_sign (ctx->pub, ctx->key, sizeof(ctx->msg), ctx->msg, ctx->sig);
+  return ctx;
+}
+
+static void
+bench_slh_dsa_sign (void *p)
+{
+  struct slh_dsa_ctx *ctx = p;
+  uint8_t sig[SLH_DSA_SHAKE_128S_SIGNATURE_SIZE];
+  slh_dsa_shake_128s_sign (ctx->pub, ctx->key, sizeof(ctx->msg), ctx->msg, sig);
+}
+
+static void
+bench_slh_dsa_verify (void *p)
+{
+  struct slh_dsa_ctx *ctx = p;
+  if (!slh_dsa_shake_128s_verify (ctx->pub, sizeof (ctx->msg), ctx->msg, ctx->sig))
+    die ("Internal error, slh_dsa_shake_128s_verify failed.\n");
+}
+
+static void
+bench_slh_dsa_clear (void *p)
+{
+  free (p);
+}
+
 struct alg alg_list[] = {
   { "rsa",   1024, bench_rsa_init,   bench_rsa_sign,   bench_rsa_verify,   bench_rsa_clear },
   { "rsa",   2048, bench_rsa_init,   bench_rsa_sign,   bench_rsa_verify,   bench_rsa_clear },
@@ -882,6 +930,7 @@ struct alg alg_list[] = {
   { "curve", 448, bench_curve_init, bench_curve_mul_g, bench_curve_mul, bench_curve_clear },
   { "gostdsa",  256, bench_gostdsa_init, bench_gostdsa_sign, bench_gostdsa_verify, bench_gostdsa_clear },
   { "gostdsa",  512, bench_gostdsa_init, bench_gostdsa_sign, bench_gostdsa_verify, bench_gostdsa_clear },
+  { "slh-dsa-shake", 128, bench_slh_dsa_init, bench_slh_dsa_sign, bench_slh_dsa_verify, bench_slh_dsa_clear },
 };
 
 #define numberof(x)  (sizeof (x) / sizeof ((x)[0]))
@@ -897,7 +946,7 @@ main (int argc, char **argv)
 
   time_init();
   printf ("%16s %4s %9s %9s\n",
-         "name", "size", "sign/ms", "verify/ms");
+         "name", "size", "sign/s", "verify/s");
 
   for (i = 0; i < numberof(alg_list); i++)
     if (!filter || strstr (alg_list[i].name, filter))