sfinx

sphincs+ inspired signing utility
git clone git://git.finwo.net/app/sfinx
Log | Files | Refs | README | LICENSE

bench.c (6175B)


      1 // Public-API benchmark for sfinx.
      2 //
      3 // Times tree key derivation, tree signing, verification, and single-key
      4 // signing. test/Makefile builds this twice, with and without -pthread, so the
      5 // worker pool can be compared against the sequential path.
      6 
      7 #include <finwo/keccak-fast.h>
      8 #include <stdint.h>
      9 #include <stdio.h>
     10 #include <stdlib.h>
     11 #include <string.h>
     12 #include <time.h>
     13 
     14 #include "sfinx.h"
     15 
     16 // Args {{{
     17 
     18 typedef struct {
     19   sfinx_hash hash;
     20   size_t     path_len;
     21   unsigned   reps;
     22 } bench_args;
     23 
     24 typedef enum {
     25   ARGS_OK,
     26   ARGS_HELP,
     27   ARGS_ERR
     28 } args_result;
     29 
     30 static void usage(const char *argv0) {
     31   fprintf(stderr, "usage: %s [-L bits] [-p path_len] [-n reps]\n", argv0);
     32   fprintf(stderr, "  -L  hash size, 224/256/384/512 (default 256)\n");
     33   fprintf(stderr, "  -p  path length in bytes, at least 1 (default 32)\n");
     34   fprintf(stderr, "  -n  repetitions, the best is reported (default 3)\n");
     35 }
     36 
     37 static int parse_u32(const char *text, uint32_t *out) {
     38   char         *end   = NULL;
     39   unsigned long value = 0;
     40   if (!text || !*text) {
     41     return 0;
     42   }
     43   value = strtoul(text, &end, 10);
     44   if (!end || *end != '\0') {
     45     return 0;
     46   }
     47   *out = (uint32_t)value;
     48   return 1;
     49 }
     50 
     51 static args_result parse_args(int argc, char **argv, bench_args *out) {
     52   out->hash     = SFINX_HASH_DEFAULT;
     53   out->path_len = SFINX_PATH_LEN_DEFAULT;
     54   out->reps     = 3;
     55 
     56   for (int i = 1; i < argc; i += 2) {
     57     const char *flag   = argv[i];
     58     const char *value  = i + 1 < argc ? argv[i + 1] : NULL;
     59     uint32_t    parsed = 0;
     60 
     61     if (strcmp(flag, "-h") == 0 || strcmp(flag, "--help") == 0) {
     62       usage(argv[0]);
     63       return ARGS_HELP;
     64     }
     65     if (!parse_u32(value, &parsed)) {
     66       fprintf(stderr, "%s expects a number\n", flag);
     67       return ARGS_ERR;
     68     }
     69     if (strcmp(flag, "-L") == 0) {
     70       out->hash = (sfinx_hash)parsed;
     71     } else if (strcmp(flag, "-p") == 0) {
     72       out->path_len = parsed;
     73     } else if (strcmp(flag, "-n") == 0) {
     74       out->reps = parsed;
     75     } else {
     76       fprintf(stderr, "unknown flag: %s\n", flag);
     77       return ARGS_ERR;
     78     }
     79   }
     80 
     81   if (sfinx_hash_len(out->hash) == 0) {
     82     fprintf(stderr, "unsupported hash size: %u\n", (unsigned)out->hash);
     83     return ARGS_ERR;
     84   }
     85   if (out->path_len == 0 || !sfinx_path_len_valid(out->path_len)) {
     86     fprintf(stderr, "invalid path length: %zu\n", out->path_len);
     87     return ARGS_ERR;
     88   }
     89   if (out->reps == 0) {
     90     fprintf(stderr, "reps must be at least 1\n");
     91     return ARGS_ERR;
     92   }
     93   return ARGS_OK;
     94 }
     95 
     96 // }}}
     97 
     98 // Timing {{{
     99 
    100 static uint64_t now_ns(void) {
    101   struct timespec ts;
    102   clock_gettime(CLOCK_MONOTONIC, &ts);
    103   return (uint64_t)ts.tv_sec * 1000000000ull + (uint64_t)ts.tv_nsec;
    104 }
    105 
    106 static double elapsed_ms(uint64_t start) {
    107   return (double)(now_ns() - start) / 1000000.0;
    108 }
    109 
    110 // }}}
    111 
    112 // Main {{{
    113 
    114 int main(int argc, char **argv) {
    115   bench_args  args;
    116   args_result parsed = parse_args(argc, argv, &args);
    117   if (parsed == ARGS_HELP) {
    118     return 0;
    119   }
    120   if (parsed == ARGS_ERR) {
    121     return 1;
    122   }
    123 
    124   size_t        len        = sfinx_hash_len(args.hash);
    125   size_t        sig_len    = sfinx_signature_len(args.hash, args.path_len);
    126   size_t        single_len = sfinx_signature_len(args.hash, 0);
    127   const uint8_t msg[]      = "sfinx benchmark";
    128   size_t        msg_len    = sizeof(msg) - 1;
    129 
    130   uint8_t  seed[32];
    131   uint8_t *path       = calloc(args.path_len + 1, 1);
    132   uint8_t *pub        = malloc(len);
    133   uint8_t *sig        = malloc(sig_len);
    134   uint8_t *single_sig = malloc(single_len);
    135 
    136   if (!path || !pub || !sig || !single_sig) {
    137     fprintf(stderr, "out of memory\n");
    138     free(path);
    139     free(pub);
    140     free(sig);
    141     free(single_sig);
    142     return 1;
    143   }
    144   for (size_t i = 0; i < sizeof(seed); i++) {
    145     seed[i] = (uint8_t)i;
    146   }
    147 
    148   double pubkey_ms = 0.0;
    149   double sign_ms   = 0.0;
    150   double verify_ms = 0.0;
    151   double single_ms = 0.0;
    152   size_t sig_got   = 0;
    153   int    ok        = 1;
    154 
    155   for (unsigned r = 0; r < args.reps && ok; r++) {
    156     size_t       got = 0;
    157     uint64_t     t0  = now_ns();
    158     sfinx_status rc  = sfinx_tree_public_key(args.hash, seed, sizeof(seed), args.path_len, pub, len, &got);
    159     double       dt  = elapsed_ms(t0);
    160     if (rc != SFINX_OK) {
    161       ok = 0;
    162     } else if (r == 0 || dt < pubkey_ms) {
    163       pubkey_ms = dt;
    164     }
    165   }
    166 
    167   for (unsigned r = 0; r < args.reps && ok; r++) {
    168     size_t       got = 0;
    169     uint64_t     t0  = now_ns();
    170     sfinx_status rc =
    171         sfinx_tree_sign(args.hash, seed, sizeof(seed), path, args.path_len, msg, msg_len, sig, sig_len, &got);
    172     double dt = elapsed_ms(t0);
    173     if (rc != SFINX_OK) {
    174       ok = 0;
    175     } else {
    176       sig_got = got;
    177       if (r == 0 || dt < sign_ms) {
    178         sign_ms = dt;
    179       }
    180     }
    181   }
    182 
    183   for (unsigned r = 0; r < args.reps && ok; r++) {
    184     uint64_t     t0 = now_ns();
    185     sfinx_status rc = sfinx_verify(pub, len, msg, msg_len, sig, sig_got);
    186     double       dt = elapsed_ms(t0);
    187     if (rc != SFINX_OK) {
    188       ok = 0;
    189     } else if (r == 0 || dt < verify_ms) {
    190       verify_ms = dt;
    191     }
    192   }
    193 
    194   for (unsigned r = 0; r < args.reps && ok; r++) {
    195     size_t       got = 0;
    196     uint64_t     t0  = now_ns();
    197     sfinx_status rc  = sfinx_single_sign(args.hash, seed, sizeof(seed), msg, msg_len, single_sig, single_len, &got);
    198     double       dt  = elapsed_ms(t0);
    199     if (rc != SFINX_OK) {
    200       ok = 0;
    201     } else if (r == 0 || dt < single_ms) {
    202       single_ms = dt;
    203     }
    204   }
    205 
    206   free(path);
    207   free(pub);
    208   free(sig);
    209   free(single_sig);
    210 
    211   if (!ok) {
    212     fprintf(stderr, "benchmark failed\n");
    213     return 1;
    214   }
    215 
    216 #ifdef _REENTRANT
    217   const char *mode = "threaded";
    218 #else
    219   const char *mode = "sequential";
    220 #endif
    221 
    222   printf("sfinx benchmark\n");
    223   printf("# hash %zu bytes, path %zu, reps %u, %s\n", len, args.path_len, args.reps, mode);
    224   printf("# core %s, batch %s\n", kf_backend_name(), kf_batch_name());
    225   printf("\n");
    226   printf("%-12s %10s\n", "case", "best (ms)");
    227   printf("%-12s %10s\n", "------------", "----------");
    228   printf("%-12s %10.3f\n", "pubkey", pubkey_ms);
    229   printf("%-12s %10.3f\n", "sign", sign_ms);
    230   printf("%-12s %10.3f\n", "verify", verify_ms);
    231   printf("%-12s %10.3f\n", "single-sign", single_ms);
    232   return 0;
    233 }
    234 
    235 // }}}
    236 
    237 // vim:fdm=marker:fdl=0