concrete-boolean 0.2.1

Homomorphic Boolean circuit interface for the concrete FHE library.
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
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
use crate::ciphertext::Ciphertext;
use crate::parameters::BooleanParameters;
use crate::{ClientKey, PLAINTEXT_FALSE, PLAINTEXT_TRUE};
use bootstrapping::{BooleanServerKey, Bootstrapper, CpuBootstrapper};
use concrete_core::prelude::*;
use std::cell::RefCell;
pub mod bootstrapping;
use crate::engine::bootstrapping::CpuBootstrapKey;

#[cfg(feature = "cuda")]
use bootstrapping::{CudaBootstrapKey, CudaBootstrapper};

pub(crate) trait BinaryGatesEngine<L, R, K> {
    fn and(&mut self, ct_left: L, ct_right: R, server_key: &K) -> Ciphertext;
    fn nand(&mut self, ct_left: L, ct_right: R, server_key: &K) -> Ciphertext;
    fn nor(&mut self, ct_left: L, ct_right: R, server_key: &K) -> Ciphertext;
    fn or(&mut self, ct_left: L, ct_right: R, server_key: &K) -> Ciphertext;
    fn xor(&mut self, ct_left: L, ct_right: R, server_key: &K) -> Ciphertext;
    fn xnor(&mut self, ct_left: L, ct_right: R, server_key: &K) -> Ciphertext;
}

/// Trait to be able to acces thread_local
/// engines in a generic way
pub(crate) trait WithThreadLocalEngine {
    fn with_thread_local_mut<R, F>(func: F) -> R
    where
        F: FnOnce(&mut Self) -> R;
}

pub(crate) type CpuBooleanEngine = BooleanEngine<CpuBootstrapper>;
#[cfg(feature = "cuda")]
pub(crate) type CudaBooleanEngine = BooleanEngine<CudaBootstrapper>;

// All our thread local engines
// that our exposed types will use internally to implement their methods
thread_local! {
    static CPU_ENGINE: RefCell<BooleanEngine<CpuBootstrapper>> = RefCell::new(BooleanEngine::<_>::new());
    #[cfg(feature = "cuda")]
    static CUDA_ENGINE: RefCell<BooleanEngine<CudaBootstrapper>> = RefCell::new(BooleanEngine::<_>::new());
}

impl WithThreadLocalEngine for CpuBooleanEngine {
    fn with_thread_local_mut<R, F>(func: F) -> R
    where
        F: FnOnce(&mut Self) -> R,
    {
        CPU_ENGINE.with(|engine_cell| func(&mut engine_cell.borrow_mut()))
    }
}

#[cfg(feature = "cuda")]
impl WithThreadLocalEngine for CudaBooleanEngine {
    fn with_thread_local_mut<R, F>(func: F) -> R
    where
        F: FnOnce(&mut Self) -> R,
    {
        CUDA_ENGINE.with(|engine_cell| func(&mut engine_cell.borrow_mut()))
    }
}

pub(crate) struct BooleanEngine<B> {
    pub(crate) engine: DefaultEngine,
    bootstrapper: B,
}

impl BooleanEngine<CpuBootstrapper> {
    pub fn create_server_key(&mut self, cks: &ClientKey) -> CpuBootstrapKey {
        let server_key = self.bootstrapper.new_server_key(cks).unwrap();

        server_key
    }
}

#[cfg(feature = "cuda")]
impl BooleanEngine<CudaBootstrapper> {
    pub fn create_server_key(&mut self, cpu_key: &CpuBootstrapKey) -> CudaBootstrapKey {
        let server_key = self.bootstrapper.new_serverk_key(cpu_key).unwrap();

        server_key
    }
}

impl<B> BooleanEngine<B> {
    pub fn create_client_key(&mut self, parameters: BooleanParameters) -> ClientKey {
        // generate the lwe secret key
        let lwe_secret_key: LweSecretKey32 = self
            .engine
            .generate_new_lwe_secret_key(parameters.lwe_dimension)
            .unwrap();

        // generate the rlwe secret key
        let glwe_secret_key: GlweSecretKey32 = self
            .engine
            .generate_new_glwe_secret_key(parameters.glwe_dimension, parameters.polynomial_size)
            .unwrap();

        ClientKey {
            lwe_secret_key,
            glwe_secret_key,
            parameters,
        }
    }
    pub fn trivial_encrypt(&mut self, message: bool) -> Ciphertext {
        Ciphertext::Trivial(message)
    }

    pub fn encrypt(&mut self, message: bool, cks: &ClientKey) -> Ciphertext {
        // encode the boolean message
        let plain: Plaintext32 = if message {
            self.engine.create_plaintext_from(&PLAINTEXT_TRUE).unwrap()
        } else {
            self.engine.create_plaintext_from(&PLAINTEXT_FALSE).unwrap()
        };

        // convert into a variance
        let var = Variance(cks.parameters.lwe_modular_std_dev.get_variance());

        // encryption
        let ct = self
            .engine
            .encrypt_lwe_ciphertext(&cks.lwe_secret_key, &plain, var)
            .unwrap();

        Ciphertext::Encrypted(ct)
    }

    pub fn decrypt(&mut self, ct: &Ciphertext, cks: &ClientKey) -> bool {
        match ct {
            Ciphertext::Trivial(b) => *b,
            Ciphertext::Encrypted(ciphertext) => {
                // decryption
                let decrypted = self
                    .engine
                    .decrypt_lwe_ciphertext(&cks.lwe_secret_key, ciphertext)
                    .unwrap();

                // cast as a u32
                let mut decrypted_u32: u32 = 0;
                self.engine
                    .discard_retrieve_plaintext(&mut decrypted_u32, &decrypted)
                    .unwrap();

                // return
                decrypted_u32 < (1 << 31)
            }
        }
    }

    pub fn not(&mut self, ct: &Ciphertext) -> Ciphertext {
        match ct {
            Ciphertext::Trivial(message) => Ciphertext::Trivial(!*message),
            Ciphertext::Encrypted(ct_ct) => {
                // Compute the linear combination for NOT: -ct
                let mut ct_res = ct_ct.clone();
                self.engine.fuse_opp_lwe_ciphertext(&mut ct_res).unwrap(); // compute the negation

                // Output the result:
                Ciphertext::Encrypted(ct_res)
            }
        }
    }
}

fn new_seeder() -> Box<dyn Seeder> {
    let seeder: Box<dyn Seeder>;
    #[cfg(target_arch = "x86_64")]
    {
        if RdseedSeeder::is_available() {
            seeder = Box::new(RdseedSeeder);
        } else {
            seeder = Box::new(UnixSeeder::new(0));
        }
    }
    #[cfg(not(target_arch = "x86_64"))]
    {
        seeder = Box::new(UnixSeeder::new(0));
    }

    seeder
}

impl<B> BooleanEngine<B>
where
    B: Bootstrapper,
{
    pub fn new() -> Self {
        let engine =
            DefaultEngine::new(new_seeder()).expect("Unexpectedly failed to create a core engine");

        Self {
            engine,
            bootstrapper: Default::default(),
        }
    }
    /// convert into an actual LWE ciphertext even when trivial
    fn convert_into_lwe_ciphertext_32(
        &mut self,
        ct: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> LweCiphertext32 {
        match ct {
            Ciphertext::Encrypted(ct_ct) => ct_ct.clone(),
            Ciphertext::Trivial(message) => {
                // encode the boolean message
                let plain: Plaintext32 = if *message {
                    self.engine.create_plaintext_from(&PLAINTEXT_TRUE).unwrap()
                } else {
                    self.engine.create_plaintext_from(&PLAINTEXT_FALSE).unwrap()
                };
                self.engine
                    .trivially_encrypt_lwe_ciphertext(server_key.lwe_size(), &plain)
                    .unwrap()
            }
        }
    }

    pub fn mux(
        &mut self,
        ct_condition: &Ciphertext,
        ct_then: &Ciphertext,
        ct_else: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        // In theory MUX gate = (ct_condition AND ct_then) + (!ct_condition AND ct_else)

        match ct_condition {
            // in the case of the condition is trivially encrypted
            Ciphertext::Trivial(message_condition) => {
                if *message_condition {
                    ct_then.clone()
                } else {
                    ct_else.clone()
                }
            }
            Ciphertext::Encrypted(ct_condition_ct) => {
                // condition is actually encrypted

                // take a shortcut if ct_then is trivially encrypted
                if let Ciphertext::Trivial(message_then) = ct_then {
                    return if *message_then {
                        self.or(ct_condition, ct_else, server_key)
                    } else {
                        let ct_not_condition = self.not(ct_condition);
                        self.and(&ct_not_condition, ct_else, server_key)
                    };
                }

                // take a shortcut if ct_else is trivially encrypted
                if let Ciphertext::Trivial(message_else) = ct_else {
                    return if *message_else {
                        let ct_not_condition = self.not(ct_condition);
                        self.or(ct_then, &ct_not_condition, server_key)
                    } else {
                        self.and(ct_condition, ct_then, server_key)
                    };
                }

                // convert inputs into LweCiphertext32
                let ct_then_ct = self.convert_into_lwe_ciphertext_32(ct_then, server_key);
                let ct_else_ct = self.convert_into_lwe_ciphertext_32(ct_else, server_key);

                let mut buffer_lwe_before_pbs_o = self
                    .engine
                    .create_lwe_ciphertext_from(vec![0u32; server_key.lwe_size().0])
                    .unwrap();
                let buffer_lwe_before_pbs = &mut buffer_lwe_before_pbs_o;
                let bootstrapper = &mut self.bootstrapper;

                // Compute the linear combination for first AND: ct_condition + ct_then +
                // (0,...,0,-1/8)
                self.engine
                    .discard_add_lwe_ciphertext(buffer_lwe_before_pbs, ct_condition_ct, &ct_then_ct)
                    .unwrap(); // ct_condition + ct_then
                let cst = self.engine.create_plaintext_from(&PLAINTEXT_FALSE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(buffer_lwe_before_pbs, &cst)
                    .unwrap(); //
                               // - 1/8

                // Compute the linear combination for second AND: - ct_condition + ct_else +
                // (0,...,0,-1/8)
                let mut ct_temp_2 = ct_condition_ct.clone(); // ct_condition
                self.engine.fuse_opp_lwe_ciphertext(&mut ct_temp_2).unwrap(); // compute the negation
                self.engine
                    .fuse_add_lwe_ciphertext(&mut ct_temp_2, &ct_else_ct)
                    .unwrap(); // + ct_else
                let cst = self.engine.create_plaintext_from(&PLAINTEXT_FALSE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut ct_temp_2, &cst)
                    .unwrap(); //
                               // - 1/8

                // Compute the first programmable bootstrapping with fixed test polynomial:
                let mut ct_pbs_1 = bootstrapper
                    .bootstrap(buffer_lwe_before_pbs, server_key)
                    .unwrap();

                let ct_pbs_2 = bootstrapper.bootstrap(&ct_temp_2, server_key).unwrap();

                // Compute the linear combination to add the two results:
                // buffer_lwe_pbs + ct_pbs_2 + (0,...,0, +1/8)
                self.engine
                    .fuse_add_lwe_ciphertext(&mut ct_pbs_1, &ct_pbs_2)
                    .unwrap(); // + buffer_lwe_pbs
                let cst = self.engine.create_plaintext_from(&PLAINTEXT_TRUE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut ct_pbs_1, &cst)
                    .unwrap(); // + 1/8

                let ct_ks = bootstrapper.keyswitch(&ct_pbs_1, server_key).unwrap();

                // Output the result:
                Ciphertext::Encrypted(ct_ks)
            }
        }
    }
}

impl<B> BinaryGatesEngine<&Ciphertext, &Ciphertext, B::ServerKey> for BooleanEngine<B>
where
    B: Bootstrapper,
{
    fn and(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        match (ct_left, ct_right) {
            (Ciphertext::Trivial(message_left), Ciphertext::Trivial(message_right)) => {
                Ciphertext::Trivial(*message_left && *message_right)
            }
            (Ciphertext::Encrypted(_), Ciphertext::Trivial(message_right)) => {
                self.and(ct_left, *message_right, server_key)
            }
            (Ciphertext::Trivial(message_left), Ciphertext::Encrypted(_)) => {
                self.and(*message_left, ct_right, server_key)
            }
            (Ciphertext::Encrypted(ct_left_ct), Ciphertext::Encrypted(ct_right_ct)) => {
                let mut buffer_lwe_before_pbs = self
                    .engine
                    .create_lwe_ciphertext_from(vec![0u32; server_key.lwe_size().0])
                    .unwrap();
                let bootstrapper = &mut self.bootstrapper;

                // compute the linear combination for AND: ct_left + ct_right + (0,...,0,-1/8)
                self.engine
                    .discard_add_lwe_ciphertext(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct)
                    .unwrap(); // ct_left + ct_right
                let cst = self.engine.create_plaintext_from(&PLAINTEXT_FALSE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut buffer_lwe_before_pbs, &cst)
                    .unwrap(); //
                               // - 1/8

                // compute the bootstrap and the key switch
                bootstrapper
                    .bootstrap_keyswitch(buffer_lwe_before_pbs, server_key)
                    .unwrap()
            }
        }
    }

    fn nand(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        match (ct_left, ct_right) {
            (Ciphertext::Trivial(message_left), Ciphertext::Trivial(message_right)) => {
                Ciphertext::Trivial(!(*message_left && *message_right))
            }
            (Ciphertext::Encrypted(_), Ciphertext::Trivial(message_right)) => {
                self.nand(ct_left, *message_right, server_key)
            }
            (Ciphertext::Trivial(message_left), Ciphertext::Encrypted(_)) => {
                self.nand(*message_left, ct_right, server_key)
            }
            (Ciphertext::Encrypted(ct_left_ct), Ciphertext::Encrypted(ct_right_ct)) => {
                let mut buffer_lwe_before_pbs = self
                    .engine
                    .create_lwe_ciphertext_from(vec![0u32; server_key.lwe_size().0])
                    .unwrap();
                let bootstrapper = &mut self.bootstrapper;

                // Compute the linear combination for NAND: - ct_left - ct_right + (0,...,0,1/8)
                self.engine
                    .discard_add_lwe_ciphertext(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct)
                    .unwrap(); // ct_left + ct_right
                self.engine
                    .fuse_opp_lwe_ciphertext(&mut buffer_lwe_before_pbs)
                    .unwrap(); // compute the negation
                let cst = self.engine.create_plaintext_from(&PLAINTEXT_TRUE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut buffer_lwe_before_pbs, &cst)
                    .unwrap(); // + 1/8

                // compute the bootstrap and the key switch
                bootstrapper
                    .bootstrap_keyswitch(buffer_lwe_before_pbs, server_key)
                    .unwrap()
            }
        }
    }

    fn nor(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        match (ct_left, ct_right) {
            (Ciphertext::Trivial(message_left), Ciphertext::Trivial(message_right)) => {
                Ciphertext::Trivial(!(*message_left || *message_right))
            }
            (Ciphertext::Encrypted(_), Ciphertext::Trivial(message_right)) => {
                self.nor(ct_left, *message_right, server_key)
            }
            (Ciphertext::Trivial(message_left), Ciphertext::Encrypted(_)) => {
                self.nor(*message_left, ct_right, server_key)
            }
            (Ciphertext::Encrypted(ct_left_ct), Ciphertext::Encrypted(ct_right_ct)) => {
                let mut buffer_lwe_before_pbs = self
                    .engine
                    .create_lwe_ciphertext_from(vec![0u32; server_key.lwe_size().0])
                    .unwrap();
                let bootstrapper = &mut self.bootstrapper;

                // Compute the linear combination for NOR: - ct_left - ct_right + (0,...,0,-1/8)
                self.engine
                    .discard_add_lwe_ciphertext(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct)
                    .unwrap(); // ct_left + ct_right
                self.engine
                    .fuse_opp_lwe_ciphertext(&mut buffer_lwe_before_pbs)
                    .unwrap(); // compute the negation
                let cst = self.engine.create_plaintext_from(&PLAINTEXT_FALSE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut buffer_lwe_before_pbs, &cst)
                    .unwrap(); //
                               // - 1/8

                // compute the bootstrap and the key switch
                bootstrapper
                    .bootstrap_keyswitch(buffer_lwe_before_pbs, server_key)
                    .unwrap()
            }
        }
    }

    fn or(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        match (ct_left, ct_right) {
            (Ciphertext::Trivial(message_left), Ciphertext::Trivial(message_right)) => {
                Ciphertext::Trivial(*message_left || *message_right)
            }
            (Ciphertext::Encrypted(_), Ciphertext::Trivial(message_right)) => {
                self.or(ct_left, *message_right, server_key)
            }
            (Ciphertext::Trivial(message_left), Ciphertext::Encrypted(_)) => {
                self.or(*message_left, ct_right, server_key)
            }
            (Ciphertext::Encrypted(ct_left_ct), Ciphertext::Encrypted(ct_right_ct)) => {
                let mut buffer_lwe_before_pbs = self
                    .engine
                    .create_lwe_ciphertext_from(vec![0u32; server_key.lwe_size().0])
                    .unwrap();
                let bootstrapper = &mut self.bootstrapper;

                // Compute the linear combination for OR: ct_left + ct_right + (0,...,0,+1/8)
                self.engine
                    .discard_add_lwe_ciphertext(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct)
                    .unwrap(); // ct_left + ct_right
                let cst = self.engine.create_plaintext_from(&PLAINTEXT_TRUE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut buffer_lwe_before_pbs, &cst)
                    .unwrap(); // + 1/8

                // compute the bootstrap and the key switch
                bootstrapper
                    .bootstrap_keyswitch(buffer_lwe_before_pbs, server_key)
                    .unwrap()
            }
        }
    }

    fn xor(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        match (ct_left, ct_right) {
            (Ciphertext::Trivial(message_left), Ciphertext::Trivial(message_right)) => {
                Ciphertext::Trivial(*message_left ^ *message_right)
            }
            (Ciphertext::Encrypted(_), Ciphertext::Trivial(message_right)) => {
                self.xor(ct_left, *message_right, server_key)
            }
            (Ciphertext::Trivial(message_left), Ciphertext::Encrypted(_)) => {
                self.xor(*message_left, ct_right, server_key)
            }
            (Ciphertext::Encrypted(ct_left_ct), Ciphertext::Encrypted(ct_right_ct)) => {
                let mut buffer_lwe_before_pbs = self
                    .engine
                    .create_lwe_ciphertext_from(vec![0u32; server_key.lwe_size().0])
                    .unwrap();
                let bootstrapper = &mut self.bootstrapper;

                // Compute the linear combination for XOR: 2*(ct_left + ct_right) + (0,...,0,1/4)
                self.engine
                    .discard_add_lwe_ciphertext(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct)
                    .unwrap(); // ct_left + ct_right
                let cst_add = self.engine.create_plaintext_from(&PLAINTEXT_TRUE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut buffer_lwe_before_pbs, &cst_add)
                    .unwrap(); // + 1/8
                let cst_mul = self.engine.create_cleartext_from(&2u32).unwrap();
                self.engine
                    .fuse_mul_lwe_ciphertext_cleartext(&mut buffer_lwe_before_pbs, &cst_mul)
                    .unwrap(); //* 2

                // compute the bootstrap and the key switch
                bootstrapper
                    .bootstrap_keyswitch(buffer_lwe_before_pbs, server_key)
                    .unwrap()
            }
        }
    }

    fn xnor(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        match (ct_left, ct_right) {
            (Ciphertext::Trivial(message_left), Ciphertext::Trivial(message_right)) => {
                Ciphertext::Trivial(!(*message_left ^ *message_right))
            }
            (Ciphertext::Encrypted(_), Ciphertext::Trivial(message_right)) => {
                self.xnor(ct_left, *message_right, server_key)
            }
            (Ciphertext::Trivial(message_left), Ciphertext::Encrypted(_)) => {
                self.xnor(*message_left, ct_right, server_key)
            }
            (Ciphertext::Encrypted(ct_left_ct), Ciphertext::Encrypted(ct_right_ct)) => {
                let mut buffer_lwe_before_pbs = self
                    .engine
                    .create_lwe_ciphertext_from(vec![0u32; server_key.lwe_size().0])
                    .unwrap();
                let bootstrapper = &mut self.bootstrapper;

                // Compute the linear combination for XNOR: 2*(-ct_left - ct_right + (0,...,0,-1/8))
                self.engine
                    .discard_add_lwe_ciphertext(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct)
                    .unwrap(); // ct_left + ct_right
                let cst_add = self.engine.create_plaintext_from(&PLAINTEXT_TRUE).unwrap();
                self.engine
                    .fuse_add_lwe_ciphertext_plaintext(&mut buffer_lwe_before_pbs, &cst_add)
                    .unwrap(); // + 1/8
                self.engine
                    .fuse_opp_lwe_ciphertext(&mut buffer_lwe_before_pbs)
                    .unwrap(); // compute the negation
                let cst_mul = self.engine.create_cleartext_from(&2u32).unwrap();
                self.engine
                    .fuse_mul_lwe_ciphertext_cleartext(&mut buffer_lwe_before_pbs, &cst_mul)
                    .unwrap(); //* 2

                // compute the bootstrap and the key switch
                bootstrapper
                    .bootstrap_keyswitch(buffer_lwe_before_pbs, server_key)
                    .unwrap()
            }
        }
    }
}

impl<B> BinaryGatesEngine<&Ciphertext, bool, B::ServerKey> for BooleanEngine<B>
where
    B: Bootstrapper,
{
    fn and(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: bool,
        _server_key: &B::ServerKey,
    ) -> Ciphertext {
        if ct_right {
            // ct AND true = ct
            ct_left.clone()
        } else {
            // ct AND false = false
            self.trivial_encrypt(false)
        }
    }

    fn nand(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: bool,
        _server_key: &B::ServerKey,
    ) -> Ciphertext {
        if ct_right {
            // NOT (ct AND true) = NOT(ct)
            self.not(ct_left)
        } else {
            // NOT (ct AND false) = NOT(false) = true
            self.trivial_encrypt(true)
        }
    }

    fn nor(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: bool,
        _server_key: &B::ServerKey,
    ) -> Ciphertext {
        if ct_right {
            // NOT (ct OR true) = NOT(true) = false
            self.trivial_encrypt(false)
        } else {
            // NOT (ct OR false) = NOT(ct)
            self.not(ct_left)
        }
    }

    fn or(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: bool,
        _server_key: &B::ServerKey,
    ) -> Ciphertext {
        if ct_right {
            // ct OR true = true
            self.trivial_encrypt(true)
        } else {
            // ct OR false = ct
            ct_left.clone()
        }
    }

    fn xor(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: bool,
        _server_key: &B::ServerKey,
    ) -> Ciphertext {
        if ct_right {
            // ct XOR true = NOT(ct)
            self.not(ct_left)
        } else {
            // ct XOR false = ct
            ct_left.clone()
        }
    }

    fn xnor(
        &mut self,
        ct_left: &Ciphertext,
        ct_right: bool,
        _server_key: &B::ServerKey,
    ) -> Ciphertext {
        if ct_right {
            // NOT(ct XOR true) = NOT(NOT(ct)) = ct
            ct_left.clone()
        } else {
            // NOT(ct XOR false) = NOT(ct)
            self.not(ct_left)
        }
    }
}

impl<B> BinaryGatesEngine<bool, &Ciphertext, B::ServerKey> for BooleanEngine<B>
where
    B: Bootstrapper,
{
    fn and(
        &mut self,
        ct_left: bool,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        self.and(ct_right, ct_left, server_key)
    }

    fn nand(
        &mut self,
        ct_left: bool,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        self.nand(ct_right, ct_left, server_key)
    }

    fn nor(
        &mut self,
        ct_left: bool,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        self.nor(ct_right, ct_left, server_key)
    }

    fn or(
        &mut self,
        ct_left: bool,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        self.or(ct_right, ct_left, server_key)
    }

    fn xor(
        &mut self,
        ct_left: bool,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        self.xor(ct_right, ct_left, server_key)
    }

    fn xnor(
        &mut self,
        ct_left: bool,
        ct_right: &Ciphertext,
        server_key: &B::ServerKey,
    ) -> Ciphertext {
        self.xnor(ct_right, ct_left, server_key)
    }
}