-
Notifications
You must be signed in to change notification settings - Fork 14
/
hax-driver.py
executable file
·173 lines (153 loc) · 4.82 KB
/
hax-driver.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
#! /usr/bin/env python3
import os
import argparse
import subprocess
import sys
def shell(command, expect=0, cwd=None, env={}):
subprocess_stdout = subprocess.DEVNULL
print("Env:", env)
print("Command: ", end="")
for i, word in enumerate(command):
if i == 4:
print("'{}' ".format(word), end="")
else:
print("{} ".format(word), end="")
print("\nDirectory: {}".format(cwd))
os_env = os.environ
os_env.update(env)
ret = subprocess.run(command, cwd=cwd, env=os_env)
if ret.returncode != expect:
raise Exception("Error {}. Expected {}.".format(ret, expect))
parser = argparse.ArgumentParser(description="Extract to F-star using Hax.")
parser.add_argument(
"--kyber-reference",
dest="kyber_reference",
default=False,
action="store_true",
help="Extract the Kyber reference implementation.",
)
parser.add_argument(
"--kyber-specification",
dest="kyber_specification",
default=False,
action="store_true",
help="Extract the Kyber specification.",
)
parser.add_argument(
"--crate-path",
type=str,
dest="crate_path",
nargs="?",
default=".",
help="Path to the crate from which to extract the code (default is path to libcrux).",
)
parser.add_argument(
"--functions",
type=str,
nargs="*",
dest="functions",
default="",
help="Space-separated list of functions to extract. The function names must be fully qualified.",
)
parser.add_argument(
"--modules",
type=str,
dest="modules",
nargs="*",
default="",
help="Space-separated list of modules to extract. The module names must be fully qualified.",
)
parser.add_argument(
"--exclude-modules",
type=str,
dest="exclude_modules",
nargs="*",
default="",
help="Space-separated list of modules to exclude from extraction. The module names must be fully qualified.",
)
parser.add_argument(
"--verify-extraction",
dest="verify_extraction",
default=False,
action="store_true",
help="Run the make command to run the FStar typechecker on the extracted files. Some files are lax-checked whereas others are strict-typechecked.",
)
options = parser.parse_args()
filter_string = ""
if options.modules:
options.modules = " ".join(["+" + module + "::*" for module in options.modules])
filter_string += "{}".format(options.modules)
if options.functions:
options.functions = " ".join(["+" + function for function in options.functions])
if not filter_string:
filter_string += "{}".format(options.functions)
else:
filter_string += " {}".format(options.functions)
if options.exclude_modules:
options.exclude_modules = " ".join(
["-" + module + "::*" for module in options.exclude_modules]
)
if not filter_string:
filter_string += "{}".format(options.exclude_modules)
else:
filter_string += " {}".format(options.exclude_modules)
cargo_hax_into = ["cargo", "hax", "into"]
hax_env = {}
exclude_sha3_implementations = "-libcrux::hacl::sha3::** -libcrux::jasmin::sha3::**"
if options.verify_extraction:
shell(["make", "-C", "proofs/fstar/extraction/"])
elif options.kyber_reference:
# Delete all extracted F* files
shell(["rm", "-f", "./proofs/fstar/extraction/Libcrux*"])
# Extract both `libcrux` and `libcrux-platform`
shell(
[
"cargo",
"hax",
"-C",
"-p",
"libcrux",
"-p",
"libcrux-platform",
";",
"into",
"-i",
f"-** +libcrux::kem::kyber::** +!libcrux_platform::platform::* {exclude_sha3_implementations} -libcrux::**::types::index_impls::**",
"fstar",
"--interfaces",
"+* -libcrux::kem::kyber::types +!libcrux_platform::** +!libcrux::digest::**",
],
cwd=".",
env=hax_env,
)
# Delete F* implementation modules for `libcrux_platform` (TODO:
# remove this when https://github.com/hacspec/hax/issues/465 is
# closed)
shell(["rm", "-f", "./sys/platform/proofs/fstar/extraction/*.fst"])
elif options.kyber_specification:
shell(
cargo_hax_into
+ [
"-i",
"-** +compress::* +ind_cpa::* +hacspec_kyber::* +matrix::* +ntt::* +parameters::* +sampling::* +serialize::* {} -libcrux::digest::*".format(
exclude_sha3_implementations
),
"fstar",
],
cwd=os.path.join("specs", "kyber"),
env=hax_env,
)
else:
if filter_string:
shell(
cargo_hax_into
+ [
"-i",
"-** {}".format(filter_string),
"fstar",
],
cwd=options.crate_path,
env=hax_env,
)
else:
shell(cargo_hax_into + ["fstar"], cwd=options.crate_path, env=hax_env)