batch.c (4736B)
1 #include <stdint.h> 2 #include <stdio.h> 3 #include <string.h> 4 5 #include "keccak-fast.h" 6 #include "keccak-fast-internal.h" 7 8 typedef int (*kf_fn)(uint8_t *, size_t, const uint8_t *, size_t); 9 typedef int (*kf_batch_fn)(size_t, const uint8_t *, size_t, uint8_t *, size_t); 10 11 void kf_scalar_batch(uint64_t states[][25], size_t count); 12 void kf_scalar_batch12(uint64_t states[][25], size_t count); 13 14 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 15 !defined(KECCAK_FAST_NO_AVX2) 16 void kf_avx2_batch(uint64_t states[][25], size_t count); 17 void kf_avx2_batch12(uint64_t states[][25], size_t count); 18 #endif 19 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 20 !defined(KECCAK_FAST_NO_AVX512) 21 void kf_avx512_batch(uint64_t states[][25], size_t count); 22 void kf_avx512_batch12(uint64_t states[][25], size_t count); 23 #endif 24 #if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) 25 int kf_armsha3_available(void); 26 void kf_armsha3_batch(uint64_t states[][25], size_t count); 27 void kf_armsha3_batch12(uint64_t states[][25], size_t count); 28 #endif 29 30 typedef struct { 31 const char *label; 32 kf_batch_perm_fn perm; 33 kf_batch_perm_fn perm12; 34 int lanes; 35 } kernel; 36 37 static int fails; 38 static int testn; 39 40 static void check(const char *name, int ok) { 41 testn++; 42 if (ok) { 43 printf("ok %d - %s\n", testn, name); 44 } else { 45 fails++; 46 printf("not ok %d - %s\n", testn, name); 47 } 48 } 49 50 struct algo { 51 const char *name; 52 kf_fn one; 53 kf_batch_fn batch; 54 size_t outlen; 55 }; 56 57 int main(void) { 58 const struct algo algos[] = { 59 {"shake128", kf_shake128, kf_shake128_batch, 32}, 60 {"shake256", kf_shake256, kf_shake256_batch, 64}, 61 {"sha3_224", kf_sha3_224, kf_sha3_224_batch, 28}, 62 {"sha3_256", kf_sha3_256, kf_sha3_256_batch, 32}, 63 {"sha3_384", kf_sha3_384, kf_sha3_384_batch, 48}, 64 {"sha3_512", kf_sha3_512, kf_sha3_512_batch, 64}, 65 {"turboshake128", kf_turboshake128, kf_turboshake128_batch, 32}, 66 {"turboshake256", kf_turboshake256, kf_turboshake256_batch, 64}, 67 }; 68 static const size_t counts[] = {1, 3, 4, 5, 8, 17}; 69 static const size_t inlens[] = {32, 200}; 70 static uint8_t in[17 * 200]; 71 static uint8_t want[17 * 64]; 72 static uint8_t got[17 * 64]; 73 74 kernel kernels[4]; 75 int nk = 0; 76 77 kernels[nk++] = (kernel){"lanes=1", kf_scalar_batch, kf_scalar_batch12, 1}; 78 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 79 !defined(KECCAK_FAST_NO_AVX2) 80 if (__builtin_cpu_supports("avx2")) 81 kernels[nk++] = (kernel){"avx2 x4", kf_avx2_batch, kf_avx2_batch12, 4}; 82 #endif 83 #if (defined(__x86_64__) || defined(_M_X64) || defined(__i386__)) && \ 84 !defined(KECCAK_FAST_NO_AVX512) 85 if (__builtin_cpu_supports("avx512f")) 86 kernels[nk++] = 87 (kernel){"avx512 x8", kf_avx512_batch, kf_avx512_batch12, 8}; 88 #endif 89 #if defined(__aarch64__) && !defined(KECCAK_FAST_NO_ARM_SHA3) 90 if (kf_armsha3_available()) 91 kernels[nk++] = 92 (kernel){"arm-sha3 x2", kf_armsha3_batch, kf_armsha3_batch12, 2}; 93 #endif 94 95 printf("1..%zu\n", (sizeof algos / sizeof algos[0]) * 96 (sizeof counts / sizeof counts[0]) * 97 (sizeof inlens / sizeof inlens[0]) * (size_t)nk); 98 99 for (int k = 0; k < nk; k++) { 100 kf_batch_perm = kernels[k].perm; 101 kf_batch_perm12 = kernels[k].perm12; 102 kf_batch_lanes = kernels[k].lanes; 103 104 for (size_t a = 0; a < sizeof algos / sizeof algos[0]; a++) { 105 for (size_t ci = 0; ci < sizeof counts / sizeof counts[0]; ci++) { 106 size_t count = counts[ci]; 107 for (size_t li = 0; li < sizeof inlens / sizeof inlens[0]; li++) { 108 size_t inlen = inlens[li]; 109 size_t outlen = algos[a].outlen; 110 char name[96]; 111 112 for (size_t m = 0; m < count; m++) { 113 for (size_t j = 0; j < inlen; j++) { 114 in[m * inlen + j] = (uint8_t)(j * 31 + m * 7 + 1); 115 } 116 } 117 for (size_t m = 0; m < count; m++) { 118 int rc = algos[a].one(want + m * outlen, outlen, in + m * inlen, 119 inlen); 120 if (rc != 0) { 121 printf("# %s one-shot returned %d\n", algos[a].name, rc); 122 } 123 } 124 125 memset(got, 0, count * outlen); 126 int rc = algos[a].batch(count, in, inlen, got, outlen); 127 snprintf(name, sizeof name, "%s %s count=%zu inlen=%zu", 128 kernels[k].label, algos[a].name, count, inlen); 129 check(name, rc == 0 && memcmp(want, got, count * outlen) == 0); 130 } 131 } 132 } 133 } 134 135 printf("# dispatched backend: %s, batch: %s\n", kf_backend_name(), 136 kf_batch_name()); 137 138 if (fails != 0) printf("# %d failed\n", fails); 139 return fails != 0; 140 }