Skip to content
This repository has been archived by the owner on Jan 28, 2022. It is now read-only.

R1CS output flatbuffer #14

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 18 additions & 12 deletions app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ import qualified Util.Cfg as Cfg
( setFromEnv
, evalCfg
, defaultCfgState
, R1CSOutput(..)
)

openFile :: FilePath -> IOMode -> IO Handle
Expand Down Expand Up @@ -173,15 +174,15 @@ type Order
-- type Order = 17
-- type OrderCtx = Ctx Order

cmdEmitR1cs :: Bool -> FilePath -> FilePath -> Cfg ()
cmdEmitR1cs asJson circomPath r1csPath = do
cmdEmitR1cs :: Cfg.R1CSOutput -> FilePath -> FilePath -> Cfg ()
cmdEmitR1cs r1csOutputType circomPath r1csPath = do
liftIO $ print "Loading circuit"
m <- liftIO $ loadMain circomPath
r1cs <- evalLog $ (Link.linkMain @Order m >>= Opt.opt)
liftIO $ do
putStrLn $ R1cs.r1csStats r1cs
--putStrLn $ R1cs.r1csShow r1cs
R1cs.writeToR1csFile asJson r1cs r1csPath
R1cs.writeToR1csFile r1csOutputType r1cs r1csPath

cmdCountTerms :: FilePath -> Cfg ()
cmdCountTerms circomPath = do
Expand All @@ -195,7 +196,7 @@ cmdCountTerms circomPath = do

cmdSetup :: FilePath -> FilePath -> FilePath -> FilePath -> FilePath -> Cfg ()
cmdSetup libsnark circomPath r1csPath pkPath vkPath = do
cmdEmitR1cs False circomPath r1csPath
cmdEmitR1cs Cfg.Legacy circomPath r1csPath
liftIO $ runSetup libsnark r1csPath pkPath vkPath

cmdProve
Expand Down Expand Up @@ -253,11 +254,11 @@ cmdCEval name path = do
r <- evalLog $ evalFn False tu name
forM_ (Map.toList r) $ \(k, v) -> liftIO $ putStrLn $ unwords [k, ":", show v]

cmdCEmitR1cs :: Bool -> Bool -> String -> FilePath -> FilePath -> Cfg ()
cmdCEmitR1cs findBugs asJson fnName cPath r1csPath = do
cmdCEmitR1cs :: Bool -> Cfg.R1CSOutput -> String -> FilePath -> FilePath -> Cfg ()
cmdCEmitR1cs findBugs r1csOutputType fnName cPath r1csPath = do
tu <- liftIO $ parseC cPath
r1cs <- evalLog $ fnToR1cs @Order findBugs Nothing tu fnName
liftIO $ R1cs.writeToR1csFile asJson r1cs r1csPath
liftIO $ R1cs.writeToR1csFile r1csOutputType r1cs r1csPath

cmdCSetup
:: Bool
Expand All @@ -269,7 +270,7 @@ cmdCSetup
-> FilePath
-> Cfg ()
cmdCSetup findBugs libsnark fnName cPath r1csPath pkPath vkPath = do
cmdCEmitR1cs findBugs False fnName cPath r1csPath
cmdCEmitR1cs findBugs Cfg.Legacy fnName cPath r1csPath
liftIO $ runSetup libsnark r1csPath pkPath vkPath

cmdCProve
Expand Down Expand Up @@ -336,9 +337,9 @@ main = do
cmd :: Cfg () = case True of
_ | args `isPresent` command "emit-r1cs" -> do
circomPath <- args `getExistingFilePath` longOption "circom"
let asJson = args `isPresent` longOption "json"
let r1csOutputType = parseR1CSOutputFromArgs args
r1csPath <- args `getArgOrExit` shortOption 'C'
cmdEmitR1cs asJson circomPath r1csPath
cmdEmitR1cs r1csOutputType circomPath r1csPath
_ | args `isPresent` command "count-terms" -> do
circomPath <- args `getExistingFilePath` longOption "circom"
cmdCountTerms circomPath
Expand Down Expand Up @@ -374,11 +375,11 @@ main = do
path <- args `getExistingFilePath` argument "path"
cmdCEval fnName path
_ | args `isPresent` command "c-emit-r1cs" -> do
let asJson = args `isPresent` longOption "json"
let r1csOutputType = parseR1CSOutputFromArgs args
fnName <- args `getArgOrExit` argument "fn-name"
path <- args `getExistingFilePath` argument "path"
r1csPath <- args `getArgOrExit` shortOption 'C'
cmdCEmitR1cs False asJson fnName path r1csPath
cmdCEmitR1cs False r1csOutputType fnName path r1csPath
_ | args `isPresent` command "c-setup" -> do
libsnark <- args `getExistingFilePath` longOption "libsnark"
fnName <- args `getArgOrExit` argument "fn-name"
Expand Down Expand Up @@ -428,3 +429,8 @@ main = do
_ -> liftIO $ exitWithUsageMessage patterns "Missing command!"
cfg <- Cfg.setFromEnv Cfg.defaultCfgState
Cfg.evalCfg cmd cfg
where
parseR1CSOutputFromArgs m | m `isPresent` longOption "json" = Cfg.Json
| m `isPresent` longOption "flatbuffer" = Cfg.FlatBuffer
| otherwise = Cfg.Legacy

Comment on lines +432 to +436
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would drop this (and other asJson handling), in favor of a Cfg-based approach.

1 change: 1 addition & 0 deletions package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ library:
- language-c >= 0.8.3 && < 0.9
- array >= 0.5
- filepath >= 1.4
- flatbuffers >= 0.2.0
- ghc >= 8.3
- bv >= 0.5
- ghc-typelits-knownnat >= 0.7
Expand Down
11 changes: 7 additions & 4 deletions src/IR/R1cs.hs
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ import System.IO ( hPutStr
, IOMode(WriteMode)
, hClose
)
import Util.Cfg ( R1CSOutput(..) )
-- Faster IO?
-- import qualified Data.Text.IO as TextIO
-- import System.IO ( openFile
Expand Down Expand Up @@ -362,10 +363,12 @@ r1csAsLines r1cs =
in [nPubIns, nWit, nConstraints] : constraintLines

-- Todo: implement this using a better IO system.
writeToR1csFile :: (Show s, KnownNat n) => Bool -> R1CS s n -> FilePath -> IO ()
writeToR1csFile asJson r1cs path = if asJson
then ByteString.writeFile path $ encode r1cs
else writeFile path $ unlines $ map (unwords . map show) $ r1csAsLines r1cs
writeToR1csFile
:: (Show s, KnownNat n) => R1CSOutput -> R1CS s n -> FilePath -> IO ()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would lift this to the Cfg monad and get the output format from it.

writeToR1csFile Json r1cs path = ByteString.writeFile path $ encode r1cs
writeToR1csFile FlatBuffer r1cs path = undefined -- TODO when we get the schema from SGA
writeToR1csFile Legacy r1cs path =
writeFile path $ unlines $ map (unwords . map show) $ r1csAsLines r1cs
-- else do
-- h <- openFile path WriteMode
-- forM_ (r1csAsLines r1cs) $ \line -> do
Expand Down
3 changes: 3 additions & 0 deletions src/Util/Cfg.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Util.Cfg
, defaultCfgState
, evalCfg
, evalCfgDefault
, R1CSOutput(..)
)
where

Expand All @@ -42,6 +43,8 @@ data SmtOptCfg = SmtOptCfg { _allowSubBlowup :: Bool
, _benesThresh :: Int
} deriving (Show)

data R1CSOutput = Json | Legacy | FlatBuffer deriving (Show, Eq)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add this to the configuration system


defaultSmtOptCfg :: SmtOptCfg
defaultSmtOptCfg = SmtOptCfg
{ _allowSubBlowup = False
Expand Down
2 changes: 2 additions & 0 deletions stack.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ extra-deps:
- mod-0.1.2.0@sha256:4c331986c46808d89da590d791841f046509041c849c4254b0872c72284f73ad,2202
- poly-0.4.0.0@sha256:75c243113712745dab5d2f4a52643705e146473770af212f9e0ce3d87a83f2ed,2123
- semirings-0.5.4@sha256:5bd49c52a22c876c93d030cafa96b1233e9e7c42dacbe9f49686574368578c9c,2998
- flatbuffers-0.2.0.0@sha256:2aad954811ae8050beecd5100f1695ba5dc67eb71d2be56023144afa61465257,4543

# Override default flag values for local packages and extra-deps
# flags: {}

Expand Down
15 changes: 4 additions & 11 deletions stack.yaml.lock
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,6 @@ packages:
sha256: e98a21bc2bdaba7dfba0aeb33d7b2ed5472cd496aea38098b40bac665171cefb
original:
hackage: mod-0.1.2.0@sha256:4c331986c46808d89da590d791841f046509041c849c4254b0872c72284f73ad,2202
- completed:
hackage: natural-numbers-0.1.2.0@sha256:0fcbe1220979b83fa612fca73a1adb1a99ff43c84f9652b3ff32b2323309b43c,1407
pantry-tree:
size: 220
sha256: bd4beb677280f4586265c5657ab4e4a599c16f9e624df43d8edda6bf1e46dcdf
original:
hackage: natural-numbers-0.1.2.0@sha256:0fcbe1220979b83fa612fca73a1adb1a99ff43c84f9652b3ff32b2323309b43c,1407
- completed:
hackage: poly-0.4.0.0@sha256:75c243113712745dab5d2f4a52643705e146473770af212f9e0ce3d87a83f2ed,2123
pantry-tree:
Expand All @@ -100,12 +93,12 @@ packages:
original:
hackage: semirings-0.5.4@sha256:5bd49c52a22c876c93d030cafa96b1233e9e7c42dacbe9f49686574368578c9c,2998
- completed:
hackage: string-interpolate-0.3.0.2@sha256:0102ed1eac13b7f85c6d2d3da764ad178d98a23d74912f5d2898eb54af624651,4022
hackage: flatbuffers-0.2.0.0@sha256:2aad954811ae8050beecd5100f1695ba5dc67eb71d2be56023144afa61465257,4543
pantry-tree:
size: 1002
sha256: 073892b62ecdbee3138c572ede59b223433ac56795af96d2b18885de23b82f96
size: 2876
sha256: 71fb1dc36729e49044f85c068bb9953e877c99a839cf9867df813ee540bea1d3
original:
hackage: string-interpolate-0.3.0.2@sha256:0102ed1eac13b7f85c6d2d3da764ad178d98a23d74912f5d2898eb54af624651,4022
hackage: flatbuffers-0.2.0.0@sha256:2aad954811ae8050beecd5100f1695ba5dc67eb71d2be56023144afa61465257,4543
snapshots:
- completed:
size: 524996
Expand Down