This repository was archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrix_generator.py
115 lines (101 loc) · 3.43 KB
/
Matrix_generator.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
# Reference: https://devpouch.tistory.com/99
import sys
blocksize = int(sys.argv[1])
keysize = int(sys.argv[2])
rounds = int(sys.argv[3])
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('Lmatrix.txt', 'w') as matfile:
for r in range(rounds):
s = ''
for row in linlayers[r]:
s += ' '.join(map(str, row)) + '\n'
matfile.write(s)
with open('roundconstant.txt', 'w') as matfile:
for r in range(rounds):
s = ''
s += ' '.join(map(str, round_constants[r])) + '\n'
matfile.write(s)
with open('Kmatrix.txt', 'w') as matfile:
for r in range(rounds + 1):
s = ''
for row in roundkey_matrices[r]:
s += ' '.join(map(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()