Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Views: 8800
1
r"""
2
"""
3
4
#*****************************************************************************
5
# Copyright (C) 2016 Paul Leopardi [email protected]
6
#
7
# Distributed under the terms of the GNU General Public License (GPL)
8
# as published by the Free Software Foundation; either version 2 of
9
# the License, or (at your option) any later version.
10
# http://www.gnu.org/licenses/
11
#*****************************************************************************
12
13
from sage.crypto.boolean_function import BooleanFunction
14
15
16
def read_s_box(fil):
17
r"""
18
"""
19
lis = []
20
lin = fil.readline()
21
if lin[0:5] != 'S-Box':
22
raise IOError
23
while lin not in ('','\n'):
24
lin = fil.readline()
25
if lin not in ('','\n'):
26
for n in range(8):
27
pos=n*9
28
lis.append(ZZ('0x'+lin[pos:pos+8]))
29
numlist = [num.digits(2,padto=32) for num in lis]
30
bitmatrix = matrix(numlist)
31
boolflist = [BooleanFunction(list(bitmatrix.T[n,:][0]))
32
for n in range(32)]
33
return boolflist
34
35
36
def read_s_boxes_file(fname='../CAST-128/cast-128-s-boxes.txt'):
37
r"""
38
"""
39
s_box_functions = [None]
40
fil = open(fname)
41
for n in range(8):
42
s_box_functions.append(read_s_box(fil))
43
fil.close()
44
return s_box_functions
45
46