parasol_runtime 0.10.0

This crate supports the Parasol CPU, providing key generation, encryption, and FHE evaluation functionality.
Documentation
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
use num::Complex;
use serde::{Deserialize, Serialize};
use sunscreen_tfhe::{
    OverlaySize, PlaintextBits, Torus,
    entities::{
        GgswCiphertext, GgswCiphertextFft, GgswCiphertextFftRef, GgswCiphertextRef, GlevCiphertext,
        GlevCiphertextRef, GlweCiphertext, GlweCiphertextRef, LweCiphertext, LweCiphertextRef,
        Polynomial, PolynomialRef,
    },
    high_level::encryption::{
        decrypt_ggsw, decrypt_glwe, decrypt_lwe, encrypt_binary_msg_rlev,
        encrypt_binary_msg_secret_glev, trivial_binary_glev, trivial_glwe, trivial_lwe,
    },
    ops::encryption::{decrypt_glev_ciphertext, rlwe_encode_encrypt_public},
};

use crate::{error::Result, fluent::CiphertextOps, params::Params, safe_bincode::GetSize};

use super::{PublicKey, SecretKey, TrivialOne, TrivialZero};
use core::mem::size_of;

#[repr(transparent)]
#[derive(Clone, Serialize, Deserialize)]
/// An [`LweCiphertext`] under the level 0 parameters. See [`Params`] for more details as to the
/// significance of these ciphertexts.
pub struct L0LweCiphertext(pub LweCiphertext<u64>);

impl From<LweCiphertext<u64>> for L0LweCiphertext {
    fn from(value: LweCiphertext<u64>) -> Self {
        Self(value)
    }
}

impl TrivialZero for L0LweCiphertext {
    fn trivial_zero(enc: &Encryption) -> Self {
        enc.trivial_lwe_l0_zero()
    }

    fn trivial_zero_from_existing(&self) -> Self {
        L0LweCiphertext(self.0.trivial_zero_from_existing())
    }
}

impl TrivialOne for L0LweCiphertext {
    fn trivial_one(enc: &Encryption) -> Self {
        enc.trivial_lwe_l0_one()
    }
}

#[repr(transparent)]
#[derive(Clone, Serialize, Deserialize)]
/// An [`LweCiphertext`] under the level 1 parameters. See [`Params`] for more details as to the
/// significance of these ciphertexts.
pub struct L1LweCiphertext(pub LweCiphertext<u64>);

impl From<LweCiphertext<u64>> for L1LweCiphertext {
    fn from(value: LweCiphertext<u64>) -> Self {
        Self(value)
    }
}

impl TrivialZero for L1LweCiphertext {
    fn trivial_zero(enc: &Encryption) -> Self {
        enc.trivial_lwe_l1_zero()
    }

    fn trivial_zero_from_existing(&self) -> Self {
        L1LweCiphertext(self.0.trivial_zero_from_existing())
    }
}

impl TrivialOne for L1LweCiphertext {
    fn trivial_one(enc: &Encryption) -> Self {
        enc.trivial_lwe_l1_one()
    }
}

#[repr(transparent)]
#[derive(Clone, Serialize, Deserialize)]
/// A [`GlweCiphertext`] under the level 1 parameters. See [`Params`] for more details as to the
/// significance of these ciphertexts.
pub struct L1GlweCiphertext(pub GlweCiphertext<u64>);

impl From<GlweCiphertext<u64>> for L1GlweCiphertext {
    fn from(value: GlweCiphertext<u64>) -> Self {
        Self(value)
    }
}

impl TrivialZero for L1GlweCiphertext {
    fn trivial_zero(enc: &Encryption) -> Self {
        enc.trivial_glwe_l1_zero()
    }

    fn trivial_zero_from_existing(&self) -> Self {
        L1GlweCiphertext(self.0.trivial_zero_from_existing())
    }
}

impl TrivialZero for L1GlevCiphertext {
    fn trivial_zero(enc: &Encryption) -> Self {
        enc.trivial_glev_l1_zero()
    }

    fn trivial_zero_from_existing(&self) -> Self {
        L1GlevCiphertext(self.0.trivial_zero_from_existing())
    }
}

impl TrivialZero for L1GgswCiphertext {
    fn trivial_zero(enc: &Encryption) -> Self {
        enc.trivial_ggsw_l1_zero()
    }

    fn trivial_zero_from_existing(&self) -> Self {
        L1GgswCiphertext(self.0.trivial_zero_from_existing())
    }
}

impl TrivialOne for L1GlweCiphertext {
    fn trivial_one(enc: &Encryption) -> Self {
        enc.trivial_glwe_l1_one()
    }
}

impl L1GlweCiphertext {
    /// Return if this ciphertext is a trivial encryption
    pub fn is_trivial_encryption(&self, enc: &Encryption) -> bool {
        self.0
            .a(&enc.params.l1_params)
            .all(|x| x.coeffs().iter().all(|x| x.inner() == 0))
    }
}

#[repr(transparent)]
#[derive(Clone)]
/// A [`GgswCiphertext`] under the level 1 parameters. See [`Params`] for more details as to the
/// significance of these ciphertexts.
pub struct L1GgswCiphertext(pub GgswCiphertextFft<Complex<f64>>);

impl From<GgswCiphertextFft<Complex<f64>>> for L1GgswCiphertext {
    fn from(value: GgswCiphertextFft<Complex<f64>>) -> Self {
        Self(value)
    }
}

#[repr(transparent)]
#[derive(Clone, Serialize, Deserialize)]
/// A [`GlevCiphertext`] under the level 1 parameters. See [`Params`] for more details as to the
/// significance of these ciphertexts.
pub struct L1GlevCiphertext(pub GlevCiphertext<u64>);

impl From<GlevCiphertext<u64>> for L1GlevCiphertext {
    fn from(value: GlevCiphertext<u64>) -> Self {
        Self(value)
    }
}

#[derive(Debug, Clone, Default)]
/// A low-level type that allows encrypting and decrypting various ciphertext types.
///
/// # Remarks
/// When interacting with the [`crate::fluent`] API, you should generally use
/// [`crate::fluent::PackedUInt::encrypt`] to create public-key encryptions of integers.
///
/// When using the Parasol processor (another crate), you should use its provided encryption
/// APIs.
pub struct Encryption {
    /// The [`Params`] parameter set this [`Encryption`] object is using.
    pub params: Params,
}

pub(crate) const NUM_PLAINTEXT_BITS: PlaintextBits = PlaintextBits(1);

impl Encryption {
    /// Create a new [`Encryption`] over the given parameter set.
    pub fn new(params: &Params) -> Self {
        Self {
            params: params.clone(),
        }
    }

    /// Allocate a new zero [`L0LweCiphertext`]
    pub fn allocate_lwe_l0(&self) -> L0LweCiphertext {
        LweCiphertext::new(&self.params.l0_params).into()
    }

    /// Allocate a new zero [`L1LweCiphertext`]
    pub fn allocate_lwe_l1(&self) -> L1LweCiphertext {
        LweCiphertext::new(&self.params.l1_params.as_lwe_def()).into()
    }

    /// Allocate a new zero [`L1GgswCiphertext`]
    pub fn allocate_ggsw_l1(&self) -> L1GgswCiphertext {
        GgswCiphertextFft::new(&self.params.l1_params, &self.params.cbs_radix).into()
    }

    /// Allocate a new zero [`L1GlweCiphertext`]
    pub fn allocate_glwe_l1(&self) -> L1GlweCiphertext {
        GlweCiphertext::new(&self.params.l1_params).into()
    }

    /// Allocate a new zero [`L1GlevCiphertext`]
    pub fn allocate_glev_l1(&self) -> L1GlevCiphertext {
        GlevCiphertext::new(&self.params.l1_params, &self.params.cbs_radix).into()
    }

    /// Encrypt `value` as an [`L0LweCiphertext`] under the given [`SecretKey`]
    pub fn encrypt_lwe_l0_secret(&self, value: bool, sk: &SecretKey) -> L0LweCiphertext {
        sk.lwe_0
            .encrypt(value as u64, &self.params.l0_params, NUM_PLAINTEXT_BITS)
            .0
            .into()
    }

    /// Encrypt `value` as an [`L1LweCiphertext`] under the given [`SecretKey`]
    pub fn encrypt_lwe_l1_secret(&self, value: bool, sk: &SecretKey) -> L1LweCiphertext {
        sk.glwe_1
            .to_lwe_secret_key()
            .encrypt(
                value as u64,
                &self.params.l1_params.as_lwe_def(),
                NUM_PLAINTEXT_BITS,
            )
            .0
            .into()
    }

    /// Encrypt `value` as an [`L1GlweCiphertext`] under the given [`SecretKey`]
    pub fn encrypt_glwe_l1_secret(
        &self,
        poly: &PolynomialRef<u64>,
        sk: &SecretKey,
    ) -> L1GlweCiphertext {
        sk.glwe_1
            .encode_encrypt_glwe(poly, &self.params.l1_params, NUM_PLAINTEXT_BITS)
            .into()
    }

    /// Encrypt `value` as an [`L1GlweCiphertext`] under the given [`PublicKey`]. Note that
    /// RLWE is a special case of GLWE where the number of polynomials is 1. This is the case
    /// for the [`crate::DEFAULT_128`] parameter set.
    ///
    /// # Panics
    /// If `self.params.l1_params.dim.size.0 != 1`
    pub fn encrypt_rlwe_l1(&self, msg: &PolynomialRef<u64>, pk: &PublicKey) -> L1GlweCiphertext {
        let mut ct = L1GlweCiphertext::allocate(self);

        rlwe_encode_encrypt_public(
            &mut ct.0,
            msg,
            &pk.rlwe_1,
            &PlaintextBits(1),
            &self.params.l1_params,
        );

        ct
    }

    /// Encrypt `value` as an [`L1GlevCiphertext`] under the given [`SecretKey`]
    pub fn encrypt_glev_l1_secret(
        &self,
        poly: &PolynomialRef<u64>,
        sk: &SecretKey,
    ) -> L1GlevCiphertext {
        encrypt_binary_msg_secret_glev(
            poly,
            &sk.glwe_1,
            &self.params.l1_params,
            &self.params.cbs_radix,
        )
        .into()
    }

    /// Encrypt `value` as an [`L1GlevCiphertext`] under the given [`PublicKey`]. Note that
    /// RLEV is a special case of GLEV where the number of polynomials is 1. This is the case
    /// for the [`crate::DEFAULT_128`] parameter set.
    ///
    /// # Panics
    /// If `self.params.l1_params.dim.size.0 != 1`
    pub fn encrypt_rlev_l1(&self, poly: &PolynomialRef<u64>, pk: &PublicKey) -> L1GlevCiphertext {
        encrypt_binary_msg_rlev(
            poly,
            &pk.rlwe_1,
            &self.params.l1_params,
            &self.params.cbs_radix,
        )
        .into()
    }

    /// Encrypt `value` as an [`L1GgswCiphertext`] under the given [`SecretKey`]
    pub fn encrypt_ggsw_l1_secret(&self, msg: bool, sk: &SecretKey) -> L1GgswCiphertext {
        let mut poly = Polynomial::new(&vec![0u64; self.params.l1_params.dim.polynomial_degree.0]);
        poly.coeffs_mut()[0] = msg as u64;

        let mut ggsw_fft = self.allocate_ggsw_l1();

        sk.glwe_1
            .encode_encrypt_ggsw(
                &poly,
                &self.params.l1_params,
                &self.params.cbs_radix,
                NUM_PLAINTEXT_BITS,
            )
            .fft(
                &mut ggsw_fft.0,
                &self.params.l1_params,
                &self.params.cbs_radix,
            );

        ggsw_fft
    }

    /// Given [`SecretKey`] `sk`, decrypt the given [`L0LweCiphertext`] `input`.
    pub fn decrypt_lwe_l0(&self, input: &L0LweCiphertext, sk: &SecretKey) -> bool {
        decrypt_lwe(
            &input.0,
            &sk.lwe_0,
            &self.params.l0_params,
            NUM_PLAINTEXT_BITS,
        ) == 1
    }

    /// Given [`SecretKey`] `sk`, decrypt the given [`L1LweCiphertext`] `input``.
    pub fn decrypt_lwe_l1(&self, input: &L1LweCiphertext, sk: &SecretKey) -> bool {
        decrypt_lwe(
            &input.0,
            sk.glwe_1.to_lwe_secret_key(),
            &self.params.l1_params.as_lwe_def(),
            NUM_PLAINTEXT_BITS,
        ) == 1
    }

    /// Given [`SecretKey`] `sk`, decrypt the given [`L1GgswCiphertext`] `input`.
    pub fn decrypt_ggsw_l1(&self, input: &L1GgswCiphertext, sk: &SecretKey) -> bool {
        let mut ggsw = GgswCiphertext::<u64>::new(&self.params.l1_params, &self.params.cbs_radix);

        input
            .0
            .ifft(&mut ggsw, &self.params.l1_params, &self.params.cbs_radix);

        let poly = decrypt_ggsw(
            &ggsw,
            &sk.glwe_1,
            &self.params.l1_params,
            &self.params.cbs_radix,
            NUM_PLAINTEXT_BITS,
        );

        poly.coeffs()[0] == 1
    }

    /// Given [`SecretKey`] `sk`, decrypt the given [`L0LweCiphertext`] `input`.
    pub fn decrypt_glwe_l1(&self, ct: &L1GlweCiphertext, sk: &SecretKey) -> Polynomial<u64> {
        decrypt_glwe(
            &ct.0,
            &sk.glwe_1,
            &self.params.l1_params,
            NUM_PLAINTEXT_BITS,
        )
    }

    /// Given [`SecretKey`] `sk`, decrypt the given [`L1GlevCiphertext`] `input`.
    pub fn decrypt_glev_l1(&self, ct: &L1GlevCiphertext, sk: &SecretKey) -> Polynomial<u64> {
        let mut msg = Polynomial::<Torus<u64>>::zero(self.params.l1_params.dim.polynomial_degree.0);

        decrypt_glev_ciphertext(
            &mut msg,
            &ct.0,
            &sk.glwe_1,
            &self.params.l1_params,
            &self.params.cbs_radix,
        );

        msg.map(|x| x.inner())
    }

    /// Create a trivial encryption of zero for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_glwe_l1_zero(&self) -> L1GlweCiphertext {
        let zero = Polynomial::zero(self.params.l1_poly_degree().0);

        self.trivial_glwe_l1(&zero)
    }

    /// Create a trivial encryption of one for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_glwe_l1_one(&self) -> L1GlweCiphertext {
        let mut one = Polynomial::zero(self.params.l1_poly_degree().0);
        one.coeffs_mut()[0] = 1;

        self.trivial_glwe_l1(&one)
    }

    /// Create a trivial encryption of the given polynomial for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_glwe_l1(&self, pt: &PolynomialRef<u64>) -> L1GlweCiphertext {
        trivial_glwe(pt, &self.params.l1_params, NUM_PLAINTEXT_BITS).into()
    }

    /// Create a trivial encryption of zero for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_lwe_l0_zero(&self) -> L0LweCiphertext {
        trivial_lwe(0, &self.params.l0_params, NUM_PLAINTEXT_BITS).into()
    }

    /// Create a trivial encryption of one for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_lwe_l0_one(&self) -> L0LweCiphertext {
        trivial_lwe(1, &self.params.l0_params, NUM_PLAINTEXT_BITS).into()
    }

    /// Create a trivial encryption of zero for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_lwe_l1_zero(&self) -> L1LweCiphertext {
        trivial_lwe(0, &self.params.l1_params.as_lwe_def(), NUM_PLAINTEXT_BITS).into()
    }

    /// Create a trivial encryption of one for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_lwe_l1_one(&self) -> L1LweCiphertext {
        trivial_lwe(1, &self.params.l1_params.as_lwe_def(), NUM_PLAINTEXT_BITS).into()
    }

    /// Create a trivial encryption of zero for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_glev_l1_zero(&self) -> L1GlevCiphertext {
        GlevCiphertext::new(&self.params.l1_params, &self.params.cbs_radix).into()
    }

    /// Create a trivial encryption of one for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_glev_l1_one(&self) -> L1GlevCiphertext {
        let mut msg = Polynomial::zero(self.params.l1_poly_degree().0);
        msg.coeffs_mut()[0] = 1;

        trivial_binary_glev(&msg, &self.params.l1_params, &self.params.cbs_radix).into()
    }

    /// Create a trivial encryption of zero for the returned ciphertext type.
    ///
    /// # Remarks
    /// Trivial encryptions provide no security and decrypt to the desired message under every secret
    /// key. They are useful as constants in FHE programs.
    ///
    /// # Security
    /// We again emphasize that trivial ciphertexts provide no security.
    pub fn trivial_ggsw_l1_zero(&self) -> L1GgswCiphertext {
        GgswCiphertextFft::new(&self.params.l1_params, &self.params.cbs_radix).into()
    }
}

impl GetSize for L0LweCiphertext {
    fn get_size(params: &Params) -> usize {
        (LweCiphertextRef::<u64>::size(params.l0_params.dim) + 1) * size_of::<u64>()
    }

    fn check_is_valid(&self, params: &Params) -> Result<()> {
        Ok(LweCiphertextRef::<u64>::check_is_valid(
            &self.0,
            params.l0_params.dim,
        )?)
    }
}

impl GetSize for L1LweCiphertext {
    fn get_size(params: &Params) -> usize {
        (LweCiphertextRef::<u64>::size(params.l1_params.as_lwe_def().dim) + 1) * size_of::<u64>()
    }

    fn check_is_valid(&self, params: &Params) -> Result<()> {
        Ok(LweCiphertextRef::<u64>::check_is_valid(
            &self.0,
            params.l1_params.as_lwe_def().dim,
        )?)
    }
}

impl GetSize for L1GlweCiphertext {
    fn get_size(params: &Params) -> usize {
        (GlweCiphertextRef::<u64>::size(params.l1_params.dim) + 1) * size_of::<u64>()
    }

    fn check_is_valid(&self, params: &Params) -> Result<()> {
        Ok(GlweCiphertextRef::<u64>::check_is_valid(
            &self.0,
            params.l1_params.dim,
        )?)
    }
}

impl GetSize for L1GgswCiphertext {
    fn get_size(params: &Params) -> usize {
        (GgswCiphertextRef::<u64>::size((params.l1_params.dim, params.cbs_radix.count)) + 1)
            * size_of::<u64>()
    }

    fn check_is_valid(&self, params: &Params) -> Result<()> {
        Ok(GgswCiphertextFftRef::<Complex<f64>>::check_is_valid(
            &self.0,
            (params.l1_params.dim, params.cbs_radix.count),
        )?)
    }
}

impl GetSize for L1GlevCiphertext {
    fn get_size(params: &Params) -> usize {
        (GlevCiphertextRef::<u64>::size((params.l1_params.dim, params.cbs_radix.count)) + 1)
            * size_of::<u64>()
    }

    fn check_is_valid(&self, params: &Params) -> Result<()> {
        Ok(GlevCiphertextRef::<u64>::check_is_valid(
            &self.0,
            (params.l1_params.dim, params.cbs_radix.count),
        )?)
    }
}

#[cfg(test)]
mod tests {
    use rand::{RngCore, rng};

    use crate::{
        DEFAULT_128,
        test_utils::{get_encryption_128, get_secret_keys_128},
    };

    use super::*;

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn can_roundtrip_l0_lwe() {
        let sk = get_secret_keys_128();
        let enc = get_encryption_128();

        let lwe = enc.encrypt_lwe_l0_secret(false, &sk);
        assert!(!enc.decrypt_lwe_l0(&lwe, &sk));

        let lwe = enc.encrypt_lwe_l0_secret(true, &sk);
        assert!(enc.decrypt_lwe_l0(&lwe, &sk));
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn can_roundtrip_l1_lwe() {
        let sk = get_secret_keys_128();
        let enc = get_encryption_128();

        let lwe = enc.encrypt_lwe_l1_secret(false, &sk);
        assert!(!enc.decrypt_lwe_l1(&lwe, &sk));

        let lwe = enc.encrypt_lwe_l1_secret(true, &sk);
        assert!(enc.decrypt_lwe_l1(&lwe, &sk));
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn can_differentiate_trivial_nontrivial_glwe() {
        let secret = get_secret_keys_128();
        let enc = get_encryption_128();

        for _ in 0..100 {
            // generate a random plaintext polynomial
            let num = rng().next_u64();
            let mut pt = Polynomial::zero(DEFAULT_128.l1_poly_degree().0);
            for i in 0..64 {
                pt.coeffs_mut()[i] = (num >> i) & 1;
            }

            assert!(enc.trivial_glwe_l1(&pt).is_trivial_encryption(&enc));

            assert!(
                !enc.encrypt_glwe_l1_secret(&pt, &secret)
                    .is_trivial_encryption(&enc)
            );
        }
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn trivial_zero_glwe1() {
        let secret = get_secret_keys_128();
        let enc = get_encryption_128();

        let zero = enc.trivial_glwe_l1_zero();

        let actual = enc.decrypt_glwe_l1(&zero, &secret);
        let expected = Polynomial::zero(DEFAULT_128.l1_poly_degree().0);

        assert_eq!(actual, expected);
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn trivial_one_glwe1() {
        let secret = get_secret_keys_128();
        let enc = get_encryption_128();

        let zero = enc.trivial_glwe_l1_one();

        let actual = enc.decrypt_glwe_l1(&zero, &secret);
        let mut expected = Polynomial::zero(DEFAULT_128.l1_poly_degree().0);
        expected.coeffs_mut()[0] = 1;

        assert_eq!(actual, expected);
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn trivial_zero_lwe0() {
        let secret = get_secret_keys_128();
        let enc = get_encryption_128();

        let zero = enc.trivial_lwe_l0_zero();

        let actual = enc.decrypt_lwe_l0(&zero, &secret);

        assert!(!actual);
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn trivial_one_lwe0() {
        let secret = get_secret_keys_128();
        let enc = get_encryption_128();

        let one = enc.trivial_lwe_l0_one();

        let actual = enc.decrypt_lwe_l0(&one, &secret);

        assert!(actual);
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn trivial_zero_glev1() {
        let secret = get_secret_keys_128();
        let enc = get_encryption_128();

        let one = enc.trivial_glev_l1_zero();

        let actual = enc.decrypt_glev_l1(&one, &secret);

        assert_eq!(actual, Polynomial::zero(DEFAULT_128.l1_poly_degree().0));
    }

    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    #[test]
    fn trivial_one_glev1() {
        let secret = get_secret_keys_128();
        let enc = get_encryption_128();

        let one = enc.trivial_glev_l1_one();

        let actual = enc.decrypt_glev_l1(&one, &secret);

        let mut expected = Polynomial::zero(DEFAULT_128.l1_poly_degree().0);
        expected.coeffs_mut()[0] = 1;

        assert_eq!(actual, expected);
    }
}