tfhe 1.7.0

TFHE-rs is a fully homomorphic encryption (FHE) library that implements Zama's variant of TFHE.
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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
use crate::core_crypto::gpu::lwe_ciphertext_list::CudaLweCiphertextList;
use crate::core_crypto::gpu::CudaStreams;
use crate::core_crypto::prelude::LweCiphertextCount;
use crate::integer::block_decomposition::DecomposableInto;
use crate::integer::ciphertext::{
    AsShortintCiphertextSlice, DataKind, Expandable, IntegerRadixCiphertext,
};
use crate::integer::gpu::ciphertext::boolean_value::CudaBooleanBlock;
use crate::integer::gpu::ciphertext::compressed_ciphertext_list::{
    CudaCompressedCiphertextListBuilder, CudaExpandable,
};
use crate::integer::gpu::ciphertext::info::{CudaBlockInfo, CudaRadixCiphertextInfo};
use crate::integer::gpu::ciphertext::{
    CudaIntegerRadixCiphertext, CudaRadixCiphertext, CudaUnsignedRadixCiphertext,
};
use crate::integer::gpu::list_compression::server_keys::{
    CudaCompressionKey, CudaDecompressionKey,
};
use crate::integer::gpu::server_key::{CudaBootstrappingKey, CudaDynamicKeyswitchingKey};
use crate::integer::gpu::{
    cuda_backend_kv_store_contains_key, cuda_backend_kv_store_get, cuda_backend_kv_store_map,
    cuda_backend_kv_store_update, CudaServerKey,
};
use crate::integer::server_key::{CompressedKVStore, KVStore};
use crate::prelude::CastInto;
use crate::shortint::ciphertext::{Degree, NoiseLevel};
use crate::shortint::parameters::AtomicPatternKind;
use rayon::iter::IntoParallelRefIterator;
use rayon::prelude::ParallelIterator;
use std::collections::BTreeMap;
use std::fmt::Display;
use std::num::NonZeroUsize;
use tfhe_cuda_backend::cuda_bind::cuda_memcpy_async_gpu_to_gpu;

/// The KVStore is a specialized encrypted HashMap
///
/// * Keys are clear numbers
/// * Values are CudaUnsignedRadixCiphertext or CudaSignedRadixCiphertext
///
/// It supports getting/modifying existing pairs of (key,value)
/// using an encrypted key.
///
/// To serialize a KVStore, convert to CPU with `CudaKVStore::to_kv_store` then compress
pub struct CudaKVStore<Key, Ct> {
    data: BTreeMap<Key, Ct>,
    block_count: Option<NonZeroUsize>,
}

#[allow(dead_code)]
impl<Key, Ct> CudaKVStore<Key, Ct> {
    pub(crate) fn from_kv_store<CpuCt>(
        kv_store: &KVStore<Key, CpuCt>,
        streams: &CudaStreams,
    ) -> Self
    where
        Key: Clone + Ord,
        Ct: CudaIntegerRadixCiphertext,
        CpuCt: IntegerRadixCiphertext,
    {
        let mut gpu_kv_store = Self::new();
        kv_store.iter().for_each(|(key, value)| {
            let d_radix =
                CudaRadixCiphertext::from_cpu_blocks(value.as_ciphertext_slice(), streams);
            let d_value = Ct::from(d_radix);
            gpu_kv_store.insert(key.clone(), d_value);
        });
        gpu_kv_store
    }
}

impl<Key, Ct> CudaKVStore<Key, Ct> {
    /// Creates an empty KVStore
    pub fn new() -> Self {
        Self {
            data: BTreeMap::new(),
            block_count: None,
        }
    }

    /// Returns the value stored for the key if any
    ///
    /// Key is in clear, see [CudaServerKey::kv_store_get] if you wish to
    /// query using an encrypted key
    pub fn get(&self, key: &Key) -> Option<&Ct>
    where
        Key: Ord,
    {
        self.data.get(key)
    }

    /// Returns the value stored for the key if any
    ///
    /// Key is in clear, see [CudaServerKey::kv_store_get] if you wish to
    /// query using an encrypted key
    pub fn get_mut(&mut self, key: &Key) -> Option<&mut Ct>
    where
        Key: Ord,
    {
        self.data.get_mut(key)
    }

    /// Inserts the value for the key
    ///
    /// Returns the previous value stored for the key if there was any
    ///
    /// # Notes
    ///
    /// If the value does not contain blocks, nothing is inserted and None is returned
    ///
    /// # Panics
    ///
    /// Panics if the number of blocks of the value is not the same as all other
    /// values stored
    pub fn insert(&mut self, key: Key, value: Ct) -> Option<Ct>
    where
        Key: Ord,
        Ct: CudaIntegerRadixCiphertext,
    {
        let n_blocks = value.as_ref().d_blocks.lwe_ciphertext_count().0;
        if n_blocks == 0 {
            return None;
        }

        let n = self
            .block_count
            .get_or_insert_with(|| NonZeroUsize::new(n_blocks).unwrap());

        assert_eq!(
            n.get(),
            n_blocks,
            "All ciphertexts must have the same number of blocks"
        );
        self.data.insert(key, value)
    }

    /// Removes a key-value pair.
    pub fn remove(&mut self, key: &Key) -> Option<Ct>
    where
        Key: Ord,
    {
        self.data.remove(key)
    }

    /// Returns the value associated to the key given in clear
    pub fn clear_get(&self, key: &Key) -> Option<&Ct>
    where
        Key: Ord,
    {
        self.data.get(key)
    }

    /// Returns the number of key-value pairs currently stored
    pub fn len(&self) -> usize {
        self.data.len()
    }

    /// Returns whether the store is empty
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    pub fn contains_key(&self, key: &Key) -> bool
    where
        Key: Ord,
    {
        self.data.contains_key(key)
    }

    pub fn duplicate(&self, streams: &CudaStreams) -> Self
    where
        Key: Clone + Ord,
        Ct: CudaIntegerRadixCiphertext,
    {
        let data = self
            .data
            .iter()
            .map(|(k, v)| (k.clone(), v.duplicate(streams)))
            .collect();
        Self {
            data,
            block_count: self.block_count,
        }
    }

    pub fn iter(&self) -> impl Iterator<Item = (&Key, &Ct)>
    where
        Key: Ord,
        Ct: Send,
    {
        self.data.iter()
    }

    #[allow(dead_code)]
    fn par_iter_keys(&self) -> impl ParallelIterator<Item = &Key>
    where
        Key: Send + Sync + Ord,
        Ct: Send + Sync,
    {
        self.data.par_iter().map(|(k, _)| k)
    }

    pub(crate) fn blocks_per_radix(&self) -> Option<NonZeroUsize> {
        self.block_count
    }

    #[allow(dead_code)]
    pub(crate) fn to_kv_store<CpuCt>(
        &self,
        streams: &CudaStreams,
    ) -> crate::integer::server_key::KVStore<Key, CpuCt>
    where
        Key: Clone + Ord,
        Ct: CudaIntegerRadixCiphertext,
        CpuCt: IntegerRadixCiphertext,
    {
        let mut kv_store = crate::integer::server_key::KVStore::new();
        for (key, d_value) in &self.data {
            let cpu_blocks = d_value.as_ref().to_cpu_blocks(streams);
            kv_store.insert(key.clone(), CpuCt::from(cpu_blocks));
        }
        kv_store
    }

    pub fn compress<CpuCt>(
        &self,
        compression_key: &CudaCompressionKey,
        streams: &CudaStreams,
    ) -> CompressedKVStore<Key, CpuCt>
    where
        Key: Copy,
        Ct: CudaIntegerRadixCiphertext,
        CpuCt: Expandable + IntegerRadixCiphertext,
    {
        assert_eq!(
            Ct::IS_SIGNED,
            CpuCt::IS_SIGNED,
            "GPU and CPU ciphertext signedness must match"
        );

        let mut builder = CudaCompressedCiphertextListBuilder::new();
        let mut keys = Vec::with_capacity(self.data.len());
        for (key, value) in &self.data {
            let ct = value.as_ref().duplicate(streams);
            let num_blocks = ct.d_blocks.lwe_ciphertext_count().0;
            if let Some(n) = NonZeroUsize::new(num_blocks) {
                keys.push(*key);
                builder.ciphertexts.push(ct);
                let kind = if Ct::IS_SIGNED {
                    DataKind::Signed(n)
                } else {
                    DataKind::Unsigned(n)
                };
                builder.info.push(kind);
            }
        }
        let cuda_compressed = builder.build(compression_key, streams);
        let compressed_list = cuda_compressed.to_compressed_ciphertext_list(streams);
        CompressedKVStore::new(keys, compressed_list)
    }

    // Concatenates all values into one contiguous device array.
    fn to_vec(&self, streams: &CudaStreams) -> CudaRadixCiphertext
    where
        Key: Ord,
        Ct: CudaIntegerRadixCiphertext + Send,
    {
        let d_blocks_refs: Vec<&CudaLweCiphertextList<u64>> =
            self.iter().map(|(_, v)| &v.as_ref().d_blocks).collect();
        let concatenated_d_blocks = CudaLweCiphertextList::from_vec_cuda_lwe_ciphertexts_list(
            d_blocks_refs.iter().copied(),
            streams,
        );
        let concatenated_info = CudaRadixCiphertextInfo {
            blocks: self
                .iter()
                .flat_map(|(_, v)| v.as_ref().info.blocks.iter())
                .copied()
                .collect(),
        };

        CudaRadixCiphertext {
            d_blocks: concatenated_d_blocks,
            info: concatenated_info,
        }
    }

    /// Scatters blocks from a concatenated `CudaRadixCiphertext` back into each value
    /// in the BTreeMap. Entry i (in iteration order) receives blocks [i*N..(i+1)*N)
    /// from `concatenated`, where N is `blocks_per_radix`.
    fn update_from_concatenated(
        &mut self,
        concatenated: &CudaRadixCiphertext,
        streams: &CudaStreams,
    ) where
        Key: Ord,
        Ct: CudaIntegerRadixCiphertext,
    {
        let blocks_per_value = self
            .block_count
            .expect("Cannot scatter into an empty store")
            .get();
        let lwe_size = concatenated.d_blocks.0.lwe_dimension.to_lwe_size().0;
        let elements_per_value = blocks_per_value * lwe_size;

        for (idx, (_key, value)) in self.data.iter_mut().enumerate() {
            let src_offset = idx * elements_per_value;
            let byte_offset = src_offset * std::mem::size_of::<u64>();
            let copy_size = elements_per_value * std::mem::size_of::<u64>();

            // SAFETY: both pointers are valid GPU allocations on the same device.
            // Source and destination slices are non-overlapping and in-bounds.
            // All copies are on the same stream (no concurrent-access hazard).
            unsafe {
                cuda_memcpy_async_gpu_to_gpu(
                    value.as_mut().d_blocks.0.d_vec.as_mut_c_ptr(0),
                    concatenated
                        .d_blocks
                        .0
                        .d_vec
                        .as_c_ptr(0)
                        .wrapping_byte_add(byte_offset),
                    copy_size as u64,
                    streams.ptr[0],
                    streams.gpu_indexes[0].get(),
                );
            }

            let info_start = idx * blocks_per_value;
            let info_end = info_start + blocks_per_value;
            value
                .as_mut()
                .info
                .blocks
                .copy_from_slice(&concatenated.info.blocks[info_start..info_end]);
        }
        streams.synchronize();
    }
}

impl<Key, Ct> Default for CudaKVStore<Key, Ct> {
    fn default() -> Self {
        Self::new()
    }
}

impl<Key, Value> CompressedKVStore<Key, Value>
where
    Value: IntegerRadixCiphertext,
{
    /// Decompresses the stored values directly onto the GPU, mirroring the CPU-side
    /// [`CompressedKVStore::decompress`] but producing a [`CudaKVStore`]. This is the
    /// `CompressedKVStore -> CudaKVStore` direction; [`CudaKVStore::to_kv_store`] is the inverse.
    pub(crate) fn decompress_to_cuda<GpuCt>(
        &self,
        decompression_key: &CudaDecompressionKey,
        streams: &CudaStreams,
    ) -> crate::Result<CudaKVStore<Key, GpuCt>>
    where
        Key: Copy + Display + Ord,
        GpuCt: CudaIntegerRadixCiphertext + CudaExpandable,
    {
        let (keys, values, is_signed) = self.parts();

        if Value::IS_SIGNED != is_signed {
            let requested = if Value::IS_SIGNED {
                "signed"
            } else {
                "unsigned"
            };
            let stored = if is_signed { "signed" } else { "unsigned" };
            return Err(crate::error!(
                "Requested value signedness does not match stored data: \
                 requested {requested} values but stored values are {stored}"
            ));
        }

        let cuda_compressed = values.to_cuda_compressed_ciphertext_list(streams);
        let mut store = CudaKVStore::<Key, GpuCt>::new();

        for (i, key) in keys.iter().enumerate() {
            let value: GpuCt = cuda_compressed
                .get(i, decompression_key, streams)?
                .ok_or_else(|| crate::error!("Missing value for key '{key}'"))?;
            let _ = store.insert(*key, value);
        }

        Ok(store)
    }
}

impl CudaServerKey {
    // Returns selectors alongside the value so callers like `map` can reuse them.
    fn kv_store_get_impl<Key, Ct>(
        &self,
        kv_store: &CudaKVStore<Key, Ct>,
        encrypted_key: &Ct,
        streams: &CudaStreams,
    ) -> (Ct, CudaBooleanBlock, CudaLweCiphertextList<u64>)
    where
        Key: DecomposableInto<u64> + CastInto<usize> + Ord + Copy + Sync,
        Ct: CudaIntegerRadixCiphertext + Send,
    {
        let num_blocks_per_value = if let Some(n) = kv_store.blocks_per_radix() {
            n.get()
        } else {
            let num_blocks = encrypted_key.as_ref().d_blocks.lwe_ciphertext_count().0;
            let trivial_ct: Ct = self.create_trivial_zero_radix(num_blocks, streams);

            let trivial_bool_ct: Ct = self.create_trivial_zero_radix(1, streams);
            let trivial_bool = CudaBooleanBlock::from_cuda_radix_ciphertext(
                trivial_bool_ct.duplicate(streams).into_inner(),
            );

            let trivial_selectors = trivial_ct.as_ref().d_blocks.duplicate(streams);
            return (trivial_ct, trivial_bool, trivial_selectors);
        };

        let num_entries = kv_store.len();

        let concatenated_values = kv_store.to_vec(streams);
        let clear_keys: Vec<Key> = kv_store.iter().map(|(k, _)| *k).collect();

        let mut result_ct: Ct = self.create_trivial_zero_radix(num_blocks_per_value, streams);
        let mut result_bool = CudaBooleanBlock(
            self.create_trivial_zero_radix::<CudaUnsignedRadixCiphertext>(1, streams),
        );

        // Selectors are allocated here rather than in the backend because they are returned.
        let selector_block_info = CudaBlockInfo {
            degree: Degree::new(0),
            message_modulus: self.message_modulus,
            carry_modulus: self.carry_modulus,
            atomic_pattern: AtomicPatternKind::Standard(self.pbs_order),
            noise_level: NoiseLevel::ZERO,
        };
        let selectors_info = CudaRadixCiphertextInfo {
            blocks: vec![selector_block_info; num_entries],
        };
        let selectors_d_blocks = CudaLweCiphertextList::new(
            self.bootstrapping_key.output_lwe_dimension(),
            LweCiphertextCount(num_entries),
            encrypted_key.as_ref().d_blocks.ciphertext_modulus(),
            streams,
        );
        let mut selectors_ct = CudaRadixCiphertext {
            d_blocks: selectors_d_blocks,
            info: selectors_info,
        };

        let CudaDynamicKeyswitchingKey::Standard(computing_ks_key) = &self.key_switching_key else {
            panic!("Only the standard atomic pattern is supported on GPU")
        };

        let num_blocks_per_value_u32 =
            u32::try_from(num_blocks_per_value).expect("num_blocks_per_value exceeds u32::MAX");

        // SAFETY: all output buffers are freshly allocated on the device bound
        // to `streams` with exclusive write access. All input buffers are
        // read-only and live on the same device.
        unsafe {
            match &self.bootstrapping_key {
                CudaBootstrappingKey::Classic(d_bsk) => {
                    cuda_backend_kv_store_get(
                        streams,
                        &mut result_ct,
                        &mut result_bool,
                        &mut selectors_ct,
                        encrypted_key.as_ref(),
                        &concatenated_values,
                        &clear_keys,
                        num_blocks_per_value_u32,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_bsk,
                        computing_ks_key.params_ffi(),
                        d_bsk.ms_noise_reduction_configuration.as_ref(),
                    );
                }
                CudaBootstrappingKey::MultiBit(d_multibit_bsk) => {
                    cuda_backend_kv_store_get(
                        streams,
                        &mut result_ct,
                        &mut result_bool,
                        &mut selectors_ct,
                        encrypted_key.as_ref(),
                        &concatenated_values,
                        &clear_keys,
                        num_blocks_per_value_u32,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_multibit_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_multibit_bsk,
                        computing_ks_key.params_ffi(),
                        None,
                    );
                }
            }
        }

        (result_ct, result_bool, selectors_ct.d_blocks)
    }

    pub fn kv_store_contains_key<Key, Ct>(
        &self,
        map: &CudaKVStore<Key, Ct>,
        encrypted_key: &Ct,
        streams: &CudaStreams,
    ) -> CudaBooleanBlock
    where
        Ct: CudaIntegerRadixCiphertext + Send,
        Key: DecomposableInto<u64> + CastInto<usize> + Ord + Copy + Sync,
    {
        if map.is_empty() {
            return CudaBooleanBlock::from_cuda_radix_ciphertext(
                self.create_trivial_zero_radix::<CudaUnsignedRadixCiphertext>(1, streams)
                    .into_inner(),
            );
        }

        let clear_keys: Vec<Key> = map.iter().map(|(k, _)| *k).collect();

        let mut result_bool = CudaBooleanBlock(
            self.create_trivial_zero_radix::<CudaUnsignedRadixCiphertext>(1, streams),
        );

        let CudaDynamicKeyswitchingKey::Standard(computing_ks_key) = &self.key_switching_key else {
            panic!("Only the standard atomic pattern is supported on GPU")
        };

        // SAFETY: all buffers are valid allocations on the same device.
        // result_bool has exclusive write access; all others are read-only.
        unsafe {
            match &self.bootstrapping_key {
                CudaBootstrappingKey::Classic(d_bsk) => {
                    cuda_backend_kv_store_contains_key(
                        streams,
                        &mut result_bool,
                        encrypted_key.as_ref(),
                        &clear_keys,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_bsk,
                        computing_ks_key.params_ffi(),
                        d_bsk.ms_noise_reduction_configuration.as_ref(),
                    );
                }
                CudaBootstrappingKey::MultiBit(d_multibit_bsk) => {
                    cuda_backend_kv_store_contains_key(
                        streams,
                        &mut result_bool,
                        encrypted_key.as_ref(),
                        &clear_keys,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_multibit_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_multibit_bsk,
                        computing_ks_key.params_ffi(),
                        None,
                    );
                }
            }
        }

        result_bool
    }

    pub fn kv_store_contains_value<Key, Ct>(
        &self,
        map: &CudaKVStore<Key, Ct>,
        encrypted_value: &Ct,
        streams: &CudaStreams,
    ) -> CudaBooleanBlock
    where
        Ct: CudaIntegerRadixCiphertext + Send,
        Key: Ord + Sync,
    {
        let values: Vec<_> = map.iter().map(|(_, v)| v.duplicate(streams)).collect();
        self.contains(&values, encrypted_value, streams)
    }

    pub fn kv_store_contains_clear_value<Key, Ct, Clear>(
        &self,
        map: &CudaKVStore<Key, Ct>,
        clear_value: Clear,
        streams: &CudaStreams,
    ) -> CudaBooleanBlock
    where
        Ct: CudaIntegerRadixCiphertext + Send,
        Key: Ord + Sync,
        Clear: DecomposableInto<u64>,
    {
        let values: Vec<_> = map.iter().map(|(_, v)| v.duplicate(streams)).collect();
        self.contains_clear(&values, clear_value, streams)
    }

    pub fn kv_store_get<Key, Ct>(
        &self,
        map: &CudaKVStore<Key, Ct>,
        encrypted_key: &Ct,
        streams: &CudaStreams,
    ) -> (Ct, CudaBooleanBlock)
    where
        Ct: CudaIntegerRadixCiphertext + Send,
        Key: DecomposableInto<u64> + CastInto<usize> + Ord + Copy + Sync,
    {
        let (result, check_block, _selectors) = self.kv_store_get_impl(map, encrypted_key, streams);
        (result, check_block)
    }

    /// Updates the value at the given key by the given value
    ///
    /// `map[encrypted_key] = new_value`
    ///
    /// This finds the value that corresponds to the given `encrypted_key`,
    /// then updates the value stored with the `new_value`.
    ///
    /// Returns a boolean block that encrypts `true` if an entry for
    /// the `encrypted_key` was found, and thus the update was done
    pub fn kv_store_update<Key, Ct>(
        &self,
        map: &mut CudaKVStore<Key, Ct>,
        encrypted_key: &Ct,
        new_value: &Ct,
        streams: &CudaStreams,
    ) -> CudaBooleanBlock
    where
        Ct: CudaIntegerRadixCiphertext + Send,
        Key: DecomposableInto<u64> + CastInto<usize> + Ord + Copy + Sync,
    {
        let num_blocks_per_value = match map.blocks_per_radix() {
            Some(n) => n.get(),
            None => {
                return CudaBooleanBlock::from_cuda_radix_ciphertext(
                    self.create_trivial_zero_radix::<CudaUnsignedRadixCiphertext>(1, streams)
                        .into_inner(),
                );
            }
        };

        let concatenated_old_values = map.to_vec(streams);
        let clear_keys: Vec<Key> = map.iter().map(|(k, _)| *k).collect();

        let mut d_check_block: CudaUnsignedRadixCiphertext =
            self.create_trivial_zero_radix(1, streams);

        let total_blocks = map.len() * num_blocks_per_value;
        let mut d_updated_values: CudaUnsignedRadixCiphertext =
            self.create_trivial_zero_radix(total_blocks, streams);

        let CudaDynamicKeyswitchingKey::Standard(computing_ks_key) = &self.key_switching_key else {
            panic!("Only the standard atomic pattern is supported on GPU")
        };

        let num_blocks_per_value_u32 =
            u32::try_from(num_blocks_per_value).expect("num_blocks_per_value exceeds u32::MAX");

        // SAFETY: all output buffers are freshly allocated on the device bound
        // to `streams` with exclusive write access. All input buffers are
        // read-only and live on the same device.
        unsafe {
            match &self.bootstrapping_key {
                CudaBootstrappingKey::Classic(d_bsk) => {
                    cuda_backend_kv_store_update(
                        streams,
                        &mut d_check_block.ciphertext,
                        &mut d_updated_values.ciphertext,
                        encrypted_key.as_ref(),
                        &concatenated_old_values,
                        new_value.as_ref(),
                        &clear_keys,
                        num_blocks_per_value_u32,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_bsk,
                        computing_ks_key.params_ffi(),
                        d_bsk.ms_noise_reduction_configuration.as_ref(),
                    );
                }
                CudaBootstrappingKey::MultiBit(d_multibit_bsk) => {
                    cuda_backend_kv_store_update(
                        streams,
                        &mut d_check_block.ciphertext,
                        &mut d_updated_values.ciphertext,
                        encrypted_key.as_ref(),
                        &concatenated_old_values,
                        new_value.as_ref(),
                        &clear_keys,
                        num_blocks_per_value_u32,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_multibit_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_multibit_bsk,
                        computing_ks_key.params_ffi(),
                        None,
                    );
                }
            }
        }

        map.update_from_concatenated(&d_updated_values.ciphertext, streams);

        CudaBooleanBlock(d_check_block)
    }

    /// Updates the value at the given key by applying a function
    ///
    /// `map[encrypted_key] = func(map[encrypted_value])`
    ///
    /// This finds the value that corresponds to the given `encrypted_key`, then
    /// calls `func` then updates the value stored with the one returned by the `func`.
    ///
    /// Returns the (old_value, new_value, check_block) where `check_block` encrypts `true` if an
    /// entry for the `encrypted_key` was found.
    pub fn kv_store_map<Key, Ct, F>(
        &self,
        map: &mut CudaKVStore<Key, Ct>,
        encrypted_key: &Ct,
        func: F,
        streams: &CudaStreams,
    ) -> (Ct, Ct, CudaBooleanBlock)
    where
        Ct: CudaIntegerRadixCiphertext + Send,
        Key: DecomposableInto<u64> + CastInto<usize> + Ord + Copy + Sync,
        F: Fn(Ct) -> Ct,
    {
        let (old_value, _, selectors) = self.kv_store_get_impl(map, encrypted_key, streams);
        let old_value_copy = old_value.duplicate(streams);
        let new_value = func(old_value);

        let num_entries = map.len();
        let num_blocks_per_value = if let Some(n) = map.blocks_per_radix() {
            n.get()
        } else {
            let trivial_bool = CudaBooleanBlock::from_cuda_radix_ciphertext(
                self.create_trivial_zero_radix::<CudaUnsignedRadixCiphertext>(1, streams)
                    .into_inner(),
            );
            return (old_value_copy, new_value, trivial_bool);
        };

        let concatenated_old_values = map.to_vec(streams);

        // The FFI expects a CudaRadixCiphertext, not a raw CudaLweCiphertextList.
        let selector_block_info = CudaBlockInfo {
            degree: Degree::new(1),
            message_modulus: self.message_modulus,
            carry_modulus: self.carry_modulus,
            atomic_pattern: AtomicPatternKind::Standard(self.pbs_order),
            noise_level: NoiseLevel::NOMINAL,
        };
        let selectors_info = CudaRadixCiphertextInfo {
            blocks: vec![selector_block_info; num_entries],
        };
        let selectors_ct = CudaRadixCiphertext {
            d_blocks: selectors,
            info: selectors_info,
        };

        let CudaDynamicKeyswitchingKey::Standard(computing_ks_key) = &self.key_switching_key else {
            panic!("Only the standard atomic pattern is supported on GPU")
        };

        let num_blocks_per_value_u32 =
            u32::try_from(num_blocks_per_value).expect("num_blocks_per_value exceeds u32::MAX");

        let mut d_check_block: CudaUnsignedRadixCiphertext =
            self.create_trivial_zero_radix(1, streams);

        let total_blocks = map.len() * num_blocks_per_value;
        let mut d_updated_values: CudaUnsignedRadixCiphertext =
            self.create_trivial_zero_radix(total_blocks, streams);

        // SAFETY: all output buffers are freshly allocated on the device bound
        // to `streams` with exclusive write access. All input buffers are
        // read-only and live on the same device.
        unsafe {
            match &self.bootstrapping_key {
                CudaBootstrappingKey::Classic(d_bsk) => {
                    cuda_backend_kv_store_map(
                        streams,
                        &mut d_check_block.ciphertext,
                        &mut d_updated_values.ciphertext,
                        &concatenated_old_values,
                        new_value.as_ref(),
                        &selectors_ct,
                        num_blocks_per_value_u32,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_bsk,
                        computing_ks_key.params_ffi(),
                        d_bsk.ms_noise_reduction_configuration.as_ref(),
                    );
                }
                CudaBootstrappingKey::MultiBit(d_multibit_bsk) => {
                    cuda_backend_kv_store_map(
                        streams,
                        &mut d_check_block.ciphertext,
                        &mut d_updated_values.ciphertext,
                        &concatenated_old_values,
                        new_value.as_ref(),
                        &selectors_ct,
                        num_blocks_per_value_u32,
                        self.message_modulus,
                        self.carry_modulus,
                        &d_multibit_bsk.d_vec,
                        &computing_ks_key.d_vec,
                        d_multibit_bsk,
                        computing_ks_key.params_ffi(),
                        None,
                    );
                }
            }
        }

        map.update_from_concatenated(&d_updated_values.ciphertext, streams);

        (old_value_copy, new_value, CudaBooleanBlock(d_check_block))
    }
}

#[cfg(test)]
mod tests {
    use rand::Rng;

    use super::*;
    use crate::integer::gpu::ciphertext::{CudaSignedRadixCiphertext, CudaUnsignedRadixCiphertext};
    use crate::integer::{
        gen_keys, ClientKey, IntegerKeyKind, RadixCiphertext, SignedRadixCiphertext,
    };
    use crate::shortint::parameters::test_params::{
        TEST_COMP_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
        TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
    };
    use crate::shortint::ShortintParameterSet;
    use std::collections::BTreeMap;

    use crate::core_crypto::gpu::CudaStreams;
    use crate::integer::server_key::CompressedKVStore;

    fn assert_store_unsigned_matches(
        clear_store: &BTreeMap<u32, u64>,
        kv_store: &CudaKVStore<u32, CudaUnsignedRadixCiphertext>,
        cks: &ClientKey,
    ) {
        assert_eq!(
            clear_store.len(),
            kv_store.len(),
            "Clear and Encrypted stores do no have the same number of pairs"
        );

        let streams = CudaStreams::new_multi_gpu();

        for (key, value) in clear_store {
            let d_ct = kv_store
                .get(key)
                .expect("Missing entry in decompressed KVStore");
            let ct = d_ct.to_radix_ciphertext(&streams);

            let decrypted: u64 = cks.decrypt_radix(&ct);

            assert_eq!(
                *value, decrypted,
                "Invalid value stored for key '{key}', expected '{value}' got '{decrypted}'"
            );
        }
    }

    #[test]
    fn test_compression_serialization_unsigned() {
        let params =
            TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into();

        let (cks, _) = gen_keys::<ShortintParameterSet>(params, IntegerKeyKind::Radix);

        let private_compression_key = cks.new_compression_private_key(
            TEST_COMP_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
        );

        let (compression_key, decompression_key) =
            cks.new_compression_decompression_keys(&private_compression_key);

        let num_blocks = 32;
        let num_keys = 100;
        let streams = CudaStreams::new_multi_gpu();

        let mut rng = rand::thread_rng();

        let mut clear_store = BTreeMap::new();
        let mut gpu_kv_store = CudaKVStore::new();
        for _ in 0..num_keys {
            let key = rng.gen::<u32>();
            let value = rng.gen::<u64>();

            let ct = cks.encrypt_radix(value, num_blocks);
            let d_ct = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ct, &streams);

            let _ = clear_store.insert(key, value);
            gpu_kv_store.insert(key, d_ct);
        }

        assert_store_unsigned_matches(&clear_store, &gpu_kv_store, &cks);

        // Validates the flow GPU -> CPU -> Compress -> Decompress -> CPU -> GPU
        let kv_store: KVStore<u32, RadixCiphertext> = gpu_kv_store.to_kv_store(&streams);
        let compressed = kv_store.compress(&compression_key);
        let kv_store = compressed.decompress(&decompression_key).unwrap();
        let gpu_kv_store = CudaKVStore::from_kv_store(&kv_store, &streams);

        assert_store_unsigned_matches(&clear_store, &gpu_kv_store, &cks);

        // Validates the flow GPU -> CPU -> Serialize -> Deserialize -> CPU -> GPU
        let mut data = vec![];
        crate::safe_serialization::safe_serialize(&compressed, &mut data, 1 << 20).unwrap();
        let compressed: CompressedKVStore<u32, RadixCiphertext> =
            crate::safe_serialization::safe_deserialize(data.as_slice(), 1 << 20).unwrap();
        let kv_store = compressed.decompress(&decompression_key).unwrap();
        let gpu_kv_store = CudaKVStore::from_kv_store(&kv_store, &streams);
        assert_store_unsigned_matches(&clear_store, &gpu_kv_store, &cks);
    }

    fn assert_store_signed_matches(
        clear_store: &BTreeMap<u32, i64>,
        kv_store: &CudaKVStore<u32, CudaSignedRadixCiphertext>,
        cks: &ClientKey,
    ) {
        assert_eq!(
            clear_store.len(),
            kv_store.len(),
            "Clear and Encrypted stores do no have the same number of pairs"
        );

        let streams = CudaStreams::new_multi_gpu();

        for (key, value) in clear_store {
            let d_ct = kv_store
                .get(key)
                .expect("Missing entry in decompressed KVStore");
            let ct = d_ct.to_signed_radix_ciphertext(&streams);

            let decrypted: i64 = cks.decrypt_signed_radix(&ct);

            assert_eq!(
                *value, decrypted,
                "Invalid value stored for key '{key}', expected '{value}' got '{decrypted}'"
            );
        }
    }

    #[test]
    fn test_compression_serialization_signed() {
        let params =
            TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into();

        let (cks, _) = gen_keys::<ShortintParameterSet>(params, IntegerKeyKind::Radix);

        let private_compression_key = cks.new_compression_private_key(
            TEST_COMP_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
        );

        let (compression_key, decompression_key) =
            cks.new_compression_decompression_keys(&private_compression_key);

        let num_blocks = 32;
        let num_keys = 100;
        let streams = CudaStreams::new_multi_gpu();

        let mut rng = rand::thread_rng();

        let mut clear_store = BTreeMap::new();
        let mut gpu_kv_store = CudaKVStore::new();
        for _ in 0..num_keys {
            let key = rng.gen::<u32>();
            let value = rng.gen::<i64>();

            let ct = cks.encrypt_signed_radix(value, num_blocks);
            let d_ct = CudaSignedRadixCiphertext::from_signed_radix_ciphertext(&ct, &streams);

            let _ = clear_store.insert(key, value);
            gpu_kv_store.insert(key, d_ct);
        }

        assert_store_signed_matches(&clear_store, &gpu_kv_store, &cks);

        // Validates the flow GPU -> CPU -> Compress -> Decompress -> CPU -> GPU
        let kv_store: KVStore<u32, SignedRadixCiphertext> = gpu_kv_store.to_kv_store(&streams);
        let compressed = kv_store.compress(&compression_key);
        let kv_store = compressed.decompress(&decompression_key).unwrap();
        let gpu_kv_store = CudaKVStore::from_kv_store(&kv_store, &streams);

        assert_store_signed_matches(&clear_store, &gpu_kv_store, &cks);

        // Validates the flow GPU -> CPU -> Serialize -> Deserialize -> CPU -> GPU
        let mut data = vec![];
        crate::safe_serialization::safe_serialize(&compressed, &mut data, 1 << 20).unwrap();
        let compressed: CompressedKVStore<u32, SignedRadixCiphertext> =
            crate::safe_serialization::safe_deserialize(data.as_slice(), 1 << 20).unwrap();
        let kv_store = compressed.decompress(&decompression_key).unwrap();
        let gpu_kv_store = CudaKVStore::from_kv_store(&kv_store, &streams);
        assert_store_signed_matches(&clear_store, &gpu_kv_store, &cks);
    }

    #[test]
    fn test_kv_store_get() {
        let params =
            TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into();
        let (cks, _) = gen_keys::<ShortintParameterSet>(params, IntegerKeyKind::Radix);

        let streams = CudaStreams::new_multi_gpu();
        let sks = CudaServerKey::new(&cks, &streams);
        streams.synchronize();

        let num_value_blocks = 4;
        let num_key_blocks = 4; // u8 key with message_modulus=4 => 8/2 = 4 blocks
        let modulus = 1u64 << (2 * num_value_blocks); // 2 bits per block

        let clear_entries: Vec<(u8, u64)> =
            vec![(1, 10), (2, 42), (3, 100), (5, 200), (7, modulus - 1)];

        let mut gpu_kv_store: CudaKVStore<u8, CudaUnsignedRadixCiphertext> = CudaKVStore::new();
        for &(key, value) in &clear_entries {
            let ct = cks.encrypt_radix(value, num_value_blocks);
            let d_ct = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ct, &streams);
            gpu_kv_store.insert(key, d_ct);
        }

        // Verify each stored entry is really there
        for &(key, expected_value) in &clear_entries {
            let encrypted_key = cks.encrypt_radix(key as u64, num_key_blocks);
            let d_encrypted_key =
                CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_key, &streams);

            let (result, found_bool) = sks.kv_store_get(&gpu_kv_store, &d_encrypted_key, &streams);

            let cpu_result = result.to_radix_ciphertext(&streams);
            let decrypted: u64 = cks.decrypt_radix(&cpu_result);
            let found = cks.decrypt_bool(&found_bool.to_boolean_block(&streams));

            assert!(found, "Key {key} should be found in the store");
            assert_eq!(
                decrypted, expected_value,
                "Key {key}: expected {expected_value}, got {decrypted}"
            );
        }

        // Verify non-stored entries are really *NOT* there
        let missing_key = 4u8;
        let encrypted_key = cks.encrypt_radix(missing_key as u64, num_key_blocks);
        let d_encrypted_key =
            CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_key, &streams);

        let (result, found_bool) = sks.kv_store_get(&gpu_kv_store, &d_encrypted_key, &streams);

        let cpu_result = result.to_radix_ciphertext(&streams);
        let decrypted: u64 = cks.decrypt_radix(&cpu_result);
        let found = cks.decrypt_bool(&found_bool.to_boolean_block(&streams));

        assert!(!found, "Key {missing_key} should not be found in the store");
        assert_eq!(decrypted, 0, "Missing key should return 0");

        // Checks what happens with an empty store
        let empty_store: CudaKVStore<u8, CudaUnsignedRadixCiphertext> = CudaKVStore::new();
        let encrypted_key = cks.encrypt_radix(1u64, num_key_blocks);
        let d_encrypted_key =
            CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_key, &streams);

        let (result, found_bool, _selectors) =
            sks.kv_store_get_impl(&empty_store, &d_encrypted_key, &streams);

        let cpu_result = result.to_radix_ciphertext(&streams);
        let decrypted: u64 = cks.decrypt_radix(&cpu_result);
        let found = cks.decrypt_bool(&found_bool.to_boolean_block(&streams));

        assert!(!found, "Empty store should not find any key");
        assert_eq!(decrypted, 0, "Empty store should return 0");
    }

    #[test]
    fn test_kv_store_contains_key() {
        let params =
            TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into();
        let (cks, _) = gen_keys::<ShortintParameterSet>(params, IntegerKeyKind::Radix);

        let streams = CudaStreams::new_multi_gpu();
        let sks = CudaServerKey::new(&cks, &streams);
        streams.synchronize();

        let num_value_blocks = 4;
        let num_key_blocks = 4;

        let clear_entries: Vec<(u8, u64)> = vec![(1, 10), (2, 42), (3, 100), (5, 200)];

        let mut gpu_kv_store: CudaKVStore<u8, CudaUnsignedRadixCiphertext> = CudaKVStore::new();
        for &(key, value) in &clear_entries {
            let ct = cks.encrypt_radix(value, num_value_blocks);
            let d_ct = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ct, &streams);
            gpu_kv_store.insert(key, d_ct);
        }

        // Keys that are in the store must return true
        for &(key, _) in &clear_entries {
            let encrypted_key = cks.encrypt_radix(key as u64, num_key_blocks);
            let d_encrypted_key =
                CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_key, &streams);

            let result_bool = sks.kv_store_contains_key(&gpu_kv_store, &d_encrypted_key, &streams);
            let found = cks.decrypt_bool(&result_bool.to_boolean_block(&streams));
            assert!(found, "Key {key} should be found in the store");
        }

        // A key that is not in the store must return false
        let missing_key = 4u8;
        let encrypted_key = cks.encrypt_radix(missing_key as u64, num_key_blocks);
        let d_encrypted_key =
            CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_key, &streams);

        let result_bool = sks.kv_store_contains_key(&gpu_kv_store, &d_encrypted_key, &streams);
        let found = cks.decrypt_bool(&result_bool.to_boolean_block(&streams));
        assert!(!found, "Key {missing_key} should not be found in the store");

        // An empty store must always return false
        let empty_store: CudaKVStore<u8, CudaUnsignedRadixCiphertext> = CudaKVStore::new();
        let encrypted_key = cks.encrypt_radix(1u64, num_key_blocks);
        let d_encrypted_key =
            CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_key, &streams);

        let result_bool = sks.kv_store_contains_key(&empty_store, &d_encrypted_key, &streams);
        let found = cks.decrypt_bool(&result_bool.to_boolean_block(&streams));
        assert!(!found, "Empty store should not find any key");
    }

    #[test]
    fn test_kv_store_contains_value() {
        let params =
            TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into();
        let (cks, _) = gen_keys::<ShortintParameterSet>(params, IntegerKeyKind::Radix);

        let streams = CudaStreams::new_multi_gpu();
        let sks = CudaServerKey::new(&cks, &streams);
        streams.synchronize();

        let num_value_blocks = 4;

        let clear_entries: Vec<(u8, u64)> = vec![(1, 10), (2, 42), (3, 100), (5, 200)];

        let mut gpu_kv_store: CudaKVStore<u8, CudaUnsignedRadixCiphertext> = CudaKVStore::new();
        for &(key, value) in &clear_entries {
            let ct = cks.encrypt_radix(value, num_value_blocks);
            let d_ct = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ct, &streams);
            gpu_kv_store.insert(key, d_ct);
        }

        // Values that are in the store must return true
        for &(_, value) in &clear_entries {
            let encrypted_value = cks.encrypt_radix(value, num_value_blocks);
            let d_encrypted_value =
                CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_value, &streams);

            let result_bool =
                sks.kv_store_contains_value(&gpu_kv_store, &d_encrypted_value, &streams);
            let found = cks.decrypt_bool(&result_bool.to_boolean_block(&streams));
            assert!(found, "Value {value} should be found in the store");
        }

        // A value that is not in the store must return false
        let missing_value = 99u64;
        let encrypted_value = cks.encrypt_radix(missing_value, num_value_blocks);
        let d_encrypted_value =
            CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_value, &streams);

        let result_bool = sks.kv_store_contains_value(&gpu_kv_store, &d_encrypted_value, &streams);
        let found = cks.decrypt_bool(&result_bool.to_boolean_block(&streams));
        assert!(
            !found,
            "Value {missing_value} should not be found in the store"
        );
    }

    #[test]
    fn test_kv_store_contains_clear_value() {
        let params =
            TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into();
        let (cks, _) = gen_keys::<ShortintParameterSet>(params, IntegerKeyKind::Radix);

        let streams = CudaStreams::new_multi_gpu();
        let sks = CudaServerKey::new(&cks, &streams);
        streams.synchronize();

        let num_value_blocks = 4;

        let clear_entries: Vec<(u8, u64)> = vec![(1, 10), (2, 42), (3, 100), (5, 200)];

        let mut gpu_kv_store: CudaKVStore<u8, CudaUnsignedRadixCiphertext> = CudaKVStore::new();
        for &(key, value) in &clear_entries {
            let ct = cks.encrypt_radix(value, num_value_blocks);
            let d_ct = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ct, &streams);
            gpu_kv_store.insert(key, d_ct);
        }

        // Clear values that are in the store must return true
        for &(_, value) in &clear_entries {
            let result_bool = sks.kv_store_contains_clear_value(&gpu_kv_store, value, &streams);
            let found = cks.decrypt_bool(&result_bool.to_boolean_block(&streams));
            assert!(found, "Clear value {value} should be found in the store");
        }

        // A clear value that is not in the store must return false
        let missing_value = 99u64;
        let result_bool = sks.kv_store_contains_clear_value(&gpu_kv_store, missing_value, &streams);
        let found = cks.decrypt_bool(&result_bool.to_boolean_block(&streams));
        assert!(
            !found,
            "Clear value {missing_value} should not be found in the store"
        );
    }

    #[test]
    fn test_kv_store_map() {
        // kv_store_map cannot use the generic test templates, hence this dedicated test.
        let params =
            TEST_PARAM_GPU_MULTI_BIT_GROUP_4_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128.into();
        let (cks, _) = gen_keys::<ShortintParameterSet>(params, IntegerKeyKind::Radix);

        let streams = CudaStreams::new_multi_gpu();
        let sks = CudaServerKey::new(&cks, &streams);
        streams.synchronize();

        let num_value_blocks = 4;
        let num_key_blocks = 4; // u8 key with message_modulus=4 => 8/2 = 4 blocks
        let modulus = 1u64 << (2 * num_value_blocks); // 2 bits per block

        let clear_entries: Vec<(u8, u64)> =
            vec![(1, 10), (2, 42), (3, 100), (5, 200), (7, modulus - 1)];

        let mut gpu_kv_store: CudaKVStore<u8, CudaUnsignedRadixCiphertext> = CudaKVStore::new();
        for &(key, value) in &clear_entries {
            let ct = cks.encrypt_radix(value, num_value_blocks);
            let d_ct = CudaUnsignedRadixCiphertext::from_radix_ciphertext(&ct, &streams);
            gpu_kv_store.insert(key, d_ct);
        }

        let key: u64 = 2;
        let s: u64 = 10;
        let f = |ct: CudaUnsignedRadixCiphertext| sks.scalar_mul(&ct, s, &streams);

        let encrypted_key = cks.encrypt_radix(key, num_key_blocks);
        let d_encrypted_key =
            CudaUnsignedRadixCiphertext::from_radix_ciphertext(&encrypted_key, &streams);

        sks.kv_store_map(&mut gpu_kv_store, &d_encrypted_key, f, &streams);
    }
}