concrete_core/specification/engines/
lwe_ciphertext_vector_discarding_subtraction.rs

1use super::engine_error;
2use crate::specification::engines::AbstractEngine;
3use crate::specification::entities::LweCiphertextVectorEntity;
4
5engine_error! {
6    LweCiphertextVectorDiscardingSubtractionError for LweCiphertextVectorDiscardingSubtractionEngine @
7    LweDimensionMismatch => "The input and output LWE dimensions must be the same.",
8    CiphertextCountMismatch => "The input and output ciphertext count must be the same."
9}
10
11impl<EngineError: std::error::Error> LweCiphertextVectorDiscardingSubtractionError<EngineError> {
12    /// Validates the inputs
13    pub fn perform_generic_checks<OutputCiphertextVector, InputCiphertextVector>(
14        output: &OutputCiphertextVector,
15        input_1: &InputCiphertextVector,
16        input_2: &InputCiphertextVector,
17    ) -> Result<(), Self>
18    where
19        InputCiphertextVector: LweCiphertextVectorEntity,
20        OutputCiphertextVector:
21            LweCiphertextVectorEntity<KeyDistribution = InputCiphertextVector::KeyDistribution>,
22    {
23        if output.lwe_dimension() != input_1.lwe_dimension()
24            || output.lwe_dimension() != input_2.lwe_dimension()
25        {
26            return Err(Self::LweDimensionMismatch);
27        }
28        if output.lwe_ciphertext_count() != input_1.lwe_ciphertext_count()
29            || output.lwe_ciphertext_count() != input_2.lwe_ciphertext_count()
30        {
31            return Err(Self::CiphertextCountMismatch);
32        }
33        Ok(())
34    }
35}
36
37/// A trait for engines subtracting (discarding) LWE ciphertext vectors.
38///
39/// # Semantics
40///
41/// This [discarding](super#operation-semantics) operation fills the `output` LWE ciphertext vector
42/// with the element-wise subtraction of the `input_2` LWE ciphertext vector to the `input_1` lwe
43/// ciphertext vector.
44///
45/// # Formal Definition
46pub trait LweCiphertextVectorDiscardingSubtractionEngine<
47    InputCiphertextVector,
48    OutputCiphertextVector,
49>: AbstractEngine where
50    InputCiphertextVector: LweCiphertextVectorEntity,
51    OutputCiphertextVector:
52        LweCiphertextVectorEntity<KeyDistribution = InputCiphertextVector::KeyDistribution>,
53{
54    /// Subtracts two LWE ciphertext vectors.
55    fn discard_sub_lwe_ciphertext_vector(
56        &mut self,
57        output: &mut OutputCiphertextVector,
58        input_1: &InputCiphertextVector,
59        input_2: &InputCiphertextVector,
60    ) -> Result<(), LweCiphertextVectorDiscardingSubtractionError<Self::EngineError>>;
61
62    /// Unsafely subtracts two LWE ciphertext vectors.
63    ///
64    /// # Safety
65    /// For the _general_ safety concerns regarding this operation, refer to the different variants
66    /// of [`LweCiphertextVectorDiscardingSubtractionError`]. For safety concerns _specific_ to an
67    /// engine, refer to the implementer safety section.
68    unsafe fn discard_sub_lwe_ciphertext_vector_unchecked(
69        &mut self,
70        output: &mut OutputCiphertextVector,
71        input_1: &InputCiphertextVector,
72        input_2: &InputCiphertextVector,
73    );
74}