/
create_supercop_project.py
184 lines (145 loc) · 6.88 KB
/
create_supercop_project.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
#!/usr/bin/python
import sys
import os, errno, shutil
from shutil import copyfile
base_dir_name = './crypto_sign'
if 2 == len(sys.argv) :
base_dir_name = sys.argv[1]
print ( "base_dir_name: " + base_dir_name )
file_dir = './supercop-project'
imple_names = [ 'ref' , 'amd64' , 'avx2' , 'neon' ]
imple_dirs = {
'ref' : 'ref' ,
'amd64' : 'amd64' ,
'avx2' : 'avx2' ,
'neon' : 'neon'
}
imple_srcs = {
'ref' : ['src','src/ref','utils'] ,
'amd64' : ['src','src/ref','src/amd64','utils'] ,
'avx2' : ['src','src/ref','src/amd64','src/ssse3','src/avx2','utils','utils/x86aesni'],
'neon' : ['src','src/ref','src/amd64','src/neon','utils','utils/neon_aesinst']
}
extra_files = {
'ref' : [ ] ,
'amd64' : [ ('architectures',f'{file_dir}/architectures.x86') ] ,
'avx2' : [ ('architectures',f'{file_dir}/architectures.x86') ] ,
'neon' : [ ('architectures',f'{file_dir}/architectures.arm') ]
}
params_variants_names = [ 'OVIp' , 'OVIppkc' , 'OVIppkcskc' , 'OVIs' , 'OVIspkc' , 'OVIspkc_skc' , 'OVIII' , 'OVIIIpkc' , 'OVIIIpkcskc' , 'OVV' , 'OVVpkc' , 'OVVpkcskc' ]
modify_paramname = {
'OVIp' : [ ] ,
'OVIppkc' : [ ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC" ) ] ,
'OVIppkcskc' : [ ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC_SKC" )] ,
'OVIs' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV16_160_64" ) ] ,
'OVIspkc' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV16_160_64" ), ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC" ) ] ,
'OVIspkcskc' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV16_160_64" ), ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC_SKC" )] ,
'OVIII' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV256_184_72" ) ] ,
'OVIIIpkc' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV256_184_72" ), ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC" ) ] ,
'OVIIIpkcskc': [ ('params.h' , "#define _OV256_112_44" , "#define _OV256_184_72" ), ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC_SKC" )] ,
'OVV' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV256_244_96" ) ] ,
'OVVpkc' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV256_244_96" ), ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC" ) ] ,
'OVVpkcskc' : [ ('params.h' , "#define _OV256_112_44" , "#define _OV256_244_96" ), ('params.h' , "#define _OV_CLASSIC" , "#define _OV_PKC_SKC" )]
}
modify_projname = {
'ref' : [ ('api.h' , '//#define _SUPERCOP_' , '#define _SUPERCOP_' ),('config.h' , '#define _UTILS_OPENSSL_' , '#define _UTILS_SUPERCOP_' ) ] ,
'amd64' : [ ('api.h' , '//#define _SUPERCOP_' , '#define _SUPERCOP_' ),('config.h' , '#define _UTILS_OPENSSL_' , '#define _UTILS_SUPERCOP_' ),( 'config.h' , "//#define _BLAS_UINT64_" , "#define _BLAS_UINT64_" ) ] ,
'neon' : [ ('api.h' , '//#define _SUPERCOP_' , '#define _SUPERCOP_' ),('config.h' , '#define _UTILS_OPENSSL_' , '#define _UTILS_SUPERCOP_' ),( 'config.h' , "//#define _BLAS_NEON_" , "#define _BLAS_NEON_" ) , ('config.h' , "//#define _UTILS_NEONAES_" , "#define _UTILS_NEONAES_" )] ,
'avx2' : [ ('api.h' , '//#define _SUPERCOP_' , '#define _SUPERCOP_' ),('config.h' , '#define _UTILS_OPENSSL_' , '#define _UTILS_SUPERCOP_' ),( 'config.h' , "//#define _BLAS_AVX2_" , "#define _BLAS_AVX2_" ) , ('config.h' , "//#define _UTILS_AESNI_" , "#define _UTILS_AESNI_" ) , ('config.h' , "//#define _MUL_WITH_MULTAB_" , "#define _MUL_WITH_MULTAB_" ) ]
}
modify_dirname = {
'OVIs/neon' : [ ('config.h' , "//#define _MUL_WITH_MULTAB_" , "#define _MUL_WITH_MULTAB_" ) ] ,
'OVIspkc/neon' : [ ('config.h' , "//#define _MUL_WITH_MULTAB_" , "#define _MUL_WITH_MULTAB_" ) ] ,
'OVIspkcskc/neon' : [ ('config.h' , "//#define _MUL_WITH_MULTAB_" , "#define _MUL_WITH_MULTAB_" ) ]
}
#
# mk dirs
#
def mkdir( dir_name ):
try:
os.makedirs(dir_name)
print( 'mkdir: ' + dir_name )
except OSError as e:
print( e )
pass
#if e.errno != errno.EEXIST:
#raise
def make_all_dirs():
print( "create all dirs:" )
mkdir( base_dir_name )
for ii in params_variants_names:
pp1 = base_dir_name + '/' + ii
mkdir( pp1 )
for pp in imple_names:
ppii = pp1 + '/' + imple_dirs[pp]
mkdir( ppii )
print( "\n----------------- mk dirs -----------------" )
make_all_dirs()
#
# copy files
#
def copy_files( dest_dir , src_dir ):
#print( 'processing: %s -> %s' % (src_dir , dest_dir ) )
if os.path.isfile( src_dir ) :
shutil.copy2( src_dir, dest_dir)
print('copy files: %s\t<--\t%s' % (dest_dir, src_dir) )
elif os.path.isdir( src_dir ) :
src_files = os.listdir(src_dir)
for file_name in src_files:
full_file_name = os.path.join(src_dir, file_name)
if (os.path.isfile(full_file_name)):
shutil.copy2(full_file_name, dest_dir)
print('copy files: %s\t<--\t%s/%s' % ( dest_dir , src_dir, file_name ) )
else :
print( "(???) don't know how to process: %s <- %s (???)" % (dest_dir,src_dir) )
def copy_files_for( proj ):
for pv in params_variants_names :
dest_dir_name = '/'.join( [ base_dir_name , pv , imple_dirs[proj] ] )
for jj in imple_srcs[proj] :
copy_files( dest_dir_name , jj )
if proj in extra_files :
for cmd in extra_files[proj] : copy_files( dest_dir_name + '/' + cmd[0] , cmd[1] )
for proj in imple_names :
print( f"\n-----------------\ncopy files for project {proj}\n-----------------" )
copy_files_for( proj )
#
# modify files
#
def modify_file( file_name , phrase , new_content ):
try:
content = list( open( file_name ) )
except :
raise
line_idx = -1
for i in range( len(content) ) :
if content[i].startswith( phrase ) :
line_idx = i
break
if line_idx < 0 :
print( "no line in %s starts with %s" % (file_name,phrase) )
return line_idx
print( "chage %s:%d -> %s" % ( file_name , line_idx , new_content ) )
try:
fp = open( file_name , "w" )
except :
return
for i in range(len(content)):
if line_idx == i :
fp.write( new_content + '\n' )
else:
fp.write( content[i] )
fp.close()
def modify_files_for( proj , m_projname , m_paramname , m_dirname ):
for pv in params_variants_names :
dest_dir_name = '/'.join( [ base_dir_name , pv , imple_dirs[proj] ] )
if proj in m_projname :
for cmd in m_projname[proj] : modify_file( dest_dir_name + '/' + cmd[0] , cmd[1] , cmd[2] )
if pv in m_paramname :
for cmd in m_paramname[pv] : modify_file( dest_dir_name + '/' + cmd[0] , cmd[1] , cmd[2] )
dirname = pv + '/' + proj
if dirname in m_dirname :
for cmd in m_dirname[dirname] : modify_file( dest_dir_name + '/' + cmd[0] , cmd[1] , cmd[2] )
for proj in imple_names :
print( f"\n-----------------\nmodify files for project {proj}\n-----------------" )
modify_files_for( proj , modify_projname , modify_paramname , modify_dirname )
exit()