concrete_core/specification/engines/
glwe_ciphertext_discarding_decryption.rs

1use super::engine_error;
2use crate::specification::engines::AbstractEngine;
3use crate::specification::entities::{
4    GlweCiphertextEntity, GlweSecretKeyEntity, PlaintextVectorEntity,
5};
6
7engine_error! {
8    GlweCiphertextDiscardingDecryptionError for GlweCiphertextDiscardingDecryptionEngine @
9    GlweDimensionMismatch => "The GLWE dimension of the key and ciphertext must be the same.",
10    PolynomialSizeMismatch => "The polynomial size of the key and ciphertext must be the same.",
11    PlaintextCountMismatch => "The size of the output plaintext vector and the input ciphertext \
12                               polynomial size must be the same."
13}
14
15impl<EngineError: std::error::Error> GlweCiphertextDiscardingDecryptionError<EngineError> {
16    /// Validates the inputs
17    pub fn perform_generic_checks<SecretKey, Ciphertext, PlaintextVector>(
18        key: &SecretKey,
19        output: &PlaintextVector,
20        input: &Ciphertext,
21    ) -> Result<(), Self>
22    where
23        SecretKey: GlweSecretKeyEntity,
24        Ciphertext: GlweCiphertextEntity,
25        PlaintextVector: PlaintextVectorEntity,
26    {
27        if key.polynomial_size() != input.polynomial_size() {
28            return Err(Self::PolynomialSizeMismatch);
29        }
30        if key.glwe_dimension() != input.glwe_dimension() {
31            return Err(Self::GlweDimensionMismatch);
32        }
33        if input.polynomial_size().0 != output.plaintext_count().0 {
34            return Err(Self::PlaintextCountMismatch);
35        }
36        Ok(())
37    }
38}
39
40/// A trait for engines decrypting (discarding) GLWE ciphertexts.
41///
42/// # Semantics
43///
44/// This [discarding](super#operation-semantics) operation fills the `output` plaintext vector with
45/// the decryption of the `input` GLWE ciphertext, under the `key` secret key.
46///
47/// # Formal Definition
48///
49/// cf [`here`](`crate::specification::engines::GlweCiphertextDecryptionEngine`)
50pub trait GlweCiphertextDiscardingDecryptionEngine<SecretKey, Ciphertext, PlaintextVector>:
51    AbstractEngine
52where
53    SecretKey: GlweSecretKeyEntity,
54    Ciphertext: GlweCiphertextEntity,
55    PlaintextVector: PlaintextVectorEntity,
56{
57    /// Decrypts a GLWE ciphertext .
58    fn discard_decrypt_glwe_ciphertext(
59        &mut self,
60        key: &SecretKey,
61        output: &mut PlaintextVector,
62        input: &Ciphertext,
63    ) -> Result<(), GlweCiphertextDiscardingDecryptionError<Self::EngineError>>;
64
65    /// Unsafely decrypts a GLWE ciphertext .
66    ///
67    /// # Safety
68    /// For the _general_ safety concerns regarding this operation, refer to the different variants
69    /// of [`GlweCiphertextDiscardingDecryptionError`]. For safety concerns _specific_ to an engine,
70    /// refer to the implementer safety section.
71    unsafe fn discard_decrypt_glwe_ciphertext_unchecked(
72        &mut self,
73        key: &SecretKey,
74        output: &mut PlaintextVector,
75        input: &Ciphertext,
76    );
77}