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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
use crate::crypto::PlaintextCount;
use crate::math::polynomial::Polynomial;
use crate::math::tensor::{AsMutSlice, AsMutTensor, AsRefSlice, AsRefTensor, Tensor};
use crate::numeric::Numeric;
use crate::{ck_dim_div, tensor_traits};
/// An plaintext (encoded) value.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct Plaintext<T: Numeric>(pub T);
/// A list of plaintexts
pub struct PlaintextList<Cont> {
pub(crate) tensor: Tensor<Cont>,
}
tensor_traits!(PlaintextList);
impl<Scalar> PlaintextList<Vec<Scalar>>
where
Scalar: Copy,
{
/// Allocates a new list of plaintexts.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// let plain_list = PlaintextList::allocate(1 as u8, PlaintextCount(100));
/// assert_eq!(plain_list.count(), PlaintextCount(100));
/// ```
pub fn allocate(value: Scalar, count: PlaintextCount) -> PlaintextList<Vec<Scalar>> {
PlaintextList::from_container(vec![value; count.0])
}
}
impl<Cont> PlaintextList<Cont> {
/// Creates a plaintext list from a container of values.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// let plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// assert_eq!(plain_list.count(), PlaintextCount(100));
/// ```
pub fn from_container(cont: Cont) -> PlaintextList<Cont> {
PlaintextList {
tensor: Tensor::from_container(cont),
}
}
pub fn from_tensor(tensor: Tensor<Cont>) -> PlaintextList<Cont> {
PlaintextList { tensor }
}
/// Returns the number of elements in the list.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// let plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// assert_eq!(plain_list.count(), PlaintextCount(100));
/// ```
pub fn count(&self) -> PlaintextCount
where
Self: AsRefTensor,
{
PlaintextCount(self.as_tensor().len())
}
/// Creates an iterator over borrowed plaintexts.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// let plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// plain_list.plaintext_iter()
/// .for_each(|a| assert_eq!(a.0, 1));
/// assert_eq!(plain_list.plaintext_iter().count(), 100);
/// ```
pub fn plaintext_iter(&self) -> impl Iterator<Item = &Plaintext<<Self as AsRefTensor>::Element>>
where
Self: AsRefTensor,
<Self as AsRefTensor>::Element: Numeric,
{
self.as_tensor().iter().map(|refe| unsafe {
&*{
refe as *const <Self as AsRefTensor>::Element
as *const Plaintext<<Self as AsRefTensor>::Element>
}
})
}
/// Creates an iterator over mutably borrowed plaintexts.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// let mut plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// plain_list.plaintext_iter_mut()
/// .for_each(|a| *a = Plaintext(2));
/// plain_list.plaintext_iter()
/// .for_each(|a| assert_eq!(a.0, 2));
/// assert_eq!(plain_list.plaintext_iter_mut().count(), 100);
/// ```
pub fn plaintext_iter_mut(
&mut self,
) -> impl Iterator<Item = &mut Plaintext<<Self as AsMutTensor>::Element>>
where
Self: AsMutTensor,
<Self as AsMutTensor>::Element: Numeric,
{
self.as_mut_tensor().iter_mut().map(|refe| unsafe {
&mut *{
refe as *mut <Self as AsMutTensor>::Element
as *mut Plaintext<<Self as AsMutTensor>::Element>
}
})
}
/// Creates an iterator over borrowed sub-lists.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// let mut plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// plain_list.sublist_iter(PlaintextCount(10))
/// .for_each(|a| assert_eq!(a.count(), PlaintextCount(10)));
/// assert_eq!(plain_list.sublist_iter(PlaintextCount(10)).count(), 10);
/// ```
pub fn sublist_iter(
&self,
count: PlaintextCount,
) -> impl Iterator<Item = PlaintextList<&[<Self as AsRefTensor>::Element]>>
where
Self: AsRefTensor,
{
self.as_tensor()
.subtensor_iter(count.0)
.map(|sub| PlaintextList::from_container(sub.into_container()))
}
/// Creates an iterator over mutably borrowed sub-lists.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// let mut plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// plain_list.sublist_iter_mut(PlaintextCount(10))
/// .for_each(|mut a| a.plaintext_iter_mut().for_each(|b| *b = Plaintext(2)));
/// plain_list.plaintext_iter()
/// .for_each(|a| assert_eq!(*a, Plaintext(2)));
/// assert_eq!(plain_list.sublist_iter_mut(PlaintextCount(10)).count(), 10);
/// ```
pub fn sublist_iter_mut(
&mut self,
count: PlaintextCount,
) -> impl Iterator<Item = PlaintextList<&mut [<Self as AsMutTensor>::Element]>>
where
Self: AsMutTensor,
{
ck_dim_div!(self.count().0 => count.0);
self.as_mut_tensor()
.subtensor_iter_mut(count.0)
.map(|sub| PlaintextList::from_container(sub.into_container()))
}
/// Return a borrowed polynomial whose coefficients are the plaintexts of this list.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// use concrete_core::math::polynomial::PolynomialSize;
/// let plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// let plain_poly = plain_list.as_polynomial();
/// assert_eq!(plain_poly.polynomial_size(), PolynomialSize(100));
/// ```
pub fn as_polynomial(&self) -> Polynomial<&[<Self as AsRefTensor>::Element]>
where
Self: AsRefTensor,
{
Polynomial::from_container(self.as_tensor().as_slice())
}
/// Return a mutably borrowed polynomial whose coefficients are the plaintexts of this list.
///
/// # Example
///
/// ```rust
/// use concrete_core::crypto::{PlaintextCount, encoding::*};
/// use concrete_core::math::polynomial::PolynomialSize;
/// let mut plain_list = PlaintextList::from_container(vec![1 as u8; 100]);
/// let mut plain_poly = plain_list.as_mut_polynomial();
/// assert_eq!(plain_poly.polynomial_size(), PolynomialSize(100));
/// ```
pub fn as_mut_polynomial(&mut self) -> Polynomial<&mut [<Self as AsRefTensor>::Element]>
where
Self: AsMutTensor,
{
Polynomial::from_container(self.as_mut_tensor().as_mut_slice())
}
}