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

Change randomness interface

Go back to make_randomness, since the module initialization time is
noticeable otherwise.
parent 6be755f1
......@@ -62,19 +62,20 @@ let check_election p =
let prng = lazy (Cryptokit.Random.(pseudo_rng (string secure_rng 32)))
module MakeSimpleMonad (G : GROUP) = struct
type 'a t = 'a
type 'a t = unit -> 'a
let ballots = ref []
let return x = x
let bind x f = f x
let return x () = x
let bind x f = f (x ())
let random q =
let size = Z.size q * Sys.word_size / 8 in
let r = Cryptokit.Random.string (Lazy.force prng) size in
Z.(of_bits r mod q)
fun () ->
let r = Cryptokit.Random.string (Lazy.force prng) size in
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
let cast x () = ballots := x :: !ballots
let fold f x () = List.fold_left (fun accu b -> f b accu ()) x !ballots
end
(** Homomorphic elections *)
......@@ -248,7 +249,7 @@ struct
overall_proof >>= fun overall_proof ->
return {choices; individual_proofs; overall_proof}
let randomness =
let make_randomness () =
sswap (Array.map (fun q ->
Array.init (Array.length q.q_answers) (fun _ -> random G.q)
) params.e_questions)
......
......@@ -15,8 +15,10 @@ val check_election : (module ELECTION_PARAMS) -> bool
module MakeSimpleMonad (G : GROUP) : ELECTION_MONAD
with type ballot = G.t Serializable_t.ballot
and type 'a t = 'a
(** Simple election monad that keeps all ballots in memory. *)
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)
......
......@@ -125,7 +125,7 @@ module type ELECTION = sig
type randomness = Z.t array array
(** Randomness needed to create a ballot. *)
val randomness : randomness m
val make_randomness : unit -> randomness m
(** Creates randomness for [create_ballot] below. The result can be
kept for Benaloh-style auditing. *)
......
......@@ -186,9 +186,9 @@ let nballots = Array.map Serializable_compat.of_ballot ballots;;
assert (Array.forall Election.check_ballot nballots);;
assert (Array.forall2 (fun b b' -> b = Compat.to_ballot b') ballots nballots);;
let create_ballot b = Election.(create_ballot randomness b)
let create_ballot b = Election.(create_ballot (make_randomness () ()) b)
let test_ballot = create_ballot [| [| 1; 0; 0; 0 |] |];;
let test_ballot = create_ballot [| [| 1; 0; 0; 0 |] |] ();;
assert (Election.check_ballot test_ballot);;
let result =
......@@ -206,7 +206,7 @@ let y = ys.(0);;
let x = Z.of_string "45298523167338358817538343074024028933886309805828157085973885299032584889325";;
assert P.G.(g **~ x =% y);;
let test_factor = Election.compute_factor tally x;;
let test_factor = Election.compute_factor tally x ();;
assert (Election.check_factor tally y test_factor);;
assert (Serializable_t.(test_factor.decryption_factors) = result.partial_decryptions.(0).decryption_factors);;
......
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