Commit f441fb36 authored by Stephane Glondu's avatar Stephane Glondu

Move DirectRandom to Common

parent 6369d8a9
......@@ -19,6 +19,8 @@
(* <http://www.gnu.org/licenses/>. *)
(**************************************************************************)
open Platform
module Array = struct
include Array
......@@ -175,3 +177,19 @@ let compare_b64 a b =
in loop 0
module SMap = Map.Make(String)
(** Direct random monad *)
module DirectRandom = struct
type 'a t = 'a
let return x = x
let bind x f = f x
let fail e = raise e
let prng = lazy (pseudo_rng (random_string secure_rng 16))
let random q =
let size = Z.bit_length q / 8 + 1 in
let r = random_string (Lazy.force prng) size in
Z.(of_bits r mod q)
end
......@@ -19,6 +19,8 @@
(* <http://www.gnu.org/licenses/>. *)
(**************************************************************************)
open Signatures
module Array : sig
include module type of Array
val exists : ('a -> bool) -> 'a array -> bool
......@@ -55,3 +57,5 @@ val save_to : string -> (Bi_outbuf.t -> 'a -> unit) -> 'a -> unit
val compare_b64 : string -> string -> int
module SMap : Map.S with type key = string
module DirectRandom : RANDOM with type 'a t = 'a
......@@ -33,22 +33,6 @@ let question_length q =
| Some true -> 1
| _ -> 0
(** Direct random monad *)
module DirectRandom = struct
type 'a t = 'a
let return x = x
let bind x f = f x
let fail e = raise e
let prng = lazy (pseudo_rng (random_string secure_rng 16))
let random q =
let size = Z.bit_length q / 8 + 1 in
let r = random_string (Lazy.force prng) size in
Z.(of_bits r mod q)
end
(** Homomorphic elections *)
module MakeElection (G : GROUP) (M : RANDOM) = struct
......
......@@ -21,27 +21,11 @@
(** Election primitives *)
open Platform
open Serializable_t
open Signatures
val question_length : question -> int
module DirectRandom : sig
(** {2 Monadic definitions} *)
include Signatures.MONAD with type 'a t = 'a
(** {2 Random number generation} *)
val random : Z.t -> Z.t t
(** [random q] returns a random number modulo [q]. It uses a secure
random number generator lazily initialized by a 128-bit seed
shared by all instances. *)
end
(** Direct random monad *)
module MakeElection (G : GROUP) (M : RANDOM) :
ELECTION with type elt = G.t and type 'a m = 'a M.t
(** Implementation of {!Signatures.ELECTION}. *)
......@@ -178,10 +178,9 @@ module Ttkeygen : CMDLINER_MODULE = struct
in
let group = get_mandatory_opt "--group" group |> string_of_file in
let module G = (val Group.of_string group : GROUP) in
let module M = Election.DirectRandom in
let module P = Trustees.MakePKI (G) (M) in
let module C = Trustees.MakeChannels (G) (M) (P) in
let module T = Trustees.MakePedersen (G) (M) (P) (C) in
let module P = Trustees.MakePKI (G) (DirectRandom) in
let module C = Trustees.MakeChannels (G) (DirectRandom) (P) in
let module T = Trustees.MakePedersen (G) (DirectRandom) (P) (C) in
match step with
| 1 ->
let key, cert = T.step1 () in
......
......@@ -52,7 +52,7 @@ let parse_params p =
module Make (P : PARSED_PARAMS) : S = struct
open P
module CG = Credential.MakeGenerate (Election.DirectRandom)
module CG = Credential.MakeGenerate (DirectRandom)
module CD = Credential.MakeDerive (G)
let derive x =
......
......@@ -59,14 +59,13 @@ let parse_params p =
module Make (P : PARSED_PARAMS) : S = struct
open P
module M = Election.DirectRandom
module E = Election.MakeElection(G)(M);;
module E = Election.MakeElection (G) (DirectRandom)
module KG = Trustees.MakeSimpleDistKeyGen (G) (M)
module KG = Trustees.MakeSimpleDistKeyGen (G) (DirectRandom)
module P = Trustees.MakePKI (G) (M)
module C = Trustees.MakeChannels (G) (M) (P)
module KP = Trustees.MakePedersen (G) (M) (P) (C)
module P = Trustees.MakePKI (G) (DirectRandom)
module C = Trustees.MakeChannels (G) (DirectRandom) (P)
module KP = Trustees.MakePedersen (G) (DirectRandom) (P) (C)
(* Load and check trustee keys, if present *)
......
......@@ -21,6 +21,7 @@
open Platform
open Serializable_j
open Common
open Tool_js_common
let election = ref None
......@@ -67,8 +68,7 @@ let compute_partial_decryption _ =
Js.Opt.option !election >>= fun e ->
let election = Group.election_params_of_string e in
let module P = (val election) in
let module M = Election.DirectRandom in
let module E = Election.MakeElection (P.G) (M) in
let module E = Election.MakeElection (P.G) (DirectRandom) in
Js.Opt.option !encrypted_tally >>= fun e ->
let encrypted_tally = encrypted_tally_of_string P.G.read e in
document##getElementById (Js.string "private_key") >>= fun e ->
......
......@@ -66,8 +66,6 @@ let parse_params p =
module Make (P : PARSED_PARAMS) : S = struct
open P
module M = Election.DirectRandom
(* Setup trustees *)
let y =
......@@ -78,12 +76,12 @@ module Make (P : PARSED_PARAMS) : S = struct
| Some keys -> keys
| None -> failwith "trustee keys are missing"
in
let module K = Trustees.MakeSimpleDistKeyGen (G) (M) in
let module K = Trustees.MakeSimpleDistKeyGen (G) (DirectRandom) in
K.combine public_keys
| Some t ->
let module P = Trustees.MakePKI (G) (M) in
let module C = Trustees.MakeChannels (G) (M) (P) in
let module K = Trustees.MakePedersen (G) (M) (P) (C) in
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
K.combine t
(* Setup election *)
......
......@@ -47,11 +47,9 @@ let parse_params p =
module Make (P : PARSED_PARAMS) : S = struct
open P
module M = Election.DirectRandom
(* Generate key *)
module KG = Trustees.MakeSimpleDistKeyGen(G)(M);;
module KG = Trustees.MakeSimpleDistKeyGen (G) (DirectRandom)
type keypair = { id : string; priv : string; pub : string }
......
......@@ -21,6 +21,7 @@
open Signatures
open Serializable_j
open Common
let stream_to_list s =
let res = ref [] in
......@@ -112,12 +113,11 @@ let verifydiff dir1 dir2 =
(* the public keys / threshold parameters must be valid *)
let module ED = (val Group.election_params_of_string election) in
let open ED in
let module M = Election.DirectRandom in
let module E = Election.MakeElection (G) (M) in
let module E = Election.MakeElection (G) (DirectRandom) in
let y =
match threshold with
| None ->
let module K = Trustees.MakeSimpleDistKeyGen (G) (M) in
let module K = Trustees.MakeSimpleDistKeyGen (G) (DirectRandom) in
let pks = match pks with
| None -> raise (VerifydiffError MissingPublicKeys)
| Some pks -> List.map (trustee_public_key_of_string G.read) pks
......@@ -127,9 +127,9 @@ let verifydiff dir1 dir2 =
K.combine (Array.of_list pks)
| Some t ->
let t = threshold_parameters_of_string G.read t in
let module P = Trustees.MakePKI (G) (M) in
let module C = Trustees.MakeChannels (G) (M) (P) in
let module K = Trustees.MakePedersen (G) (M) (P) (C) in
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
if not (K.check t) then
raise (VerifydiffError InvalidThreshold);
K.combine t
......
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