Skip to content

Commit

Permalink
Merge pull request #231 from zkFold/hov-remove-monadblueprint
Browse files Browse the repository at this point in the history
Get rid of MonadBlueprint and move functions from ArithmeticCircuit.Combinators to relevant places of usage
  • Loading branch information
vlasin authored Aug 27, 2024
2 parents ba9e703 + 6537781 commit e34614b
Show file tree
Hide file tree
Showing 15 changed files with 268 additions and 391 deletions.
34 changes: 19 additions & 15 deletions src/ZkFold/Symbolic/Compiler.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,33 +10,37 @@ module ZkFold.Symbolic.Compiler (
solder,
) where

import Data.Aeson (ToJSON)
import Data.Eq (Eq)
import Data.Function (const, (.))
import Data.Proxy (Proxy)
import Prelude (FilePath, IO, Monoid (mempty), Show (..),
putStrLn, type (~), ($), (++))
import Data.Aeson (ToJSON)
import Data.Eq (Eq)
import Data.Function (const, (.))
import Data.Functor (($>))
import Data.Proxy (Proxy)
import Data.Traversable (for)
import Prelude (FilePath, IO, Monoid (mempty), Show (..), Traversable,
putStrLn, type (~), ($), (++))

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.Number
import ZkFold.Base.Data.Vector (Vector, unsafeToVector)
import ZkFold.Prelude (writeFileJSON)
import ZkFold.Symbolic.Class (Arithmetic)
import ZkFold.Base.Data.Vector (Vector, unsafeToVector)
import ZkFold.Prelude (writeFileJSON)
import ZkFold.Symbolic.Class (Arithmetic, Symbolic (..))
import ZkFold.Symbolic.Compiler.ArithmeticCircuit
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Combinators (forceOne)
import ZkFold.Symbolic.Data.Class
import ZkFold.Symbolic.MonadCircuit (MonadCircuit (..))

{-
ZkFold Symbolic compiler module dependency order:
1. ZkFold.Symbolic.Compiler.ArithmeticCircuit.Internal
2. ZkFold.Symbolic.Compiler.ArithmeticCircuit.Map
3. ZkFold.Symbolic.Compiler.ArithmeticCircuit.MonadBlueprint
4. ZkFold.Symbolic.Compiler.ArithmeticCircuit.Combinators
5. ZkFold.Symbolic.Compiler.ArithmeticCircuit.Instance
6. ZkFold.Symbolic.Compiler.ArithmeticCircuit
7. ZkFold.Symbolic.Compiler
3. ZkFold.Symbolic.Compiler.ArithmeticCircuit.Instance
4. ZkFold.Symbolic.Compiler.ArithmeticCircuit
5. ZkFold.Symbolic.Compiler
-}

forceOne :: (Symbolic c, Traversable f) => c f -> c f
forceOne r = fromCircuitF r (\fi -> for fi $ \i -> constraint (\x -> x i - one) $> i)


-- | Arithmetizes an argument by feeding an appropriate amount of inputs.
solder ::
forall a c f .
Expand Down
52 changes: 35 additions & 17 deletions src/ZkFold/Symbolic/Compiler/ArithmeticCircuit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,26 +33,27 @@ module ZkFold.Symbolic.Compiler.ArithmeticCircuit (
checkClosedCircuit
) where

import Control.Monad.State (execState)
import Data.Map hiding (drop, foldl, foldr, map, null, splitAt,
take)
import GHC.Generics (U1 (..))
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), drop, length, product,
splitAt, sum, take, (!!), (^))
import Test.QuickCheck (Arbitrary, Property, conjoin, property, vector,
withMaxSuccess, (===))
import Text.Pretty.Simple (pPrint)
import Control.Monad (foldM)
import Control.Monad.State (execState)
import Data.Map hiding (drop, foldl, foldr, map, null, splitAt,
take)
import GHC.Generics (U1 (..))
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), drop, length, product, splitAt,
sum, take, (!!), (^))
import Test.QuickCheck (Arbitrary, Property, conjoin, property, vector,
withMaxSuccess, (===))
import Text.Pretty.Simple (pPrint)

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Polynomials.Multivariate (evalMonomial, evalPolynomial)
import ZkFold.Prelude (length)
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Combinators (desugarRange)
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Instance ()
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Internal (Arithmetic, ArithmeticCircuit (..), Constraint,
apply, eval, eval1, exec, exec1,
witnessGenerator)
import ZkFold.Base.Algebra.Polynomials.Multivariate (evalMonomial, evalPolynomial)
import ZkFold.Prelude (length)
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Instance ()
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Internal (Arithmetic, ArithmeticCircuit (..), Constraint,
apply, eval, eval1, exec, exec1, witnessGenerator)
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Map
import ZkFold.Symbolic.Data.Combinators (expansion)
import ZkFold.Symbolic.MonadCircuit (MonadCircuit (..))

--------------------------------- High-level functions --------------------------------

Expand All @@ -67,6 +68,23 @@ applyArgs r args =
optimize :: ArithmeticCircuit a f -> ArithmeticCircuit a f
optimize = id


desugarRange :: (Arithmetic a, MonadCircuit i a m) => i -> a -> m ()
desugarRange i b
| b == negate one = return ()
| otherwise = do
let bs = binaryExpansion b
is <- expansion (length bs) i
case dropWhile ((== one) . fst) (zip bs is) of
[] -> return ()
((_, k0):ds) -> do
z <- newAssigned (one - ($ k0))
ge <- foldM (\j (c, k) -> newAssigned $ forceGE j c k) z ds
constraint (($ ge) - one)
where forceGE j c k
| c == zero = ($ j) * (one - ($ k))
| otherwise = one + ($ k) * (($ j) - one)

-- | Desugars range constraints into polynomial constraints
desugarRanges :: Arithmetic a => ArithmeticCircuit a f -> ArithmeticCircuit a f
desugarRanges c =
Expand Down
152 changes: 0 additions & 152 deletions src/ZkFold/Symbolic/Compiler/ArithmeticCircuit/Combinators.hs

This file was deleted.

29 changes: 14 additions & 15 deletions src/ZkFold/Symbolic/Compiler/ArithmeticCircuit/Instance.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,23 @@

module ZkFold.Symbolic.Compiler.ArithmeticCircuit.Instance where

import Data.Aeson hiding (Bool)
import Data.Map hiding (drop, foldl, foldl', foldr, map, null,
splitAt, take)
import GHC.Generics (Par1 (..))
import GHC.Num (integerToNatural)
import Prelude (Show, mempty, pure, return, show, ($), (++),
(<$>))
import qualified Prelude as Haskell
import System.Random (mkStdGen)
import Test.QuickCheck (Arbitrary (arbitrary), Gen, chooseInteger,
elements)
import Data.Aeson hiding (Bool)
import Data.Map hiding (drop, foldl, foldl', foldr, map, null,
splitAt, take, toList)
import GHC.Generics (Par1 (..))
import GHC.Num (integerToNatural)
import Prelude (Show, mempty, pure, return, show, ($), (++),
(<$>))
import qualified Prelude as Haskell
import System.Random (mkStdGen)
import Test.QuickCheck (Arbitrary (arbitrary), Gen, chooseInteger,
elements)

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.Number
import ZkFold.Base.Data.Par1 ()
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Combinators (getAllVars)
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Internal hiding (constraint)
import ZkFold.Symbolic.Data.FieldElement (FieldElement (..))
import ZkFold.Base.Data.Par1 ()
import ZkFold.Symbolic.Compiler.ArithmeticCircuit.Internal hiding (constraint)
import ZkFold.Symbolic.Data.FieldElement (FieldElement (..))

------------------------------------- Instances -------------------------------------

Expand Down
Loading

0 comments on commit e34614b

Please sign in to comment.