#include "cuda_helper.h" __device__ __constant__ uint64_t const keccak_round_constants[24] = { 0x0000000000000001ULL, 0x0000000000008082ULL, 0x800000000000808AULL, 0x8000000080008000ULL, 0x000000000000808BULL, 0x0000000080000001ULL, 0x8000000080008081ULL, 0x8000000000008009ULL, 0x000000000000008AULL, 0x0000000000000088ULL, 0x0000000080008009ULL, 0x000000008000000AULL, 0x000000008000808BULL, 0x800000000000008BULL, 0x8000000000008089ULL, 0x8000000000008003ULL, 0x8000000000008002ULL, 0x8000000000000080ULL, 0x000000000000800AULL, 0x800000008000000AULL, 0x8000000080008081ULL, 0x8000000000008080ULL, 0x0000000080000001ULL, 0x8000000080008008ULL }; #define bitselect(a, b, c) ((a) ^ ((c) & ((b) ^ (a)))) __device__ __forceinline__ void keccak_f1600_block(uint2* s, uint32_t out_size) { uint2 t[5], u, v; #pragma unroll 3 for (int i = 0; i < 24; i++) { /* theta: c = a[0,i] ^ a[1,i] ^ .. a[4,i] */ t[0] = s[0] ^ s[5] ^ s[10] ^ s[15] ^ s[20]; t[1] = s[1] ^ s[6] ^ s[11] ^ s[16] ^ s[21]; t[2] = s[2] ^ s[7] ^ s[12] ^ s[17] ^ s[22]; t[3] = s[3] ^ s[8] ^ s[13] ^ s[18] ^ s[23]; t[4] = s[4] ^ s[9] ^ s[14] ^ s[19] ^ s[24]; /* theta: d[i] = c[i+4] ^ rotl(c[i+1],1) */ /* theta: a[0,i], a[1,i], .. a[4,i] ^= d[i] */ u = t[4] ^ ROL2(t[1], 1); s[0] ^= u; s[5] ^= u; s[10] ^= u; s[15] ^= u; s[20] ^= u; u = t[0] ^ ROL2(t[2], 1); s[1] ^= u; s[6] ^= u; s[11] ^= u; s[16] ^= u; s[21] ^= u; u = t[1] ^ ROL2(t[3], 1); s[2] ^= u; s[7] ^= u; s[12] ^= u; s[17] ^= u; s[22] ^= u; u = t[2] ^ ROL2(t[4], 1); s[3] ^= u; s[8] ^= u; s[13] ^= u; s[18] ^= u; s[23] ^= u; u = t[3] ^ ROL2(t[0], 1); s[4] ^= u; s[9] ^= u; s[14] ^= u; s[19] ^= u; s[24] ^= u; /* rho pi: b[..] = rotl(a[..], ..) */ u = s[1]; s[1] = ROL2(s[6], 44); s[6] = ROL2(s[9], 20); s[9] = ROL2(s[22], 61); s[22] = ROL2(s[14], 39); s[14] = ROL2(s[20], 18); s[20] = ROL2(s[2], 62); s[2] = ROL2(s[12], 43); s[12] = ROL2(s[13], 25); s[13] = ROL2(s[19], 8); s[19] = ROL2(s[23], 56); s[23] = ROL2(s[15], 41); s[15] = ROL2(s[4], 27); s[4] = ROL2(s[24], 14); s[24] = ROL2(s[21], 2); s[21] = ROL2(s[8], 55); s[8] = ROL2(s[16], 45); s[16] = ROL2(s[5], 36); s[5] = ROL2(s[3], 28); s[3] = ROL2(s[18], 21); s[18] = ROL2(s[17], 15); s[17] = ROL2(s[11], 10); s[11] = ROL2(s[7], 6); s[7] = ROL2(s[10], 3); s[10] = ROL2(u, 1); // squeeze this in here /* chi: a[i,j] ^= ~b[i,j+1] & b[i,j+2] */ u = s[0]; v = s[1]; s[0] ^= (~v) & s[2]; /* iota: a[0,0] ^= round constant */ s[0] ^= vectorize(keccak_round_constants[i]); if (i == 23 && out_size == 1) return; // continue chi s[1] ^= (~s[2]) & s[3]; s[2] ^= (~s[3]) & s[4]; s[3] ^= (~s[4]) & u; s[4] ^= (~u) & v; u = s[5]; v = s[6]; s[5] ^= (~v) & s[7]; s[6] ^= (~s[7]) & s[8]; s[7] ^= (~s[8]) & s[9]; if (i == 23) return; s[8] ^= (~s[9]) & u; s[9] ^= (~u) & v; u = s[10]; v = s[11]; s[10] = bitselect(s[10] ^ s[12], s[10], s[11]); s[11] = bitselect(s[11] ^ s[13], s[11], s[12]); s[12] = bitselect(s[12] ^ s[14], s[12], s[13]); s[13] = bitselect(s[13] ^ u, s[13], s[14]); s[14] = bitselect(s[14] ^ v, s[14], u); u = s[15]; v = s[16]; s[15] = bitselect(s[15] ^ s[17], s[15], s[16]); s[16] = bitselect(s[16] ^ s[18], s[16], s[17]); s[17] = bitselect(s[17] ^ s[19], s[17], s[18]); s[18] = bitselect(s[18] ^ u, s[18], s[19]); s[19] = bitselect(s[19] ^ v, s[19], u); u = s[20]; v = s[21]; s[20] = bitselect(s[20] ^ s[22], s[20], s[21]); s[21] = bitselect(s[21] ^ s[23], s[21], s[22]); s[22] = bitselect(s[22] ^ s[24], s[22], s[23]); s[23] = bitselect(s[23] ^ u, s[23], s[24]); s[24] = bitselect(s[24] ^ v, s[24], u); } }