concrete_core/backends/core/implementation/engines/
lwe_ciphertext_creation.rs

1use crate::backends::core::implementation::engines::CoreEngine;
2use crate::backends::core::implementation::entities::{
3    LweCiphertext32, LweCiphertext64, LweCiphertextMutView32, LweCiphertextMutView64,
4    LweCiphertextView32, LweCiphertextView64,
5};
6use crate::backends::core::private::crypto::lwe::LweCiphertext as ImplLweCiphertext;
7use crate::specification::engines::{LweCiphertextCreationEngine, LweCiphertextCreationError};
8
9/// # Description:
10/// Implementation of [`LweCiphertextCreationEngine`] for [`CoreEngine`] which returns an
11/// [`LweCiphertext32`].
12impl LweCiphertextCreationEngine<Vec<u32>, LweCiphertext32> for CoreEngine {
13    /// # Example:
14    /// ```
15    /// use concrete_commons::parameters::LweSize;
16    /// use concrete_core::prelude::*;
17    /// # use std::error::Error;
18    ///
19    /// # fn main() -> Result<(), Box<dyn Error>> {
20    /// // Here we create a container outside of the engine
21    /// // Note that the size here is just for demonstration purposes and should not be chosen
22    /// // without proper security analysis for production
23    /// let lwe_size = LweSize(128);
24    /// let owned_container = vec![0_u32; lwe_size.0];
25    ///
26    /// let mut engine = CoreEngine::new(())?;
27    /// let ciphertext: LweCiphertext32 = engine.create_lwe_ciphertext(owned_container)?;
28    /// engine.destroy(ciphertext)?;
29    /// #
30    /// # Ok(())
31    /// # }
32    /// ```
33    fn create_lwe_ciphertext(
34        &mut self,
35        container: Vec<u32>,
36    ) -> Result<LweCiphertext32, LweCiphertextCreationError<Self::EngineError>> {
37        LweCiphertextCreationError::<Self::EngineError>::perform_generic_checks(container.len())?;
38        Ok(unsafe { self.create_lwe_ciphertext_unchecked(container) })
39    }
40
41    unsafe fn create_lwe_ciphertext_unchecked(&mut self, container: Vec<u32>) -> LweCiphertext32 {
42        LweCiphertext32(ImplLweCiphertext::from_container(container))
43    }
44}
45
46/// # Description:
47/// Implementation of [`LweCiphertextCreationEngine`] for [`CoreEngine`] which returns an
48/// [`LweCiphertext64`].
49impl LweCiphertextCreationEngine<Vec<u64>, LweCiphertext64> for CoreEngine {
50    /// # Example:
51    /// ```
52    /// use concrete_commons::parameters::LweSize;
53    /// use concrete_core::prelude::*;
54    /// # use std::error::Error;
55    ///
56    /// # fn main() -> Result<(), Box<dyn Error>> {
57    /// // Here we create a container outside of the engine
58    /// // Note that the size here is just for demonstration purposes and should not be chosen
59    /// // without proper security analysis for production
60    /// let lwe_size = LweSize(128);
61    /// let owned_container = vec![0_u64; lwe_size.0];
62    ///
63    /// let mut engine = CoreEngine::new(())?;
64    /// let ciphertext: LweCiphertext64 = engine.create_lwe_ciphertext(owned_container)?;
65    /// engine.destroy(ciphertext)?;
66    /// #
67    /// # Ok(())
68    /// # }
69    /// ```
70    fn create_lwe_ciphertext(
71        &mut self,
72        container: Vec<u64>,
73    ) -> Result<LweCiphertext64, LweCiphertextCreationError<Self::EngineError>> {
74        LweCiphertextCreationError::<Self::EngineError>::perform_generic_checks(container.len())?;
75        Ok(unsafe { self.create_lwe_ciphertext_unchecked(container) })
76    }
77
78    unsafe fn create_lwe_ciphertext_unchecked(&mut self, container: Vec<u64>) -> LweCiphertext64 {
79        LweCiphertext64(ImplLweCiphertext::from_container(container))
80    }
81}
82
83/// # Description:
84/// Implementation of [`LweCiphertextCreationEngine`] for [`CoreEngine`] which returns an immutable
85/// [`LweCiphertextView32`] that does not own its memory.
86impl<'data> LweCiphertextCreationEngine<&'data [u32], LweCiphertextView32<'data>> for CoreEngine {
87    /// # Example:
88    /// ```
89    /// use concrete_commons::parameters::LweSize;
90    /// use concrete_core::prelude::*;
91    /// # use std::error::Error;
92    ///
93    /// # fn main() -> Result<(), Box<dyn Error>> {
94    /// // Here we create a container outside of the engine
95    /// // Note that the size here is just for demonstration purposes and should not be chosen
96    /// // without proper security analysis for production
97    /// let lwe_size = LweSize(128);
98    /// let mut owned_container = vec![0_u32; lwe_size.0];
99    ///
100    /// let slice = &owned_container[..];
101    ///
102    /// let mut engine = CoreEngine::new(())?;
103    /// let ciphertext_view: LweCiphertextView32 = engine.create_lwe_ciphertext(slice)?;
104    /// engine.destroy(ciphertext_view)?;
105    /// #
106    /// # Ok(())
107    /// # }
108    /// ```
109    fn create_lwe_ciphertext(
110        &mut self,
111        container: &'data [u32],
112    ) -> Result<LweCiphertextView32<'data>, LweCiphertextCreationError<Self::EngineError>> {
113        LweCiphertextCreationError::<Self::EngineError>::perform_generic_checks(container.len())?;
114        Ok(unsafe { self.create_lwe_ciphertext_unchecked(container) })
115    }
116
117    unsafe fn create_lwe_ciphertext_unchecked(
118        &mut self,
119        container: &'data [u32],
120    ) -> LweCiphertextView32<'data> {
121        LweCiphertextView32(ImplLweCiphertext::from_container(container))
122    }
123}
124
125/// # Description:
126/// Implementation of [`LweCiphertextCreationEngine`] for [`CoreEngine`] which returns a mutable
127/// [`LweCiphertextMutView32`] that does not own its memory.
128impl<'data> LweCiphertextCreationEngine<&'data mut [u32], LweCiphertextMutView32<'data>>
129    for CoreEngine
130{
131    /// # Example:
132    /// ```
133    /// use concrete_commons::parameters::LweSize;
134    /// use concrete_core::prelude::*;
135    /// # use std::error::Error;
136    ///
137    /// # fn main() -> Result<(), Box<dyn Error>> {
138    /// // Here we create a container outside of the engine
139    /// // Note that the size here is just for demonstration purposes and should not be chosen
140    /// // without proper security analysis for production
141    /// let lwe_size = LweSize(128);
142    /// let mut owned_container = vec![0_u32; lwe_size.0];
143    ///
144    /// let slice = &mut owned_container[..];
145    ///
146    /// let mut engine = CoreEngine::new(())?;
147    /// let ciphertext_view: LweCiphertextMutView32 = engine.create_lwe_ciphertext(slice)?;
148    /// engine.destroy(ciphertext_view)?;
149    /// #
150    /// # Ok(())
151    /// # }
152    /// ```
153    fn create_lwe_ciphertext(
154        &mut self,
155        container: &'data mut [u32],
156    ) -> Result<LweCiphertextMutView32<'data>, LweCiphertextCreationError<Self::EngineError>> {
157        LweCiphertextCreationError::<Self::EngineError>::perform_generic_checks(container.len())?;
158        Ok(unsafe { self.create_lwe_ciphertext_unchecked(container) })
159    }
160
161    unsafe fn create_lwe_ciphertext_unchecked(
162        &mut self,
163        container: &'data mut [u32],
164    ) -> LweCiphertextMutView32<'data> {
165        LweCiphertextMutView32(ImplLweCiphertext::from_container(container))
166    }
167}
168
169/// # Description:
170/// Implementation of [`LweCiphertextCreationEngine`] for [`CoreEngine`] which returns an immutable
171/// [`LweCiphertextView64`] that does not own its memory.
172impl<'data> LweCiphertextCreationEngine<&'data [u64], LweCiphertextView64<'data>> for CoreEngine {
173    /// # Example:
174    /// ```
175    /// use concrete_core::prelude::*;
176    /// # use std::error::Error;
177    ///
178    /// # fn main() -> Result<(), Box<dyn Error>> {
179    /// // Here we create a container outside of the engine
180    /// // Note that the size here is just for demonstration purposes and should not be chosen
181    /// // without proper security analysis for production
182    /// let mut owned_container = vec![0_u64; 128];
183    ///
184    /// let slice = &owned_container[..];
185    ///
186    /// let mut engine = CoreEngine::new(())?;
187    /// let ciphertext_view: LweCiphertextView64 = engine.create_lwe_ciphertext(slice)?;
188    /// engine.destroy(ciphertext_view)?;
189    /// #
190    /// # Ok(())
191    /// # }
192    /// ```
193    fn create_lwe_ciphertext(
194        &mut self,
195        container: &'data [u64],
196    ) -> Result<LweCiphertextView64<'data>, LweCiphertextCreationError<Self::EngineError>> {
197        LweCiphertextCreationError::<Self::EngineError>::perform_generic_checks(container.len())?;
198        Ok(unsafe { self.create_lwe_ciphertext_unchecked(container) })
199    }
200
201    unsafe fn create_lwe_ciphertext_unchecked(
202        &mut self,
203        container: &'data [u64],
204    ) -> LweCiphertextView64<'data> {
205        LweCiphertextView64(ImplLweCiphertext::from_container(container))
206    }
207}
208
209/// # Description:
210/// Implementation of [`LweCiphertextCreationEngine`] for [`CoreEngine`] which returns a mutable
211/// [`LweCiphertextMutView64`] that does not own its memory.
212impl<'data> LweCiphertextCreationEngine<&'data mut [u64], LweCiphertextMutView64<'data>>
213    for CoreEngine
214{
215    /// # Example:
216    /// ```
217    /// use concrete_commons::parameters::LweSize;
218    /// use concrete_core::prelude::*;
219    /// # use std::error::Error;
220    ///
221    /// # fn main() -> Result<(), Box<dyn Error>> {
222    /// // Here we create a container outside of the engine
223    /// // Note that the size here is just for demonstration purposes and should not be chosen
224    /// // without proper security analysis for production
225    /// let lwe_size = LweSize(128);
226    /// let mut owned_container = vec![0_u64; lwe_size.0];
227    ///
228    /// let slice = &mut owned_container[..];
229    ///
230    /// let mut engine = CoreEngine::new(())?;
231    /// let ciphertext_view: LweCiphertextMutView64 = engine.create_lwe_ciphertext(slice)?;
232    /// engine.destroy(ciphertext_view)?;
233    /// #
234    /// # Ok(())
235    /// # }
236    /// ```
237    fn create_lwe_ciphertext(
238        &mut self,
239        container: &'data mut [u64],
240    ) -> Result<LweCiphertextMutView64<'data>, LweCiphertextCreationError<Self::EngineError>> {
241        LweCiphertextCreationError::<Self::EngineError>::perform_generic_checks(container.len())?;
242        Ok(unsafe { self.create_lwe_ciphertext_unchecked(container) })
243    }
244
245    unsafe fn create_lwe_ciphertext_unchecked(
246        &mut self,
247        container: &'data mut [u64],
248    ) -> LweCiphertextMutView64<'data> {
249        LweCiphertextMutView64(ImplLweCiphertext::from_container(container))
250    }
251}