This repository was archived by the owner on Jun 8, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathkeccak.c
More file actions
634 lines (537 loc) · 17.8 KB
/
keccak.c
File metadata and controls
634 lines (537 loc) · 17.8 KB
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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
// ethash: C/C++ implementation of Ethash, the Ethereum Proof of Work algorithm.
// Copyright 2018 Pawel Bylica.
// SPDX-License-Identifier: Apache-2.0
#include "../support/attributes.h"
#include <ethash/keccak.h>
#if !__has_builtin(__builtin_memcpy) && !defined(__GNUC__)
#include <string.h>
#define __builtin_memcpy memcpy
#define __builtin_memset memset
#endif
#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
#define to_le64(X) __builtin_bswap64(X)
#else
#define to_le64(X) X
#endif
/// Loads 64-bit integer from given memory location as little-endian number.
static inline ALWAYS_INLINE uint64_t load_le(const uint8_t* data)
{
/* memcpy is the best way of expressing the intention. Every compiler will
optimize is to single load instruction if the target architecture
supports unaligned memory access (GCC and clang even in O0).
This is great trick because we are violating C/C++ memory alignment
restrictions with no performance penalty. */
uint64_t word;
__builtin_memcpy(&word, data, sizeof(word));
return to_le64(word);
}
/// Rotates the bits of x left by the count value specified by s.
/// The s must be in range <0, 64> exclusively, otherwise the result is undefined.
static inline uint64_t rol(uint64_t x, unsigned s)
{
return (x << s) | (x >> (64 - s));
}
static const uint64_t round_constants[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};
/// The Keccak-f[1600] function.
///
/// The implementation of the Keccak-f function with 1600-bit width of the permutation (b).
/// The size of the state is also 1600 bit what gives 25 64-bit words.
///
/// @param state The state of 25 64-bit words on which the permutation is to be performed.
///
/// The implementation based on:
/// - "simple" implementation by Ronny Van Keer, included in "Reference and optimized code in C",
/// https://keccak.team/archives.html, CC0-1.0 / Public Domain.
static inline ALWAYS_INLINE void keccakf1600_implementation(uint64_t state[25])
{
uint64_t Aba, Abe, Abi, Abo, Abu;
uint64_t Aga, Age, Agi, Ago, Agu;
uint64_t Aka, Ake, Aki, Ako, Aku;
uint64_t Ama, Ame, Ami, Amo, Amu;
uint64_t Asa, Ase, Asi, Aso, Asu;
uint64_t Eba, Ebe, Ebi, Ebo, Ebu;
uint64_t Ega, Ege, Egi, Ego, Egu;
uint64_t Eka, Eke, Eki, Eko, Eku;
uint64_t Ema, Eme, Emi, Emo, Emu;
uint64_t Esa, Ese, Esi, Eso, Esu;
uint64_t Ba, Be, Bi, Bo, Bu;
uint64_t Da, De, Di, Do, Du;
Aba = state[0];
Abe = state[1];
Abi = state[2];
Abo = state[3];
Abu = state[4];
Aga = state[5];
Age = state[6];
Agi = state[7];
Ago = state[8];
Agu = state[9];
Aka = state[10];
Ake = state[11];
Aki = state[12];
Ako = state[13];
Aku = state[14];
Ama = state[15];
Ame = state[16];
Ami = state[17];
Amo = state[18];
Amu = state[19];
Asa = state[20];
Ase = state[21];
Asi = state[22];
Aso = state[23];
Asu = state[24];
for (size_t n = 0; n < 24; n += 2)
{
// Round (n + 0): Axx -> Exx
Ba = Aba ^ Aga ^ Aka ^ Ama ^ Asa;
Be = Abe ^ Age ^ Ake ^ Ame ^ Ase;
Bi = Abi ^ Agi ^ Aki ^ Ami ^ Asi;
Bo = Abo ^ Ago ^ Ako ^ Amo ^ Aso;
Bu = Abu ^ Agu ^ Aku ^ Amu ^ Asu;
Da = Bu ^ rol(Be, 1);
De = Ba ^ rol(Bi, 1);
Di = Be ^ rol(Bo, 1);
Do = Bi ^ rol(Bu, 1);
Du = Bo ^ rol(Ba, 1);
Ba = Aba ^ Da;
Be = rol(Age ^ De, 44);
Bi = rol(Aki ^ Di, 43);
Bo = rol(Amo ^ Do, 21);
Bu = rol(Asu ^ Du, 14);
Eba = Ba ^ (~Be & Bi) ^ round_constants[n];
Ebe = Be ^ (~Bi & Bo);
Ebi = Bi ^ (~Bo & Bu);
Ebo = Bo ^ (~Bu & Ba);
Ebu = Bu ^ (~Ba & Be);
Ba = rol(Abo ^ Do, 28);
Be = rol(Agu ^ Du, 20);
Bi = rol(Aka ^ Da, 3);
Bo = rol(Ame ^ De, 45);
Bu = rol(Asi ^ Di, 61);
Ega = Ba ^ (~Be & Bi);
Ege = Be ^ (~Bi & Bo);
Egi = Bi ^ (~Bo & Bu);
Ego = Bo ^ (~Bu & Ba);
Egu = Bu ^ (~Ba & Be);
Ba = rol(Abe ^ De, 1);
Be = rol(Agi ^ Di, 6);
Bi = rol(Ako ^ Do, 25);
Bo = rol(Amu ^ Du, 8);
Bu = rol(Asa ^ Da, 18);
Eka = Ba ^ (~Be & Bi);
Eke = Be ^ (~Bi & Bo);
Eki = Bi ^ (~Bo & Bu);
Eko = Bo ^ (~Bu & Ba);
Eku = Bu ^ (~Ba & Be);
Ba = rol(Abu ^ Du, 27);
Be = rol(Aga ^ Da, 36);
Bi = rol(Ake ^ De, 10);
Bo = rol(Ami ^ Di, 15);
Bu = rol(Aso ^ Do, 56);
Ema = Ba ^ (~Be & Bi);
Eme = Be ^ (~Bi & Bo);
Emi = Bi ^ (~Bo & Bu);
Emo = Bo ^ (~Bu & Ba);
Emu = Bu ^ (~Ba & Be);
Ba = rol(Abi ^ Di, 62);
Be = rol(Ago ^ Do, 55);
Bi = rol(Aku ^ Du, 39);
Bo = rol(Ama ^ Da, 41);
Bu = rol(Ase ^ De, 2);
Esa = Ba ^ (~Be & Bi);
Ese = Be ^ (~Bi & Bo);
Esi = Bi ^ (~Bo & Bu);
Eso = Bo ^ (~Bu & Ba);
Esu = Bu ^ (~Ba & Be);
// Round (n + 1): Exx -> Axx
Ba = Eba ^ Ega ^ Eka ^ Ema ^ Esa;
Be = Ebe ^ Ege ^ Eke ^ Eme ^ Ese;
Bi = Ebi ^ Egi ^ Eki ^ Emi ^ Esi;
Bo = Ebo ^ Ego ^ Eko ^ Emo ^ Eso;
Bu = Ebu ^ Egu ^ Eku ^ Emu ^ Esu;
Da = Bu ^ rol(Be, 1);
De = Ba ^ rol(Bi, 1);
Di = Be ^ rol(Bo, 1);
Do = Bi ^ rol(Bu, 1);
Du = Bo ^ rol(Ba, 1);
Ba = Eba ^ Da;
Be = rol(Ege ^ De, 44);
Bi = rol(Eki ^ Di, 43);
Bo = rol(Emo ^ Do, 21);
Bu = rol(Esu ^ Du, 14);
Aba = Ba ^ (~Be & Bi) ^ round_constants[n + 1];
Abe = Be ^ (~Bi & Bo);
Abi = Bi ^ (~Bo & Bu);
Abo = Bo ^ (~Bu & Ba);
Abu = Bu ^ (~Ba & Be);
Ba = rol(Ebo ^ Do, 28);
Be = rol(Egu ^ Du, 20);
Bi = rol(Eka ^ Da, 3);
Bo = rol(Eme ^ De, 45);
Bu = rol(Esi ^ Di, 61);
Aga = Ba ^ (~Be & Bi);
Age = Be ^ (~Bi & Bo);
Agi = Bi ^ (~Bo & Bu);
Ago = Bo ^ (~Bu & Ba);
Agu = Bu ^ (~Ba & Be);
Ba = rol(Ebe ^ De, 1);
Be = rol(Egi ^ Di, 6);
Bi = rol(Eko ^ Do, 25);
Bo = rol(Emu ^ Du, 8);
Bu = rol(Esa ^ Da, 18);
Aka = Ba ^ (~Be & Bi);
Ake = Be ^ (~Bi & Bo);
Aki = Bi ^ (~Bo & Bu);
Ako = Bo ^ (~Bu & Ba);
Aku = Bu ^ (~Ba & Be);
Ba = rol(Ebu ^ Du, 27);
Be = rol(Ega ^ Da, 36);
Bi = rol(Eke ^ De, 10);
Bo = rol(Emi ^ Di, 15);
Bu = rol(Eso ^ Do, 56);
Ama = Ba ^ (~Be & Bi);
Ame = Be ^ (~Bi & Bo);
Ami = Bi ^ (~Bo & Bu);
Amo = Bo ^ (~Bu & Ba);
Amu = Bu ^ (~Ba & Be);
Ba = rol(Ebi ^ Di, 62);
Be = rol(Ego ^ Do, 55);
Bi = rol(Eku ^ Du, 39);
Bo = rol(Ema ^ Da, 41);
Bu = rol(Ese ^ De, 2);
Asa = Ba ^ (~Be & Bi);
Ase = Be ^ (~Bi & Bo);
Asi = Bi ^ (~Bo & Bu);
Aso = Bo ^ (~Bu & Ba);
Asu = Bu ^ (~Ba & Be);
}
state[0] = Aba;
state[1] = Abe;
state[2] = Abi;
state[3] = Abo;
state[4] = Abu;
state[5] = Aga;
state[6] = Age;
state[7] = Agi;
state[8] = Ago;
state[9] = Agu;
state[10] = Aka;
state[11] = Ake;
state[12] = Aki;
state[13] = Ako;
state[14] = Aku;
state[15] = Ama;
state[16] = Ame;
state[17] = Ami;
state[18] = Amo;
state[19] = Amu;
state[20] = Asa;
state[21] = Ase;
state[22] = Asi;
state[23] = Aso;
state[24] = Asu;
}
static void keccakf1600_generic(uint64_t state[25])
{
keccakf1600_implementation(state);
}
/// The pointer to the best Keccak-f[1600] function implementation,
/// selected during runtime initialization.
static void (*keccakf1600_best)(uint64_t[25]) = keccakf1600_generic;
#if !defined(_MSC_VER) && defined(__x86_64__) && __has_attribute(target)
__attribute__((target("bmi,bmi2"))) static void keccakf1600_bmi(uint64_t state[25])
{
keccakf1600_implementation(state);
}
__attribute__((constructor)) static void select_keccakf1600_implementation(void)
{
// Init CPU information.
// This is needed on macOS because of the bug: https://bugs.llvm.org/show_bug.cgi?id=48459.
__builtin_cpu_init();
// Check if both BMI and BMI2 are supported. Some CPUs like Intel E5-2697 v2 incorrectly
// report BMI2 but not BMI being available.
if (__builtin_cpu_supports("bmi") && __builtin_cpu_supports("bmi2"))
keccakf1600_best = keccakf1600_bmi;
}
#endif
static inline ALWAYS_INLINE void keccak(
uint64_t* out, size_t bits, const uint8_t* data, size_t size)
{
static const size_t word_size = sizeof(uint64_t);
const size_t hash_size = bits / 8;
const size_t block_size = (1600 - bits * 2) / 8;
size_t i;
uint64_t* state_iter;
uint64_t last_word = 0;
uint8_t* last_word_iter = (uint8_t*)&last_word;
uint64_t state[25] = {0};
while (size >= block_size)
{
for (i = 0; i < (block_size / word_size); ++i)
{
state[i] ^= load_le(data);
data += word_size;
}
keccakf1600_best(state);
size -= block_size;
}
state_iter = state;
while (size >= word_size)
{
*state_iter ^= load_le(data);
++state_iter;
data += word_size;
size -= word_size;
}
while (size > 0)
{
*last_word_iter = *data;
++last_word_iter;
++data;
--size;
}
*last_word_iter = 0x01;
*state_iter ^= to_le64(last_word);
state[(block_size / word_size) - 1] ^= 0x8000000000000000;
keccakf1600_best(state);
for (i = 0; i < (hash_size / word_size); ++i)
out[i] = to_le64(state[i]);
}
union ethash_hash256 ethash_keccak256(const uint8_t* data, size_t size)
{
union ethash_hash256 hash;
keccak(hash.word64s, 256, data, size);
return hash;
}
union ethash_hash256 ethash_keccak256_32(const uint8_t data[32])
{
union ethash_hash256 hash;
keccak(hash.word64s, 256, data, 32);
return hash;
}
union ethash_hash512 ethash_keccak512(const uint8_t* data, size_t size)
{
union ethash_hash512 hash;
keccak(hash.word64s, 512, data, size);
return hash;
}
union ethash_hash512 ethash_keccak512_64(const uint8_t data[64])
{
union ethash_hash512 hash;
keccak(hash.word64s, 512, data, 64);
return hash;
}
static inline ALWAYS_INLINE void keccak_init(struct ethash_keccak256_context* ctx, size_t bits)
{
__builtin_memset((uint8_t*)ctx->state, 0, sizeof ctx->state);
ctx->hash_size = bits / 8;
ctx->block_size = (1600 - bits * 2) / 8;
ctx->last_word = 0;
ctx->last_word_iter = (uint8_t*)&(ctx->last_word);
ctx->state_iter = ctx->state;
}
static inline ALWAYS_INLINE void keccak_update(
struct ethash_keccak256_context* ctx, const uint8_t* data, size_t size)
{
static const size_t word_size = sizeof(uint64_t);
size_t i;
size_t block_size_b = ctx->block_size / word_size; // block size in bytes
size_t last_word_unfilled_size = // calculate unfilled space in last word
(word_size - (size_t)(ctx->last_word_iter - (uint8_t*)&(ctx->last_word)));
size_t state_unfilled_size = // calculate unfilled space in state
(block_size_b - (size_t)(ctx->state_iter - ctx->state));
// fill the last word unfilled space with bytes until it's full
while(last_word_unfilled_size > 0 && size > 0)
{
*ctx->last_word_iter = *data;
++ctx->last_word_iter;
++data;
--size;
--last_word_unfilled_size;
}
// if the last word is full, move it to state.
if(ctx->last_word_iter == (uint8_t*)&(ctx->last_word) + word_size)
{
*ctx->state_iter ^= to_le64(ctx->last_word);
++ctx->state_iter;
ctx->last_word = 0;
ctx->last_word_iter = (uint8_t*)&(ctx->last_word);
--state_unfilled_size;
}
// fill the state unfilled space with words until it's full
while(state_unfilled_size > 0 && size >= word_size)
{
*ctx->state_iter ^= load_le(data);
++ctx->state_iter;
data += word_size;
size -= word_size;
--state_unfilled_size;
}
// if the state is full, calculate keccak and reset the state iterator
if(ctx->state_iter == ctx->state + (ctx->block_size / word_size))
{
keccakf1600_best(ctx->state);
ctx->state_iter = ctx->state;
}
// if there is more data then block size, fill the whole blocks
if(size >= ctx->block_size)
{
while(size >= ctx->block_size)
{
for (i = 0; i < (ctx->block_size / word_size); ++i)
{
ctx->state[i] ^= load_le(data);
data += word_size;
}
keccakf1600_best(ctx->state);
size -= ctx->block_size;
}
ctx->state_iter = ctx->state;
}
// if there is more data then word size, fill the whole words
while (size >= word_size)
{
*ctx->state_iter ^= load_le(data);
++ctx->state_iter;
data += word_size;
size -= word_size;
}
// if there is still some data put it into last word.
while (size > 0)
{
*ctx->last_word_iter = *data;
++ctx->last_word_iter;
++data;
--size;
}
// if the last word is full, move it to state.
if(ctx->last_word_iter == (uint8_t*)&(ctx->last_word) + word_size)
{
*ctx->state_iter ^= to_le64(ctx->last_word);
++ctx->state_iter;
ctx->last_word = 0;
ctx->last_word_iter = (uint8_t*)&(ctx->last_word);
}
// if the state is full, calculate keccak and reset the state iterator
if(ctx->state_iter == ctx->state + (ctx->block_size / word_size))
{
keccakf1600_best(ctx->state);
ctx->state_iter = ctx->state;
}
}
static inline ALWAYS_INLINE void keccak_final(struct ethash_keccak256_context* ctx, uint64_t* out)
{
static const size_t word_size = sizeof(uint64_t);
size_t i;
*ctx->last_word_iter = 0x01;
*ctx->state_iter ^= to_le64(ctx->last_word);
ctx->state[(ctx->block_size / word_size) - 1] ^= 0x8000000000000000;
keccakf1600_best(ctx->state);
for (i = 0; i < (ctx->hash_size / word_size); ++i)
out[i] = to_le64(ctx->state[i]);
}
void ethash_keccak256_init(struct ethash_keccak256_context* ctx)
{
keccak_init(ctx, 256);
}
void ethash_keccak256_update(struct ethash_keccak256_context* ctx, const uint8_t* data, size_t size)
{
keccak_update(ctx, data, size);
}
union ethash_hash256 ethash_keccak256_final(struct ethash_keccak256_context* ctx)
{
union ethash_hash256 hash;
keccak_final(ctx, hash.word64s);
return hash;
}
static inline ALWAYS_INLINE void keccak_init_2(struct ethash_keccak256_context* ctx, size_t bits)
{
__builtin_memset((uint8_t*)ctx->state, 0, sizeof ctx->state);
ctx->state_iter = ctx->state;
ctx->hash_size = bits / 8;
ctx->block_size = (1600 - bits * 2) / 8;
ctx->last_word = 0;
ctx->last_word_iter = (uint8_t*)&ctx->last_word;
__builtin_memset(ctx->buffer, 0, sizeof ctx->buffer);
ctx->buffer_index = 0;
}
static inline ALWAYS_INLINE void keccak_update_2(
struct ethash_keccak256_context* ctx, const uint8_t* data, size_t size)
{
static const size_t word_size = sizeof(uint64_t);
while(size > 0)
{
size_t empty_space_size = ctx->block_size - ctx->buffer_index;
size_t data_to_load_size = size >= empty_space_size ? empty_space_size : size;
__builtin_memcpy(&ctx->buffer[ctx->buffer_index], data, data_to_load_size);
ctx->buffer_index += data_to_load_size;
size -= data_to_load_size;
data += data_to_load_size;
if(ctx->buffer_index == ctx->block_size)
{
size_t i;
uint8_t* d = &ctx->buffer[0];
for (i = 0; i < (ctx->block_size / word_size); ++i)
{
*ctx->state_iter ^= load_le(d);
++ctx->state_iter;
d += word_size;
}
keccakf1600_best(ctx->state);
ctx->state_iter = ctx->state;
ctx->buffer_index = 0;
}
}
}
static inline ALWAYS_INLINE void keccak_final_2(struct ethash_keccak256_context* ctx, uint64_t* out)
{
static const size_t word_size = sizeof(uint64_t);
size_t i;
if(ctx->buffer_index != 0)
{
uint8_t* d = ctx->buffer;
for (i = 0; i < (ctx->buffer_index / word_size); ++i)
{
*ctx->state_iter ^= load_le(d);
++ctx->state_iter;
d += word_size;
}
size_t last_word_size = ctx->buffer_index % word_size;
d = &ctx->buffer[ctx->buffer_index - last_word_size];
ctx->last_word_iter = (uint8_t*)&ctx->last_word;
while (last_word_size > 0)
{
*ctx->last_word_iter = *d;
++ctx->last_word_iter;
++d;
--last_word_size;
}
}
*ctx->last_word_iter = 0x01;
*ctx->state_iter ^= to_le64(ctx->last_word);
ctx->state[(ctx->block_size / word_size) - 1] ^= 0x8000000000000000;
keccakf1600_best(ctx->state);
for (i = 0; i < (ctx->hash_size / word_size); ++i)
out[i] = to_le64(ctx->state[i]);
}
void ethash_keccak256_init_2(struct ethash_keccak256_context* ctx)
{
keccak_init_2(ctx, 256);
}
void ethash_keccak256_update_2(struct ethash_keccak256_context* ctx, const uint8_t* data, size_t size)
{
keccak_update_2(ctx, data, size);
}
union ethash_hash256 ethash_keccak256_final_2(struct ethash_keccak256_context* ctx)
{
union ethash_hash256 hash;
keccak_final_2(ctx, hash.word64s);
return hash;
}