-
Notifications
You must be signed in to change notification settings - Fork 8
/
generate_matrices.py
125 lines (112 loc) · 3.86 KB
/
generate_matrices.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
blocksize = 256
keysize = 80
rounds = 12
def main():
''' Use the global parameters `blocksize`, `keysize` and `rounds`
to create the set of matrices and constants for the corresponding
LowMC instance. Save those in a file named
`matrices_and_constants.dat`.
'''
gen = grain_ssg()
linlayers = []
for _ in range(rounds):
linlayers.append(instantiate_matrix(blocksize, blocksize, gen))
round_constants = []
for _ in range(rounds):
constant = [next(gen) for _ in range(blocksize)]
round_constants.append(constant)
roundkey_matrices = []
for _ in range(rounds + 1):
mat = instantiate_matrix(blocksize, keysize, gen)
roundkey_matrices.append(mat)
with open('matrices_and_constants.dat', 'w') as matfile:
s = 'LowMC matrices and constants\n'\
'============================\n'\
'Block size: ' + str(blocksize) + '\n'\
'Key size: ' + str(keysize) + '\n'\
'Rounds: ' + str(rounds) + '\n\n'
matfile.write(s)
s = 'Linear layer matrices\n'\
'---------------------'
matfile.write(s)
for r in range(rounds):
s = '\nLinear layer ' + str(r + 1) + ':\n'
for row in linlayers[r]:
s += str(row) + '\n'
matfile.write(s)
s = '\nRound constants\n'\
'---------------------'
matfile.write(s)
for r in range(rounds):
s = '\nRound constant ' + str(r + 1) + ':\n'
s += str(round_constants[r]) + '\n'
matfile.write(s)
s = '\nRound key matrices\n'\
'---------------------'
matfile.write(s)
for r in range(rounds + 1):
s = '\nRound key matrix ' + str(r) + ':\n'
for row in roundkey_matrices[r]:
s += str(row) + '\n'
matfile.write(s)
def instantiate_matrix(n, m, gen):
''' Instantiate a matrix of maximal rank using bits from the
generatator `gen`.
'''
while True:
mat = []
for _ in range(n):
row = []
for _ in range(m):
row.append(next(gen))
mat.append(row)
if rank(mat) >= min(n, m):
return mat
def rank(matrix):
''' Determine the rank of a binary matrix. '''
# Copy matrix
mat = [[x for x in row] for row in matrix]
n = len(matrix)
m = len(matrix[0])
for c in range(m):
if c > n - 1:
return n
r = c
while mat[r][c] != 1:
r += 1
if r >= n:
return c
mat[c], mat[r] = mat[r], mat[c]
for r in range(c + 1, n):
if mat[r][c] == 1:
for j in range(m):
mat[r][j] ^= mat[c][j]
return m
def grain_ssg():
''' A generator for using the Grain LSFR in a self-shrinking generator. '''
state = [1 for _ in range(80)]
index = 0
# Discard first 160 bits
for _ in range(160):
state[index] ^= state[(index + 13) % 80] ^ state[(index + 23) % 80]\
^ state[(index + 38) % 80] ^ state[(index + 51) % 80]\
^ state[(index + 62) % 80]
index += 1
index %= 80
choice = False
while True:
state[index] ^= state[(index + 13) % 80] ^ state[(index + 23) % 80]\
^ state[(index + 38) % 80] ^ state[(index + 51) % 80]\
^ state[(index + 62) % 80]
choice = state[index]
index += 1
index %= 80
state[index] ^= state[(index + 13) % 80] ^ state[(index + 23) % 80]\
^ state[(index + 38) % 80] ^ state[(index + 51) % 80]\
^ state[(index + 62) % 80]
if choice == 1:
yield state[index]
index += 1
index %= 80
if __name__ == '__main__':
main()