Skip to content

Commit

Permalink
[ new ] Bump version to v0.19.1
Browse files Browse the repository at this point in the history
  • Loading branch information
banacorn committed Dec 6, 2023
1 parent 7eefe0f commit 7273d39
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion keelung.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ cabal-version: 1.12
-- see: https://github.com/sol/hpack

name: keelung
version: 0.19.0
version: 0.19.1
synopsis: DSL for creating zero-knowledge proofs
description: Please see the README on GitHub at <https://github.com/btq-ag/keelung#readme>
category: Cryptography
Expand Down
2 changes: 1 addition & 1 deletion package.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: keelung
version: 0.19.0
version: 0.19.1
github: "btq-ag/keelung"
license: Apache-2.0
author: "BTQ AG"
Expand Down
41 changes: 21 additions & 20 deletions src/Keelung.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ where
import Control.Monad.Except
import Data.ByteString.Char8 qualified as BS
import Data.Field.Galois (GaloisField)
import Data.Foldable (toList)
import Data.List (intercalate)
import Data.Serialize (Serialize)
import Data.Serialize qualified as Serialize
Expand All @@ -64,15 +65,14 @@ import System.IO.Error qualified as IO
import System.Info qualified
import System.Process qualified as Process
import Text.Read (readMaybe)
import Data.Foldable (toList)

-- | IMPORTANT: The compatibale compiler version of this library, Make sure it's updated and matched accordingly.
keelungCompilerVersion :: (Int, Int)
keelungCompilerVersion = (0, 19)

-- | Patch version of this library
compilerPatchVersion :: Int
compilerPatchVersion = 0
compilerPatchVersion = 1

-- | The version of this library in String
keelungVersion :: String
Expand All @@ -81,7 +81,7 @@ keelungVersion = intercalate "." [show (fst keelungCompilerVersion), show (snd k
--------------------------------------------------------------------------------

-- | Entry point for the Keelung command line interface
keelung :: Encode t => Comp t -> IO ()
keelung :: (Encode t) => Comp t -> IO ()
keelung program = do
-- replace with beefier option parser
options <- getOptions
Expand All @@ -101,15 +101,15 @@ keelung program = do
--------------------------------------------------------------------------------

-- | Compile a program to a 'R1CS' constraint system.
compile :: Encode t => FieldType -> Comp t -> IO (Either Error (R1CS Integer))
compile :: (Encode t) => FieldType -> Comp t -> IO (Either Error (R1CS Integer))
compile = compileWithOpts 1 [] []

-- | Compile a program to a 'R1CS' constraint system with optimization level 0.
compileO0 :: Encode t => FieldType -> Comp t -> IO (Either Error (R1CS Integer))
compileO0 :: (Encode t) => FieldType -> Comp t -> IO (Either Error (R1CS Integer))
compileO0 = compileWithOpts 0 [] []

-- | Compile a program to a 'R1CS' constraint system with optimization level and RTS options as arguments.
compileWithOpts :: Encode t => Int -> [String] -> [String] -> FieldType -> Comp t -> IO (Either Error (R1CS Integer))
compileWithOpts :: (Encode t) => Int -> [String] -> [String] -> FieldType -> Comp t -> IO (Either Error (R1CS Integer))
compileWithOpts level opts rtsopts fieldType prog = runM $ do
elab <- liftEither (elaborateAndEncode prog)
let options = "protocol" : optOptimize level : opts <> ["+RTS"] <> rtsopts <> ["-RTS"]
Expand All @@ -133,7 +133,7 @@ rtsoptMemory m h a = ["-M" <> show m <> "G", "-H" <> show h <> "G", "-A" <> show

-- | Generate a proof given circuit, inputs (witness), paratemer, and proof
prove' ::
Encode t =>
(Encode t) =>
FilePath ->
FilePath ->
FilePath ->
Expand Down Expand Up @@ -170,7 +170,7 @@ prove' circuitPath witnessPath paramPath proofPath fieldType prog publicInput pr
Left err -> print err
Right (_, msg) -> putStr msg

prove :: Encode t => FieldType -> Comp t -> [Integer] -> [Integer] -> IO ()
prove :: (Encode t) => FieldType -> Comp t -> [Integer] -> [Integer] -> IO ()
prove f p i o = do
Path.createDirectoryIfMissing True "aurora"
prove' "aurora/circuit.jsonl" "aurora/witness.jsonl" "aurora/parameter.json" "aurora/proof" f p i o
Expand Down Expand Up @@ -203,14 +203,14 @@ verify :: IO ()
verify = verify' "aurora/circuit.jsonl" "aurora/witness.jsonl" "aurora/parameter.json" "aurora/proof"

-- | Compile a program as R1CS and write it to circuit.jsonl.
genCircuit :: Encode t => FilePath -> FieldType -> Comp t -> M (R1CS Integer)
genCircuit :: (Encode t) => FilePath -> FieldType -> Comp t -> M (R1CS Integer)
genCircuit filePath fieldType prog = do
elab <- liftEither (elaborateAndEncode prog)
r1cs <- callKeelungc ["protocol", "toJSON", "--filepath", filePath] (fieldType, elab) :: M (R1CS Integer)
liftIO $ putStrLn $ "Generated circuit file at: " <> filePath
return r1cs

genCircuitBin :: Encode t => FilePath -> FieldType -> Comp t -> IO (Either Error String)
genCircuitBin :: (Encode t) => FilePath -> FieldType -> Comp t -> IO (Either Error String)
genCircuitBin filePath fieldType prog = runM $ do
elab <- liftEither (elaborateAndEncode prog)
_ <- callKeelungc ["protocol", "genCircuitBin", "--filepath", filePath] (fieldType, elab) :: M (R1CS Integer)
Expand All @@ -221,15 +221,15 @@ genCircuitBin filePath fieldType prog = runM $ do
-- genCircuitDefault = genCircuit "aurora/circuit.jsonl"

-- | Generate witnesses for a program with inputs and write them to witness.jsonl.
genWitness_ :: Encode t => FilePath -> FieldType -> Comp t -> [Integer] -> [Integer] -> M [Integer]
genWitness_ :: (Encode t) => FilePath -> FieldType -> Comp t -> [Integer] -> [Integer] -> M [Integer]
genWitness_ filePath fieldType prog publicInput privateInput = do
elab <- liftEither (elaborateAndEncode prog)
output <- callKeelungc ["protocol", "genWitness", "--filepath", filePath] (fieldType, elab, publicInput, privateInput)
liftIO $ putStrLn $ "Generated witness file at: " <> filePath
return output

-- | Generate witnesses for a program with inputs and write them to witness.jsonl.
genWtns :: Encode t => FilePath -> FieldType -> Comp t -> [Integer] -> [Integer] -> IO (Either Error String)
genWtns :: (Encode t) => FilePath -> FieldType -> Comp t -> [Integer] -> [Integer] -> IO (Either Error String)
genWtns filePath fieldType prog publicInput privateInput = runM $ do
elab <- liftEither (elaborateAndEncode prog)
_ <- callKeelungc ["protocol", "genWtns", "--filepath", filePath] (fieldType, elab, publicInput, privateInput) :: M [Integer]
Expand All @@ -243,10 +243,10 @@ genParameters filePath = do
liftIO $ putStrLn $ "Generated parameter file at: " <> filePath

-- | For generating witness.jsonl
witness' :: Encode t => FilePath -> FieldType -> Comp t -> [Integer] -> [Integer] -> IO [Integer]
witness' :: (Encode t) => FilePath -> FieldType -> Comp t -> [Integer] -> [Integer] -> IO [Integer]
witness' fp fieldType prog publicInput privateInput = runM (genWitness_ fp fieldType prog publicInput privateInput) >>= printErrorInstead

witness :: Encode t => FieldType -> Comp t -> [Integer] -> [Integer] -> IO [Integer]
witness :: (Encode t) => FieldType -> Comp t -> [Integer] -> [Integer] -> IO [Integer]
witness = witness' "aurora/witness.jsonl"

-- | For generating inputs.jsonl
Expand All @@ -260,18 +260,18 @@ genInputsDefault = genInputs "inputs.jsonl"

--------------------------------------------------------------------------------

printErrorInstead :: Show e => Either e [a] -> IO [a]
printErrorInstead :: (Show e) => Either e [a] -> IO [a]
printErrorInstead (Left err) = do
print err
return []
printErrorInstead (Right values) = return values

-- | Interpret a program with public and private inputs
interpret :: Encode t => FieldType -> Comp t -> [Integer] -> [Integer] -> IO [Integer]
interpret :: (Encode t) => FieldType -> Comp t -> [Integer] -> [Integer] -> IO [Integer]
interpret fieldType prog publicInput privateInput = interpretEither fieldType prog publicInput privateInput >>= printErrorInstead

-- | Interpret a program with public and private inputs
interpretEither :: Encode t => FieldType -> Comp t -> [Integer] -> [Integer] -> IO (Either Error [Integer])
interpretEither :: (Encode t) => FieldType -> Comp t -> [Integer] -> [Integer] -> IO (Either Error [Integer])
interpretEither fieldType prog publicInput privateInput =
runM
( do
Expand All @@ -282,10 +282,10 @@ interpretEither fieldType prog publicInput privateInput =
--------------------------------------------------------------------------------

-- | Elaborate a program and encode it
elaborateAndEncode :: Encode t => Comp t -> Either Error Encoding.Elaborated
elaborateAndEncode :: (Encode t) => Comp t -> Either Error Encoding.Elaborated
elaborateAndEncode prog = encodeElaborated <$> elaborate prog
where
encodeElaborated :: Encode t => Elaborated t -> Encoding.Elaborated
encodeElaborated :: (Encode t) => Elaborated t -> Encoding.Elaborated
encodeElaborated (Elaborated expr comp) = runHeapM (compHeap comp) $ do
let Computation counters _addrSize _heap assertions sideEffects = comp
in Encoding.Elaborated
Expand Down Expand Up @@ -410,6 +410,7 @@ readKeelungVersion cmd args = do
-- | Check if the compiler version matches the version of this library
-- patch number can be different
checkCompilerVersion :: (Int, Int, Int) -> M ()
checkCompilerVersion (0, 19, 0) = throwError (VersionMismatchError 0 19 0) -- special case for v0.19.1 against v0.19.0, should be removed in the future
checkCompilerVersion (major, minor, patch) = do
if (major, minor) == keelungCompilerVersion && patch >= 0
then return ()
Expand Down Expand Up @@ -444,7 +445,7 @@ catchIOError err f = lift (IO.catchIOError (Right <$> f) (const (return (Left er

--------------------------------------------------------------------------------

instance Encode a => Show (Comp a) where
instance (Encode a) => Show (Comp a) where
show prog = case elaborateAndEncode prog of
Left err -> show err
Right elaborated -> show elaborated
2 changes: 1 addition & 1 deletion src/Keelung/Error.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ instance Show Error where
show CannotReadVersionError = "Cannot read the version of the Keelung compiler"
show (VersionMismatchError major minor patch) =
"The version of the Keelung compiler is not supported: \n"
++ " expected range of version: >= v0.19.0 and < v0.20.0, but got v"
++ " expected range of version: >= v0.19.1 and < v0.20.0, but got v"
++ show major
++ "."
++ show minor
Expand Down

0 comments on commit 7273d39

Please sign in to comment.