-
Notifications
You must be signed in to change notification settings - Fork 8
/
run_examples.py
227 lines (186 loc) · 8.94 KB
/
run_examples.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
"""
Program to run our examples
It not only tests the simple examples (which just used alloc etc) but also
tests for examples on cross-program-calls among other things.
"""
# TODO: set up formatting and linting for Python files in CI.
import os
import re
import shlex
import subprocess
import unittest
import toml
from colorama import Fore, Style
os_environ = os.environ
# Comment the following line if you do not want verbose output
os_environ["MOZAK_STARK_DEBUG"] = "true"
# Turn the following to `True` if you do not want output capturing
capture_output = False
# Running timeout per prove-and-verify (in seconds)
timeout = 600
class ReadTomlError(Exception):
"""Error while reading TOML file."""
def read_toml_file(file_path: str):
"""Reads a toml file and returns the parsed content."""
try:
with open(file_path, "r", encoding="utf-8") as f:
return toml.load(f)
except FileNotFoundError:
raise
except Exception as e:
raise ReadTomlError(f"Error reading TOML file: {e}") from e
def list_cargo_projects(directory: str):
"""Lists all cargo projects down one level at a given root directory"""
try:
return [
dir
for dir in os.listdir(directory)
if os.path.exists(os.path.join(directory, dir, "mozakvm", "Cargo.toml"))
]
except OSError as e:
raise OSError(f"Error while listing directory: {e}") from e
def has_sdk_dependency_beyond_core_features(cargo_file: str) -> bool:
"""Reads a `Cargo.toml` file and analyses whether the dependency on
`mozak-sdk` is only on "core" features."""
sdk_dependency = read_toml_file(cargo_file)["dependencies"]["mozak-sdk"]
return (
sdk_dependency.get("default-features", True)
or len(sdk_dependency.get("features", [])) > 0
)
def has_no_native_target(example_dir: str) -> bool:
"""Checks if the example directory doesn't have native directory inside.
So we also require that the crate shouldn't have sdk dependency beyond core features
"""
return (
"native" not in os.listdir(example_dir) and
not has_sdk_dependency_beyond_core_features(f"{example_dir}/mozakvm/Cargo.toml")
)
class ExamplesTester(unittest.TestCase):
"""Test class for running examples"""
def test_core_only_examples(self):
"""This test runs examples that depend on just the core
capabilities of the `sdk` i.e. alloc, heap, panic etc
"""
prove_and_verify_exceptions = {"panic"} # TODO: check why `panic` doesn't work
for example in set(list_cargo_projects("examples")):
if has_no_native_target(
f"examples/{example}"
):
print(
f"{Style.BRIGHT}{Fore.BLUE}{example}{Style.RESET_ALL} is detected core-only example"
)
build_command = "cargo mozakvm-build"
print(f"Testing build: {Fore.BLUE}{build_command}{Style.RESET_ALL}")
subprocess.run(
args=shlex.split(build_command),
cwd=os.path.join("examples", example, "mozakvm"),
capture_output=capture_output,
timeout=timeout,
env=os_environ,
check=True,
)
if example in prove_and_verify_exceptions:
print(
f"{Fore.RED}ZK prove and verify skipping for {Style.BRIGHT}{example}{Style.NORMAL} as it is marked as an exception{Style.RESET_ALL}"
)
else:
prove_and_verify_command = f"""cargo run --features=parallel --bin mozak-cli -- prove-and-verify -vvv \
examples/{example}/mozakvm/target/riscv32im-mozak-mozakvm-elf/mozak-release/{example}-mozakvm \
"""
print(
f"ZK prove and verify: {Fore.BLUE}{prove_and_verify_command}{Style.RESET_ALL}"
)
subprocess.run(
args=shlex.split(prove_and_verify_command),
capture_output=capture_output,
timeout=timeout,
env=os_environ,
check=True,
)
print()
def test_full_featured_examples(self):
"""This test runs examples that depend on more than just the core
capabilities of the `sdk` i.e. make use of types, traits, system
tape etc
"""
prove_and_verify_exceptions = {}
for example in set(list_cargo_projects("examples")):
if not has_no_native_target(f"examples/{example}"):
print(
f"{Style.BRIGHT}{Fore.BLUE}{example}{Style.RESET_ALL} is detected fully-featured example, building",
)
build_command = "cargo mozakvm-build --features=\"std\""
print(
f"Testing build: {Fore.BLUE}{build_command}{Style.RESET_ALL}",
)
subprocess.run(
args=shlex.split(build_command),
cwd=os.path.join("examples", example, "mozakvm"),
capture_output=capture_output,
timeout=timeout,
env=os_environ,
check=True,
)
print()
for example in set(list_cargo_projects("examples")):
if not has_no_native_target(f"examples/{example}"):
print(
f"{Style.BRIGHT}{Fore.BLUE}{example}{Style.RESET_ALL} is detected fully-featured example, ZK prove and verify",
)
if example in prove_and_verify_exceptions:
print(
f"{Fore.RED}ZK prove and verify skipping for {Style.BRIGHT}{example}{Style.NORMAL} as it is marked as an exception{Style.RESET_ALL}",
)
else:
# Unlike core-only examples, fully featured examples make use of
# cross-program-calls and system-tapes. Hence, they don't only need
# to test their own prove-and-verify, but need to ensure that other
# programs also prove-and-verify for system-tape they generated. All
# dependent programs to be tested are supposed to be listed in
# `package.metadata.mozak.example_dependents` in respective `Cargo.toml` (the dependent's
# `Cargo.toml` is not read for recursive expansion).
extra_info = read_toml_file(f"examples/{example}/mozakvm/Cargo.toml")[
"package"
]["metadata"]["mozak"]
dependents = []
if "example_dependents" in extra_info.keys():
dependents = extra_info["example_dependents"]
system_tape_generation_command = """cargo run --release"""
print(
f"System tape generation: {Fore.BLUE}{system_tape_generation_command}{Style.RESET_ALL}",
)
subprocess.run(
args=shlex.split(system_tape_generation_command),
cwd=f"examples/{example}/native",
capture_output=capture_output,
timeout=timeout,
env=os_environ,
check=True,
)
print()
system_tape = f"examples/{example}/native/out/tape.json"
programs_to_run = [
f"examples/{example}/mozakvm/target/riscv32im-mozak-mozakvm-elf/mozak-release/{example}-mozakvm",
]
for dependent in dependents:
programs_to_run.append(
f"examples/{dependent}/mozakvm/target/riscv32im-mozak-mozakvm-elf/mozak-release/{dependent}-mozakvm"
)
for elf in programs_to_run:
print(
f"ZK prove and verify for {Style.BRIGHT}{Fore.BLUE}{example}{Style.RESET_ALL} requires execution of {elf} ",
)
execution_command = f"""cargo run --features=parallel --bin mozak-cli -- prove-and-verify -vvv {elf} --system-tape {system_tape} """
print(
f"ZK prove and verify (sub-proof): {Fore.BLUE}{execution_command}{Style.RESET_ALL}",
)
subprocess.run(
args=shlex.split(execution_command),
capture_output=capture_output,
timeout=timeout,
env=os_environ,
check=True,
)
print()
if __name__ == "__main__":
unittest.main()