-
Notifications
You must be signed in to change notification settings - Fork 0
/
generate.py
executable file
·281 lines (229 loc) · 8.77 KB
/
generate.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
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
#!/usr/bin/python3
import sys
import random
from ciphers.rc4 import RC4
from ciphers.spritz import Spritz
from state import RC4State, SpritzState
from cli import parse_args
State = None
Cipher = None
def generate_rc4_random_state(size):
# registers i, j
registers = [0, 0]
permutation = [i for i in range(size)]
random.shuffle(permutation)
return RC4State([registers, permutation])
def generate_spritz_random_state(size):
# registers i, j, k
registers = [random.randint(0, size-1) for _ in range(3)]
# state is just before update+output and after last shuffle, so a = 0
# z = 0 as we didn't produce any output yet
# registers z, a
registers += [0, 0, 1]
w = random.randint(1, size-1)
while Spritz.gcd(w, size) != 1:
w = random.randint(1, size-1)
registers[-1] = w
permutation = [i for i in range(size)]
random.shuffle(permutation)
return SpritzState([registers, permutation])
def generate_spritz_random_special_state(size):
assert size & 0x1 == 0
# i - even
# j, k - even
registers = [2 * random.randint(0, size//2-1) for _ in range(3)]
# z = 0, a = 0
registers += [0, 0]
w = random.randint(1, size-1)
while Spritz.gcd(w, size) != 1:
w = random.randint(1, size-1)
registers += [w]
even_vals = [2*i for i in range(size//2)]
random.shuffle(even_vals)
odd_vals = [2*i + 1 for i in range(size//2)]
random.shuffle(odd_vals)
permutation = []
# odd indices have even values
# even indices have odd values
for i in range(size//2):
permutation.append(odd_vals[i])
permutation.append(even_vals[i])
return SpritzState([registers, permutation])
def generate_revealed_state(from_state, revealed):
size = from_state.size
indexes_to_reveal = random.sample([i for i in range(size)], revealed)
permutation = []
for i in range(size):
value = None
if i in indexes_to_reveal:
value = from_state.S[i]
permutation.append(value)
return State([from_state.registers, permutation])
def good_wrong_values(state, wrong_indexes, wrong_values):
assert len(wrong_indexes) == len(wrong_values)
for i, v in zip(wrong_indexes, wrong_values):
if state.S[i] == v:
return False
return True
def wrong_revealed_state(revealed_state, number_of_wrong):
revealed_indexes = []
revealed_values = set()
for i, v in enumerate(revealed_state.S):
if v is not None:
revealed_indexes.append(i)
revealed_values.add(v)
wrong_indexes = random.sample(revealed_indexes, number_of_wrong)
wrong_indexes_values = set()
for i in wrong_indexes:
wrong_indexes_values.add(revealed_state.S[i])
wrong_values_set = set(range(revealed_state.size)) - revealed_values | wrong_indexes_values
wrong_values = random.sample(wrong_values_set, number_of_wrong)
while not good_wrong_values(revealed_state, wrong_indexes, wrong_values):
wrong_values = random.sample(wrong_values_set, number_of_wrong)
j = 0
permutation = revealed_state.S[:]
for i in wrong_indexes:
assert permutation[i] is not None
permutation[i] = wrong_values[j]
j += 1
return State([revealed_state.registers, permutation])
def more_revealed_state(backtrack_state, revealed_state, to_reveal):
not_revealed_indexes = set()
for i, v in enumerate(revealed_state.S):
if v is None:
not_revealed_indexes.add(i)
indexes_to_reveal = random.sample(not_revealed_indexes, to_reveal)
permutation = revealed_state.S[:]
for i in indexes_to_reveal:
permutation[i] = backtrack_state.S[i]
assert sum([(0 if x is None else 1) for x in revealed_state.S]) + to_reveal == sum([(0 if x is None else 1) for x in permutation])
return State([revealed_state.registers, permutation])
def random_revealed_state(from_state, to_reveal):
size = from_state.size
indexes_to_reveal = random.sample([i for i in range(size)], to_reveal)
values_to_reveal = random.sample([i for i in range(size)], to_reveal)
permutation = [None for _ in range(size)]
for i in range(to_reveal):
permutation[indexes_to_reveal[i]] = values_to_reveal[i]
return State([from_state.registers, permutation])
def generate_states(size, shift, amount, generate):
states = []
for _ in range(amount):
starting_state = generate(size)
cipher = Cipher(starting_state.state)
cipher.keystream(shift)
backtrack_state = State([cipher.state[0], [None for _ in range(size)]])
states.append([
starting_state,
backtrack_state,
shift
])
return states
def read_states(input_file):
states = []
while True:
initial = input_file.readline()
if initial == '':
break
revealed = input_file.readline()
shift = int(input_file.readline())
states.append([
State(string=initial),
State(string=revealed),
shift
])
return states
def write_states(states_list, only_revealed=False):
for generated in states_list:
if only_revealed:
print(generated[0])
else:
for el in generated:
print(el)
def reveal_values_simple(amount, base_state):
size = base_state.size
indexes_to_reveal = random.sample([i for i in range(size)], amount)
permutation = [None for _ in range(size)]
for i in indexes_to_reveal:
permutation[i] = base_state.S[i]
return State([base_state.registers, permutation])
def reveal_values_special(even, odd, base_state):
size = base_state.size
assert size%2 == 0
even_indexes = random.sample([2*i for i in range(size//2)], even)
odd_indexes = random.sample([2*i+1 for i in range(size//2)], odd)
permutation = [None for _ in range(size)]
for i in even_indexes:
permutation[i] = base_state.S[i]
for i in odd_indexes:
permutation[i] = base_state.S[i]
return State([base_state.registers, permutation])
def reveal_random_values_simple(amount, base_state):
size = base_state.size
indexes_to_reveal = random.sample([i for i in range(size)], amount)
values_to_reveal = random.sample([i for i in range(size)], amount)
permutation = [None for _ in range(size)]
for i in range(amount):
permutation[indexes_to_reveal[i]] = values_to_reveal[i]
return State([base_state.registers, permutation])
def reveal_random_values_special(even, odd, base_state):
size = base_state.size
even_indexes = random.sample([2*i for i in range(size//2)], even)
even_index_values = random.sample([2*i+1 for i in range(size//2)], even)
odd_indexes = random.sample([2*i+1 for i in range(size//2)], odd)
odd_index_values = random.sample([2*i for i in range(size//2)], even)
permutation = [None for _ in range(size)]
for i in range(even):
assert even_indexes[i] % 2 == 0
permutation[even_indexes[i]] = even_index_values[i]
for i in range(odd):
assert odd_indexes[i] % 2 == 1
permutation[odd_indexes[i]] = odd_index_values[i]
return State([base_state.registers, permutation])
def move_state(base_state, steps):
cipher = Cipher(base_state.state)
cipher.keystream(steps)
return State(cipher.state)
def main(args):
global State, Cipher
if args.rc4:
State = RC4State
Cipher = RC4
elif args.spritz:
State = SpritzState
Cipher = Spritz
states = []
if args.input:
states = read_states(sys.stdin)
else:
if args.rc4:
f = generate_rc4_random_state
elif args.spritz:
f = generate_spritz_random_state
if args.special:
f = generate_spritz_random_special_state
states = generate_states(
args.size,
args.prefix_length,
args.amount,
f
)
assert len(states) != 0
for i in range(len(states)):
moved_state = move_state(states[i][0], args.prefix_length)
revealed_state = states[i][1]
if not args.special:
if args.reveal_random:
revealed_state = reveal_random_values_simple(args.preselected, moved_state)
elif args.reveal:
revealed_state = reveal_values_simple(args.preselected, moved_state)
else:
# args.special
if args.reveal_random:
revealed_state = reveal_random_values_special(args.preselected_even, args.preselected_odd, moved_state)
elif args.reveal:
revealed_state = reveal_values_special(args.preselected_even, args.preselected_odd, moved_state)
states[i][1] = revealed_state
write_states(states, args.smt)
if __name__ == '__main__':
main(parse_args())