Commit 291cb470 authored by Stephane Glondu's avatar Stephane Glondu
Browse files

In tool modules, all Run functors take an abstract GROUP

parent 15efea94
......@@ -44,7 +44,7 @@ module type PARAMS = sig
val file : string option
val derive : string option
val dir : string
module G : Group_field.GROUP
module G : GROUP
end
let parse_args () = begin
......@@ -129,7 +129,7 @@ module Run (P : PARAMS) : EMPTY = struct
let hex = do_derive uuid x in
let x = Z.(of_string_base 16 hex mod G.q) in
let y = G.(g **~ x) in
Z.to_string y
G.to_string y
let count, ids =
match count, file, derive with
......
......@@ -51,8 +51,8 @@ module type PARAMS = sig
val params : ff_pubkey params
val election_fingerprint : string
val group : ff_params
val y : number
module G : Group_field.GROUP
module G : GROUP
val y : G.t
end
......@@ -150,13 +150,16 @@ 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_number
trustee_public_key_of_string read_elt
) "public_keys.jsons" |> option_map Array.of_list
let () =
......@@ -164,7 +167,7 @@ module Run (P : PARAMS) : EMPTY = struct
| Some pks ->
assert (Array.forall KG.check pks);
let y' = KG.combine pks in
assert (P.y =% y')
assert G.(y =~ y')
| None -> ()
let public_keys =
......@@ -186,19 +189,19 @@ module Run (P : PARAMS) : EMPTY = struct
(* Load ballots, if present *)
module ZSet = Set.Make(Z)
module GSet = Set.Make (G)
let public_creds =
load_from_file Z.of_string "public_creds.txt" |>
load_from_file G.of_string "public_creds.txt" |>
option_map (fun xs ->
List.fold_left (fun accu x ->
ZSet.add x accu
) ZSet.empty xs
GSet.add x accu
) GSet.empty xs
)
let ballots =
load_from_file (fun line ->
ballot_of_string read_number line,
ballot_of_string read_elt line,
sha256_b64 line
) "ballots.jsons"
......@@ -206,7 +209,7 @@ module Run (P : PARAMS) : EMPTY = struct
match public_creds with
| Some creds -> (fun b ->
match b.signature with
| Some s -> ZSet.mem s.s_public_key creds
| Some s -> GSet.mem s.s_public_key creds
| None -> false
)
| None -> (fun _ -> true)
......@@ -245,7 +248,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_number b)
print_endline (string_of_ballot write_elt b)
| _ -> failwith "invalid plaintext ballot file"
)
......@@ -255,7 +258,7 @@ module Run (P : PARAMS) : EMPTY = struct
(match load_from_file (number_of_string) fn with
| Some [sk] ->
let pk = G.(g **~ sk) in
if Array.forall (fun x -> not (x =% pk)) pks then (
if Array.forall (fun x -> not G.(x =~ pk)) pks then (
Printf.eprintf "Warning: your key is not present in public_keys.jsons!\n";
);
let tally = Lazy.force encrypted_tally in
......@@ -263,7 +266,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_number factor)
print_endline (string_of_partial_decryption write_elt factor)
| _ -> failwith "invalid private key file"
)
| None -> ()
......@@ -272,7 +275,7 @@ module Run (P : PARAMS) : EMPTY = struct
let result =
load_from_file (
result_of_string read_number
result_of_string read_elt
) "result.json"
let () =
......@@ -283,7 +286,7 @@ module Run (P : PARAMS) : EMPTY = struct
failwith "invalid result file"
| None ->
let factors = load_from_file (
partial_decryption_of_string read_number
partial_decryption_of_string read_elt
) "partial_decryptions.jsons" |> option_map Array.of_list in
match factors with
| Some factors ->
......@@ -293,7 +296,7 @@ module Run (P : PARAMS) : EMPTY = struct
assert (E.check_result e result);
if do_finalize then (
save_to "result.json" (
write_result write_number
write_result write_elt
) result;
Printf.eprintf "result.json written\n%!"
);
......
......@@ -26,8 +26,9 @@ open Common
module type PARAMS = sig
val uuid : Uuidm.t
val template : template
module G : Group_field.GROUP
module G : GROUP
val params : G.t -> G.t wrapped_pubkey params
val write_params : Bi_outbuf.t -> G.t wrapped_pubkey params -> unit
end
let parse_args () = begin
......@@ -70,6 +71,8 @@ let parse_args () = begin
r
in
let {g; p; q} = group in
let module P = struct
let uuid = match !uuid with
......@@ -90,6 +93,17 @@ let parse_args () = begin
module G = (val Group_field.make group : Group_field.GROUP)
let params y = {
e_description = template.t_description;
e_name = template.t_name;
e_public_key = {ffpk_g = g; ffpk_p = p; ffpk_q = q; ffpk_y = y};
e_questions = template.t_questions;
e_uuid = uuid;
e_short_name = template.t_short_name;
}
let write_params = write_params write_ff_pubkey
end in
(module P : PARAMS)
......@@ -99,6 +113,8 @@ end
module Run (P : PARAMS) : EMPTY = struct
open P
let read_elt = make_read G.of_string
(* Setup group *)
module M = Election.MakeSimpleMonad(G);;
......@@ -118,7 +134,7 @@ module Run (P : PARAMS) : EMPTY = struct
in
close_in ic;
let keys = List.map (fun x ->
trustee_public_key_of_string read_number x
trustee_public_key_of_string read_elt x
) raw_keys |> Array.of_list in
assert (Array.forall KG.check keys);
keys
......@@ -127,20 +143,11 @@ module Run (P : PARAMS) : EMPTY = struct
(* Setup election *)
let {g; p; q} = G.group
let params = {
e_description = P.template.t_description;
e_name = P.template.t_name;
e_public_key = {ffpk_g = g; ffpk_p = p; ffpk_q = q; ffpk_y = y};
e_questions = P.template.t_questions;
e_uuid = P.uuid;
e_short_name = P.template.t_short_name;
}
let params = P.params y
(* Save to disk *)
let () = save_to "election.json" (write_params write_ff_pubkey) params
let () = save_to "election.json" write_params params
end
......
......@@ -25,7 +25,7 @@ open Signatures
open Common
module type PARAMS = sig
module G : Group_field.GROUP
module G : GROUP
end
let parse_args () = begin
......@@ -76,6 +76,8 @@ end
module Run (P : PARAMS) : EMPTY = struct
open P
let write_elt = make_write G.to_string
(* Setup group *)
module M = Election.MakeSimpleMonad(G);;
......@@ -89,7 +91,7 @@ module Run (P : PARAMS) : EMPTY = struct
(* Save to file *)
let id = String.sub
(sha256_hex (Z.to_string public_key.trustee_public_key))
(sha256_hex (G.to_string public_key.trustee_public_key))
0 8 |> String.uppercase
;;
......@@ -100,7 +102,7 @@ module Run (P : PARAMS) : EMPTY = struct
id ^ ".pubkey",
0o444,
public_key,
write_trustee_public_key write_number
write_trustee_public_key write_elt
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