-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #359 from zkFold/TurtlePU/UPLC-tests
Simple UPLC Converter tests
- Loading branch information
Showing
4 changed files
with
109 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# LANGUAGE MonoLocalBinds #-} | ||
{-# LANGUAGE TypeOperators #-} | ||
|
||
{-# OPTIONS_GHC -Wno-orphans #-} | ||
|
||
import Control.Applicative ((<*>)) | ||
import Control.Monad (return) | ||
import Data.Eq (Eq) | ||
import Data.Function (const, ($)) | ||
import Data.Functor (Functor, (<$>)) | ||
import GHC.Generics (Par1 (..), U1 (..), (:*:) (..)) | ||
import System.IO (IO) | ||
import Test.Hspec (describe, hspec) | ||
import Test.Hspec.QuickCheck (prop) | ||
import Test.QuickCheck | ||
import Text.Show (Show) | ||
|
||
import ZkFold.Base.Algebra.Basic.Field (Zp) | ||
import ZkFold.Base.Algebra.EllipticCurve.BLS12_381 (BLS12_381_Base) | ||
import ZkFold.Symbolic.Compiler (ArithmeticCircuit, compile) | ||
import ZkFold.Symbolic.Compiler.ArithmeticCircuit (eval) | ||
import ZkFold.Symbolic.Data.Bool (false, true) | ||
import ZkFold.Symbolic.Data.Class (SymbolicData (..)) | ||
import ZkFold.Symbolic.Data.Input (SymbolicInput) | ||
import ZkFold.Symbolic.UPLC.Converter (contractV3) | ||
import ZkFold.UPLC.BuiltinFunction | ||
import ZkFold.UPLC.Term | ||
|
||
areSame :: | ||
( SymbolicData f, Context f ~ c, Support f ~ s, Layout f ~ l | ||
, c ~ ArithmeticCircuit a i, Arbitrary (i a), Show (i a) | ||
, SymbolicInput s, Context s ~ c, Layout s ~ i | ||
, Functor l, Eq (l a), Show (l a) | ||
, a ~ Zp BLS12_381_Base) => | ||
(Term -> f) -> Term -> f -> Property | ||
areSame v t f = | ||
let acT = compile (v t) | ||
acF = compile f | ||
in property $ \i -> eval acT i === eval acF i | ||
|
||
instance (Arbitrary (f a), Arbitrary (g a)) => Arbitrary ((f :*: g) a) where | ||
arbitrary = (:*:) <$> arbitrary <*> arbitrary | ||
|
||
instance Arbitrary (U1 a) where | ||
arbitrary = return U1 | ||
|
||
instance Arbitrary a => Arbitrary (Par1 a) where | ||
arbitrary = Par1 <$> arbitrary | ||
|
||
tFalse, tTrue, tUnit :: Term | ||
tFalse = TConstant (CBool false) | ||
tTrue = TConstant (CBool true) | ||
tUnit = TConstant (CUnit ()) | ||
|
||
infixl 1 $$ | ||
($$) :: Term -> Term -> Term | ||
($$) = TApp | ||
|
||
main :: IO () | ||
main = hspec $ describe "UPLC tests" $ do | ||
prop "false is ok" $ areSame contractV3 (TLam tFalse) (const true) | ||
prop "error is not ok" $ areSame contractV3 (TLam TError) (const false) | ||
prop "substitution is ok" $ | ||
areSame contractV3 (TLam $ TLam (TVariable 0) $$ tTrue) (const true) | ||
prop "pair is ok" $ areSame contractV3 | ||
(TLam $ TBuiltin (BFPoly FstPair) $$ TConstant (CPair (CUnit ()) (CBool false))) | ||
(const true) | ||
prop "bool is not a pair" $ | ||
areSame contractV3 (TLam $ TBuiltin (BFPoly SndPair) $$ tTrue) (const false) | ||
prop "trivial if if ok" $ areSame contractV3 | ||
(TLam $ TBuiltin (BFPoly IfThenElse) $$ tTrue $$ tTrue $$ tFalse) | ||
(const true) | ||
prop "lazy error in if is ok" $ areSame contractV3 | ||
(TLam $ TBuiltin (BFPoly IfThenElse) $$ tTrue $$ tUnit $$ TError) | ||
(const true) | ||
prop "error propagation in if" $ areSame contractV3 | ||
(TLam $ TBuiltin (BFPoly IfThenElse) $$ tFalse $$ tUnit $$ TError) | ||
(const false) | ||
prop "if as an argument is ok" $ areSame contractV3 | ||
(TLam $ TLam (TVariable 0 $$ tTrue $$ tUnit $$ TError) $$ TBuiltin (BFPoly IfThenElse)) | ||
(const true) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters