From 8a72db8a4c6116373d75c744040771028eabc7fc Mon Sep 17 00:00:00 2001 From: Athish Pranav D Date: Thu, 22 Aug 2024 20:26:41 +0530 Subject: [PATCH] Add tests Signed-off-by: Athish Pranav D --- tests/dune | 2 +- tests/test_paillier.ml | 62 +++++++++++++++++++++++++++++++++++++++++ tests/test_pk_runner.ml | 1 + 3 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 tests/test_paillier.ml diff --git a/tests/dune b/tests/dune index 32afebe7..4c09efe4 100644 --- a/tests/dune +++ b/tests/dune @@ -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) diff --git a/tests/test_paillier.ml b/tests/test_paillier.ml new file mode 100644 index 00000000..261cc632 --- /dev/null +++ b/tests/test_paillier.ml @@ -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 ; + ] ; +] diff --git a/tests/test_pk_runner.ml b/tests/test_pk_runner.ml index ae47e676..797e5845 100644 --- a/tests/test_pk_runner.ml +++ b/tests/test_pk_runner.ml @@ -6,6 +6,7 @@ let suite = "DHE" >::: Test_dh.suite; "DSA" >::: Test_dsa.suite; "RSA" >::: Test_rsa.suite; + "Paillier" >::: Test_paillier.suite; ] let () =