1515use client_traits:: SecureAggregationClient ;
1616use kahe_traits:: { HasKahe , KaheBase , KaheEncrypt , KaheKeygen , TrySecretKeyInto } ;
1717use messages:: { ClientMessage , DecryptorPublicKey } ;
18+ use prng_traits:: SecurePrng ;
19+ use std:: cell:: RefCell ;
1820use vahe_traits:: { HasVahe , VaheBase , VerifiableEncrypt } ;
1921
2022/// Lightweight client directly exposing KAHE/VAHE types.
2123pub struct WillowV1Client < Kahe : KaheBase , Vahe : VaheBase > {
2224 pub kahe : Kahe ,
2325 pub vahe : Vahe ,
24- pub prng : Kahe :: Rng , // Using a single PRNG for both VAHE and KAHE.
26+ pub prng : RefCell < Kahe :: Rng > , // Using a single PRNG for both VAHE and KAHE.
2527}
2628
2729impl < Kahe : KaheBase , Vahe : VaheBase > HasKahe for WillowV1Client < Kahe , Vahe > {
@@ -38,6 +40,17 @@ impl<Kahe: KaheBase, Vahe: VaheBase> HasVahe for WillowV1Client<Kahe, Vahe> {
3840 }
3941}
4042
43+ impl < Kahe : KaheBase , Vahe : VaheBase > WillowV1Client < Kahe , Vahe > {
44+ pub fn new_with_randomly_generated_seed (
45+ kahe : Kahe ,
46+ vahe : Vahe ,
47+ ) -> Result < Self , status:: StatusError > {
48+ let seed = Kahe :: Rng :: generate_seed ( ) ?;
49+ let prng = RefCell :: new ( Kahe :: Rng :: create ( & seed) ?) ;
50+ Ok ( Self { kahe, vahe, prng } )
51+ }
52+ }
53+
4154/// Implementation of the `SecureAggregationClient` trait for the generic
4255/// KAHE/VAHE client, using WillowCommon as the common types (e.g. protocol
4356/// messages are directly the AHE public key and ciphertexts).
@@ -51,16 +64,17 @@ where
5164 type PlaintextSlice < ' a > = <Kahe as KaheBase >:: PlaintextSlice < ' a > ;
5265
5366 fn create_client_message (
54- & mut self ,
67+ & self ,
5568 plaintext : & Self :: PlaintextSlice < ' _ > ,
5669 signed_public_key : & DecryptorPublicKey < Vahe > ,
5770 nonce : & [ u8 ] ,
5871 ) -> Result < ClientMessage < Kahe , Vahe > , status:: StatusError > {
5972 // Generate a new KAHE key.
60- let kahe_secret_key = self . kahe . key_gen ( & mut self . prng ) ?;
73+ let kahe_secret_key = self . kahe . key_gen ( & mut self . prng . borrow_mut ( ) ) ?;
6174
6275 // Encrypt long plaintext with KAHE.
63- let kahe_ciphertext = self . kahe . encrypt ( plaintext, & kahe_secret_key, & mut self . prng ) ?;
76+ let kahe_ciphertext =
77+ self . kahe . encrypt ( plaintext, & kahe_secret_key, & mut self . prng . borrow_mut ( ) ) ?;
6478
6579 // Convert KAHE secret key into short AHE plaintext.
6680 let ahe_plaintext: Vahe :: Plaintext = self . kahe . try_secret_key_into ( kahe_secret_key) ?;
7084 & ahe_plaintext,
7185 signed_public_key,
7286 nonce,
73- & mut self . prng ,
87+ & mut self . prng . borrow_mut ( ) ,
7488 ) ?;
7589
7690 // Keep a copy of the nonce so the message can be forwarded as-is.
@@ -112,9 +126,7 @@ mod test {
112126 let ( kahe_config, ahe_config) = create_shell_configs ( & aggregation_config) ?;
113127 let kahe = ShellKahe :: new ( kahe_config, CONTEXT_STRING ) ?;
114128 let vahe = ShellVahe :: new ( ahe_config, CONTEXT_STRING ) ?;
115- let client_seed = SingleThreadHkdfPrng :: generate_seed ( ) ?;
116- let prng = SingleThreadHkdfPrng :: create ( & client_seed) ?;
117- let mut client = WillowV1Client { kahe, vahe, prng } ;
129+ let client = WillowV1Client :: new_with_randomly_generated_seed ( kahe, vahe) ?;
118130
119131 // Generate AHE keys.
120132 let mut testing_decryptor =
@@ -153,17 +165,13 @@ mod test {
153165 let ( kahe_config, ahe_config) = create_shell_configs ( & aggregation_config) ?;
154166 let kahe = ShellKahe :: new ( kahe_config, CONTEXT_STRING ) ?;
155167 let vahe = ShellVahe :: new ( ahe_config, CONTEXT_STRING ) ?;
156- let client1_seed = SingleThreadHkdfPrng :: generate_seed ( ) ?;
157- let prng = SingleThreadHkdfPrng :: create ( & client1_seed) ?;
158- let mut client1 = WillowV1Client { kahe, vahe, prng } ;
168+ let client1 = WillowV1Client :: new_with_randomly_generated_seed ( kahe, vahe) ?;
159169
160170 // Create a second client.
161171 let ( kahe_config, ahe_config) = create_shell_configs ( & aggregation_config) ?;
162172 let kahe = ShellKahe :: new ( kahe_config, CONTEXT_STRING ) ?;
163173 let vahe = ShellVahe :: new ( ahe_config, CONTEXT_STRING ) ?;
164- let client2_seed = SingleThreadHkdfPrng :: generate_seed ( ) ?;
165- let prng = SingleThreadHkdfPrng :: create ( & client2_seed) ?;
166- let mut client2 = WillowV1Client { kahe, vahe, prng } ;
174+ let client2 = WillowV1Client :: new_with_randomly_generated_seed ( kahe, vahe) ?;
167175
168176 // Generate AHE keys.
169177 let mut testing_decryptor =
0 commit comments