concrete_core/specification/engines/lwe_ciphertext_encryption.rs
1use super::engine_error;
2
3use crate::prelude::Variance;
4use crate::specification::engines::AbstractEngine;
5use crate::specification::entities::{LweCiphertextEntity, LweSecretKeyEntity, PlaintextEntity};
6
7engine_error! {
8 LweCiphertextEncryptionError for LweCiphertextEncryptionEngine @
9}
10
11/// A trait for engines encrypting LWE ciphertexts.
12///
13/// # Semantics
14///
15/// This [pure](super#operation-semantics) operation generates an LWE ciphertext containing the
16/// encryption of the `input` plaintext under the `key` secret key.
17///
18/// # Formal Definition
19///
20/// ## LWE Encryption
21/// ###### inputs:
22/// - $\mathsf{pt}\in\mathbb{Z}\_q$: a plaintext
23/// - $\vec{s}\in\mathbb{Z}\_q^n$: a secret key
24/// - $\mathcal{D\_{\sigma^2,\mu}}$: a normal distribution of variance $\sigma^2$ and a mean $\mu$
25///
26/// ###### outputs:
27/// - $\mathsf{ct} = \left( \vec{a} , b\right) \in \mathsf{LWE}^n\_{\vec{s}}( \mathsf{pt} )\subseteq
28/// \mathbb{Z}\_q^{(n+1)}$: an LWE ciphertext
29///
30/// ###### algorithm:
31/// 1. uniformly sample a vector $\vec{a}\in\mathbb{Z}\_q^n$
32/// 2. sample an integer error term $e \hookleftarrow \mathcal{D\_{\sigma^2,\mu}}$
33/// 3. compute $b = \left\langle \vec{a} , \vec{s} \right\rangle + \mathsf{pt} + e \in\mathbb{Z}\_q$
34/// 4. output $\left( \vec{a} , b\right)$
35pub trait LweCiphertextEncryptionEngine<SecretKey, Plaintext, Ciphertext>: AbstractEngine
36where
37 SecretKey: LweSecretKeyEntity,
38 Plaintext: PlaintextEntity,
39 Ciphertext: LweCiphertextEntity,
40{
41 /// Encrypts an LWE ciphertext.
42 fn encrypt_lwe_ciphertext(
43 &mut self,
44 key: &SecretKey,
45 input: &Plaintext,
46 noise: Variance,
47 ) -> Result<Ciphertext, LweCiphertextEncryptionError<Self::EngineError>>;
48
49 /// Unsafely encrypts an LWE ciphertext.
50 ///
51 /// # Safety
52 /// For the _general_ safety concerns regarding this operation, refer to the different variants
53 /// of [`LweCiphertextEncryptionError`]. For safety concerns _specific_ to an
54 /// engine, refer to the implementer safety section.
55 unsafe fn encrypt_lwe_ciphertext_unchecked(
56 &mut self,
57 key: &SecretKey,
58 input: &Plaintext,
59 noise: Variance,
60 ) -> Ciphertext;
61}