-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_keccak_function.py
More file actions
139 lines (59 loc) · 2.67 KB
/
Copy path_keccak_function.py
File metadata and controls
139 lines (59 loc) · 2.67 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
def keccak_f(state):
"""
Implements the Keccak-f permutation function.
Arguments:
state (list of list of int): A 5x5 matrix where each element is a 64-bit integer.
Returns:
list of list of int: The transformed state matrix after applying the permutation.
"""
def theta(state):
""" Theta step in the Keccak-f permutation. """
C = [0] * 5
D = [0] * 5
for x in range(5):
C[x] = state[x][0] ^ state[x][1] ^ state[x][2] ^ state[x][3] ^ state[x][4]
for x in range(5):
D[x] = C[(x-1) % 5] ^ rotate_left(C[(x+1) % 5], 1)
for x in range(5):
for y in range(5):
state[x][y] ^= D[x]
def rho_and_pi(state):
""" Rho and Pi steps in the Keccak-f permutation. """
new_state = [[0] * 5 for _ in range(5)]
for x in range(5):
for y in range(5):
new_state[y][(2*x + 3*y) % 5] = rotate_left(state[x][y], (x+1)*(y+1) % 64)
return new_state
def chi(state):
""" Chi step in the Keccak-f permutation. """
new_state = [[0] * 5 for _ in range(5)]
for x in range(5):
for y in range(5):
new_state[x][y] = state[x][y] ^ ((~state[(x+1) % 5][y]) & state[(x+2) % 5][y])
return new_state
def iota(state, round_idx):
""" Iota step in the Keccak-f permutation. """
RC = [0x0000000000000001, 0x0000000000008082, 0x800000000000808A,
0x8000000080008000, 0x000000000000808B, 0x0000000080000001,
0x8000000080008081, 0x8000000000008009, 0x000000000000008A,
0x0000000000000088, 0x0000000080008009, 0x000000008000000A,
0x000000008000808B, 0x800000000000008B, 0x8000000000008089,
0x8000000000008003, 0x8000000000008002, 0x8000000000000080,
0x000000000000800A, 0x800000008000000A, 0x8000000080008081,
0x8000000000008080, 0x0000000080000001, 0x8000000080008008]
state[0][0] ^= RC[round_idx]
num_rounds = 24
for round_idx in range(num_rounds):
theta(state)
state = rho_and_pi(state)
state = chi(state)
iota(state, round_idx)
return state
# Helper function for rotating bits left.
def rotate_left(value, amount):
return ((value << amount) | (value >> (64 - amount))) & 0xFFFFFFFFFFFFFFFF
# Example of initializing the state matrix and running the function
# (Commenting out to prevent execution here; this is just for demonstration)
# initial_state = [[0] * 5 for _ in range(5)]
# result_state = keccak_f(initial_state)
# print(result_state)