marsupial-sys 0.2.0

ffi crate for the xkcp/k12 implementation of the kangarootwelve hash function
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*
K12 based on the eXtended Keccak Code Package (XKCP)
https://github.com/XKCP/XKCP

The Keccak-p permutations, designed by Guido Bertoni, Joan Daemen, Michaƫl Peeters and Gilles Van Assche.

Implementation by Gilles Van Assche and Ronny Van Keer, hereby denoted as "the implementer".

For more information, feedback or questions, please refer to the Keccak Team website:
https://keccak.team/

To the extent possible under law, the implementer has waived all copyright
and related or neighboring rights to the source code in this file.
http://creativecommons.org/publicdomain/zero/1.0/

---

Please refer to the XKCP for more details.
*/

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include "brg_endian.h"
#include "KeccakP-1600-SnP.h"

#ifdef KeccakP1600_disableParallelism
#undef KeccakP1600_enable_simd_options
#else

// Forward declaration
void KangarooTwelve_SetProcessorCapabilities();
#ifdef KeccakP1600_enable_simd_options
int K12_SSSE3_requested_disabled = 0;
int K12_AVX2_requested_disabled = 0;
int K12_AVX512_requested_disabled = 0;
#endif  // KeccakP1600_enable_simd_options
int K12_enableSSSE3 = 0;
int K12_enableAVX2 = 0;
int K12_enableAVX512 = 0;

/* ---------------------------------------------------------------- */

void KT128_SSSE3_Process2Leaves(const unsigned char *input, unsigned char *output);
void KT128_AVX512_Process2Leaves(const unsigned char *input, unsigned char *output);

void KT256_SSSE3_Process2Leaves(const unsigned char *input, unsigned char *output);
void KT256_AVX512_Process2Leaves(const unsigned char *input, unsigned char *output);

int KeccakP1600times2_IsAvailable()
{
    int result = 0;
    result |= K12_enableAVX512;
    result |= K12_enableSSSE3;
    return result;
}

const char * KeccakP1600times2_GetImplementation()
{
    if (K12_enableAVX512) {
        return "AVX-512 implementation";
    } else if (K12_enableSSSE3) {
        return "SSSE3 implementation";
    } else {
        return "";
    }
}

void KT128_Process2Leaves(const unsigned char *input, unsigned char *output)
{
    if (K12_enableAVX512) {
        KT128_AVX512_Process2Leaves(input, output);
    } else if (K12_enableSSSE3) {
        KT128_SSSE3_Process2Leaves(input, output);
    }
}

void KT256_Process2Leaves(const unsigned char *input, unsigned char *output)
{
    if (K12_enableAVX512) {
        KT256_AVX512_Process2Leaves(input, output);
    } else if (K12_enableSSSE3) {
        KT256_SSSE3_Process2Leaves(input, output);
    }
}


void KT128_AVX2_Process4Leaves(const unsigned char *input, unsigned char *output);
void KT128_AVX512_Process4Leaves(const unsigned char *input, unsigned char *output);

void KT256_AVX2_Process4Leaves(const unsigned char *input, unsigned char *output);
void KT256_AVX512_Process4Leaves(const unsigned char *input, unsigned char *output);

int KeccakP1600times4_IsAvailable()
{
    int result = 0;
    result |= K12_enableAVX512;
    result |= K12_enableAVX2;
    return result;
}

const char * KeccakP1600times4_GetImplementation()
{
    if (K12_enableAVX512) {
        return "AVX-512 implementation";
    } else if (K12_enableAVX2) {
        return "AVX2 implementation";
    } else {
        return "";
    }
}

void KT128_Process4Leaves(const unsigned char *input, unsigned char *output)
{
    if (K12_enableAVX512) {
        KT128_AVX512_Process4Leaves(input, output);
    } else if (K12_enableAVX2) {
        KT128_AVX2_Process4Leaves(input, output);
    }
}

void KT256_Process4Leaves(const unsigned char *input, unsigned char *output)
{
    if (K12_enableAVX512) {
        KT256_AVX512_Process4Leaves(input, output);
    } else if (K12_enableAVX2) {
        KT256_AVX2_Process4Leaves(input, output);
    }
}

void KT128_AVX512_Process8Leaves(const unsigned char *input, unsigned char *output);

void KT256_AVX512_Process8Leaves(const unsigned char *input, unsigned char *output);

int KeccakP1600times8_IsAvailable()
{
    int result = 0;
    result |= K12_enableAVX512;
    return result;
}

const char * KeccakP1600times8_GetImplementation()
{
    if (K12_enableAVX512) {
        return "AVX-512 implementation";
    } else {
        return "";
    }
}

void KT128_Process8Leaves(const unsigned char *input, unsigned char *output)
{
    if (K12_enableAVX512)
        KT128_AVX512_Process8Leaves(input, output);
}

void KT256_Process8Leaves(const unsigned char *input, unsigned char *output)
{
    if (K12_enableAVX512)
        KT256_AVX512_Process8Leaves(input, output);
}

#endif  // KeccakP1600_disableParallelism

const char * KeccakP1600_GetImplementation()
{
    if (K12_enableAVX512)
        return "AVX-512 implementation";
    else
#ifndef KeccakP1600_noAssembly
    if (K12_enableAVX2)
        return "AVX2 implementation";
    else
#endif
        return "generic 64-bit implementation";
}

void KeccakP1600_Initialize(void *state)
{
    KangarooTwelve_SetProcessorCapabilities();
    if (K12_enableAVX512)
        KeccakP1600_AVX512_Initialize(state);
    else
#ifndef KeccakP1600_noAssembly
    if (K12_enableAVX2)
        KeccakP1600_AVX2_Initialize(state);
    else
#endif
        KeccakP1600_opt64_Initialize(state);
}

void KeccakP1600_AddByte(void *state, unsigned char data, unsigned int offset)
{
    if (K12_enableAVX512)
        ((unsigned char*)(state))[offset] ^= data;
    else
#ifndef KeccakP1600_noAssembly
    if (K12_enableAVX2)
        KeccakP1600_AVX2_AddByte(state, data, offset);
    else
#endif
        KeccakP1600_opt64_AddByte(state, data, offset);
}

void KeccakP1600_AddBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
{
    if (K12_enableAVX512)
        KeccakP1600_AVX512_AddBytes(state, data, offset, length);
    else
#ifndef KeccakP1600_noAssembly
    if (K12_enableAVX2)
        KeccakP1600_AVX2_AddBytes(state, data, offset, length);
    else
#endif
        KeccakP1600_opt64_AddBytes(state, data, offset, length);
}

void KeccakP1600_Permute_12rounds(void *state)
{
    if (K12_enableAVX512)
        KeccakP1600_AVX512_Permute_12rounds(state);
    else
#ifndef KeccakP1600_noAssembly
    if (K12_enableAVX2)
        KeccakP1600_AVX2_Permute_12rounds(state);
    else
#endif
        KeccakP1600_opt64_Permute_12rounds(state);
}

void KeccakP1600_ExtractBytes(const void *state, unsigned char *data, unsigned int offset, unsigned int length)
{
    if (K12_enableAVX512)
        KeccakP1600_AVX512_ExtractBytes(state, data, offset, length);
    else
#ifndef KeccakP1600_noAssembly
    if (K12_enableAVX2)
        KeccakP1600_AVX2_ExtractBytes(state, data, offset, length);
    else
#endif
        KeccakP1600_opt64_ExtractBytes(state, data, offset, length);
}

size_t KeccakP1600_12rounds_FastLoop_Absorb(void *state, unsigned int laneCount, const unsigned char *data, size_t dataByteLen)
{
    if (K12_enableAVX512)
        return KeccakP1600_AVX512_12rounds_FastLoop_Absorb(state, laneCount, data, dataByteLen);
    else
#ifndef KeccakP1600_noAssembly
    if (K12_enableAVX2)
        return KeccakP1600_AVX2_12rounds_FastLoop_Absorb(state, laneCount, data, dataByteLen);
    else
#endif
        return KeccakP1600_opt64_12rounds_FastLoop_Absorb(state, laneCount, data, dataByteLen);
}

/* ---------------------------------------------------------------- */

/* Processor capability detection code by Samuel Neves and Jack O'Connor, see
 * https://github.com/BLAKE3-team/BLAKE3/blob/master/c/blake3_dispatch.c
 */

#if defined(__x86_64__) || defined(_M_X64)
#define IS_X86
#define IS_X86_64
#endif

#if defined(__i386__) || defined(_M_IX86)
#define IS_X86
#define IS_X86_32
#endif

#if defined(IS_X86)
static uint64_t xgetbv() {
#if defined(_MSC_VER)
  return _xgetbv(0);
#else
  uint32_t eax = 0, edx = 0;
  __asm__ __volatile__("xgetbv\n" : "=a"(eax), "=d"(edx) : "c"(0));
  return ((uint64_t)edx << 32) | eax;
#endif
}

static void cpuid(uint32_t out[4], uint32_t id) {
#if defined(_MSC_VER)
  __cpuid((int *)out, id);
#elif defined(__i386__) || defined(_M_IX86)
  __asm__ __volatile__("movl %%ebx, %1\n"
                       "cpuid\n"
                       "xchgl %1, %%ebx\n"
                       : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3])
                       : "a"(id));
#else
  __asm__ __volatile__("cpuid\n"
                       : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3])
                       : "a"(id));
#endif
}

static void cpuidex(uint32_t out[4], uint32_t id, uint32_t sid) {
#if defined(_MSC_VER)
  __cpuidex((int *)out, id, sid);
#elif defined(__i386__) || defined(_M_IX86)
  __asm__ __volatile__("movl %%ebx, %1\n"
                       "cpuid\n"
                       "xchgl %1, %%ebx\n"
                       : "=a"(out[0]), "=r"(out[1]), "=c"(out[2]), "=d"(out[3])
                       : "a"(id), "c"(sid));
#else
  __asm__ __volatile__("cpuid\n"
                       : "=a"(out[0]), "=b"(out[1]), "=c"(out[2]), "=d"(out[3])
                       : "a"(id), "c"(sid));
#endif
}

#endif

enum cpu_feature {
  SSE2 = 1 << 0,
  SSSE3 = 1 << 1,
  SSE41 = 1 << 2,
  AVX = 1 << 3,
  AVX2 = 1 << 4,
  AVX512F = 1 << 5,
  AVX512VL = 1 << 6,
  /* ... */
  UNDEFINED = 1 << 30
};

static enum cpu_feature g_cpu_features = UNDEFINED;

static enum cpu_feature
    get_cpu_features(void) {

  if (g_cpu_features != UNDEFINED) {
    return g_cpu_features;
  } else {
#if defined(IS_X86)
    uint32_t regs[4] = {0};
    uint32_t *eax = &regs[0], *ebx = &regs[1], *ecx = &regs[2], *edx = &regs[3];
    (void)edx;
    enum cpu_feature features = 0;
    cpuid(regs, 0);
    const int max_id = *eax;
    cpuid(regs, 1);
#if defined(__amd64__) || defined(_M_X64)
    features |= SSE2;
#else
    if (*edx & (1UL << 26))
      features |= SSE2;
#endif
    if (*ecx & (1UL << 9))
      features |= SSSE3;
    if (*ecx & (1UL << 19))
      features |= SSE41;

    if (*ecx & (1UL << 27)) { // OSXSAVE
      const uint64_t mask = xgetbv();
      if ((mask & 6) == 6) { // SSE and AVX states
        if (*ecx & (1UL << 28))
          features |= AVX;
        if (max_id >= 7) {
          cpuidex(regs, 7, 0);
          if (*ebx & (1UL << 5))
            features |= AVX2;
          if ((mask & 224) == 224) { // Opmask, ZMM_Hi256, Hi16_Zmm
            if (*ebx & (1UL << 31))
              features |= AVX512VL;
            if (*ebx & (1UL << 16))
              features |= AVX512F;
          }
        }
      }
    }
    g_cpu_features = features;
    return features;
#else
    /* How to detect NEON? */
    return 0;
#endif
  }
}

void KangarooTwelve_SetProcessorCapabilities()
{
    enum cpu_feature features = get_cpu_features();
    K12_enableSSSE3 = (features & SSSE3);
    K12_enableAVX2 = (features & AVX2);
    K12_enableAVX512 = (features & AVX512F) && (features & AVX512VL);
#ifdef KeccakP1600_enable_simd_options
    K12_enableSSSE3 = K12_enableSSSE3 && !K12_SSSE3_requested_disabled;
    K12_enableAVX2 = K12_enableAVX2 && !K12_AVX2_requested_disabled;
    K12_enableAVX512 = K12_enableAVX512 && !K12_AVX512_requested_disabled;
#endif  // KeccakP1600_enable_simd_options
}

#ifdef KeccakP1600_enable_simd_options
int KangarooTwelve_DisableSSSE3(void) {
    KangarooTwelve_SetProcessorCapabilities();
    K12_SSSE3_requested_disabled = 1;
    if (K12_enableSSSE3) {
        KangarooTwelve_SetProcessorCapabilities();
        return 1;  // SSSE3 was disabled on this call.
    } else {
        return 0;  // Nothing changed.
    }
}

int KangarooTwelve_DisableAVX2(void) {
    KangarooTwelve_SetProcessorCapabilities();
    K12_AVX2_requested_disabled = 1;
    if (K12_enableAVX2) {
        KangarooTwelve_SetProcessorCapabilities();
        return 1;  // AVX2 was disabled on this call.
    } else {
        return 0;  // Nothing changed.
    }
}

int KangarooTwelve_DisableAVX512(void) {
    KangarooTwelve_SetProcessorCapabilities();
    K12_AVX512_requested_disabled = 1;
    if (K12_enableAVX512) {
        KangarooTwelve_SetProcessorCapabilities();
        return 1;  // AVX512 was disabled on this call.
    } else {
        return 0;  // Nothing changed.
    }
}

void KangarooTwelve_EnableAllCpuFeatures(void) {
    K12_SSSE3_requested_disabled = 0;
    K12_AVX2_requested_disabled = 0;
    K12_AVX512_requested_disabled = 0;
    KangarooTwelve_SetProcessorCapabilities();
}
#endif  // KeccakP1600_enable_simd_options