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

In Z, replace size with bit_length, complete JSBN bindings

parent 3e3d7268
...@@ -50,7 +50,7 @@ module MakeSimpleMonad (G : GROUP) = struct ...@@ -50,7 +50,7 @@ module MakeSimpleMonad (G : GROUP) = struct
let fail e = raise e let fail e = raise e
let random q = let random q =
let size = Z.size q * Sys.word_size / 8 in let size = Z.bit_length q / 8 + 1 in
fun () -> fun () ->
let r = random_string (Lazy.force prng) size in let r = random_string (Lazy.force prng) size in
Z.(of_bits r mod q) Z.(of_bits r mod q)
......
...@@ -52,7 +52,7 @@ module Z : sig ...@@ -52,7 +52,7 @@ module Z : sig
val powm : t -> t -> t -> t val powm : t -> t -> t -> t
val invert : t -> t -> t val invert : t -> t -> t
val probab_prime : t -> int -> int val probab_prime : t -> int -> int
val size : t -> int val bit_length : t -> int
val of_bits : string -> t val of_bits : string -> t
end end
......
...@@ -61,6 +61,7 @@ module Z = struct ...@@ -61,6 +61,7 @@ module Z = struct
let lt x y = compare x y < 0 let lt x y = compare x y < 0
let powm x y m = meth_call x "modPow" [| y; m |] let powm x y m = meth_call x "modPow" [| y; m |]
let invert x m = meth_call x "modInverse" [| m |] let invert x m = meth_call x "modInverse" [| m |]
let bit_length x = meth_call x "bitLength" [| |]
let erem x y = let erem x y =
let r = x mod y in let r = x mod y in
...@@ -70,8 +71,15 @@ module Z = struct ...@@ -70,8 +71,15 @@ module Z = struct
meth_call x "isProbablePrime" [| |] |> meth_call x "isProbablePrime" [| |] |>
Js.float_of_number |> int_of_float Js.float_of_number |> int_of_float
let size x = assert false let z256 = of_int 256
let of_bits x = assert false
let of_bits x =
let n = String.length x in
let rec loop res i =
if i >= 0
then loop (res * z256 + of_int (int_of_char x.[i])) (pred i)
else res
in loop zero (pred n)
end end
type datetime type datetime
......
...@@ -92,6 +92,7 @@ let random_string = Cryptokit.Random.string ...@@ -92,6 +92,7 @@ let random_string = Cryptokit.Random.string
module Z = struct module Z = struct
include Z include Z
let ( =% ) = equal let ( =% ) = equal
let bit_length x = Pervasives.(String.length (to_bits x) * 8)
end end
open CalendarLib open CalendarLib
......
...@@ -95,6 +95,11 @@ module Tests = struct ...@@ -95,6 +95,11 @@ module Tests = struct
check "neq" (fun () -> Z.(not (c =% d))); check "neq" (fun () -> Z.(not (c =% d)));
check "geq" (fun () -> Z.geq c d); check "geq" (fun () -> Z.geq c d);
check "lt" (fun () -> Z.lt d c); check "lt" (fun () -> Z.lt d c);
let i = Z.of_string "272660753928370030481696309961224617984" in
check "bit_length" (fun () -> Z.bit_length i = 128);
let j = Z.of_bits "\x81\xab\xd3\xed\x0b\x19\x2e\x40\x7a\xca" in
let k = Z.of_string "956173156978067279948673" in
check "of_bits" (fun () -> Z.(j =% k));
Printf.ksprintf alert "%d tests were successful!" !ntests Printf.ksprintf alert "%d tests were successful!" !ntests
let cmds = ["do_unit_tests", unit_tests] let cmds = ["do_unit_tests", unit_tests]
......
...@@ -61,7 +61,7 @@ module MakeLwtRandom (X : LWT_RNG) = struct ...@@ -61,7 +61,7 @@ module MakeLwtRandom (X : LWT_RNG) = struct
let fail = Lwt.fail let fail = Lwt.fail
let random q = let random q =
let size = Z.size q * Sys.word_size / 8 in let size = Z.bit_length q / 8 + 1 in
lwt rng = X.rng in lwt rng = X.rng in
let r = random_string rng size in let r = random_string rng size in
return Z.(of_bits r mod q) return Z.(of_bits r mod q)
......
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