hjugement-protocol 0.0.0.20190519 → 0.0.1.20190623
raw patch · 5 files changed
+58/−37 lines, 5 filesPVP ok
version bump matches the API change (PVP)
API changes (from Hackage documentation)
Files
- hjugement-protocol.cabal +1/−1
- src/Voting/Protocol/Credential.hs +11/−14
- src/Voting/Protocol/FFC.hs +35/−7
- tests/HUnit/Credential.hs +7/−11
- tests/HUnit/FFC.hs +4/−4
hjugement-protocol.cabal view
@@ -2,7 +2,7 @@ -- PVP: +-+------- breaking API changes -- | | +----- non-breaking API additions -- | | | +--- code changes with no API change-version: 0.0.0.20190519+version: 0.0.1.20190623 category: Politic synopsis: A cryptographic protocol for the Majority Judgment. description:
src/Voting/Protocol/Credential.hs view
@@ -144,20 +144,17 @@ -- using 'Crypto.fastPBKDF2_SHA256'. credentialSecretKey :: Reifies c FFC => UUID -> Credential -> (SecretKey c) credentialSecretKey (UUID uuid) (Credential cred) =- fromNatural $ BS.foldl'- (\acc b -> acc`shiftL`3 + fromIntegral b)- (0::Natural)- (ByteArray.convert deriv)- where- deriv :: BS.ByteString- deriv =- Crypto.fastPBKDF2_SHA256- Crypto.Parameters- { Crypto.iterCounts = 1000- , Crypto.outputLength = 256 `div` 8- }- (Text.encodeUtf8 cred)- (Text.encodeUtf8 uuid)+ fromNatural $+ BS.foldl' -- NOTE: interpret the SHA256 as a big-endian number.+ (\acc b -> acc`shiftL`8 + fromIntegral b)+ (0::Natural) $+ Crypto.fastPBKDF2_SHA256+ Crypto.Parameters+ { Crypto.iterCounts = 1000+ , Crypto.outputLength = 32 -- bytes, ie. 256 bits+ }+ (Text.encodeUtf8 cred)+ (Text.encodeUtf8 uuid) -- ** Type 'PublicKey' type PublicKey = G
src/Voting/Protocol/FFC.hs view
@@ -24,6 +24,7 @@ import Data.Aeson (ToJSON(..),FromJSON(..),(.:),(.:?),(.=)) import Data.Bits import Data.Bool+import Data.Either (Either(..)) import Data.Eq (Eq(..)) import Data.Foldable (Foldable, foldl') import Data.Function (($), (.), id)@@ -40,7 +41,7 @@ import GHC.Natural (minusNaturalMaybe) import Numeric.Natural (Natural) import Prelude (Integer, Integral(..), fromIntegral, Enum(..))-import Text.Read (readMaybe)+import Text.Read (readMaybe, readEither) import Text.Show (Show(..)) import qualified Control.Monad.Trans.State.Strict as S import qualified Crypto.Hash as Crypto@@ -86,15 +87,38 @@ -- WARNING: 'ffc_groupOrder' MUST be a prime number dividing @('ffc_fieldCharac'-1)@ -- to ensure that ElGamal is secure in terms of the DDH assumption. } deriving (Eq,Show,Generic,NFData)-deriving instance ToJSON FFC+instance ToJSON FFC where+ toJSON FFC{..} =+ JSON.object+ [ "name" .= ffc_name+ , "p" .= show ffc_fieldCharac+ , "g" .= show ffc_groupGen+ , "q" .= show ffc_groupOrder+ ]+ toEncoding FFC{..} =+ JSON.pairs+ ( "name" .= ffc_name+ <> "p" .= show ffc_fieldCharac+ <> "g" .= show ffc_groupGen+ <> "q" .= show ffc_groupOrder+ ) instance FromJSON FFC where parseJSON = JSON.withObject "FFC" $ \o -> do ffc_name <- fromMaybe "" <$> (o .:? "name")- ffc_fieldCharac <- o .: "p"- ffc_groupGen <- o .: "g"- ffc_groupOrder <- o .: "q"+ p <- o .: "p"+ g <- o .: "g"+ q <- o .: "q" -- TODO: check p is probable prime -- TODO: check q is probable prime+ ffc_fieldCharac <- case readEither (Text.unpack p) of+ Left err -> JSON.typeMismatch ("FFC: fieldCharac: "<>err) (JSON.String p)+ Right a -> return a+ ffc_groupGen <- case readEither (Text.unpack g) of+ Left err -> JSON.typeMismatch ("FFC: groupGen: "<>err) (JSON.String g)+ Right a -> return a+ ffc_groupOrder <- case readEither (Text.unpack q) of+ Left err -> JSON.typeMismatch ("FFC: groupOrder: "<>err) (JSON.String q)+ Right a -> return a unless (nat ffc_groupGen < ffc_fieldCharac) $ JSON.typeMismatch "FFC: groupGen is not lower than fieldCharac" (JSON.Object o) unless (ffc_groupOrder < ffc_fieldCharac) $@@ -306,8 +330,12 @@ hash :: Reifies c FFC => BS.ByteString -> [G c] -> E c hash bs gs = do let s = bs <> BS.intercalate (fromString ",") (bytesNat <$> gs)- let h = ByteArray.convert (Crypto.hashWith Crypto.SHA256 s)- fromNatural (BS.foldl' (\acc b -> acc`shiftL`3 + fromIntegral b) (0::Natural) h)+ let h = Crypto.hashWith Crypto.SHA256 s+ fromNatural $+ BS.foldl' -- NOTE: interpret the SHA256 as a big-endian number.+ (\acc b -> acc`shiftL`8 + fromIntegral b)+ (0::Natural)+ (ByteArray.convert h) -- * Type 'E' -- | An exponent of a (necessarily cyclic) subgroup of a Finite Prime Field.
tests/HUnit/Credential.hs view
@@ -1,7 +1,6 @@ {-# LANGUAGE OverloadedStrings #-} module HUnit.Credential where -import Control.Applicative (Applicative(..)) import Test.Tasty.HUnit import qualified Control.Monad.Trans.State.Strict as S import qualified System.Random as Random@@ -36,18 +35,15 @@ , "xLcs7ev6Jy6FHHE" ==> Right (Credential "xLcs7ev6Jy6FHHE") ] , testGroup "credentialSecretKey" $- [ testSecretKey weakFFC 0 122- , testSecretKey weakFFC 1 35- , testSecretKey beleniosFFC 0 2317630607062989137269685509390- , testSecretKey beleniosFFC 1 1968146140481358915910346867611+ [ testSecretKey beleniosFFC+ (UUID "xLcs7ev6Jy6FHH")+ (Credential "xLcs7ev6Jy6FHHE")+ 24202898752499029126606335829564687069186982035759723128887013101942425902424 ] ] -testSecretKey :: FFC -> Int -> Natural -> TestTree-testSecretKey ffc seed exp =+testSecretKey :: FFC -> UUID -> Credential -> Natural -> TestTree+testSecretKey ffc uuid cred exp = reify ffc $ \(Proxy::Proxy c) ->- let (uuid@(UUID u), cred@(Credential c)) =- (`S.evalState` Random.mkStdGen seed) $- (,) <$> randomUUID <*> randomCredential in- testCase (show (u,c)) $+ testCase (show (uuid,cred)) $ credentialSecretKey @c uuid cred @?= E exp
tests/HUnit/FFC.hs view
@@ -28,19 +28,19 @@ reify weakFFC $ \(Proxy::Proxy c) -> [ testCase "[groupGen]" $ hash "start" [groupGen @c] @?=- fromNatural 100+ fromNatural 62 , testCase "[groupGen, groupGen]" $ hash "start" [groupGen @c, groupGen] @?=- fromNatural 16+ fromNatural 31 ] , testGroup "BeleniosParams" $ reify beleniosFFC $ \(Proxy::Proxy c) -> [ testCase "[groupGen]" $ hash "start" [groupGen @c] @?=- fromNatural 1832875488615060263192702367259+ fromNatural 75778590284190557660612328423573274641033882642784670156837892421285248292707 , testCase "[groupGen, groupGen]" $ hash "start" [groupGen @c, groupGen] @?=- fromNatural 2495277906542783643199702546512+ fromNatural 28798937720387703653439047952832768487958170248947132321730024269734141660223 ] ] ]