| 1 |
|
| 2 |
|
| 3 |
import shlex |
| 4 |
import sys |
| 5 |
import math |
| 6 |
import re |
| 7 |
import os |
| 8 |
from collections import OrderedDict |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
def removesuffix(s, suffix): |
| 13 |
if s.endswith(suffix): |
| 14 |
return s[: -len(suffix)] |
| 15 |
return s |
| 16 |
|
| 17 |
|
| 18 |
def removeprefix(s, prefix): |
| 19 |
if s.startswith(prefix): |
| 20 |
return s[len(prefix) :] |
| 21 |
return s |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
solinasprimes = dict( |
| 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 |
) |
| 33 |
|
| 34 |
montgomeryprimes = dict( |
| 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 |
) |
| 45 |
|
| 46 |
saturatedsolinasprimes = dict(curve25519_solinas=("2^255 - 19")) |
| 47 |
|
| 48 |
dettmanprimes = dict( |
| 49 |
secp256k1_dettman=("48", "5", "2", "2^256 - 4294968273") |
| 50 |
) |
| 51 |
|
| 52 |
output_makefile = "--makefile" in sys.argv[1:] |
| 53 |
directories = tuple(i for i in sys.argv[1:] if i not in ("--makefile",)) |
| 54 |
|
| 55 |
asm_op_names = OrderedDict() |
| 56 |
|
| 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 |
) |
| 60 |
for dirname in directories: |
| 61 |
m = regex.match(os.path.basename(dirname)) |
| 62 |
if m: |
| 63 |
for fname in os.listdir(dirname): |
| 64 |
groups = m.groupdict() |
| 65 |
asm_op_names.setdefault((groups["name"], groups["op"]), []).append( |
| 66 |
os.path.join(dirname, fname) |
| 67 |
) |
| 68 |
|
| 69 |
|
| 70 |
def asm_op_names_key(val): |
| 71 |
(name, op), fnames = val |
| 72 |
n = 0 |
| 73 |
|
| 74 |
if name in dettmanprimes.keys(): |
| 75 |
kind = 0 |
| 76 |
limbwidth, n, last_reduction, prime = dettmanprimes[name] |
| 77 |
|
| 78 |
elif name in saturatedsolinasprimes.keys(): |
| 79 |
kind = 1 |
| 80 |
prime = saturatedsolinasprimes[name] |
| 81 |
|
| 82 |
elif name in montgomeryprimes.keys(): |
| 83 |
kind = 2 |
| 84 |
prime = montgomeryprimes[name] |
| 85 |
|
| 86 |
elif name in solinasprimes.keys(): |
| 87 |
kind = 3 |
| 88 |
n, prime = solinasprimes[name] |
| 89 |
|
| 90 |
else: |
| 91 |
assert False, name |
| 92 |
|
| 93 |
return (kind, n, prime, op, name, fnames) |
| 94 |
|
| 95 |
|
| 96 |
def is_small(val): |
| 97 |
(kind, n, prime, op, name, fnames) = asm_op_names_key(val) |
| 98 |
prime = eval(prime.replace("^", "**")) |
| 99 |
return math.log2(prime) / 64 <= 4 |
| 100 |
|
| 101 |
|
| 102 |
asm_op_names_items = tuple(sorted(asm_op_names.items(), key=asm_op_names_key)) |
| 103 |
small_asm_op_names_items = tuple(val for val in asm_op_names_items if is_small(val)) |
| 104 |
|
| 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 |
] |
| 111 |
|
| 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] |
| 116 |
|
| 117 |
if output_makefile: |
| 118 |
print( |
| 119 |
f""" |
| 120 |
|
| 121 |
# Allow SLOWEST_FIRST=1 to be passed to test files in reverse order. |
| 122 |
# When testing interactively, we probably want to test quicker files |
| 123 |
# first, to be more snappy. But on CI, testing the slowest files |
| 124 |
# first probably allows for slightly better scheduling when there's |
| 125 |
# more parallelism. |
| 126 |
|
| 127 |
AMD64_ASM_STATUS_FILES := $(if $(SLOWEST_FIRST),{' '.join(reversed(status_files))},{' '.join(status_files)}) |
| 128 |
AMD64_ASM_ONLY_STATUS_FILES := $(if $(SLOWEST_FIRST),{' '.join(reversed(only_status_files))},{' '.join(only_status_files)}) |
| 129 |
AMD64_ASM_SMALL_STATUS_FILES := $(if $(SLOWEST_FIRST),{' '.join(reversed(small_status_files))},{' '.join(small_status_files)}) |
| 130 |
AMD64_ASM_SMALL_ONLY_STATUS_FILES := $(if $(SLOWEST_FIRST),{' '.join(reversed(small_only_status_files))},{' '.join(small_only_status_files)}) |
| 131 |
|
| 132 |
""" |
| 133 |
) |
| 134 |
|
| 135 |
for item in asm_op_names_items: |
| 136 |
(kind, n, prime, op, name, fnames) = asm_op_names_key(item) |
| 137 |
if kind == 0: |
| 138 |
binary = "src/ExtractionOCaml/dettman_multiplication" |
| 139 |
binary_descr = "Dettman Multiplication" |
| 140 |
limbwidth, _n, last_reduction, _prime = dettmanprimes[name] |
| 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 |
) |
| 160 |
elif kind == 1: |
| 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 |
) |
| 179 |
elif kind == 2: |
| 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 |
) |
| 198 |
elif kind == 3: |
| 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 |
) |
| 220 |
else: |
| 221 |
assert False, name |
| 222 |
if output_makefile: |
| 223 |
short_fnames = [ |
| 224 |
removesuffix(os.path.basename(fname), ".asm") for fname in fnames |
| 225 |
] |
| 226 |
description = f'{name} {prime.replace(" ", "")} ({op}) ({binary_descr}) ({" ".join(short_fnames)})' |
| 227 |
output_name = f"fiat-amd64/{name}-{op}" |
| 228 |
print( |
| 229 |
f""" |
| 230 |
only-test-amd64-files-print-report:: {output_name}.only-status |
| 231 |
\t@ test $$(cat $<) -eq 0 || echo 'TEST AMD64 {description} ... \t$(RED)$(BOLD)FAILED$(NORMAL)$(NC)' |
| 232 |
|
| 233 |
test-amd64-files-print-report:: {output_name}.status |
| 234 |
\t@ test $$(cat $<) -eq 0 || echo 'TEST AMD64 {description} ... \t$(RED)$(BOLD)FAILED$(NORMAL)$(NC)' |
| 235 |
|
| 236 |
clean:: |
| 237 |
\t$(HIDE)rm -f {output_name}.status {output_name}.only-status {output_name}.invocation {output_name}.description {output_name}.out {output_name}.out-asm {output_name}.stdout |
| 238 |
|
| 239 |
.PHONY: {output_name}.only-status |
| 240 |
|
| 241 |
{output_name}.status: {binary} |
| 242 |
|
| 243 |
{output_name}.status {output_name}.only-status: {' '.join(fnames)} |
| 244 |
\t$(SHOW)'TEST AMD64 {description} ...' |
| 245 |
\t$(HIDE)rm -f $@ |
| 246 |
\t$(HIDE)echo {shlex.quote(invocation + ' -o /dev/null --output-asm /dev/null')} > {output_name}.invocation |
| 247 |
\t$(HIDE)echo '{description}' > {output_name}.description |
| 248 |
\t$(HIDE)$(TIMER) $(PERF_RECORDER) {invocation} -o {output_name}.out --output-asm {output_name}.out-asm >{output_name}.stdout && \\ |
| 249 |
\t {{ echo $$? > $@; echo 'TEST AMD64 {description} ... \t$(GREEN)$(BOLD)PASSED$(NORMAL)$(NC)'; }} || \\ |
| 250 |
\t {{ echo $$? > $@; echo 'TEST AMD64 {description} ... \t$(RED)$(BOLD)FAILED$(NORMAL)$(NC)'; \\ |
| 251 |
\t echo '================== stdout =================='; \\ |
| 252 |
\t cat {output_name}.stdout; \\ |
| 253 |
\t echo '============================================'; \\ |
| 254 |
\t exit 1; }} |
| 255 |
""" |
| 256 |
) |
| 257 |
else: |
| 258 |
print(invocation, "-o", "/dev/null", "--output-asm", "/dev/null") |