keccak-fast.c

Minimal SIMD keccak implementation
git clone git://git.finwo.net/lib/keccak-fast.c
Log | Files | Refs | README | LICENSE

scalar.c (13221B)


      1 /* Keccak-f[1600] and the FIPS 202 sponge of coruus/keccak-tiny, renamed
      2  * kf_scalar_* for the dispatcher.
      3  *
      4  * Implementor: David Leon Gil. License: CC0, attribution kindly requested.
      5  * Blame taken too, but not liability.
      6  */
      7 
      8 #include <stddef.h>
      9 #include <stdint.h>
     10 #include <string.h>
     11 
     12 #include "../keccak-fast-internal.h"
     13 
     14 /* no-avx: clang vectorizes this 1.9x slower; no-bmi: that is scalar_bmi.c */
     15 #if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
     16 #pragma clang attribute push(                                                  \
     17     __attribute__((target("no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2"))),        \
     18     apply_to=function)
     19 #elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
     20 #pragma GCC target("no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2")
     21 #endif
     22 
     23 static const uint64_t RC[24] = {
     24   1ULL, 0x8082ULL, 0x800000000000808aULL, 0x8000000080008000ULL,
     25   0x808bULL, 0x80000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL,
     26   0x8aULL, 0x88ULL, 0x80008009ULL, 0x8000000aULL,
     27   0x8000808bULL, 0x800000000000008bULL, 0x8000000000008089ULL, 0x8000000000008003ULL,
     28   0x8000000000008002ULL, 0x8000000000000080ULL, 0x800aULL, 0x800000008000000aULL,
     29   0x8000000080008081ULL, 0x8000000000008080ULL, 0x80000001ULL, 0x8000000080008008ULL};
     30 
     31 #define rol(x, s) (((x) << s) | ((x) >> (64 - s)))
     32 
     33 #define KFP_LOAD(P, S) \
     34   P##0 = S[0]; \
     35   P##1 = S[1]; \
     36   P##2 = S[2]; \
     37   P##3 = S[3]; \
     38   P##4 = S[4]; \
     39   P##5 = S[5]; \
     40   P##6 = S[6]; \
     41   P##7 = S[7]; \
     42   P##8 = S[8]; \
     43   P##9 = S[9]; \
     44   P##10 = S[10]; \
     45   P##11 = S[11]; \
     46   P##12 = S[12]; \
     47   P##13 = S[13]; \
     48   P##14 = S[14]; \
     49   P##15 = S[15]; \
     50   P##16 = S[16]; \
     51   P##17 = S[17]; \
     52   P##18 = S[18]; \
     53   P##19 = S[19]; \
     54   P##20 = S[20]; \
     55   P##21 = S[21]; \
     56   P##22 = S[22]; \
     57   P##23 = S[23]; \
     58   P##24 = S[24]; \
     59 
     60 #define KFP_STORE(S, P) \
     61   S[0] = P##0; \
     62   S[1] = P##1; \
     63   S[2] = P##2; \
     64   S[3] = P##3; \
     65   S[4] = P##4; \
     66   S[5] = P##5; \
     67   S[6] = P##6; \
     68   S[7] = P##7; \
     69   S[8] = P##8; \
     70   S[9] = P##9; \
     71   S[10] = P##10; \
     72   S[11] = P##11; \
     73   S[12] = P##12; \
     74   S[13] = P##13; \
     75   S[14] = P##14; \
     76   S[15] = P##15; \
     77   S[16] = P##16; \
     78   S[17] = P##17; \
     79   S[18] = P##18; \
     80   S[19] = P##19; \
     81   S[20] = P##20; \
     82   S[21] = P##21; \
     83   S[22] = P##22; \
     84   S[23] = P##23; \
     85   S[24] = P##24; \
     86 
     87 #define KFP_INIT_C(SRC) \
     88   c0 = SRC##0 ^ SRC##5 ^ SRC##10 ^ SRC##15 ^ SRC##20; \
     89   c1 = SRC##1 ^ SRC##6 ^ SRC##11 ^ SRC##16 ^ SRC##21; \
     90   c2 = SRC##2 ^ SRC##7 ^ SRC##12 ^ SRC##17 ^ SRC##22; \
     91   c3 = SRC##3 ^ SRC##8 ^ SRC##13 ^ SRC##18 ^ SRC##23; \
     92   c4 = SRC##4 ^ SRC##9 ^ SRC##14 ^ SRC##19 ^ SRC##24; \
     93 
     94 #define KFP_R(SRC, DST, RCIDX) \
     95   d0 = c4 ^ rol(c1, 1); \
     96   d1 = c0 ^ rol(c2, 1); \
     97   d2 = c1 ^ rol(c3, 1); \
     98   d3 = c2 ^ rol(c4, 1); \
     99   d4 = c3 ^ rol(c0, 1); \
    100   SRC##0 ^= d0; \
    101   b0 = SRC##0; \
    102   SRC##6 ^= d1; \
    103   b1 = rol(SRC##6, 44); \
    104   SRC##12 ^= d2; \
    105   b2 = rol(SRC##12, 43); \
    106   SRC##18 ^= d3; \
    107   b3 = rol(SRC##18, 21); \
    108   SRC##24 ^= d4; \
    109   b4 = rol(SRC##24, 14); \
    110   DST##0 = b0 ^ ((~b1) & b2); \
    111   DST##1 = b1 ^ ((~b2) & b3); \
    112   DST##2 = b2 ^ ((~b3) & b4); \
    113   DST##3 = b3 ^ ((~b4) & b0); \
    114   DST##4 = b4 ^ ((~b0) & b1); \
    115   DST##0 ^= RC[RCIDX]; \
    116   c0 = DST##0; \
    117   c1 = DST##1; \
    118   c2 = DST##2; \
    119   c3 = DST##3; \
    120   c4 = DST##4; \
    121   SRC##3 ^= d3; \
    122   b5 = rol(SRC##3, 28); \
    123   SRC##9 ^= d4; \
    124   b6 = rol(SRC##9, 20); \
    125   SRC##10 ^= d0; \
    126   b7 = rol(SRC##10, 3); \
    127   SRC##16 ^= d1; \
    128   b8 = rol(SRC##16, 45); \
    129   SRC##22 ^= d2; \
    130   b9 = rol(SRC##22, 61); \
    131   DST##5 = b5 ^ ((~b6) & b7); \
    132   DST##6 = b6 ^ ((~b7) & b8); \
    133   DST##7 = b7 ^ ((~b8) & b9); \
    134   DST##8 = b8 ^ ((~b9) & b5); \
    135   DST##9 = b9 ^ ((~b5) & b6); \
    136   c0 ^= DST##5; \
    137   c1 ^= DST##6; \
    138   c2 ^= DST##7; \
    139   c3 ^= DST##8; \
    140   c4 ^= DST##9; \
    141   SRC##1 ^= d1; \
    142   b10 = rol(SRC##1, 1); \
    143   SRC##7 ^= d2; \
    144   b11 = rol(SRC##7, 6); \
    145   SRC##13 ^= d3; \
    146   b12 = rol(SRC##13, 25); \
    147   SRC##19 ^= d4; \
    148   b13 = rol(SRC##19, 8); \
    149   SRC##20 ^= d0; \
    150   b14 = rol(SRC##20, 18); \
    151   DST##10 = b10 ^ ((~b11) & b12); \
    152   DST##11 = b11 ^ ((~b12) & b13); \
    153   DST##12 = b12 ^ ((~b13) & b14); \
    154   DST##13 = b13 ^ ((~b14) & b10); \
    155   DST##14 = b14 ^ ((~b10) & b11); \
    156   c0 ^= DST##10; \
    157   c1 ^= DST##11; \
    158   c2 ^= DST##12; \
    159   c3 ^= DST##13; \
    160   c4 ^= DST##14; \
    161   SRC##4 ^= d4; \
    162   b15 = rol(SRC##4, 27); \
    163   SRC##5 ^= d0; \
    164   b16 = rol(SRC##5, 36); \
    165   SRC##11 ^= d1; \
    166   b17 = rol(SRC##11, 10); \
    167   SRC##17 ^= d2; \
    168   b18 = rol(SRC##17, 15); \
    169   SRC##23 ^= d3; \
    170   b19 = rol(SRC##23, 56); \
    171   DST##15 = b15 ^ ((~b16) & b17); \
    172   DST##16 = b16 ^ ((~b17) & b18); \
    173   DST##17 = b17 ^ ((~b18) & b19); \
    174   DST##18 = b18 ^ ((~b19) & b15); \
    175   DST##19 = b19 ^ ((~b15) & b16); \
    176   c0 ^= DST##15; \
    177   c1 ^= DST##16; \
    178   c2 ^= DST##17; \
    179   c3 ^= DST##18; \
    180   c4 ^= DST##19; \
    181   SRC##2 ^= d2; \
    182   b20 = rol(SRC##2, 62); \
    183   SRC##8 ^= d3; \
    184   b21 = rol(SRC##8, 55); \
    185   SRC##14 ^= d4; \
    186   b22 = rol(SRC##14, 39); \
    187   SRC##15 ^= d0; \
    188   b23 = rol(SRC##15, 41); \
    189   SRC##21 ^= d1; \
    190   b24 = rol(SRC##21, 2); \
    191   DST##20 = b20 ^ ((~b21) & b22); \
    192   DST##21 = b21 ^ ((~b22) & b23); \
    193   DST##22 = b22 ^ ((~b23) & b24); \
    194   DST##23 = b23 ^ ((~b24) & b20); \
    195   DST##24 = b24 ^ ((~b20) & b21); \
    196   c0 ^= DST##20; \
    197   c1 ^= DST##21; \
    198   c2 ^= DST##22; \
    199   c3 ^= DST##23; \
    200   c4 ^= DST##24; \
    201 
    202 #define KFP_R2(SRC, DST, I) KFP_R(SRC, DST, I) KFP_R(DST, SRC, I + 1)
    203 #define KFP_R4(SRC, DST, I) KFP_R2(SRC, DST, I) KFP_R2(SRC, DST, I + 2)
    204 
    205 #define KFP_DECL(P)                                                            \
    206   uint64_t P##0, P##1, P##2, P##3, P##4, P##5, P##6, P##7, P##8, P##9,         \
    207       P##10, P##11, P##12, P##13, P##14, P##15, P##16, P##17, P##18, P##19,    \
    208       P##20, P##21, P##22, P##23, P##24
    209 
    210 #define KFP_BODY(ROUNDS)                                                       \
    211   uint64_t *s = (uint64_t *)state;                                             \
    212   KFP_DECL(a);                                                                 \
    213   KFP_DECL(e);                                                                 \
    214   KFP_DECL(b);                                                                 \
    215   uint64_t c0, c1, c2, c3, c4, d0, d1, d2, d3, d4;                             \
    216   KFP_LOAD(a, s)                                                               \
    217   KFP_INIT_C(a)                                                                \
    218   ROUNDS                                                                       \
    219   KFP_STORE(s, a)
    220 
    221 static inline void keccakf(void *state) {
    222   KFP_BODY(for (int i = 0; i < 24; i += 4) { KFP_R4(a, e, i) })
    223 }
    224 
    225 static inline void keccak12(void *state) {
    226   KFP_BODY(for (int i = 12; i < 24; i += 4) { KFP_R4(a, e, i) })
    227 }
    228 
    229 #define _(S) do { S } while (0)
    230 #define FOR(i, ST, L, S) \
    231   _(for (size_t i = 0; i < L; i += ST) { S; })
    232 #define mkapply_ds(NAME, S) \
    233   static inline void NAME(uint8_t *dst, const uint8_t *src, size_t len) { \
    234     FOR(i, 1, len, S);                                                    \
    235   }
    236 #define mkapply_sd(NAME, S) \
    237   static inline void NAME(const uint8_t *src, uint8_t *dst, size_t len) { \
    238     FOR(i, 1, len, S);                                                    \
    239   }
    240 
    241 mkapply_ds(xorin, dst[i] ^= src[i])
    242 mkapply_sd(setout, dst[i] = src[i])
    243 
    244 #define Plen 200
    245 
    246 #define foldP(I, L, F, PERM) \
    247   while (L >= rate) {      \
    248     F(a, I, rate);         \
    249     PERM(a);               \
    250     I += rate;             \
    251     L -= rate;             \
    252   }
    253 
    254 #define KF_HASH_BODY(PERM)                                                     \
    255   uint8_t a[Plen] = {0};                                                       \
    256   foldP(in, inlen, xorin, PERM);                                               \
    257   a[inlen] ^= delim;                                                           \
    258   a[rate - 1] ^= 0x80;                                                         \
    259   xorin(a, in, inlen);                                                         \
    260   PERM(a);                                                                     \
    261   foldP(out, outlen, setout, PERM);                                            \
    262   setout(a, out, outlen);                                                      \
    263   memset(a, 0, 200);                                                           \
    264   return 0
    265 
    266 static inline int kf_hash24(uint8_t *out, size_t outlen, const uint8_t *in,
    267                             size_t inlen, size_t rate, uint8_t delim) {
    268   if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
    269     return -1;
    270   }
    271   KF_HASH_BODY(keccakf);
    272 }
    273 
    274 static inline int kf_hash12(uint8_t *out, size_t outlen, const uint8_t *in,
    275                             size_t inlen, size_t rate, uint8_t delim) {
    276   if ((out == NULL) || ((in == NULL) && inlen != 0) || (rate >= Plen)) {
    277     return -1;
    278   }
    279   KF_HASH_BODY(keccak12);
    280 }
    281 
    282 #define KF_CAT2(a, b) a##b
    283 #define KF_CAT(a, b) KF_CAT2(a, b)
    284 #define KF_FN(name) KF_CAT(KF_PREFIX, name)
    285 #define KF_PREFIX kf_scalar_
    286 
    287 #define defshake(bits)                                                         \
    288   int KF_FN(shake##bits)(uint8_t *out, size_t outlen,                          \
    289                          const uint8_t *in, size_t inlen) {                    \
    290     return kf_hash24(out, outlen, in, inlen, 200 - (bits / 4), 0x1f);          \
    291   }
    292 #define defsha3(bits)                                                          \
    293   int KF_FN(sha3_##bits)(uint8_t *out, size_t outlen,                          \
    294                          const uint8_t *in, size_t inlen) {                    \
    295     if (outlen > (bits / 8)) {                                                 \
    296       return -1;                                                               \
    297     }                                                                          \
    298     return kf_hash24(out, outlen, in, inlen, 200 - (bits / 4), 0x06);          \
    299   }
    300 #define defturboshake(bits)                                                    \
    301   int KF_FN(turboshake##bits)(uint8_t *out, size_t outlen,                     \
    302                               const uint8_t *in, size_t inlen) {               \
    303     return kf_hash12(out, outlen, in, inlen, 200 - (bits / 4), 0x1f);          \
    304   }
    305 
    306 defshake(128)
    307 defshake(256)
    308 defsha3(224)
    309 defsha3(256)
    310 defsha3(384)
    311 defsha3(512)
    312 defturboshake(128)
    313 defturboshake(256)
    314 
    315 #undef defshake
    316 #undef defsha3
    317 #undef defturboshake
    318 
    319 void kf_scalar_perm(uint64_t st[25]) {
    320   keccakf(st);
    321 }
    322 
    323 void kf_scalar_perm12(uint64_t st[25]) {
    324   keccak12(st);
    325 }
    326 
    327 void kf_scalar_batch(uint64_t states[][25], size_t count) {
    328   for (size_t i = 0; i < count; i++) {
    329     keccakf(states[i]);
    330   }
    331 }
    332 
    333 void kf_scalar_batch12(uint64_t states[][25], size_t count) {
    334   for (size_t i = 0; i < count; i++) {
    335     keccak12(states[i]);
    336   }
    337 }
    338 
    339 #define KF_DEFSCALARCHAIN(NAME, PERM)                                          \
    340   void NAME(size_t count, uint8_t *x, size_t outlen, const uint8_t *tail,      \
    341             size_t tail_len, size_t ctr_len, uint8_t ctr0, uint32_t iters,     \
    342             size_t rate, uint8_t delim) {                                      \
    343     uint64_t st[25];                                                           \
    344     for (size_t r = 0; r < count; r++) {                                       \
    345       uint8_t       *xr = x + r * outlen;                                      \
    346       const uint8_t *tr = tail_len ? tail + r * tail_len : NULL;               \
    347       for (uint32_t it = 0; it < iters; it++) {                                \
    348         kf_chain_fill(st, xr, outlen, tr, tail_len, ctr_len,                   \
    349                       (uint8_t)(ctr0 + it), rate, delim);                      \
    350         PERM(st);                                                              \
    351         memcpy(xr, st, outlen);                                                \
    352       }                                                                        \
    353     }                                                                          \
    354   }
    355 
    356 KF_DEFSCALARCHAIN(kf_scalar_chain, keccakf)
    357 KF_DEFSCALARCHAIN(kf_scalar_chain12, keccak12)
    358 
    359 #undef KF_DEFSCALARCHAIN
    360 
    361 __attribute__((constructor)) static void kf_scalar_register(void) {
    362   if (kf_backend_priority <= 100) {
    363     kf_shake128 = kf_scalar_shake128;
    364     kf_shake256 = kf_scalar_shake256;
    365     kf_sha3_224 = kf_scalar_sha3_224;
    366     kf_sha3_256 = kf_scalar_sha3_256;
    367     kf_sha3_384 = kf_scalar_sha3_384;
    368     kf_sha3_512 = kf_scalar_sha3_512;
    369     kf_turboshake128 = kf_scalar_turboshake128;
    370     kf_turboshake256 = kf_scalar_turboshake256;
    371     kf_backend_priority = 100;
    372     kf_backend_label    = "scalar";
    373   }
    374   if (kf_batch_priority <= 100) {
    375     kf_batch_perm     = kf_scalar_batch;
    376     kf_batch_perm12   = kf_scalar_batch12;
    377     kf_batch_priority = 100;
    378     kf_batch_lanes    = 1;
    379     kf_batch_label    = "scalar";
    380   }
    381   if (kf_chain_priority <= 100) {
    382     kf_chain_kernel   = kf_scalar_chain;
    383     kf_chain_kernel12 = kf_scalar_chain12;
    384     kf_chain_priority = 100;
    385     kf_chain_lanes    = 1;
    386     kf_chain_label    = "scalar";
    387   }
    388   if (kf_stream_priority <= 100) {
    389     kf_stream_perm     = kf_scalar_perm;
    390     kf_stream_perm12   = kf_scalar_perm12;
    391     kf_stream_priority = 100;
    392     kf_stream_label    = "scalar";
    393   }
    394 }
    395 
    396 #if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
    397 #pragma clang attribute pop
    398 #endif