use simple_ring::Polynomial;
use crate::BFV;
#[derive(Clone)]
pub struct BFVPlaintext {
pub plain: Polynomial,
pub len: usize,
}
impl BFVPlaintext {
pub fn empty(bfv: &BFV) -> Self { Self {
plain: Polynomial::zeros(bfv.params.n),
len: 0, }
}
pub fn push(mut self, to_push: &str) -> Self {
let encoded: Vec<u64> = to_push.as_bytes().iter().map(|&b| b as u64).collect();
let remaining = self.plain.coeffs.len() - self.len;
let len_to_write = encoded.len().min(remaining);
for i in 0..len_to_write { self.plain.coeffs[self.len + i] = encoded[i];
}
self.len += len_to_write; self
}
pub fn new(text: &str, bfv: &BFV) -> Self { let n = bfv.params.n;
let mut coeffs: Vec<u64> = vec![0; n];
let bytes = text.as_bytes();
let len_to_write = bytes.len().min(n);
for i in 0..len_to_write { coeffs[i] = bytes[i] as u64;
}
Self {
plain: Polynomial::new(coeffs), len: len_to_write,
}
}
pub fn new_from_coeffs(coeffs: Vec<u64>, bfv: &BFV) -> Self { Self { plain: Polynomial::new(coeffs), len: bfv.params.n }
}
pub fn decode(&self) -> String { let vec = self.plain.coeffs.to_vec();
let vec: Vec<u8> = vec.iter().map(|&b| b as u8).collect();
str::from_utf8(&vec).unwrap().to_string()
}
}