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

More uniform pattern in tool modules

parent 531bbeb1
......@@ -39,24 +39,24 @@ let do_derive uuid x =
transform_string (Hexa.encode ())
module type PARAMS = sig
val group : (module Group_field.GROUP)
val uuid : Uuidm.t
val count : int option ref
val file : string option ref
val derive : string option ref
val dir : string ref
val count : int option
val file : string option
val derive : string option
val dir : string
module G : Group_field.GROUP
end
module GetParams (X : EMPTY) : PARAMS = struct
let parse_args () = begin
(* Argument parsing *)
let dir = ref (Sys.getcwd ())
let uuid = ref None
let count = ref None
let file = ref None
let derive = ref None
let group = ref None
let dir = ref (Sys.getcwd ()) in
let uuid = ref None in
let count = ref None in
let file = ref None in
let derive = ref None in
let group = ref None in
let speclist = Arg.([
"--dir", String (fun s -> dir := s), "directory where output will be written";
......@@ -65,16 +65,18 @@ module GetParams (X : EMPTY) : PARAMS = struct
"--file", String (fun s -> file := Some s), "file with list of identities";
"--derive", String (fun s -> derive := Some s), "derive public credential from given private one";
"--group", String (fun s -> group := Some s), "file with group parameters";
])
]) in
let usage_msg =
Printf.sprintf "Usage: %s credgen [--dir <dir>] --uuid <uuid> {--count <n> | --file <file> | --derive <privcred>}" Sys.argv.(0)
in
let anon_fun x =
Printf.eprintf "I do not know what to do with %s!\n" x;
exit 1
in
let () = Arg.parse speclist anon_fun usage_msg
let () = Arg.parse speclist anon_fun usage_msg in
let group = match !group with
| None ->
......@@ -86,22 +88,33 @@ module GetParams (X : EMPTY) : PARAMS = struct
let lb = Lexing.from_channel ic in
let r = Serializable_j.read_ff_params ls lb in
close_in ic;
Group_field.make r
r
in
let uuid = match !uuid with
| None ->
Printf.eprintf "UUID is missing!\n";
exit 1
| Some u ->
match Uuidm.of_string u with
let module P = struct
let uuid = match !uuid with
| None ->
Printf.eprintf "UUID is missing!\n";
exit 1
| Some u ->
match Uuidm.of_string u with
| Some u -> u
| None ->
Printf.eprintf "UUID is invalid!\n";
exit 1
let count = !count
let file = !file
let derive = !derive
let dir = !dir
module G = (val Group_field.make group : Group_field.GROUP)
end in
(module P : PARAMS)
end
module RunCredgen (P : PARAMS) (G : Group_field.GROUP) = struct
module Run (P : PARAMS) : EMPTY = struct
open P
(* Some helpers *)
......@@ -119,7 +132,7 @@ module RunCredgen (P : PARAMS) (G : Group_field.GROUP) = struct
Z.to_string y
let count, ids =
match !count, !file, !derive with
match count, file, derive with
| Some i, None, None ->
if i < 1 then (
Printf.eprintf "You must generate at least one credential!\n";
......@@ -217,7 +230,7 @@ module RunCredgen (P : PARAMS) (G : Group_field.GROUP) = struct
output_char oc '\n'
let save (kind, filename, perm, thing) =
let full_filename = Filename.concat !dir filename in
let full_filename = Filename.concat dir filename in
let oc = open_out_gen [
Open_wronly; Open_creat; Open_excl
] perm full_filename in
......@@ -234,7 +247,6 @@ end
let derive = do_derive
let main () =
let module P = GetParams (struct end) in
let module G = (val P.group : Group_field.GROUP) in
let module X = RunCredgen (P) (G) in
let module P = (val parse_args () : PARAMS) in
let module X : EMPTY = Run (P) in
()
......@@ -52,19 +52,20 @@ module type PARAMS = sig
val election_fingerprint : string
val group : ff_params
val y : number
module G : Group_field.GROUP
end
module GetParams (X : EMPTY) : PARAMS = struct
let parse_args () = begin
(* Command-line arguments *)
let initial_dir = Sys.getcwd ()
let dir = ref initial_dir
let sk_file = ref None
let do_finalize = ref false
let do_decrypt = ref false
let ballot_file = ref None
let initial_dir = Sys.getcwd () in
let dir = ref initial_dir in
let sk_file = ref None in
let do_finalize = ref false in
let do_decrypt = ref false in
let ballot_file = ref None in
let speclist = Arg.([
"--dir", String (fun s -> dir := s), "path to election files";
......@@ -73,21 +74,24 @@ module GetParams (X : EMPTY) : PARAMS = struct
if Filename.is_relative s then Filename.concat initial_dir s else s
in sk_file := Some fname
), "path to private key";
])
]) in
let usage_msg =
Printf.sprintf "Usage: %s election [--dir <dir>] [--privkey <privkey>] { vote <ballot> | verify | decrypt | finalize }" Sys.argv.(0)
in
let usage () =
Arg.usage speclist usage_msg;
exit 1
in
let anon_args = ref []
let anon_args = ref [] in
let anon_fun x =
anon_args := x :: !anon_args
in
let () = Arg.parse speclist anon_fun usage_msg
let () = Arg.parse speclist anon_fun usage_msg in
let () = match List.rev !anon_args with
| [] -> usage ()
......@@ -107,8 +111,9 @@ module GetParams (X : EMPTY) : PARAMS = struct
exit 1
| Some _ -> do_decrypt := true)
| x :: _ -> usage ()
in
let () = Sys.chdir !dir
let () = Sys.chdir !dir in
(* Load and check election *)
......@@ -119,19 +124,27 @@ module GetParams (X : EMPTY) : PARAMS = struct
) "election.json") with
| Some [e] -> e
| _ -> failwith "invalid election file"
in
let {ffpk_g = g; ffpk_p = p; ffpk_q = q; ffpk_y = y} = params.e_public_key
let group = {g; p; q}
let {ffpk_g = g; ffpk_p = p; ffpk_q = q; ffpk_y = y} = params.e_public_key in
let sk_file = !sk_file
let do_finalize = !do_finalize
let do_decrypt = !do_decrypt
let ballot_file = !ballot_file
let module P = struct
let sk_file = !sk_file
let do_finalize = !do_finalize
let do_decrypt = !do_decrypt
let ballot_file = !ballot_file
let params = params
let election_fingerprint = election_fingerprint
let group = {g; p; q}
let y = y
module G = (val Group_field.make group : Group_field.GROUP)
end in
end
(module P : PARAMS)
end
module RunTool (G : Group_field.GROUP) (P : PARAMS) = struct
module Run (P : PARAMS) : EMPTY = struct
open P
module M = Election.MakeSimpleMonad(G)
......@@ -294,7 +307,6 @@ end
let main () =
let module P = GetParams(struct end) in
let module G = (val Group_field.make P.group : Group_field.GROUP) in
let module X = RunTool (G) (P) in
let module P = (val parse_args () : PARAMS) in
let module X : EMPTY = Run (P) in
()
......@@ -25,36 +25,40 @@ open Signatures
open Common
module type PARAMS = sig
val group : (module Group_field.GROUP)
val uuid : Uuidm.t
val template : template
module G : Group_field.GROUP
end
module GetParams (X : EMPTY) : PARAMS = struct
let group = ref None
let uuid = ref None
let template = ref None
let parse_args () = begin
let group = ref None in
let uuid = ref None in
let template = ref None in
let speclist = Arg.([
"--group", String (fun s -> group := Some s), "file with group parameters";
"--uuid", String (fun s -> uuid := Some s), "UUID of the election";
"--template", String (fun s -> template := Some s), "file with election template";
])
]) in
let usage_msg =
Printf.sprintf "Usage: %s mkelection --group <file> --uuid <uuid> --template <file>" Sys.argv.(0)
in
let usage () =
Arg.usage speclist usage_msg;
exit 1
in
let die s = prerr_endline s; usage ()
let die s = prerr_endline s; usage () in
let anon_fun x =
Printf.eprintf "I do not know what to do with %s\n" x;
usage ()
in
let () = Arg.parse speclist anon_fun usage_msg
let () = Arg.parse speclist anon_fun usage_msg in
let group = match !group with
| None -> die "--group is missing"
......@@ -63,27 +67,37 @@ module GetParams (X : EMPTY) : PARAMS = struct
let ls = Yojson.init_lexer () in
let lb = Lexing.from_channel ic in
let r = read_ff_params ls lb in
close_in ic;
Group_field.make r
r
in
let uuid = match !uuid with
| None -> die "--uuid is missing"
| Some uuid -> match Uuidm.of_string uuid with
| None -> die "invalid UUID"
| Some u -> u
let module P = struct
let uuid = match !uuid with
| None -> die "--uuid is missing"
| Some uuid -> match Uuidm.of_string uuid with
| None -> die "invalid UUID"
| Some u -> u
let template = match !template with
| None -> die "--template is missing"
| Some fname ->
let ic = open_in fname in
let ls = Yojson.init_lexer () in
let lb = Lexing.from_channel ic in
let r = read_template ls lb in
close_in ic;
r
module G = (val Group_field.make group : Group_field.GROUP)
end in
(module P : PARAMS)
let template = match !template with
| None -> die "--template is missing"
| Some fname ->
let ic = open_in fname in
let ls = Yojson.init_lexer () in
let lb = Lexing.from_channel ic in
let r = read_template ls lb in
close_in ic;
r
end
module MakeElection (G : Group_field.GROUP) (P : PARAMS) = struct
module Run (P : PARAMS) : EMPTY = struct
open P
(* Setup group *)
......@@ -131,7 +145,6 @@ module MakeElection (G : Group_field.GROUP) (P : PARAMS) = struct
end
let main () =
let module P = GetParams (struct end) in
let module G = (val P.group : Group_field.GROUP) in
let module X = MakeElection (G) (P) in
let module P = (val parse_args () : PARAMS) in
let module X : EMPTY = Run (P) in
()
......@@ -25,28 +25,32 @@ open Signatures
open Common
module type PARAMS = sig
val group : (module Group_field.GROUP)
module G : Group_field.GROUP
end
module GetParams (X : EMPTY) : PARAMS = struct
let group = ref None
let parse_args () = begin
let group = ref None in
let speclist = Arg.([
"--group", String (fun s -> group := Some s), "file with group parameters";
])
]) in
let usage_msg =
Printf.sprintf "Usage: %s trustee-keygen --group <file>" Sys.argv.(0)
in
let usage () =
Arg.usage speclist usage_msg;
exit 1
in
let anon_fun x =
Printf.eprintf "I do not know what to do with %s\n" x;
usage ()
in
let () = Arg.parse speclist anon_fun usage_msg
let () = Arg.parse speclist anon_fun usage_msg in
let group = match !group with
| None ->
......@@ -58,10 +62,19 @@ module GetParams (X : EMPTY) : PARAMS = struct
let lb = Lexing.from_channel ic in
let r = read_ff_params ls lb in
close_in ic;
Group_field.make r
r
in
let module P = struct
module G = (val Group_field.make group : Group_field.GROUP)
end in
(module P : PARAMS)
end
module RunTrusteeKeygen (G : Group_field.GROUP) = struct
module Run (P : PARAMS) : EMPTY = struct
open P
(* Setup group *)
......@@ -114,7 +127,6 @@ end
let main () =
let module P = GetParams (struct end) in
let module G = (val P.group : Group_field.GROUP) in
let module X = RunTrusteeKeygen (G) in
let module P = (val parse_args () : PARAMS) in
let module X : EMPTY = Run (P) in
()
Supports Markdown
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