concrete_core/specification/engines/
glwe_ciphertext_encryption.rs

1use super::engine_error;
2use crate::prelude::Variance;
3use crate::specification::engines::AbstractEngine;
4use crate::specification::entities::{
5    GlweCiphertextEntity, GlweSecretKeyEntity, PlaintextVectorEntity,
6};
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
39///
40/// ## GLWE Encryption
41/// ###### inputs:
42/// - $\mathsf{PT}\in\mathcal{R}\_q$: a plaintext
43/// - $\vec{S} \in\mathcal{R}\_q^k$: a secret key
44/// - $\mathcal{D\_{\sigma^2,\mu}}$: a normal distribution of variance $\sigma^2$ and mean $\mu$
45///
46/// ###### outputs:
47/// - $\mathsf{CT} = \left( \vec{A} , B \right) \in \mathsf{GLWE}\_{\vec{S}}( \mathsf{PT} )\subseteq
48///   \mathcal{R}\_q^{k+1}$: an GLWE ciphertext
49///
50/// ###### algorithm:
51/// 1. uniformly sample each coefficient of the polynomial vector $\vec{A}\in\mathcal{R}^k\_q$
52/// 2. sample each integer error coefficient of an error polynomial $E\in\mathcal{R}\_q$ from
53/// $\mathcal{D\_{\sigma^2,\mu}}$ 3. compute $B = \left\langle \vec{A} , \vec{S} \right\rangle +
54/// \mathsf{PT} + E \in\mathcal{R}\_q$ 4. output $\left( \vec{A} , B \right)$
55pub trait GlweCiphertextEncryptionEngine<SecretKey, PlaintextVector, Ciphertext>:
56    AbstractEngine
57where
58    SecretKey: GlweSecretKeyEntity,
59    PlaintextVector: PlaintextVectorEntity,
60    Ciphertext: GlweCiphertextEntity,
61{
62    /// Encrypts a plaintext vector into a GLWE ciphertext.
63    fn encrypt_glwe_ciphertext(
64        &mut self,
65        key: &SecretKey,
66        input: &PlaintextVector,
67        noise: Variance,
68    ) -> Result<Ciphertext, GlweCiphertextEncryptionError<Self::EngineError>>;
69
70    /// Unsafely encrypts a plaintext vector into a GLWE ciphertext.
71    ///
72    /// # Safety
73    /// For the _general_ safety concerns regarding this operation, refer to the different variants
74    /// of [`GlweCiphertextEncryptionError`]. For safety concerns _specific_ to an engine, refer to
75    /// the implementer safety section.
76    unsafe fn encrypt_glwe_ciphertext_unchecked(
77        &mut self,
78        key: &SecretKey,
79        input: &PlaintextVector,
80        noise: Variance,
81    ) -> Ciphertext;
82}