Skip to content

Commit 9400890

Browse files
tholopcopybara-github
authored andcommitted
Use interior mutability for the Client's PRNG; add constructor.
PiperOrigin-RevId: 859651327
1 parent 68bbe5b commit 9400890

File tree

7 files changed

+36
-50
lines changed

7 files changed

+36
-50
lines changed

willow/benches/shell_benchmarks.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,7 @@ fn setup_base(args: &Args) -> BaseInputs {
131131
// Create client.
132132
let kahe = ShellKahe::new(kahe_config.clone(), CONTEXT_STRING).unwrap();
133133
let vahe = ShellVahe::new(ahe_config.clone(), CONTEXT_STRING).unwrap();
134-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
135-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
136-
let client = WillowV1Client { kahe, vahe, prng };
134+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
137135

138136
// Create decryptor.
139137
let vahe = ShellVahe::new(ahe_config.clone(), CONTEXT_STRING).unwrap();
@@ -218,7 +216,7 @@ struct VerifierInputs {
218216
}
219217

220218
fn setup_verifier_verify_client_message(args: &Args) -> VerifierInputs {
221-
let mut inputs = setup_base(args);
219+
let inputs = setup_base(args);
222220
let mut decryption_request_contributions = vec![];
223221
for _ in 0..args.n_iterations {
224222
// Generates a plaintext and encrypts.
@@ -257,7 +255,7 @@ fn run_verifier_verify_client_message(inputs: &mut VerifierInputs) {
257255
}
258256

259257
fn setup_server_handle_client_message(args: &Args) -> ServerInputs {
260-
let mut inputs = setup_base(args);
258+
let inputs = setup_base(args);
261259
let mut ciphertext_contributions = vec![];
262260
for _ in 0..args.n_iterations {
263261
// Generates a plaintext and encrypts.

willow/src/api/client.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ impl WillowShellClient {
8686
let context_bytes = aggregation_config.compute_context_bytes()?;
8787
let kahe = ShellKahe::new(kahe_config, &context_bytes)?;
8888
let vahe = ShellVahe::new(ahe_config, &context_bytes)?;
89-
let client_seed = SingleThreadHkdfPrng::generate_seed()?;
90-
let prng = SingleThreadHkdfPrng::create(&client_seed)?;
91-
let client = WillowV1Client { kahe, vahe, prng };
89+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe)?;
9290
Ok(WillowShellClient(client))
9391
}
9492

willow/src/traits/client.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub trait SecureAggregationClient: HasKahe + HasVahe {
2626
/// Creates a client message to be sent to the Server.
2727
/// nonce is used for the VAHE encryption, has to be unique.
2828
fn create_client_message(
29-
&mut self,
29+
&self,
3030
plaintext: &Self::PlaintextSlice<'_>,
3131
signed_public_key: &DecryptorPublicKey<<Self as HasVahe>::Vahe>,
3232
nonce: &[u8],

willow/src/willow_v1/client.rs

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,15 @@
1515
use client_traits::SecureAggregationClient;
1616
use kahe_traits::{HasKahe, KaheBase, KaheEncrypt, KaheKeygen, TrySecretKeyInto};
1717
use messages::{ClientMessage, DecryptorPublicKey};
18+
use prng_traits::SecurePrng;
19+
use std::cell::RefCell;
1820
use vahe_traits::{HasVahe, VaheBase, VerifiableEncrypt};
1921

2022
/// Lightweight client directly exposing KAHE/VAHE types.
2123
pub 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

2729
impl<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)?;
@@ -70,7 +84,7 @@ where
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 =

willow/src/willow_v1/server.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,7 @@ mod tests {
392392
CONTEXT_STRING,
393393
)
394394
.unwrap();
395-
let seed = SingleThreadHkdfPrng::generate_seed()?;
396-
let prng = SingleThreadHkdfPrng::create(&seed)?;
397-
let mut client = WillowV1Client { kahe, vahe, prng };
395+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe)?;
398396

399397
// Create decryptor.
400398
let vahe = ShellVahe::new(

willow/src/willow_v1/verifier.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,9 +305,7 @@ mod tests {
305305
CONTEXT_STRING,
306306
)
307307
.unwrap();
308-
let seed = SingleThreadHkdfPrng::generate_seed()?;
309-
let prng = SingleThreadHkdfPrng::create(&seed)?;
310-
let mut client = WillowV1Client { kahe, vahe, prng };
308+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe)?;
311309

312310
// Create decryptor, which needs its own `vahe` (with same public polynomials
313311
// generated from the seeds) and `prng`.

willow/tests/willow_v1_shell.rs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ fn encrypt_decrypt_one() -> googletest::Result<()> {
5757
let kahe =
5858
ShellKahe::new(create_shell_kahe_config(&aggregation_config).unwrap(), CONTEXT_STRING)
5959
.unwrap();
60-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
61-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
62-
let mut client = WillowV1Client { kahe, vahe, prng };
60+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
6361

6462
// Create decryptor, which needs its own `vahe` (with same public polynomials
6563
// generated from the seeds) and `prng`.
@@ -147,9 +145,7 @@ fn encrypt_decrypt_one_serialized() -> googletest::Result<()> {
147145
let vahe =
148146
ShellVahe::new(create_shell_ahe_config(max_number_of_decryptors).unwrap(), CONTEXT_STRING)
149147
.unwrap();
150-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
151-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
152-
let mut client = WillowV1Client { kahe, vahe, prng };
148+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
153149

154150
// Create decryptor, which needs its own `vahe` (with same public polynomials
155151
// generated from the seeds) and `prng`.
@@ -287,9 +283,7 @@ fn encrypt_decrypt_multiple_clients() -> googletest::Result<()> {
287283
let kahe =
288284
ShellKahe::new(create_shell_kahe_config(&aggregation_config).unwrap(), CONTEXT_STRING)
289285
.unwrap();
290-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
291-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
292-
let client = WillowV1Client { kahe, vahe, prng };
286+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
293287
clients.push(client);
294288
}
295289

@@ -420,9 +414,7 @@ fn encrypt_decrypt_multiple_clients_including_invalid_proofs() -> googletest::Re
420414
let kahe =
421415
ShellKahe::new(create_shell_kahe_config(&aggregation_config).unwrap(), CONTEXT_STRING)
422416
.unwrap();
423-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
424-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
425-
let client = WillowV1Client { kahe, vahe, prng };
417+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
426418
good_clients.push(client);
427419
}
428420

@@ -437,9 +429,7 @@ fn encrypt_decrypt_multiple_clients_including_invalid_proofs() -> googletest::Re
437429
let kahe =
438430
ShellKahe::new(create_shell_kahe_config(&aggregation_config).unwrap(), CONTEXT_STRING)
439431
.unwrap();
440-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
441-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
442-
let client = WillowV1Client { kahe, vahe, prng };
432+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
443433
bad_clients.push(client);
444434
}
445435

@@ -652,9 +642,7 @@ fn encrypt_decrypt_many_clients_decryptors() -> googletest::Result<()> {
652642
let kahe =
653643
ShellKahe::new(create_shell_kahe_config(&aggregation_config).unwrap(), CONTEXT_STRING)
654644
.unwrap();
655-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
656-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
657-
let mut client = WillowV1Client { kahe, vahe, prng };
645+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
658646

659647
let client_input_values =
660648
generate_random_unsigned_vector(INPUT_LENGTH as usize, INPUT_DOMAIN as u64);
@@ -730,9 +718,7 @@ fn encrypt_decrypt_no_dropout() -> googletest::Result<()> {
730718
let kahe =
731719
ShellKahe::new(create_shell_kahe_config(&aggregation_config).unwrap(), CONTEXT_STRING)
732720
.unwrap();
733-
let seed = SingleThreadHkdfPrng::generate_seed().unwrap();
734-
let prng = SingleThreadHkdfPrng::create(&seed).unwrap();
735-
let client = WillowV1Client { kahe, vahe, prng };
721+
let client = WillowV1Client::new_with_randomly_generated_seed(kahe, vahe).unwrap();
736722
clients.push(client);
737723
}
738724

0 commit comments

Comments
 (0)