Check-in [8761eb293a]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Add ARMv8 neon implementation
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8761eb293a63771cd8ce34ff6c0314b2df3e14c2
User & Date: bernd 2020-01-12 21:38:52
Context
2020-01-12
21:45
Add ARMv8 neon implementation check-in: 3ffc9ee36e user: bernd tags: trunk
21:38
Add ARMv8 neon implementation check-in: 8761eb293a user: bernd tags: trunk
21:09
Improve haves check-in: 90b0958714 user: bernd tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Added keccakp/KeccakF-1600-armv8a-neon.c.





















































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
/*
The Keccak sponge function, designed by Guido Bertoni, Joan Daemen,
Micha√ęl Peeters and Gilles Van Assche. For more information, feedback or
questions, please refer to our website: http://keccak.noekeon.org/

Implementation by the designers,
hereby denoted as "the implementer".

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/
*/

#define STATEI unsigned long long int
#include <string.h>
#include "brg_endian.h"
#include "KeccakF-1600-opt64-settings.h"
#include "KeccakF-1600.h"

extern void KeccakP1600_Permute(void * state, int round);
extern void KeccakP1600_Initialize(void * state);

void KeccakF(keccak_state state, int round)
{
  KeccakP1600_Permute(state, round);
}

void KeccakInitializeState(keccak_state state)
{
  KeccakP1600_Initialize(state);
}

void KeccakInitialize()
{
}

void KeccakExtract(keccak_state state, UINT64 *data, int byteCount)
{
  memmove(data, state, byteCount);
}

void KeccakAbsorb(keccak_state state, UINT64 *data, int byteCount)
{
  int i;
  keccak_state datai;
  memmove(datai, data, byteCount);
  for(i=0; i<byteCount-7; i+=8) {
    state[i>>3] ^= datai[i>>3];
  }
  if(byteCount & 7) {
    UINT64 m = 0xffffffffffffffffull >> ((8-byteCount) & 7)*8;
    state[i>>3] ^= datai[i>>3] & m;
  }
}

void KeccakEncrypt(keccak_state state, UINT64 *data, int byteCount)
{
  int i;
  keccak_state datai;
  memmove(datai, data, byteCount);
  for(i=0; i<byteCount-7; i+=8) {
    datai[i>>3] = state[i>>3] ^= datai[i>>3];
  }
  if(byteCount & 7) {
    UINT64 m = 0xffffffffffffffffull >> ((8-byteCount) & 7)*8;
    state[i>>3] ^= datai[i>>3] & m;
    datai[i>>3] = state[i>>3];
  }
  memmove(data, datai, byteCount);
}

void KeccakDecrypt(keccak_state state, UINT64 *data, int byteCount)
{
  int i;
  UINT64 tmp;
  keccak_state datai;
  memmove(datai, data, byteCount);
  for(i=0; i<byteCount-7; i+=8) {
    tmp = datai[i>>3] ^ state[i>>3];
    state[i>>3] = datai[i>>3];
    datai[i>>3] = tmp;
  }
  if(byteCount & 7) {
    UINT64 m = 0xffffffffffffffffull >> ((8-byteCount) & 7)*8;
    tmp = datai[i>>3] ^ state[i>>3];
    state[i>>3] = (datai[i>>3] & m) | (state[i>>3] & ~m);
    datai[i>>3] = tmp;
  }
  memmove(data, datai, byteCount);
}

Added keccakp/KeccakF-1600-armv8a-neon.s.



















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
// Implementation by Andre Moraes
//
// This file implements Keccak-p[1600] in a SnP-compatible way.
// Please refer to SnP-documentation.h for more details.
//
// This implementation comes with KeccakP-1600-SnP.h in the same folder.
// Please refer to LowLevel.build for the exact list of other files it must be combined with.

// INFO: Tested on Cortex-A53(odroid-c2), using gcc.
// WARNING: These functions work only on little endian CPU with ARMv8a + NEON architecture
// WARNING: State must be 512 bit (64 bytes) aligned.
// WARNING: Don't use V8-V15 or X19-X28 since we aren't saving them

// Note that byte order, same as the Keyakv2 Convection:
// v19 = A[0] || A[4]
// v19.2d[0] = A[0]
// v19.2d[1] = A[4]

// Register-Lane Lookup
// v19 = A[0]  || A[4]
// v20 = A[1]  || A[5]
// v21 = A[2]  || A[6]
// v22 = A[3]  || A[7]

// v23 = A[8]  || A[12]
// v24 = A[9]  || A[13]
// v25 = A[10] || A[14]
// v26 = A[11] || A[15]

// v27 = A[16] || A[20]
// v28 = A[17] || A[21]
// v29 = A[18] || A[22]
// v30 = A[19] || A[23]

// v31 = A[24] || ?????

// Transpose
// trn1 v0.2d, v19.2d, v20.2d
// trn2 v2.2d, v19.2d, v20.2d
// v0  = A[0] || A[1]
// v1  = A[4] || A[5]

// Extract
// ext v0.16b, v19.16b, v20.16b, #8
// v0  = A[4] || A[1]

.macro    LoadState
    ld4     { v19.2d, v20.2d, v21.2d, v22.2d }, [x0], #64
    ld4     { v23.2d, v24.2d, v25.2d, v26.2d }, [x0], #64
    ld4     { v27.2d, v28.2d, v29.2d, v30.2d }, [x0], #64
    ld1     { v31.d }[0], [x0], #8
    sub     x0, x0, #200
    movi    v16.2d, #0
    .endm

.macro    StoreState
    st4     { v19.2d, v20.2d, v21.2d, v22.2d }, [x0], #64
    st4     { v23.2d, v24.2d, v25.2d, v26.2d }, [x0], #64
    st4     { v27.2d, v28.2d, v29.2d, v30.2d }, [x0], #64
    st1     { v31.d }[0], [x0], #8
    .endm

.macro    RhoPi dst, src, sav, rot
    ror     \src, \src, #64-\rot
    mov     \sav, \dst
    mov     \dst, \src
    .endm

// NEON has no BIT-wise vector rotate operation
.macro    ROTL64  dst, src, rot
    .if (\rot & 7) != 0                 // Bit-wise rotation
    shl     \dst\().2d, \src\().2d, #\rot
    sri     \dst\().2d, \src\().2d, #64-\rot
    .else                               // Byte-wise rotation, we can use EXT
    ext     \dst\().16b, \src\().16b, \src\().16b, #\rot/8
    .endif
    .endm

.macro    KeccakRound
    // Theta - Build new lanes
    eor     v0.16b, v19.16b, v25.16b      // v0 = (A[0] ^ A[10]) || (A[4] ^ A[14])
    eor     v1.16b, v20.16b, v26.16b      // v1 = (A[1] ^ A[11]) || (A[5] ^ A[15])
    eor     v2.16b, v21.16b, v28.16b      // v2 = (A[2] ^ A[17]) || (A[6] ^ A[21])
    eor     v3.16b, v22.16b, v23.16b      // v3 = (A[3] ^ A[8])  || (A[7] ^ A[12])
    eor     v4.16b, v24.16b, v30.16b      // v4 = (A[9] ^ A[19]) || (A[13] ^ A[23])

    eor     v1.16b, v1.16b,  v27.16b      // v1 = (A[1] ^ A[11] ^ A[16]) || (A[5] ^ A[15] ^ A[20])
    eor     v3.16b, v3.16b,  v29.16b      // v3 = (A[3] ^ A[8] ^ A[18])  || (A[7] ^ A[12] ^ A[22])

    trn1    v5.2d, v0.2d, v1.2d           // v5 = (A[0] ^ A[10]) || (A[1] ^ A[11] ^ A[16])
    trn2    v6.2d, v1.2d, v2.2d           // v6 = (A[5] ^ A[15] ^ A[20]) ||  (A[6] ^ A[21])
    eor     v1.16b, v5.16b, v6.16b        // v1 = B[0] || B[1]

    ext     v5.16b, v4.16b, v2.16b, #8    // v5 = (A[13] ^ A[23]) || (A[2] ^ A[17])
    eor     v3.16b, v3.16b, v5.16b        // v3 = B[3] || B[2]

    mov     v5.2d[0], v0.2d[1]            // v5 = (A[4] ^ A[14]) || ????
    eor     v4.16b, v4.16b, v5.16b        // v4 = (A[9] ^ A[19] ^ A[4] ^ A[14]) || ????
    eor     v4.16b, v4.16b, v31.16b       // v4 = B[4] || ????

    ext     v2.16b, v1.16b, v1.16b, #8    // v2 = B[1] || B[0]
    mov     v4.2d[1], v3.2d[0]            // v4 = B[4] || B[3]
    trn2    v0.2d, v3.2d, v1.2d           // v0 = B[2] || B[1]

    ROTL64  v5, v2, 1                     // v5 = ROTL64(B[1], 1) || ROTL64(B[0], 1)
    ROTL64  v6, v3, 1                     // v6 = ROTL64(B[3], 1) || ROTL64(B[2], 1)
    ROTL64  v7, v4, 1                     // v7 = ROTL64(B[4], 1) || ROTL64(B[3], 1)

    eor     v18.16b, v4.16b, v5.16b       // v18 = B[4] ^ ROTL64(B[1], 1) || B[3] ^ ROTL64(B[0], 1)
    eor     v2.16b, v2.16b, v6.16b        // v2  = B[1] ^ ROTL64(B[3], 1) || B[0] ^ ROTL64(B[2], 1)
    eor     v0.16b, v0.16b, v7.16b        // v0  = B[2] ^ ROTL64(B[4], 1) || B[1] ^ ROTL64(B[3], 1)

    ext     v7.16b, v5.16b, v7.16b, #8    // v7 = ROTL64(B[0], 1) || ROTL64(B[4], 1)
    eor     v7.16b, v3.16b, v7.16b        // v7 = B[3] ^ ROTL64(B[0], 1) || B[2] ^ ROTL64(B[4], 1)

    ext     v6.16b, v6.16b, v5.16b, #8    // v6 = ROTL64(B[2], 1) || ROTL64(B[1], 1)
    trn1    v4.2d, v1.2d, v4.2d           // v4 = B[0] || B[4]
    eor     v6.16b, v4.16b, v6.16b        // v6 = B[0] ^ ROTL64(B[2], 1) || B[4] ^ ROTL64(B[1], 1)

    // Theta - Apply lanes
    eor     v19.16b, v19.16b, v18.16b     // A[0]  ^= B[4] ^ ROTL64(B[1], 1), A[4] ^= B[3] ^ ROTL64(B[0], 1)
    eor     v20.16b, v20.16b, v6.16b      // A[1]  ^= B[0] ^ ROTL64(B[2], 1), A[5] ^= B[4] ^ ROTL64(B[1], 1)
    eor     v21.16b, v21.16b, v2.16b      // A[2]  ^= B[1] ^ ROTL64(B[3], 1), A[6] ^= B[0] ^ ROTL64(B[2], 1)
    eor     v22.16b, v22.16b, v0.16b      // A[3]  ^= B[2] ^ ROTL64(B[4], 1), A[7] ^= B[1] ^ ROTL64(B[3], 1)
    eor     v23.16b, v23.16b, v0.16b      // A[8]  ^= B[2] ^ ROTL64(B[4], 1), A[12] ^= B[1] ^ ROTL64(B[3], 1)
    eor     v24.16b, v24.16b, v7.16b      // A[9]  ^= B[3] ^ ROTL64(B[0], 1), A[13] ^= B[2] ^ ROTL64(B[4], 1)
    eor     v25.16b, v25.16b, v18.16b     // A[10] ^= B[4] ^ ROTL64(B[1], 1), A[14] ^= B[3] ^ ROTL64(B[0], 1)
    eor     v26.16b, v26.16b, v6.16b      // A[11] ^= B[0] ^ ROTL64(B[2], 1), A[15] ^= B[4] ^ ROTL64(B[1], 1)
    eor     v27.16b, v27.16b, v6.16b      // A[16] ^= B[0] ^ ROTL64(B[2], 1), A[20] ^= B[4] ^ ROTL64(B[1], 1)
    eor     v28.16b, v28.16b, v2.16b      // A[17] ^= B[1] ^ ROTL64(B[3], 1), A[21] ^= B[0] ^ ROTL64(B[2], 1)
    eor     v29.16b, v29.16b, v0.16b      // A[18] ^= B[2] ^ ROTL64(B[4], 1), A[22] ^= B[1] ^ ROTL64(B[3], 1)
    eor     v30.16b, v30.16b, v7.16b      // A[19] ^= B[3] ^ ROTL64(B[0], 1), A[23] ^= B[2] ^ ROTL64(B[4], 1)
    eor     v31.16b, v31.16b, v7.16b      // A[24] ^= B[3] ^ ROTL64(B[0], 1), ????

    // Rho Pi
    mov     x11, v20.2d[0]                // x11   = A[1]

    RhoPi   v25.2d[0], x11, x10, 1        // A[10] = ROTL64(A[1], 1)
    RhoPi   v22.2d[1], x10, x11, 3        // A[7]  = ROTL64(A[10], 3)
    RhoPi   v26.2d[0], x11, x10, 6        // A[11] = ROTL64(A[7], 6)
    RhoPi   v28.2d[0], x10, x11, 10       // A[17] = ROTL64(A[11], 10)
    RhoPi   v29.2d[0], x11, x10, 15       // A[18] = ROTL64(A[17], 15)
    RhoPi   v22.2d[0], x10, x11, 21       // A[3]  = ROTL64(A[18], 21)
    RhoPi   v20.2d[1], x11, x10, 28       // A[5]  = ROTL64(A[3], 28)
    RhoPi   v27.2d[0], x10, x11, 36       // A[16] = ROTL64(A[5], 36)
    RhoPi   v23.2d[0], x11, x10, 45       // A[8]  = ROTL64(A[16], 45)
    RhoPi   v28.2d[1], x10, x11, 55       // A[21] = ROTL64(A[8], 55)
    RhoPi   v31.2d[0], x11, x10, 2        // A[24] = ROTL64(A[21], 2)
    RhoPi   v19.2d[1], x10, x11, 14       // A[4]  = ROTL64(A[24], 14)
    RhoPi   v26.2d[1], x11, x10, 27       // A[15] = ROTL64(A[4], 27)
    RhoPi   v30.2d[1], x10, x11, 41       // A[23] = ROTL64(A[15], 41)
    RhoPi   v30.2d[0], x11, x10, 56       // A[19] = ROTL64(A[23], 56)
    RhoPi   v24.2d[1], x10, x11, 8        // A[13] = ROTL64(A[19], 8)
    RhoPi   v23.2d[1], x11, x10, 25       // A[12] = ROTL64(A[13], 25)
    RhoPi   v21.2d[0], x10, x11, 43       // A[2]  = ROTL64(A[12], 43)
    RhoPi   v27.2d[1], x11, x10, 62       // A[20] = ROTL64(A[2], 62)
    RhoPi   v25.2d[1], x10, x11, 18       // A[14] = ROTL64(A[20], 18)
    RhoPi   v29.2d[1], x11, x10, 39       // A[22] = ROTL64(A[14], 39)
    RhoPi   v24.2d[0], x10, x11, 61       // A[9]  = ROTL64(A[22], 61)
    RhoPi   v21.2d[1], x11, x10, 20       // A[6]  = ROTL64(A[9], 20)

    ror     x10, x10, #20
    mov     v20.2d[0], x10                // A[1]  = ROTL64(A[6], 44)

    // Chi - Some lanes are applied earlier so we can reuse registers
    ext     v18.16b, v26.16b, v31.16b, #8 // v18 = A[15] || A[24]
    bic     v6.16b, v27.16b, v18.16b      // v6 = ~A[15] & A[16] || ~A[24] & A[20]

    ext     v17.16b, v26.16b, v31.16b, #8 // v17 =  A[15] ||  A[24]
    bic     v5.16b, v17.16b, v30.16b      // v5 = ~A[19] & A[15] || ~A[23] & A[24]

    bic     v3.16b, v30.16b, v29.16b      // v3 = ~A[18] & A[19] || ~A[22] & A[23]

    eor     v30.16b, v30.16b, v6.16b      // A[19] ^= ~A[15] & A[16], A[23] ^= ~A[24] & A[20]

    trn1    v18.2d, v26.2d, v25.2d        // v18 =  A[11] ||  A[10]
    ext     v17.16b, v23.16b, v26.16b, #8 // v17 =  A[12] ||  A[11]
    bic     v7.16b, v17.16b, v18.16b      // v7  =  ~A[11] & A[12] || ~A[10] & A[11]

    trn2    v18.2d, v20.2d, v25.2d        // v18 =  A[5]  ||  A[14]
    ext     v17.16b, v21.16b, v25.16b, #8 // v17 =  A[6]  ||  A[10]
    bic     v6.16b, v17.16b, v18.16b      // v6  = ~A[5] & A[6] || ~A[14] & A[10]

    trn1    v18.2d, v20.2d, v19.2d        // v18 =  A[1] ||  A[0]
    trn1    v17.2d, v21.2d, v20.2d        // v17 =  A[2] ||  A[1]
    bic     v1.16b, v17.16b, v18.16b      // v1  = ~A[1] & A[2] || ~A[0] & A[1]

    ext     v18.16b, v19.16b, v23.16b, #8 // v18 =  A[4] ||  A[8]
    trn1    v17.2d, v19.2d, v24.2d        // v17 =  A[0] ||  A[9]
    bic     v0.16b, v17.16b, v18.16b      // v0  = ~A[4] & A[0] || ~A[8] & A[9]

    ext     v18.16b, v23.16b, v27.16b, #8 // v18 =  A[12] ||  A[16]
    ext     v17.16b, v24.16b, v28.16b, #8 // v17 =  A[13] ||  A[17]
    bic     v4.16b, v17.16b, v18.16b      // v4  = ~A[12] & A[13] || ~A[16] & A[17]

    mov     v18.2d[0], v27.2d[1]          // v18 =  A[20] || ????
    mov     v17.2d[0], v28.2d[1]          // v17 =  A[21] || ????
    bic     v2.16b, v17.16b, v18.16b      // v2  = ~A[20] & A[21] || ????
    eor     v31.16b, v31.16b, v2.16b      // A[24] ^= ~A[20] & A[21], ????

    bic     v2.16b, v29.16b, v28.16b      // v2  = ~A[17] & A[18] || ~A[21] & A[22]
    eor     v27.16b, v27.16b, v2.16b      // A[16] ^= ~A[17] & A[18], A[20] ^= ~A[21] & A[22]

    bic     v2.16b, v22.16b, v21.16b      // v2  = ~A[2]  & A[3]  || ~A[6]  & A[7]

    eor     v28.16b, v28.16b, v3.16b      // A[17] ^= ~A[18] & A[19], A[21] ^= ~A[22] & A[23]
    eor     v29.16b, v29.16b, v5.16b      // A[18] ^= ~A[19] & A[15], A[22] ^= ~A[23] & A[24]

    ext     v17.16b, v19.16b, v23.16b, #8 // v17 =  A[4] || A[8]
    bic     v3.16b, v17.16b, v22.16b      // v3  = ~A[3]  & A[4]  || ~A[7]  & A[8]

    trn2    v17.2d, v20.2d, v25.2d        // v17 =  A[5]  || A[14]
    bic     v5.16b, v17.16b, v24.16b      // v5  = ~A[9]  & A[5]  || ~A[13] & A[14]

    // Chi - Apply remaining lanes
    eor     v19.16b, v19.16b, v1.16b      // A[0] ^= ~A[1] & A[2], A[4] ^= ~A[0] & A[1]
    eor     v20.16b, v20.16b, v2.16b      // A[1] ^= ~A[2] & A[3], A[5] ^= ~A[6] & A[7]
    eor     v21.16b, v21.16b, v3.16b      // A[2] ^= ~A[3] & A[4], A[6] ^= ~A[7] & A[8]
    eor     v22.16b, v22.16b, v0.16b      // A[3] ^= ~A[4] & A[0], A[7] ^= ~A[8] & A[9]
    eor     v23.16b, v23.16b, v5.16b      // A[8] ^= ~A[9] & A[5], A[12] ^= ~A[13] & A[14]
    eor     v24.16b, v24.16b, v6.16b      // A[9] ^= ~A[5] & A[6], A[13] ^= ~A[14] & A[10]
    eor     v25.16b, v25.16b, v7.16b      // A[10] ^= ~A[11] & A[12], A[14] ^= ~A[10] & A[11]
    eor     v26.16b, v26.16b, v4.16b      // A[11] ^= ~A[12] & A[13], A[15] ^= ~A[16] & A[17]

    // Iota
    ld1     { v16.d }[0], [x1], #8
    eor     v19.16b, v19.16b, v16.16b
    .endm

.align 8
KeccakP1600_Permute_RoundConstants24:
    .quad   0x0000000000000001
    .quad   0x0000000000008082
    .quad   0x800000000000808a
    .quad   0x8000000080008000
    .quad   0x000000000000808b
    .quad   0x0000000080000001
    .quad   0x8000000080008081
    .quad   0x8000000000008009
    .quad   0x000000000000008a
    .quad   0x0000000000000088
    .quad   0x0000000080008009
    .quad   0x000000008000000a
KeccakP1600_Permute_RoundConstants12:
    .quad   0x000000008000808b
    .quad   0x800000000000008b
    .quad   0x8000000000008089
    .quad   0x8000000000008003
    .quad   0x8000000000008002
    .quad   0x8000000000000080
    .quad   0x000000000000800a
    .quad   0x800000008000000a
    .quad   0x8000000080008081
    .quad   0x8000000000008080
    .quad   0x0000000080000001
    .quad   0x8000000080008008
KeccakP1600_Permute_RoundConstants0:

//----------------------------------------------------------------------------
//
// void KeccakP1600_Initialize(void *state)
//
.align 8
.global   KeccakP1600_Initialize
KeccakP1600_Initialize:
    movi    v0.2d, #0
    movi    v1.2d, #0
    movi    v2.2d, #0
    movi    v3.2d, #0
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64  // Clear 8lanes=64 bytes at a time
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64
    st1     { v0.d }[0], [x0], #8
    ret


// ----------------------------------------------------------------------------
//
//  void KeccakP1600_AddByte(void *state, unsigned char byte, unsigned int offset)
//
.align 8
.global   KeccakP1600_AddByte
KeccakP1600_AddByte:
    ldrb    w3, [x0, x2]
    eor     w3, w3, w1
    strb    w3, [x0, x2]
    ret


// ----------------------------------------------------------------------------
//
//  void KeccakP1600_AddBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
//
.align 8
.global   KeccakP1600_AddBytes
KeccakP1600_AddBytes:
    add     x0, x0, x2
    subs    w4, w3, #1
    b.cc    KeccakP1600_AddBytes_Exit // length 0, move along
KeccakP1600_AddBytes_8LanesLoop: // Go 8 lanes=64 bytes at a time
    subs    w3, w3, #64
    b.cc    KeccakP1600_AddBytes_Lanes // Jump if length is negative
    ld4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0]
    ld4     { v4.2d, v5.2d, v6.2d, v7.2d }, [x1], #64
    eor     v0.16b, v0.16b, v4.16b
    eor     v1.16b, v1.16b, v5.16b
    eor     v2.16b, v2.16b, v6.16b
    eor     v3.16b, v3.16b, v7.16b
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64
    b       KeccakP1600_AddBytes_8LanesLoop
KeccakP1600_AddBytes_Lanes: // If length ever becomes negative, we have to fix it
    add     w3, w3, #64
KeccakP1600_AddBytes_LanesLoop: // Same thing but go 1 lanes=8 bytes at a time
    subs    w3, w3, #8
    b.cc    KeccakP1600_AddBytes_Bytes
    ld1     { v0.d }[0], [x0]
    ld1     { v4.d }[0], [x1], #8
    eor     v0.8b, v0.8b, v4.8b
    st1     { v0.d }[0], [x0], #8
    b       KeccakP1600_AddBytes_LanesLoop
KeccakP1600_AddBytes_Bytes:
    add     w3, w3, #8
KeccakP1600_AddBytes_BytesLoop: // Same thing but go 1 byte at a time
    subs    w3, w3, #1
    b.cc    KeccakP1600_AddBytes_Exit
    ldrb    w4, [x0]
    ldrb    w5, [x1], #1
    eor     w4, w4, w5
    strb    w4, [x0], #1
    b       KeccakP1600_AddBytes_BytesLoop
KeccakP1600_AddBytes_Exit:
    ret

// ----------------------------------------------------------------------------
//
//  void KeccakP1600_OverwriteBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
//
.align 8
.global   KeccakP1600_OverwriteBytes
KeccakP1600_OverwriteBytes:
    add     x0, x0, x2
    subs    w4, w3, #1
    b.cc    KeccakP1600_OverwriteBytes_Exit
KeccakP1600_OverwriteBytes_8LanesLoop:
    subs    w3, w3, #64
    b.cc    KeccakP1600_OverwriteBytes_Lanes
    ld4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x1], #64
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64
    b       KeccakP1600_OverwriteBytes_8LanesLoop
KeccakP1600_OverwriteBytes_Lanes:
    add    w3, w3, #64
KeccakP1600_OverwriteBytes_LanesLoop:
    subs    w3, w3, #8
    b.cc    KeccakP1600_OverwriteBytes_Bytes
    ld1     { v0.d }[0], [x1], #8
    st1     { v0.d }[0], [x0], #8
    b       KeccakP1600_OverwriteBytes_LanesLoop
KeccakP1600_OverwriteBytes_Bytes:
    add    w3, w3, #8
KeccakP1600_OverwriteBytes_BytesLoop:
    subs    w3, w3, #1
    b.cc    KeccakP1600_OverwriteBytes_Exit
    ldrb    w4, [x1], #1
    strb    w4, [x0], #1
    b       KeccakP1600_OverwriteBytes_BytesLoop
KeccakP1600_OverwriteBytes_Exit:
    ret


//----------------------------------------------------------------------------
//
// void KeccakP1600_OverwriteWithZeroes(void *state, unsigned int byteCount)
//
.align 8
.global   KeccakP1600_OverwriteWithZeroes
KeccakP1600_OverwriteWithZeroes:
    subs    w2, w1, #1
    b.cc    KeccakP1600_OverwriteWithZeroes_Exit
    movi    v0.2d, #0
    movi    v1.2d, #0
    movi    v2.2d, #0
    movi    v3.2d, #0
    mov     w2, #0
KeccakP1600_OverwriteWithZeroes_8LanesLoop:
    subs    w1, w1, #64
    b.cc    KeccakP1600_OverwriteWithZeroes_Lanes
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64
    b       KeccakP1600_OverwriteWithZeroes_8LanesLoop
KeccakP1600_OverwriteWithZeroes_Lanes:
    add     w1, w1, #64
KeccakP1600_OverwriteWithZeroes_LanesLoop:
    subs    w1, w1, #8
    b.cc    KeccakP1600_OverwriteWithZeroes_Bytes
    st1     { v0.d }[0], [x0], #8
    b       KeccakP1600_OverwriteWithZeroes_LanesLoop
KeccakP1600_OverwriteWithZeroes_Bytes:
    add     w1, w1, #8
KeccakP1600_OverwriteWithZeroes_LoopBytes:
    subs    w1, w1, #1
    b.cc    KeccakP1600_OverwriteWithZeroes_Exit
    strb    w2, [x0], #1
    b       KeccakP1600_OverwriteWithZeroes_LoopBytes
KeccakP1600_OverwriteWithZeroes_Exit:
    ret


// ----------------------------------------------------------------------------
//
//  void KeccakP1600_ExtractBytes(void *state, const unsigned char *data, unsigned int offset, unsigned int length)
//
.align 8
.global   KeccakP1600_ExtractBytes
KeccakP1600_ExtractBytes:
    add     x0, x0, x2
    subs    w4, w3, #1
    b.cc    KeccakP1600_ExtractBytes_Exit
KeccakP1600_ExtractBytes_8LanesLoop:
    subs    w3, w3, #64
    b.cc    KeccakP1600_ExtractBytes_Lanes
    ld4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x1], #64
    b       KeccakP1600_ExtractBytes_8LanesLoop
KeccakP1600_ExtractBytes_Lanes:
    add     w3, w3, #64
KeccakP1600_ExtractBytes_LanesLoop:
    subs    w3, w3, #8
    b.cc    KeccakP1600_ExtractBytes_Bytes
    ld1     { v0.d }[0], [x0], #8
    st1     { v0.d }[0], [x1], #8
    b       KeccakP1600_ExtractBytes_LanesLoop
KeccakP1600_ExtractBytes_Bytes:
    add     w3, w3, #8
KeccakP1600_ExtractBytes_BytesLoop:
    subs    w3, w3, #1
    b.cc    KeccakP1600_ExtractBytes_Exit
    ldrb    w4, [x0], #1
    strb    w4, [x1], #1
    b       KeccakP1600_ExtractBytes_BytesLoop
KeccakP1600_ExtractBytes_Exit:
    ret


// ----------------------------------------------------------------------------
//
//  void KeccakP800_ExtractAndAddBytes(void *state, const unsigned char *input, unsigned char *output, unsigned int offset, unsigned int length)
//
.align 8
.global   KeccakP1600_ExtractAndAddBytes
KeccakP1600_ExtractAndAddBytes:
    add     x0, x0, x3
    subs    w5, w4, #1
    b.cc    KeccakP1600_ExtractAndAddBytes_Exit
KeccakP1600_ExtractAndAddBytes_8LanesLoop:
    subs    w4, w4, #64
    b.cc    KeccakP1600_ExtractAndAddBytes_Lanes
    ld4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x0], #64
    ld4     { v4.2d, v5.2d, v6.2d, v7.2d }, [x1], #64
    eor     v0.16b, v0.16b, v4.16b
    eor     v1.16b, v1.16b, v5.16b
    eor     v2.16b, v2.16b, v6.16b
    eor     v3.16b, v3.16b, v7.16b
    st4     { v0.2d, v1.2d, v2.2d, v3.2d }, [x2], #64
    b       KeccakP1600_ExtractAndAddBytes_8LanesLoop
KeccakP1600_ExtractAndAddBytes_Lanes:
    add     w4, w4, #64
KeccakP1600_ExtractAndAddBytes_LanesLoop:
    subs    w4, w4, #8
    b.cc    KeccakP1600_ExtractAndAddBytes_Bytes
    ld1     { v0.d }[0], [x0], #8
    ld1     { v4.d }[0], [x1], #8
    eor     v0.8b, v0.8b, v4.8b
    st1     { v0.d }[0], [x2], #8
    b       KeccakP1600_ExtractAndAddBytes_LanesLoop
KeccakP1600_ExtractAndAddBytes_Bytes:
    add     w4, w4, #8
KeccakP1600_ExtractAndAddBytes_BytesLoop:
    subs    w4, w4, #1
    b.cc    KeccakP1600_ExtractAndAddBytes_Exit
    ldrb    w5, [x0], #1
    ldrb    w6, [x1], #1
    eor     w5, w5, w6
    strb    w5, [x2], #1
    b       KeccakP1600_ExtractAndAddBytes_BytesLoop
KeccakP1600_ExtractAndAddBytes_Exit:
    ret

// ----------------------------------------------------------------------------
//
//  void KeccakP1600_Permute_Nrounds( void *state, unsigned int nrounds )
//
.align 8
.global   KeccakP1600_Permute_Nrounds
KeccakP1600_Permute_Nrounds:
    mov     x2, x1
    adr     x1, KeccakP1600_Permute_RoundConstants0
	lsl		x3, x2, #3
	sub		x1, x1, x3
    b       KeccakP1600_Permute

// ----------------------------------------------------------------------------
//
//  void KeccakP1600_Permute_12rounds( void *state )
//
.align 8
.global   KeccakP1600_Permute_12rounds
KeccakP1600_Permute_12rounds:
    adr     x1, KeccakP1600_Permute_RoundConstants12
    mov     x2, #12
    b       KeccakP1600_Permute


// ----------------------------------------------------------------------------
//
//  void KeccakP1600_Permute_24rounds( void *state )
//
.align 8
.global   KeccakP1600_Permute_24rounds
KeccakP1600_Permute_24rounds:
    adr     x1, KeccakP1600_Permute_RoundConstants24
    mov     x2, #24
    b       KeccakP1600_Permute

//----------------------------------------------------------------------------
//
// void KeccakP1600_Permute( void *state, uint64_t *rc, unsigned int nrounds )
//
.align 8
.global   KeccakP1600_Permute
KeccakP1600_Permute:
    LoadState
KeccakP1600_Permute_RoundLoop:
    KeccakRound
    subs    w2, w2, #1
    bne     KeccakP1600_Permute_RoundLoop
KeccakP1600_Permute_Exit:
    StoreState
    ret

Changes to keccakp/configure.ac.

25
26
27
28
29
30
31









32
33
34
35
36
37
38
esac

flags="$CFLAGS"
nomflags="`echo $CFLAGS | sed -e 's/-m[[^ ]]* //g'`"

# special optimizations for some plattforms
case "$host" in









     arm*-*linux*)
	flags="$CFLAGS"
	asmflags="$CFLAGS"
	fastasmflags="-O3 -mfpu=neon $nomflags"
	fastflags="-O3 -mfpu=neon $nomflags"
	as="inplace-armgcc-ARMv7A-NEON"
	enable_asm=yes







>
>
>
>
>
>
>
>
>







25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
esac

flags="$CFLAGS"
nomflags="`echo $CFLAGS | sed -e 's/-m[[^ ]]* //g'`"

# special optimizations for some plattforms
case "$host" in
     aarch64*)
	asmflags="-O3 -mfpu=neon $nomflags"
	flags="-O3 -mfpu=neon $nomflags"
	as="armv8a-neon"
	enable_asm=yes
	plattform=armv8a-neon
	CFLAGS=""
	CCASFLAGS=""
	;;
     arm*-*linux*)
	flags="$CFLAGS"
	asmflags="$CFLAGS"
	fastasmflags="-O3 -mfpu=neon $nomflags"
	fastflags="-O3 -mfpu=neon $nomflags"
	as="inplace-armgcc-ARMv7A-NEON"
	enable_asm=yes