@@ -113,11 +113,12 @@ fn unpack_and_sanitize_message_and_carries(
113
113
114
114
/// This function sanitizes boolean blocks to make sure they encrypt a 0 or a 1
115
115
fn sanitize_boolean_blocks (
116
- packed_blocks : Vec < Ciphertext > ,
116
+ expanded_blocks : Vec < Ciphertext > ,
117
117
sks : & ServerKey ,
118
118
infos : & [ DataKind ] ,
119
119
) -> Vec < Ciphertext > {
120
120
let message_modulus = sks. message_modulus ( ) . 0 ;
121
+ let msg_extract = sks. key . generate_lookup_table ( |x : u64 | x % message_modulus) ;
121
122
let msg_extract_bool = sks. key . generate_lookup_table ( |x : u64 | {
122
123
let tmp = x % message_modulus;
123
124
if tmp == 0 {
@@ -138,15 +139,15 @@ fn sanitize_boolean_blocks(
138
139
let acc = if matches ! ( data_kind, DataKind :: Boolean ) {
139
140
Some ( & msg_extract_bool)
140
141
} else {
141
- None
142
+ Some ( & msg_extract )
142
143
} ;
143
144
144
145
functions[ overall_block_idx] = acc;
145
146
overall_block_idx += 1 ;
146
147
}
147
148
}
148
149
149
- packed_blocks
150
+ expanded_blocks
150
151
. into_par_iter ( )
151
152
. zip ( functions. into_par_iter ( ) )
152
153
. map ( |( mut block, sanitize_acc) | {
@@ -479,7 +480,10 @@ impl IntegerUnpackingToShortintCastingModeHelper {
479
480
}
480
481
}
481
482
482
- pub fn generate_function ( & self , infos : & [ DataKind ] ) -> CastingFunctionsOwned {
483
+ pub fn generate_unpack_and_sanitize_functions (
484
+ & self ,
485
+ infos : & [ DataKind ] ,
486
+ ) -> CastingFunctionsOwned {
483
487
let block_count: usize = infos. iter ( ) . map ( |x| x. num_blocks ( ) ) . sum ( ) ;
484
488
let packed_block_count = block_count. div_ceil ( 2 ) ;
485
489
let mut functions = vec ! [ Some ( Vec :: with_capacity( 2 ) ) ; packed_block_count] ;
@@ -515,6 +519,30 @@ impl IntegerUnpackingToShortintCastingModeHelper {
515
519
516
520
functions
517
521
}
522
+
523
+ pub fn generate_sanitize_without_unpacking_functions (
524
+ & self ,
525
+ infos : & [ DataKind ] ,
526
+ ) -> CastingFunctionsOwned {
527
+ let total_block_count: usize = infos. iter ( ) . map ( |x| x. num_blocks ( ) ) . sum ( ) ;
528
+ let mut functions = Vec :: with_capacity ( total_block_count) ;
529
+
530
+ for data_kind in infos {
531
+ let block_count = data_kind. num_blocks ( ) ;
532
+ for _ in 0 ..block_count {
533
+ let sanitize_function: & ( dyn Fn ( u64 ) -> u64 + Sync ) =
534
+ if matches ! ( data_kind, DataKind :: Boolean ) {
535
+ self . msg_extract_bool . as_ref ( )
536
+ } else {
537
+ self . msg_extract . as_ref ( )
538
+ } ;
539
+
540
+ functions. push ( Some ( vec ! [ sanitize_function] ) ) ;
541
+ }
542
+ }
543
+
544
+ functions
545
+ }
518
546
}
519
547
520
548
impl CompactCiphertextList {
@@ -681,23 +709,21 @@ impl CompactCiphertextList {
681
709
IntegerCompactCiphertextListExpansionMode :: CastAndUnpackIfNecessary (
682
710
key_switching_key_view,
683
711
) => {
684
- let function_helper;
685
- let functions;
712
+ let dest_sks = & key_switching_key_view. key . dest_server_key ;
713
+ let function_helper = IntegerUnpackingToShortintCastingModeHelper :: new (
714
+ dest_sks. message_modulus ,
715
+ dest_sks. carry_modulus ,
716
+ ) ;
686
717
let functions = if is_packed {
687
- let dest_sks = & key_switching_key_view. key . dest_server_key ;
688
- function_helper = IntegerUnpackingToShortintCastingModeHelper :: new (
689
- dest_sks. message_modulus ,
690
- dest_sks. carry_modulus ,
691
- ) ;
692
- functions = function_helper. generate_function ( & self . info ) ;
693
- Some ( functions. as_slice ( ) )
718
+ function_helper. generate_unpack_and_sanitize_functions ( & self . info )
694
719
} else {
695
- None
720
+ function_helper . generate_sanitize_without_unpacking_functions ( & self . info )
696
721
} ;
722
+
697
723
self . ct_list
698
724
. expand ( ShortintCompactCiphertextListCastingMode :: CastIfNecessary {
699
725
casting_key : key_switching_key_view. key ,
700
- functions,
726
+ functions : Some ( functions . as_slice ( ) ) ,
701
727
} ) ?
702
728
}
703
729
IntegerCompactCiphertextListExpansionMode :: UnpackAndSanitizeIfNecessary ( sks) => {
@@ -811,26 +837,23 @@ impl ProvenCompactCiphertextList {
811
837
IntegerCompactCiphertextListExpansionMode :: CastAndUnpackIfNecessary (
812
838
key_switching_key_view,
813
839
) => {
814
- let function_helper;
815
- let functions;
840
+ let dest_sks = & key_switching_key_view. key . dest_server_key ;
841
+ let function_helper = IntegerUnpackingToShortintCastingModeHelper :: new (
842
+ dest_sks. message_modulus ,
843
+ dest_sks. carry_modulus ,
844
+ ) ;
816
845
let functions = if is_packed {
817
- let dest_sks = & key_switching_key_view. key . dest_server_key ;
818
- function_helper = IntegerUnpackingToShortintCastingModeHelper :: new (
819
- dest_sks. message_modulus ,
820
- dest_sks. carry_modulus ,
821
- ) ;
822
- functions = function_helper. generate_function ( & self . info ) ;
823
- Some ( functions. as_slice ( ) )
846
+ function_helper. generate_unpack_and_sanitize_functions ( & self . info )
824
847
} else {
825
- None
848
+ function_helper . generate_sanitize_without_unpacking_functions ( & self . info )
826
849
} ;
827
850
self . ct_list . verify_and_expand (
828
851
crs,
829
852
& public_key. key ,
830
853
metadata,
831
854
ShortintCompactCiphertextListCastingMode :: CastIfNecessary {
832
855
casting_key : key_switching_key_view. key ,
833
- functions,
856
+ functions : Some ( functions . as_slice ( ) ) ,
834
857
} ,
835
858
) ?
836
859
}
@@ -902,23 +925,20 @@ impl ProvenCompactCiphertextList {
902
925
IntegerCompactCiphertextListExpansionMode :: CastAndUnpackIfNecessary (
903
926
key_switching_key_view,
904
927
) => {
905
- let function_helper;
906
- let functions;
928
+ let dest_sks = & key_switching_key_view. key . dest_server_key ;
929
+ let function_helper = IntegerUnpackingToShortintCastingModeHelper :: new (
930
+ dest_sks. message_modulus ,
931
+ dest_sks. carry_modulus ,
932
+ ) ;
907
933
let functions = if is_packed {
908
- let dest_sks = & key_switching_key_view. key . dest_server_key ;
909
- function_helper = IntegerUnpackingToShortintCastingModeHelper :: new (
910
- dest_sks. message_modulus ,
911
- dest_sks. carry_modulus ,
912
- ) ;
913
- functions = function_helper. generate_function ( & self . info ) ;
914
- Some ( functions. as_slice ( ) )
934
+ function_helper. generate_unpack_and_sanitize_functions ( & self . info )
915
935
} else {
916
- None
936
+ function_helper . generate_sanitize_without_unpacking_functions ( & self . info )
917
937
} ;
918
938
self . ct_list . expand_without_verification (
919
939
ShortintCompactCiphertextListCastingMode :: CastIfNecessary {
920
940
casting_key : key_switching_key_view. key ,
921
- functions,
941
+ functions : Some ( functions . as_slice ( ) ) ,
922
942
} ,
923
943
) ?
924
944
}
0 commit comments