Debian Codegraph @

crypto_xof/shake256/unrollround/keccak.py

raw symbols SCIP
1 #!/usr/bin/env python3
2 # 20221231 djb
3
4 print('// auto-generated by keccak.py')
5 print('')
6
7 R = 1
8
9 print('static const uint64_t roundconstant[24] = {')
10 for n in range(24):
11 roundconstant = 0
12 for y in range(7):
13 R = (R<<1)^(113*(R>>7))
14 R &= 255
15 if R&2:
16 roundconstant ^= 1<<((1<<y)-1)
17 print(' 0x%016x,'%roundconstant)
18 print('} ;')
19 print('')
20
21 print('static void keccak(uint64_t *s)')
22 print('{')
23 for j in range(25):
24 print(' uint64_t s%d%d = s[%d];' % (j//5,j%5,j))
25 print(' uint64_t B0,B1,B2,B3,B4;')
26 print(' uint64_t D0,D1,D2,D3,D4;')
27 print(' uint64_t %s;' % ','.join('t%d%d' % (y,x) for y in range(5) for x in range(5)))
28 print('')
29 print(' for (int n = 0;n < 24;++n) {')
30
31 for x in range(5):
32 print(' B%d = s0%d^s1%d^s2%d^s3%d^s4%d;' % (x,x,x,x,x,x))
33 print('')
34
35 for x in range(5):
36 print(' D%d = B%d^ROL(B%d,1);' % (x,(x+4)%5,(x+1)%5))
37 for y in range(5):
38 print(' s%d%d ^= D%d;' % (y,x,x))
39
40 x,y = 1,0
41 r = 0
42 for j in range(24):
43 newx,newy = y,(2*x+3*y)%5
44 r += j+1
45 r %= 64
46 print(' t%d%d = ROL(s%d%d,%d);' % (newy,newx,y,x,r))
47 x,y = newx,newy
48
49 print(' t00 = s00;')
50 print('')
51
52 for y in range(5):
53 for x in range(5):
54 print(' s%d%d = t%d%d^(t%d%d&~t%d%d);' % (y,x,y,x,y,(x+2)%5,y,(x+1)%5))
55
56 print(' s00 ^= roundconstant[n];')
57 print(' }')
58 print('')
59 for j in range(25):
60 print(' s[%d] = s%d%d;' % (j,j//5,j%5))
61 print('}')