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

Exporting make_{read,write} was a bad idea

They do not behave as expected on serialized JSON objects. Instead,
directly export a reader and a writer as needed.
parent 5ffa02ef
......@@ -65,6 +65,17 @@ let unsafe_make group =
let check x = check_modulo p x && x **~ q =~ one
let to_string = Z.to_string
let of_string = Z.of_string
let read state buf =
match Yojson.Safe.from_lexbuf ~stream:true state buf with
| `String s -> Z.of_string s
| _ -> assert false
let write buf x =
Bi_outbuf.add_char buf '"';
Bi_outbuf.add_string buf (Z.to_string x);
Bi_outbuf.add_char buf '"'
let hash prefix xs =
let x = prefix ^ (map_and_concat_with_commas Z.to_string xs) in
let z = Z.of_string_base 16 (sha256_hex x) in
......
......@@ -21,14 +21,6 @@
open Serializable_builtin_t
(** {1 Helpers for interacting with atd-generated stuff} *)
val make_write :
('a -> string) -> Bi_outbuf.t -> 'a -> unit
val make_read :
(string -> 'a) -> Yojson.Safe.lexer_state -> Lexing.lexbuf -> 'a
(** {1 Serializers for type number} *)
val write_number : Bi_outbuf.t -> number -> unit
......
......@@ -23,6 +23,11 @@
open Serializable_t
(** Helpers for interacting with atd stuff *)
type 'a reader = Yojson.Safe.lexer_state -> Lexing.lexbuf -> 'a
type 'a writer = Bi_outbuf.t -> 'a -> unit
(** Empty module *)
module type EMPTY = sig end
......@@ -65,6 +70,12 @@ module type GROUP = sig
val of_string : string -> t
(** Conversion from string. *)
val read : t reader
(** Reading from a stream. *)
val write : t writer
(** Writing to a stream. *)
val hash : string -> t array -> Z.t
(** Hash an array of elements into an integer mod [q]. The string
argument is a string that is prepended before computing the hash. *)
......
......@@ -150,16 +150,13 @@ module Run (P : PARAMS) : EMPTY = struct
module M = Election.MakeSimpleMonad(G)
module E = Election.MakeElection(G)(M);;
let read_elt = make_read G.of_string
let write_elt = make_write G.to_string
(* Load and check trustee keys, if present *)
module KG = Election.MakeSimpleDistKeyGen(G)(M);;
let public_keys_with_pok =
load_from_file (
trustee_public_key_of_string read_elt
trustee_public_key_of_string G.read
) "public_keys.jsons" |> option_map Array.of_list
let () =
......@@ -201,7 +198,7 @@ module Run (P : PARAMS) : EMPTY = struct
let ballots =
load_from_file (fun line ->
ballot_of_string read_elt line,
ballot_of_string G.read line,
sha256_b64 line
) "ballots.jsons"
......@@ -248,7 +245,7 @@ module Run (P : PARAMS) : EMPTY = struct
in
let b = E.create_ballot e ?sk (E.make_randomness e ()) b () in
assert (E.check_ballot e b);
print_endline (string_of_ballot write_elt b)
print_endline (string_of_ballot G.write b)
| _ -> failwith "invalid plaintext ballot file"
)
......@@ -266,7 +263,7 @@ module Run (P : PARAMS) : EMPTY = struct
E.compute_factor tally sk ()
in
assert (E.check_factor tally pk factor);
print_endline (string_of_partial_decryption write_elt factor)
print_endline (string_of_partial_decryption G.write factor)
| _ -> failwith "invalid private key file"
)
| None -> ()
......@@ -275,7 +272,7 @@ module Run (P : PARAMS) : EMPTY = struct
let result =
load_from_file (
result_of_string read_elt
result_of_string G.read
) "result.json"
let () =
......@@ -286,7 +283,7 @@ module Run (P : PARAMS) : EMPTY = struct
failwith "invalid result file"
| None ->
let factors = load_from_file (
partial_decryption_of_string read_elt
partial_decryption_of_string G.read
) "partial_decryptions.jsons" |> option_map Array.of_list in
match factors with
| Some factors ->
......@@ -296,7 +293,7 @@ module Run (P : PARAMS) : EMPTY = struct
assert (E.check_result e result);
if do_finalize then (
save_to "result.json" (
write_result write_elt
write_result G.write
) result;
Printf.eprintf "result.json written\n%!"
);
......
......@@ -117,8 +117,6 @@ end
module Run (P : PARAMS) : EMPTY = struct
open P
let read_elt = make_read G.of_string
(* Setup group *)
module M = Election.MakeSimpleMonad(G);;
......@@ -138,7 +136,7 @@ module Run (P : PARAMS) : EMPTY = struct
in
close_in ic;
let keys = List.map (fun x ->
trustee_public_key_of_string read_elt x
trustee_public_key_of_string G.read x
) raw_keys |> Array.of_list in
assert (Array.forall KG.check keys);
keys
......
......@@ -76,8 +76,6 @@ end
module Run (P : PARAMS) : EMPTY = struct
open P
let write_elt = make_write G.to_string
(* Setup group *)
module M = Election.MakeSimpleMonad(G);;
......@@ -102,7 +100,7 @@ module Run (P : PARAMS) : EMPTY = struct
id ^ ".pubkey",
0o444,
public_key,
write_trustee_public_key write_elt
write_trustee_public_key G.write
let privkey =
"private",
......
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