Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mz/string tests #1767

Merged
merged 17 commits into from
Nov 20, 2024
Merged

Mz/string tests #1767

merged 17 commits into from
Nov 20, 2024

Conversation

mayeul-zama
Copy link
Contributor

No description provided.

Comment on lines +240 to +245
loop {
match rhs.len().cmp(&lhs.len()) {
Ordering::Less => rhs.append_null(self),
Ordering::Equal => break,
Ordering::Greater => lhs.append_null(self),
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks inefficient as we can have the lenght of both strings and extend once ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO the code is simpler this way

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let len_diff = rhs.len() as isize - lhs.len() as isze;

match len_diff.cmp(&0) {
Ordering::Less => rhs.extend_null(len_diff().abs()),
Ordering::Equal => (),
Ordering::Greater => lhs.extend_null(len_diff().abs()),
}

does not look bad IMO

Copy link
Member

@IceTDrinker IceTDrinker left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments

.github/workflows/aws_tfhe_tests.yml Outdated Show resolved Hide resolved
.github/workflows/aws_tfhe_tests.yml Show resolved Hide resolved
tfhe/src/shortint/client_key/mod.rs Outdated Show resolved Hide resolved
tfhe/src/strings/assert_functions/test_common.rs Outdated Show resolved Hide resolved
tfhe/src/strings/assert_functions/mod.rs Outdated Show resolved Hide resolved
tfhe/src/strings/assert_functions/test_common.rs Outdated Show resolved Hide resolved
tfhe/src/strings/mod.rs Outdated Show resolved Hide resolved
tfhe/src/strings/assert_functions/test_common.rs Outdated Show resolved Hide resolved
(true, false) | (false, true) => {
// We cannot assume the result is not padded.
// We ensure that it is padded with a single null
// which is ignored by our implementations
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ignored ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment has only been moved in this PR
I guess it meant that the result is still correct
I think we can just remove this last line, wdyt?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no idea I don't know the string code, if we do indeed ignore something there it's fine to keep the comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reworded

tfhe/src/strings/mod.rs Outdated Show resolved Hide resolved
@mayeul-zama mayeul-zama force-pushed the mz/string_tests branch 2 times, most recently from 19f43f0 to 69fe184 Compare November 15, 2024 15:56
Comment on lines 30 to 25
fn test_all2() {
test_all(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

test_all calls test_all_impl

Comment on lines +278 to +290
let uint = str.to_uint();
let mut shift_bits = self.scalar_left_shift_parallelized(shift, 3);

// `shift_bits` needs to have the same block len as `uint` for the tfhe-rs shift to work
self.pad_or_trim_ciphertext(&mut shift_bits, uint.blocks().len());

let shifted = self.right_shift_parallelized(&uint, &shift_bits);
let len = uint.blocks().len();

let shifted = if len == 0 {
uint
} else {
self.right_shift_parallelized(&uint, &shift_bits)
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a note for later: As we now have block rotation available, it might be better, so this should be measured at some point

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we have a follow up issue so we don't forget ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks

/// let sk = ServerKey::new_radix_server_key(&ck);
/// let (s1, s2) = ("hello", "hello");
///
/// 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 result = sk.string_eq(&enc_s1, enc_s2.as_ref());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not great from a usability standpoint :|

@IceTDrinker
Copy link
Member

Why do we need the GenericPatternRef again ? I don't see what we are gaining there, looks like a downgrade forcing users to use as_ref() where they could have "just" used an "&" on the pattern ?

Looks good to merge to move forward though, just would like an answer on the GenericPattern thing which could be changed in a later PR

@tmontaigu
Copy link
Contributor

tmontaigu commented Nov 19, 2024

I believe GenericPatternRef is there so that we do not need to own the value that is in the enum removing some clones

If we still want the user to be able to do &some_input we could use generics in our function (trading ease of use for user vs slightly more complex signature)

pub struct FheString;

enum GenericPaternRef<'a> {
    Clear(&'a str),
    Enc(&'a FheString)
}

impl<'a> From<&'a String> for GenericPaternRef<'a>{
   fn from(value: &'a String) -> Self {
    Self::Clear(value.as_str())
   }
}

impl<'a> From<&'a str> for GenericPaternRef<'a>{
   fn from(value: &'a str) -> Self {
    Self::Clear(value)
   }
}

// This one is so that `&"litetal"` works
impl<'a, 'b> From<&'a &'b str> for GenericPaternRef<'a>{
   fn from(value: &'a &'b str) -> Self {
    Self::Clear(*value)
   }
}

impl<'a> From<&'a FheString> for GenericPaternRef<'a>{
   fn from(value: &'a FheString) -> Self {
    Self::Enc(value)
   }
}

fn trim<'a>(string: &FheString, pat: impl Into<GenericPaternRef<'a>>) {
    match pat.into() {
        GenericPaternRef::Clear(_) => {todo!()},
        GenericPaternRef::Enc(_) => {todo!()},
    }
}

fn main() {
    let fhe_string = FheString;

    let fhe_pat = FheString;
    let clear_pat = "Yoshi";
    let clear_pat_str = "Yoshi".to_string();

    trim(&fhe_string, &fhe_pat);
    trim(&fhe_string, &clear_pat);
    trim(&fhe_string, &clear_pat_str);
}

@mayeul-zama
Copy link
Contributor Author

mayeul-zama commented Nov 20, 2024

Why do we need the GenericPatternRef again ? I don't see what we are gaining there, looks like a downgrade forcing users to use as_ref() where they could have "just" used an "&" on the pattern ?

It is indeed to avoid clones. In particular in the HLAPI wrappers.
The HLAPI should be more ergonomic with traits and hide GenericPattern enum

I think having simple and clean APIs in shortint and integer (even if they are more cumbersome to use) is ok as long as the HLAPI is easy to use

@mayeul-zama mayeul-zama merged commit 46cf465 into main Nov 20, 2024
102 checks passed
@mayeul-zama mayeul-zama deleted the mz/string_tests branch November 20, 2024 08:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants