22Runner for Circom.
33"""
44
5- import json , tempfile , subprocess , shutil
5+ import json
6+ import shutil
7+ import tempfile
68from pathlib import Path
9+
710from 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
1119class 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]\n name = "test_regex"\n type = "bin"\n authors = [""]\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