Skip to content
Draft
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
69 changes: 1 addition & 68 deletions tfhe/examples/utilities/generates_test_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@ use clap::{Arg, ArgAction, Command};
use tfhe::boolean;
use tfhe::boolean::parameters::{BooleanParameters, DEFAULT_PARAMETERS, DEFAULT_PARAMETERS_KS_PBS};
use tfhe::keycache::NamedParam;
#[cfg(feature = "experimental")]
use tfhe::shortint::keycache::KEY_CACHE_WOPBS;
use tfhe::shortint::keycache::{KEY_CACHE, KEY_CACHE_KSK};
#[cfg(tarpaulin)]
use tfhe::shortint::parameters::coverage_parameters::{
Expand All @@ -19,12 +17,6 @@ use tfhe::shortint::parameters::current_params::*;
use tfhe::shortint::parameters::{
ClassicPBSParameters, PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M128,
};
#[cfg(feature = "experimental")]
use tfhe::shortint::parameters::{
WopbsParameters, LEGACY_WOPBS_PARAM_MESSAGE_1_CARRY_1_KS_PBS,
LEGACY_WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS, LEGACY_WOPBS_PARAM_MESSAGE_3_CARRY_3_KS_PBS,
LEGACY_WOPBS_PARAM_MESSAGE_4_CARRY_4_KS_PBS,
};
use tfhe::shortint::MultiBitPBSParameters;

const KSK_PARAMS: [(
Expand Down Expand Up @@ -94,18 +86,9 @@ fn client_server_keys() {
generate_pbs_multi_bit_keys(&MULTI_BIT_PARAMS);
}

#[cfg(feature = "experimental")]
{
const WOPBS_PARAMS: [(ClassicPBSParameters, WopbsParameters); 1] = [(
V1_6_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M128,
LEGACY_WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS,
)];
generate_wopbs_keys(&WOPBS_PARAMS);
}

const BOOLEAN_PARAMS: [BooleanParameters; 2] =
[DEFAULT_PARAMETERS, DEFAULT_PARAMETERS_KS_PBS];
generate_boolean_keys(&BOOLEAN_PARAMS);
generate_boolean_keys(BOOLEAN_PARAMS.as_slice());
} else {
const PBS_KEYS: [ClassicPBSParameters; 15] = [
// TUniform
Expand All @@ -128,30 +111,6 @@ fn client_server_keys() {
V1_6_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M64,
];
generate_pbs_keys(&PBS_KEYS);

#[cfg(feature = "experimental")]
{
const WOPBS_PARAMS: [(ClassicPBSParameters, WopbsParameters); 4] = [
(
V1_6_PARAM_MESSAGE_1_CARRY_1_KS_PBS_GAUSSIAN_2M64,
LEGACY_WOPBS_PARAM_MESSAGE_1_CARRY_1_KS_PBS,
),
(
V1_6_PARAM_MESSAGE_2_CARRY_2_KS_PBS_GAUSSIAN_2M64,
LEGACY_WOPBS_PARAM_MESSAGE_2_CARRY_2_KS_PBS,
),
(
V1_6_PARAM_MESSAGE_3_CARRY_3_KS_PBS_GAUSSIAN_2M64,
LEGACY_WOPBS_PARAM_MESSAGE_3_CARRY_3_KS_PBS,
),
(
V1_6_PARAM_MESSAGE_4_CARRY_4_KS_PBS_GAUSSIAN_2M64,
LEGACY_WOPBS_PARAM_MESSAGE_4_CARRY_4_KS_PBS,
),
];

generate_wopbs_keys(&WOPBS_PARAMS);
}
}
}

Expand Down Expand Up @@ -235,32 +194,6 @@ fn generate_ksk_keys(
}
}

#[cfg(feature = "experimental")]
fn generate_wopbs_keys(params: &[(ClassicPBSParameters, WopbsParameters)]) {
println!("Generating woPBS keys");

for (i, (params_shortint, params_wopbs)) in params.iter().copied().enumerate() {
println!(
"Generating [{} / {}] : {}, {}",
i + 1,
params.len(),
params_shortint.name(),
params_wopbs.name(),
);

let start = std::time::Instant::now();

let _ = KEY_CACHE_WOPBS.get_from_param((params_shortint, params_wopbs));

let stop = start.elapsed().as_secs();

println!("Generation took {stop} seconds");

// Clear keys as we go to avoid filling the RAM
KEY_CACHE_WOPBS.clear_in_memory_cache()
}
}

fn generate_boolean_keys(params: &[BooleanParameters]) {
println!("Generating boolean (ClientKey, ServerKey)");

Expand Down
1 change: 0 additions & 1 deletion tfhe/src/high_level_api/xof_key_set/internal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ impl crate::integer::ClientKey {
glwe_secret_key,
lwe_secret_key,
parameters: std_params,
wopbs_parameters: None,
},
),
}
Expand Down
2 changes: 1 addition & 1 deletion tfhe/src/integer/client_key/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl ClientKey {
}

pub fn parameters(&self) -> crate::shortint::AtomicPatternParameters {
self.key.parameters().ap_parameters().unwrap()
self.key.parameters().ap_parameters()
}

#[cfg(test)]
Expand Down
39 changes: 0 additions & 39 deletions tfhe/src/integer/gpu/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ use crate::integer::server_key::{MiniUnsignedInteger, Reciprocable, ScalarMultip
use crate::integer::{ClientKey, RadixClientKey};
use crate::prelude::{CastFrom, CastInto};
use crate::shortint::ciphertext::{Degree, NoiseLevel};
use crate::shortint::parameters::ModulusSwitchType;
use crate::shortint::{CarryModulus, MessageModulus};
use crate::MatchValues;
use itertools::Itertools;
Expand Down Expand Up @@ -220,44 +219,6 @@ where
let shortint_parameters_set: crate::shortint::parameters::ShortintParameterSet =
parameters_set.try_into().unwrap();

let is_wopbs_only_params = shortint_parameters_set.wopbs_only();

// TODO
// Manually manage the wopbs only case as a workaround pending wopbs rework
// WOPBS used for PBS have no known failure probability at the moment, putting 1.0 for now
let shortint_parameters_set = if is_wopbs_only_params {
let wopbs_params = shortint_parameters_set.wopbs_parameters().unwrap();
let pbs_params = crate::shortint::parameters::ClassicPBSParameters {
lwe_dimension: wopbs_params.lwe_dimension,
glwe_dimension: wopbs_params.glwe_dimension,
polynomial_size: wopbs_params.polynomial_size,
lwe_noise_distribution: wopbs_params.lwe_noise_distribution,
glwe_noise_distribution: wopbs_params.glwe_noise_distribution,
pbs_base_log: wopbs_params.pbs_base_log,
pbs_level: wopbs_params.pbs_level,
ks_base_log: wopbs_params.ks_base_log,
ks_level: wopbs_params.ks_level,
message_modulus: wopbs_params.message_modulus,
carry_modulus: wopbs_params.carry_modulus,
max_noise_level: crate::shortint::parameters::MaxNoiseLevel::from_msg_carry_modulus(
wopbs_params.message_modulus,
wopbs_params.carry_modulus,
),
log2_p_fail: 1.0,
ciphertext_modulus: wopbs_params.ciphertext_modulus,
encryption_key_choice: wopbs_params.encryption_key_choice,
modulus_switch_noise_reduction_params: ModulusSwitchType::Standard,
};

crate::shortint::parameters::ShortintParameterSet::try_new_pbs_and_wopbs_param_set((
pbs_params,
wopbs_params,
))
.unwrap()
} else {
shortint_parameters_set
};

let gen_keys_inner = |parameters_set, streams: &CudaStreams| {
let cks = ClientKey::new(parameters_set);
let sks = CudaServerKey::new(&cks, streams);
Expand Down
33 changes: 0 additions & 33 deletions tfhe/src/integer/keycache.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
#[cfg(feature = "experimental")]
use crate::integer::wopbs::WopbsKey;
use crate::integer::{ClientKey, IntegerKeyKind, ServerKey};
use crate::shortint::atomic_pattern::AtomicPatternParameters;
#[cfg(feature = "experimental")]
use crate::shortint::{PBSParameters, WopbsParameters};

#[cfg(feature = "hpu")]
use std::sync::{Mutex, OnceLock};
Expand Down Expand Up @@ -94,33 +90,4 @@ impl IntegerKeyCache {
}
}

#[derive(Default)]
#[cfg(feature = "experimental")]
pub struct WopbsKeyCache;

#[cfg(feature = "experimental")]
impl WopbsKeyCache {
pub fn get_from_params<P>(&self, (pbs_params, wopbs_params): (P, WopbsParameters)) -> WopbsKey
where
P: Into<PBSParameters>,
{
let cache = &crate::shortint::keycache::KEY_CACHE_WOPBS;
let shortint_wops_key = cache.get_from_param((pbs_params, wopbs_params));

// For cargo nextest which runs in separate processes we load keys once per process, this
// allows to remove the copy loaded in the keycache to avoid OOM errors, the nice effect of
// linux file caching is that the keys will still be in RAM most likely, not requiring re
// re-reading from file for all processes.
if let Ok(val) = std::env::var("TFHE_RS_CLEAR_IN_MEMORY_KEY_CACHE") {
if val == "1" {
cache.clear_in_memory_cache()
}
}

WopbsKey::from(shortint_wops_key.wopbs_key().clone())
}
}

pub static KEY_CACHE: IntegerKeyCache = IntegerKeyCache::new();
#[cfg(feature = "experimental")]
pub static KEY_CACHE_WOPBS: WopbsKeyCache = WopbsKeyCache;
66 changes: 7 additions & 59 deletions tfhe/src/integer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ pub mod parameters;
pub mod prelude;
pub mod public_key;
pub mod server_key;
#[cfg(feature = "experimental")]
pub mod wopbs;

#[cfg(feature = "gpu")]
pub mod gpu;
Expand All @@ -79,7 +77,6 @@ pub mod hpu;
#[cfg(feature = "zk-pok")]
pub use ciphertext::ProvenCompactCiphertextList;

use crate::shortint::parameters::ModulusSwitchType;
pub use bigint::i256::I256;
pub use bigint::i512::I512;
pub use bigint::u256::U256;
Expand Down Expand Up @@ -119,68 +116,19 @@ where
let shortint_parameters_set: crate::shortint::parameters::ShortintParameterSet =
parameters_set.try_into().unwrap();

let is_wopbs_only_params = shortint_parameters_set.wopbs_only();

// TODO
// Manually manage the wopbs only case as a workaround pending wopbs rework
// WOPBS used for PBS have no known failure probability at the moment, putting 1.0 for now
let shortint_parameters_set = if is_wopbs_only_params {
let wopbs_params = shortint_parameters_set.wopbs_parameters().unwrap();
let pbs_params = crate::shortint::parameters::ClassicPBSParameters {
lwe_dimension: wopbs_params.lwe_dimension,
glwe_dimension: wopbs_params.glwe_dimension,
polynomial_size: wopbs_params.polynomial_size,
lwe_noise_distribution: wopbs_params.lwe_noise_distribution,
glwe_noise_distribution: wopbs_params.glwe_noise_distribution,
pbs_base_log: wopbs_params.pbs_base_log,
pbs_level: wopbs_params.pbs_level,
ks_base_log: wopbs_params.ks_base_log,
ks_level: wopbs_params.ks_level,
message_modulus: wopbs_params.message_modulus,
carry_modulus: wopbs_params.carry_modulus,
max_noise_level: crate::shortint::parameters::MaxNoiseLevel::from_msg_carry_modulus(
wopbs_params.message_modulus,
wopbs_params.carry_modulus,
),
log2_p_fail: 1.0,
ciphertext_modulus: wopbs_params.ciphertext_modulus,
encryption_key_choice: wopbs_params.encryption_key_choice,
modulus_switch_noise_reduction_params: ModulusSwitchType::Standard,
};

crate::shortint::parameters::ShortintParameterSet::try_new_pbs_and_wopbs_param_set((
pbs_params,
wopbs_params,
))
.unwrap()
} else {
shortint_parameters_set
};

let gen_keys_inner = |parameters_set| {
let cks = ClientKey::new(parameters_set);
#[cfg(any(test, feature = "internal-keycache"))]
{
keycache::KEY_CACHE.get_from_params(shortint_parameters_set.ap_parameters(), key_kind)
}
#[cfg(all(not(test), not(feature = "internal-keycache")))]
{
let cks = ClientKey::new(shortint_parameters_set);
let sks = match key_kind {
IntegerKeyKind::Radix => ServerKey::new_radix_server_key(&cks),
IntegerKeyKind::CRT => ServerKey::new_crt_server_key(&cks),
};

(cks, sks)
};

#[cfg(any(test, feature = "internal-keycache"))]
{
if is_wopbs_only_params {
// TODO
// Keycache is broken for the wopbs only case, so generate keys instead
gen_keys_inner(shortint_parameters_set)
} else {
keycache::KEY_CACHE
.get_from_params(shortint_parameters_set.ap_parameters().unwrap(), key_kind)
}
}
#[cfg(all(not(test), not(feature = "internal-keycache")))]
{
gen_keys_inner(shortint_parameters_set)
}
}

Expand Down
Loading
Loading