From 3b22760ee081a52a3116ed0083db55d6d1461ef1 Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Mon, 9 Dec 2024 15:05:09 +0100 Subject: [PATCH] chore(csprng)!: remove generator_aarch64_aes feature BREAKING_CHANGE: - The `generator_aarch64_aes` feature is no longer supported for tfhe-csprng --- tfhe-csprng/Cargo.toml | 3 +- tfhe-csprng/build.rs | 93 ------------------- tfhe-csprng/examples/generate.rs | 4 +- .../generators/implem/aarch64/block_cipher.rs | 3 +- tfhe-csprng/src/generators/implem/mod.rs | 4 +- tfhe/Cargo.toml | 6 +- .../math/random/activated_random_generator.rs | 8 +- 7 files changed, 12 insertions(+), 109 deletions(-) delete mode 100644 tfhe-csprng/build.rs diff --git a/tfhe-csprng/Cargo.toml b/tfhe-csprng/Cargo.toml index 13623d39d4..ce6ebd34cd 100644 --- a/tfhe-csprng/Cargo.toml +++ b/tfhe-csprng/Cargo.toml @@ -26,11 +26,10 @@ clap = "=4.4.4" [features] parallel = ["rayon"] generator_fallback = [] -generator_aarch64_aes = [] x86_64 = ["parallel", "generator_fallback"] x86_64-unix = ["x86_64"] -aarch64 = ["parallel", "generator_aarch64_aes", "generator_fallback"] +aarch64 = ["parallel", "generator_fallback"] aarch64-unix = ["aarch64"] software_prng = [] diff --git a/tfhe-csprng/build.rs b/tfhe-csprng/build.rs deleted file mode 100644 index c6ac34527c..0000000000 --- a/tfhe-csprng/build.rs +++ /dev/null @@ -1,93 +0,0 @@ -// To have clear error messages during compilation about why some piece of code may not be available -// we decided to check the features compatibility with the target configuration in this script. - -use std::collections::HashMap; -use std::env; - -// See https://doc.rust-lang.org/reference/conditional-compilation.html#target_arch for various -// compilation configuration - -// Can be easily extended if needed -pub struct FeatureRequirement { - pub feature_name: &'static str, - // target_arch requirement - pub feature_req_target_arch: Option<&'static str>, - // target_family requirement - pub feature_req_target_family: Option<&'static str>, -} - -// We implement a version of default that is const which is not possible through the Default trait -impl FeatureRequirement { - // As we cannot use cfg!(feature = "feature_name") with something else than a literal, we need - // a reference to the HashMap we populate with the enabled features - fn is_activated(&self, build_activated_features: &HashMap<&'static str, bool>) -> bool { - *build_activated_features.get(self.feature_name).unwrap() - } - - // panics if the requirements are not met - fn check_requirements(&self) { - let target_arch = get_target_arch_cfg(); - if let Some(feature_req_target_arch) = self.feature_req_target_arch { - if feature_req_target_arch != target_arch { - panic!( - "Feature `{}` requires target_arch `{}`, current cfg: `{}`", - self.feature_name, feature_req_target_arch, target_arch - ) - } - } - - let target_families = get_target_family_cfgs(); - if let Some(feature_req_target_family) = self.feature_req_target_family { - if target_families - .split(',') - .all(|family| family != feature_req_target_family) - { - panic!( - "Feature `{}` requires target_family `{}`, current cfgs: `{}`", - self.feature_name, feature_req_target_family, target_families - ) - } - } - } -} - -// const vecs are not yet a thing so use a fixed size array (update the array size when adding -// requirements) -static FEATURE_REQUIREMENTS: [FeatureRequirement; 1] = [FeatureRequirement { - feature_name: "generator_aarch64_aes", - feature_req_target_arch: Some("aarch64"), - feature_req_target_family: None, -}]; - -// For a "feature_name" feature_cfg!("feature_name") expands to -// ("feature_name", cfg!(feature = "feature_name")) -macro_rules! feature_cfg { - ($feat_name:literal) => { - ($feat_name, cfg!(feature = $feat_name)) - }; -} - -// Static HashMap would require an additional crate (phf or lazy static e.g.), so we just write a -// function that returns the HashMap we are interested in -fn get_feature_enabled_status() -> HashMap<&'static str, bool> { - HashMap::from([feature_cfg!("generator_aarch64_aes")]) -} - -// See https://stackoverflow.com/a/43435335/18088947 for the inspiration of this code -fn get_target_arch_cfg() -> String { - env::var("CARGO_CFG_TARGET_ARCH").expect("CARGO_CFG_TARGET_ARCH is not set") -} - -fn get_target_family_cfgs() -> String { - env::var("CARGO_CFG_TARGET_FAMILY").expect("CARGO_CFG_TARGET_FAMILY is not set") -} - -fn main() { - let feature_enabled_status = get_feature_enabled_status(); - - // This will panic if some requirements for a feature are not met - FEATURE_REQUIREMENTS - .iter() - .filter(|&req| FeatureRequirement::is_activated(req, &feature_enabled_status)) - .for_each(FeatureRequirement::check_requirements); -} diff --git a/tfhe-csprng/examples/generate.rs b/tfhe-csprng/examples/generate.rs index 3ff3ede11b..fa8b03e66c 100644 --- a/tfhe-csprng/examples/generate.rs +++ b/tfhe-csprng/examples/generate.rs @@ -4,12 +4,12 @@ use clap::{value_parser, Arg, Command}; #[cfg(all(target_arch = "x86_64", target_feature = "aes"))] use tfhe_csprng::generators::AesniRandomGenerator as ActivatedRandomGenerator; -#[cfg(feature = "generator_aarch64_aes")] +#[cfg(all(target_arch = "aarch64", target_feature = "neon"))] use tfhe_csprng::generators::NeonAesRandomGenerator as ActivatedRandomGenerator; use tfhe_csprng::generators::RandomGenerator; #[cfg(all( not(all(target_arch = "x86_64", target_feature = "aes")), - not(feature = "generator_aarch64_aes"), + not(all(target_arch = "aarch64", target_feature = "neon")), ))] use tfhe_csprng::generators::SoftwareRandomGenerator as ActivatedRandomGenerator; diff --git a/tfhe-csprng/src/generators/implem/aarch64/block_cipher.rs b/tfhe-csprng/src/generators/implem/aarch64/block_cipher.rs index 3ccb139aa7..0b8dd502da 100644 --- a/tfhe-csprng/src/generators/implem/aarch64/block_cipher.rs +++ b/tfhe-csprng/src/generators/implem/aarch64/block_cipher.rs @@ -25,7 +25,8 @@ impl AesBlockCipher for ArmAesBlockCipher { if !(aes_detected && neon_detected) { panic!( "The ArmAesBlockCipher requires both aes and neon aarch64 CPU features.\n\ - aes feature available: {}\nneon feature available: {}\n.", + aes feature available: {}\nneon feature available: {}\n\ + Please consider enabling the SoftwareRandomGenerator with the `software_prng` feature", aes_detected, neon_detected ) } diff --git a/tfhe-csprng/src/generators/implem/mod.rs b/tfhe-csprng/src/generators/implem/mod.rs index 55b8c38677..027f3b0bc3 100644 --- a/tfhe-csprng/src/generators/implem/mod.rs +++ b/tfhe-csprng/src/generators/implem/mod.rs @@ -3,9 +3,9 @@ mod aesni; #[cfg(target_arch = "x86_64")] pub use aesni::*; -#[cfg(feature = "generator_aarch64_aes")] +#[cfg(target_arch = "aarch64")] mod aarch64; -#[cfg(feature = "generator_aarch64_aes")] +#[cfg(target_arch = "aarch64")] pub use aarch64::*; #[cfg(feature = "software_prng")] diff --git a/tfhe/Cargo.toml b/tfhe/Cargo.toml index 1f26d67d75..c33b45287b 100644 --- a/tfhe/Cargo.toml +++ b/tfhe/Cargo.toml @@ -132,10 +132,6 @@ parallel-wasm-api = ["dep:wasm-bindgen-rayon"] nightly-avx512 = ["tfhe-fft/nightly", "tfhe-ntt/nightly", "pulp/nightly"] -# Enable the aarch64 specific accelerated implementation of the random generator for the default -# backend -generator_aarch64_aes = ["tfhe-csprng/generator_aarch64_aes"] - # Private features __profiling = [] __long_run_tests = [] @@ -147,7 +143,7 @@ __long_run_tests = [] x86_64 = [] x86_64-unix = ["x86_64"] -aarch64 = ["generator_aarch64_aes"] +aarch64 = [] aarch64-unix = ["aarch64"] [package.metadata.docs.rs] diff --git a/tfhe/src/core_crypto/commons/math/random/activated_random_generator.rs b/tfhe/src/core_crypto/commons/math/random/activated_random_generator.rs index 63cd4c97b1..25058050a0 100644 --- a/tfhe/src/core_crypto/commons/math/random/activated_random_generator.rs +++ b/tfhe/src/core_crypto/commons/math/random/activated_random_generator.rs @@ -1,19 +1,19 @@ #[cfg(all(target_arch = "x86_64", target_feature = "aes"))] use tfhe_csprng::generators::AesniRandomGenerator; -#[cfg(feature = "generator_aarch64_aes")] +#[cfg(all(target_arch = "aarch64", target_feature = "neon"))] use tfhe_csprng::generators::NeonAesRandomGenerator; #[cfg(all( not(all(target_arch = "x86_64", target_feature = "aes")), - not(feature = "generator_aarch64_aes") + not(all(target_arch = "aarch64", target_feature = "neon")) ))] use tfhe_csprng::generators::SoftwareRandomGenerator; #[cfg(all(target_arch = "x86_64", target_feature = "aes"))] pub type ActivatedRandomGenerator = AesniRandomGenerator; -#[cfg(feature = "generator_aarch64_aes")] +#[cfg(all(target_arch = "aarch64", target_feature = "neon"))] pub type ActivatedRandomGenerator = NeonAesRandomGenerator; #[cfg(all( not(all(target_arch = "x86_64", target_feature = "aes")), - not(feature = "generator_aarch64_aes") + not(all(target_arch = "aarch64", target_feature = "neon")) ))] pub type ActivatedRandomGenerator = SoftwareRandomGenerator;