concrete_core/specification/engines/
lwe_ciphertext_loading.rs

1use super::engine_error;
2use crate::specification::engines::AbstractEngine;
3use crate::specification::entities::{LweCiphertextEntity, LweCiphertextVectorEntity};
4use concrete_commons::parameters::LweCiphertextIndex;
5
6engine_error! {
7    LweCiphertextLoadingError for LweCiphertextLoadingEngine @
8    IndexTooLarge => "The index must not exceed the size of the vector."
9}
10
11impl<EngineError: std::error::Error> LweCiphertextLoadingError<EngineError> {
12    /// Validates the inputs
13    pub fn perform_generic_checks<Ciphertext, CiphertextVector>(
14        vector: &CiphertextVector,
15        i: LweCiphertextIndex,
16    ) -> Result<(), Self>
17    where
18        Ciphertext: LweCiphertextEntity,
19        CiphertextVector: LweCiphertextVectorEntity<KeyDistribution = Ciphertext::KeyDistribution>,
20    {
21        if i.0 >= vector.lwe_ciphertext_count().0 {
22            return Err(Self::IndexTooLarge);
23        }
24        Ok(())
25    }
26}
27
28/// A trait for engines loading LWE ciphertexts from LWE ciphertext vectors.
29///
30/// # Semantics
31///
32/// This [pure](super#operation-semantics) operation generates an LWE ciphertext containing the
33/// `i`th LWE ciphertext of the `vector` LWE ciphertext vector.
34///
35/// # Formal Definition
36pub trait LweCiphertextLoadingEngine<CiphertextVector, Ciphertext>: AbstractEngine
37where
38    Ciphertext: LweCiphertextEntity,
39    CiphertextVector: LweCiphertextVectorEntity<KeyDistribution = Ciphertext::KeyDistribution>,
40{
41    /// Loads an LWE ciphertext from an LWE ciphertext vector.
42    fn load_lwe_ciphertext(
43        &mut self,
44        vector: &CiphertextVector,
45        i: LweCiphertextIndex,
46    ) -> Result<Ciphertext, LweCiphertextLoadingError<Self::EngineError>>;
47
48    /// Unsafely loads an LWE ciphertext from an LWE ciphertext vector.
49    ///
50    /// # Safety
51    /// For the _general_ safety concerns regarding this operation, refer to the different variants
52    /// of [`LweCiphertextLoadingError`]. For safety concerns _specific_ to an engine,
53    /// refer to the implementer safety section.
54    unsafe fn load_lwe_ciphertext_unchecked(
55        &mut self,
56        vector: &CiphertextVector,
57        i: LweCiphertextIndex,
58    ) -> Ciphertext;
59}