1 {-# LANGUAGE OverloadedStrings #-}
4 import Control.DeepSeq (NFData)
5 import qualified Data.List as List
6 import qualified Data.Text as Text
7 import qualified Text.Printf as Printf
8 import qualified Data.Aeson as JSON
10 import Voting.Protocol
16 CryptoParams crypto c =>
19 Int -> Int -> Election crypto v c
20 makeElection nQuests nChoices = elec
22 election_uuid = UUID "xLcs7ev6Jy6FHH"
24 { election_name = Text.pack $ "elec"<>show nQuests<>show nChoices
25 , election_description = "benchmarkable election"
27 , election_crypto = reflect (Proxy @c)
28 , election_public_key =
29 let secKey = credentialSecretKey election_uuid (Credential "xLcs7ev6Jy6FHHE") in
31 , election_hash = hashElection elec
32 , election_version = Just (reflect (Proxy @v))
33 , election_questions =
34 (<$> [1..nQuests]) $ \quest -> Question
35 { question_text = Text.pack $ "quest"<>show quest
36 , question_choices = (<$> [1..nChoices]) $ \choice -> Text.pack $ "choice"<>show choice
38 , question_maxi = one -- sum $ List.replicate nChoices one
42 makeVotes :: Election crypto v c -> [[Bool]]
43 makeVotes Election{..} =
44 [ True : List.tail [ False | _choice <- question_choices quest ]
45 | quest <- election_questions
50 CryptoParams crypto c => Key crypto =>
51 Election crypto v c -> Ballot crypto v c
53 case runExcept $ (`evalStateT` mkStdGen seed) $ do
54 ballotSecKey <- randomSecretKey
55 encryptBallot elec (Just ballotSecKey) $
57 Right ballot -> ballot
58 Left err -> error ("encryptBallot: "<>show err)
62 titleElection :: Election crypto v c -> String
63 titleElection Election{..} =
64 Printf.printf "(questions=%i)×(choices=%i)==%i"
65 nQuests nChoices (nQuests * nChoices)
67 nQuests = List.length election_questions
68 nChoices = List.foldr max 0 $ List.length . question_choices <$> election_questions
72 CryptoParams crypto c =>
77 Proxy v -> Proxy c -> Int -> Int -> Benchmark
78 benchEncryptBallot _v _c nQuests nChoices =
80 let elec :: Election crypto v c = makeElection nQuests nChoices
82 env setupEnv $ \ ~(elec) ->
83 bench (titleElection elec) $
89 CryptoParams crypto c =>
93 Proxy v -> Proxy c -> Int -> Int -> Benchmark
94 benchVerifyBallot (_v::Proxy v) (_c::Proxy c) nQuests nChoices =
96 let elec :: Election crypto v c = makeElection nQuests nChoices
97 let ballot = makeBallot elec
98 return (elec,ballot) in
99 env setupEnv $ \ ~(elec, ballot) ->
100 bench (titleElection elec) $
101 nf (verifyBallot elec) ballot
103 benchmarks :: [Benchmark]
105 [ benchsByVersion stableVersion
106 -- , benchsByVersion experimentalVersion
109 benchsByVersion :: Version -> Benchmark
110 benchsByVersion version =
111 reify version $ \v ->
112 bgroup ("v"<>show version)
113 [ benchsByCrypto v weakFFC
114 , benchsByCrypto v beleniosFFC
119 ReifyCrypto crypto =>
121 JSON.ToJSON crypto =>
123 Proxy v -> crypto -> Benchmark
124 benchsByCrypto v crypto =
125 reifyCrypto crypto $ \c ->
126 bgroup (Text.unpack (cryptoName crypto))
127 [ bgroup "encryptBallot"
128 [ benchEncryptBallot v c nQuests nChoices
129 | (nQuests,nChoices) <- inputs
131 , bgroup "verifyBallot"
132 [ benchVerifyBallot v c nQuests nChoices
133 | (nQuests,nChoices) <- inputs
139 | nQ <- [1,5,10,15,20,25]