From 5056e063804598a49f3e344c8e2fc1a11713b7e2 Mon Sep 17 00:00:00 2001 From: "Mayeul@Zama" <69792125+mayeul-zama@users.noreply.github.com> Date: Wed, 16 Oct 2024 17:27:35 +0200 Subject: [PATCH] chore(fhe_strings): add encryption-decryption test --- tfhe/examples/fhe_strings/assert_functions/mod.rs | 11 +++++++++++ .../fhe_strings/assert_functions/test_vectors.rs | 9 +++++++++ tfhe/examples/fhe_strings/main.rs | 2 ++ 3 files changed, 22 insertions(+) diff --git a/tfhe/examples/fhe_strings/assert_functions/mod.rs b/tfhe/examples/fhe_strings/assert_functions/mod.rs index 3aac49296..bc5ce9411 100644 --- a/tfhe/examples/fhe_strings/assert_functions/mod.rs +++ b/tfhe/examples/fhe_strings/assert_functions/mod.rs @@ -125,6 +125,17 @@ impl Keys { assert_eq!(dec, expected); } + pub fn assert_encrypt_decrypt(&self, str: &str, str_pad: Option) { + let enc_str = FheString::new(&self.ck, str, str_pad); + + let dec = self.ck.decrypt_ascii(&enc_str); + + println!("\n\x1b[1mEncrypt/Decrypt:\x1b[0m"); + result_message(str, str, &dec, Duration::from_nanos(0)); + + assert_eq!(str, &dec); + } + pub fn assert_contains( &self, str: &str, diff --git a/tfhe/examples/fhe_strings/assert_functions/test_vectors.rs b/tfhe/examples/fhe_strings/assert_functions/test_vectors.rs index 2a2ba8eb3..f7bc6d6f9 100644 --- a/tfhe/examples/fhe_strings/assert_functions/test_vectors.rs +++ b/tfhe/examples/fhe_strings/assert_functions/test_vectors.rs @@ -140,6 +140,15 @@ fn test_is_empty() { } } +#[test] +fn test_encrypt_decrypt() { + let keys = Keys::new(); + + for (str, str_pad) in TEST_CASES_MATCH { + keys.assert_encrypt_decrypt(str, Some(str_pad)); + } +} + #[test] fn test_contains() { let keys = Keys::new(); diff --git a/tfhe/examples/fhe_strings/main.rs b/tfhe/examples/fhe_strings/main.rs index d431068ab..2376c797c 100644 --- a/tfhe/examples/fhe_strings/main.rs +++ b/tfhe/examples/fhe_strings/main.rs @@ -105,6 +105,8 @@ fn main() { keys.assert_len(str, str_pad); keys.assert_is_empty(str, str_pad); + keys.assert_encrypt_decrypt(str, str_pad); + keys.assert_contains(str, str_pad, pat, pat_pad); keys.assert_ends_with(str, str_pad, pat, pat_pad); keys.assert_starts_with(str, str_pad, pat, pat_pad);