Skip to content

Commit

Permalink
In tool modules, all Run functors take an abstract GROUP
Browse files Browse the repository at this point in the history
  • Loading branch information
glondu committed Mar 25, 2014
1 parent 15efea9 commit 291cb47
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 35 deletions.
4 changes: 2 additions & 2 deletions src/bin/credgen.ml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
35 changes: 19 additions & 16 deletions src/bin/election_tool.ml
Expand Up @@ -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


Expand Down Expand Up @@ -150,21 +150,24 @@ 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 () =
match public_keys_with_pok with
| 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 =
Expand All @@ -186,27 +189,27 @@ 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"

let check_signature_present =
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)
Expand Down Expand Up @@ -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"
)

Expand All @@ -255,15 +258,15 @@ 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
let factor =
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 -> ()
Expand All @@ -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 () =
Expand All @@ -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 ->
Expand All @@ -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%!"
);
Expand Down
35 changes: 21 additions & 14 deletions src/bin/mkelection.ml
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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);;
Expand All @@ -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
Expand All @@ -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

Expand Down
8 changes: 5 additions & 3 deletions src/bin/tkeygen.ml
Expand Up @@ -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
Expand Down Expand Up @@ -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);;
Expand All @@ -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
;;

Expand All @@ -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",
Expand Down

0 comments on commit 291cb47

Please sign in to comment.