Skip to content

Commit cf6e34f

Browse files
committed
move encoding from global opts
1 parent 86591c7 commit cf6e34f

File tree

1 file changed

+30
-28
lines changed
  • circom-compat/src/Circom

1 file changed

+30
-28
lines changed

Diff for: circom-compat/src/Circom/CLI.hs

+30-28
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,7 @@ import R1CS (R1CS, Witness (Witness), isValidWitness, toR1CS)
2626
import Prelude (MonadFail (fail))
2727

2828
data GlobalOpts = GlobalOpts
29-
{ cmd :: Command,
30-
encoding :: Encoding
29+
{ cmd :: Command
3130
}
3231

3332
optsParser :: Text -> ParserInfo GlobalOpts
@@ -43,27 +42,6 @@ optsParser progName =
4342
globalOptsParser =
4443
GlobalOpts
4544
<$> hsubparser (compileCommand <> solveCommand <> verifyCommand)
46-
<*> encodingParser
47-
48-
encodingParser :: Parser Encoding
49-
encodingParser =
50-
let readEncoding = eitherReader $ \case
51-
"hex" -> pure HexString
52-
"decimal-string" -> pure DecString
53-
"decimal" -> pure Dec
54-
_ -> throwError $ "Invalid encoding, expected one of: hex, decimal-string, decimal"
55-
in option
56-
readEncoding
57-
( long "encoding"
58-
<> help "encoding for inputs and outputs"
59-
<> showDefaultWith
60-
( \case
61-
HexString -> "hex"
62-
DecString -> "decimal-string"
63-
Dec -> "decimal"
64-
)
65-
<> value Dec
66-
)
6745

6846
compileCommand :: Mod CommandFields Command
6947
compileCommand =
@@ -88,7 +66,8 @@ data CompileOpts = CompileOpts
8866
coGenDotFile :: Bool,
8967
coIncludeJson :: Bool,
9068
coR1CSFile :: FilePath,
91-
coCircuitBinFile :: FilePath
69+
coCircuitBinFile :: FilePath,
70+
coEncoding :: Encoding
9271
}
9372

9473
compileOptsParser :: Text -> Parser CompileOpts
@@ -119,6 +98,27 @@ compileOptsParser progName =
11998
<> showDefault
12099
<> value (Text.unpack progName <> ".bin")
121100
)
101+
<*> encodingParser
102+
103+
encodingParser :: Parser Encoding
104+
encodingParser =
105+
let readEncoding = eitherReader $ \case
106+
"hex" -> pure HexString
107+
"decimal-string" -> pure DecString
108+
"decimal" -> pure Dec
109+
_ -> throwError $ "Invalid encoding, expected one of: hex, decimal-string, decimal"
110+
in option
111+
readEncoding
112+
( long "encoding"
113+
<> help "encoding for inputs and outputs"
114+
<> showDefaultWith
115+
( \case
116+
HexString -> "hex"
117+
DecString -> "decimal-string"
118+
Dec -> "decimal"
119+
)
120+
<> value Dec
121+
)
122122

123123
data OptimizeOpts = OptimizeOpts
124124
{ removeUnreachable :: Bool
@@ -137,7 +137,8 @@ data SolveOpts = SolveOpts
137137
soIncludeJson :: Bool,
138138
soCircuitBinFile :: FilePath,
139139
soWitnessFile :: FilePath,
140-
soShowOutputs :: Bool
140+
soShowOutputs :: Bool,
141+
soEncoding :: Encoding
141142
}
142143

143144
solveOptsParser :: Text -> Parser SolveOpts
@@ -169,6 +170,7 @@ solveOptsParser progName =
169170
( long "show-outputs"
170171
<> help "print the output values as json"
171172
)
173+
<*> encodingParser
172174

173175
data VerifyOpts = VerifyOpts
174176
{ voR1CSFile :: FilePath,
@@ -209,7 +211,7 @@ defaultMain progName program = do
209211
let binFilePath = coCircuitBinFile compilerOpts
210212
encodeFile binFilePath prog
211213
when (coGenInputsTemplate compilerOpts) $ do
212-
let inputsTemplate = mkInputsTemplate (encoding opts) (cpVars prog)
214+
let inputsTemplate = mkInputsTemplate (coEncoding compilerOpts) (cpVars prog)
213215
inputsTemplateFilePath = Text.unpack progName <> "-inputs-template.json"
214216
writeIOVars inputsTemplateFilePath inputsTemplate
215217
when (coIncludeJson compilerOpts) $ do
@@ -220,7 +222,7 @@ defaultMain progName program = do
220222
writeFile dotFilePath $ arithCircuitToDot (cpCircuit prog)
221223
Solve solveOpts -> do
222224
inputs <- do
223-
IOVars _ is <- readIOVars (encoding opts) (soInputsFile solveOpts)
225+
IOVars _ is <- readIOVars (soEncoding solveOpts) (soInputsFile solveOpts)
224226
pure $ map (map (fromInteger @f . unFieldElem)) is
225227
let binFilePath = soCircuitBinFile solveOpts
226228
circuit <- decodeFile binFilePath
@@ -230,7 +232,7 @@ defaultMain progName program = do
230232
when (soIncludeJson solveOpts) $ do
231233
A.encodeFile (wtnsFilePath <> ".json") (map fromP wtns)
232234
when (soShowOutputs solveOpts) $ do
233-
let outputs = mkOutputs (encoding opts) (cpVars circuit) (witnessFromCircomWitness wtns)
235+
let outputs = mkOutputs (soEncoding solveOpts) (cpVars circuit) (witnessFromCircomWitness wtns)
234236
print $ A.encode $ encodeIOVars outputs
235237
Verify verifyOpts -> do
236238
let r1csFilePath = voR1CSFile verifyOpts

0 commit comments

Comments
 (0)