Commit 63ff6087 authored by Stephane Glondu's avatar Stephane Glondu

Prepare for more test data

 * max can be int or null
 * votes.json can be absent
 * incidentally, fix verify_partial_decryptions
parent 76fb81b8
......@@ -80,3 +80,25 @@ let read_datetime state buf =
let datetime_of_string s =
datetime_of_json (Yojson.Safe.from_string s)
(** {1 Serializers for type int_or_null} *)
let write_int_or_null buf = function
| Some n -> Bi_outbuf.add_string buf (string_of_int n)
| None -> Bi_outbuf.add_string buf "null"
let string_of_int_or_null ?(len=4) n =
let buf = Bi_outbuf.create len in
write_int_or_null buf n;
Bi_outbuf.contents buf
let int_or_null_of_json = function
| `Int i -> Some i
| `Null -> None
| _ -> assert false
let read_int_or_null state buf =
int_or_null_of_json (Yojson.Safe.from_lexbuf ~stream:true state buf)
let int_or_null_of_string s =
int_or_null_of_json (Yojson.Safe.from_string s)
......@@ -20,3 +20,10 @@ val write_datetime : Bi_outbuf.t -> datetime -> unit
val string_of_datetime : ?len:int -> datetime -> string
val read_datetime : Yojson.Safe.lexer_state -> Lexing.lexbuf -> datetime
val datetime_of_string : string -> datetime
(** {1 Serializers for type int_or_null} *)
val write_int_or_null : Bi_outbuf.t -> int_or_null -> unit
val string_of_int_or_null : ?len:int -> int_or_null -> string
val read_int_or_null : Yojson.Safe.lexer_state -> Lexing.lexbuf -> int_or_null
val int_or_null_of_string : string -> int_or_null
type number = Z.t
type uuid = Uuidm.t
type datetime = CalendarLib.Fcalendar.Precise.t * string option
type int_or_null = int option
......@@ -61,9 +61,10 @@ module type ELGAMAL_CRYPTO = sig
val verify_answer : t -> question -> t answer -> bool
val verify_vote : t election -> string -> t vote -> bool
val verify_equality : t -> t -> t -> t proof -> bool
val verify_partial_decryption :
t election -> t tally -> t trustee_public_key -> t partial_decryption -> bool
val verify_partial_decryptions : t election -> t election_public_data -> bool
val verify_partial_decryption : t election ->
t tally -> t trustee_public_key -> t partial_decryption -> bool
val verify_partial_decryptions : t election ->
t trustee_public_key array -> t result -> bool
val verify_result : t election -> t result -> bool
val compute_encrypted_tally : t election -> t vote array -> t encrypted_tally
end
......@@ -124,6 +125,11 @@ module Make (G : GROUP) = struct
let verify_answer y question answer =
let {q_max; q_min; q_answers; _} = question in
let q_max =
match q_max with
| Some q -> q
| None -> assert false (* FIXME *)
in
let nb = Array.length q_answers in
Array.length answer.choices = nb &&
Array.length answer.individual_proofs = nb &&
......@@ -163,14 +169,10 @@ module Make (G : GROUP) = struct
) question.q_answers
) election.e_questions
let verify_partial_decryptions election public_data =
(* FIXME: move this match elsewhere *)
match public_data.election_result with
| Some r ->
Array.forall2 (verify_partial_decryption election r.encrypted_tally.tally)
public_data.public_keys
r.partial_decryptions
| None -> false
let verify_partial_decryptions election public_keys r =
Array.forall2 (verify_partial_decryption election r.encrypted_tally.tally)
public_keys
r.partial_decryptions
let verify_result election public_data =
let pds = public_data.partial_decryptions in
......
......@@ -33,9 +33,10 @@ module type ELGAMAL_CRYPTO = sig
val verify_answer : t -> question -> t answer -> bool
val verify_vote : t election -> string -> t vote -> bool
val verify_equality : t -> t -> t -> t proof -> bool
val verify_partial_decryption :
t election -> t tally -> t trustee_public_key -> t partial_decryption -> bool
val verify_partial_decryptions : t election -> t election_public_data -> bool
val verify_partial_decryption : t election ->
t tally -> t trustee_public_key -> t partial_decryption -> bool
val verify_partial_decryptions : t election ->
t trustee_public_key array -> t result -> bool
val verify_result : t election -> t result -> bool
val compute_encrypted_tally : t election -> t vote array -> t encrypted_tally
end
......
......@@ -3,6 +3,7 @@
type number <ocaml predef from="Core_datatypes"> = abstract
type uuid <ocaml predef from="Core_datatypes"> = abstract
type datetime <ocaml predef from="Core_datatypes"> = abstract
type int_or_null <ocaml predef from="Core_datatypes"> = abstract
(** {1 ElGamal keys} *)
......@@ -37,7 +38,7 @@ type question = {
answer_urls : unit list;
answers : string list <ocaml repr="array">;
choice_type : choice_type;
max : int;
max : int_or_null;
min : int;
question : string;
result_type : result_type;
......
......@@ -42,10 +42,13 @@ let load_election_data raw =
(data "public.json")
in
let votes =
non_empty_lines_of_file (data "votes.json") |>
Lwt_main.run |>
List.map (Helios_datatypes_j.vote_of_string Core_datatypes_j.read_number) |>
List.rev
let file = data "votes.json" in
if Sys.file_exists file then (
non_empty_lines_of_file file |>
Lwt_main.run |>
List.map (Helios_datatypes_j.vote_of_string Core_datatypes_j.read_number) |>
List.rev
) else []
in
Helios_services.({ raw; fingerprint; election; votes; public_data })
......
......@@ -69,7 +69,7 @@ type 'a election_test_data = {
raw : string;
fingerprint : string;
election : 'a election;
votes : 'a vote array;
votes : 'a vote array option;
public_data : 'a election_public_data;
private_data : 'a election_private_data;
}
......@@ -92,10 +92,13 @@ let load_election_test_data ?(verbose=false) dir =
let private_data = load_and_check ~verbose Types.election_private_data (data "private.json") in
let fingerprint = hashB raw in
let votes =
non_empty_lines_of_file (data "votes.json") |>
Lwt_main.run |>
List.map (Helios_datatypes_j.vote_of_string Core_datatypes_j.read_number) |>
Array.of_list
let file = data "votes.json" in
if Sys.file_exists file then Some (
non_empty_lines_of_file file |>
Lwt_main.run |>
List.map (Helios_datatypes_j.vote_of_string Core_datatypes_j.read_number) |>
Array.of_list
) else None
in
{ raw; fingerprint; election; votes; public_data; private_data }
......@@ -109,30 +112,41 @@ let verbose_verify_election_test_data e =
let {g; p; q; y} = e.election.e_public_key in
let module G = (val ElGamal.make_ff_msubgroup p q g : ElGamal.GROUP with type t = Z.t) in
let module Crypto = ElGamal.Make (G) in
let r =
match e.public_data.election_result with
| Some r -> r
| None -> assert false
in
verbose_assert "election key"
(lazy (Crypto.verify_election_key
e.election.e_public_key.y
e.public_data.public_keys));
verbose_assert "votes"
(lazy (Array.foralli
(fun _ x -> Crypto.verify_vote e.election e.fingerprint x)
e.votes));
verbose_assert "encrypted tally"
(lazy (r.encrypted_tally =
Crypto.compute_encrypted_tally e.election e.votes));
verbose_assert "partial decryptions"
(lazy (Crypto.verify_partial_decryptions e.election e.public_data));
verbose_assert "result"
(lazy (Crypto.verify_result e.election r));
verbose_assert "private keys"
(lazy (Array.foralli
(fun _ k -> Crypto.verify_private_key k)
e.private_data.private_keys));;
verbose_assert "election key" (lazy (
Crypto.verify_election_key
e.election.e_public_key.y
e.public_data.public_keys
));
(match e.votes with
| Some votes ->
verbose_assert "votes" (lazy (
Array.foralli
(fun _ x -> Crypto.verify_vote e.election e.fingerprint x)
votes
));
(match e.public_data.election_result with
| Some r ->
verbose_assert "encrypted tally" (lazy (
r.encrypted_tally = Crypto.compute_encrypted_tally e.election votes
))
| None -> ()
);
| None -> Printf.eprintf " no votes available\n%!"
);
(match e.public_data.election_result with
| Some r ->
verbose_assert "partial decryptions" (lazy (
Crypto.verify_partial_decryptions
e.election e.public_data.public_keys r
));
verbose_assert "result" (lazy (Crypto.verify_result e.election r));
| None -> Printf.eprintf " no results available\n%!"
);
verbose_assert "private keys" (lazy (
Array.foralli
(fun _ k -> Crypto.verify_private_key k)
e.private_data.private_keys
));;
let notdotfiles_of_directory dirname =
Lwt_unix.files_of_directory dirname |>
......
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