77import random
88import uuid
99from pathlib import Path
10- from zkregex_fuzzer .reproduce import reproduce
10+
11+ from zkregex_fuzzer .configs import GENERATORS , TARGETS , VALID_INPUT_GENERATORS
1112from zkregex_fuzzer .fuzzer import fuzz_with_database , fuzz_with_grammar
1213from zkregex_fuzzer .grammar import REGEX_GRAMMAR
13- from zkregex_fuzzer .configs import TARGETS , VALID_INPUT_GENERATORS , GENERATORS
1414from zkregex_fuzzer .harness import HarnessStatus
1515from zkregex_fuzzer .logger import logger
16+ from zkregex_fuzzer .reproduce import reproduce
1617from zkregex_fuzzer .runner .circom import CircomSubprocess , SnarkjsSubprocess , ZkRegexSubprocess
1718from zkregex_fuzzer .runner .subprocess import BarretenbergSubprocess , NoirSubprocess
1819
1920def fuzz_parser ():
20- parser = argparse .ArgumentParser (
21- add_help = False
22- )
21+ parser = argparse .ArgumentParser (add_help = False )
2322 parser .add_argument (
2423 "--regex-num" ,
2524 type = int ,
2625 default = 10 ,
27- help = "Number of regexes to generate (default: 10)."
26+ help = "Number of regexes to generate (default: 10)." ,
2827 )
2928 parser .add_argument (
3029 "--inputs-num" ,
3130 type = int ,
3231 default = 10 ,
33- help = "Number of inputs to generate for each regex (default: 10)."
32+ help = "Number of inputs to generate for each regex (default: 10)." ,
3433 )
3534 parser .add_argument (
3635 "--oracle" ,
3736 choices = ["valid" , "invalid" ],
38- help = "Wherether the generated inputs should be valid or invalid wrt the regex."
37+ help = "Wherether the generated inputs should be valid or invalid wrt the regex." ,
3938 )
4039 parser .add_argument (
4140 "--target" ,
4241 choices = list (TARGETS .keys ()),
43- help = f"The target to fuzz (options: { list (TARGETS .keys ())} )."
42+ help = f"The target to fuzz (options: { list (TARGETS .keys ())} )." ,
4443 )
4544 parser .add_argument (
4645 "--valid-input-generator" ,
4746 choices = list (VALID_INPUT_GENERATORS .keys ()),
48- help = f"The valid input generator to use for the fuzzer (options: { list (VALID_INPUT_GENERATORS .keys ())} )."
47+ help = f"The valid input generator to use for the fuzzer (options: { list (VALID_INPUT_GENERATORS .keys ())} )." ,
4948 )
5049 parser .add_argument (
5150 "--seed" ,
@@ -62,7 +61,7 @@ def fuzz_parser():
6261 "--save-output" ,
6362 type = str ,
6463 default = os .getcwd (),
65- help = f "The output path where the reproducible files will be stored (default: .)"
64+ help = "The output path where the reproducible files will be stored (default: .)" ,
6665 )
6766 parser .add_argument (
6867 "--fuzzer" ,
@@ -74,31 +73,31 @@ def fuzz_parser():
7473 "--grammar-max-depth" ,
7574 type = int ,
7675 default = 5 ,
77- help = "Maximum depth of recursion in the grammar (default: 5)."
76+ help = "Maximum depth of recursion in the grammar (default: 5)." ,
7877 )
7978 parser .add_argument (
8079 "--max-input-size" ,
8180 type = int ,
8281 default = 600 ,
83- help = "Maximum size of the circuit input (default: 600)."
82+ help = "Maximum size of the circuit input (default: 600)." ,
8483 )
8584 parser .add_argument (
8685 "--circom-library" ,
8786 nargs = "*" ,
8887 type = str ,
89- help = "Path to the circom library to be included"
88+ help = "Path to the circom library to be included" ,
9089 )
9190
9291 parser .add_argument (
9392 "--circom-prove" ,
9493 action = "store_true" ,
95- help = "Run the proving and verification step with SnarkJS."
94+ help = "Run the proving and verification step with SnarkJS." ,
9695 )
9796
9897 parser .add_argument (
9998 "--circom-ptau" ,
10099 type = str ,
101- help = "Path to the ptau (powers-of-tau) file for the proving step"
100+ help = "Path to the ptau (powers-of-tau) file for the proving step" ,
102101 )
103102
104103 parser .add_argument (
@@ -117,17 +116,16 @@ def fuzz_parser():
117116
118117 return parser
119118
119+
120120def reproduce_parser ():
121- parser = argparse .ArgumentParser (
122- add_help = False
123- )
121+ parser = argparse .ArgumentParser (add_help = False )
124122
125123 parser .add_argument (
126124 "--path" ,
127125 nargs = "+" ,
128126 type = str ,
129127 help = "Path to the target directory output that want to be reproduced (support wildcard pattern)." ,
130- required = True
128+ required = True ,
131129 )
132130 parser .add_argument (
133131 "--logger-level" ,
@@ -138,11 +136,12 @@ def reproduce_parser():
138136
139137 return parser
140138
139+
141140def do_fuzz (args ):
142141 if args .oracle == "valid" and not args .valid_input_generator :
143142 print ("Valid input generator is required for valid oracle." )
144143 exit (1 )
145-
144+
146145 if args .target == "circom" :
147146 try :
148147 zk_regex_version = ZkRegexSubprocess .get_installed_version ()
@@ -177,7 +176,7 @@ def do_fuzz(args):
177176 if not args .circom_ptau :
178177 print ("Path to ptau file is required for proving." )
179178 exit (1 )
180-
179+
181180 ptau_path = Path (args .circom_ptau ).resolve ()
182181 if not ptau_path .exists ():
183182 print (f"Path to ptau file { ptau_path } does not exist." )
@@ -221,46 +220,44 @@ def do_fuzz(args):
221220 regex_num = args .regex_num ,
222221 inputs_num = args .inputs_num ,
223222 max_depth = args .grammar_max_depth ,
224- kwargs = kwargs
223+ kwargs = kwargs ,
225224 )
226225 elif args .fuzzer == "database" :
227226 fuzz_with_database (
228227 target_implementation = args .target ,
229228 oracle_params = (args .oracle == "valid" , args .valid_input_generator ),
230229 regex_num = args .regex_num ,
231230 inputs_num = args .inputs_num ,
232- kwargs = kwargs
231+ kwargs = kwargs ,
233232 )
234233
234+
235235def do_reproduce (args ):
236236 reproduce (args .path )
237237
238+
238239def main ():
239-
240240 parser = argparse .ArgumentParser ()
241241
242242 subparser = parser .add_subparsers (dest = "subcommand" )
243243 subparser .add_parser (
244- "fuzz" ,
245- help = "Fuzz the target regex implementation." ,
246- parents = [fuzz_parser ()]
244+ "fuzz" , help = "Fuzz the target regex implementation." , parents = [fuzz_parser ()]
247245 )
248246 subparser .add_parser (
249247 "reproduce" ,
250248 help = "Reproduce the bug that found by the fuzzer." ,
251- parents = [reproduce_parser ()]
249+ parents = [reproduce_parser ()],
252250 )
253251
254252 args = parser .parse_args ()
255253
256-
257254 logger .setLevel (args .logger_level )
258255
259256 if args .subcommand == "fuzz" :
260257 do_fuzz (args )
261258 elif args .subcommand == "reproduce" :
262259 do_reproduce (args )
263-
260+
264261
265262if __name__ == "__main__" :
266263 main ()
0 commit comments