-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
79 lines (70 loc) · 1.8 KB
/
utils.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
from cryptools.sagestuff import identity_matrix, GF, shuffle
# MATRIX 2x2
def mat2blocks(mat):
assert mat.nrows() == mat.ncols()
n = mat.nrows()/2
res = [[None, None], [None, None]]
for i in xrange(2):
for j in xrange(2):
res[i][j] = mat[i*n:i*n+n,j*n:j*n+n]
return res
def UL2(mat):
bs = mat2blocks(mat)
d = bs[1][1]
if d.is_singular():
raise ZeroDivisionError("no UL2 decomposition")
a = (~d)*bs[1][0]
c = bs[0][1]*(~d)
b = bs[0][0] + c * d * a
return (a, b, c, d)
def idlo(mat):
"""
I 0
mat I
"""
assert mat.nrows() == mat.ncols()
n = mat.nrows()
res = identity_matrix(GF(2), 2*n)
res[n:,:n] = mat
return res
def idup(mat):
"""
I mat
0 I
"""
assert mat.nrows() == mat.ncols()
n = mat.nrows()
res = identity_matrix(GF(2), 2*n)
res[:n,n:] = mat
return res
def diag(a, b):
"""
a 0
0 b
"""
assert a.nrows() == a.ncols() == b.nrows() == b.ncols()
n = a.nrows()
res = identity_matrix(GF(2), 2*n)
res[:n,:n] = a
res[n:,n:] = b
return res
from cryptools.utils import sage_cache
from cryptools.sbox2 import SBox2
@sage_cache("data/functions")
def generate_functions(n, deg, perm, num=100):
if perm:
assert deg == n - 1
return [SBox2.gen.random_permutation(n) for _ in xrange(num)]
else:
return [SBox2.gen.random_sbox_of_degree(n, n, deg) for _ in xrange(num)]
def generate_FN_two_last(funcpool, nr):
shuffle(funcpool)
fs = funcpool[:nr]
s = SBox2.gen.feistel_network_xor(funcs=fs)
spre = SBox2.gen.feistel_network_xor(funcs=fs[:-1])
return fs, s, spre
def generate_FN(funcpool, nr):
shuffle(funcpool)
fs = funcpool[:nr]
s = SBox2.gen.feistel_network_xor(funcs=fs)
return fs, s