1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use super::engine_error;
use crate::prelude::PolynomialSize;
use crate::specification::engines::AbstractEngine;
use crate::specification::entities::GlweCiphertextEntity;
engine_error! {
GlweCiphertextCreationError for GlweCiphertextCreationEngine @
EmptyContainer => "The container used to create the GLWE ciphertext is of length 0!",
InvalidContainerSize => "The length of the container used to create the GLWE ciphertext \
needs to be a multiple of `polynomial_size`."
}
impl<EngineError: std::error::Error> GlweCiphertextCreationError<EngineError> {
/// Validates the inputs, the container is expected to have a length of
/// glwe_size * polynomial_size, during construction we only get the container and the
/// polynomial size so we check the length is consistent, the GLWE size is deduced by the
/// ciphertext implementation from the container and the polynomial size.
pub fn perform_generic_checks(
container_length: usize,
polynomial_size: PolynomialSize,
) -> Result<(), Self> {
if container_length == 0 {
return Err(Self::EmptyContainer);
}
if container_length % polynomial_size.0 != 0 {
return Err(Self::InvalidContainerSize);
}
Ok(())
}
}
/// A trait for engines creating a GLWE ciphertext from an arbitrary container.
///
/// # Semantics
///
/// This [pure](super#operation-semantics) operation creates a GLWE ciphertext from the abitrary
/// `container`. By arbitrary here, we mean that `Container` can be any type that allows to
/// instantiate a `GlweCiphertextEntity`.
pub trait GlweCiphertextCreationEngine<Container, Ciphertext>: AbstractEngine
where
Ciphertext: GlweCiphertextEntity,
{
/// Creates a GLWE ciphertext from an arbitrary container.
fn create_glwe_ciphertext_from(
&mut self,
container: Container,
polynomial_size: PolynomialSize,
) -> Result<Ciphertext, GlweCiphertextCreationError<Self::EngineError>>;
/// Unsafely creates a GLWE ciphertext from an arbitrary container.
///
/// # Safety
/// For the _general_ safety concerns regarding this operation, refer to the different variants
/// of [`GlweCiphertextCreationError`]. For safety concerns _specific_ to an engine, refer
/// to the implementer safety section.
unsafe fn create_glwe_ciphertext_from_unchecked(
&mut self,
container: Container,
polynomial_size: PolynomialSize,
) -> Ciphertext;
}