Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vks4git hash #80

Merged
merged 11 commits into from
Apr 15, 2024
2 changes: 1 addition & 1 deletion .stylish-haskell.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ steps:
# Set this to null to disable all line wrapping.
#
# Default: 80.
columns: null
columns: 120 # null

# By default, line endings are converted according to the OS. You can override
# preferred format here.
Expand Down
3 changes: 1 addition & 2 deletions bench/BenchPolyMul.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ import ZkFold.Base.Algebra.Basic.Field
import ZkFold.Base.Algebra.Basic.Number (Prime)
import ZkFold.Base.Algebra.EllipticCurve.BLS12_381
import ZkFold.Base.Algebra.Polynomials.Univariate
import ZkFold.Prelude (zipWithDefault)

-- | Only for testing DFT with smaller numbers which can be easily calculated by hand for cross-check.
-- DFT of a polynomial of length n requires calculating primitive roots of unity of order n.
-- Choosing 17 allows us to calculate DFT of polynomials of length up to 256 and 16 as all these numbers divide 257 - 1.
-- Choosing 257 allows us to calculate DFT of polynomials of length up to 256 as all these numbers divide 257 - 1.
instance Prime 257

-- | Generate random polynomials of given size
Expand Down
14 changes: 12 additions & 2 deletions examples/Examples/ByteString.hs
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module Examples.ByteString (
exampleByteStringAnd,
exampleByteStringOr
exampleByteStringOr,
exampleByteStringExtend
) where

import Data.Data (Proxy (Proxy))
import Data.Function (($))
import Data.List ((++))
import Data.String (String)
import GHC.TypeNats (KnownNat, natVal)
import GHC.TypeNats (KnownNat, natVal, type (<=))
import System.IO (IO, putStrLn)
import Text.Show (show)

Expand All @@ -26,6 +28,14 @@ exampleByteStringAnd = makeExample @n "*" "and" (&&)
exampleByteStringOr :: forall n . KnownNat n => IO ()
exampleByteStringOr = makeExample @n "+" "or" (||)

exampleByteStringExtend :: forall n k . (KnownNat n, KnownNat k, n <= k) => IO ()
exampleByteStringExtend = do
let n = show $ natVal (Proxy @n)
let k = show $ natVal (Proxy @k)
putStrLn $ "\nExample: Extending a bytestring of length " ++ n ++ " to length " ++ k
let file = "compiled_scripts/bytestring" ++ n ++ "_to_" ++ k ++ ".json"
compileIO @(Zp BLS12_381_Scalar) file $ extend @(ByteString n (ArithmeticCircuit (Zp BLS12_381_Scalar))) @(ByteString k (ArithmeticCircuit (Zp BLS12_381_Scalar)))

type Binary a = a -> a -> a

type UBinary n = Binary (ByteString n (ArithmeticCircuit (Zp BLS12_381_Scalar)))
Expand Down
3 changes: 2 additions & 1 deletion examples/Examples/Eq.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module Examples.Eq (exampleEq) where

import Prelude hiding (Bool, Eq (..), Num (..), Ord (..), any, not, (!!), (/), (^), (||))
import Prelude hiding (Bool, Eq (..), Num (..), Ord (..), any, not, (!!),
(/), (^), (||))

import ZkFold.Base.Algebra.Basic.Field (Zp)
import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_Scalar)
Expand Down
3 changes: 2 additions & 1 deletion examples/Examples/LEQ.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

module Examples.LEQ (exampleLEQ) where

import Prelude hiding (Bool, Eq (..), Num (..), Ord (..), any, not, (!!), (/), (^), (||))
import Prelude hiding (Bool, Eq (..), Num (..), Ord (..), any, not, (!!),
(/), (^), (||))

import ZkFold.Base.Algebra.Basic.Field (Zp)
import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_Scalar)
Expand Down
14 changes: 2 additions & 12 deletions examples/Examples/MiMCHash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,14 @@

module Examples.MiMCHash (exampleMiMC) where

import Examples.MiMC.Constants (mimcConstants)
import Numeric.Natural (Natural)
import Prelude hiding (Eq (..), Num (..), any, not, (!!), (/), (^), (||))

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.Field (Zp)
import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_Scalar)
import ZkFold.Prelude ((!!))
import ZkFold.Symbolic.Algorithms.Hash.MiMC (mimcHash)
import ZkFold.Symbolic.Compiler
import ZkFold.Symbolic.Data.Conditional (bool)
import ZkFold.Symbolic.Types (Symbolic)

-- | MiMC hash function
mimcHash :: forall a . Symbolic a => Natural -> a -> a -> a -> a
mimcHash nRounds k xL xR =
let c = mimcConstants !! (nRounds-!1)
t5 = (xL + k + c) ^ (5 :: Natural)
in bool (xR + t5) (mimcHash (nRounds-!1) k (xR + t5) xL) (nRounds > 1)


exampleMiMC :: IO ()
exampleMiMC = do
Expand Down
3 changes: 2 additions & 1 deletion examples/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Main where

import Examples.ByteString (exampleByteStringAnd, exampleByteStringOr)
import Examples.ByteString (exampleByteStringAnd, exampleByteStringExtend, exampleByteStringOr)
import Examples.Conditional (exampleConditional)
import Examples.Eq (exampleEq)
import Examples.Fibonacci (exampleFibonacci)
Expand Down Expand Up @@ -35,3 +35,4 @@ main = do
exampleByteStringAnd @500
exampleByteStringOr @32
exampleByteStringOr @500
exampleByteStringExtend @1 @512
3 changes: 3 additions & 0 deletions src/ZkFold/Base/Algebra/Basic/Field.hs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ residue = (`mod` fromIntegral (value @p))
toZp :: forall p . KnownNat p => Integer -> Zp p
toZp = Zp . residue @p

instance ToConstant (Zp p) Natural where
toConstant = fromZp

instance KnownNat p => Finite (Zp p) where
type Order (Zp p) = p

Expand Down
5 changes: 3 additions & 2 deletions src/ZkFold/Base/Algebra/Polynomials/Multivariate.hs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,11 @@ module ZkFold.Base.Algebra.Polynomials.Multivariate (

import Data.Bifunctor (first)
import Data.Containers.ListUtils (nubOrd)
import Data.Map (Map, keys, singleton, toList)
import Data.Map.Strict (Map, keys, singleton, toList)
import Data.Maybe (fromJust)
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), length, product, replicate, sum, (!!), (^))
import Prelude hiding (Num (..), length, product, replicate,
sum, (!!), (^))

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Polynomials.Multivariate.Monomial
Expand Down
27 changes: 15 additions & 12 deletions src/ZkFold/Base/Algebra/Polynomials/Multivariate/Monomial.hs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NoGeneralisedNewtypeDeriving #-}
{-# LANGUAGE TypeApplications #-}

module ZkFold.Base.Algebra.Polynomials.Multivariate.Monomial
( M(..)
Expand All @@ -8,18 +10,19 @@ module ZkFold.Base.Algebra.Polynomials.Multivariate.Monomial
, Variable
) where

import Data.Aeson (FromJSON, ToJSON)
import Data.List (intercalate)
import Data.Map (Map, toList, unionWith, differenceWith, empty, fromListWith)
import qualified Data.Map as Map
import Numeric.Natural (Natural)
import GHC.Generics (Generic)
import Prelude hiding (Num(..), (/), (!!), lcm, length, sum, take, drop)
import Test.QuickCheck (Arbitrary (..))
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON, ToJSON)
import Data.List (intercalate)
import Data.Map.Strict (Map, differenceWith, empty, toList, unionWith)
import qualified Data.Map.Strict as Map
import GHC.Generics (Generic)
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), drop, lcm, length, sum, take, (!!), (/))
import Test.QuickCheck (Arbitrary (..))

import ZkFold.Base.Data.Vector
import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.Number
import ZkFold.Base.Data.Vector

type Variable i = Ord i

Expand All @@ -32,7 +35,7 @@ instance Monomial i j => FromMonomial i j (Map i j) where
fromMonomial = id

instance Monomial i Bool => FromMonomial i Bool (Vector d (i, Bool)) where
fromMonomial v = fromListWith (+) $ map (\(i, _) -> (i, one)) $ filter snd $ fromVector v
fromMonomial v = Map.fromListWith (+) $ map (\(i, _) -> (i, one)) $ filter snd $ fromVector v

class Monomial i j => ToMonomial i j m where
toMonomial :: Map i j -> Maybe m
Expand All @@ -47,7 +50,7 @@ instance (Monomial i j, Integral j, KnownNat d) => ToMonomial i j (Vector d (i,

-- | Monomial type
newtype M i j m = M m
deriving (Generic, FromJSON, ToJSON)
deriving (Generic, NFData, FromJSON, ToJSON)

instance (Show i, Show j, FromMonomial i j m) => Show (M i j m) where
show (M m) = intercalate "∙" (map showVar (toList $ fromMonomial @i @j @m m))
Expand Down
28 changes: 16 additions & 12 deletions src/ZkFold/Base/Algebra/Polynomials/Multivariate/Polynomial.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NoGeneralisedNewtypeDeriving #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeOperators #-}

module ZkFold.Base.Algebra.Polynomials.Multivariate.Polynomial
( P(..)
Expand All @@ -8,15 +10,17 @@ module ZkFold.Base.Algebra.Polynomials.Multivariate.Polynomial
, ToPolynomial(..)
) where

import Data.Aeson (FromJSON, ToJSON)
import Data.Functor ((<&>))
import Data.Bifunctor (Bifunctor(..))
import Data.List (intercalate, foldl')
import Data.Map (Map, empty)
import Numeric.Natural (Natural)
import GHC.Generics (Generic)
import Prelude hiding (Num(..), (/), (!!), lcm, length, sum, take, drop)
import Test.QuickCheck (Arbitrary (..))
import Control.DeepSeq (NFData)
import Data.Aeson (FromJSON, ToJSON)
import Data.Bifunctor (Bifunctor (..))
import Data.Functor ((<&>))
import Data.List (foldl', intercalate)
import Data.Map.Strict (Map, empty)
import GHC.Generics (Generic)
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), drop, lcm, length, sum, take,
(!!), (/))
import Test.QuickCheck (Arbitrary (..))

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Polynomials.Multivariate.Monomial
Expand All @@ -41,7 +45,7 @@ instance (Polynomial c i j) => ToPolynomial c i j m [(c, M i j m)] where

-- | Polynomial type
newtype P c i j m p = P p
deriving (Generic, FromJSON, ToJSON)
deriving (Generic, NFData, FromJSON, ToJSON)

instance (Show c, Show i, Show j, FromPolynomial c i j m p, FromMonomial i j m) => Show (P c i j m p) where
show (P p) = intercalate " + "
Expand Down
3 changes: 2 additions & 1 deletion src/ZkFold/Base/Algebra/Polynomials/Multivariate/Set.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module ZkFold.Base.Algebra.Polynomials.Multivariate.Set where

import Data.Map (Map)
import Prelude hiding (Num (..), length, product, replicate, sum, (!!), (^))
import Prelude hiding (Num (..), length, product, replicate,
sum, (!!), (^))

import ZkFold.Base.Algebra.Polynomials.Multivariate.Monomial
import ZkFold.Base.Algebra.Polynomials.Multivariate.Polynomial
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
module ZkFold.Base.Algebra.Polynomials.Multivariate.Substitution where

import Data.Map (Map, lookup)
import Prelude hiding (Num (..), length, lookup, product, replicate, sum, (!!), (^))
import Prelude hiding (Num (..), length, lookup, product,
replicate, sum, (!!), (^))

import ZkFold.Base.Algebra.Basic.Field (Zp, fromZp)
import ZkFold.Base.Algebra.Polynomials.Multivariate.Monomial
Expand Down
31 changes: 17 additions & 14 deletions src/ZkFold/Base/Algebra/Polynomials/Univariate.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE NoGeneralisedNewtypeDeriving #-}
{-# LANGUAGE TypeApplications #-}

module ZkFold.Base.Algebra.Polynomials.Univariate
( toPoly
Expand Down Expand Up @@ -35,27 +37,25 @@ module ZkFold.Base.Algebra.Polynomials.Univariate
, mulPolyNaive
) where

import GHC.Generics
import Control.DeepSeq (NFData (..))
import qualified Data.Vector as V
import GHC.Generics (Generic)
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), drop, length, product, replicate, sum, take, (/), (^))
import Prelude hiding (Num (..), drop, length, product, replicate, sum, take, (/),
(^))
import qualified Prelude as P
import Test.QuickCheck (Arbitrary (..), chooseInt)

import ZkFold.Prelude (zipWithDefault)
import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.DFT (genericDft)
import ZkFold.Base.Algebra.Basic.Number
import ZkFold.Prelude (zipWithDefault)

-------------------------------- Arbitrary degree polynomials --------------------------------

-- TODO (Issue #17): hide constructor
newtype Poly c = P (V.Vector c)
deriving (Eq, Show, Functor)

deriving instance Generic (Poly c)
deriving instance (NFData c) => NFData (Poly c)
deriving (Eq, Show, Functor, Generic, NFData)

toPoly :: (Ring c, Eq c) => V.Vector c -> Poly c
toPoly = removeZeros . P
Expand Down Expand Up @@ -249,7 +249,7 @@ eea a b = go (a, one) (b, zero)

-- TODO (Issue #17): hide constructor
newtype PolyVec c (size :: Natural) = PV (V.Vector c)
deriving (Eq, Show)
deriving (Eq, Show, Generic, NFData)

toPolyVec :: forall c size . (Ring c, KnownNat size) => V.Vector c -> PolyVec c size
toPolyVec = PV . V.take (fromIntegral (value @size)) . addZeros @c @size
Expand Down Expand Up @@ -360,15 +360,18 @@ removeZeros (P cs)
addZeros :: forall c size . (Ring c, KnownNat size) => V.Vector c -> V.Vector c
addZeros cs = cs V.++ V.replicate (fromIntegral (value @size) P.- V.length cs) zero


-- ** THE CODE BELOW IS ONLY USED FOR BENCHMARKING MULTIPLICATION **

-- | Naive vector multiplication, O(n^2)
--
mulPoly :: forall a. Field a => Poly a -> Poly a -> Poly a
mulPoly (P v1) (P v2) = P $ mulVector v1 v2

-- | Adaptation of Karatsuba's algorithm. O(n^log_2(3))
--
mulPolyKaratsuba :: Field a => Poly a -> Poly a -> Poly a
mulPolyKaratsuba (P v1) (P v2) = P result
mulPolyKaratsuba :: (Eq a, Field a) => Poly a -> Poly a -> Poly a
mulPolyKaratsuba (P v1) (P v2) = removeZeros $ P result
where
l = max (V.length v1) (V.length v2)
p = ceiling @Double @Integer $ logBase 2 (fromIntegral l)
Expand All @@ -381,8 +384,8 @@ mulPolyKaratsuba (P v1) (P v2) = P result

-- DFT multiplication of vectors. O(nlogn)
--
mulPolyDft :: forall a . Field a => Poly a -> Poly a -> Poly a
mulPolyDft (P v1) (P v2) = P result
mulPolyDft :: forall a . (Eq a, Field a) => Poly a -> Poly a -> Poly a
mulPolyDft (P v1) (P v2) = removeZeros $ P result
where
l = max (V.length v1) (V.length v2)
p = (ceiling @Double $ logBase 2 (fromIntegral l)) P.+ 1
Expand Down
3 changes: 2 additions & 1 deletion src/ZkFold/Base/Protocol/ARK/Plonk.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import Data.Map (Map, elems, single
import qualified Data.Map as Map
import qualified Data.Vector as V
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), drop, length, replicate, sum, take, (!!), (/), (^))
import Prelude hiding (Num (..), drop, length, replicate, sum, take, (!!),
(/), (^))
import qualified Prelude as P
import Test.QuickCheck (Arbitrary (..))

Expand Down
3 changes: 2 additions & 1 deletion src/ZkFold/Base/Protocol/ARK/Protostar/FiatShamir.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import ZkFold.Base.Data.ByteString (FromByteString
import ZkFold.Base.Protocol.ARK.Protostar.CommitOpen
import qualified ZkFold.Base.Protocol.ARK.Protostar.SpecialSound as SpS
import ZkFold.Base.Protocol.ARK.Protostar.SpecialSound (SpecialSoundProtocol (..), SpecialSoundTranscript)
import ZkFold.Base.Protocol.NonInteractiveProof (NonInteractiveProof (..), ToTranscript (..), challenge)
import ZkFold.Base.Protocol.NonInteractiveProof (NonInteractiveProof (..), ToTranscript (..),
challenge)

data FiatShamir f a = FiatShamir a (SpS.Input f a)

Expand Down
3 changes: 2 additions & 1 deletion src/ZkFold/Base/Protocol/ARK/Protostar/Lookup.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import Data.Map (fromList, mapW
import Data.These (These (..))
import Data.Zip
import Numeric.Natural (Natural)
import Prelude hiding (Num (..), repeat, sum, zip, zipWith, (!!), (/), (^))
import Prelude hiding (Num (..), repeat, sum, zip, zipWith, (!!), (/),
(^))

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.Basic.Field (Zp)
Expand Down
Loading
Loading