shrub/pkg/king/test/NounConversionTests.hs
Elliot Glaysher 676a8ee6a6 WIP: Use edwards curve point types from Crypto.ECC.Edwards25519
This changes Pass to decode the Ed.Point directly in the type,
instead of having the packed Atom representation or raw ByteStrings.
Added conversion quickchecks and also decoded data off the Ethereum
contracts.
2019-09-24 14:01:39 -07:00

59 lines
1.6 KiB
Haskell

module NounConversionTests (tests) where
import Arvo.Event
import Noun.Conversions
import UrbitPrelude
import Crypto.Random.Types
import Test.QuickCheck hiding ((.&.))
import Test.QuickCheck.Gen
import Test.QuickCheck.Random
import Test.Tasty
import Test.Tasty.QuickCheck
import Test.Tasty.TH
import qualified Crypto.ECC.Edwards25519 as Ed
import qualified Crypto.Error as Ed
import qualified Data.ByteArray as BA
-- String Representations of Atoms ---------------------------------------------
instance Arbitrary UV where
arbitrary = UV <$> arbitrarySizedNatural
instance Arbitrary UW where
arbitrary = UW <$> arbitrarySizedNatural
vRoundTrip :: UV -> Bool
vRoundTrip uv = Just uv == (fromNoun $ toNoun $ uv)
wRoundTrip :: UW -> Bool
wRoundTrip uw = Just uw == (fromNoun $ toNoun uw)
-- Cryptographic Point Representations -----------------------------------------
instance Crypto.Random.Types.MonadRandom Gen where
getRandomBytes size = BA.pack <$> vector size
instance Arbitrary Ed.Point where
arbitrary = Ed.toPoint <$> Ed.scalarGenerate
instance Arbitrary Ed.Scalar where
arbitrary = Ed.scalarGenerate
passRoundTrip :: Ed.Point -> Ed.Point -> Bool
passRoundTrip crypt sign =
Just val == (fromNoun $ toNoun val)
where val = (Pass2 crypt sign)
--------------------------------------------------------------------------------
tests :: TestTree
tests =
testGroup "Noun"
[ testProperty "0v0 printing/parsing round trip" $ vRoundTrip
, testProperty "0w0 printing/parsing round trip" $ wRoundTrip
, testProperty "Pass round trip" $ passRoundTrip
]