Skip to content

Commit 3c25615

Browse files
committed
Reformat python automatically
1 parent ce9e409 commit 3c25615

File tree

1 file changed

+142
-51
lines changed

1 file changed

+142
-51
lines changed

fiat-amd64/gentest.py

+142-51
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,65 @@
77
import os
88
from collections import OrderedDict
99

10+
1011
# in Python 3.9 and newer, this is primitive as str.removesuffix
1112
def removesuffix(s, suffix):
12-
if s.endswith(suffix): return s[:-len(suffix)]
13+
if s.endswith(suffix):
14+
return s[: -len(suffix)]
1315
return s
16+
17+
1418
def removeprefix(s, prefix):
15-
if s.startswith(prefix): return s[len(prefix):]
19+
if s.startswith(prefix):
20+
return s[len(prefix) :]
1621
return s
1722

23+
1824
# grep -oP "src/.*square" fiat-c/src/*64*.c
1925

20-
#unsaturated_solinas
21-
solinasprimes = dict( # num limbs, prime
22-
curve25519=('5', '2^255 - 19'),
23-
p448_solinas=('8', '2^448 - 2^224 - 1'),
24-
p521=('9', '2^521 - 1'),
25-
poly1305=('3', '2^130 - 5'))
26+
# unsaturated_solinas
27+
solinasprimes = dict( # num limbs, prime
28+
curve25519=("5", "2^255 - 19"),
29+
p448_solinas=("8", "2^448 - 2^224 - 1"),
30+
p521=("9", "2^521 - 1"),
31+
poly1305=("3", "2^130 - 5"),
32+
)
2633

2734
montgomeryprimes = dict(
28-
curve25519_scalar='2^252 + 27742317777372353535851937790883648493',
29-
p224='2^224 - 2^96 + 1',
30-
p256='2^256 - 2^224 + 2^192 + 2^96 - 1',
31-
p256_scalar='2^256 - 2^224 + 2^192 - 89188191075325690597107910205041859247',
32-
p384='2^384 - 2^128 - 2^96 + 2^32 - 1',
33-
p384_scalar='2^384 - 1388124618062372383947042015309946732620727252194336364173',
34-
p434='2^216 * 3^137 - 1',
35-
secp256k1_montgomery='2^256 - 2^32 - 977',
36-
secp256k1_scalar='2^256 - 432420386565659656852420866394968145599')
35+
curve25519_scalar="2^252 + 27742317777372353535851937790883648493",
36+
p224="2^224 - 2^96 + 1",
37+
p256="2^256 - 2^224 + 2^192 + 2^96 - 1",
38+
p256_scalar="2^256 - 2^224 + 2^192 - 89188191075325690597107910205041859247",
39+
p384="2^384 - 2^128 - 2^96 + 2^32 - 1",
40+
p384_scalar="2^384 - 1388124618062372383947042015309946732620727252194336364173",
41+
p434="2^216 * 3^137 - 1",
42+
secp256k1_montgomery="2^256 - 2^32 - 977",
43+
secp256k1_scalar="2^256 - 432420386565659656852420866394968145599",
44+
)
3745

38-
saturatedsolinasprimes = dict(
39-
curve25519_solinas=('2^255 - 19'))
46+
saturatedsolinasprimes = dict(curve25519_solinas=("2^255 - 19"))
4047

41-
dettmanprimes = dict( # last limb width, limbs, last reduction, prime
42-
secp256k1_dettman=('48', '5', '2', '2^256 - 4294968273'))
48+
dettmanprimes = dict( # last limb width, limbs, last reduction, prime
49+
secp256k1_dettman=("48", "5", "2", "2^256 - 4294968273")
50+
)
4351

44-
output_makefile = ('--makefile' in sys.argv[1:])
45-
directories = tuple(i for i in sys.argv[1:] if i not in ('--makefile',))
52+
output_makefile = "--makefile" in sys.argv[1:]
53+
directories = tuple(i for i in sys.argv[1:] if i not in ("--makefile",))
4654

4755
asm_op_names = OrderedDict()
4856

49-
regex = re.compile(r'fiat_(?P<name>[^_]+(_(solinas|montgomery|dettman))?)_(?P<op>(carry_)?(square|mul|from_bytes|to_bytes|add|sub|opp))')
57+
regex = re.compile(
58+
r"fiat_(?P<name>[^_]+(_(solinas|montgomery|dettman))?)_(?P<op>(carry_)?(square|mul|from_bytes|to_bytes|add|sub|opp))"
59+
)
5060
for dirname in directories:
5161
m = regex.match(os.path.basename(dirname))
5262
if m:
5363
for fname in os.listdir(dirname):
5464
groups = m.groupdict()
55-
asm_op_names.setdefault((groups['name'], groups['op']), []).append(os.path.join(dirname,fname))
65+
asm_op_names.setdefault((groups["name"], groups["op"]), []).append(
66+
os.path.join(dirname, fname)
67+
)
68+
5669

5770
def asm_op_names_key(val):
5871
(name, op), fnames = val
@@ -79,24 +92,31 @@ def asm_op_names_key(val):
7992

8093
return (kind, n, prime, op, name, fnames)
8194

95+
8296
def is_small(val):
8397
(kind, n, prime, op, name, fnames) = asm_op_names_key(val)
84-
prime = eval(prime.replace('^', '**'))
98+
prime = eval(prime.replace("^", "**"))
8599
return math.log2(prime) / 64 <= 4
86100

101+
87102
asm_op_names_items = tuple(sorted(asm_op_names.items(), key=asm_op_names_key))
88103
small_asm_op_names_items = tuple(val for val in asm_op_names_items if is_small(val))
89104

90-
status_file_stems = [f'fiat-amd64/{name}-{op}' for (name, op), _fnames in asm_op_names_items]
91-
small_status_file_stems = [f'fiat-amd64/{name}-{op}' for (name, op), _fnames in small_asm_op_names_items]
105+
status_file_stems = [
106+
f"fiat-amd64/{name}-{op}" for (name, op), _fnames in asm_op_names_items
107+
]
108+
small_status_file_stems = [
109+
f"fiat-amd64/{name}-{op}" for (name, op), _fnames in small_asm_op_names_items
110+
]
92111

93-
status_files = [stem + '.status' for stem in status_file_stems]
94-
only_status_files = [stem + '.only-status' for stem in status_file_stems]
95-
small_status_files = [stem + '.status' for stem in small_status_file_stems]
96-
small_only_status_files = [stem + '.only-status' for stem in small_status_file_stems]
112+
status_files = [stem + ".status" for stem in status_file_stems]
113+
only_status_files = [stem + ".only-status" for stem in status_file_stems]
114+
small_status_files = [stem + ".status" for stem in small_status_file_stems]
115+
small_only_status_files = [stem + ".only-status" for stem in small_status_file_stems]
97116

98117
if output_makefile:
99-
print(f'''
118+
print(
119+
f"""
100120
101121
# Allow SLOWEST_FIRST=1 to be passed to test files in reverse order.
102122
# When testing interactively, we probably want to test quicker files
@@ -109,34 +129,104 @@ def is_small(val):
109129
AMD64_ASM_SMALL_STATUS_FILES := $(if $(SLOWEST_FIRST),{' '.join(reversed(small_status_files))},{' '.join(small_status_files)})
110130
AMD64_ASM_SMALL_ONLY_STATUS_FILES := $(if $(SLOWEST_FIRST),{' '.join(reversed(small_only_status_files))},{' '.join(small_only_status_files)})
111131
112-
''')
132+
"""
133+
)
113134

114135
for item in asm_op_names_items:
115136
(kind, n, prime, op, name, fnames) = asm_op_names_key(item)
116137
if kind == 0:
117-
binary = 'src/ExtractionOCaml/dettman_multiplication'
118-
binary_descr = 'Dettman Multiplication'
138+
binary = "src/ExtractionOCaml/dettman_multiplication"
139+
binary_descr = "Dettman Multiplication"
119140
limbwidth, _n, last_reduction, _prime = dettmanprimes[name]
120-
invocation = ' '.join([binary, name, '64', n, limbwidth, last_reduction, shlex.quote(prime), op, '--no-wide-int', '--shiftr-avoid-uint1'] + [item for fname in fnames for item in ('--hints-file', shlex.quote(fname))])
141+
invocation = " ".join(
142+
[
143+
binary,
144+
name,
145+
"64",
146+
n,
147+
limbwidth,
148+
last_reduction,
149+
shlex.quote(prime),
150+
op,
151+
"--no-wide-int",
152+
"--shiftr-avoid-uint1",
153+
]
154+
+ [
155+
item
156+
for fname in fnames
157+
for item in ("--hints-file", shlex.quote(fname))
158+
]
159+
)
121160
elif kind == 1:
122-
binary = 'src/ExtractionOCaml/solinas_reduction'
123-
binary_descr = 'Saturated Solinas'
124-
invocation = ' '.join([binary, name, '64', shlex.quote(prime), op, '--no-wide-int', '--shiftr-avoid-uint1'] + [item for fname in fnames for item in ('--hints-file', shlex.quote(fname))])
161+
binary = "src/ExtractionOCaml/solinas_reduction"
162+
binary_descr = "Saturated Solinas"
163+
invocation = " ".join(
164+
[
165+
binary,
166+
name,
167+
"64",
168+
shlex.quote(prime),
169+
op,
170+
"--no-wide-int",
171+
"--shiftr-avoid-uint1",
172+
]
173+
+ [
174+
item
175+
for fname in fnames
176+
for item in ("--hints-file", shlex.quote(fname))
177+
]
178+
)
125179
elif kind == 2:
126-
binary = 'src/ExtractionOCaml/word_by_word_montgomery'
127-
binary_descr = 'Word-by-Word Montgomery'
128-
invocation = ' '.join([binary, name, '64', shlex.quote(prime), op, '--no-wide-int', '--shiftr-avoid-uint1'] + [item for fname in fnames for item in ('--hints-file', shlex.quote(fname))])
180+
binary = "src/ExtractionOCaml/word_by_word_montgomery"
181+
binary_descr = "Word-by-Word Montgomery"
182+
invocation = " ".join(
183+
[
184+
binary,
185+
name,
186+
"64",
187+
shlex.quote(prime),
188+
op,
189+
"--no-wide-int",
190+
"--shiftr-avoid-uint1",
191+
]
192+
+ [
193+
item
194+
for fname in fnames
195+
for item in ("--hints-file", shlex.quote(fname))
196+
]
197+
)
129198
elif kind == 3:
130-
binary = 'src/ExtractionOCaml/unsaturated_solinas'
131-
binary_descr = 'Unsaturated Solinas'
132-
invocation = ' '.join([binary, name, '64', n, shlex.quote(prime), op, '--no-wide-int', '--shiftr-avoid-uint1', '--tight-bounds-mul-by', '1.000001'] + [item for fname in fnames for item in ('--hints-file', shlex.quote(fname))])
199+
binary = "src/ExtractionOCaml/unsaturated_solinas"
200+
binary_descr = "Unsaturated Solinas"
201+
invocation = " ".join(
202+
[
203+
binary,
204+
name,
205+
"64",
206+
n,
207+
shlex.quote(prime),
208+
op,
209+
"--no-wide-int",
210+
"--shiftr-avoid-uint1",
211+
"--tight-bounds-mul-by",
212+
"1.000001",
213+
]
214+
+ [
215+
item
216+
for fname in fnames
217+
for item in ("--hints-file", shlex.quote(fname))
218+
]
219+
)
133220
else:
134221
assert False, name
135222
if output_makefile:
136-
short_fnames = [removesuffix(os.path.basename(fname),'.asm') for fname in fnames]
223+
short_fnames = [
224+
removesuffix(os.path.basename(fname), ".asm") for fname in fnames
225+
]
137226
description = f'{name} {prime.replace(" ", "")} ({op}) ({binary_descr}) ({" ".join(short_fnames)})'
138-
output_name = f'fiat-amd64/{name}-{op}'
139-
print(f'''
227+
output_name = f"fiat-amd64/{name}-{op}"
228+
print(
229+
f"""
140230
only-test-amd64-files-print-report:: {output_name}.only-status
141231
\t@ test $$(cat $<) -eq 0 || echo 'TEST AMD64 {description} ... \t$(RED)$(BOLD)FAILED$(NORMAL)$(NC)'
142232
@@ -162,6 +252,7 @@ def is_small(val):
162252
\t cat {output_name}.stdout; \\
163253
\t echo '============================================'; \\
164254
\t exit 1; }}
165-
''')
255+
"""
256+
)
166257
else:
167-
print(invocation, '-o', '/dev/null', '--output-asm', '/dev/null')
258+
print(invocation, "-o", "/dev/null", "--output-asm", "/dev/null")

0 commit comments

Comments
 (0)