Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 13 additions & 12 deletions tfhe/examples/utilities/generates_test_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ use tfhe::shortint::parameters::{
};
use tfhe::shortint::MultiBitPBSParameters;

const KSK_PARAMS: [(
ClassicPBSParameters,
ClassicPBSParameters,
ShortintKeySwitchingParameters,
); 1] = [(
V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128,
V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128,
V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128,
)];

fn client_server_keys() {
let matches = Command::new("test key gen")
.arg(
Expand All @@ -45,6 +55,9 @@ fn client_server_keys() {
)
.get_matches();

// Always generate those as they may be used in the different cases
generate_ksk_keys(&KSK_PARAMS);

// If set using the command line flag "--ladner-fischer" this algorithm will be used in
// additions
let multi_bit_only: bool = matches.get_flag("multi_bit_only");
Expand Down Expand Up @@ -81,18 +94,6 @@ fn client_server_keys() {
generate_pbs_multi_bit_keys(&MULTI_BIT_PARAMS);
}

const KSK_PARAMS: [(
ClassicPBSParameters,
ClassicPBSParameters,
ShortintKeySwitchingParameters,
); 1] = [(
V1_0_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M128,
V1_0_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128,
V1_0_PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS_GAUSSIAN_2M128,
)];

generate_ksk_keys(&KSK_PARAMS);

#[cfg(feature = "experimental")]
{
const WOPBS_PARAMS: [(ClassicPBSParameters, WopbsParameters); 1] = [(
Expand Down
22 changes: 18 additions & 4 deletions tfhe/src/keycache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod utils {
use fs2::FileExt;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::fs::File;
use std::fs::{File, OpenOptions};
use std::io::{BufReader, BufWriter};
use std::ops::Deref;
use std::path::PathBuf;
Expand Down Expand Up @@ -110,7 +110,10 @@ pub mod utils {

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

let file = File::create(&path_buf).unwrap();
// Lock for writing
let file = OpenOptions::new()
.create(true)
.write(true)
.truncate(false)
.open(&path_buf)
.unwrap();

// TODO Manage file locking for inter process stuff, unfortunately linux locks are a
// mess and nothing seems to work
//
// Lock for writing this only works for our process not inter process
file.lock_exclusive().unwrap();
// Truncate manually
file.set_len(0).unwrap();

let file_writer = BufWriter::new(file);
bincode::serialize_into(file_writer, &(param, key)).unwrap();
Expand Down
Loading