Commit 6801b834 authored by Stephane Glondu's avatar Stephane Glondu
Browse files

Doc + encrypted_tally out of ELECTION

parent 34a72c52
......@@ -80,11 +80,7 @@ end
(** Homomorphic elections *)
module MakeElection
(P : ELECTION_PARAMS)
(M : ELECTION_MONAD with type ballot = P.G.t Serializable_t.ballot)
=
struct
module MakeElection (P : ELECTION_PARAMS) (M : RANDOM) = struct
open P
open G
......@@ -115,6 +111,10 @@ struct
beta = c1.beta *~ c2.beta;
}
let neutral_ciphertext = Array.map (fun q ->
Array.make (Array.length q.q_answers) dummy_ciphertext
) params.e_questions
let combine_ciphertexts = Array.mmap2 eg_combine
type plaintext = int array array
......
......@@ -13,16 +13,31 @@ val check_finite_field : p:Z.t -> q:Z.t -> g:Z.t -> bool
val check_election : (module ELECTION_PARAMS) -> bool
(** Check consistency of election parameters. *)
module MakeSimpleMonad (G : GROUP) : ELECTION_MONAD
with type ballot = G.t Serializable_t.ballot
and type 'a t = unit -> 'a
(** Simple election monad that keeps all ballots in memory. It uses a
secure random number generator lazily initialized by a seed shared
by all instances. *)
module MakeElection
(P : ELECTION_PARAMS)
(M : ELECTION_MONAD with type ballot = P.G.t Serializable_t.ballot)
: ELECTION
with type elt = P.G.t
and type 'a m = 'a M.t
module MakeSimpleMonad (G : GROUP) : sig
(** {2 Monadic definitions} *)
type 'a t = unit -> 'a
val return : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
(** {2 Random number generation} *)
val random : Z.t -> Z.t t
(** [random q] returns a random number modulo [q]. It uses a secure
random number generator lazily initialized by a seed shared by all
instances. *)
(** {2 Ballot box management} *)
(** The following implements the {!module:Signatures.BALLOT_BOX} interface. *)
type ballot = G.t Serializable_t.ballot
val cast : ballot -> unit t
val fold : (ballot -> 'a -> 'a t) -> 'a -> 'a t
end
(** Simple election monad that keeps all ballots in memory. *)
module MakeElection (P : ELECTION_PARAMS) (M : RANDOM) :
ELECTION with type elt = P.G.t and type 'a m = 'a M.t
(** Implementation of {!Signatures.ELECTION}. *)
......@@ -40,18 +40,24 @@ module type GROUP = sig
(** A total ordering over the elements of the group. *)
end
(** Monad capturing impure operations used by elections. *)
module type ELECTION_MONAD = sig
(** {2 Usual monadic operations} *)
(** Monad signature. *)
module type MONAD = sig
type 'a t
val return : 'a -> 'a t
val bind : 'a t -> ('a -> 'b t) -> 'b t
end
(** {2 General-purpose impure operations} *)
(** Random number generation. *)
module type RANDOM = sig
include MONAD
val random : Z.t -> Z.t t
(** [random q] returns a random number modulo [q]. *)
end
(** Ballot box. *)
module type BALLOT_BOX = sig
include MONAD
(** {2 Election-specific operations} *)
......@@ -106,6 +112,9 @@ module type ELECTION = sig
type ciphertext = elt Serializable_t.ciphertext array array
(** A ciphertext that can be homomorphically combined. *)
val neutral_ciphertext : ciphertext
(** The neutral element for [combine_ciphertext] below. *)
val combine_ciphertexts : ciphertext -> ciphertext -> ciphertext
(** Combine two ciphertexts. The encrypted tally of an election is
the combination of all ciphertexts of valid cast ballots. *)
......
......@@ -95,35 +95,43 @@ let verbose_verify_election_test_data (e, ballots, signatures, private_data) =
));
let module M = Election.MakeSimpleMonad(P.G) in
let module E = Election.MakeElection(P)(M) in
let encrypted_tally = lazy (
M.fold (fun b tally ->
M.return (E.combine_ciphertexts tally (E.extract_ciphertext b))
) E.neutral_ciphertext ()
) in
if Array.length ballots = 0 then (
Printf.eprintf " no ballots available\n%!"
) else (
verbose_assert "ballots" (lazy (
Array.foralli (fun _ x ->
E.check_ballot (Serializable_compat.ballot x)
let b = Serializable_compat.ballot x in
if E.check_ballot b then (
M.cast b (); true
) else false
) ballots
));
(*
(match e.public_data.election_result with
| Some r ->
verbose_assert "encrypted tally" (lazy (
r.encrypted_tally = Crypto.compute_encrypted_tally e.election ballots
r.encrypted_tally.tally = Lazy.force encrypted_tally
))
| None -> ()
);
*)
);
(*
(match e.public_data.election_result with
| Some r ->
verbose_assert "partial decryptions" (lazy (
Crypto.check_partial_decryptions
e.election e.public_data.public_keys r
verbose_assert "partial decryptions and result" (lazy (
let result = E.combine_factors
r.encrypted_tally.num_tallied
(Lazy.force encrypted_tally)
(Array.map Serializable_compat.partial_decryption r.partial_decryptions)
in
E.check_result result &&
E.extract_tally result = r.result
));
verbose_assert "result" (lazy (Crypto.check_result e.election r));
| None -> Printf.eprintf " no results available\n%!"
);
*)
verbose_assert "signature count" (lazy (
Array.length signatures = Array.length ballots
));
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment