Commit a98ac43f authored by Stephane Glondu's avatar Stephane Glondu
Browse files

Add log records to BALLOT_BOX signature

parent 04557ba7
......@@ -64,6 +64,7 @@ let prng = lazy (Cryptokit.Random.(pseudo_rng (string secure_rng 16)))
module MakeSimpleMonad (G : GROUP) = struct
type 'a t = unit -> 'a
let ballots = ref []
let records = ref []
let return x () = x
let bind x f = f (x ())
let fail e = raise e
......@@ -75,8 +76,11 @@ module MakeSimpleMonad (G : GROUP) = struct
Z.(of_bits r mod q)
type ballot = G.t Serializable_t.ballot
let cast x () = ballots := x :: !ballots
let fold f x () = List.fold_left (fun accu b -> f b accu ()) x !ballots
type record = string
let cast x r () = ballots := x :: !ballots; records := r :: !records
let fold_ballots f x () = List.fold_left (fun accu b -> f b accu ()) x !ballots
let fold_records f x () = List.fold_left (fun accu b -> f b accu ()) x !records
let turnout () = List.length !ballots
end
(** Distributed key generation *)
......
......@@ -31,11 +31,10 @@ module MakeSimpleMonad (G : GROUP) : sig
(** {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
include Signatures.BALLOT_BOX
with type 'a m := 'a t
and type ballot = G.t Serializable_t.ballot
and type record = string
end
(** Simple election monad that keeps all ballots in memory. *)
......
......@@ -58,7 +58,8 @@ end
(** Ballot box. *)
module type BALLOT_BOX = sig
include MONAD
type 'a m
(** The type of monadic values. *)
(** {2 Election-specific operations} *)
......@@ -66,12 +67,21 @@ module type BALLOT_BOX = sig
(** The type of ballots. The monad is supposed to keep track of all
cast ballots (e.g. in a database). *)
val cast : ballot -> unit t
type record
(** The type of log records. *)
val cast : ballot -> record -> unit m
(** Cast a ballot. *)
val fold : (ballot -> 'a -> 'a t) -> 'a -> 'a t
(** [fold f a] computes [(f bN ... (f b2 (f b1 a))...)], where [b1
... bN] are all cast ballots. *)
val fold_ballots : (ballot -> 'a -> 'a m) -> 'a -> 'a m
(** [fold_ballots f a] computes [(f bN ... (f b2 (f b1 a))...)],
where [b1 ... bN] are all cast ballots. *)
val fold_records : (record -> 'a -> 'a m) -> 'a -> 'a m
(** Same as [fold_ballots] for records. *)
val turnout : int m
(** Number of cast ballots. *)
end
(** Parameters for an election. *)
......
......@@ -187,7 +187,7 @@ let verbose_verify_election_test_data e =
several match blocks below. It is lazy because it must be
computed only after all ballots are cast (i.e. not right
now). *)
M.fold (fun b tally ->
M.fold_ballots (fun b tally ->
M.return (E.combine_ciphertexts tally (E.extract_ciphertext b))
) E.neutral_ciphertext ()
) in
......@@ -199,7 +199,7 @@ let verbose_verify_election_test_data e =
List.for_all (fun x ->
let b = Serializable_compat.ballot x in
if E.check_ballot b then (
M.cast b (); true
M.cast b "anonymous" (); true
) else false
) ballots
);
......
......@@ -81,13 +81,11 @@ module E = Election.MakeElection(P)(M);;
(* Vote *)
let num_cast = ref 0
let vote b =
try
let b = E.create_ballot (E.make_randomness () ()) b () in
let ok = E.check_ballot b in
if ok then (M.cast b (); incr num_cast);
if ok then M.cast b "anonymous" ();
ok
with _ -> false
;;
......@@ -103,7 +101,7 @@ assert (not (vote [|[| 1; 0; 0; 0; 0 |]; [| 0; 1; 0; 1; 0; 0 |]; [| 0; 1; 1 |]|]
(* Tally *)
let encrypted_tally = M.fold (fun b t ->
let encrypted_tally = M.fold_ballots (fun b t ->
M.return (E.combine_ciphertexts (E.extract_ciphertext b) t)
) E.neutral_ciphertext ();;
......@@ -112,7 +110,7 @@ let factors = Array.map (fun x ->
) private_keys;;
assert (Array.forall2 (E.check_factor encrypted_tally) P.public_keys factors);;
let result = E.combine_factors !num_cast encrypted_tally factors;;
let result = E.combine_factors (M.turnout ()) encrypted_tally factors;;
assert (E.check_result result);;
let tally = E.extract_tally result;;
......@@ -143,7 +141,7 @@ let save_to_disk () =
let election = { election with
e_public_key = { g; p; q; y }
} in
let ballots = Array.of_list (M.fold (fun x xs () -> x::xs) [] ()) in
let ballots = Array.of_list (M.fold_ballots (fun x xs () -> x::xs) [] ()) in
let dir = Printf.sprintf "tests/data/{%s}"
(Uuidm.to_string election.e_uuid)
in
......
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