Skip to content

Commit

Permalink
fix(strings): fix clippy lints
Browse files Browse the repository at this point in the history
  • Loading branch information
mayeul-zama committed Oct 23, 2024
1 parent c9b9caf commit d17263c
Show file tree
Hide file tree
Showing 14 changed files with 270 additions and 263 deletions.
57 changes: 27 additions & 30 deletions tfhe/src/high_level_api/strings/assert_functions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#[cfg(test)]
mod test_vectors;

use server_key::split_ascii_whitespace;

use super::*;
use crate::high_level_api::strings::ciphertext::{ClearString, GenericPattern};
use std::time::Duration;
Expand All @@ -11,12 +13,11 @@ where
{
println!(
"\x1b[1;32m--------------------------------\x1b[0m\n\
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
\x1b[1;32m--------------------------------\x1b[0m",
str, expected, dec, dur,
);
}

Expand All @@ -26,13 +27,12 @@ where
{
println!(
"\x1b[1;32m--------------------------------\x1b[0m\n\
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mPattern: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
\x1b[1;32;1mPattern: \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
\x1b[1;32m--------------------------------\x1b[0m",
str, pat, expected, dec, dur,
);
}

Expand All @@ -42,13 +42,12 @@ where
{
println!(
"\x1b[1;32m--------------------------------\x1b[0m\n\
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mPattern (clear): \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
\x1b[1;32;1mPattern (clear): \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
\x1b[1;32m--------------------------------\x1b[0m",
str, pat, expected, dec, dur,
);
}

Expand All @@ -58,13 +57,12 @@ where
{
println!(
"\x1b[1;32m--------------------------------\x1b[0m\n\
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mRhs: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
\x1b[1;32;1mRhs: \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
\x1b[1;32m--------------------------------\x1b[0m",
str, pat, expected, dec, dur,
);
}

Expand All @@ -74,13 +72,12 @@ where
{
println!(
"\x1b[1;32m--------------------------------\x1b[0m\n\
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mRhs (clear): \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
\x1b[1;32;1mRhs (clear): \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
\x1b[1;32m--------------------------------\x1b[0m",
str, pat, expected, dec, dur,
);
}

Expand Down Expand Up @@ -729,7 +726,7 @@ impl Keys {

// Call next enough times
let start = Instant::now();
let mut split_iter = self.sk.split_ascii_whitespace(&enc_str);
let mut split_iter = split_ascii_whitespace(&enc_str);
for _ in 0..expected.len() {
results.push(split_iter.next(&self.sk))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ const TEST_CASES_COMP: [(&str, u32); 15] = [
("foofoo44", 1),
];

#[allow(clippy::type_complexity)]
const TEST_CASES_SPLIT: [((&str, u32), (&str, u32)); 21] = [
// Empty strings and patterns with different paddings to test edge cases
(("", 0), ("", 0)),
Expand All @@ -88,6 +89,7 @@ const TEST_CASES_SPLIT: [((&str, u32), (&str, u32)); 21] = [
(("Ghirahim", 2), ("hi", 0)),
];

#[allow(clippy::type_complexity)]
const TEST_CASES_REPLACE: [((&str, u32), (&str, u32), (&str, u32)); 27] = [
// Empty string matches with different padding combinations
(("", 0), ("", 0), ("", 0)),
Expand Down
22 changes: 12 additions & 10 deletions tfhe/src/high_level_api/strings/ciphertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl ClearString {
assert!(str.is_ascii() && !str.contains('\0'));
assert!(str.len() <= N * 8);

ClearString { str }
Self { str }
}

pub fn str(&self) -> &str {
Expand All @@ -56,7 +56,7 @@ impl FheAsciiChar {
}

pub fn null(sk: &ServerKey) -> Self {
FheAsciiChar {
Self {
enc_char: sk.key().create_trivial_zero_radix(4),
}
}
Expand Down Expand Up @@ -113,7 +113,7 @@ impl FheString {

// Converts a `RadixCiphertext` to a `FheString`, building a `FheAsciiChar` for each 4 blocks.
// Panics if the uint doesn't have a number of blocks that is multiple of 4.
pub fn from_uint(uint: RadixCiphertext, padded: bool) -> FheString {
pub fn from_uint(uint: RadixCiphertext, padded: bool) -> Self {
let blocks_len = uint.blocks().len();
assert_eq!(blocks_len % 4, 0);

Expand All @@ -130,7 +130,7 @@ impl FheString {
ascii_vec.push(FheAsciiChar { enc_char: byte })
}

FheString {
Self {
enc_string: ascii_vec,
padded,
}
Expand Down Expand Up @@ -178,8 +178,8 @@ impl FheString {
self.len() == 0 || (self.is_padded() && self.len() == 1)
}

pub fn empty() -> FheString {
FheString {
pub fn empty() -> Self {
Self {
enc_string: vec![],
padded: false,
}
Expand All @@ -202,15 +202,17 @@ mod tests {
let enc = FheString::new(&ck, str, Some(7));

let uint = enc.to_uint(&sk);
let mut converted = FheString::from_uint(uint, false);
converted.set_is_padded(true);

let converted = FheString::from_uint(uint, true);

let dec = ck.decrypt_ascii(&converted);

assert_eq!(dec, str);

let uint_into = enc.into_uint(&sk);
let mut converted = FheString::from_uint(uint_into, false);
converted.set_is_padded(true);

let converted = FheString::from_uint(uint_into, true);

let dec = ck.decrypt_ascii(&converted);

assert_eq!(dec, str);
Expand Down
3 changes: 2 additions & 1 deletion tfhe/src/high_level_api/strings/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn test_all2() {
test_all("", None, "", None, "", None, "", None, 0, 0);
}

#[allow(clippy::too_many_arguments)]
pub fn test_all(
str: &str,
str_pad: Option<u32>,
Expand Down Expand Up @@ -90,6 +91,6 @@ impl Keys {
fn new() -> Self {
let (ck, sk) = gen_keys();

Keys { ck, sk }
Self { ck, sk }
}
}
4 changes: 3 additions & 1 deletion tfhe/src/high_level_api/strings/server_key/comp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ impl ServerKey {
if rhs_len == 0 || (rhs.is_padded() && rhs_len == 1) {
return match self.is_empty(lhs) {
FheStringIsEmpty::Padding(enc_val) => Some(enc_val),
_ => Some(self.key.create_trivial_boolean_block(false)),
FheStringIsEmpty::NoPadding(_) => {
Some(self.key.create_trivial_boolean_block(false))
}
};
}

Expand Down
10 changes: 6 additions & 4 deletions tfhe/src/high_level_api/strings/server_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ mod no_patterns;
mod pattern;
mod trim;

pub use trim::split_ascii_whitespace;

use crate::high_level_api::strings::ciphertext::{FheAsciiChar, FheString};
use crate::high_level_api::strings::client_key::ClientKey;
use crate::high_level_api::strings::N;
Expand Down Expand Up @@ -104,7 +106,7 @@ impl ServerKey {
let diff = str_block_len - pat_block_len;
self.key.trim_radix_blocks_lsb_assign(&mut uint_str, diff);
}
_ => (),
Ordering::Equal => (),
}

let clear_pat_uint = self.pad_cipher_and_cleartext_lsb(&mut uint_str, clear_pat);
Expand Down Expand Up @@ -181,7 +183,7 @@ impl ServerKey {
self.key
.extend_radix_with_trivial_zero_blocks_lsb_assign(rhs, diff);
}
_ => (),
Ordering::Equal => (),
}
}

Expand All @@ -198,7 +200,7 @@ impl ServerKey {
let diff = cipher_len - len;
self.key.trim_radix_blocks_msb_assign(cipher, diff);
}
_ => (),
Ordering::Equal => (),
}
}

Expand All @@ -215,7 +217,7 @@ impl ServerKey {
let diff = lhs_blocks - rhs_blocks;
self.key.trim_radix_blocks_lsb_assign(lhs, diff);
}
_ => (),
Ordering::Equal => (),
}
}

Expand Down
2 changes: 1 addition & 1 deletion tfhe/src/high_level_api/strings/server_key/no_patterns.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ impl ServerKey {
/// assert_eq!(repeated_enc, "hihihi");
/// ```
pub fn repeat(&self, str: &FheString, n: &UIntArg) -> FheString {
if let UIntArg::Clear(0) = n {
if matches!(n, UIntArg::Clear(0)) {
return FheString::empty();
}

Expand Down
17 changes: 9 additions & 8 deletions tfhe/src/high_level_api/strings/server_key/pattern/contains.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use crate::high_level_api::strings::ciphertext::{FheAsciiChar, FheString, GenericPattern};
use crate::high_level_api::strings::server_key::pattern::{CharIter, IsMatch};
use crate::high_level_api::strings::server_key::ServerKey;
use crate::integer::{BooleanBlock, IntegerRadixCiphertext, RadixCiphertext};
use itertools::Itertools;
use rayon::prelude::*;
use rayon::range::Iter;
use crate::integer::{BooleanBlock, IntegerRadixCiphertext, RadixCiphertext};

use super::{clear_ends_with_cases, contains_cases, ends_with_cases};

impl ServerKey {
// Compare pat with str, with pat shifted right (in relation to str) the number given by iter
Expand Down Expand Up @@ -110,16 +112,15 @@ impl ServerKey {
match self.length_checks(str, &trivial_or_enc_pat) {
IsMatch::Clear(val) => return self.key.create_trivial_boolean_block(val),
IsMatch::Cipher(val) => return val,
_ => (),
IsMatch::None => (),
}

let ignore_pat_pad = trivial_or_enc_pat.is_padded();

let null = (!str.is_padded() && trivial_or_enc_pat.is_padded())
.then_some(FheAsciiChar::null(self));

let (str_iter, pat_iter, iter) =
self.contains_cases(str, &trivial_or_enc_pat, null.as_ref());
let (str_iter, pat_iter, iter) = contains_cases(str, &trivial_or_enc_pat, null.as_ref());

match pat {
GenericPattern::Clear(pat) => {
Expand Down Expand Up @@ -170,7 +171,7 @@ impl ServerKey {
match self.length_checks(str, &trivial_or_enc_pat) {
IsMatch::Clear(val) => return self.key.create_trivial_boolean_block(val),
IsMatch::Cipher(val) => return val,
_ => (),
IsMatch::None => (),
}

if !trivial_or_enc_pat.is_padded() {
Expand Down Expand Up @@ -242,19 +243,19 @@ impl ServerKey {
match self.length_checks(str, &trivial_or_enc_pat) {
IsMatch::Clear(val) => return self.key.create_trivial_boolean_block(val),
IsMatch::Cipher(val) => return val,
_ => (),
IsMatch::None => (),
}

match pat {
GenericPattern::Clear(pat) => {
let (str_iter, clear_pat, iter) = self.clear_ends_with_cases(str, pat.str());
let (str_iter, clear_pat, iter) = clear_ends_with_cases(str, pat.str());

self.clear_compare_shifted((str_iter, &clear_pat), iter.into_par_iter())
}
GenericPattern::Enc(pat) => {
let null = (str.is_padded() ^ pat.is_padded()).then_some(FheAsciiChar::null(self));

let (str_iter, pat_iter, iter) = self.ends_with_cases(str, pat, null.as_ref());
let (str_iter, pat_iter, iter) = ends_with_cases(str, pat, null.as_ref());

self.compare_shifted((str_iter, pat_iter), iter.into_par_iter(), false)
}
Expand Down
Loading

0 comments on commit d17263c

Please sign in to comment.