Skip to content

Commit

Permalink
Merge pull request #49 from vks4git/main
Browse files Browse the repository at this point in the history
Fixed bug with KZG test; Added -Werror flag and fixed warnings.
  • Loading branch information
vlasin authored Mar 13, 2024
2 parents adb0854 + cbd6c67 commit 73ae354
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 26 deletions.
8 changes: 4 additions & 4 deletions src/ZkFold/Base/Algebra/Polynomials/Multivariate/Monomial.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ import ZkFold.Base.Algebra.Polynomials.Multivariate.Monomial.Class
newtype M i j m = M m
deriving (Generic, FromJSON, ToJSON)

instance (Show i, Show j, Monomial i j, FromMonomial i j m) => Show (M i j m) where
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))
where
showVar :: (i, j) -> String
showVar (i, j) = "x" ++ show i ++ (if j == one then "" else "^" ++ show j)

instance (Monomial i j, FromMonomial i j m) => (Eq (M i j m)) where
instance (FromMonomial i j m) => (Eq (M i j m)) where
(M asl) == (M asr) = fromMonomial @i @j @m asl == fromMonomial @i @j @m asr

instance (Monomial i j, FromMonomial i j m) => Ord (M i j m) where
instance (FromMonomial i j m) => Ord (M i j m) where
compare (M asl) (M asr) = go (toList $ fromMonomial @i @j @m asl) (toList $ fromMonomial @i @j @m asr)
where
go [] [] = EQ
Expand All @@ -52,4 +52,4 @@ instance Monomial i j => MultiplicativeGroup (M i j (Map i j)) where
invert (M m) = M $ Map.map negate $ fromMonomial @i @j m

(M l) / (M r) = M $ differenceWith f (fromMonomial @i @j l) (fromMonomial @i @j r)
where f a b = if a == b then Nothing else Just (a - b)
where f a b = if a == b then Nothing else Just (a - b)
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ import ZkFold.Base.Algebra.Polynomials.Multivariate.Polynomial.Class
newtype P c i j m p = P p
deriving (Generic, FromJSON, ToJSON)

instance (Show c, Show i, Show j, Polynomial c i j, FromPolynomial c i j m p, FromMonomial i j m) => Show (P c i j m p) where
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 " + " $ map showMono (fromPolynomial @c @i @j p)
where
showMono :: (c, M i j m) -> String
showMono (c, m) = show c <> "" <> show m

instance (Polynomial c i j, FromPolynomial c i j m p, FromMonomial i j m) => Eq (P c i j m p) where
instance (FromPolynomial c i j m p, FromMonomial i j m) => Eq (P c i j m p) where
(P l) == (P r) = fromPolynomial @c @i @j @m l == fromPolynomial r

-- TODO: this assumes sorted monomials! Needs fixing.
instance (Polynomial c i j, FromPolynomial c i j m p, FromMonomial i j m) => Ord (P c i j m p) where
instance (FromPolynomial c i j m p, FromMonomial i j m) => Ord (P c i j m p) where
compare (P l) (P r) = compare (map snd $ fromPolynomial @c @i @j @m l) (map snd $ fromPolynomial @c @i @j @m r)

instance Arbitrary p => Arbitrary (P c i j m p) where
Expand Down Expand Up @@ -73,4 +73,4 @@ instance forall c i j m p . (Polynomial c i j, m ~ Map i j, p ~ [(c, M i j m)])
one = P [(one, M empty)]

instance forall c i j m p . (Polynomial c i j, m ~ Map i j, p ~ [(c, M i j m)]) => Scale (P c i j m p) c where
scale c (P p) = P $ map (first (*c)) p
scale c (P p) = P $ map (first (*c)) p
5 changes: 2 additions & 3 deletions src/ZkFold/Base/Protocol/ARK/Protostar/CommitOpen.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import Prelude hiding (length)
import ZkFold.Base.Data.ByteString (ToByteString(..))
import ZkFold.Base.Protocol.ARK.Protostar.SpecialSound (SpecialSoundProtocol(..), SpecialSoundTranscript)
import ZkFold.Prelude (length)
import ZkFold.Symbolic.Compiler (Arithmetic)

data CommitOpen f c a = CommitOpen (ProverMessage f a -> c) a

Expand All @@ -16,7 +15,7 @@ instance ToByteString c => ToByteString (CommitOpenProverMessage t c a) where
toByteString (Commit c) = toByteString c
toByteString _ = mempty

instance (Arithmetic f, SpecialSoundProtocol f a, Eq c) => SpecialSoundProtocol f (CommitOpen f c a) where
instance (SpecialSoundProtocol f a, Eq c) => SpecialSoundProtocol f (CommitOpen f c a) where
type Witness f (CommitOpen f c a) = (Witness f a, [ProverMessage f a])
type Input f (CommitOpen f c a) = Input f a
type ProverMessage t (CommitOpen f c a) = CommitOpenProverMessage t c a
Expand Down Expand Up @@ -58,4 +57,4 @@ opening a'@(CommitOpen _ a) w i challenge =
m = prover @f a w i tsA
c = prover a' (w, ms) i ts
in (ms ++ [m], ts ++ [(c, challenge ts c)])
in foldl f ([], []) [1 .. rounds @f a]
in foldl f ([], []) [1 .. rounds @f a]
31 changes: 16 additions & 15 deletions src/ZkFold/Base/Protocol/Commitment/KZG.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@

module ZkFold.Base.Protocol.Commitment.KZG where

import Data.ByteString (ByteString, empty)
import Data.Map (Map, (!), insert, toList, keys, fromList)
import Data.Kind (Type)
import Prelude hiding (Num(..), (^), (/), sum, length)
import Test.QuickCheck (Arbitrary (..), chooseInteger)
import Control.Monad (replicateM)
import Data.ByteString (ByteString, empty)
import Data.Kind (Type)
import Data.Map.Strict (Map, fromList, insert, keys, toList, (!))
import Prelude hiding (Num (..), length, sum, (/), (^))
import Test.QuickCheck (Arbitrary (..), chooseInt)

import ZkFold.Base.Algebra.Basic.Class
import ZkFold.Base.Algebra.EllipticCurve.Class
import ZkFold.Base.Algebra.Polynomials.Univariate
import ZkFold.Base.Data.ByteString (ToByteString, FromByteString)
import ZkFold.Base.Data.ByteString (FromByteString, ToByteString)
import ZkFold.Base.Protocol.NonInteractiveProof
import ZkFold.Prelude (length)
import ZkFold.Prelude (length)

newtype KZG c1 c2 t f d = KZG f
deriving (Show, Eq, Arbitrary)
Expand All @@ -23,22 +24,22 @@ newtype KZG c1 c2 t f d = KZG f
instance Finite d => Finite (KZG c1 c2 t f d) where
order = order @d

newtype WitnessKZG c1 c2 t f d = WitnessKZG (Map f [PolyVec f (KZG c1 c2 t f d)])
newtype WitnessKZG c1 c2 t f d = WitnessKZG { runWitness :: Map f [PolyVec f (KZG c1 c2 t f d)] }
instance (EllipticCurve c1, f ~ ScalarField c1) => Show (WitnessKZG c1 c2 t f d) where
show (WitnessKZG w) = "WitnessKZG " <> show w
instance (EllipticCurve c1, f ~ ScalarField c1, Finite d) => Arbitrary (WitnessKZG c1 c2 t f d) where
arbitrary = do
n <- chooseInteger (1, 3)
m <- chooseInteger (1, 5)
WitnessKZG . fromList <$> mapM (const $ (,) <$> arbitrary <*> mapM (const arbitrary) [1..m]) [1..n]
n <- chooseInt (1, 3)
m <- chooseInt (1, 5)
WitnessKZG . fromList <$> replicateM n ((,) <$> arbitrary <*> replicateM m arbitrary)

-- TODO (Issue #18): check list lengths
instance forall (c1 :: Type) (c2 :: Type) t f d kzg . (EllipticCurve c1, f ~ ScalarField c1, EllipticCurve c2, f ~ ScalarField c2,
instance forall (c1 :: Type) (c2 :: Type) t f d kzg . (f ~ ScalarField c1, f ~ ScalarField c2,
Pairing c1 c2 t, ToByteString f, FromByteString f, Finite d, KZG c1 c2 t f d ~ kzg)
=> NonInteractiveProof (KZG c1 c2 t f d) where
type Transcript (KZG c1 c2 t f d) = ByteString
type Setup (KZG c1 c2 t f d) = ([Point c1], Point c2, Point c2)
type Witness (KZG c1 c2 t f d) = Map f [PolyVec f (KZG c1 c2 t f d)]
type Witness (KZG c1 c2 t f d) = WitnessKZG c1 c2 t f d
type Input (KZG c1 c2 t f d) = Map f ([Point c1], [f])
type Proof (KZG c1 c2 t f d) = Map f (Point c1)

Expand All @@ -52,7 +53,7 @@ instance forall (c1 :: Type) (c2 :: Type) t f d kzg . (EllipticCurve c1, f ~ Sca
prove :: Setup kzg
-> Witness kzg
-> (Input kzg, Proof kzg)
prove (gs, _, _) w = snd $ foldl proveOne (empty, (mempty, mempty)) (toList w)
prove (gs, _, _) (WitnessKZG w) = snd $ foldl proveOne (empty, (mempty, mempty)) (toList w)
where
proveOne :: (Transcript kzg, (Input kzg, Proof kzg))
-> (f, [PolyVec f kzg])
Expand Down Expand Up @@ -102,4 +103,4 @@ provePolyVecEval :: forall size f . (Finite size, FiniteField f, Eq f) => PolyVe
provePolyVecEval f z = (f - toPolyVec [negate $ f `evalPolyVec` z]) / toPolyVec [negate z, one]

com :: (EllipticCurve curve, f ~ ScalarField curve) => [Point curve] -> PolyVec f size -> Point curve
com gs f = sum $ zipWith mul (fromPolyVec f) gs
com gs f = sum $ zipWith mul (fromPolyVec f) gs
2 changes: 2 additions & 0 deletions zkfold-base.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ common options
ghc-options:
-fwarn-redundant-constraints
-Wall
-Werror
-Wcompat
-Widentities
-Wincomplete-record-updates
-Wincomplete-uni-patterns
-Wnoncanonical-monad-instances
-Wpartial-fields
-Wredundant-constraints
-O2
default-extensions:
BangPatterns,
BinaryLiterals,
Expand Down

0 comments on commit 73ae354

Please sign in to comment.