@@ -18,102 +18,102 @@ use tfhe::prelude::*;
1818use tfhe_trivium :: TriviumStream ;
1919
2020fn get_hexadecimal_string_from_lsb_first_stream (a : Vec <bool >) -> String {
21- assert! (a . len () % 8 == 0 );
22- let mut hexadecimal : String = "" . to_string ();
23- for test in a . chunks (8 ) {
24- // Encoding is bytes in LSB order
25- match test [4 .. 8 ] {
26- [false , false , false , false ] => hexadecimal . push ('0' ),
27- [true , false , false , false ] => hexadecimal . push ('1' ),
28- [false , true , false , false ] => hexadecimal . push ('2' ),
29- [true , true , false , false ] => hexadecimal . push ('3' ),
30-
31- [false , false , true , false ] => hexadecimal . push ('4' ),
32- [true , false , true , false ] => hexadecimal . push ('5' ),
33- [false , true , true , false ] => hexadecimal . push ('6' ),
34- [true , true , true , false ] => hexadecimal . push ('7' ),
35-
36- [false , false , false , true ] => hexadecimal . push ('8' ),
37- [true , false , false , true ] => hexadecimal . push ('9' ),
38- [false , true , false , true ] => hexadecimal . push ('A' ),
39- [true , true , false , true ] => hexadecimal . push ('B' ),
40-
41- [false , false , true , true ] => hexadecimal . push ('C' ),
42- [true , false , true , true ] => hexadecimal . push ('D' ),
43- [false , true , true , true ] => hexadecimal . push ('E' ),
44- [true , true , true , true ] => hexadecimal . push ('F' ),
45- _ => ()
46- };
47- match test [0 .. 4 ] {
48- [false , false , false , false ] => hexadecimal . push ('0' ),
49- [true , false , false , false ] => hexadecimal . push ('1' ),
50- [false , true , false , false ] => hexadecimal . push ('2' ),
51- [true , true , false , false ] => hexadecimal . push ('3' ),
52-
53- [false , false , true , false ] => hexadecimal . push ('4' ),
54- [true , false , true , false ] => hexadecimal . push ('5' ),
55- [false , true , true , false ] => hexadecimal . push ('6' ),
56- [true , true , true , false ] => hexadecimal . push ('7' ),
57-
58- [false , false , false , true ] => hexadecimal . push ('8' ),
59- [true , false , false , true ] => hexadecimal . push ('9' ),
60- [false , true , false , true ] => hexadecimal . push ('A' ),
61- [true , true , false , true ] => hexadecimal . push ('B' ),
62-
63- [false , false , true , true ] => hexadecimal . push ('C' ),
64- [true , false , true , true ] => hexadecimal . push ('D' ),
65- [false , true , true , true ] => hexadecimal . push ('E' ),
66- [true , true , true , true ] => hexadecimal . push ('F' ),
67- _ => ()
68- };
69- }
70- return hexadecimal ;
21+ assert! (a . len () % 8 == 0 );
22+ let mut hexadecimal : String = "" . to_string ();
23+ for test in a . chunks (8 ) {
24+ // Encoding is bytes in LSB order
25+ match test [4 .. 8 ] {
26+ [false , false , false , false ] => hexadecimal . push ('0' ),
27+ [true , false , false , false ] => hexadecimal . push ('1' ),
28+ [false , true , false , false ] => hexadecimal . push ('2' ),
29+ [true , true , false , false ] => hexadecimal . push ('3' ),
30+
31+ [false , false , true , false ] => hexadecimal . push ('4' ),
32+ [true , false , true , false ] => hexadecimal . push ('5' ),
33+ [false , true , true , false ] => hexadecimal . push ('6' ),
34+ [true , true , true , false ] => hexadecimal . push ('7' ),
35+
36+ [false , false , false , true ] => hexadecimal . push ('8' ),
37+ [true , false , false , true ] => hexadecimal . push ('9' ),
38+ [false , true , false , true ] => hexadecimal . push ('A' ),
39+ [true , true , false , true ] => hexadecimal . push ('B' ),
40+
41+ [false , false , true , true ] => hexadecimal . push ('C' ),
42+ [true , false , true , true ] => hexadecimal . push ('D' ),
43+ [false , true , true , true ] => hexadecimal . push ('E' ),
44+ [true , true , true , true ] => hexadecimal . push ('F' ),
45+ _ => ()
46+ };
47+ match test [0 .. 4 ] {
48+ [false , false , false , false ] => hexadecimal . push ('0' ),
49+ [true , false , false , false ] => hexadecimal . push ('1' ),
50+ [false , true , false , false ] => hexadecimal . push ('2' ),
51+ [true , true , false , false ] => hexadecimal . push ('3' ),
52+
53+ [false , false , true , false ] => hexadecimal . push ('4' ),
54+ [true , false , true , false ] => hexadecimal . push ('5' ),
55+ [false , true , true , false ] => hexadecimal . push ('6' ),
56+ [true , true , true , false ] => hexadecimal . push ('7' ),
57+
58+ [false , false , false , true ] => hexadecimal . push ('8' ),
59+ [true , false , false , true ] => hexadecimal . push ('9' ),
60+ [false , true , false , true ] => hexadecimal . push ('A' ),
61+ [true , true , false , true ] => hexadecimal . push ('B' ),
62+
63+ [false , false , true , true ] => hexadecimal . push ('C' ),
64+ [true , false , true , true ] => hexadecimal . push ('D' ),
65+ [false , true , true , true ] => hexadecimal . push ('E' ),
66+ [true , true , true , true ] => hexadecimal . push ('F' ),
67+ _ => ()
68+ };
69+ }
70+ return hexadecimal ;
7171}
7272
7373fn main () {
74- let config = ConfigBuilder :: default (). build ();
75- let (client_key , server_key ) = generate_keys (config );
74+ let config = ConfigBuilder :: default (). build ();
75+ let (client_key , server_key ) = generate_keys (config );
7676
77- let key_string = " 0053A6F94C9FF24598EB" . to_string ();
78- let mut key = [false ; 80 ];
77+ let key_string = " 0053A6F94C9FF24598EB" . to_string ();
78+ let mut key = [false ; 80 ];
7979
80- for i in (0 .. key_string . len ()). step_by (2 ) {
81- let mut val : u8 = u8 :: from_str_radix (& key_string [i .. i + 2 ], 16 ). unwrap ();
82- for j in 0 .. 8 {
83- key [8 * (i >> 1 ) + j ] = val % 2 == 1 ;
84- val >>= 1 ;
85- }
86- }
80+ for i in (0 .. key_string . len ()). step_by (2 ) {
81+ let mut val : u8 = u8 :: from_str_radix (& key_string [i .. i + 2 ], 16 ). unwrap ();
82+ for j in 0 .. 8 {
83+ key [8 * (i >> 1 ) + j ] = val % 2 == 1 ;
84+ val >>= 1 ;
85+ }
86+ }
8787
88- let iv_string = " 0D74DB42A91077DE45AC" . to_string ();
89- let mut iv = [false ; 80 ];
88+ let iv_string = " 0D74DB42A91077DE45AC" . to_string ();
89+ let mut iv = [false ; 80 ];
9090
91- for i in (0 .. iv_string . len ()). step_by (2 ) {
92- let mut val : u8 = u8 :: from_str_radix (& iv_string [i .. i + 2 ], 16 ). unwrap ();
93- for j in 0 .. 8 {
94- iv [8 * (i >> 1 ) + j ] = val % 2 == 1 ;
95- val >>= 1 ;
96- }
97- }
91+ for i in (0 .. iv_string . len ()). step_by (2 ) {
92+ let mut val : u8 = u8 :: from_str_radix (& iv_string [i .. i + 2 ], 16 ). unwrap ();
93+ for j in 0 .. 8 {
94+ iv [8 * (i >> 1 ) + j ] = val % 2 == 1 ;
95+ val >>= 1 ;
96+ }
97+ }
9898
99- let output_0_63 = " F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358" . to_string ();
99+ let output_0_63 = " F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358" . to_string ();
100100
101- let cipher_key = key . map (| x | FheBool :: encrypt (x , & client_key ));
102- let cipher_iv = iv . map (| x | FheBool :: encrypt (x , & client_key ));
101+ let cipher_key = key . map (| x | FheBool :: encrypt (x , & client_key ));
102+ let cipher_iv = iv . map (| x | FheBool :: encrypt (x , & client_key ));
103103
104104
105- let mut trivium = TriviumStream :: <FheBool >:: new (cipher_key , cipher_iv , & server_key );
105+ let mut trivium = TriviumStream :: <FheBool >:: new (cipher_key , cipher_iv , & server_key );
106106
107- let mut vec = Vec :: <bool >:: with_capacity (64 * 8 );
108- while vec . len () < 64 * 8 {
109- let cipher_outputs = trivium . next_64 ();
110- for c in cipher_outputs {
111- vec . push (c . decrypt (& client_key ))
112- }
113- }
107+ let mut vec = Vec :: <bool >:: with_capacity (64 * 8 );
108+ while vec . len () < 64 * 8 {
109+ let cipher_outputs = trivium . next_64 ();
110+ for c in cipher_outputs {
111+ vec . push (c . decrypt (& client_key ))
112+ }
113+ }
114114
115- let hexadecimal = get_hexadecimal_string_from_lsb_first_stream (vec );
116- assert_eq! (output_0_63 , hexadecimal [0 .. 64 * 2 ]);
115+ let hexadecimal = get_hexadecimal_string_from_lsb_first_stream (vec );
116+ assert_eq! (output_0_63 , hexadecimal [0 .. 64 * 2 ]);
117117}
118118```
119119
@@ -129,75 +129,76 @@ Other sizes than 64 bit are expected to be available in the future.
129129
130130# FHE shortint Trivium implementation
131131
132- The same implementation is also available for generic Ciphertexts representing bits (meant to be used with parameters ` V0_11_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64 ` ).
132+ The same implementation is also available for generic Ciphertexts representing bits (meant to be used with parameters ` V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128 ` ).
133133It uses a lower level API of tfhe-rs, so the syntax is a little bit different. It also implements the ` TransCiphering ` trait. For optimization purposes, it does not internally run
134134on the same cryptographic parameters as the high level API of tfhe-rs. As such, it requires the usage of a casting key, to switch from one parameter space to another, which makes
135135its setup a little more intricate.
136136
137137Example code:
138138``` rust
139139use tfhe :: shortint :: prelude :: * ;
140- use tfhe :: shortint :: parameters :: {
141- V0_11_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64 ,
142- V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64 ,
140+ use tfhe :: shortint :: parameters :: v1_0 :: {
141+ V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128 ,
142+ V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128 ,
143+ V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128 ,
143144};
144145use tfhe :: {ConfigBuilder , generate_keys, FheUint64 };
145146use tfhe :: prelude :: * ;
146147use tfhe_trivium :: TriviumStreamShortint ;
147148
148149fn test_shortint () {
149- let config = ConfigBuilder :: default ()
150- . use_custom_parameters (V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64 )
150+ let config = ConfigBuilder :: default ()
151+ . use_custom_parameters (V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128 )
151152 . build ();
152- let (hl_client_key , hl_server_key ) = generate_keys (config );
153+ let (hl_client_key , hl_server_key ) = generate_keys (config );
153154 let underlying_ck : tfhe :: shortint :: ClientKey = (* hl_client_key . as_ref ()). clone (). into ();
154155 let underlying_sk : tfhe :: shortint :: ServerKey = (* hl_server_key . as_ref ()). clone (). into ();
155156
156- let (client_key , server_key ): (ClientKey , ServerKey ) = gen_keys (V0_11_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64 );
157+ let (client_key , server_key ): (ClientKey , ServerKey ) = gen_keys (V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128 );
157158 let ksk = KeySwitchingKey :: new (
158159 (& client_key , Some (& server_key )),
159160 (& underlying_ck , & underlying_sk ),
160- V0_11_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS ,
161+ V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128_2M128 ,
161162 );
162163
163- let key_string = " 0053A6F94C9FF24598EB" . to_string ();
164- let mut key = [0 ; 80 ];
164+ let key_string = " 0053A6F94C9FF24598EB" . to_string ();
165+ let mut key = [0 ; 80 ];
165166
166- for i in (0 .. key_string . len ()). step_by (2 ) {
167- let mut val = u64 :: from_str_radix (& key_string [i .. i + 2 ], 16 ). unwrap ();
168- for j in 0 .. 8 {
169- key [8 * (i >> 1 ) + j ] = val % 2 ;
170- val >>= 1 ;
171- }
172- }
167+ for i in (0 .. key_string . len ()). step_by (2 ) {
168+ let mut val = u64 :: from_str_radix (& key_string [i .. i + 2 ], 16 ). unwrap ();
169+ for j in 0 .. 8 {
170+ key [8 * (i >> 1 ) + j ] = val % 2 ;
171+ val >>= 1 ;
172+ }
173+ }
173174
174- let iv_string = " 0D74DB42A91077DE45AC" . to_string ();
175- let mut iv = [0 ; 80 ];
175+ let iv_string = " 0D74DB42A91077DE45AC" . to_string ();
176+ let mut iv = [0 ; 80 ];
176177
177- for i in (0 .. iv_string . len ()). step_by (2 ) {
178- let mut val = u64 :: from_str_radix (& iv_string [i .. i + 2 ], 16 ). unwrap ();
179- for j in 0 .. 8 {
180- iv [8 * (i >> 1 ) + j ] = val % 2 ;
181- val >>= 1 ;
182- }
183- }
184- let output_0_63 = " F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358" . to_string ();
178+ for i in (0 .. iv_string . len ()). step_by (2 ) {
179+ let mut val = u64 :: from_str_radix (& iv_string [i .. i + 2 ], 16 ). unwrap ();
180+ for j in 0 .. 8 {
181+ iv [8 * (i >> 1 ) + j ] = val % 2 ;
182+ val >>= 1 ;
183+ }
184+ }
185+ let output_0_63 = " F4CD954A717F26A7D6930830C4E7CF0819F80E03F25F342C64ADC66ABA7F8A8E6EAA49F23632AE3CD41A7BD290A0132F81C6D4043B6E397D7388F3A03B5FE358" . to_string ();
185186
186- let cipher_key = key . map (| x | client_key . encrypt (x ));
187- let cipher_iv = iv . map (| x | client_key . encrypt (x ));
187+ let cipher_key = key . map (| x | client_key . encrypt (x ));
188+ let cipher_iv = iv . map (| x | client_key . encrypt (x ));
188189
189- let mut ciphered_message = vec! [FheUint64 :: try_encrypt (0u64 , & hl_client_key ). unwrap (); 9 ];
190+ let mut ciphered_message = vec! [FheUint64 :: try_encrypt (0u64 , & hl_client_key ). unwrap (); 9 ];
190191
191- let mut trivium = TriviumStreamShortint :: new (cipher_key , cipher_iv , & server_key , & ksk );
192+ let mut trivium = TriviumStreamShortint :: new (cipher_key , cipher_iv , & server_key , & ksk );
192193
193- let mut vec = Vec :: <u64 >:: with_capacity (8 );
194- while vec . len () < 8 {
195- let trans_ciphered_message = trivium . trans_encrypt_64 (ciphered_message . pop (). unwrap (), & hl_server_key );
196- vec . push (trans_ciphered_message . decrypt (& hl_client_key ));
197- }
194+ let mut vec = Vec :: <u64 >:: with_capacity (8 );
195+ while vec . len () < 8 {
196+ let trans_ciphered_message = trivium . trans_encrypt_64 (ciphered_message . pop (). unwrap (), & hl_server_key );
197+ vec . push (trans_ciphered_message . decrypt (& hl_client_key ));
198+ }
198199
199- let hexadecimal = get_hexagonal_string_from_u64 (vec );
200- assert_eq! (output_0_63 , hexadecimal [0 .. 64 * 2 ]);
200+ let hexadecimal = get_hexagonal_string_from_u64 (vec );
201+ assert_eq! (output_0_63 , hexadecimal [0 .. 64 * 2 ]);
201202}
202203```
203204
0 commit comments