Skip to content

Commit b0d7bb9

Browse files
committed
chore: pre-generate keyswitching keys for shortint tests
- we run in a cross process race condition which fucks up the key file - no rust crate seems to help and linux locks are just a fucking mess - also avoid truncating file when we are going to write to it, get a lock first
1 parent 396f30f commit b0d7bb9

File tree

2 files changed

+31
-16
lines changed

2 files changed

+31
-16
lines changed

tfhe/examples/utilities/generates_test_keys.rs

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,16 @@ use tfhe::shortint::parameters::{
2727
};
2828
use tfhe::shortint::MultiBitPBSParameters;
2929

30+
const KSK_PARAMS: [(
31+
ClassicPBSParameters,
32+
ClassicPBSParameters,
33+
ShortintKeySwitchingParameters,
34+
); 1] = [(
35+
V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128,
36+
V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128,
37+
V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128,
38+
)];
39+
3040
fn client_server_keys() {
3141
let matches = Command::new("test key gen")
3242
.arg(
@@ -45,6 +55,9 @@ fn client_server_keys() {
4555
)
4656
.get_matches();
4757

58+
// Always generate those as they may be used in the different cases
59+
generate_ksk_keys(&KSK_PARAMS);
60+
4861
// If set using the command line flag "--ladner-fischer" this algorithm will be used in
4962
// additions
5063
let multi_bit_only: bool = matches.get_flag("multi_bit_only");
@@ -81,18 +94,6 @@ fn client_server_keys() {
8194
generate_pbs_multi_bit_keys(&MULTI_BIT_PARAMS);
8295
}
8396

84-
const KSK_PARAMS: [(
85-
ClassicPBSParameters,
86-
ClassicPBSParameters,
87-
ShortintKeySwitchingParameters,
88-
); 1] = [(
89-
V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128,
90-
V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128,
91-
V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128,
92-
)];
93-
94-
generate_ksk_keys(&KSK_PARAMS);
95-
9697
#[cfg(feature = "experimental")]
9798
{
9899
const WOPBS_PARAMS: [(ClassicPBSParameters, WopbsParameters); 1] = [(

tfhe/src/keycache/mod.rs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ pub mod utils {
77
use fs2::FileExt;
88
use serde::de::DeserializeOwned;
99
use serde::Serialize;
10-
use std::fs::File;
10+
use std::fs::{File, OpenOptions};
1111
use std::io::{BufReader, BufWriter};
1212
use std::ops::Deref;
1313
use std::path::PathBuf;
@@ -110,7 +110,10 @@ pub mod utils {
110110

111111
if path_buf.exists() {
112112
let file = File::open(&path_buf).unwrap();
113-
// Lock for reading
113+
// TODO Manage file locking for inter process stuff, unfortunately linux locks are a
114+
// mess and nothing seems to work
115+
//
116+
// Lock for reading this only works for our process not inter process
114117
fs2::FileExt::lock_shared(&file).unwrap();
115118
let file_reader = BufReader::new(file);
116119
bincode::deserialize_from::<_, (P, K)>(file_reader)
@@ -128,9 +131,20 @@ pub mod utils {
128131
path_buf.push(param.name());
129132
path_buf.set_extension("bin");
130133

131-
let file = File::create(&path_buf).unwrap();
132-
// Lock for writing
134+
let file = OpenOptions::new()
135+
.create(true)
136+
.write(true)
137+
.truncate(false)
138+
.open(&path_buf)
139+
.unwrap();
140+
141+
// TODO Manage file locking for inter process stuff, unfortunately linux locks are a
142+
// mess and nothing seems to work
143+
//
144+
// Lock for writing this only works for our process not inter process
133145
file.lock_exclusive().unwrap();
146+
// Truncate manually
147+
file.set_len(0).unwrap();
134148

135149
let file_writer = BufWriter::new(file);
136150
bincode::serialize_into(file_writer, &(param, key)).unwrap();

0 commit comments

Comments
 (0)