Commit 1fa83008 authored by Stephane Glondu's avatar Stephane Glondu

Move most of ELECTION.process_ballots to Question.process_ciphertexts

parent f143218b
Pipeline #68596 passed with stages
in 15 minutes and 59 seconds
......@@ -68,20 +68,6 @@ module Make (W : ELECTION_DATA) (M : RANDOM) = struct
let ( / ) x y = x *~ invert y
let dummy_ciphertext =
{
alpha = G.one;
beta = G.one;
}
(** Multiply two ElGamal ciphertexts. *)
let eg_combine c1 c2 =
{
alpha = c1.alpha *~ c2.alpha;
beta = c1.beta *~ c2.beta;
}
type plaintext = int array array
type ballot = elt Serializable_t.ballot
......@@ -171,17 +157,9 @@ module Make (W : ELECTION_DATA) (M : RANDOM) = struct
Array.forall2 (verify_answer p.e_public_key zkp) p.e_questions b.answers
let process_ballots bs =
let bs = Array.map (fun b -> Array.map2 Q.extract_ciphertexts election.e_params.e_questions b.answers) bs in
SArray (
Array.mapi (fun i q ->
match Question.neutral_shape q with
| Some s ->
let s = Shape.map (fun _ -> dummy_ciphertext) s in
Array.fold_left (fun accu b ->
Shape.map2 eg_combine accu b.(i)
) s bs
| None ->
SArray (Array.map (fun b -> b.(i)) bs)
Q.process_ciphertexts q (Array.map (fun b -> Q.extract_ciphertexts q b.answers.(i)) bs)
) election.e_params.e_questions
)
......
......@@ -54,10 +54,6 @@ let write_question b = function
in
Yojson.Safe.write_json b (`Assoc o)
let neutral_shape = function
| Standard q -> Some (SArray (Array.make (Question_std.question_length q) (SAtomic ())))
| Open _ -> None
let erase_question = function
| Standard q ->
let open Question_std_t in
......@@ -83,6 +79,7 @@ module type S = sig
val verify_answer : question -> public_key:elt -> prefix:string -> Yojson.Safe.json -> bool
val extract_ciphertexts : question -> Yojson.Safe.json -> elt ciphertext shape
val process_ciphertexts : question -> elt ciphertext shape array -> elt ciphertext shape
val compute_result : num_tallied:int -> question -> elt shape -> int shape
val check_result : question -> elt shape -> int shape -> bool
......@@ -137,6 +134,11 @@ module Make (M : RANDOM) (G : GROUP) = struct
|> Question_open_j.answer_of_string G.read
|> QOpen.extract_ciphertexts
let process_ciphertexts q e =
match q with
| Standard q -> QStandard.process_ciphertexts q e
| Open q -> QOpen.process_ciphertexts q e
let compute_result ~num_tallied =
let compute_std = lazy (QStandard.compute_result ~num_tallied) in
fun q x ->
......
......@@ -30,7 +30,6 @@ type question =
val read_question : Yojson.Safe.lexer_state -> Lexing.lexbuf -> question
val write_question : Bi_outbuf.t -> question -> unit
val neutral_shape : question -> unit shape option
val erase_question : question -> question
module type S = sig
......@@ -41,6 +40,7 @@ module type S = sig
val verify_answer : question -> public_key:elt -> prefix:string -> Yojson.Safe.json -> bool
val extract_ciphertexts : question -> Yojson.Safe.json -> elt ciphertext shape
val process_ciphertexts : question -> elt ciphertext shape array -> elt ciphertext shape
val compute_result : num_tallied:int -> question -> elt shape -> int shape
val check_result : question -> elt shape -> int shape -> bool
......
......@@ -35,6 +35,7 @@ module type S = sig
val verify_answer : question -> public_key:elt -> prefix:string -> elt answer -> bool
val extract_ciphertexts : elt answer -> elt ciphertext shape
val process_ciphertexts : question -> elt ciphertext shape array -> elt ciphertext shape
val compute_result : question -> elt shape -> int shape
val check_result : question -> elt shape -> int shape -> bool
......@@ -71,6 +72,17 @@ module Make (M : RANDOM) (G : GROUP) = struct
let extract_ciphertexts a =
SAtomic a.choices
let compare_ciphertexts x y =
match x, y with
| SAtomic x, SAtomic y ->
let c = G.compare x.alpha y.alpha in
if c = 0 then G.compare x.beta y.beta else c
| _, _ -> invalid_arg "Question_open.compare_ciphertexts"
let process_ciphertexts _ es =
Array.fast_sort compare_ciphertexts es;
SArray es
let compute_result q x =
let n = Array.length q.q_answers in
let rec aux = function
......
......@@ -31,6 +31,7 @@ module type S = sig
val verify_answer : question -> public_key:elt -> prefix:string -> elt answer -> bool
val extract_ciphertexts : elt answer -> elt ciphertext shape
val process_ciphertexts : question -> elt ciphertext shape array -> elt ciphertext shape
val compute_result : question -> elt shape -> int shape
val check_result : question -> elt shape -> int shape -> bool
......
......@@ -43,6 +43,7 @@ module type S = sig
val verify_answer : question -> public_key:elt -> prefix:string -> elt answer -> bool
val extract_ciphertexts : elt answer -> elt ciphertext shape
val process_ciphertexts : question -> elt ciphertext shape array -> elt ciphertext shape
val compute_result : num_tallied:int -> question -> elt shape -> int shape
val check_result : question -> elt shape -> int shape -> bool
......@@ -424,6 +425,10 @@ module Make (M : RANDOM) (G : GROUP) = struct
let extract_ciphertexts a =
SArray (Array.map (fun x -> SAtomic x) a.choices)
let process_ciphertexts q es =
let neutral = SArray (Array.make (question_length q) (SAtomic dummy_ciphertext)) in
Array.fold_left (Shape.map2 eg_combine) neutral es
let compute_result ~num_tallied =
let log =
let module GMap = Map.Make(G) in
......
......@@ -33,6 +33,7 @@ module type S = sig
val verify_answer : question -> public_key:elt -> prefix:string -> elt answer -> bool
val extract_ciphertexts : elt answer -> elt ciphertext shape
val process_ciphertexts : question -> elt ciphertext shape array -> elt ciphertext shape
val compute_result : num_tallied:int -> question -> elt shape -> int shape
val check_result : question -> elt shape -> int shape -> bool
......
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