Compare commits

...

1 Commits

Author SHA1 Message Date
Mayeul@Zama
65fe15fc5b chore(core): remove log and modular standard_dev 2025-02-12 09:41:37 +01:00
2 changed files with 8 additions and 134 deletions

View File

@@ -6,7 +6,6 @@
//! we rely on different representations for this quantity:
//!
//! + $\sigma$ can be encoded in the [`StandardDev`] type.
//! + $p$ can be encoded in the [`LogStandardDev`] type.
//! + $\sigma^2$ can be encoded in the [`Variance`] type.
//!
//! In any of those cases, the corresponding type implements the `DispersionParameter` trait,
@@ -28,96 +27,15 @@ pub trait DispersionParameter: Copy {
fn get_standard_dev(&self) -> StandardDev;
/// Return the variance of the distribution, i.e. $\sigma^2 = 2^{2p}$.
fn get_variance(&self) -> Variance;
/// Return base 2 logarithm of the standard deviation of the distribution, i.e.
/// $\log\_2(\sigma)=p$
fn get_log_standard_dev(&self) -> LogStandardDev;
/// For a `Uint` type representing $\mathbb{Z}/2^q\mathbb{Z}$, we return $2^{q-p}$.
fn get_modular_standard_dev(&self, log2_modulus: u32) -> ModularStandardDev;
/// For a `Uint` type representing $\mathbb{Z}/2^q\mathbb{Z}$, we return $2^{2(q-p)}$.
fn get_modular_variance(&self, log2_modulus: u32) -> ModularVariance;
/// For a `Uint` type representing $\mathbb{Z}/2^q\mathbb{Z}$, we return $q-p$.
fn get_modular_log_standard_dev(&self, log2_modulus: u32) -> ModularLogStandardDev;
}
fn log2_modulus_to_modulus(log2_modulus: u32) -> f64 {
2.0f64.powi(log2_modulus as i32)
}
/// A distribution parameter that uses the base-2 logarithm of the standard deviation as
/// representation.
///
/// # Example:
///
/// ```rust
/// use tfhe::core_crypto::commons::dispersion::{DispersionParameter, LogStandardDev};
/// let params = LogStandardDev::from_log_standard_dev(-25.);
/// assert_eq!(params.get_standard_dev().0, 2_f64.powf(-25.));
/// assert_eq!(params.get_log_standard_dev().0, -25.);
/// assert_eq!(params.get_variance().0, 2_f64.powf(-25.).powi(2));
/// assert_eq!(
/// params.get_modular_standard_dev(32).value,
/// 2_f64.powf(32. - 25.)
/// );
/// assert_eq!(params.get_modular_log_standard_dev(32).value, 32. - 25.);
/// assert_eq!(
/// params.get_modular_variance(32).value,
/// 2_f64.powf(32. - 25.).powi(2)
/// );
///
/// let modular_params = LogStandardDev::from_modular_log_standard_dev(22., 32);
/// assert_eq!(modular_params.get_standard_dev().0, 2_f64.powf(-10.));
/// ```
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct LogStandardDev(pub f64);
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct ModularLogStandardDev {
pub value: f64,
pub modulus: f64,
}
impl LogStandardDev {
pub fn from_log_standard_dev(log_std: f64) -> Self {
Self(log_std)
}
pub fn from_modular_log_standard_dev(log_std: f64, log2_modulus: u32) -> Self {
Self(log_std - log2_modulus as f64)
}
}
impl DispersionParameter for LogStandardDev {
fn get_standard_dev(&self) -> StandardDev {
StandardDev(f64::powf(2., self.0))
}
fn get_variance(&self) -> Variance {
Variance(f64::powf(2., self.0 * 2.))
}
fn get_log_standard_dev(&self) -> Self {
Self(self.0)
}
fn get_modular_standard_dev(&self, log2_modulus: u32) -> ModularStandardDev {
ModularStandardDev {
value: f64::powf(2., log2_modulus as f64 + self.0),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
fn get_modular_variance(&self, log2_modulus: u32) -> ModularVariance {
ModularVariance {
value: f64::powf(2., (log2_modulus as f64 + self.0) * 2.),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
fn get_modular_log_standard_dev(&self, log2_modulus: u32) -> ModularLogStandardDev {
ModularLogStandardDev {
value: log2_modulus as f64 + self.0,
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
}
/// A distribution parameter that uses the standard deviation as representation.
///
/// # Example:
@@ -126,14 +44,8 @@ impl DispersionParameter for LogStandardDev {
/// use tfhe::core_crypto::commons::dispersion::{DispersionParameter, StandardDev};
/// let params = StandardDev::from_standard_dev(2_f64.powf(-25.));
/// assert_eq!(params.get_standard_dev().0, 2_f64.powf(-25.));
/// assert_eq!(params.get_log_standard_dev().0, -25.);
/// assert_eq!(params.get_variance().0, 2_f64.powf(-25.).powi(2));
/// assert_eq!(
/// params.get_modular_standard_dev(32).value,
/// 2_f64.powf(32. - 25.)
/// );
/// assert_eq!(params.get_modular_log_standard_dev(32).value, 32. - 25.);
/// assert_eq!(
/// params.get_modular_variance(32).value,
/// 2_f64.powf(32. - 25.).powi(2)
/// );
@@ -142,17 +54,15 @@ impl DispersionParameter for LogStandardDev {
#[versionize(StandardDevVersions)]
pub struct StandardDev(pub f64);
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
pub struct ModularStandardDev {
pub value: f64,
pub modulus: f64,
}
impl StandardDev {
pub fn from_standard_dev(std: f64) -> Self {
Self(std)
}
pub fn from_log_standard_dev(log_std: f64) -> Self {
Self(2_f64.powf(log_std))
}
pub fn from_modular_standard_dev(std: f64, log2_modulus: u32) -> Self {
Self(std / 2_f64.powf(log2_modulus as f64))
}
@@ -165,27 +75,12 @@ impl DispersionParameter for StandardDev {
fn get_variance(&self) -> Variance {
Variance(self.0.powi(2))
}
fn get_log_standard_dev(&self) -> LogStandardDev {
LogStandardDev(self.0.log2())
}
fn get_modular_standard_dev(&self, log2_modulus: u32) -> ModularStandardDev {
ModularStandardDev {
value: 2_f64.powf(log2_modulus as f64 + self.0.log2()),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
fn get_modular_variance(&self, log2_modulus: u32) -> ModularVariance {
ModularVariance {
value: 2_f64.powf(2. * (log2_modulus as f64 + self.0.log2())),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
fn get_modular_log_standard_dev(&self, log2_modulus: u32) -> ModularLogStandardDev {
ModularLogStandardDev {
value: log2_modulus as f64 + self.0.log2(),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
}
/// A distribution parameter that uses the variance as representation
@@ -196,14 +91,8 @@ impl DispersionParameter for StandardDev {
/// use tfhe::core_crypto::commons::dispersion::{DispersionParameter, Variance};
/// let params = Variance::from_variance(2_f64.powi(-50));
/// assert_eq!(params.get_standard_dev().0, 2_f64.powf(-25.));
/// assert_eq!(params.get_log_standard_dev().0, -25.);
/// assert_eq!(params.get_variance().0, 2_f64.powf(-25.).powi(2));
/// assert_eq!(
/// params.get_modular_standard_dev(32).value,
/// 2_f64.powf(32. - 25.)
/// );
/// assert_eq!(params.get_modular_log_standard_dev(32).value, 32. - 25.);
/// assert_eq!(
/// params.get_modular_variance(32).value,
/// 2_f64.powf(32. - 25.).powi(2)
/// );
@@ -234,25 +123,10 @@ impl DispersionParameter for Variance {
fn get_variance(&self) -> Self {
Self(self.0)
}
fn get_log_standard_dev(&self) -> LogStandardDev {
LogStandardDev(self.0.sqrt().log2())
}
fn get_modular_standard_dev(&self, log2_modulus: u32) -> ModularStandardDev {
ModularStandardDev {
value: 2_f64.powf(log2_modulus as f64 + self.0.sqrt().log2()),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
fn get_modular_variance(&self, log2_modulus: u32) -> ModularVariance {
ModularVariance {
value: 2_f64.powf(2. * (log2_modulus as f64 + self.0.sqrt().log2())),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
fn get_modular_log_standard_dev(&self, log2_modulus: u32) -> ModularLogStandardDev {
ModularLogStandardDev {
value: log2_modulus as f64 + self.0.sqrt().log2(),
modulus: log2_modulus_to_modulus(log2_modulus),
}
}
}

View File

@@ -1,7 +1,7 @@
use super::*;
use crate::core_crypto::algorithms::slice_algorithms::*;
use crate::core_crypto::algorithms::test::{FftWopPbsKeys, FftWopPbsTestParams};
use crate::core_crypto::commons::dispersion::LogStandardDev;
use crate::core_crypto::commons::dispersion::StandardDev;
use crate::core_crypto::commons::math::decomposition::SignedDecomposer;
use crate::core_crypto::commons::test_tools;
use crate::core_crypto::fft_impl::common::tests::gen_keys_or_get_from_cache_if_enabled;
@@ -103,7 +103,7 @@ pub fn test_extract_bits() {
let level_ksk = DecompositionLevelCount(7);
let base_log_ksk = DecompositionBaseLog(4);
let std = LogStandardDev::from_log_standard_dev(-60.);
let std = StandardDev::from_log_standard_dev(-60.);
let noise_distribution = DynamicDistribution::new_gaussian(std);
let ciphertext_modulus = CiphertextModulus::new_native();
@@ -266,7 +266,7 @@ fn test_circuit_bootstrapping_binary() {
let level_count_cbs = DecompositionLevelCount(1);
let base_log_cbs = DecompositionBaseLog(10);
let std = LogStandardDev::from_log_standard_dev(-60.);
let std = StandardDev::from_log_standard_dev(-60.);
let noise_distribution = DynamicDistribution::new_gaussian(std);
let ciphertext_modulus = CiphertextModulus::new_native();
@@ -451,7 +451,7 @@ pub fn test_cmux_tree() {
// Define settings for an insecure toy example
let polynomial_size = PolynomialSize(512);
let glwe_dimension = GlweDimension(1);
let std = LogStandardDev::from_log_standard_dev(-60.);
let std = StandardDev::from_log_standard_dev(-60.);
let noise_distribution = DynamicDistribution::new_gaussian(std);
let level = DecompositionLevelCount(3);
let base_log = DecompositionBaseLog(6);