mkelection.ml 4.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
(**************************************************************************)
(*                                BELENIOS                                *)
(*                                                                        *)
(*  Copyright © 2012-2014 Inria                                           *)
(*                                                                        *)
(*  This program is free software: you can redistribute it and/or modify  *)
(*  it under the terms of the GNU Affero General Public License as        *)
(*  published by the Free Software Foundation, either version 3 of the    *)
(*  License, or (at your option) any later version, with the additional   *)
(*  exemption that compiling, linking, and/or using OpenSSL is allowed.   *)
(*                                                                        *)
(*  This program is distributed in the hope that it will be useful, but   *)
(*  WITHOUT ANY WARRANTY; without even the implied warranty of            *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *)
(*  Affero General Public License for more details.                       *)
(*                                                                        *)
(*  You should have received a copy of the GNU Affero General Public      *)
(*  License along with this program.  If not, see                         *)
(*  <http://www.gnu.org/licenses/>.                                       *)
(**************************************************************************)

open Util
open Serializable_t
Stephane Glondu's avatar
Stephane Glondu committed
24
open Signatures
25
26
27
28
29
30
31

module type PARAMS = sig
  val group : (module Election.FF_GROUP)
  val uuid : Uuidm.t
  val template : template
end

Stephane Glondu's avatar
Stephane Glondu committed
32
module GetParams (X : EMPTY) : PARAMS = struct
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
  let group = ref None
  let uuid = ref None
  let template = ref None

  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";
  ])

  let usage_msg =
    Printf.sprintf "Usage: %s mkelection --group <file> --uuid <uuid> --template <file>" Sys.argv.(0)

  let usage () =
    Arg.usage speclist usage_msg;
    exit 1

  let die s = prerr_endline s; usage ()

  let anon_fun x =
    Printf.eprintf "I do not know what to do with %s\n" x;
    usage ()

  let () = Arg.parse speclist anon_fun usage_msg

  let group = match !group with
    | None -> die "--group 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 = Serializable_j.read_ff_params ls lb in
      close_in ic;
      Election.finite_field r

  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 = Serializable_j.read_template ls lb in
      close_in ic;
      r
end

module MakeElection (G : Election.FF_GROUP) (P : PARAMS) = struct

  (* Setup group *)

  module M = Election.MakeSimpleMonad(G);;

  (* Setup trustees *)

  module KG = Election.MakeSimpleDistKeyGen(G)(M);;

  let public_keys =
    let ic = open_in "public_keys.jsons" in
    let raw_keys =
      let rec loop xs =
        match (try Some (input_line ic) with End_of_file -> None) with
        | Some x -> loop (x::xs)
        | None -> xs
      in loop []
    in
    close_in ic;
    let keys = List.map (fun x ->
      Serializable_j.trustee_public_key_of_string Serializable_builtin_j.read_number x
    ) raw_keys |> Array.of_list in
    assert (Array.forall KG.check keys);
    keys

  let y = KG.combine public_keys

  (* 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;
  }

  (* Save to disk *)

  open Serializable_j
  let () = save_to "election.json" (write_params write_ff_pubkey) params

end

let main () =
  let module P = GetParams (struct end) in
  let module G = (val P.group : Election.FF_GROUP) in
  let module X = MakeElection (G) (P) in
  ()