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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
//! Module containing the definition of the SeededGgswCiphertext.
use tfhe_versionable::Versionize;
use crate::core_crypto::algorithms::*;
use crate::core_crypto::backward_compatibility::entities::seeded_ggsw_ciphertext::SeededGgswCiphertextVersions;
use crate::core_crypto::commons::generators::{
EncryptionRandomGeneratorForkConfig, MaskRandomGeneratorForkConfig,
};
use crate::core_crypto::commons::math::random::{
CompressionSeed, DefaultRandomGenerator, Distribution, RandomGenerable,
};
use crate::core_crypto::commons::parameters::*;
use crate::core_crypto::commons::traits::*;
use crate::core_crypto::entities::*;
/// A [`seeded GGSW Ciphertext`](`SeededGgswCiphertext`).
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize, Versionize)]
#[versionize(SeededGgswCiphertextVersions)]
pub struct SeededGgswCiphertext<C: Container>
where
C::Element: UnsignedInteger,
{
data: C,
glwe_size: GlweSize,
polynomial_size: PolynomialSize,
decomp_base_log: DecompositionBaseLog,
compression_seed: CompressionSeed,
ciphertext_modulus: CiphertextModulus<C::Element>,
}
impl<T: UnsignedInteger, C: Container<Element = T>> AsRef<[T]> for SeededGgswCiphertext<C> {
fn as_ref(&self) -> &[T] {
self.data.as_ref()
}
}
impl<T: UnsignedInteger, C: ContainerMut<Element = T>> AsMut<[T]> for SeededGgswCiphertext<C> {
fn as_mut(&mut self) -> &mut [T] {
self.data.as_mut()
}
}
/// Return the number of elements in a [`SeededGgswCiphertext`] given a [`GlweSize`],
/// [`PolynomialSize`] and [`DecompositionLevelCount`].
pub fn seeded_ggsw_ciphertext_size(
glwe_size: GlweSize,
polynomial_size: PolynomialSize,
decomp_level_count: DecompositionLevelCount,
) -> usize {
decomp_level_count.0 * seeded_ggsw_level_matrix_size(glwe_size, polynomial_size)
}
/// Return the number of elements in a [`SeededGgswLevelMatrix`] given a [`GlweSize`] and
/// [`PolynomialSize`].
pub fn seeded_ggsw_level_matrix_size(
glwe_size: GlweSize,
polynomial_size: PolynomialSize,
) -> usize {
glwe_size.0 * polynomial_size.0
}
impl<Scalar: UnsignedInteger, C: Container<Element = Scalar>> SeededGgswCiphertext<C> {
/// Create a [`SeededGgswCiphertext`] from an existing container.
///
/// # Note
///
/// This function only wraps a container in the appropriate type. If you want to encrypt data
/// you need to use [`crate::core_crypto::algorithms::encrypt_constant_ggsw_ciphertext`] or its
/// parallel counterpart
/// [`crate::core_crypto::algorithms::par_encrypt_constant_ggsw_ciphertext`] using
/// this ciphertext as output.
///
/// This docstring exhibits [`SeededGgswCiphertext`] primitives usage.
///
/// ```rust
/// use tfhe::core_crypto::prelude::*;
///
/// // DISCLAIMER: these toy example parameters are not guaranteed to be secure or yield correct
/// // computations
/// // Define parameters for SeededGgswCiphertext creation
/// let glwe_size = GlweSize(2);
/// let polynomial_size = PolynomialSize(1024);
/// let decomp_base_log = DecompositionBaseLog(8);
/// let decomp_level_count = DecompositionLevelCount(3);
/// let ciphertext_modulus = CiphertextModulus::new_native();
///
/// // Get a seeder
/// let mut seeder = new_seeder();
/// let seeder = seeder.as_mut();
///
/// // Create a new SeededGgswCiphertext
/// let ggsw = SeededGgswCiphertext::new(
/// 0u64,
/// glwe_size,
/// polynomial_size,
/// decomp_base_log,
/// decomp_level_count,
/// seeder.seed().into(),
/// ciphertext_modulus,
/// );
///
/// assert_eq!(ggsw.glwe_size(), glwe_size);
/// assert_eq!(ggsw.polynomial_size(), polynomial_size);
/// assert_eq!(ggsw.decomposition_base_log(), decomp_base_log);
/// assert_eq!(ggsw.decomposition_level_count(), decomp_level_count);
/// assert_eq!(ggsw.ciphertext_modulus(), ciphertext_modulus);
/// assert_eq!(
/// ggsw.seeded_ggsw_level_matrix_size(),
/// seeded_ggsw_level_matrix_size(glwe_size, polynomial_size)
/// );
///
/// let compression_seed = ggsw.compression_seed();
///
/// // Demonstrate how to recover the allocated container
/// let underlying_container: Vec<u64> = ggsw.into_container();
///
/// // Recreate a ciphertext using from_container
/// let ggsw = SeededGgswCiphertext::from_container(
/// underlying_container,
/// glwe_size,
/// polynomial_size,
/// decomp_base_log,
/// compression_seed,
/// ciphertext_modulus,
/// );
///
/// assert_eq!(ggsw.glwe_size(), glwe_size);
/// assert_eq!(ggsw.polynomial_size(), polynomial_size);
/// assert_eq!(ggsw.decomposition_base_log(), decomp_base_log);
/// assert_eq!(ggsw.decomposition_level_count(), decomp_level_count);
/// assert_eq!(ggsw.ciphertext_modulus(), ciphertext_modulus);
/// assert_eq!(
/// ggsw.seeded_ggsw_level_matrix_size(),
/// seeded_ggsw_level_matrix_size(glwe_size, polynomial_size)
/// );
///
/// let ggsw = ggsw.decompress_into_ggsw_ciphertext();
///
/// assert_eq!(ggsw.glwe_size(), glwe_size);
/// assert_eq!(ggsw.polynomial_size(), polynomial_size);
/// assert_eq!(ggsw.decomposition_base_log(), decomp_base_log);
/// assert_eq!(ggsw.decomposition_level_count(), decomp_level_count);
/// assert_eq!(ggsw.ciphertext_modulus(), ciphertext_modulus);
/// assert_eq!(
/// ggsw.ggsw_level_matrix_size(),
/// ggsw_level_matrix_size(glwe_size, polynomial_size)
/// );
/// ```
pub fn from_container(
container: C,
glwe_size: GlweSize,
polynomial_size: PolynomialSize,
decomp_base_log: DecompositionBaseLog,
compression_seed: CompressionSeed,
ciphertext_modulus: CiphertextModulus<C::Element>,
) -> Self {
assert!(
ciphertext_modulus.is_compatible_with_native_modulus(),
"Seeded entities are not yet compatible with non power of 2 moduli."
);
assert!(
container.container_len() > 0,
"Got an empty container to create a SeededGgswCiphertext"
);
assert!(
container
.container_len()
.is_multiple_of(glwe_size.0 * polynomial_size.0),
"The provided container length is not valid. \
It needs to be dividable by glwe_size * polynomial_size: {}. \
Got container length: {} and glwe_size: {glwe_size:?}, \
polynomial_size: {polynomial_size:?}.",
glwe_size.0 * polynomial_size.0,
container.container_len()
);
Self {
data: container,
glwe_size,
polynomial_size,
decomp_base_log,
compression_seed,
ciphertext_modulus,
}
}
/// Return the [`PolynomialSize`] of the [`SeededGgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn polynomial_size(&self) -> PolynomialSize {
self.polynomial_size
}
/// Return the [`GlweSize`] of the [`SeededGgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn glwe_size(&self) -> GlweSize {
self.glwe_size
}
/// Return the [`DecompositionBaseLog`] of the [`SeededGgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn decomposition_base_log(&self) -> DecompositionBaseLog {
self.decomp_base_log
}
/// Return the [`DecompositionLevelCount`] of the [`SeededGgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn decomposition_level_count(&self) -> DecompositionLevelCount {
DecompositionLevelCount(self.data.container_len() / self.seeded_ggsw_level_matrix_size())
}
/// Return the [`CompressionSeed`] of the [`SeededGgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn compression_seed(&self) -> CompressionSeed {
self.compression_seed.clone()
}
/// Return the [`CiphertextModulus`] of the [`SeededGgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn ciphertext_modulus(&self) -> CiphertextModulus<C::Element> {
self.ciphertext_modulus
}
/// Return the size in number of elements of a single [`SeededGgswLevelMatrix`] of the current
/// [`SeededGgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn seeded_ggsw_level_matrix_size(&self) -> usize {
// GlweSize SeededGlweCiphertext(glwe_size, polynomial_size) per level
seeded_ggsw_level_matrix_size(self.glwe_size, self.polynomial_size)
}
/// Interpret the [`SeededGgswCiphertext`] as a [`PolynomialList`].
pub fn as_polynomial_list(&self) -> PolynomialListView<'_, Scalar> {
PolynomialListView::from_container(self.as_ref(), self.polynomial_size)
}
/// Interpret the [`SeededGgswCiphertext`] as a [`SeededGlweCiphertextList`].
pub fn as_seeded_glwe_list(&self) -> SeededGlweCiphertextListView<'_, Scalar> {
SeededGlweCiphertextListView::from_container(
self.as_ref(),
self.glwe_size(),
self.polynomial_size(),
self.compression_seed(),
self.ciphertext_modulus(),
)
}
/// Return a view of the [`SeededGgswCiphertext`]. This is useful if an algorithm takes a view
/// by value.
pub fn as_view(&self) -> SeededGgswCiphertextView<'_, Scalar> {
SeededGgswCiphertextView::from_container(
self.as_ref(),
self.glwe_size(),
self.polynomial_size(),
self.decomposition_base_log(),
self.compression_seed(),
self.ciphertext_modulus(),
)
}
/// Consume the [`SeededGgswCiphertext`] and decompress it into a standard
/// [`GgswCiphertext`].
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn decompress_into_ggsw_ciphertext(self) -> GgswCiphertextOwned<Scalar>
where
Scalar: UnsignedTorus,
{
let mut decompressed_ct = GgswCiphertextOwned::new(
Scalar::ZERO,
self.glwe_size(),
self.polynomial_size(),
self.decomposition_base_log(),
self.decomposition_level_count(),
self.ciphertext_modulus(),
);
decompress_seeded_ggsw_ciphertext::<_, _, _, DefaultRandomGenerator>(
&mut decompressed_ct,
&self,
);
decompressed_ct
}
/// Parllel variant of
/// [`decompress_into_ggsw_ciphertext`](`Self::decompress_into_ggsw_ciphertext`)
pub fn par_decompress_into_ggsw_ciphertext(self) -> GgswCiphertextOwned<Scalar>
where
Scalar: UnsignedTorus + Send + Sync,
{
let mut decompressed_ct = GgswCiphertextOwned::new(
Scalar::ZERO,
self.glwe_size(),
self.polynomial_size(),
self.decomposition_base_log(),
self.decomposition_level_count(),
self.ciphertext_modulus(),
);
par_decompress_seeded_ggsw_ciphertext::<_, _, _, DefaultRandomGenerator>(
&mut decompressed_ct,
&self,
);
decompressed_ct
}
/// Consume the entity and return its underlying container.
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn into_container(self) -> C {
self.data
}
pub fn encryption_fork_config<MaskDistribution, NoiseDistribution>(
&self,
mask_distribution: MaskDistribution,
noise_distribution: NoiseDistribution,
) -> EncryptionRandomGeneratorForkConfig
where
MaskDistribution: Distribution,
NoiseDistribution: Distribution,
Scalar: RandomGenerable<MaskDistribution, CustomModulus = Scalar>
+ RandomGenerable<NoiseDistribution, CustomModulus = Scalar>,
{
ggsw_ciphertext_encryption_fork_config(
self.glwe_size(),
self.polynomial_size(),
self.decomposition_level_count(),
mask_distribution,
noise_distribution,
self.ciphertext_modulus(),
)
}
pub fn decompression_fork_config<MaskDistribution>(
&self,
mask_distribution: MaskDistribution,
) -> MaskRandomGeneratorForkConfig
where
MaskDistribution: Distribution,
Scalar: RandomGenerable<MaskDistribution, CustomModulus = Scalar>,
{
let decomposition_level_count = self.decomposition_level_count();
let ggsw_level_matrix_mask_sample_count = ggsw_level_matrix_encryption_mask_sample_count(
self.glwe_size(),
self.polynomial_size(),
);
let ciphertext_modulus = self.ciphertext_modulus();
let modulus = ciphertext_modulus.get_custom_modulus_as_optional_scalar();
MaskRandomGeneratorForkConfig::new(
decomposition_level_count.0,
ggsw_level_matrix_mask_sample_count,
mask_distribution,
modulus,
)
}
}
impl<Scalar: UnsignedInteger, C: ContainerMut<Element = Scalar>> SeededGgswCiphertext<C> {
/// Mutable variant of [`SeededGgswCiphertext::as_polynomial_list`].
pub fn as_mut_polynomial_list(&mut self) -> PolynomialListMutView<'_, Scalar> {
let polynomial_size = self.polynomial_size;
PolynomialListMutView::from_container(self.as_mut(), polynomial_size)
}
/// Mutable variant of [`SeededGgswCiphertext::as_seeded_glwe_list`].
pub fn as_mut_seeded_glwe_list(&mut self) -> SeededGlweCiphertextListMutView<'_, Scalar> {
let polynomial_size = self.polynomial_size();
let glwe_size = self.glwe_size();
let compression_seed = self.compression_seed();
let ciphertext_modulus = self.ciphertext_modulus();
SeededGlweCiphertextListMutView::from_container(
self.as_mut(),
glwe_size,
polynomial_size,
compression_seed,
ciphertext_modulus,
)
}
/// Mutable variant of [`SeededGgswCiphertext::as_view`].
pub fn as_mut_view(&mut self) -> SeededGgswCiphertextMutView<'_, Scalar> {
let glwe_size = self.glwe_size();
let polynomial_size = self.polynomial_size();
let decomp_base_log = self.decomposition_base_log();
let compression_seed = self.compression_seed();
let ciphertext_modulus = self.ciphertext_modulus();
SeededGgswCiphertextMutView::from_container(
self.as_mut(),
glwe_size,
polynomial_size,
decomp_base_log,
compression_seed,
ciphertext_modulus,
)
}
}
/// A [`SeededGgswCiphertext`] owning the memory for its own storage.
pub type SeededGgswCiphertextOwned<Scalar> = SeededGgswCiphertext<Vec<Scalar>>;
/// A [`SeededGgswCiphertext`] immutably borrowing memory for its own storage.
pub type SeededGgswCiphertextView<'data, Scalar> = SeededGgswCiphertext<&'data [Scalar]>;
/// A [`SeededGgswCiphertext`] immutably borrowing memory for its own storage.
pub type SeededGgswCiphertextMutView<'data, Scalar> = SeededGgswCiphertext<&'data mut [Scalar]>;
impl<Scalar: UnsignedInteger> SeededGgswCiphertextOwned<Scalar> {
/// Allocate memory and create a new owned [`SeededGgswCiphertext`].
///
/// # Note
///
/// This function allocates a vector of the appropriate size and wraps it in the appropriate
/// type. If you want to encrypt data you need to use
/// [`crate::core_crypto::algorithms::encrypt_constant_ggsw_ciphertext`] or its parallel
/// counterpart [`crate::core_crypto::algorithms::par_encrypt_constant_ggsw_ciphertext`]
/// using this ciphertext as output.
///
/// See [`SeededGgswCiphertext::from_container`] for usage.
pub fn new(
fill_with: Scalar,
glwe_size: GlweSize,
polynomial_size: PolynomialSize,
decomp_base_log: DecompositionBaseLog,
decomp_level_count: DecompositionLevelCount,
compression_seed: CompressionSeed,
ciphertext_modulus: CiphertextModulus<Scalar>,
) -> Self {
Self::from_container(
vec![
fill_with;
seeded_ggsw_ciphertext_size(glwe_size, polynomial_size, decomp_level_count)
],
glwe_size,
polynomial_size,
decomp_base_log,
compression_seed,
ciphertext_modulus,
)
}
}
/// Metadata used in the [`CreateFrom`] implementation to create [`SeededGgswCiphertext`] entities.
#[derive(Clone)]
pub struct SeededGgswCiphertextCreationMetadata<Scalar: UnsignedInteger> {
pub glwe_size: GlweSize,
pub polynomial_size: PolynomialSize,
pub decomp_base_log: DecompositionBaseLog,
pub compression_seed: CompressionSeed,
pub ciphertext_modulus: CiphertextModulus<Scalar>,
}
impl<Scalar: UnsignedInteger, C: Container<Element = Scalar>> CreateFrom<C>
for SeededGgswCiphertext<C>
{
type Metadata = SeededGgswCiphertextCreationMetadata<Scalar>;
#[inline]
fn create_from(from: C, meta: Self::Metadata) -> Self {
let SeededGgswCiphertextCreationMetadata {
glwe_size,
polynomial_size,
decomp_base_log,
compression_seed,
ciphertext_modulus,
} = meta;
Self::from_container(
from,
glwe_size,
polynomial_size,
decomp_base_log,
compression_seed,
ciphertext_modulus,
)
}
}
/// A convenience structure to more easily write iterators on a [`SeededGgswCiphertext`] levels.
pub struct SeededGgswLevelMatrix<C: Container>
where
C::Element: UnsignedInteger,
{
data: C,
glwe_size: GlweSize,
polynomial_size: PolynomialSize,
compression_seed: CompressionSeed,
ciphertext_modulus: CiphertextModulus<C::Element>,
}
impl<Scalar: UnsignedInteger, C: Container<Element = Scalar>> SeededGgswLevelMatrix<C> {
/// Create a [`SeededGgswLevelMatrix`] from an existing container.
///
/// # Note
///
/// This docstring exhibits [`SeededGgswLevelMatrix`] primitives usage.
///
/// ```rust
/// use tfhe::core_crypto::prelude::*;
///
/// // DISCLAIMER: these toy example parameters are not guaranteed to be secure or yield correct
/// // computations
/// // Define parameters for SeededGgswLevelMatrix creation
/// let glwe_size = GlweSize(2);
/// let polynomial_size = PolynomialSize(1024);
/// let ciphertext_modulus = CiphertextModulus::new_native();
///
/// // Get a seeder
/// let mut seeder = new_seeder();
/// let seeder = seeder.as_mut();
///
/// let container = vec![0u64; seeded_ggsw_level_matrix_size(glwe_size, polynomial_size)];
///
/// // Create a new SeededGgswLevelMatrix
/// let ggsw_level_matrix = SeededGgswLevelMatrix::from_container(
/// container,
/// glwe_size,
/// polynomial_size,
/// seeder.seed().into(),
/// ciphertext_modulus,
/// );
///
/// assert_eq!(ggsw_level_matrix.glwe_size(), glwe_size);
/// assert_eq!(ggsw_level_matrix.polynomial_size(), polynomial_size);
/// assert_eq!(ggsw_level_matrix.ciphertext_modulus(), ciphertext_modulus);
/// ```
pub fn from_container(
container: C,
glwe_size: GlweSize,
polynomial_size: PolynomialSize,
compression_seed: CompressionSeed,
ciphertext_modulus: CiphertextModulus<C::Element>,
) -> Self {
assert!(
ciphertext_modulus.is_compatible_with_native_modulus(),
"Seeded entities are not yet compatible with non power of 2 moduli."
);
assert!(
container.container_len() == seeded_ggsw_level_matrix_size(glwe_size, polynomial_size),
"The provided container length is not valid. \
Expected length of {} (glwe_size * polynomial_size), got {}",
seeded_ggsw_level_matrix_size(glwe_size, polynomial_size),
container.container_len(),
);
Self {
data: container,
glwe_size,
polynomial_size,
compression_seed,
ciphertext_modulus,
}
}
/// Return the [`GlweSize`] of the [`SeededGgswLevelMatrix`].
///
/// See [`SeededGgswLevelMatrix::from_container`] for usage.
pub fn glwe_size(&self) -> GlweSize {
self.glwe_size
}
/// Return the [`PolynomialSize`] of the [`SeededGgswLevelMatrix`].
///
/// See [`SeededGgswLevelMatrix::from_container`] for usage.
pub fn polynomial_size(&self) -> PolynomialSize {
self.polynomial_size
}
/// Return the [`CiphertextModulus`] of the [`SeededGgswLevelMatrix`].
///
/// See [`SeededGgswLevelMatrix::from_container`] for usage.
pub fn ciphertext_modulus(&self) -> CiphertextModulus<C::Element> {
self.ciphertext_modulus
}
/// Interpret the [`SeededGgswLevelMatrix`] as a [`SeededGlweCiphertextList`].
pub fn as_seeded_glwe_list(&self) -> SeededGlweCiphertextListView<'_, Scalar> {
SeededGlweCiphertextListView::from_container(
self.data.as_ref(),
self.glwe_size,
self.polynomial_size,
self.compression_seed.clone(),
self.ciphertext_modulus,
)
}
pub fn encryption_fork_config<MaskDistribution, NoiseDistribution>(
&self,
mask_distribution: MaskDistribution,
noise_distribution: NoiseDistribution,
) -> EncryptionRandomGeneratorForkConfig
where
MaskDistribution: Distribution,
NoiseDistribution: Distribution,
Scalar: RandomGenerable<MaskDistribution, CustomModulus = Scalar>
+ RandomGenerable<NoiseDistribution, CustomModulus = Scalar>,
{
ggsw_level_matrix_encryption_fork_config(
self.glwe_size(),
self.polynomial_size(),
mask_distribution,
noise_distribution,
self.ciphertext_modulus(),
)
}
pub fn decompression_fork_config<MaskDistribution>(
&self,
mask_distribution: MaskDistribution,
) -> MaskRandomGeneratorForkConfig
where
MaskDistribution: Distribution,
Scalar: RandomGenerable<MaskDistribution, CustomModulus = Scalar>,
{
let glwe_size = self.glwe_size();
let glwe_ciphertext_mask_sample_count = glwe_ciphertext_encryption_mask_sample_count(
glwe_size.to_glwe_dimension(),
self.polynomial_size(),
);
let ciphertext_modulus = self.ciphertext_modulus();
let modulus = ciphertext_modulus.get_custom_modulus_as_optional_scalar();
MaskRandomGeneratorForkConfig::new(
glwe_size.0,
glwe_ciphertext_mask_sample_count,
mask_distribution,
modulus,
)
}
}
impl<Scalar: UnsignedInteger, C: ContainerMut<Element = Scalar>> SeededGgswLevelMatrix<C> {
/// Mutable variant of [`SeededGgswLevelMatrix::as_seeded_glwe_list`]
pub fn as_mut_seeded_glwe_list(&mut self) -> SeededGlweCiphertextListMutView<'_, Scalar> {
SeededGlweCiphertextListMutView::from_container(
self.data.as_mut(),
self.glwe_size,
self.polynomial_size,
self.compression_seed.clone(),
self.ciphertext_modulus,
)
}
}
/// Metadata used in the [`CreateFrom`] implementation to create [`SeededGgswLevelMatrix`] entities.
#[derive(Clone)]
pub struct SeededGgswLevelMatrixCreationMetadata<Scalar: UnsignedInteger> {
pub glwe_size: GlweSize,
pub polynomial_size: PolynomialSize,
pub compression_seed: CompressionSeed,
pub ciphertext_modulus: CiphertextModulus<Scalar>,
}
impl<Scalar: UnsignedInteger, C: Container<Element = Scalar>> CreateFrom<C>
for SeededGgswLevelMatrix<C>
{
type Metadata = SeededGgswLevelMatrixCreationMetadata<C::Element>;
#[inline]
fn create_from(from: C, meta: Self::Metadata) -> Self {
let SeededGgswLevelMatrixCreationMetadata {
glwe_size,
polynomial_size,
compression_seed,
ciphertext_modulus,
} = meta;
Self::from_container(
from,
glwe_size,
polynomial_size,
compression_seed,
ciphertext_modulus,
)
}
}
impl<Scalar: UnsignedInteger, C: Container<Element = Scalar>> ContiguousEntityContainer
for SeededGgswCiphertext<C>
{
type Element = C::Element;
type EntityViewMetadata = SeededGgswLevelMatrixCreationMetadata<Self::Element>;
type EntityView<'this>
= SeededGgswLevelMatrix<&'this [Self::Element]>
where
Self: 'this;
type SelfViewMetadata = ();
type SelfView<'this>
= DummyCreateFrom
where
Self: 'this;
fn get_entity_view_creation_metadata(&self) -> Self::EntityViewMetadata {
SeededGgswLevelMatrixCreationMetadata {
glwe_size: self.glwe_size,
polynomial_size: self.polynomial_size,
compression_seed: self.compression_seed.clone(),
ciphertext_modulus: self.ciphertext_modulus,
}
}
fn get_entity_view_pod_size(&self) -> usize {
self.seeded_ggsw_level_matrix_size()
}
/// Unimplemented for [`SeededGgswCiphertext`]. At the moment it does not make sense to
/// return "sub" SeededGgswCiphertext.
fn get_self_view_creation_metadata(&self) -> Self::SelfViewMetadata {
unimplemented!(
"This function is not supported for SeededGgswCiphertext. \
At the moment it does not make sense to return 'sub' SeededGgswCiphertext."
)
}
}
impl<Scalar: UnsignedInteger, C: ContainerMut<Element = Scalar>> ContiguousEntityContainerMut
for SeededGgswCiphertext<C>
{
type EntityMutView<'this>
= SeededGgswLevelMatrix<&'this mut [Self::Element]>
where
Self: 'this;
type SelfMutView<'this>
= DummyCreateFrom
where
Self: 'this;
}