concrete_core/specification/engines/
glwe_ciphertext_decryption.rs

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