Skip to content

Commit d17263c

Browse files
committed
fix(strings): fix clippy lints
1 parent c9b9caf commit d17263c

File tree

14 files changed

+270
-263
lines changed

14 files changed

+270
-263
lines changed

tfhe/src/high_level_api/strings/assert_functions/mod.rs

Lines changed: 27 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#[cfg(test)]
22
mod test_vectors;
33

4+
use server_key::split_ascii_whitespace;
5+
46
use super::*;
57
use crate::high_level_api::strings::ciphertext::{ClearString, GenericPattern};
68
use std::time::Duration;
@@ -11,12 +13,11 @@ where
1113
{
1214
println!(
1315
"\x1b[1;32m--------------------------------\x1b[0m\n\
14-
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
15-
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
16-
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
17-
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
16+
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
17+
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
18+
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
19+
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
1820
\x1b[1;32m--------------------------------\x1b[0m",
19-
str, expected, dec, dur,
2021
);
2122
}
2223

@@ -26,13 +27,12 @@ where
2627
{
2728
println!(
2829
"\x1b[1;32m--------------------------------\x1b[0m\n\
29-
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
30-
\x1b[1;32;1mPattern: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
31-
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
32-
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
33-
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
30+
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
31+
\x1b[1;32;1mPattern: \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
32+
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
33+
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
34+
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
3435
\x1b[1;32m--------------------------------\x1b[0m",
35-
str, pat, expected, dec, dur,
3636
);
3737
}
3838

@@ -42,13 +42,12 @@ where
4242
{
4343
println!(
4444
"\x1b[1;32m--------------------------------\x1b[0m\n\
45-
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
46-
\x1b[1;32;1mPattern (clear): \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
47-
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
48-
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
49-
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
45+
\x1b[1;32;1mString: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
46+
\x1b[1;32;1mPattern (clear): \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
47+
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
48+
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
49+
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
5050
\x1b[1;32m--------------------------------\x1b[0m",
51-
str, pat, expected, dec, dur,
5251
);
5352
}
5453

@@ -58,13 +57,12 @@ where
5857
{
5958
println!(
6059
"\x1b[1;32m--------------------------------\x1b[0m\n\
61-
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
62-
\x1b[1;32;1mRhs: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
63-
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
64-
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
65-
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
60+
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
61+
\x1b[1;32;1mRhs: \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
62+
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
63+
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
64+
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
6665
\x1b[1;32m--------------------------------\x1b[0m",
67-
str, pat, expected, dec, dur,
6866
);
6967
}
7068

@@ -74,13 +72,12 @@ where
7472
{
7573
println!(
7674
"\x1b[1;32m--------------------------------\x1b[0m\n\
77-
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
78-
\x1b[1;32;1mRhs (clear): \x1b[0m\x1b[0;33m{:?}\x1b[0m\n\
79-
\x1b[1;32;1mClear API Result: \x1b[0m{:?}\n\
80-
\x1b[1;32;1mT-fhe API Result: \x1b[0m{:?}\n\
81-
\x1b[1;34mExecution Time: \x1b[0m{:?}\n\
75+
\x1b[1;32;1mLhs: \x1b[0m\x1b[0;33m{str:?}\x1b[0m\n\
76+
\x1b[1;32;1mRhs (clear): \x1b[0m\x1b[0;33m{pat:?}\x1b[0m\n\
77+
\x1b[1;32;1mClear API Result: \x1b[0m{expected:?}\n\
78+
\x1b[1;32;1mT-fhe API Result: \x1b[0m{dec:?}\n\
79+
\x1b[1;34mExecution Time: \x1b[0m{dur:?}\n\
8280
\x1b[1;32m--------------------------------\x1b[0m",
83-
str, pat, expected, dec, dur,
8481
);
8582
}
8683

@@ -729,7 +726,7 @@ impl Keys {
729726

730727
// Call next enough times
731728
let start = Instant::now();
732-
let mut split_iter = self.sk.split_ascii_whitespace(&enc_str);
729+
let mut split_iter = split_ascii_whitespace(&enc_str);
733730
for _ in 0..expected.len() {
734731
results.push(split_iter.next(&self.sk))
735732
}

tfhe/src/high_level_api/strings/assert_functions/test_vectors.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ const TEST_CASES_COMP: [(&str, u32); 15] = [
6262
("foofoo44", 1),
6363
];
6464

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

92+
#[allow(clippy::type_complexity)]
9193
const TEST_CASES_REPLACE: [((&str, u32), (&str, u32), (&str, u32)); 27] = [
9294
// Empty string matches with different padding combinations
9395
(("", 0), ("", 0), ("", 0)),

tfhe/src/high_level_api/strings/ciphertext.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl ClearString {
3232
assert!(str.is_ascii() && !str.contains('\0'));
3333
assert!(str.len() <= N * 8);
3434

35-
ClearString { str }
35+
Self { str }
3636
}
3737

3838
pub fn str(&self) -> &str {
@@ -56,7 +56,7 @@ impl FheAsciiChar {
5656
}
5757

5858
pub fn null(sk: &ServerKey) -> Self {
59-
FheAsciiChar {
59+
Self {
6060
enc_char: sk.key().create_trivial_zero_radix(4),
6161
}
6262
}
@@ -113,7 +113,7 @@ impl FheString {
113113

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

@@ -130,7 +130,7 @@ impl FheString {
130130
ascii_vec.push(FheAsciiChar { enc_char: byte })
131131
}
132132

133-
FheString {
133+
Self {
134134
enc_string: ascii_vec,
135135
padded,
136136
}
@@ -178,8 +178,8 @@ impl FheString {
178178
self.len() == 0 || (self.is_padded() && self.len() == 1)
179179
}
180180

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

204204
let uint = enc.to_uint(&sk);
205-
let mut converted = FheString::from_uint(uint, false);
206-
converted.set_is_padded(true);
205+
206+
let converted = FheString::from_uint(uint, true);
207+
207208
let dec = ck.decrypt_ascii(&converted);
208209

209210
assert_eq!(dec, str);
210211

211212
let uint_into = enc.into_uint(&sk);
212-
let mut converted = FheString::from_uint(uint_into, false);
213-
converted.set_is_padded(true);
213+
214+
let converted = FheString::from_uint(uint_into, true);
215+
214216
let dec = ck.decrypt_ascii(&converted);
215217

216218
assert_eq!(dec, str);

tfhe/src/high_level_api/strings/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ fn test_all2() {
2121
test_all("", None, "", None, "", None, "", None, 0, 0);
2222
}
2323

24+
#[allow(clippy::too_many_arguments)]
2425
pub fn test_all(
2526
str: &str,
2627
str_pad: Option<u32>,
@@ -90,6 +91,6 @@ impl Keys {
9091
fn new() -> Self {
9192
let (ck, sk) = gen_keys();
9293

93-
Keys { ck, sk }
94+
Self { ck, sk }
9495
}
9596
}

tfhe/src/high_level_api/strings/server_key/comp.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ impl ServerKey {
2323
if rhs_len == 0 || (rhs.is_padded() && rhs_len == 1) {
2424
return match self.is_empty(lhs) {
2525
FheStringIsEmpty::Padding(enc_val) => Some(enc_val),
26-
_ => Some(self.key.create_trivial_boolean_block(false)),
26+
FheStringIsEmpty::NoPadding(_) => {
27+
Some(self.key.create_trivial_boolean_block(false))
28+
}
2729
};
2830
}
2931

tfhe/src/high_level_api/strings/server_key/mod.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ mod no_patterns;
33
mod pattern;
44
mod trim;
55

6+
pub use trim::split_ascii_whitespace;
7+
68
use crate::high_level_api::strings::ciphertext::{FheAsciiChar, FheString};
79
use crate::high_level_api::strings::client_key::ClientKey;
810
use crate::high_level_api::strings::N;
@@ -104,7 +106,7 @@ impl ServerKey {
104106
let diff = str_block_len - pat_block_len;
105107
self.key.trim_radix_blocks_lsb_assign(&mut uint_str, diff);
106108
}
107-
_ => (),
109+
Ordering::Equal => (),
108110
}
109111

110112
let clear_pat_uint = self.pad_cipher_and_cleartext_lsb(&mut uint_str, clear_pat);
@@ -181,7 +183,7 @@ impl ServerKey {
181183
self.key
182184
.extend_radix_with_trivial_zero_blocks_lsb_assign(rhs, diff);
183185
}
184-
_ => (),
186+
Ordering::Equal => (),
185187
}
186188
}
187189

@@ -198,7 +200,7 @@ impl ServerKey {
198200
let diff = cipher_len - len;
199201
self.key.trim_radix_blocks_msb_assign(cipher, diff);
200202
}
201-
_ => (),
203+
Ordering::Equal => (),
202204
}
203205
}
204206

@@ -215,7 +217,7 @@ impl ServerKey {
215217
let diff = lhs_blocks - rhs_blocks;
216218
self.key.trim_radix_blocks_lsb_assign(lhs, diff);
217219
}
218-
_ => (),
220+
Ordering::Equal => (),
219221
}
220222
}
221223

tfhe/src/high_level_api/strings/server_key/no_patterns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ impl ServerKey {
342342
/// assert_eq!(repeated_enc, "hihihi");
343343
/// ```
344344
pub fn repeat(&self, str: &FheString, n: &UIntArg) -> FheString {
345-
if let UIntArg::Clear(0) = n {
345+
if matches!(n, UIntArg::Clear(0)) {
346346
return FheString::empty();
347347
}
348348

tfhe/src/high_level_api/strings/server_key/pattern/contains.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
use crate::high_level_api::strings::ciphertext::{FheAsciiChar, FheString, GenericPattern};
22
use crate::high_level_api::strings::server_key::pattern::{CharIter, IsMatch};
33
use crate::high_level_api::strings::server_key::ServerKey;
4+
use crate::integer::{BooleanBlock, IntegerRadixCiphertext, RadixCiphertext};
45
use itertools::Itertools;
56
use rayon::prelude::*;
67
use rayon::range::Iter;
7-
use crate::integer::{BooleanBlock, IntegerRadixCiphertext, RadixCiphertext};
8+
9+
use super::{clear_ends_with_cases, contains_cases, ends_with_cases};
810

911
impl ServerKey {
1012
// Compare pat with str, with pat shifted right (in relation to str) the number given by iter
@@ -110,16 +112,15 @@ impl ServerKey {
110112
match self.length_checks(str, &trivial_or_enc_pat) {
111113
IsMatch::Clear(val) => return self.key.create_trivial_boolean_block(val),
112114
IsMatch::Cipher(val) => return val,
113-
_ => (),
115+
IsMatch::None => (),
114116
}
115117

116118
let ignore_pat_pad = trivial_or_enc_pat.is_padded();
117119

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

121-
let (str_iter, pat_iter, iter) =
122-
self.contains_cases(str, &trivial_or_enc_pat, null.as_ref());
123+
let (str_iter, pat_iter, iter) = contains_cases(str, &trivial_or_enc_pat, null.as_ref());
123124

124125
match pat {
125126
GenericPattern::Clear(pat) => {
@@ -170,7 +171,7 @@ impl ServerKey {
170171
match self.length_checks(str, &trivial_or_enc_pat) {
171172
IsMatch::Clear(val) => return self.key.create_trivial_boolean_block(val),
172173
IsMatch::Cipher(val) => return val,
173-
_ => (),
174+
IsMatch::None => (),
174175
}
175176

176177
if !trivial_or_enc_pat.is_padded() {
@@ -242,19 +243,19 @@ impl ServerKey {
242243
match self.length_checks(str, &trivial_or_enc_pat) {
243244
IsMatch::Clear(val) => return self.key.create_trivial_boolean_block(val),
244245
IsMatch::Cipher(val) => return val,
245-
_ => (),
246+
IsMatch::None => (),
246247
}
247248

248249
match pat {
249250
GenericPattern::Clear(pat) => {
250-
let (str_iter, clear_pat, iter) = self.clear_ends_with_cases(str, pat.str());
251+
let (str_iter, clear_pat, iter) = clear_ends_with_cases(str, pat.str());
251252

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

257-
let (str_iter, pat_iter, iter) = self.ends_with_cases(str, pat, null.as_ref());
258+
let (str_iter, pat_iter, iter) = ends_with_cases(str, pat, null.as_ref());
258259

259260
self.compare_shifted((str_iter, pat_iter), iter.into_par_iter(), false)
260261
}

0 commit comments

Comments
 (0)