concrete_core/specification/engines/
glwe_ciphertext_discarding_conversion.rs

1use super::engine_error;
2use crate::specification::engines::AbstractEngine;
3use crate::specification::entities::GlweCiphertextEntity;
4
5engine_error! {
6    GlweCiphertextDiscardingConversionError for GlweCiphertextDiscardingConversionEngine @
7    GlweDimensionMismatch => "The input and output GLWE dimension must be the same.",
8    PolynomialSizeMismatch => "The input and output polynomial size must be the same."
9}
10
11impl<EngineError: std::error::Error> GlweCiphertextDiscardingConversionError<EngineError> {
12    /// Validates the inputs
13    pub fn perform_generic_checks<Input, Output>(output: &Output, input: &Input) -> Result<(), Self>
14    where
15        Input: GlweCiphertextEntity,
16        Output: GlweCiphertextEntity<KeyDistribution = Input::KeyDistribution>,
17    {
18        if input.glwe_dimension() != output.glwe_dimension() {
19            return Err(Self::GlweDimensionMismatch);
20        }
21
22        if input.polynomial_size() != output.polynomial_size() {
23            return Err(Self::PolynomialSizeMismatch);
24        }
25
26        Ok(())
27    }
28}
29
30/// A trait for engines converting (discarding) GLWE ciphertexts .
31///
32/// # Semantics
33///
34/// This [discarding](super#operation-semantics) operation fills the `output` GLWE ciphertext with
35/// the conversion of the `input` GLWE ciphertext to a type with a different representation (for
36/// instance from cpu to gpu memory).
37///
38/// # Formal Definition
39pub trait GlweCiphertextDiscardingConversionEngine<Input, Output>: AbstractEngine
40where
41    Input: GlweCiphertextEntity,
42    Output: GlweCiphertextEntity<KeyDistribution = Input::KeyDistribution>,
43{
44    /// Converts a GLWE ciphertext .
45    fn discard_convert_glwe_ciphertext(
46        &mut self,
47        output: &mut Output,
48        input: &Input,
49    ) -> Result<(), GlweCiphertextDiscardingConversionError<Self::EngineError>>;
50
51    /// Unsafely converts a GLWE ciphertext .
52    ///
53    /// # Safety
54    /// For the _general_ safety concerns regarding this operation, refer to the different variants
55    /// of [`GlweCiphertextDiscardingConversionError`]. For safety concerns _specific_ to an engine,
56    /// refer to the implementer safety section.
57    unsafe fn discard_convert_glwe_ciphertext_unchecked(
58        &mut self,
59        output: &mut Output,
60        input: &Input,
61    );
62}