diff --git a/fuzz/fuzz_targets/fuzz_target_1.rs b/fuzz/fuzz_targets/fuzz_target_1.rs index ad112e9..72e9ebb 100644 --- a/fuzz/fuzz_targets/fuzz_target_1.rs +++ b/fuzz/fuzz_targets/fuzz_target_1.rs @@ -52,9 +52,7 @@ fn fuzz_homomorphic_operations( ]; for (i, result) in add_results.iter().enumerate() { let decrypted_add = decryptor.decrypt(result); - println!("{:?},{:?}",decrypted_add,data1); assert_eq!(decrypted_add.len(), data1.len()); - println!("Addition result {}: {:?}", i, decrypted_add); } // Test homomorphic_subtract with all combinations @@ -66,7 +64,6 @@ fn fuzz_homomorphic_operations( for (i, result) in sub_results.iter().enumerate() { let decrypted_sub = decryptor.decrypt(result); assert_eq!(decrypted_sub.len(), data1.len()); - println!("Subtraction result {}: {:?}", i, decrypted_sub); } // Test homomorphic_multiply with all combinations @@ -78,19 +75,25 @@ fn fuzz_homomorphic_operations( for (i, result) in mul_results.iter().enumerate() { let decrypted_mul = decryptor.decrypt(result); assert_eq!(decrypted_mul.len(), data1.len()); - println!("Multiplication result {}: {:?}", i, decrypted_mul); } // Test homomorphic_divide with all combinations let div_results = [ - encryptor.homomorphic_divide(&enc_collection1, &enc_value), - encryptor.homomorphic_divide(&enc_value, &enc_collection1), - encryptor.homomorphic_divide(&enc_collection1, &enc_collection1), + encryptor.homomorphic_divide(&enc_collection1, &enc_value), + encryptor.homomorphic_divide(&enc_value, &enc_collection1), + encryptor.homomorphic_divide(&enc_collection1, &enc_collection1), ]; + for (i, result) in div_results.iter().enumerate() { let decrypted_div = decryptor.decrypt(result); - println!("{:?},{:?}",decrypted_div,data1); - println!("Division result {}: {:?}", i, decrypted_div); + + // Skip assertion if the decrypted result is empty (indicating a division by zero) + if decrypted_div.is_empty() { + println!("Skipping assertion for index {} due to division by zero.", i); + continue; + } + + assert_eq!(decrypted_div.len(), data1.len()); } } @@ -141,10 +144,7 @@ fn fuzz_advanced_operations( let decrypted_exp = decryptor.decrypt(&exp_result); assert_eq!(decrypted_exp.len(), data.len()); - let div_result = encryptor.homomorphic_divide(&encrypted_data, &encrypted_value); - let decrypted_div = decryptor.decrypt(&div_result); - println!("{:?}", decrypted_div); - println!("{:?}", data); + } fuzz_target!(|data: &[u8]| { diff --git a/src/stringfn.rs b/src/stringfn.rs index 6f66759..a6af9a3 100644 --- a/src/stringfn.rs +++ b/src/stringfn.rs @@ -30,26 +30,36 @@ impl CKKSEncryptor { /// - `encrypted_poly`: The encrypted string as a polynomial. /// - `range`: A range representing the indices to extract (e.g., `0..3`, `3..`, `..5`). pub fn extract_encrypted_substring(&self, encrypted_poly: &Polynomial, range: R) -> Polynomial - where - R: RangeBounds, - { - // Convert RangeBounds into a concrete Range - let start = match range.start_bound() { - std::ops::Bound::Included(&s) => s, - std::ops::Bound::Excluded(&s) => s + 1, - std::ops::Bound::Unbounded => 0, - }; - - let end = match range.end_bound() { - std::ops::Bound::Included(&e) => e + 1, - std::ops::Bound::Excluded(&e) => e, - std::ops::Bound::Unbounded => encrypted_poly.coeffs.len(), - }; - - // Apply the range to get the substring coefficients - let substring_coeffs = encrypted_poly.coeffs[start..end].to_vec(); - - // Return a new polynomial with the substring coefficients - Polynomial::new(substring_coeffs) +where + R: RangeBounds, +{ + // Convert RangeBounds into a concrete Range + let start = match range.start_bound() { + std::ops::Bound::Included(&s) => s, + std::ops::Bound::Excluded(&s) => s + 1, + std::ops::Bound::Unbounded => 0, + }; + + let end = match range.end_bound() { + std::ops::Bound::Included(&e) => e + 1, + std::ops::Bound::Excluded(&e) => e, + std::ops::Bound::Unbounded => encrypted_poly.coeffs.len(), + }; + + // Ensure start and end are within bounds + let start = start.clamp(0, encrypted_poly.coeffs.len()); + let end = end.clamp(0, encrypted_poly.coeffs.len()); + + if start >= end { + // If the range is invalid (start >= end), return an empty polynomial + return Polynomial::new(vec![]); } + + // Apply the range to get the substring coefficients + let substring_coeffs = encrypted_poly.coeffs[start..end].to_vec(); + + // Return a new polynomial with the substring coefficients + Polynomial::new(substring_coeffs) +} + }