From aea0ffbf75a67f4fa713d6f37d7d1b5e50a3faec Mon Sep 17 00:00:00 2001 From: Eitan Chatav Date: Mon, 13 Jan 2025 16:05:27 -0800 Subject: [PATCH] testing --- .../Base/Algebra/EllipticCurve/Class.hs | 10 +++++++- symbolic-base/src/ZkFold/Base/Protocol/KZG.hs | 24 ++++++++----------- .../test/Tests/Arithmetization/Test4.hs | 8 +++---- symbolic-base/test/Tests/Binary.hs | 22 ++++++++--------- 4 files changed, 34 insertions(+), 30 deletions(-) diff --git a/symbolic-base/src/ZkFold/Base/Algebra/EllipticCurve/Class.hs b/symbolic-base/src/ZkFold/Base/Algebra/EllipticCurve/Class.hs index 0c1c83c33..c33e14a30 100644 --- a/symbolic-base/src/ZkFold/Base/Algebra/EllipticCurve/Class.hs +++ b/symbolic-base/src/ZkFold/Base/Algebra/EllipticCurve/Class.hs @@ -29,8 +29,9 @@ import Data.Kind (Type) import Data.String (fromString) import GHC.Generics import GHC.TypeLits (Symbol) -import Prelude (Integer, type (~)) +import Prelude (Integer, type (~), ($), (>>=), return) import qualified Prelude +import Test.QuickCheck hiding (scale) import ZkFold.Base.Algebra.Basic.Class import ZkFold.Base.Algebra.Basic.Number @@ -142,6 +143,13 @@ class HasPointInf point where pointInf :: point {- | `Weierstrass` tags a `ProjectivePlanar` @point@, over a `Field` @field@, with a phantom `WeierstrassCurve` @curve@. -} newtype Weierstrass curve point = Weierstrass {pointWeierstrass :: point} +instance + ( Arbitrary (ScalarFieldOf (Weierstrass curve point)) + , CyclicGroup (Weierstrass curve point) + ) => Arbitrary (Weierstrass curve point) where + arbitrary = do + c <- arbitrary @ScalarFieldOf (Weierstrass curve point) + return $ scale c pointGen instance ( WeierstrassCurve curve field , Conditional bool bool diff --git a/symbolic-base/src/ZkFold/Base/Protocol/KZG.hs b/symbolic-base/src/ZkFold/Base/Protocol/KZG.hs index 744e35d43..5a1234fb8 100644 --- a/symbolic-base/src/ZkFold/Base/Protocol/KZG.hs +++ b/symbolic-base/src/ZkFold/Base/Protocol/KZG.hs @@ -8,7 +8,6 @@ module ZkFold.Base.Protocol.KZG where import Control.Monad (replicateM) import Data.ByteString (ByteString, empty) -import Data.Kind (Type) import Data.Map.Strict (Map, fromList, insert, keys, toList, (!)) import qualified Data.Vector as V import Data.Vector.Binary () @@ -35,19 +34,16 @@ instance Arbitrary field => Arbitrary (KZG field g1 g2 d) where newtype WitnessKZG field g1 g2 d = WitnessKZG { runWitness :: Map field (V.Vector (PolyVec field d)) } instance (Show field) => Show (WitnessKZG field g1 g2 d) where show (WitnessKZG w) = "WitnessKZG " <> show w --- instance --- ( SubgroupCurve c1 Bool baseField1 scalarField point --- , SubgroupCurve c2 Bool baseField2 scalarField point --- , KnownNat d --- , Arbitrary scalarField --- , Ord scalarField --- , g1 ~ point baseField1 --- , g2 ~ point baseField2 --- ) => Arbitrary (WitnessKZG scalarField g1 g2 d) where --- arbitrary = do --- n <- chooseInt (1, 3) --- m <- chooseInt (1, 5) --- WitnessKZG . fromList <$> replicateM n ((,) <$> arbitrary <*> (V.fromList <$> replicateM m arbitrary)) +instance + ( KnownNat d + , Arbitrary scalarField + , Ord scalarField + , Ring scalarField + ) => Arbitrary (WitnessKZG scalarField g1 g2 d) where + arbitrary = do + n <- chooseInt (1, 3) + m <- chooseInt (1, 5) + WitnessKZG . fromList <$> replicateM n ((,) <$> arbitrary <*> (V.fromList <$> replicateM m arbitrary)) -- TODO (Issue #18): check list lengths instance forall f g1 g2 gt d kzg core. diff --git a/symbolic-base/test/Tests/Arithmetization/Test4.hs b/symbolic-base/test/Tests/Arithmetization/Test4.hs index a6635631a..a30125a6f 100644 --- a/symbolic-base/test/Tests/Arithmetization/Test4.hs +++ b/symbolic-base/test/Tests/Arithmetization/Test4.hs @@ -11,16 +11,16 @@ import Test.Hspec (Spec, describe, it import Test.QuickCheck (Testable (..), (==>)) import ZkFold.Base.Algebra.Basic.Class (FromConstant (..), one, zero) -import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_G1) -import ZkFold.Base.Algebra.EllipticCurve.Class (EllipticCurve (..)) +import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_G1_Point) +import ZkFold.Base.Algebra.EllipticCurve.Class (CyclicGroup (..)) import ZkFold.Symbolic.Class import ZkFold.Symbolic.Compiler (ArithmeticCircuit (..), compile, eval) import ZkFold.Symbolic.Data.Bool (Bool (..)) import ZkFold.Symbolic.Data.Eq (Eq (..)) import ZkFold.Symbolic.Data.FieldElement (FieldElement) -type C = BLS12_381_G1 -type F = ScalarField C +type C = BLS12_381_G1_Point +type F = ScalarFieldOf C lockedByTxId :: forall a c . (FromConstant a (FieldElement c), Symbolic c) => a -> FieldElement c -> Bool c lockedByTxId targetValue inputValue = inputValue == fromConstant targetValue diff --git a/symbolic-base/test/Tests/Binary.hs b/symbolic-base/test/Tests/Binary.hs index a1775b04e..6d35b0f78 100644 --- a/symbolic-base/test/Tests/Binary.hs +++ b/symbolic-base/test/Tests/Binary.hs @@ -7,10 +7,10 @@ import Test.Hspec.QuickCheck import Test.QuickCheck import ZkFold.Base.Algebra.Basic.Field (Zp) -import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_G1, BLS12_381_G2, BLS12_381_Scalar) -import ZkFold.Base.Algebra.EllipticCurve.BN254 (BN254_G1, BN254_G2) +import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_G1_Point, BLS12_381_G2_Point, BLS12_381_G1_CompressedPoint, BLS12_381_G2_CompressedPoint, BLS12_381_Scalar) +import ZkFold.Base.Algebra.EllipticCurve.BN254 (BN254_G1_Point, BN254_G2_Point) import ZkFold.Base.Algebra.EllipticCurve.Class (CompressedPoint, Point) -import ZkFold.Base.Algebra.EllipticCurve.Pasta (Pallas, Vesta) +import ZkFold.Base.Algebra.EllipticCurve.Pasta (Pallas_Point, Vesta_Point) import ZkFold.Base.Data.ByteString (LittleEndian, fromByteString, toByteString) doesRoundtrip :: (Binary a, Eq a, Show a) => a -> Property @@ -23,11 +23,11 @@ specBinary :: IO () specBinary = hspec $ describe "Binary instance" $ do prop "roundtrips LittleEndian" $ doesRoundtrip @LittleEndian prop "roundtrips Zp BLS12_381_Scalar" $ doesRoundtrip @(Zp BLS12_381_Scalar) - prop "roundtrips Point BN254_G1" $ doesRoundtrip @(Point BN254_G1) - prop "roundtrips Point BN254_G2" $ doesRoundtrip @(Point BN254_G2) - prop "roundtrips Point BLS12_381_G1" $ doesRoundtrip @(Point BLS12_381_G1) - prop "roundtrips CompressedPoint BLS12_381_G1" $ doesRoundtrip @(CompressedPoint BLS12_381_G1) - prop "roundtrips Point BLS12_381_G2" $ doesRoundtrip @(Point BLS12_381_G2) - prop "roundtrips CompressedPoint BLS12_381_G2" $ doesRoundtrip @(CompressedPoint BLS12_381_G2) - prop "roundtrips Point Pallas" $ doesRoundtrip @(Point Pallas) - prop "roundtrips Point Vesta" $ doesRoundtrip @(Point Vesta) + prop "roundtrips Point BN254_G1" $ doesRoundtrip @BN254_G1_Point + prop "roundtrips Point BN254_G2" $ doesRoundtrip @BN254_G2_Point + prop "roundtrips Point BLS12_381_G1" $ doesRoundtrip @BLS12_381_G1_Point + prop "roundtrips CompressedPoint BLS12_381_G1" $ doesRoundtrip @BLS12_381_G1_CompressedPoint + prop "roundtrips Point BLS12_381_G2" $ doesRoundtrip @BLS12_381_G2_Point + prop "roundtrips CompressedPoint BLS12_381_G2" $ doesRoundtrip @BLS12_381_G2_CompressedPoint + prop "roundtrips Point Pallas" $ doesRoundtrip @Pallas_Point + prop "roundtrips Point Vesta" $ doesRoundtrip @Vesta_Point