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

More JSBN bindings

parent 59da2ae4
......@@ -40,26 +40,32 @@ module Z = struct
let zero = get lib "ZERO"
let one = get lib "ONE"
let of_int x = assert false
let of_string_base b x = new_obj lib
[|
x |> Js.string |> inject;
b |> float_of_int |> Js.number_of_float |> inject;
|]
let of_string x =
new_obj lib [| x |> Js.string |> inject |]
let of_string_base b x = assert false
let of_string x = of_string_base 10 x
let of_int x = x |> string_of_int |> of_string
let ( + ) x y = meth_call x "add" [| y |]
let ( - ) x y = meth_call x "subtract" [| y |]
let ( * ) x y = meth_call x "multiply" [| y |]
let ( mod ) x y = assert false
let ( mod ) x y = meth_call x "mod" [| y |]
let erem x y = assert false
let to_int x = 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 powm x y m = assert false
let invert x m = assert false
let probab_prime x n = assert false
let powm x y m = meth_call x "modPow" [| y; m |]
let invert x m = meth_call x "modInverse" [| m |]
let probab_prime x n =
meth_call x "isProbablePrime" [| |] |>
Js.float_of_number |> int_of_float
let size x = assert false
let of_bits x = assert false
end
......
......@@ -80,6 +80,16 @@ module Tests = struct
check "add" (operator Z.( + ) "44882170531862833234");
check "mul" (operator Z.( * ) "416966603126589360375328894595477783568");
check "sub" (operator Z.( - ) "-18615660588463118978");
let a = 132180439 and b = 41907500 in
let c = Z.of_int a and d = Z.of_int b in
let int_roundtrip a c () = a = Z.to_int c in
check "int_roundtrip_a" (int_roundtrip a c);
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 "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);
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