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 24, 2024
1 parent faf6d3e commit f2b0199
Show file tree
Hide file tree
Showing 16 changed files with 320 additions and 319 deletions.
57 changes: 27 additions & 30 deletions tfhe/src/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::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
2 changes: 2 additions & 0 deletions tfhe/src/strings/assert_functions/test_vectors.rs
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/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
1 change: 1 addition & 0 deletions tfhe/src/strings/client_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ impl EncU16 {
}

impl ClientKey {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
key: FheClientKey::new(PARAM_MESSAGE_2_CARRY_2),
Expand Down
3 changes: 2 additions & 1 deletion tfhe/src/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 }
}
}
28 changes: 15 additions & 13 deletions tfhe/src/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 Expand Up @@ -59,8 +61,8 @@ impl ServerKey {
/// let (ck, sk) = gen_keys();
/// let (s1, s2) = ("hello", "hello");
///
/// let enc_s1 = FheString::new(&ck, &s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, &s2, None));
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.eq(&enc_s1, &enc_s2);
/// let are_equal = ck.key().decrypt_bool(&result);
Expand Down Expand Up @@ -113,8 +115,8 @@ impl ServerKey {
/// let (ck, sk) = gen_keys();
/// let (s1, s2) = ("hello", "world");
///
/// let enc_s1 = FheString::new(&ck, &s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, &s2, None));
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = GenericPattern::Enc(FheString::new(&ck, s2, None));
///
/// let result = sk.ne(&enc_s1, &enc_s2);
/// let are_not_equal = ck.key().decrypt_bool(&result);
Expand All @@ -140,8 +142,8 @@ impl ServerKey {
/// let (ck, sk) = gen_keys();
/// let (s1, s2) = ("apple", "banana");
///
/// let enc_s1 = FheString::new(&ck, &s1, None);
/// let enc_s2 = FheString::new(&ck, &s2, None);
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = FheString::new(&ck, s2, None);
///
/// let result = sk.lt(&enc_s1, &enc_s2);
/// let is_lt = ck.key().decrypt_bool(&result);
Expand Down Expand Up @@ -170,8 +172,8 @@ impl ServerKey {
/// let (ck, sk) = gen_keys();
/// let (s1, s2) = ("banana", "apple");
///
/// let enc_s1 = FheString::new(&ck, &s1, None);
/// let enc_s2 = FheString::new(&ck, &s2, None);
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = FheString::new(&ck, s2, None);
///
/// let result = sk.gt(&enc_s1, &enc_s2);
/// let is_gt = ck.key().decrypt_bool(&result);
Expand Down Expand Up @@ -201,8 +203,8 @@ impl ServerKey {
/// let (ck, sk) = gen_keys();
/// let (s1, s2) = ("apple", "banana");
///
/// let enc_s1 = FheString::new(&ck, &s1, None);
/// let enc_s2 = FheString::new(&ck, &s2, None);
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = FheString::new(&ck, s2, None);
///
/// let result = sk.le(&enc_s1, &enc_s2);
/// let is_le = ck.key().decrypt_bool(&result);
Expand Down Expand Up @@ -232,8 +234,8 @@ impl ServerKey {
/// let (ck, sk) = gen_keys();
/// let (s1, s2) = ("banana", "apple");
///
/// let enc_s1 = FheString::new(&ck, &s1, None);
/// let enc_s2 = FheString::new(&ck, &s2, None);
/// let enc_s1 = FheString::new(&ck, s1, None);
/// let enc_s2 = FheString::new(&ck, s2, None);
///
/// let result = sk.ge(&enc_s1, &enc_s2);
/// let is_ge = ck.key().decrypt_bool(&result);
Expand Down
10 changes: 6 additions & 4 deletions tfhe/src/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::integer::bigint::static_unsigned::StaticUnsignedBigInt;
use crate::integer::prelude::*;
use crate::integer::{BooleanBlock, RadixCiphertext, ServerKey as FheServerKey};
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
Loading

0 comments on commit f2b0199

Please sign in to comment.