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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//! The encoding type for BFV.
use std::fmt::Display;
use fhe_traits::FhePlaintextEncoding;
#[derive(Debug, Clone, Eq, PartialEq)]
pub(crate) enum EncodingEnum {
Poly,
Simd,
}
impl Display for EncodingEnum {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{self:?}")
}
}
/// An encoding for the plaintext.
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct Encoding {
pub(crate) encoding: EncodingEnum,
pub(crate) level: usize,
}
impl Encoding {
/// A Poly encoding encodes a vector as coefficients of a polynomial;
/// homomorphic operations are therefore polynomial operations.
pub fn poly() -> Self {
Self {
encoding: EncodingEnum::Poly,
level: 0,
}
}
/// A Simd encoding encodes a vector so that homomorphic operations are
/// component-wise operations on the coefficients of the underlying vectors.
/// The Simd encoding require that the plaintext modulus is congruent to 1
/// modulo the degree of the underlying polynomial.
pub fn simd() -> Self {
Self {
encoding: EncodingEnum::Simd,
level: 0,
}
}
/// A poly encoding at a given level.
pub fn poly_at_level(level: usize) -> Self {
Self {
encoding: EncodingEnum::Poly,
level,
}
}
/// A simd encoding at a given level.
pub fn simd_at_level(level: usize) -> Self {
Self {
encoding: EncodingEnum::Simd,
level,
}
}
}
impl From<Encoding> for String {
fn from(e: Encoding) -> Self {
String::from(&e)
}
}
impl From<&Encoding> for String {
fn from(e: &Encoding) -> Self {
format!("{e:?}")
}
}
impl FhePlaintextEncoding for Encoding {}