bench.c (17883B)
1 #if defined(__linux__) 2 #define _GNU_SOURCE 3 #include <sched.h> 4 #endif 5 #include <stdint.h> 6 #include <stdio.h> 7 #include <stdlib.h> 8 #include <string.h> 9 #include <time.h> 10 11 #include "keccak-fast.h" 12 #include "keccak-fast-internal.h" 13 14 int kct_sha3_256(uint8_t *, size_t, const uint8_t *, size_t); 15 int kct_shake256(uint8_t *, size_t, const uint8_t *, size_t); 16 17 int kf_scalar_sha3_256(uint8_t *, size_t, const uint8_t *, size_t); 18 int kf_scalar_sha3_512(uint8_t *, size_t, const uint8_t *, size_t); 19 int kf_scalar_shake128(uint8_t *, size_t, const uint8_t *, size_t); 20 int kf_scalar_shake256(uint8_t *, size_t, const uint8_t *, size_t); 21 int kf_scalar_turboshake128(uint8_t *, size_t, const uint8_t *, size_t); 22 int kf_scalar_turboshake256(uint8_t *, size_t, const uint8_t *, size_t); 23 24 #define KF_CHAIN_PROTO(name) \ 25 void name(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, \ 26 uint8_t, uint32_t, size_t, uint8_t) 27 28 KF_CHAIN_PROTO(kf_scalar_chain); 29 KF_CHAIN_PROTO(kf_scalar_chain12); 30 31 void kf_scalar_perm(uint64_t st[25]); 32 void kf_scalar_perm12(uint64_t st[25]); 33 34 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 35 !defined(KECCAK_FAST_NO_BMI) 36 KF_CHAIN_PROTO(kf_bmi_chain); 37 KF_CHAIN_PROTO(kf_bmi_chain12); 38 void kf_bmi_perm(uint64_t st[25]); 39 void kf_bmi_perm12(uint64_t st[25]); 40 #endif 41 42 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 43 !defined(KECCAK_FAST_NO_AVX2) 44 void kf_avx2_batch(uint64_t states[][25], size_t count); 45 void kf_avx2_batch12(uint64_t states[][25], size_t count); 46 void kf_avx2_chain(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, 47 uint8_t, uint32_t, size_t, uint8_t); 48 void kf_avx2_chain12(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, 49 uint8_t, uint32_t, size_t, uint8_t); 50 #endif 51 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 52 !defined(KECCAK_FAST_NO_AVX512) 53 void kf_avx512_batch(uint64_t states[][25], size_t count); 54 void kf_avx512_batch12(uint64_t states[][25], size_t count); 55 void kf_avx512_chain(size_t, uint8_t *, size_t, const uint8_t *, size_t, size_t, 56 uint8_t, uint32_t, size_t, uint8_t); 57 void kf_avx512_chain12(size_t, uint8_t *, size_t, const uint8_t *, size_t, 58 size_t, uint8_t, uint32_t, size_t, uint8_t); 59 #endif 60 #if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) 61 int kf_armsha3_available(void); 62 void kf_armsha3_batch(uint64_t states[][25], size_t count); 63 void kf_armsha3_batch12(uint64_t states[][25], size_t count); 64 void kf_armsha3_chain(size_t, uint8_t *, size_t, const uint8_t *, size_t, 65 size_t, uint8_t, uint32_t, size_t, uint8_t); 66 void kf_armsha3_chain12(size_t, uint8_t *, size_t, const uint8_t *, size_t, 67 size_t, uint8_t, uint32_t, size_t, uint8_t); 68 void kf_armsha3_perm(uint64_t st[25]); 69 void kf_armsha3_perm12(uint64_t st[25]); 70 #endif 71 72 #ifndef BENCH_FLAGS 73 #define BENCH_FLAGS "" 74 #endif 75 76 #define BENCH_COUNT 64 77 #define BENCH_LINKS 256 78 #define BENCH_STREAM 16384 79 #define BENCH_CHUNK 4096 80 81 typedef int (*kf_fn)(uint8_t *, size_t, const uint8_t *, size_t); 82 typedef int (*kf_batch_fn)(size_t, const uint8_t *, size_t, uint8_t *, size_t); 83 typedef int (*kf_chain_api)(size_t, uint8_t *, size_t, const uint8_t *, size_t, 84 size_t, uint8_t, uint32_t); 85 typedef int (*kf_stream_init_fn)(kf_ctx *); 86 typedef int (*kf_stream_absorb_fn)(kf_ctx *, const uint8_t *, size_t); 87 typedef int (*kf_stream_squeeze_fn)(kf_ctx *, uint8_t *, size_t); 88 89 static volatile uint64_t g_sink; 90 91 static uint64_t now_ns(void) { 92 struct timespec ts; 93 clock_gettime(CLOCK_MONOTONIC, &ts); 94 return (uint64_t)ts.tv_sec * 1000000000ULL + (uint64_t)ts.tv_nsec; 95 } 96 97 static int pin_cpu(int cpu) { 98 #if defined(__linux__) 99 cpu_set_t set; 100 CPU_ZERO(&set); 101 CPU_SET(cpu, &set); 102 return sched_setaffinity(0, sizeof set, &set); 103 #else 104 (void)cpu; 105 return -1; 106 #endif 107 } 108 109 static double time_once(kf_fn fn, uint8_t *out, size_t outlen, 110 const uint8_t *in, size_t inlen, size_t iters) { 111 uint64_t t0 = now_ns(); 112 for (size_t i = 0; i < iters; i++) { 113 fn(out, outlen, in, inlen); 114 g_sink += out[0]; 115 } 116 return (double)(now_ns() - t0) / (double)iters; 117 } 118 119 static double time_batch(kf_batch_fn fn, size_t count, const uint8_t *in, 120 size_t inlen, uint8_t *out, size_t outlen, 121 size_t iters) { 122 uint64_t t0 = now_ns(); 123 for (size_t i = 0; i < iters; i++) { 124 fn(count, in, inlen, out, outlen); 125 g_sink += out[0]; 126 } 127 return (double)(now_ns() - t0) / ((double)iters * (double)count); 128 } 129 130 static double time_chain(kf_chain_api fn, size_t count, uint8_t *x, 131 size_t outlen, const uint8_t *tail, size_t tail_len, 132 size_t ctr_len, size_t iters) { 133 uint64_t t0 = now_ns(); 134 for (size_t i = 0; i < iters; i++) { 135 fn(count, x, outlen, tail, tail_len, ctr_len, 0, BENCH_LINKS); 136 g_sink += x[0]; 137 } 138 return (double)(now_ns() - t0) / 139 ((double)iters * (double)count * (double)BENCH_LINKS); 140 } 141 142 static double time_stream(kf_stream_init_fn init, kf_stream_absorb_fn absorb, 143 kf_stream_squeeze_fn squeeze, uint8_t *out, 144 size_t outlen, const uint8_t *in, size_t inlen, 145 size_t iters) { 146 uint64_t t0 = now_ns(); 147 for (size_t i = 0; i < iters; i++) { 148 kf_ctx ctx; 149 init(&ctx); 150 for (size_t off = 0; off < inlen; off += BENCH_CHUNK) { 151 size_t k = inlen - off < BENCH_CHUNK ? inlen - off : BENCH_CHUNK; 152 absorb(&ctx, in + off, k); 153 } 154 squeeze(&ctx, out, outlen); 155 g_sink += out[0]; 156 } 157 return (double)(now_ns() - t0) / (double)iters; 158 } 159 160 enum { ONE_NONE = 0, ONE_SCALAR, ONE_DISPATCH, ONE_DONOR }; 161 162 typedef struct { 163 const char *label; 164 int one; 165 kf_batch_perm_fn perm; 166 kf_batch_perm_fn perm12; 167 kf_chain_fn chain; 168 kf_chain_fn chain12; 169 int lanes; 170 kf_perm_fn sperm; 171 kf_perm_fn sperm12; 172 } row_t; 173 174 typedef struct { 175 const char *name; 176 size_t rate; 177 kf_fn donor; 178 kf_fn scalar_one; 179 kf_hash_fn *dispatched; 180 kf_batch_fn batch_fn; 181 kf_chain_api chain_fn; 182 kf_stream_init_fn sinit; 183 kf_stream_absorb_fn sabsorb; 184 kf_stream_squeeze_fn ssqueeze; 185 size_t inlen; 186 size_t outlen; 187 size_t tail_len; 188 size_t ctr_len; 189 int xof; 190 } algo_t; 191 192 static void cell(double v) { 193 if (v < 0.0) { 194 printf(" %10s", "-"); 195 } else { 196 printf(" %10.3f", v); 197 } 198 } 199 200 static double best_once(kf_fn fn, uint8_t *out, size_t outlen, 201 const uint8_t *in, size_t inlen, int reps, 202 uint64_t target_ns) { 203 double probe = time_once(fn, out, outlen, in, inlen, 20); 204 if (probe <= 0.0) probe = 1.0; 205 size_t iters = (size_t)((double)target_ns / probe); 206 if (iters < 5) iters = 5; 207 double best = -1.0; 208 for (int k = 0; k < reps; k++) { 209 double t = time_once(fn, out, outlen, in, inlen, iters); 210 if (best < 0.0 || t < best) best = t; 211 } 212 return best; 213 } 214 215 static double mibs(double ns) { 216 return (double)BENCH_STREAM * 1e9 / (ns * 1048576.0); 217 } 218 219 static void report_algo(const algo_t *a, const row_t *rows, int nrows, 220 uint8_t *in, uint8_t *out, int reps, 221 uint64_t target_ns) { 222 kf_batch_perm_fn sp = kf_batch_perm; 223 kf_batch_perm_fn sp12 = kf_batch_perm12; 224 int sl = kf_batch_lanes; 225 kf_chain_fn sc = kf_chain_kernel; 226 kf_chain_fn sc12 = kf_chain_kernel12; 227 int scl = kf_chain_lanes; 228 kf_perm_fn ss = kf_stream_perm; 229 kf_perm_fn ss12 = kf_stream_perm12; 230 231 printf("%s, rate %zu, %zu byte block\n", a->name, a->rate, a->inlen); 232 printf(" %-12s %10s %10s %10s %10s %10s %10s\n", "backend", "one-shot", 233 "batch", "chain", "absorb", "squeeze", "stream"); 234 printf(" %-12s %10s %10s %10s %10s %10s %10s\n", "", "Mh/s", "Mh/s", "Mh/s", 235 "MiB/s", "MiB/s", "MiB/s"); 236 237 for (int i = 0; i < nrows; i++) { 238 const row_t *r = &rows[i]; 239 kf_fn one = NULL; 240 double v_one = -1.0, v_bat = -1.0, v_chn = -1.0; 241 double v_abs = -1.0, v_sqz = -1.0, v_str = -1.0; 242 243 if (r->one == ONE_SCALAR) one = a->scalar_one; 244 if (r->one == ONE_DISPATCH) one = *a->dispatched; 245 if (r->one == ONE_DONOR) one = a->donor; 246 if (r->one == ONE_DONOR && a->donor == NULL) continue; 247 248 if (one != NULL) { 249 v_one = 1000.0 / best_once(one, out, a->outlen, in, a->inlen, reps, 250 target_ns); 251 v_abs = mibs(best_once(one, out, a->outlen, in, BENCH_STREAM, reps, 252 target_ns)); 253 if (a->xof) { 254 v_sqz = mibs( 255 best_once(one, out, BENCH_STREAM, in, 32, reps, target_ns)); 256 } 257 } 258 259 if (r->perm != NULL) { 260 kf_batch_perm = r->perm; 261 kf_batch_perm12 = r->perm12; 262 kf_batch_lanes = r->lanes; 263 double p = time_batch(a->batch_fn, BENCH_COUNT, in, a->inlen, out, 264 a->outlen, 200); 265 if (p <= 0.0) p = 1.0; 266 size_t it = (size_t)((double)target_ns / (p * (double)BENCH_COUNT)); 267 if (it < 10) it = 10; 268 double best = -1.0; 269 for (int k = 0; k < reps; k++) { 270 double t = time_batch(a->batch_fn, BENCH_COUNT, in, a->inlen, out, 271 a->outlen, it); 272 if (best < 0.0 || t < best) best = t; 273 } 274 v_bat = 1000.0 / best; 275 } 276 277 if (r->chain != NULL && a->chain_fn != NULL) { 278 kf_chain_kernel = r->chain; 279 kf_chain_kernel12 = r->chain12; 280 kf_chain_lanes = r->lanes; 281 double p = time_chain(a->chain_fn, BENCH_COUNT, out, a->outlen, in, 282 a->tail_len, a->ctr_len, 2); 283 if (p <= 0.0) p = 1.0; 284 size_t it = (size_t)((double)target_ns / 285 (p * (double)BENCH_COUNT * (double)BENCH_LINKS)); 286 if (it < 2) it = 2; 287 double best = -1.0; 288 for (int k = 0; k < reps; k++) { 289 double t = time_chain(a->chain_fn, BENCH_COUNT, out, a->outlen, in, 290 a->tail_len, a->ctr_len, it); 291 if (best < 0.0 || t < best) best = t; 292 } 293 v_chn = 1000.0 / best; 294 } 295 296 if (r->sperm != NULL) { 297 kf_stream_perm = r->sperm; 298 kf_stream_perm12 = r->sperm12; 299 double p = time_stream(a->sinit, a->sabsorb, a->ssqueeze, out, a->outlen, 300 in, BENCH_STREAM, 20); 301 if (p <= 0.0) p = 1.0; 302 size_t it = (size_t)((double)target_ns / p); 303 if (it < 5) it = 5; 304 double best = -1.0; 305 for (int k = 0; k < reps; k++) { 306 double t = time_stream(a->sinit, a->sabsorb, a->ssqueeze, out, 307 a->outlen, in, BENCH_STREAM, it); 308 if (best < 0.0 || t < best) best = t; 309 } 310 v_str = mibs(best); 311 } 312 313 printf(" %-12s", r->label); 314 cell(v_one); 315 cell(v_bat); 316 cell(v_chn); 317 cell(v_abs); 318 cell(v_sqz); 319 cell(v_str); 320 printf("\n"); 321 } 322 323 kf_batch_perm = sp; 324 kf_batch_perm12 = sp12; 325 kf_batch_lanes = sl; 326 kf_chain_kernel = sc; 327 kf_chain_kernel12 = sc12; 328 kf_chain_lanes = scl; 329 kf_stream_perm = ss; 330 kf_stream_perm12 = ss12; 331 printf("\n"); 332 } 333 334 static void explain(void) { 335 printf("One table per algorithm, one row per backend. Higher is better in\n"); 336 printf("every column. A dash means the backend has no kernel for that mode.\n\n"); 337 printf(" one-shot 1 hash per call, time / calls\n"); 338 printf(" kf_sha3_256(out, 32, in, inlen)\n"); 339 printf(" batch %d messages per call, time / (calls * %d)\n", BENCH_COUNT, 340 BENCH_COUNT); 341 printf(" kf_sha3_256_batch(%d, in, inlen, out, 32)\n", 342 BENCH_COUNT); 343 printf(" chain %d chains advanced %d links per call,\n", BENCH_COUNT, 344 BENCH_LINKS); 345 printf(" time / (calls * %d * %d)\n", BENCH_COUNT, BENCH_LINKS); 346 printf(" kf_sha3_256_chain(%d, x, 32, tail, t, 1, 0, %d)\n", 347 BENCH_COUNT, BENCH_LINKS); 348 printf("\n"); 349 printf("Each table fixes the hashed block, so all three modes run the same\n"); 350 printf("number of permutations per hash and the spread across them is\n"); 351 printf("per-call overhead alone. The chain tail is sized so its block\n"); 352 printf("matches the one-shot input length.\n\n"); 353 printf("The last three columns move %d KiB on a single stream instead.\n", 354 BENCH_STREAM / 1024); 355 printf("absorb and squeeze are one-shot only, stream is the same bytes\n"); 356 printf("through the streaming interface. Batch and chain exist to step a\n"); 357 printf("whole group, so the question they answer is hashes per second, not\n"); 358 printf("bytes per second down one stream.\n\n"); 359 printf(" absorb long input, short digest. What a checksum over a file\n"); 360 printf(" sees, and the number that matters for plain SHA3.\n"); 361 printf(" squeeze short input, long output. The sponge is seeded once and\n"); 362 printf(" then stepped, which is what an XOF is for and the number\n"); 363 printf(" that matters for the SHAKE variants.\n"); 364 printf(" stream %d byte chunks through kf_<algo>_absorb, then squeezed.\n", 365 BENCH_CHUNK); 366 printf("\n"); 367 printf("Both directions cost rate bytes per permutation, so rate is the\n"); 368 printf("ceiling and the tables are ordered by it.\n"); 369 printf("\n"); 370 } 371 372 int main(void) { 373 const int reps = 9; 374 const uint64_t target_ns = 25ULL * 1000000ULL; 375 const size_t in_cap = BENCH_COUNT * BENCH_STREAM; 376 const size_t out_cap = BENCH_COUNT * BENCH_STREAM; 377 uint8_t *in = malloc(in_cap); 378 uint8_t *out = malloc(out_cap); 379 int cpu = 0; 380 const char *cs = getenv("BENCH_CPU"); 381 382 if (in == NULL || out == NULL) return 1; 383 for (size_t i = 0; i < in_cap; i++) in[i] = (uint8_t)(i * 31 + 7); 384 if (cs) cpu = atoi(cs); 385 386 printf("keccak-fast backend comparison\n"); 387 printf("build: %s\n", BENCH_FLAGS); 388 if (pin_cpu(cpu) != 0) { 389 printf("# warning: could not pin to cpu %d, results will be noisier\n", cpu); 390 } else { 391 printf("pinned to cpu %d\n", cpu); 392 } 393 printf("best of %d interleaved reps, about %llu ms per rep\n", reps, 394 (unsigned long long)(target_ns / 1000000ULL)); 395 printf("dispatched: core %s, batch %s, chain %s, stream %s\n\n", 396 kf_backend_name(), kf_batch_name(), kf_chain_name(), kf_stream_name()); 397 explain(); 398 399 row_t rows[6]; 400 int nr = 0; 401 402 rows[nr++] = 403 (row_t){"donor", ONE_DONOR, NULL, NULL, NULL, NULL, 1, NULL, NULL}; 404 rows[nr++] = (row_t){"scalar", ONE_SCALAR, NULL, NULL, 405 kf_scalar_chain, kf_scalar_chain12, 1, 406 kf_scalar_perm, kf_scalar_perm12}; 407 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 408 !defined(KECCAK_FAST_NO_BMI) 409 #if defined(__clang__) 410 if (__builtin_cpu_supports("bmi")) 411 #else 412 if (__builtin_cpu_supports("bmi") && __builtin_cpu_supports("bmi2")) 413 #endif 414 rows[nr++] = (row_t){kf_backend_name(), ONE_DISPATCH, NULL, NULL, 415 kf_bmi_chain, kf_bmi_chain12, 1, 416 kf_bmi_perm, kf_bmi_perm12}; 417 #endif 418 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 419 !defined(KECCAK_FAST_NO_AVX2) 420 if (__builtin_cpu_supports("avx2")) 421 rows[nr++] = (row_t){"avx2 x4", ONE_NONE, kf_avx2_batch, 422 kf_avx2_batch12, kf_avx2_chain, kf_avx2_chain12, 4, 423 NULL, NULL}; 424 #endif 425 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 426 !defined(KECCAK_FAST_NO_AVX512) 427 if (__builtin_cpu_supports("avx512f")) 428 rows[nr++] = 429 (row_t){"avx512 x8", ONE_NONE, kf_avx512_batch, 430 kf_avx512_batch12, kf_avx512_chain, kf_avx512_chain12, 8, 431 NULL, NULL}; 432 #endif 433 #if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) 434 if (kf_armsha3_available()) 435 rows[nr++] = 436 (row_t){"arm-sha3 x2", ONE_DISPATCH, kf_armsha3_batch, 437 kf_armsha3_batch12, kf_armsha3_chain, kf_armsha3_chain12, 2, 438 kf_armsha3_perm, kf_armsha3_perm12}; 439 #endif 440 441 const algo_t algos[] = { 442 {"SHAKE128", 168, NULL, kf_scalar_shake128, &kf_shake128, 443 kf_shake128_batch, kf_shake128_chain, kf_shake128_init, 444 kf_shake128_absorb, kf_shake128_squeeze, 64, 32, 31, 1, 1}, 445 {"TurboSHAKE128", 168, NULL, kf_scalar_turboshake128, &kf_turboshake128, 446 kf_turboshake128_batch, kf_turboshake128_chain, kf_turboshake128_init, 447 kf_turboshake128_absorb, kf_turboshake128_squeeze, 64, 32, 31, 1, 1}, 448 {"SHA3-256", 136, kct_sha3_256, kf_scalar_sha3_256, &kf_sha3_256, 449 kf_sha3_256_batch, kf_sha3_256_chain, kf_sha3_256_init, 450 kf_sha3_256_absorb, kf_sha3_256_squeeze, 64, 32, 31, 1, 0}, 451 {"SHAKE256", 136, kct_shake256, kf_scalar_shake256, &kf_shake256, 452 kf_shake256_batch, kf_shake256_chain, kf_shake256_init, 453 kf_shake256_absorb, kf_shake256_squeeze, 64, 32, 31, 1, 1}, 454 {"TurboSHAKE256", 136, NULL, kf_scalar_turboshake256, &kf_turboshake256, 455 kf_turboshake256_batch, kf_turboshake256_chain, kf_turboshake256_init, 456 kf_turboshake256_absorb, kf_turboshake256_squeeze, 64, 32, 31, 1, 1}, 457 {"SHA3-512", 72, NULL, kf_scalar_sha3_512, &kf_sha3_512, 458 kf_sha3_512_batch, kf_sha3_512_chain, kf_sha3_512_init, 459 kf_sha3_512_absorb, kf_sha3_512_squeeze, 64, 64, 6, 1, 0}, 460 }; 461 462 for (size_t i = 0; i < sizeof algos / sizeof algos[0]; i++) { 463 report_algo(&algos[i], rows, nr, in, out, reps, target_ns); 464 } 465 466 printf("sink %llu\n", (unsigned long long)g_sink); 467 free(in); 468 free(out); 469 return 0; 470 }