Skip to content

Commit 46bb600

Browse files
committed
Format linter
1 parent bac9810 commit 46bb600

File tree

9 files changed

+77
-61
lines changed

9 files changed

+77
-61
lines changed

src/zkregex_fuzzer/cli.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,14 @@
1414
from zkregex_fuzzer.harness import HarnessStatus
1515
from zkregex_fuzzer.logger import logger
1616
from zkregex_fuzzer.reproduce import reproduce
17-
from zkregex_fuzzer.runner.circom import CircomSubprocess, SnarkjsSubprocess, ZkRegexSubprocess
17+
from zkregex_fuzzer.runner.circom import (
18+
CircomSubprocess,
19+
SnarkjsSubprocess,
20+
ZkRegexSubprocess,
21+
)
1822
from zkregex_fuzzer.runner.subprocess import BarretenbergSubprocess, NoirSubprocess
1923

24+
2025
def fuzz_parser():
2126
parser = argparse.ArgumentParser(add_help=False)
2227
parser.add_argument(
@@ -49,7 +54,7 @@ def fuzz_parser():
4954
parser.add_argument(
5055
"--seed",
5156
default=str(uuid.uuid4()),
52-
help=f"Seed for random generator (default: UUIDv4)"
57+
help="Seed for random generator (default: UUIDv4)",
5358
)
5459
parser.add_argument(
5560
"--save",
@@ -103,16 +108,15 @@ def fuzz_parser():
103108
parser.add_argument(
104109
"--noir-prove",
105110
action="store_true",
106-
help="Run the proving and verification step with Barretenberg."
111+
help="Run the proving and verification step with Barretenberg.",
107112
)
108113

109114
parser.add_argument(
110115
"--logger-level",
111116
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
112117
default="INFO",
113-
help="Set the logger level (default: INFO)."
118+
help="Set the logger level (default: INFO).",
114119
)
115-
116120

117121
return parser
118122

@@ -131,7 +135,7 @@ def reproduce_parser():
131135
"--logger-level",
132136
choices=["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"],
133137
default="INFO",
134-
help="Set the logger level (default: INFO)."
138+
help="Set the logger level (default: INFO).",
135139
)
136140

137141
return parser

src/zkregex_fuzzer/configs.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
from zkregex_fuzzer.runner import CircomRunner, PythonReRunner, NoirRunner
21
from zkregex_fuzzer.grammar import REGEX_GRAMMAR
32
from zkregex_fuzzer.regexgen import DatabaseRegexGenerator, GrammarRegexGenerator
4-
from zkregex_fuzzer.runner import CircomRunner, PythonReRunner
3+
from zkregex_fuzzer.runner import CircomRunner, NoirRunner, PythonReRunner
54
from zkregex_fuzzer.vinpgen import ExrexGenerator, GrammarBasedGenerator, RstrGenerator
65

76
TARGETS = {

src/zkregex_fuzzer/grammar.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
TODO:
2020
- Add more grammars.
2121
"""
22+
2223
import string
2324
from typing import List
2425

src/zkregex_fuzzer/report.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
"""
22
Implements the logic for generating a report from the fuzzing results.
33
"""
4+
45
from zkregex_fuzzer.harness import HarnessResult
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
from .base_runner import RegexCompileError, RegexRunError, Runner
22
from .circom import CircomRunner
3-
from .python import PythonReRunner
4-
from .circom import CircomRunner
53
from .noir import NoirRunner
4+
from .python import PythonReRunner

src/zkregex_fuzzer/runner/circom.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22
Runner for Circom.
33
"""
44

5-
import json, tempfile, subprocess, shutil
5+
import json
6+
import shutil
7+
import tempfile
68
from pathlib import Path
9+
710
from zkregex_fuzzer.logger import logger
8-
from zkregex_fuzzer.runner.base_runner import Runner, RegexCompileError, RegexRunError
11+
from zkregex_fuzzer.runner.base_runner import RegexRunError, Runner
912
from zkregex_fuzzer.runner.subprocess import (
10-
ZkRegexSubprocess,
1113
CircomSubprocess,
1214
SnarkjsSubprocess,
15+
ZkRegexSubprocess,
1316
)
1417

1518

@@ -40,7 +43,7 @@ def compile(self, regex: str) -> None:
4043
"""
4144
Compile the regex.
4245
"""
43-
logger.debug(f"Compiling regex starts")
46+
logger.debug("Compiling regex starts")
4447
# Create JSON for the regex for zk-regex
4548
base_json = {"parts": []}
4649
# TODO: handle the following if is_public is set to True:
@@ -57,11 +60,11 @@ def compile(self, regex: str) -> None:
5760
circom_file_path = str(Path(self._dir_path) / "regex.circom")
5861

5962
# Call zk-regex to generate the circom code
60-
logger.debug(f"Generating circom code starts")
63+
logger.debug("Generating circom code starts")
6164
ZkRegexSubprocess.compile_to_circom(
6265
json_file_path, circom_file_path, self._template_name
6366
)
64-
logger.debug(f"Generating circom code ends")
67+
logger.debug("Generating circom code ends")
6568

6669
# Append the circom file to include the main function
6770
with open(circom_file_path, "a") as f:
@@ -74,25 +77,25 @@ def compile(self, regex: str) -> None:
7477
self._circom_path = circom_file_path
7578

7679
# Compile the circom code to wasm
77-
logger.debug(f"Compiling circom code starts")
80+
logger.debug("Compiling circom code starts")
7881
self._wasm_path, self._r1cs_path = CircomSubprocess.compile(
7982
circom_file_path, self._link_path
8083
)
81-
logger.debug(f"Compiling circom code ends")
84+
logger.debug("Compiling circom code ends")
8285

8386
# Also setup the proving and verification key if the flag is set
8487
if self._run_the_prover:
8588
self._zkey_path = SnarkjsSubprocess.setup_zkey(
8689
self._r1cs_path, self._ptau_path
8790
)
8891
self._vkey_path = SnarkjsSubprocess.export_verification_key(self._zkey_path)
89-
logger.debug(f"Compiling regex ends")
92+
logger.debug("Compiling regex ends")
9093

9194
def match(self, input: str) -> tuple[bool, str]:
9295
"""
9396
Match the regex on an input.
9497
"""
95-
logger.debug(f"Matching regex starts")
98+
logger.debug("Matching regex starts")
9699
# Convert input to list of decimal ASCII values and pad input with zeroes
97100
numeric_input = [ord(c) for c in input] + [0] * (
98101
self._circom_max_input_size - len(input)
@@ -110,9 +113,9 @@ def match(self, input: str) -> tuple[bool, str]:
110113
raise RegexRunError(f"Input too large for input: {len(numeric_input)}")
111114

112115
# Generate the witness
113-
logger.debug(f"Generating witness starts")
116+
logger.debug("Generating witness starts")
114117
witness_path = SnarkjsSubprocess.witness_gen(self._wasm_path, input_path)
115-
logger.debug(f"Generating witness ends")
118+
logger.debug("Generating witness ends")
116119

117120
# Also run the proving backend if the flag is set
118121
if self._run_the_prover:
@@ -121,7 +124,7 @@ def match(self, input: str) -> tuple[bool, str]:
121124
# Verification
122125
if not SnarkjsSubprocess.verify(self._vkey_path, proof, public_input):
123126
raise RegexRunError(
124-
f"Error running with SnarkJS: Proof verification failed"
127+
"Error running with SnarkJS: Proof verification failed"
125128
)
126129

127130
# Extract from the witness the result of the match
@@ -131,7 +134,7 @@ def match(self, input: str) -> tuple[bool, str]:
131134

132135
# Return the output of the match
133136
output = int(result[1])
134-
logger.debug(f"Matching regex ends")
137+
logger.debug("Matching regex ends")
135138
return output == 1, ""
136139

137140
def clean(self):

src/zkregex_fuzzer/runner/noir.py

Lines changed: 43 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,19 @@
22
Runner for Circom.
33
"""
44

5-
import json, tempfile, subprocess, shutil
5+
import json
6+
import shutil
7+
import tempfile
68
from pathlib import Path
9+
710
from zkregex_fuzzer.logger import logger
8-
from zkregex_fuzzer.runner.base_runner import Runner, RegexCompileError, RegexRunError
9-
from zkregex_fuzzer.runner.subprocess import BarretenbergSubprocess, NoirSubprocess, ZkRegexSubprocess
11+
from zkregex_fuzzer.runner.base_runner import RegexRunError, Runner
12+
from zkregex_fuzzer.runner.subprocess import (
13+
BarretenbergSubprocess,
14+
NoirSubprocess,
15+
ZkRegexSubprocess,
16+
)
17+
1018

1119
class NoirRunner(Runner):
1220
"""
@@ -23,40 +31,40 @@ def __init__(self, regex: str, kwargs: dict):
2331
self.identifer = ""
2432

2533
def _construct_working_dir(self) -> str:
26-
2734
dir_path = Path(self._path)
2835

2936
# create nargo.toml
3037
with open(dir_path / "Nargo.toml", "w") as f:
3138
content = '[package]\nname = "test_regex"\ntype = "bin"\nauthors = [""]\n\n[dependencies]'
3239
f.write(content)
3340

34-
src_path = (dir_path / "src")
41+
src_path = dir_path / "src"
3542
src_path.mkdir()
3643

3744
# create src/main.nr
3845
with open(src_path / "main.nr", "w") as f:
3946
main_func = "mod regex;\n\n"
40-
main_func += f"global MAX_INPUT_SIZE: u32 = {self._noir_max_input_size};\n\n"
41-
main_func += "fn main(input: [u8; MAX_INPUT_SIZE]) { regex::regex_match(input); }"
47+
main_func += (
48+
f"global MAX_INPUT_SIZE: u32 = {self._noir_max_input_size};\n\n"
49+
)
50+
main_func += (
51+
"fn main(input: [u8; MAX_INPUT_SIZE]) { regex::regex_match(input); }"
52+
)
4253
f.write(main_func)
4354

4455
return str(src_path)
4556

46-
4757
def compile(self, regex: str) -> None:
4858
"""
4959
Compile the regex.
5060
"""
51-
logger.debug(f"Compiling regex starts")
61+
logger.debug("Compiling regex starts")
5262

5363
# Setup main directory
5464
src_path = self._construct_working_dir()
5565

56-
# Create JSON for the regex for zk-regex
57-
base_json = {
58-
"parts": []
59-
}
66+
# Create JSON for the regex for zk-regex
67+
base_json = {"parts": []}
6068
# TODO: handle the following if is_public is set to True:
6169
# the section containing this ^ must be non-public (is_public: false).
6270
regex_parts = {"regex_def": regex, "is_public": False}
@@ -65,67 +73,71 @@ def compile(self, regex: str) -> None:
6573

6674
# Write the JSON to a temporary file
6775
json_file_path = str(Path(self._path) / "regex.json")
68-
with open(json_file_path, 'wb') as f:
76+
with open(json_file_path, "wb") as f:
6977
f.write(regex_json.encode())
7078

7179
noir_file_path = str(Path(src_path) / "regex.nr")
7280

7381
# Call zk-regex to generate the noir code
74-
logger.debug(f"Generating noir code starts")
82+
logger.debug("Generating noir code starts")
7583
ZkRegexSubprocess.compile_to_noir(json_file_path, noir_file_path)
76-
logger.debug(f"Generating noir code ends")
84+
logger.debug("Generating noir code ends")
7785

7886
# Compile the noir code (nargo check)
79-
logger.debug(f"Compiling noir code starts")
87+
logger.debug("Compiling noir code starts")
8088
NoirSubprocess.compile(self._path)
81-
logger.debug(f"Compiling noir code ends")
89+
logger.debug("Compiling noir code ends")
8290

8391
if self._run_the_prover:
8492
BarretenbergSubprocess.export_verification_key(self._path)
8593

86-
logger.debug(f"Compiling regex ends")
94+
logger.debug("Compiling regex ends")
8795

8896
def match(self, input: str) -> tuple[bool, str]:
8997
"""
9098
Match the regex on an input.
9199
"""
92100

93-
logger.debug(f"Matching regex starts")
101+
logger.debug("Matching regex starts")
94102
# Convert input to list of decimal ASCII values and pad input with zeroes
95-
numeric_input = [ord(c) for c in input] + [0] * (self._noir_max_input_size - len(input))
96-
103+
numeric_input = [ord(c) for c in input] + [0] * (
104+
self._noir_max_input_size - len(input)
105+
)
106+
97107
# Write input to a Prover.toml
98108
with open(Path(self._path) / "Prover.toml", "w") as f:
99109
f.write(f"input = {str(numeric_input)}")
100-
110+
101111
# Skip if input is larger than noir max input size
102112
if len(numeric_input) > self._noir_max_input_size:
103-
raise RegexRunError(f"Input too large for input: {len(numeric_input)}")
104-
113+
raise RegexRunError("Input too large for input: {len(numeric_input)}")
114+
105115
# Generate the witness
106-
logger.debug(f"Generating witness starts")
116+
logger.debug("Generating witness starts")
107117
match = NoirSubprocess.witness_gen(self._path)
108-
logger.debug(f"Generating witness ends")
118+
logger.debug("Generating witness ends")
109119

110120
if self._run_the_prover and match:
111121
# prove
112122
BarretenbergSubprocess.prove(self._path)
113123
# verify
114124
if not BarretenbergSubprocess.verify(self._path):
115-
raise RegexRunError(f"Error running with Barretenberg: Proof verification failed")
125+
raise RegexRunError(
126+
"Error running with Barretenberg: Proof verification failed"
127+
)
116128

117129
return match, ""
118-
130+
119131
def clean(self):
120132
# Remove all temporary files
121133
if Path(self._path).exists():
122134
shutil.rmtree(self._path)
123-
135+
124136
def save(self, path) -> str:
125137
base_path = Path(self._path)
126138
target_path = Path(path).resolve()
127139

128140
dst_path = target_path / f"output_{base_path.stem}"
129141
base_path.replace(dst_path)
130142

131-
return str(dst_path)
143+
return str(dst_path)

0 commit comments

Comments
 (0)