/
mod.rs
1082 lines (955 loc) · 40.2 KB
/
mod.rs
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
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Module with the engine definitions.
//!
//! Engines are required to abstract cryptographic notions and efficiently manage memory from the
//! underlying `core_crypto` module.
use crate::boolean::ciphertext::{Ciphertext, CompressedCiphertext};
use crate::boolean::parameters::{
BooleanKeySwitchingParameters, BooleanParameters, EncryptionKeyChoice,
};
use crate::boolean::{ClientKey, CompressedPublicKey, PublicKey, PLAINTEXT_FALSE, PLAINTEXT_TRUE};
use crate::core_crypto::algorithms::*;
use crate::core_crypto::entities::*;
use std::cell::RefCell;
pub mod bootstrapping;
use crate::boolean::engine::bootstrapping::{Bootstrapper, CompressedServerKey, ServerKey};
use crate::core_crypto::commons::generators::{
DeterministicSeeder, EncryptionRandomGenerator, SecretRandomGenerator,
};
use crate::core_crypto::commons::math::random::{ActivatedRandomGenerator, Seeder};
use crate::core_crypto::commons::parameters::{PBSOrder, *};
use crate::core_crypto::seeders::new_seeder;
#[cfg(test)]
mod tests;
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;
}
pub(crate) trait BinaryGatesAssignEngine<L, R, K> {
fn and_assign(&mut self, ct_left: L, ct_right: R, server_key: &K);
fn nand_assign(&mut self, ct_left: L, ct_right: R, server_key: &K);
fn nor_assign(&mut self, ct_left: L, ct_right: R, server_key: &K);
fn or_assign(&mut self, ct_left: L, ct_right: R, server_key: &K);
fn xor_assign(&mut self, ct_left: L, ct_right: R, server_key: &K);
fn xnor_assign(&mut self, ct_left: L, ct_right: R, server_key: &K);
}
/// 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;
}
// All our thread local engines
// that our exposed types will use internally to implement their methods
thread_local! {
static BOOLEAN_ENGINE: RefCell<BooleanEngine> = RefCell::new(BooleanEngine::new());
}
pub struct BooleanEngine {
/// A structure containing a single CSPRNG to generate secret key coefficients.
secret_generator: SecretRandomGenerator<ActivatedRandomGenerator>,
/// A structure containing two CSPRNGs to generate material for encryption like public masks
/// and secret errors.
///
/// The [`EncryptionRandomGenerator`] contains two CSPRNGs, one publicly seeded used to
/// generate mask coefficients and one privately seeded used to generate errors during
/// encryption.
encryption_generator: EncryptionRandomGenerator<ActivatedRandomGenerator>,
bootstrapper: Bootstrapper,
}
impl WithThreadLocalEngine for BooleanEngine {
fn with_thread_local_mut<R, F>(func: F) -> R
where
F: FnOnce(&mut Self) -> R,
{
BOOLEAN_ENGINE.with(|engine_cell| func(&mut engine_cell.borrow_mut()))
}
}
// We have q = 2^32 so log2q = 32
const LOG2_Q_32: usize = 32;
impl BooleanEngine {
pub fn create_client_key(&mut self, parameters: BooleanParameters) -> ClientKey {
// generate the lwe secret key
let lwe_secret_key = allocate_and_generate_new_binary_lwe_secret_key(
parameters.lwe_dimension,
&mut self.secret_generator,
);
// generate the glwe secret key
let glwe_secret_key = allocate_and_generate_new_binary_glwe_secret_key(
parameters.glwe_dimension,
parameters.polynomial_size,
&mut self.secret_generator,
);
ClientKey {
lwe_secret_key,
glwe_secret_key,
parameters,
}
}
pub fn create_server_key(&mut self, cks: &ClientKey) -> ServerKey {
self.bootstrapper.new_server_key(cks).unwrap()
}
pub fn create_compressed_server_key(&mut self, cks: &ClientKey) -> CompressedServerKey {
self.bootstrapper.new_compressed_server_key(cks).unwrap()
}
pub fn create_public_key(&mut self, client_key: &ClientKey) -> PublicKey {
let client_parameters = client_key.parameters;
let (lwe_sk, encryption_noise) = match client_parameters.encryption_key_choice {
EncryptionKeyChoice::Big => (
client_key.glwe_secret_key.as_lwe_secret_key(),
client_key.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(client_key.lwe_secret_key.as_ref());
(view, client_key.parameters.lwe_modular_std_dev)
}
};
// Formula is (n + 1) * log2(q) + 128
let zero_encryption_count = LwePublicKeyZeroEncryptionCount(
lwe_sk.lwe_dimension().to_lwe_size().0 * LOG2_Q_32 + 128,
);
#[cfg(not(feature = "__wasm_api"))]
let lwe_public_key: LwePublicKeyOwned<u32> = par_allocate_and_generate_new_lwe_public_key(
&lwe_sk,
zero_encryption_count,
encryption_noise,
CiphertextModulus::new_native(),
&mut self.encryption_generator,
);
#[cfg(feature = "__wasm_api")]
let lwe_public_key: LwePublicKeyOwned<u32> = allocate_and_generate_new_lwe_public_key(
&lwe_sk,
zero_encryption_count,
encryption_noise,
CiphertextModulus::new_native(),
&mut self.encryption_generator,
);
PublicKey {
lwe_public_key,
parameters: client_key.parameters.to_owned(),
}
}
pub fn create_compressed_public_key(&mut self, client_key: &ClientKey) -> CompressedPublicKey {
let client_parameters = client_key.parameters;
let (lwe_sk, encryption_noise) = match client_parameters.encryption_key_choice {
EncryptionKeyChoice::Big => (
client_key.glwe_secret_key.as_lwe_secret_key(),
client_key.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(client_key.lwe_secret_key.as_ref());
(view, client_key.parameters.lwe_modular_std_dev)
}
};
// Formula is (n + 1) * log2(q) + 128
let zero_encryption_count = LwePublicKeyZeroEncryptionCount(
lwe_sk.lwe_dimension().to_lwe_size().0 * LOG2_Q_32 + 128,
);
#[cfg(not(feature = "__wasm_api"))]
let compressed_lwe_public_key = par_allocate_and_generate_new_seeded_lwe_public_key(
&lwe_sk,
zero_encryption_count,
encryption_noise,
CiphertextModulus::new_native(),
&mut self.bootstrapper.seeder,
);
#[cfg(feature = "__wasm_api")]
let compressed_lwe_public_key = allocate_and_generate_new_seeded_lwe_public_key(
&lwe_sk,
zero_encryption_count,
encryption_noise,
CiphertextModulus::new_native(),
&mut self.bootstrapper.seeder,
);
CompressedPublicKey {
compressed_lwe_public_key,
parameters: client_parameters,
}
}
pub(crate) fn new_key_switching_key(
&mut self,
cks1: &ClientKey,
cks2: &ClientKey,
params: BooleanKeySwitchingParameters,
) -> LweKeyswitchKeyOwned<u32> {
let (lwe_sk1, lwe_sk2) = match (
cks1.parameters.encryption_key_choice,
cks2.parameters.encryption_key_choice,
) {
(EncryptionKeyChoice::Big, EncryptionKeyChoice::Big) => (
cks1.glwe_secret_key.as_lwe_secret_key(),
cks2.glwe_secret_key.as_lwe_secret_key(),
),
(EncryptionKeyChoice::Small, EncryptionKeyChoice::Small) => {
let view1 = LweSecretKey::from_container(cks1.lwe_secret_key.as_ref());
let view2 = LweSecretKey::from_container(cks2.lwe_secret_key.as_ref());
(view1, view2)
}
(choice1, choice2) => panic!(
"EncryptionKeyChoice of cks1 and cks2 must be the same.\
cks1 has {:?}, cks2 has: {:?}
",
choice1, choice2
),
};
// Creation of the key switching key
allocate_and_generate_new_lwe_keyswitch_key(
&lwe_sk1,
&lwe_sk2,
params.ks_base_log,
params.ks_level,
cks2.parameters.lwe_modular_std_dev,
CiphertextModulus::new_native(),
&mut self.encryption_generator,
)
}
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: Plaintext<u32> = if message {
Plaintext(PLAINTEXT_TRUE)
} else {
Plaintext(PLAINTEXT_FALSE)
};
let (lwe_sk, encryption_noise) = match cks.parameters.encryption_key_choice {
EncryptionKeyChoice::Big => (
cks.glwe_secret_key.as_lwe_secret_key(),
cks.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(cks.lwe_secret_key.as_ref());
(view, cks.parameters.lwe_modular_std_dev)
}
};
// encryption
let ct = allocate_and_encrypt_new_lwe_ciphertext(
&lwe_sk,
plain,
encryption_noise,
CiphertextModulus::new_native(),
&mut self.encryption_generator,
);
Ciphertext::Encrypted(ct)
}
pub fn encrypt_compressed(&mut self, message: bool, cks: &ClientKey) -> CompressedCiphertext {
// encode the boolean message
let plain: Plaintext<u32> = if message {
Plaintext(PLAINTEXT_TRUE)
} else {
Plaintext(PLAINTEXT_FALSE)
};
let (lwe_sk, encryption_noise) = match cks.parameters.encryption_key_choice {
EncryptionKeyChoice::Big => (
cks.glwe_secret_key.as_lwe_secret_key(),
cks.parameters.glwe_modular_std_dev,
),
EncryptionKeyChoice::Small => {
let view = LweSecretKey::from_container(cks.lwe_secret_key.as_ref());
(view, cks.parameters.lwe_modular_std_dev)
}
};
// encryption
let ct = allocate_and_encrypt_new_seeded_lwe_ciphertext(
&lwe_sk,
plain,
encryption_noise,
CiphertextModulus::new_native(),
&mut self.bootstrapper.seeder,
);
CompressedCiphertext { ciphertext: ct }
}
pub fn encrypt_with_public_key(&mut self, message: bool, pks: &PublicKey) -> Ciphertext {
// encode the boolean message
let plain: Plaintext<u32> = if message {
Plaintext(PLAINTEXT_TRUE)
} else {
Plaintext(PLAINTEXT_FALSE)
};
let mut output = LweCiphertext::new(
0u32,
pks.lwe_public_key.lwe_size(),
CiphertextModulus::new_native(),
);
// encryption
encrypt_lwe_ciphertext_with_public_key(
&pks.lwe_public_key,
&mut output,
plain,
&mut self.secret_generator,
);
Ciphertext::Encrypted(output)
}
pub fn encrypt_with_compressed_public_key(
&mut self,
message: bool,
compressed_pk: &CompressedPublicKey,
) -> Ciphertext {
let plain: Plaintext<u32> = if message {
Plaintext(PLAINTEXT_TRUE)
} else {
Plaintext(PLAINTEXT_FALSE)
};
let mut output = LweCiphertext::new(
0u32,
compressed_pk.compressed_lwe_public_key.lwe_size(),
CiphertextModulus::new_native(),
);
encrypt_lwe_ciphertext_with_seeded_public_key(
&compressed_pk.compressed_lwe_public_key,
&mut output,
plain,
&mut self.secret_generator,
);
Ciphertext::Encrypted(output)
}
pub fn decrypt(&mut self, ct: &Ciphertext, cks: &ClientKey) -> bool {
match ct {
Ciphertext::Trivial(b) => *b,
Ciphertext::Encrypted(ciphertext) => {
let lwe_sk = match cks.parameters.encryption_key_choice {
EncryptionKeyChoice::Big => cks.glwe_secret_key.as_lwe_secret_key(),
EncryptionKeyChoice::Small => {
LweSecretKey::from_container(cks.lwe_secret_key.as_ref())
}
};
// decryption
let decrypted = decrypt_lwe_ciphertext(&lwe_sk, ciphertext);
// cast as a u32
let decrypted_u32 = decrypted.0;
// 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();
lwe_ciphertext_opposite_assign(&mut ct_res);
// Output the result:
Ciphertext::Encrypted(ct_res)
}
}
}
pub fn not_assign(&mut self, ct: &mut Ciphertext) {
match ct {
Ciphertext::Trivial(message) => *message = !*message,
Ciphertext::Encrypted(ct_ct) => {
lwe_ciphertext_opposite_assign(ct_ct); // compute the negation
}
}
}
}
impl Default for BooleanEngine {
fn default() -> Self {
Self::new()
}
}
impl BooleanEngine {
/// Replace the thread_local BooleanEngine
///
/// `new_engine` will replace the already_existing
/// `thread_local` engine.
///
/// # Example
///
/// ```rust
/// use tfhe::boolean::engine::BooleanEngine;
/// use tfhe::core_crypto::commons::generators::DeterministicSeeder;
/// use tfhe::core_crypto::commons::math::random::Seed;
/// use tfhe::core_crypto::prelude::ActivatedRandomGenerator;
///
/// // WARNING: Using a deterministic seed is not recommended
/// // as it renders the random generation insecure
///
/// let deterministic_seed = Seed(0);
///
/// let mut seeder = DeterministicSeeder::<ActivatedRandomGenerator>::new(deterministic_seed);
/// let boolean_engine = BooleanEngine::new_from_seeder(&mut seeder);
/// BooleanEngine::replace_thread_local(boolean_engine);
///
/// // This uses the engine create earlier
/// let (cks, sks) = tfhe::boolean::gen_keys();
/// ```
pub fn replace_thread_local(new_engine: Self) {
Self::with_thread_local_mut(|local_engine| {
let _ = std::mem::replace(local_engine, new_engine);
})
}
pub fn new() -> Self {
let mut root_seeder = new_seeder();
Self::new_from_seeder(root_seeder.as_mut())
}
pub fn new_from_seeder(root_seeder: &mut dyn Seeder) -> Self {
let mut deterministic_seeder =
DeterministicSeeder::<ActivatedRandomGenerator>::new(root_seeder.seed());
// Note that the operands are evaluated from left to right for Rust Struct expressions
// See: https://doc.rust-lang.org/stable/reference/expressions.html?highlight=left#evaluation-order-of-operands
Self {
secret_generator: SecretRandomGenerator::<_>::new(deterministic_seeder.seed()),
encryption_generator: EncryptionRandomGenerator::<_>::new(
deterministic_seeder.seed(),
&mut deterministic_seeder,
),
bootstrapper: Bootstrapper::new(&mut deterministic_seeder),
}
}
/// convert into an actual LWE ciphertext even when trivial
fn convert_into_lwe_ciphertext_32(
&mut self,
ct: &Ciphertext,
server_key: &ServerKey,
) -> LweCiphertextOwned<u32> {
match ct {
Ciphertext::Encrypted(ct_ct) => ct_ct.clone(),
Ciphertext::Trivial(message) => {
// encode the boolean message
let plain: Plaintext<u32> = if *message {
Plaintext(PLAINTEXT_TRUE)
} else {
Plaintext(PLAINTEXT_FALSE)
};
let lwe_size = match server_key.pbs_order {
PBSOrder::KeyswitchBootstrap => server_key
.key_switching_key
.input_key_lwe_dimension()
.to_lwe_size(),
PBSOrder::BootstrapKeyswitch => server_key
.bootstrapping_key
.input_lwe_dimension()
.to_lwe_size(),
};
allocate_and_trivially_encrypt_new_lwe_ciphertext(
lwe_size,
plain,
CiphertextModulus::new_native(),
)
}
}
}
pub fn mux(
&mut self,
ct_condition: &Ciphertext,
ct_then: &Ciphertext,
ct_else: &Ciphertext,
server_key: &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 LweCiphertextOwned<u32>
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 = LweCiphertext::new(
0u32,
ct_condition_ct.lwe_size(),
ct_condition_ct.ciphertext_modulus(),
);
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)
lwe_ciphertext_add(buffer_lwe_before_pbs, ct_condition_ct, &ct_then_ct);
let cst = Plaintext(PLAINTEXT_FALSE);
lwe_ciphertext_plaintext_add_assign(buffer_lwe_before_pbs, cst); // - 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
lwe_ciphertext_opposite_assign(&mut ct_temp_2); // compute the negation
lwe_ciphertext_add_assign(&mut ct_temp_2, &ct_else_ct); // + ct_else
let cst = Plaintext(PLAINTEXT_FALSE);
lwe_ciphertext_plaintext_add_assign(&mut ct_temp_2, cst); // - 1/8
match server_key.pbs_order {
PBSOrder::KeyswitchBootstrap => {
let ct_ks_1 = bootstrapper
.keyswitch(buffer_lwe_before_pbs, server_key)
.unwrap();
// Compute the first programmable bootstrapping with fixed test polynomial:
let mut ct_pbs_1 = bootstrapper.bootstrap(&ct_ks_1, server_key).unwrap();
let ct_ks_2 = bootstrapper.keyswitch(&ct_temp_2, server_key).unwrap();
let ct_pbs_2 = bootstrapper.bootstrap(&ct_ks_2, server_key).unwrap();
// Compute the linear combination to add the two results:
// buffer_lwe_pbs + ct_pbs_2 + (0,...,0, +1/8)
lwe_ciphertext_add_assign(&mut ct_pbs_1, &ct_pbs_2); // + buffer_lwe_pbs
let cst = Plaintext(PLAINTEXT_TRUE);
lwe_ciphertext_plaintext_add_assign(&mut ct_pbs_1, cst); // + 1/8
// Output the result:
Ciphertext::Encrypted(ct_pbs_1)
}
PBSOrder::BootstrapKeyswitch => {
// 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)
lwe_ciphertext_add_assign(&mut ct_pbs_1, &ct_pbs_2); // + buffer_lwe_pbs
let cst = Plaintext(PLAINTEXT_TRUE);
lwe_ciphertext_plaintext_add_assign(&mut ct_pbs_1, cst); // + 1/8
let ct_ks = bootstrapper.keyswitch(&ct_pbs_1, server_key).unwrap();
// Output the result:
Ciphertext::Encrypted(ct_ks)
}
}
}
}
}
}
impl BinaryGatesEngine<&Ciphertext, &Ciphertext, ServerKey> for BooleanEngine {
fn and(
&mut self,
ct_left: &Ciphertext,
ct_right: &Ciphertext,
server_key: &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 = LweCiphertext::new(
0u32,
ct_left_ct.lwe_size(),
ct_left_ct.ciphertext_modulus(),
);
let bootstrapper = &mut self.bootstrapper;
// compute the linear combination for AND: ct_left + ct_right + (0,...,0,-1/8)
// ct_left + ct_right
lwe_ciphertext_add(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct);
let cst = Plaintext(PLAINTEXT_FALSE);
// - 1/8
lwe_ciphertext_plaintext_add_assign(&mut buffer_lwe_before_pbs, cst);
// compute the bootstrap and the key switch
bootstrapper
.apply_bootstrapping_pattern(buffer_lwe_before_pbs, server_key)
.unwrap()
}
}
}
fn nand(
&mut self,
ct_left: &Ciphertext,
ct_right: &Ciphertext,
server_key: &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 = LweCiphertext::new(
0u32,
ct_left_ct.lwe_size(),
ct_left_ct.ciphertext_modulus(),
);
let bootstrapper = &mut self.bootstrapper;
// Compute the linear combination for NAND: - ct_left - ct_right + (0,...,0,1/8)
// ct_left + ct_right
lwe_ciphertext_add(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct);
lwe_ciphertext_opposite_assign(&mut buffer_lwe_before_pbs);
let cst = Plaintext(PLAINTEXT_TRUE);
// + 1/8
lwe_ciphertext_plaintext_add_assign(&mut buffer_lwe_before_pbs, cst);
// compute the bootstrap and the key switch
bootstrapper
.apply_bootstrapping_pattern(buffer_lwe_before_pbs, server_key)
.unwrap()
}
}
}
fn nor(
&mut self,
ct_left: &Ciphertext,
ct_right: &Ciphertext,
server_key: &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 = LweCiphertext::new(
0u32,
ct_left_ct.lwe_size(),
ct_left_ct.ciphertext_modulus(),
);
let bootstrapper = &mut self.bootstrapper;
// Compute the linear combination for NOR: - ct_left - ct_right + (0,...,0,-1/8)
// ct_left + ct_right
lwe_ciphertext_add(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct);
// compute the negation
lwe_ciphertext_opposite_assign(&mut buffer_lwe_before_pbs);
let cst = Plaintext(PLAINTEXT_FALSE);
// - 1/8
lwe_ciphertext_plaintext_add_assign(&mut buffer_lwe_before_pbs, cst);
// compute the bootstrap and the key switch
bootstrapper
.apply_bootstrapping_pattern(buffer_lwe_before_pbs, server_key)
.unwrap()
}
}
}
fn or(
&mut self,
ct_left: &Ciphertext,
ct_right: &Ciphertext,
server_key: &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 = LweCiphertext::new(
0u32,
ct_left_ct.lwe_size(),
ct_left_ct.ciphertext_modulus(),
);
let bootstrapper = &mut self.bootstrapper;
// Compute the linear combination for OR: ct_left + ct_right + (0,...,0,+1/8)
// ct_left + ct_right
lwe_ciphertext_add(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct);
let cst = Plaintext(PLAINTEXT_TRUE);
// + 1/8
lwe_ciphertext_plaintext_add_assign(&mut buffer_lwe_before_pbs, cst);
// compute the bootstrap and the key switch
bootstrapper
.apply_bootstrapping_pattern(buffer_lwe_before_pbs, server_key)
.unwrap()
}
}
}
fn xor(
&mut self,
ct_left: &Ciphertext,
ct_right: &Ciphertext,
server_key: &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 = LweCiphertext::new(
0u32,
ct_left_ct.lwe_size(),
ct_left_ct.ciphertext_modulus(),
);
let bootstrapper = &mut self.bootstrapper;
// Compute the linear combination for XOR: 2*(ct_left + ct_right) + (0,...,0,1/4)
// ct_left + ct_right
lwe_ciphertext_add(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct);
let cst_add = Plaintext(PLAINTEXT_TRUE);
// + 1/8
lwe_ciphertext_plaintext_add_assign(&mut buffer_lwe_before_pbs, cst_add);
let cst_mul = Cleartext(2u32);
//* 2
lwe_ciphertext_cleartext_mul_assign(&mut buffer_lwe_before_pbs, cst_mul);
// compute the bootstrap and the key switch
bootstrapper
.apply_bootstrapping_pattern(buffer_lwe_before_pbs, server_key)
.unwrap()
}
}
}
fn xnor(
&mut self,
ct_left: &Ciphertext,
ct_right: &Ciphertext,
server_key: &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 = LweCiphertext::new(
0u32,
ct_left_ct.lwe_size(),
ct_left_ct.ciphertext_modulus(),
);
let bootstrapper = &mut self.bootstrapper;
// Compute the linear combination for XNOR: 2*(-ct_left - ct_right + (0,...,0,-1/8))
// ct_left + ct_right
lwe_ciphertext_add(&mut buffer_lwe_before_pbs, ct_left_ct, ct_right_ct);
let cst_add = Plaintext(PLAINTEXT_TRUE);
// + 1/8
lwe_ciphertext_plaintext_add_assign(&mut buffer_lwe_before_pbs, cst_add);
// compute the negation
lwe_ciphertext_opposite_assign(&mut buffer_lwe_before_pbs);
let cst_mul = Cleartext(2u32);
//* 2
lwe_ciphertext_cleartext_mul_assign(&mut buffer_lwe_before_pbs, cst_mul);
// compute the bootstrap and the key switch
bootstrapper
.apply_bootstrapping_pattern(buffer_lwe_before_pbs, server_key)
.unwrap()
}
}
}
}
impl BinaryGatesAssignEngine<&mut Ciphertext, &Ciphertext, ServerKey> for BooleanEngine {
fn and_assign(
&mut self,
ct_left: &mut Ciphertext,
ct_right: &Ciphertext,
server_key: &ServerKey,
) {
let ct_left_clone = ct_left.clone();
*ct_left = self.and(&ct_left_clone, ct_right, server_key);
}
fn nand_assign(
&mut self,
ct_left: &mut Ciphertext,
ct_right: &Ciphertext,
server_key: &ServerKey,
) {
let ct_left_clone = ct_left.clone();
*ct_left = self.nand(&ct_left_clone, ct_right, server_key);
}
fn nor_assign(
&mut self,
ct_left: &mut Ciphertext,
ct_right: &Ciphertext,
server_key: &ServerKey,
) {
let ct_left_clone = ct_left.clone();
*ct_left = self.nor(&ct_left_clone, ct_right, server_key);
}
fn or_assign(
&mut self,
ct_left: &mut Ciphertext,
ct_right: &Ciphertext,
server_key: &ServerKey,
) {
let ct_left_clone = ct_left.clone();
*ct_left = self.or(&ct_left_clone, ct_right, server_key);
}
fn xor_assign(
&mut self,
ct_left: &mut Ciphertext,
ct_right: &Ciphertext,
server_key: &ServerKey,
) {
let ct_left_clone = ct_left.clone();
*ct_left = self.xor(&ct_left_clone, ct_right, server_key);
}
fn xnor_assign(
&mut self,
ct_left: &mut Ciphertext,
ct_right: &Ciphertext,
server_key: &ServerKey,
) {
let ct_left_clone = ct_left.clone();
*ct_left = self.xnor(&ct_left_clone, ct_right, server_key);
}
}
impl BinaryGatesAssignEngine<&mut Ciphertext, bool, ServerKey> for BooleanEngine {
fn and_assign(&mut self, ct_left: &mut Ciphertext, ct_right: bool, server_key: &ServerKey) {
let ct_left_clone = ct_left.clone();
*ct_left = self.and(&ct_left_clone, ct_right, server_key);
}
fn nand_assign(&mut self, ct_left: &mut Ciphertext, ct_right: bool, server_key: &ServerKey) {
let ct_left_clone = ct_left.clone();
*ct_left = self.nand(&ct_left_clone, ct_right, server_key);
}
fn nor_assign(&mut self, ct_left: &mut Ciphertext, ct_right: bool, server_key: &ServerKey) {
let ct_left_clone = ct_left.clone();
*ct_left = self.nor(&ct_left_clone, ct_right, server_key);
}
fn or_assign(&mut self, ct_left: &mut Ciphertext, ct_right: bool, server_key: &ServerKey) {
let ct_left_clone = ct_left.clone();
*ct_left = self.or(&ct_left_clone, ct_right, server_key);
}
fn xor_assign(&mut self, ct_left: &mut Ciphertext, ct_right: bool, server_key: &ServerKey) {
let ct_left_clone = ct_left.clone();
*ct_left = self.xor(&ct_left_clone, ct_right, server_key);
}
fn xnor_assign(&mut self, ct_left: &mut Ciphertext, ct_right: bool, server_key: &ServerKey) {
let ct_left_clone = ct_left.clone();
*ct_left = self.xnor(&ct_left_clone, ct_right, server_key);
}
}
impl BinaryGatesAssignEngine<bool, &mut Ciphertext, ServerKey> for BooleanEngine {
fn and_assign(&mut self, ct_left: bool, ct_right: &mut Ciphertext, server_key: &ServerKey) {
let ct_right_clone = ct_right.clone();
*ct_right = self.and(ct_left, &ct_right_clone, server_key);
}
fn nand_assign(&mut self, ct_left: bool, ct_right: &mut Ciphertext, server_key: &ServerKey) {
let ct_right_clone = ct_right.clone();
*ct_right = self.nand(ct_left, &ct_right_clone, server_key);
}
fn nor_assign(&mut self, ct_left: bool, ct_right: &mut Ciphertext, server_key: &ServerKey) {
let ct_right_clone = ct_right.clone();
*ct_right = self.nor(ct_left, &ct_right_clone, server_key);
}
fn or_assign(&mut self, ct_left: bool, ct_right: &mut Ciphertext, server_key: &ServerKey) {
let ct_right_clone = ct_right.clone();
*ct_right = self.or(ct_left, &ct_right_clone, server_key);
}
fn xor_assign(&mut self, ct_left: bool, ct_right: &mut Ciphertext, server_key: &ServerKey) {
let ct_right_clone = ct_right.clone();
*ct_right = self.xor(ct_left, &ct_right_clone, server_key);
}
fn xnor_assign(&mut self, ct_left: bool, ct_right: &mut Ciphertext, server_key: &ServerKey) {
let ct_right_clone = ct_right.clone();
*ct_right = self.xnor(ct_left, &ct_right_clone, server_key);
}
}
impl BinaryGatesEngine<&Ciphertext, bool, ServerKey> for BooleanEngine {
fn and(&mut self, ct_left: &Ciphertext, ct_right: bool, _server_key: &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,