Commit fcdd3ddb authored by Stephane Glondu's avatar Stephane Glondu

Move finite field arithmetic to its own module

Though small, this code really does not belong to Election, which does
not (and should not) depend on a specific group implementation.
parent c2975979
Serializable_t
Signatures
Group_field
Election
......@@ -39,7 +39,7 @@ let do_derive uuid x =
transform_string (Hexa.encode ())
module type PARAMS = sig
val group : (module Election.FF_GROUP)
val group : (module Group_field.GROUP)
val uuid : Uuidm.t
val count : int option ref
val file : string option ref
......@@ -86,7 +86,7 @@ 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;
Election.finite_field r
Group_field.make r
let uuid = match !uuid with
| None ->
......@@ -101,7 +101,7 @@ module GetParams (X : EMPTY) : PARAMS = struct
end
module RunCredgen (P : PARAMS) (G : Election.FF_GROUP) = struct
module RunCredgen (P : PARAMS) (G : Group_field.GROUP) = struct
open P
(* Some helpers *)
......@@ -235,6 +235,6 @@ let derive = do_derive
let main () =
let module P = GetParams (struct end) in
let module G = (val P.group : Election.FF_GROUP) in
let module G = (val P.group : Group_field.GROUP) in
let module X = RunCredgen (P) (G) in
()
......@@ -132,7 +132,7 @@ module GetParams (X : EMPTY) : PARAMS = struct
end
module RunTool (G : Election.FF_GROUP) (P : PARAMS) = struct
module RunTool (G : Group_field.GROUP) (P : PARAMS) = struct
open P
module M = Election.MakeSimpleMonad(G)
......@@ -303,6 +303,6 @@ end
let main () =
let module P = GetParams(struct end) in
let module G = (val Election.finite_field P.group : Election.FF_GROUP) in
let module G = (val Group_field.make P.group : Group_field.GROUP) in
let module X = RunTool (G) (P) in
()
......@@ -24,7 +24,7 @@ open Serializable_t
open Signatures
module type PARAMS = sig
val group : (module Election.FF_GROUP)
val group : (module Group_field.GROUP)
val uuid : Uuidm.t
val template : template
end
......@@ -63,7 +63,7 @@ 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;
Election.finite_field r
Group_field.make r
let uuid = match !uuid with
| None -> die "--uuid is missing"
......@@ -82,7 +82,7 @@ module GetParams (X : EMPTY) : PARAMS = struct
r
end
module MakeElection (G : Election.FF_GROUP) (P : PARAMS) = struct
module MakeElection (G : Group_field.GROUP) (P : PARAMS) = struct
(* Setup group *)
......@@ -132,6 +132,6 @@ end
let main () =
let module P = GetParams (struct end) in
let module G = (val P.group : Election.FF_GROUP) in
let module G = (val P.group : Group_field.GROUP) in
let module X = MakeElection (G) (P) in
()
......@@ -24,7 +24,7 @@ open Serializable_t
open Signatures
module type PARAMS = sig
val group : (module Election.FF_GROUP)
val group : (module Group_field.GROUP)
end
module GetParams (X : EMPTY) : PARAMS = struct
......@@ -57,10 +57,10 @@ 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;
Election.finite_field r
Group_field.make r
end
module RunTrusteeKeygen (G : Election.FF_GROUP) = struct
module RunTrusteeKeygen (G : Group_field.GROUP) = struct
(* Setup group *)
......@@ -114,6 +114,6 @@ end
let main () =
let module P = GetParams (struct end) in
let module G = (val P.group : Election.FF_GROUP) in
let module G = (val P.group : Group_field.GROUP) in
let module X = RunTrusteeKeygen (G) in
()
......@@ -27,59 +27,6 @@ open Signatures
let check_modulo p x = Z.(geq x zero && lt x p)
let map_and_concat_with_commas f xs =
let n = Array.length xs in
let res = Buffer.create (n * 1024) in
for i = 0 to n-1 do
Buffer.add_string res (f xs.(i));
Buffer.add_char res ',';
done;
let size = Buffer.length res - 1 in
if size > 0 then Buffer.sub res 0 size else ""
(** Finite field arithmetic *)
let check_finite_field {p; q; g} =
Z.probab_prime p 20 > 0 &&
Z.probab_prime q 20 > 0 &&
check_modulo p g &&
check_modulo p q &&
Z.(powm g q p =% one)
module type FF_GROUP = GROUP
with type t = Z.t
and type group = ff_params
let unsafe_finite_field group =
let {p; q; g} = group in
let module G = struct
open Z
type t = Z.t
let p = p
let q = q
let one = Z.one
let g = g
let ( *~ ) a b = a * b mod p
let ( **~ ) a b = powm a b p
let invert x = Z.invert x p
let ( =~ ) = Z.equal
let check x = check_modulo p x && x **~ q =~ one
let to_string = Z.to_string
let hash prefix xs =
let x = prefix ^ (map_and_concat_with_commas Z.to_string xs) in
let z = Z.of_string_base 16 (sha256_hex x) in
Z.(z mod q)
let compare = Z.compare
type group = ff_params
let group = group
end in (module G : FF_GROUP)
let finite_field group =
if check_finite_field group then
unsafe_finite_field group
else
invalid_arg "incorrect finite field parameters"
(** Parameters *)
let check_election_public_key (type t) g e =
......
......@@ -23,18 +23,6 @@
open Signatures
module type FF_GROUP = GROUP
with type t = Z.t
and type group = Serializable_t.ff_params
(** Multiplicative subgroup of a finite field. *)
val finite_field : Serializable_t.ff_params -> (module FF_GROUP)
(** [finite_field params] builds the multiplicative subgroup of
F[params.p], generated by [params.g], of order [params.q]. *)
val check_finite_field : Serializable_t.ff_params -> bool
(** Check consistency of finite field parameters. *)
val check_election_public_key : (module GROUP with type t = 'a) ->
'a election -> bool
(** Check election public key. *)
......
(**************************************************************************)
(* 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_j
(** Helper functions *)
let check_modulo p x = Z.(geq x zero && lt x p)
let map_and_concat_with_commas f xs =
let n = Array.length xs in
let res = Buffer.create (n * 1024) in
for i = 0 to n-1 do
Buffer.add_string res (f xs.(i));
Buffer.add_char res ',';
done;
let size = Buffer.length res - 1 in
if size > 0 then Buffer.sub res 0 size else ""
(** Finite field arithmetic *)
let check_params {p; q; g} =
Z.probab_prime p 20 > 0 &&
Z.probab_prime q 20 > 0 &&
check_modulo p g &&
check_modulo p q &&
Z.(powm g q p =% one)
module type GROUP = Signatures.GROUP
with type t = Z.t
and type group = ff_params
let unsafe_make group =
let {p; q; g} = group in
let module G = struct
open Z
type t = Z.t
let p = p
let q = q
let one = Z.one
let g = g
let ( *~ ) a b = a * b mod p
let ( **~ ) a b = powm a b p
let invert x = Z.invert x p
let ( =~ ) = Z.equal
let check x = check_modulo p x && x **~ q =~ one
let to_string = Z.to_string
let hash prefix xs =
let x = prefix ^ (map_and_concat_with_commas Z.to_string xs) in
let z = Z.of_string_base 16 (sha256_hex x) in
Z.(z mod q)
let compare = Z.compare
type group = ff_params
let group = group
end in (module G : GROUP)
let make group =
if check_params group then unsafe_make group
else invalid_arg "incorrect finite field parameters"
(**************************************************************************)
(* 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/>. *)
(**************************************************************************)
(** Finite field arithmetic *)
module type GROUP = Signatures.GROUP
with type t = Z.t
and type group = Serializable_t.ff_params
(** Multiplicative subgroup of a finite field. *)
val check_params : Serializable_t.ff_params -> bool
(** Check consistency of finite field parameters. *)
val make : Serializable_t.ff_params -> (module GROUP)
(** [finite_field params] builds the multiplicative subgroup of
F[params.p], generated by [params.g], of order [params.q]. It
checks the consistency of the parameters. *)
......@@ -2,4 +2,5 @@ Util
Serializable_builtin_j
Serializable_t
Serializable_j
Group_field
Election
......@@ -8,6 +8,7 @@ Auth_dummy
Auth_password
Auth_cas
Web_election
Group_field
Election
Web_templates
Web_site
......
......@@ -77,7 +77,7 @@ let make config =
module Auth = Web_auth.Make (N)
module W : WEB_ELECTION = struct
module G = (val Election.finite_field group : Election.FF_GROUP)
module G = (val Group_field.make group : Group_field.GROUP)
module M = MakeLwtRandom(struct let rng = make_rng () end)
module E = Election.MakeElection(G)(M)
module H = Auth.Handlers
......
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