tool_mkelection.ml 3.72 KB
Newer Older
1 2 3
(**************************************************************************)
(*                                BELENIOS                                *)
(*                                                                        *)
Stephane Glondu's avatar
Stephane Glondu committed
4
(*  Copyright © 2012-2018 Inria                                           *)
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
(*                                                                        *)
(*  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/>.                                       *)
(**************************************************************************)

22
open Serializable_builtin_t
23
open Serializable_j
24
open Signatures
25
open Common
26 27

module type PARAMS = sig
28 29 30 31
  val uuid : string
  val group : string
  val template : string
  val get_public_keys : unit -> string array option
32
  val get_threshold : unit -> string option
33 34 35 36 37 38 39
end

module type S = sig
  val mkelection : unit -> string
end

module type PARSED_PARAMS = sig
40
  val uuid : uuid
41
  val template : template
42
  module G : GROUP
43
  val get_public_keys : unit -> G.t trustee_public_key array option
44
  val get_threshold : unit -> G.t threshold_parameters option
45 46
end

47 48 49
let parse_params p =
  let module P = (val p : PARAMS) in
  let module R = struct
50
    let uuid = uuid_of_raw_string P.uuid
51 52 53 54 55 56
    let template = template_of_string P.template
    module G = (val Group.of_string P.group : GROUP)
    let get_public_keys () =
      match P.get_public_keys () with
      | None -> None
      | Some xs -> Some (Array.map (trustee_public_key_of_string G.read) xs)
57 58 59 60
    let get_threshold () =
      match P.get_threshold () with
      | None -> None
      | Some t -> Some (threshold_parameters_of_string G.read t)
61 62 63 64
  end
  in (module R : PARSED_PARAMS)

module Make (P : PARSED_PARAMS) : S = struct
65
  open P
66 67 68

  (* Setup trustees *)

69 70 71 72 73 74 75 76
  let y =
    match get_threshold () with
    | None ->
       let public_keys =
         match get_public_keys () with
         | Some keys -> keys
         | None -> failwith "trustee keys are missing"
       in
77
       let module K = Trustees.MakeSimple (G) (DirectRandom) in
78 79
       K.combine public_keys
    | Some t ->
80 81 82
       let module P = Trustees.MakePKI (G) (DirectRandom) in
       let module C = Trustees.MakeChannels (G) (DirectRandom) (P) in
       let module K = Trustees.MakePedersen (G) (DirectRandom) (P) (C) in
83
       K.combine t
84 85 86

  (* Setup election *)

87 88 89
  let params = {
    e_description = template.t_description;
    e_name = template.t_name;
90
    e_public_key = {wpk_group = G.group; wpk_y = y};
91 92 93
    e_questions = template.t_questions;
    e_uuid = uuid;
  }
94

95
  (* Generate and serialize election.json *)
96

97
  let mkelection () =
98
    string_of_params (write_wrapped_pubkey G.write_group G.write) params
99 100 101

end

102 103 104 105
let make params =
  let module P = (val parse_params params : PARSED_PARAMS) in
  let module R = Make (P) in
  (module R : S)