keccak-fast.c

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

scalar.c (2142B)


      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 /* The fallback must run on any x86-64: no AVX (clang vectorizes it under
     15  * -march=native and runs 1.9x slower) and no BMI (that is scalar_bmi.c). */
     16 #if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
     17 #pragma clang attribute push(                                                  \
     18     __attribute__((target("no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2"))),        \
     19     apply_to=function)
     20 #elif defined(__GNUC__) && (defined(__x86_64__) || defined(__i386__))
     21 #pragma GCC target("no-avx,no-avx2,no-avx512f,no-bmi,no-bmi2")
     22 #endif
     23 
     24 #define KF_PREFIX kf_scalar_
     25 #define KF_LINKAGE
     26 #include "scalar-impl.h"
     27 
     28 void kf_scalar_permute(uint64_t state[25]) {
     29   keccakf(state);
     30 }
     31 
     32 void kf_scalar_batch(uint64_t states[][25], size_t count) {
     33   for (size_t i = 0; i < count; i++) {
     34     keccakf(states[i]);
     35   }
     36 }
     37 
     38 void kf_scalar_batch12(uint64_t states[][25], size_t count) {
     39   for (size_t i = 0; i < count; i++) {
     40     keccak12(states[i]);
     41   }
     42 }
     43 
     44 __attribute__((constructor)) static void kf_scalar_register(void) {
     45   if (kf_backend_priority <= KF_PRIO_SCALAR) {
     46     kf_shake128 = kf_scalar_shake128;
     47     kf_shake256 = kf_scalar_shake256;
     48     kf_sha3_224 = kf_scalar_sha3_224;
     49     kf_sha3_256 = kf_scalar_sha3_256;
     50     kf_sha3_384 = kf_scalar_sha3_384;
     51     kf_sha3_512 = kf_scalar_sha3_512;
     52     kf_turboshake128 = kf_scalar_turboshake128;
     53     kf_turboshake256 = kf_scalar_turboshake256;
     54     kf_backend_priority = KF_PRIO_SCALAR;
     55     kf_backend_label    = "scalar";
     56   }
     57   if (kf_batch_priority <= KF_PRIO_SCALAR) {
     58     kf_batch_perm     = kf_scalar_batch;
     59     kf_batch_perm12   = kf_scalar_batch12;
     60     kf_batch_priority = KF_PRIO_SCALAR;
     61     kf_batch_lanes    = 1;
     62     kf_batch_label    = "scalar";
     63   }
     64 }
     65 
     66 #if defined(__clang__) && (defined(__x86_64__) || defined(__i386__))
     67 #pragma clang attribute pop
     68 #endif