-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpso.py
148 lines (123 loc) · 6.42 KB
/
pso.py
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
#!/usr/bin/env python3
"""
Partial sum optimization for Skinny.
Copyright (C) 2023
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
import minizinc
import json
import argparse
from datetime import timedelta
import math
def propagate_dependency(start_round, final_round, balanced_cell):
# Round i: Zi-1 -(ARK)-> Xi -(SR)-> Yi -(MC)-> Zi
X = [[True if c == balanced_cell else False for c in range(16)]]
W = []
or3 = lambda x, y, z: [xi or yi or zi for xi, yi, zi in zip(x, y, z)]
shiftrows = lambda X : X[0:4] + X[7:8] + X[4:7] + X[10:12] + X[8:10] + X[13:16] + X[12:13]
mixcolumn = lambda X : X[12:16] + or3(X[0:4], X[4:8], X[8:12]) + X[4:8] + or3(X[4:8], X[8:12], X[12:16])
for r in range(start_round, final_round+1):
W.append(shiftrows(X[-1]))
X.append(mixcolumn(W[-1]))
# Tweakey schedule
#RT = [[hex(c)[2:] for c in range(16)]]
RT = [[c for c in range(16)]] # rounds are 1-indexed
permute = lambda X : [X[9], X[15], X[8], X[13], X[10], X[14], X[12], X[11]] + X[:8]
for r in range(final_round):
RT.append(permute(RT[-1]))
return X, W, [r[:8] for r in RT[start_round:]]
def build_key_guess(RT, X, tweakey_cell, tweakey_setting):
steps = 0
tweakey_count = {key: 0 for key in range(16)}
tweakey_uncertain = {key: [] for key in range(16)}
stk_to_guess = [[-1 for _ in range(len(RT[0]))] for _ in range(len(RT))]
for i in range(len(RT)-1, 0, -1):
for j in range(len(RT[i])):
if X[i][j] and RT[i][j] != tweakey_cell:
tweakey_count[RT[i][j]] += 1
if tweakey_count[RT[i][j]] < tweakey_setting:
stk_to_guess[i][j] = 0
steps += 1
elif tweakey_count[RT[i][j]] < (tweakey_setting+2):
if tweakey_count[RT[i][j]] == (tweakey_setting+1):
stk_to_guess[i][j] = 1
k = tweakey_uncertain[RT[i][j]][-1]
stk_to_guess[k[0]][k[1]] = 1
tweakey_uncertain[RT[i][j]].append([i, j])
for i in tweakey_uncertain.values():
if len(i) == 1:
k = i.pop()
stk_to_guess[k[0]][k[1]] = 0
steps += 1
tweakey_uncertain = [i for i in tweakey_uncertain.values() if i != []]
return stk_to_guess, tweakey_uncertain, int(steps + len(tweakey_uncertain) + 1)
def optimize_ps(parameter):
# Create a MiniZinc model
model = minizinc.Model()
model.add_file('pso.mzn')
# Transform Model into a instance
gecode = minizinc.Solver.lookup("com.google.ortools.sat")
inst = minizinc.Instance(gecode, model)
X, _, RT = propagate_dependency(parameter['start_round'], parameter['final_round'], parameter['balanced_cell'])
_, _, steps = build_key_guess(RT, X, parameter['tweakey_cell'], parameter['tweakey_setting'])
print(f'Max steps: {steps}')
inst["tweakey_setting"] = parameter['tweakey_setting']
inst["start_round"] = parameter['start_round']
inst["final_round"] = parameter['final_round']
inst["tweakey_cell"] = parameter['tweakey_cell']
inst["balanced_cell"] = parameter['balanced_cell']
inst["input_active"] = parameter['input_active']
if parameter['steps']:
inst["steps"] = parameter['steps']
else:
inst["steps"] = steps
inst["scale"] = parameter['scale']
# Solve the instance
result = inst.solve() # processes=4, timeout=timedelta(minutes=30))
file_name = f"{parameter['tweakey_setting']}_{parameter['final_round']}_{parameter['start_round']}_{parameter['tweakey_cell']}_{parameter['balanced_cell']}_{parameter['input_active']}.json"
log2cost = math.log2(result['total_time_cost']/(16*(parameter['final_round']+1)))+parameter['scale']
print(f'Cost: 2^{log2cost}')
parameter['cost'] = result['total_time_cost']/(16*(parameter['final_round']+1)) * 2**parameter['scale']
parameter['status'] = result.status.name
try:
with open(file_name,"r") as f:
old_parameter = json.load(f)
if parameter['cost'] >= int(old_parameter['cost']):
print("There already exists a partial sum order file with a better cost! - Not writing to file")
return
except IOError as e:
pass
parameter['keys'] = [[] for _ in range(max(map(max, result['stk'])))]
for i, round in enumerate(result['stk']):
for j, cell in enumerate(round):
if cell != -1:
k = {}
k['r'] = i + parameter["start_round"]
k['c'] = j
parameter['keys'][cell-1].append(k)
try:
with open(file_name, 'w') as file:
json.dump(parameter, file)
except IOError as e:
print(e)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Find partial sum recovery steps")
parser.add_argument('tweakey_setting', action='store', type=int, help="Specify version of Skinny used")
parser.add_argument('final_round', action='store', type=int, help="Final round of the key recovery")
parser.add_argument('start_round', action='store', type=int, help="Start round of the key recovery")
parser.add_argument('tweakey_cell', action='store', type=int, help="Specify which tweakey cell is controlled by the attacker")
parser.add_argument('balanced_cell', action='store', type=int, help="Specify the balanced cell from the output of the distinguisher")
parser.add_argument('input_active', action='store', type=int, help="Specify how many cell are active at the input of the distinguisher")
parser.add_argument('scale', action='store', type=int, help="Scale the time complexity, must be used since many solver only support limited data types")
parser.add_argument('-s', '--steps', nargs='?', type=int, help="Specify the maximum number of steps, default are the involved subtweakey cells")
parameter = parser.parse_args()
optimize_ps(vars(parameter))