Skip to content

Commit

Permalink
refactor(shortint): move parameters set to their own directory
Browse files Browse the repository at this point in the history
This is done to ease automatic parameters updates.
  • Loading branch information
soonum committed Sep 11, 2024
1 parent c0d9839 commit 488c942
Show file tree
Hide file tree
Showing 19 changed files with 87 additions and 70 deletions.
4 changes: 2 additions & 2 deletions tfhe/benches/integer/zk_pke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use tfhe::integer::parameters::{
use tfhe::integer::{ClientKey, CompactPrivateKey, CompactPublicKey, ServerKey};
use tfhe::keycache::NamedParam;
use tfhe::shortint::parameters::classic::tuniform::p_fail_2_minus_64::ks_pbs::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::PBSParameters;
use tfhe::zk::{CompactPkeCrs, ZkComputeLoad};
use utilities::{write_to_json, OperatorType};
Expand Down
4 changes: 2 additions & 2 deletions tfhe/docs/guides/zk-pok.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ pub fn main() -> Result<(), Box<dyn std::error::Error>> {

let params = tfhe::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
// Indicate which parameters to use for the Compact Public Key encryption
let cpk_params = tfhe::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
let cpk_params = tfhe::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
// And parameters allowing to keyswitch/cast to the computation parameters.
let casting_params = tfhe::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
let casting_params = tfhe::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
// Enable the dedicated parameters on the config
let config = tfhe::ConfigBuilder::with_custom_parameters(params)
.use_dedicated_compact_public_key_parameters((cpk_params, casting_params));
Expand Down
6 changes: 3 additions & 3 deletions tfhe/examples/utilities/generates_test_keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ use tfhe::shortint::parameters::coverage_parameters::{
COVERAGE_PARAM_MESSAGE_2_CARRY_3_KS_PBS, COVERAGE_PARAM_MESSAGE_5_CARRY_1_KS_PBS,
COVERAGE_PARAM_MULTI_BIT_MESSAGE_2_CARRY_2_GROUP_2_KS_PBS,
};
use tfhe::shortint::parameters::key_switching::{
ShortintKeySwitchingParameters, PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS,
};
use tfhe::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS;
use tfhe::shortint::parameters::key_switching::ShortintKeySwitchingParameters;

use tfhe::shortint::parameters::{
ClassicPBSParameters, WopbsParameters, ALL_MULTI_BIT_PARAMETER_VEC,
PARAM_MESSAGE_1_CARRY_1_KS_PBS, PARAM_MESSAGE_1_CARRY_2_KS_PBS, PARAM_MESSAGE_1_CARRY_3_KS_PBS,
Expand Down
4 changes: 2 additions & 2 deletions tfhe/examples/utilities/shortint_key_sizes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::io::Write;
use std::path::Path;
use tfhe::keycache::NamedParam;
use tfhe::shortint::keycache::KEY_CACHE;
use tfhe::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::list_compression::COMP_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use tfhe::shortint::parameters::{
PARAM_MESSAGE_1_CARRY_1_KS_PBS, PARAM_MESSAGE_2_CARRY_2_KS_PBS,
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/c_api/shortint/parameters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ pub use crate::core_crypto::commons::dispersion::StandardDev;
pub use crate::core_crypto::commons::parameters::{
DecompositionBaseLog, DecompositionLevelCount, GlweDimension, LweDimension, PolynomialSize,
};
pub use crate::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
pub use crate::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
pub use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
pub use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
pub use crate::shortint::parameters::*;
use std::os::raw::c_int;

Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/high_level_api/compact_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -541,8 +541,8 @@ mod tests {
#[cfg(feature = "zk-pok")]
#[test]
fn test_proven_compact_list_with_casting() {
use crate::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;

let config = crate::ConfigBuilder::with_custom_parameters(
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/high_level_api/integers/unsigned/tests/cpu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ use crate::high_level_api::{generate_keys, set_server_key, ConfigBuilder, FheUin
use crate::integer::U256;
use crate::safe_deserialization::safe_deserialize_conformant;
use crate::shortint::parameters::classic::compact_pk::*;
use crate::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::*;
use crate::{
ClientKey, CompactCiphertextList, CompactCiphertextListConformanceParams, CompactPublicKey,
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/high_level_api/tests/tags_on_entities.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::prelude::*;
use crate::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::*;
use crate::shortint::ClassicPBSParameters;
use crate::{
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/integer/ciphertext/compact_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -668,8 +668,8 @@ mod tests {
ClientKey, CompactPrivateKey, CompactPublicKey, RadixCiphertext, ServerKey,
};
use crate::shortint::parameters::classic::tuniform::p_fail_2_minus_64::ks_pbs::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::zk::{CompactPkeCrs, ZkComputeLoad};
use rand::random;

Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/integer/key_switching_key/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::integer::{
ClientKey, CompactPrivateKey, CompactPublicKey, CrtClientKey, IntegerCiphertext,
IntegerKeyKind, RadixCiphertext, RadixClientKey, ServerKey,
};
use crate::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::{
use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::{
PARAM_KEYSWITCH_PKE_TO_BIG_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
PARAM_KEYSWITCH_PKE_TO_SMALL_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
};
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/js_on_wasm_api/shortint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ use crate::core_crypto::commons::generators::DeterministicSeeder;
use crate::core_crypto::commons::math::random::Seed;
use crate::core_crypto::prelude::ActivatedRandomGenerator;
use crate::shortint::parameters::classic::compact_pk::*;
use crate::shortint::parameters::compact_public_key_only::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::compact_public_key_only::p_fail_2_minus_64::ks_pbs::PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
use crate::shortint::parameters::*;
use std::panic::set_hook;
use wasm_bindgen::prelude::*;
Expand Down
1 change: 1 addition & 0 deletions tfhe/src/shortint/keycache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::shortint::parameters::classic::compact_pk::*;
use crate::shortint::parameters::classic::tuniform::p_fail_2_minus_64::ks_pbs::PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64;
#[cfg(tarpaulin)]
use crate::shortint::parameters::coverage_parameters::*;
use crate::shortint::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS;
use crate::shortint::parameters::key_switching::*;
use crate::shortint::parameters::multi_bit::*;
use crate::shortint::parameters::parameters_wopbs::*;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
pub mod p_fail_2_minus_64;

use super::{CiphertextModulus, PBSOrder};
use crate::core_crypto::commons::parameters::{DynamicDistribution, LweDimension};
use crate::shortint::backward_compatibility::parameters::compact_public_key_only::{
Expand Down Expand Up @@ -149,14 +151,3 @@ impl TryFrom<PBSParameters> for CompactPublicKeyEncryptionParameters {
params.try_into()
}
}

pub const PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64: CompactPublicKeyEncryptionParameters =
CompactPublicKeyEncryptionParameters {
encryption_lwe_dimension: LweDimension(1024),
encryption_noise_distribution: DynamicDistribution::new_t_uniform(42),
message_modulus: MessageModulus(4),
carry_modulus: CarryModulus(4),
ciphertext_modulus: CiphertextModulus::new_native(),
expansion_kind: CompactCiphertextListExpansionKind::RequiresCasting,
}
.validate();
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use crate::core_crypto::commons::parameters::{
CiphertextModulus, DynamicDistribution, LweDimension,
};
use crate::shortint::parameters::{
CarryModulus, CompactCiphertextListExpansionKind, CompactPublicKeyEncryptionParameters,
MessageModulus,
};

pub const PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64: CompactPublicKeyEncryptionParameters =
CompactPublicKeyEncryptionParameters {
encryption_lwe_dimension: LweDimension(1024),
encryption_noise_distribution: DynamicDistribution::new_t_uniform(42),
message_modulus: MessageModulus(4),
carry_modulus: CarryModulus(4),
ciphertext_modulus: CiphertextModulus::new_native(),
expansion_kind: CompactCiphertextListExpansionKind::RequiresCasting,
}
.validate();
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod ks_pbs;
39 changes: 39 additions & 0 deletions tfhe/src/shortint/parameters/key_switching/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
pub mod p_fail_2_minus_64;

use crate::shortint::backward_compatibility::parameters::key_switching::ShortintKeySwitchingParametersVersions;
use crate::shortint::parameters::{
DecompositionBaseLog, DecompositionLevelCount, EncryptionKeyChoice,
};
use serde::{Deserialize, Serialize};
use tfhe_versionable::Versionize;

/// A set of cryptographic parameters for homomorphic Shortint key switching.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Versionize)]
#[versionize(ShortintKeySwitchingParametersVersions)]
pub struct ShortintKeySwitchingParameters {
pub ks_base_log: DecompositionBaseLog,
pub ks_level: DecompositionLevelCount,
pub destination_key: EncryptionKeyChoice,
}

impl ShortintKeySwitchingParameters {
/// Constructs a new set of parameters for shortint key switching.
///
/// # Warning
///
/// Failing to fix the parameters properly would yield incorrect and insecure computation.
/// Unless you are a cryptographer who really knows the impact of each of those parameters, you
/// __must__ stick with the provided parameters (if any), which both offer correct results with
/// 128 bits of security.
pub fn new(
ks_base_log: DecompositionBaseLog,
ks_level: DecompositionLevelCount,
destination_key: EncryptionKeyChoice,
) -> Self {
Self {
ks_base_log,
ks_level,
destination_key,
}
}
}
Original file line number Diff line number Diff line change
@@ -1,41 +1,7 @@
use crate::shortint::backward_compatibility::parameters::key_switching::ShortintKeySwitchingParametersVersions;
use crate::shortint::parameters::{
DecompositionBaseLog, DecompositionLevelCount, EncryptionKeyChoice,
ShortintKeySwitchingParameters,
};
use serde::{Deserialize, Serialize};
use tfhe_versionable::Versionize;

/// A set of cryptographic parameters for homomorphic Shortint key switching.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, Versionize)]
#[versionize(ShortintKeySwitchingParametersVersions)]
pub struct ShortintKeySwitchingParameters {
pub ks_base_log: DecompositionBaseLog,
pub ks_level: DecompositionLevelCount,
pub destination_key: EncryptionKeyChoice,
}

impl ShortintKeySwitchingParameters {
/// Constructs a new set of parameters for shortint key switching.
///
/// # Warning
///
/// Failing to fix the parameters properly would yield incorrect and insecure computation.
/// Unless you are a cryptographer who really knows the impact of each of those parameters, you
/// __must__ stick with the provided parameters (if any), which both offer correct results with
/// 128 bits of security.
pub fn new(
ks_base_log: DecompositionBaseLog,
ks_level: DecompositionLevelCount,
destination_key: EncryptionKeyChoice,
) -> Self {
Self {
ks_base_log,
ks_level,
destination_key,
}
}
}

pub const PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS: ShortintKeySwitchingParameters =
ShortintKeySwitchingParameters {
ks_level: DecompositionLevelCount(15),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod ks_pbs;
2 changes: 1 addition & 1 deletion tfhe/src/shortint/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub use super::ciphertext::{Ciphertext, CompressedCiphertext, PBSOrder};
pub use super::client_key::ClientKey;
pub use super::gen_keys;
pub use super::key_switching_key::KeySwitchingKey;
pub use super::parameters::key_switching::PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS;
pub use super::parameters::key_switching::p_fail_2_minus_64::ks_pbs::PARAM_KEYSWITCH_1_1_KS_PBS_TO_2_2_KS_PBS;
pub use super::parameters::{
CarryModulus, CiphertextModulus, ClassicPBSParameters, DecompositionBaseLog,
DecompositionLevelCount, EncryptionKeyChoice, GlweDimension, LweDimension, MaxNoiseLevel,
Expand Down

0 comments on commit 488c942

Please sign in to comment.