1 {-# LANGUAGE DataKinds #-}
2 {-# LANGUAGE OverloadedStrings #-}
3 {-# LANGUAGE PatternSynonyms #-}
4 module HUnit.Trustee.Indispensable where
6 import Test.Tasty.HUnit
7 import qualified Data.Text as Text
8 import qualified System.Random as Random
9 import qualified Text.Printf as Printf
11 import Voting.Protocol
16 hunit = testGroup "Indispensable"
17 [ testGroup "verifyIndispensableTrusteePublicKey" $
18 [ testsVerifyIndispensableTrusteePublicKey weakFFC
20 , testGroup "verifyTally" $
21 [ testsVerifyTally weakFFC
22 , testsVerifyTally beleniosFFC
26 testsVerifyIndispensableTrusteePublicKey :: FFC -> TestTree
27 testsVerifyIndispensableTrusteePublicKey ffc =
28 testGroup (Text.unpack $ ffc_name ffc)
29 [ testVerifyIndispensableTrusteePublicKey ffc 0 (Right ())
32 testVerifyIndispensableTrusteePublicKey ::
33 FFC -> Int -> Either ErrorTrusteePublicKey () -> TestTree
34 testVerifyIndispensableTrusteePublicKey ffc seed exp =
36 reify ffc $ \(Proxy::Proxy c) ->
38 (`evalStateT` Random.mkStdGen seed) $ do
39 trusteeSecKey :: SecretKey c <- randomSecretKey
40 trusteePubKey <- proveIndispensableTrusteePublicKey trusteeSecKey
41 lift $ verifyIndispensableTrusteePublicKey trusteePubKey
43 testCase (Text.unpack $ ffc_name ffc) $
46 testsVerifyTally :: FFC -> TestTree
47 testsVerifyTally ffc =
48 testGroup (Text.unpack $ ffc_name ffc)
49 [ testVerifyTally ffc 0 1 1 1
50 , testVerifyTally ffc 0 2 1 1
51 , testVerifyTally ffc 0 1 2 1
52 , testVerifyTally ffc 0 2 2 1
53 , testVerifyTally ffc 0 5 10 5
56 testVerifyTally :: FFC -> Int -> Natural -> Natural -> Natural -> TestTree
57 testVerifyTally ffc seed nTrustees nQuests nChoices =
58 let clearTallyResult = dummyTallyResult nQuests nChoices in
59 let decryptedTallyResult :: Either ErrorTally [[Natural]] =
60 reify ffc $ \(Proxy::Proxy c) ->
62 (`evalStateT` Random.mkStdGen seed) $ do
63 secKeyByTrustee :: [SecretKey c] <-
64 replicateM (fromIntegral nTrustees) $ randomSecretKey
65 trusteePubKeys <- forM secKeyByTrustee $ proveIndispensableTrusteePublicKey
66 let pubKeyByTrustee = trustee_PublicKey <$> trusteePubKeys
67 let elecPubKey = combineIndispensableTrusteePublicKeys trusteePubKeys
68 (encTally, countMax) <- encryptTallyResult elecPubKey clearTallyResult
69 decShareByTrustee <- forM secKeyByTrustee $ proveDecryptionShare encTally
70 lift $ verifyDecryptionShareByTrustee encTally pubKeyByTrustee decShareByTrustee
71 tally@Tally{..} <- lift $
72 proveTally (encTally, countMax) decShareByTrustee $
73 combineIndispensableDecryptionShares pubKeyByTrustee
74 lift $ verifyTally tally $
75 combineIndispensableDecryptionShares pubKeyByTrustee
76 return tally_countByChoiceByQuest
78 testCase (Printf.printf "#T=%i,#Q=%i,#C=%i (%i maxCount)"
79 nTrustees nQuests nChoices
80 (dummyTallyCount nQuests nChoices)) $
81 decryptedTallyResult @?= Right clearTallyResult
83 dummyTallyCount :: Natural -> Natural -> Natural
84 dummyTallyCount quest choice = quest * choice
86 dummyTallyResult :: Natural -> Natural -> [[Natural]]
87 dummyTallyResult nQuests nChoices =
88 [ [ dummyTallyCount q c | c <- [1..nChoices] ]
94 Monad m => RandomGen r =>
95 PublicKey c -> [[Natural]] -> StateT r m (EncryptedTally c, Natural)
96 encryptTallyResult pubKey countByChoiceByQuest =
98 forM countByChoiceByQuest $
101 (_encNonce, enc) <- lift $ encrypt pubKey (fromNatural count)