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
77
//! Module containing the definition of the Plaintext.
use tfhe_versionable::Versionize;
use crate::core_crypto::backward_compatibility::entities::plaintext::PlaintextVersions;
use crate::core_crypto::commons::traits::*;
/// A plaintext (encoded) value.
#[derive(Debug, Copy, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize, Versionize)]
#[versionize(PlaintextVersions)]
pub struct Plaintext<T: Numeric>(pub T);
/// An immutable reference to a plaintext (encoded) value.
///
/// Can be converted to a plaintext via a call to `into`
/// ```rust
/// use tfhe::core_crypto::entities::*;
///
/// pub fn takes_plaintext(plain: Plaintext<u64>) {
/// println!("{plain:?}");
/// }
///
/// let encoded_msg = 3u64 << 60;
///
/// // A plaintext containing a reference can be returned by iterators for example, here is how
/// // to convert them painlessly.
/// let ref_plaintext = PlaintextRef(&encoded_msg);
/// takes_plaintext(ref_plaintext.into());
/// ```
pub struct PlaintextRef<'data, T: Numeric>(pub &'data T);
/// A mutable reference to a plaintext (encoded) value.
///
/// Can be converted to a plaintext via a call to `into`
/// ```rust
/// use tfhe::core_crypto::entities::*;
///
/// pub fn takes_plaintext(plain: Plaintext<u64>) {
/// println!("{plain:?}");
/// }
///
/// let mut encoded_msg = 3u64 << 60;
///
/// // A plaintext containing a reference can be returned by iterators for example, here is how
/// // to convert them painlessly.
/// let ref_plaintext = PlaintextRefMut(&mut encoded_msg);
/// takes_plaintext(ref_plaintext.into());
/// ```
pub struct PlaintextRefMut<'data, T: Numeric>(pub &'data mut T);
impl<'data, T: Numeric> CreateFrom<&'data [T]> for PlaintextRef<'data, T> {
type Metadata = ();
#[inline]
fn create_from(from: &[T], _: Self::Metadata) -> PlaintextRef<'_, T> {
PlaintextRef(&from[0])
}
}
impl<'data, T: Numeric> CreateFrom<&'data mut [T]> for PlaintextRefMut<'data, T> {
type Metadata = ();
#[inline]
fn create_from(from: &mut [T], _: Self::Metadata) -> PlaintextRefMut<'_, T> {
PlaintextRefMut(&mut from[0])
}
}
impl<T: Numeric + Copy> From<PlaintextRef<'_, T>> for Plaintext<T> {
fn from(plaintext_ref: PlaintextRef<T>) -> Self {
Self(*plaintext_ref.0)
}
}
impl<T: Numeric + Copy> From<PlaintextRefMut<'_, T>> for Plaintext<T> {
fn from(plaintext_ref: PlaintextRefMut<T>) -> Self {
Self(*plaintext_ref.0)
}
}