Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
Signed-off-by: Athish Pranav D <[email protected]>
  • Loading branch information
Athishpranav2003 committed Aug 22, 2024
1 parent 0dfb98d commit 8a72db8
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 1 deletion.
2 changes: 1 addition & 1 deletion tests/dune
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
(libraries test_common mirage-crypto-pk mirage-crypto-rng.unix randomconv
ounit2)
(package mirage-crypto-pk)
(modules test_numeric test_dh test_dsa test_rsa test_pk_runner))
(modules test_numeric test_dh test_dsa test_rsa test_paillier test_pk_runner))

(test
(name test_entropy_collection)
Expand Down
62 changes: 62 additions & 0 deletions tests/test_paillier.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
open OUnit2

(* open Mirage_crypto.Uncommon *)
open Mirage_crypto_pk

open Test_common

let vz = Z.of_string_base 16

module Null = struct

type g = string ref

let block = 1

let create ?time:_ () = ref ""

let generate_into ~g buf ~off n =
try
Bytes.blit_string !g 0 buf off n;
g := String.sub !g n (String.length !g - n)
with Invalid_argument _ -> raise Mirage_crypto_rng.Unseeded_generator

let reseed ~g buf = g := !g ^ buf

let seeded ~g = String.length !g > 0

let accumulate ~g _source = `Acc (reseed ~g)

let pools = 0
end

let random_is seed =
Mirage_crypto_rng.create ~seed:seed (module Null)

let gen_paillier ~bits =
let key = Paillier.(generate ~bits ()) in
assert_equal
~msg:Printf.(sprintf "key size not %d bits" bits)
bits Paillier.(priv_bits (snd key)) ;
key

let paillier_selftest ~bits n =
"selftest" >:: times ~n @@ fun _ ->
let msg = Z.(~$100) in
let key = gen_paillier ~bits in
let enc = Paillier.(encrypt ~pub_key:(fst key) ~msg ()) in
let dec = Paillier.(decrypt ~priv_key:(snd key) ~c:enc) in

assert_equal
~msg:Printf.(sprintf "failed decryption with")
msg dec


let suite = [
"Paillier" >::: [
paillier_selftest ~bits:89 100 ;
paillier_selftest ~bits:131 100 ;
paillier_selftest ~bits:1024 10 ;
paillier_selftest ~bits:2048 10 ;
] ;
]
1 change: 1 addition & 0 deletions tests/test_pk_runner.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ let suite =
"DHE" >::: Test_dh.suite;
"DSA" >::: Test_dsa.suite;
"RSA" >::: Test_rsa.suite;
"Paillier" >::: Test_paillier.suite;
]

let () =
Expand Down

0 comments on commit 8a72db8

Please sign in to comment.