forked from whbjzzwjxq/ZKAP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval_detect.py
More file actions
151 lines (141 loc) · 4.35 KB
/
eval_detect.py
File metadata and controls
151 lines (141 loc) · 4.35 KB
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
import argparse
import json
import os
from os import path
from typing import Dict
from utils.utils import *
parser = argparse.ArgumentParser()
parser.add_argument(
"-i",
"--input",
help="Input path or circuit.",
required=True,
)
parser.add_argument(
"-o",
"--output",
help="Output path, must be a directory, default is current directory.",
default="",
)
parser.add_argument(
"-p",
"--printinfo",
help="Print the CDG of given circuit.",
action="store_true",
)
parser.add_argument(
"-g",
"--graphviz",
help="Print the generated graph in dot format.",
action="store_true",
)
parser.add_argument(
"-r",
"--refresh",
help="Ignore current result.",
action="store_true",
)
parser.add_argument(
"--timeout",
help="Timeout for detecting.",
type=int,
default=1200,
)
parser.add_argument(
"-a",
"--arraysize",
help="The max size of array defined in the input circuit.",
default=256,
type=int,
)
def detect(args):
input_path: str = args.input
output_dir: str = args.output
if not path.isdir(output_dir):
raise ValueError(f"Output path must be a directory, current is: {output_dir}")
if path.isdir(input_path):
project_name = path.basename(input_path)
if project_name == "":
project_name = "default_project"
circom_paths = [
path.join(input_path, f)
for f in os.listdir(input_path)
if f.endswith(".circom")
]
else:
project_name = path.basename(input_path).removesuffix(".circom")
circom_paths = [input_path]
log_paths = []
for circom_path in circom_paths:
circom_filename = path.basename(circom_path)
(
output_subdir,
llfile_path,
log_path,
printinfo_path,
graphviz_path,
graphviz_pic_path,
) = gen_result_path(project_name, circom_filename, output_dir)
# Compilation
if not path.exists(llfile_path) or args.refresh:
compile_circom(circom_path, output_subdir, args.arraysize)
if not path.exists(llfile_path):
raise RuntimeError(f"Compilation for {circom_filename} failed!")
# Evaluation
if args.graphviz:
if not path.exists(graphviz_pic_path) or args.refresh:
_, result = detect_llfile(llfile_path, "PrintGraphviz")
with open(graphviz_path, "wb") as f:
f.write(result)
cmds = [
"sfdp",
"-x",
"-Goverlap=scale",
"-Tpng",
graphviz_path,
f"1> {graphviz_pic_path}",
]
execute(cmds)
elif args.printinfo:
if not path.exists(printinfo_path) or args.refresh:
exec_detection(printinfo_path, llfile_path, "PrintGraphInfo")
else:
if not path.exists(log_path) or args.refresh:
exec_detection(log_path, llfile_path, "All", args.timeout)
log_paths.append(log_path)
if not args.graphviz and not args.printinfo:
summary_bugs(output_dir, project_name, log_paths)
def summary_bugs(output_dir: str, project_name: str, log_paths: List[str]):
results = {}
circuit_names = set()
output_path = path.join(output_dir, project_name, ".json")
for log_path in log_paths:
if not log_path.endswith(".all.json"):
continue
with open(log_path, "r") as f:
try:
result: Dict[str, str] = json.load(f)
except json.JSONDecodeError as err:
print(f"Error JSON: {log_path}, Reason: {err}")
continue
if "error" in result:
continue
for k, v in result.items():
if k == "time":
continue
circuit_name = k
if circuit_name in circuit_names:
continue
circuit_names.add(circuit_name)
report_nums = 0
for d in detector_names:
report_nums += len(v["reports"][d])
if report_nums != 0:
results[circuit_name] = v
with open(output_path, "w") as f:
json.dump(results, f)
def _main():
args = parser.parse_args()
detect(args)
if __name__ == "__main__":
_main()