use crate::polynomial::Polynomial;
use crate::ckks::CKKSEncryptor;
use std::ops::RangeBounds;
impl CKKSEncryptor {
pub fn homomorphic_length(&self, encrypted_poly: &Polynomial) -> usize {
let non_zero_coeffs = encrypted_poly.coeffs.iter().filter(|&&coeff| coeff != 0).count();
non_zero_coeffs
}
pub fn concatenate_encrypted_strings(&self, encrypted_poly1: &Polynomial, encrypted_poly2: &Polynomial) -> Polynomial {
let mut combined_coeffs = encrypted_poly1.coeffs.clone();
combined_coeffs.extend_from_slice(&encrypted_poly2.coeffs);
Polynomial::new(combined_coeffs)
}
pub fn extract_encrypted_substring<R>(&self, encrypted_poly: &Polynomial, range: R) -> Polynomial
where
R: RangeBounds<usize>,
{
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(),
};
let start = start.clamp(0, encrypted_poly.coeffs.len());
let end = end.clamp(0, encrypted_poly.coeffs.len());
if start >= end {
return Polynomial::new(vec![]);
}
let substring_coeffs = encrypted_poly.coeffs[start..end].to_vec();
Polynomial::new(substring_coeffs)
}
}