concrete_core/specification/engines/
glwe_ciphertext_encryption.rs

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