tfhe_hpu_backend/entities/
glwe_ciphertext.rs1use super::parameters::*;
7use super::traits::container::*;
8
9#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
11pub struct HpuGlweCiphertext<C: Container> {
12 data: C,
13 params: HpuParameters,
14}
15
16impl<C: ContainerMut> AsMut<[C::Element]> for HpuGlweCiphertext<C> {
17 fn as_mut(&mut self) -> &mut [C::Element] {
18 self.data.as_mut()
19 }
20}
21
22impl<C: ContainerMut> AsRef<[C::Element]> for HpuGlweCiphertext<C> {
23 fn as_ref(&self) -> &[C::Element] {
24 self.data.as_ref()
25 }
26}
27
28pub fn hpu_glwe_ciphertext_size(params: &HpuParameters) -> usize {
29 (params.pbs_params.glwe_dimension + 1) * params.pbs_params.polynomial_size
30}
31
32impl<C: Container> HpuGlweCiphertext<C> {
33 pub fn from_container(container: C, params: HpuParameters) -> Self {
35 assert!(
36 container.container_len() > 0,
37 "Got an empty container to create a HpuGlweCiphertext"
38 );
39 assert!(
40 container.container_len() == hpu_glwe_ciphertext_size(¶ms),
41 "The provided container length is not valid. \
42 It needs to match with parameters. \
43 Got container length: {} and based on parameters value expect: {}.",
44 container.container_len(),
45 hpu_glwe_ciphertext_size(¶ms),
46 );
47 Self {
48 data: container,
49 params,
50 }
51 }
52
53 pub fn into_container(self) -> C {
57 self.data
58 }
59}
60
61impl<C: Container> HpuGlweCiphertext<C> {
62 pub fn params(&self) -> &HpuParameters {
66 &self.params
67 }
68
69 pub fn as_view(&self) -> HpuGlweCiphertext<&'_ [C::Element]> {
72 HpuGlweCiphertext {
73 data: self.data.as_ref(),
74 params: self.params.clone(),
75 }
76 }
77}
78
79impl<C: ContainerMut> HpuGlweCiphertext<C> {
80 pub fn as_mut_view(&mut self) -> HpuGlweCiphertext<&'_ mut [C::Element]> {
82 HpuGlweCiphertext {
83 data: self.data.as_mut(),
84 params: self.params.clone(),
85 }
86 }
87}
88
89pub type HpuGlweCiphertextOwned<Scalar> = HpuGlweCiphertext<Vec<Scalar>>;
91pub type HpuGlweCiphertextView<'data, Scalar> = HpuGlweCiphertext<&'data [Scalar]>;
93pub type HpuGlweCiphertextMutView<'data, Scalar> = HpuGlweCiphertext<&'data mut [Scalar]>;
95
96impl<Scalar: std::clone::Clone> HpuGlweCiphertextOwned<Scalar> {
97 pub fn new(fill_with: Scalar, params: HpuParameters) -> Self {
106 Self::from_container(vec![fill_with; hpu_glwe_ciphertext_size(¶ms)], params)
107 }
108}