-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathkeccak.cu
360 lines (307 loc) · 10.3 KB
/
keccak.cu
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
/*
* keccak.cu Implementation of Keccak/SHA3 digest
*
* Date: 12 June 2019
* Revision: 1
*
* This file is released into the Public Domain.
*/
extern "C"
{
#include "keccak.cuh"
}
#define KECCAK_ROUND 24
#define KECCAK_STATE_SIZE 25
#define KECCAK_Q_SIZE 192
__constant__ LONG CUDA_KECCAK_CONSTS[24] = { 0x0000000000000001, 0x0000000000008082,
0x800000000000808a, 0x8000000080008000, 0x000000000000808b, 0x0000000080000001, 0x8000000080008081,
0x8000000000008009, 0x000000000000008a, 0x0000000000000088, 0x0000000080008009, 0x000000008000000a,
0x000000008000808b, 0x800000000000008b, 0x8000000000008089, 0x8000000000008003, 0x8000000000008002,
0x8000000000000080, 0x000000000000800a, 0x800000008000000a, 0x8000000080008081, 0x8000000000008080,
0x0000000080000001, 0x8000000080008008 };
typedef struct {
BYTE sha3_flag;
WORD digestbitlen;
LONG rate_bits;
LONG rate_BYTEs;
LONG absorb_round;
int64_t state[KECCAK_STATE_SIZE];
BYTE q[KECCAK_Q_SIZE];
LONG bits_in_queue;
} cuda_keccak_ctx_t;
typedef cuda_keccak_ctx_t CUDA_KECCAK_CTX;
__device__ LONG cuda_keccak_leuint64(void *in)
{
LONG a;
memcpy(&a, in, 8);
return a;
}
__device__ int64_t cuda_keccak_MIN(int64_t a, int64_t b)
{
if (a > b) return b;
return a;
}
__device__ LONG cuda_keccak_UMIN(LONG a, LONG b)
{
if (a > b) return b;
return a;
}
__device__ void cuda_keccak_extract(cuda_keccak_ctx_t *ctx)
{
LONG len = ctx->rate_bits >> 6;
int64_t a;
int s = sizeof(LONG);
for (int i = 0;i < len;i++) {
a = cuda_keccak_leuint64((int64_t*)&ctx->state[i]);
memcpy(ctx->q + (i * s), &a, s);
}
}
__device__ __forceinline__ LONG cuda_keccak_ROTL64(LONG a, LONG b)
{
return (a << b) | (a >> (64 - b));
}
__device__ void cuda_keccak_permutations(cuda_keccak_ctx_t * ctx)
{
int64_t* A = ctx->state;;
int64_t *a00 = A, *a01 = A + 1, *a02 = A + 2, *a03 = A + 3, *a04 = A + 4;
int64_t *a05 = A + 5, *a06 = A + 6, *a07 = A + 7, *a08 = A + 8, *a09 = A + 9;
int64_t *a10 = A + 10, *a11 = A + 11, *a12 = A + 12, *a13 = A + 13, *a14 = A + 14;
int64_t *a15 = A + 15, *a16 = A + 16, *a17 = A + 17, *a18 = A + 18, *a19 = A + 19;
int64_t *a20 = A + 20, *a21 = A + 21, *a22 = A + 22, *a23 = A + 23, *a24 = A + 24;
for (int i = 0; i < KECCAK_ROUND; i++) {
/* Theta */
int64_t c0 = *a00 ^ *a05 ^ *a10 ^ *a15 ^ *a20;
int64_t c1 = *a01 ^ *a06 ^ *a11 ^ *a16 ^ *a21;
int64_t c2 = *a02 ^ *a07 ^ *a12 ^ *a17 ^ *a22;
int64_t c3 = *a03 ^ *a08 ^ *a13 ^ *a18 ^ *a23;
int64_t c4 = *a04 ^ *a09 ^ *a14 ^ *a19 ^ *a24;
int64_t d1 = cuda_keccak_ROTL64(c1, 1) ^ c4;
int64_t d2 = cuda_keccak_ROTL64(c2, 1) ^ c0;
int64_t d3 = cuda_keccak_ROTL64(c3, 1) ^ c1;
int64_t d4 = cuda_keccak_ROTL64(c4, 1) ^ c2;
int64_t d0 = cuda_keccak_ROTL64(c0, 1) ^ c3;
*a00 ^= d1;
*a05 ^= d1;
*a10 ^= d1;
*a15 ^= d1;
*a20 ^= d1;
*a01 ^= d2;
*a06 ^= d2;
*a11 ^= d2;
*a16 ^= d2;
*a21 ^= d2;
*a02 ^= d3;
*a07 ^= d3;
*a12 ^= d3;
*a17 ^= d3;
*a22 ^= d3;
*a03 ^= d4;
*a08 ^= d4;
*a13 ^= d4;
*a18 ^= d4;
*a23 ^= d4;
*a04 ^= d0;
*a09 ^= d0;
*a14 ^= d0;
*a19 ^= d0;
*a24 ^= d0;
/* Rho pi */
c1 = cuda_keccak_ROTL64(*a01, 1);
*a01 = cuda_keccak_ROTL64(*a06, 44);
*a06 = cuda_keccak_ROTL64(*a09, 20);
*a09 = cuda_keccak_ROTL64(*a22, 61);
*a22 = cuda_keccak_ROTL64(*a14, 39);
*a14 = cuda_keccak_ROTL64(*a20, 18);
*a20 = cuda_keccak_ROTL64(*a02, 62);
*a02 = cuda_keccak_ROTL64(*a12, 43);
*a12 = cuda_keccak_ROTL64(*a13, 25);
*a13 = cuda_keccak_ROTL64(*a19, 8);
*a19 = cuda_keccak_ROTL64(*a23, 56);
*a23 = cuda_keccak_ROTL64(*a15, 41);
*a15 = cuda_keccak_ROTL64(*a04, 27);
*a04 = cuda_keccak_ROTL64(*a24, 14);
*a24 = cuda_keccak_ROTL64(*a21, 2);
*a21 = cuda_keccak_ROTL64(*a08, 55);
*a08 = cuda_keccak_ROTL64(*a16, 45);
*a16 = cuda_keccak_ROTL64(*a05, 36);
*a05 = cuda_keccak_ROTL64(*a03, 28);
*a03 = cuda_keccak_ROTL64(*a18, 21);
*a18 = cuda_keccak_ROTL64(*a17, 15);
*a17 = cuda_keccak_ROTL64(*a11, 10);
*a11 = cuda_keccak_ROTL64(*a07, 6);
*a07 = cuda_keccak_ROTL64(*a10, 3);
*a10 = c1;
/* Chi */
c0 = *a00 ^ (~*a01 & *a02);
c1 = *a01 ^ (~*a02 & *a03);
*a02 ^= ~*a03 & *a04;
*a03 ^= ~*a04 & *a00;
*a04 ^= ~*a00 & *a01;
*a00 = c0;
*a01 = c1;
c0 = *a05 ^ (~*a06 & *a07);
c1 = *a06 ^ (~*a07 & *a08);
*a07 ^= ~*a08 & *a09;
*a08 ^= ~*a09 & *a05;
*a09 ^= ~*a05 & *a06;
*a05 = c0;
*a06 = c1;
c0 = *a10 ^ (~*a11 & *a12);
c1 = *a11 ^ (~*a12 & *a13);
*a12 ^= ~*a13 & *a14;
*a13 ^= ~*a14 & *a10;
*a14 ^= ~*a10 & *a11;
*a10 = c0;
*a11 = c1;
c0 = *a15 ^ (~*a16 & *a17);
c1 = *a16 ^ (~*a17 & *a18);
*a17 ^= ~*a18 & *a19;
*a18 ^= ~*a19 & *a15;
*a19 ^= ~*a15 & *a16;
*a15 = c0;
*a16 = c1;
c0 = *a20 ^ (~*a21 & *a22);
c1 = *a21 ^ (~*a22 & *a23);
*a22 ^= ~*a23 & *a24;
*a23 ^= ~*a24 & *a20;
*a24 ^= ~*a20 & *a21;
*a20 = c0;
*a21 = c1;
/* Iota */
*a00 ^= CUDA_KECCAK_CONSTS[i];
}
}
__device__ void cuda_keccak_absorb(cuda_keccak_ctx_t *ctx, BYTE* in)
{
LONG offset = 0;
for (LONG i = 0; i < ctx->absorb_round; ++i) {
ctx->state[i] ^= cuda_keccak_leuint64(in + offset);
offset += 8;
}
cuda_keccak_permutations(ctx);
}
__device__ void cuda_keccak_pad(cuda_keccak_ctx_t *ctx)
{
ctx->q[ctx->bits_in_queue >> 3] |= (1L << (ctx->bits_in_queue & 7));
if (++(ctx->bits_in_queue) == ctx->rate_bits) {
cuda_keccak_absorb(ctx, ctx->q);
ctx->bits_in_queue = 0;
}
LONG full = ctx->bits_in_queue >> 6;
LONG partial = ctx->bits_in_queue & 63;
LONG offset = 0;
for (int i = 0; i < full; ++i) {
ctx->state[i] ^= cuda_keccak_leuint64(ctx->q + offset);
offset += 8;
}
if (partial > 0) {
LONG mask = (1L << partial) - 1;
ctx->state[full] ^= cuda_keccak_leuint64(ctx->q + offset) & mask;
}
ctx->state[(ctx->rate_bits - 1) >> 6] ^= 9223372036854775808ULL;/* 1 << 63 */
cuda_keccak_permutations(ctx);
cuda_keccak_extract(ctx);
ctx->bits_in_queue = ctx->rate_bits;
}
/*
* Digestbitlen must be 128 224 256 288 384 512
*/
__device__ void cuda_keccak_init(cuda_keccak_ctx_t *ctx, WORD digestbitlen)
{
memset(ctx, 0, sizeof(cuda_keccak_ctx_t));
ctx->sha3_flag = 0;
ctx->digestbitlen = digestbitlen;
ctx->rate_bits = 1600 - ((ctx->digestbitlen) << 1);
ctx->rate_BYTEs = ctx->rate_bits >> 3;
ctx->absorb_round = ctx->rate_bits >> 6;
ctx->bits_in_queue = 0;
}
/*
* Digestbitlen must be 224 256 384 512
*/
__device__ void cuda_keccak_sha3_init(cuda_keccak_ctx_t *ctx, WORD digestbitlen)
{
cuda_keccak_init(ctx, digestbitlen);
ctx->sha3_flag = 1;
}
__device__ void cuda_keccak_update(cuda_keccak_ctx_t *ctx, BYTE *in, LONG inlen)
{
int64_t BYTEs = ctx->bits_in_queue >> 3;
int64_t count = 0;
while (count < inlen) {
if (BYTEs == 0 && count <= ((int64_t)(inlen - ctx->rate_BYTEs))) {
do {
cuda_keccak_absorb(ctx, in + count);
count += ctx->rate_BYTEs;
} while (count <= ((int64_t)(inlen - ctx->rate_BYTEs)));
} else {
int64_t partial = cuda_keccak_MIN(ctx->rate_BYTEs - BYTEs, inlen - count);
memcpy(ctx->q + BYTEs, in + count, partial);
BYTEs += partial;
count += partial;
if (BYTEs == ctx->rate_BYTEs) {
cuda_keccak_absorb(ctx, ctx->q);
BYTEs = 0;
}
}
}
ctx->bits_in_queue = BYTEs << 3;
}
__device__ void cuda_keccak_final(cuda_keccak_ctx_t *ctx, BYTE *out)
{
if (ctx->sha3_flag) {
int mask = (1 << 2) - 1;
ctx->q[ctx->bits_in_queue >> 3] = (BYTE)(0x02 & mask);
ctx->bits_in_queue += 2;
}
cuda_keccak_pad(ctx);
LONG i = 0;
while (i < ctx->digestbitlen) {
if (ctx->bits_in_queue == 0) {
cuda_keccak_permutations(ctx);
cuda_keccak_extract(ctx);
ctx->bits_in_queue = ctx->rate_bits;
}
LONG partial_block = cuda_keccak_UMIN(ctx->bits_in_queue, ctx->digestbitlen - i);
memcpy(out + (i >> 3), ctx->q + (ctx->rate_BYTEs - (ctx->bits_in_queue >> 3)), partial_block >> 3);
ctx->bits_in_queue -= partial_block;
i += partial_block;
}
}
__global__ void kernel_keccak_hash(BYTE* indata, WORD inlen, BYTE* outdata, WORD n_batch, WORD KECCAK_BLOCK_SIZE)
{
WORD thread = blockIdx.x * blockDim.x + threadIdx.x;
if (thread >= n_batch)
{
return;
}
BYTE* in = indata + thread * inlen;
BYTE* out = outdata + thread * KECCAK_BLOCK_SIZE;
CUDA_KECCAK_CTX ctx;
cuda_keccak_init(&ctx, KECCAK_BLOCK_SIZE << 3);
cuda_keccak_update(&ctx, in, inlen);
cuda_keccak_final(&ctx, out);
}
extern "C"
{
void mcm_cuda_keccak_hash_batch(BYTE * in, WORD inlen, BYTE * out, WORD n_outbit, WORD n_batch)
{
BYTE * cuda_indata;
BYTE * cuda_outdata;
const WORD KECCAK_BLOCK_SIZE = (n_outbit >> 3);
cudaMalloc(&cuda_indata, inlen * n_batch);
cudaMalloc(&cuda_outdata, KECCAK_BLOCK_SIZE * n_batch);
cudaMemcpy(cuda_indata, in, inlen * n_batch, cudaMemcpyHostToDevice);
WORD thread = 256;
WORD block = (n_batch + thread - 1) / thread;
kernel_keccak_hash << < block, thread >> > (cuda_indata, inlen, cuda_outdata, n_batch, KECCAK_BLOCK_SIZE);
cudaMemcpy(out, cuda_outdata, KECCAK_BLOCK_SIZE * n_batch, cudaMemcpyDeviceToHost);
cudaDeviceSynchronize();
cudaError_t error = cudaGetLastError();
if (error != cudaSuccess) {
printf("Error cuda keccak hash: %s \n", cudaGetErrorString(error));
}
cudaFree(cuda_indata);
cudaFree(cuda_outdata);
}
}