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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use tfhe_versionable::Versionize;
use crate::conformance::ParameterSetConformant;
use crate::core_crypto::backward_compatibility::entities::packed_integers::PackedIntegersVersions;
use crate::core_crypto::prelude::*;
#[derive(Clone, Debug, Eq, PartialEq, serde::Serialize, serde::Deserialize, Versionize)]
#[versionize(PackedIntegersVersions)]
pub struct PackedIntegers<Scalar: UnsignedInteger> {
packed_coeffs: Vec<Scalar>,
log_modulus: CiphertextModulusLog,
initial_len: usize,
}
impl<Scalar: UnsignedInteger> PackedIntegers<Scalar> {
pub(crate) fn from_raw_parts(
packed_coeffs: Vec<Scalar>,
log_modulus: CiphertextModulusLog,
initial_len: usize,
) -> Self {
let required_bits_packed = initial_len * log_modulus.0;
let expected_len = required_bits_packed.div_ceil(Scalar::BITS);
assert_eq!(
packed_coeffs.len(),
expected_len,
"Invalid size for the packed coeffs, got {}, expected {}",
packed_coeffs.len(),
expected_len
);
Self {
packed_coeffs,
log_modulus,
initial_len,
}
}
pub fn pack<InputScalar: UnsignedInteger + CastInto<Scalar>>(
slice: &[InputScalar],
log_modulus: CiphertextModulusLog,
) -> Self {
assert!(log_modulus.0 <= InputScalar::BITS);
assert!(log_modulus.0 <= Scalar::BITS);
let log_modulus = log_modulus.0;
let in_len = slice.len();
assert!(log_modulus <= Scalar::BITS);
let number_bits_to_pack = in_len * log_modulus;
let len = number_bits_to_pack.div_ceil(Scalar::BITS);
// Lowest bits are on the right
//
// Target mapping:
// log_modulus
// |-------|
//
// slice : | k+2 | k+1 | k |
// packed_coeffs: i+1 | i | i-1
//
// |---------------|
// Scalar::BITS
//
// |---|
// start_shift
//
// |---|
// shift1
// (1st loop iteration)
//
// |-----------|
// shift2
// (2nd loop iteration)
//
// packed_coeffs[i] =
// slice[k] >> start_shift
// | slice[k+1] << shift1
// | slice[k+2] << shift2
//
// In the lowest bits of packed_coeffs[i], we want the highest bits of slice[k],
// hence the right shift
// The next bits should be the bits of slice[k+1] which we must left shifted to avoid
// overlapping
// This goes on
let packed_coeffs = (0..len)
.map(|i| {
let k = Scalar::BITS * i / log_modulus;
let mut j = k;
let start_shift = i * Scalar::BITS - j * log_modulus;
debug_assert!(
log_modulus == InputScalar::BITS
|| (slice[j] >> log_modulus == InputScalar::ZERO)
);
let value: Scalar = slice[j].cast_into();
let mut value = value >> start_shift;
j += 1;
while j * log_modulus < ((i + 1) * Scalar::BITS) && j < slice.len() {
let shift = j * log_modulus - i * Scalar::BITS;
debug_assert!(
log_modulus == InputScalar::BITS
|| (slice[j] >> log_modulus == InputScalar::ZERO)
);
let value2: Scalar = slice[j].cast_into();
value |= value2 << shift;
j += 1;
}
value
})
.collect();
let log_modulus = CiphertextModulusLog(log_modulus);
Self {
packed_coeffs,
log_modulus,
initial_len: slice.len(),
}
}
pub fn unpack<OutputScalar>(&self) -> impl Iterator<Item = OutputScalar> + '_
where
Scalar: CastInto<OutputScalar>,
OutputScalar: UnsignedInteger,
{
let log_modulus = self.log_modulus.0;
assert!(log_modulus <= Scalar::BITS);
assert!(log_modulus <= OutputScalar::BITS);
// log_modulus lowest bits set to 1
let mask = if log_modulus < Scalar::BITS {
(Scalar::ONE << log_modulus) - Scalar::ONE
} else {
assert_eq!(log_modulus, Scalar::BITS);
Scalar::MAX
};
(0..self.initial_len).map(move |i| {
let start = i * log_modulus;
let end = (i + 1) * log_modulus;
let start_block = start / Scalar::BITS;
let start_remainder = start % Scalar::BITS;
let end_block_inclusive = (end - 1) / Scalar::BITS;
let result = if start_block == end_block_inclusive {
// Lowest bits are on the right
//
// Target mapping:
// Scalar::BITS
// |---------------|
//
// packed_coeffs: | start_block+1 | start_block |
// container : | i+1 | i | i-1 |
//
// |-------|
// log_modulus
//
// |---|
// start_remainder
//
// In container[i] we want the bits of packed_coeffs[start_block] starting from
// index start_remainder
//
// container[i] = lowest_bits of single_part
//
let single_part = self.packed_coeffs[start_block] >> start_remainder;
single_part & mask
} else {
// Lowest bits are on the right
//
// Target mapping:
// Scalar::BITS
// |---------------|
//
// packed_coeffs: | start_block+1 | start_block |
// container : | i+1 | i | i-1 |
//
// |-------|
// log_modulus
//
// |-----------|
// start_remainder
//
// |---|
// Scalar::BITS - start_remainder
//
// In the lowest bits of container[i] we want the highest bits of
// packed_coeffs[start_block] starting from index start_remainder
//
// In the next bits, we want the lowest bits of packed_coeffs[start_block + 1]
// left shifted to avoid overlapping
//
// container[i] = lowest_bits of (first_part|second_part)
//
assert_eq!(end_block_inclusive, start_block + 1);
let first_part = self.packed_coeffs[start_block] >> start_remainder;
let second_part =
self.packed_coeffs[start_block + 1] << (Scalar::BITS - start_remainder);
(first_part | second_part) & mask
};
result.cast_into()
})
}
pub fn log_modulus(&self) -> CiphertextModulusLog {
self.log_modulus
}
pub fn packed_coeffs(&self) -> &[Scalar] {
&self.packed_coeffs
}
pub fn initial_len(&self) -> usize {
self.initial_len
}
}
impl<Scalar: UnsignedInteger> ParameterSetConformant for PackedIntegers<Scalar> {
type ParameterSet = usize;
fn is_conformant(&self, len: &usize) -> bool {
let Self {
packed_coeffs,
log_modulus,
initial_len,
} = self;
let number_packed_bits = *len * log_modulus.0;
let packed_len = number_packed_bits.div_ceil(Scalar::BITS);
*len == *initial_len && packed_coeffs.len() == packed_len
}
}
#[cfg(test)]
mod test {
use super::*;
use rand::{Fill, Rng};
#[test]
fn pack_unpack() {
pack_unpack_single::<u64>(32, 700);
pack_unpack_single::<u64>(27, 700);
pack_unpack_single::<u64>(64, 700);
pack_unpack_single::<u128>(64, 700);
pack_unpack_single::<u128>(79, 700);
pack_unpack_single::<u128>(128, 700);
}
fn pack_unpack_single<Scalar>(log_modulus: usize, len: usize)
where
[Scalar]: Fill,
Scalar: UnsignedInteger + CastFrom<usize>,
{
let mut cont = vec![Scalar::ZERO; len];
rand::thread_rng().fill(cont.as_mut_slice());
for val in cont.iter_mut() {
*val %= log_modulus.cast_into();
}
let packed = PackedIntegers::<Scalar>::pack(&cont, CiphertextModulusLog(log_modulus));
let unpacked: Vec<Scalar> = packed.unpack().collect();
assert_eq!(cont, unpacked);
}
}