Commit 3e3d7268 authored by Stephane Glondu's avatar Stephane Glondu
Browse files

More JSBN bindings

parent d7b83c7d
......@@ -52,16 +52,20 @@ module Z = struct
let ( - ) x y = meth_call x "subtract" [| y |]
let ( * ) x y = meth_call x "multiply" [| y |]
let ( mod ) x y = meth_call x "mod" [| y |]
let erem x y = assert false
let to_int x = meth_call x "intValue" [| |]
let to_string x = meth_call x "toString" [| |] |> Js.to_string
let compare x y = assert false
let ( =% ) x y = assert false
let geq x y = assert false
let lt x y = assert false
let compare x y = meth_call x "compareTo" [| y |]
let ( =% ) x y = compare x y = 0
let geq 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 invert x m = meth_call x "modInverse" [| m |]
let erem x y =
let r = x mod y in
if lt r zero then r + y else r
let probab_prime x n =
meth_call x "isProbablePrime" [| |] |>
Js.float_of_number |> int_of_float
......
......@@ -87,9 +87,14 @@ module Tests = struct
check "int_roundtrip_b" (int_roundtrip b d);
let m = Z.of_int 181944121 in
check "mod" (fun () -> Z.to_int Z.((c * d) mod m) = 30881634);
check "erem" (fun () -> Z.to_int Z.((zero - c * d) mod m) = 151062487);
check "powm" (fun () -> Z.to_int (Z.powm c d m) = 81171525);
check "invert" (fun () -> Z.to_int (Z.invert c m) = 54455411);
check "prime" (fun () -> Z.probab_prime m 5 > 0);
check "eq" (fun () -> Z.(c =% c));
check "neq" (fun () -> Z.(not (c =% d)));
check "geq" (fun () -> Z.geq c d);
check "lt" (fun () -> Z.lt d c);
Printf.ksprintf alert "%d tests were successful!" !ntests
let cmds = ["do_unit_tests", unit_tests]
......
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