From db28761db268bcf5b955a3badcd6369612dd54e8 Mon Sep 17 00:00:00 2001 From: Pedro Barbosa <1180592@isep.ipp.pt> Date: Tue, 23 Jan 2024 14:26:45 +0000 Subject: [PATCH] changed wasm32 support from feature-based to target-based compilation --- tfhe/Cargo.toml | 5 +---- tfhe/src/boolean/engine/bootstrapping.rs | 4 ++-- tfhe/src/boolean/engine/mod.rs | 8 ++++---- tfhe/src/core_crypto/seeders.rs | 8 ++++---- tfhe/src/shortint/ciphertext/mod.rs | 4 ++-- tfhe/src/shortint/engine/public_side.rs | 8 ++++---- tfhe/src/shortint/engine/server_side.rs | 8 ++++---- tfhe/src/shortint/public_key/compact.rs | 4 ++-- tfhe/src/shortint/public_key/standard.rs | 4 ++-- 9 files changed, 25 insertions(+), 28 deletions(-) diff --git a/tfhe/Cargo.toml b/tfhe/Cargo.toml index 2670bb6a46..924aabfea3 100644 --- a/tfhe/Cargo.toml +++ b/tfhe/Cargo.toml @@ -100,15 +100,12 @@ boolean-c-api = ["boolean", "__c_api"] shortint-c-api = ["shortint", "__c_api"] high-level-c-api = ["boolean-c-api", "shortint-c-api", "integer", "__c_api"] -wasm = [ - "dep:getrandom", -] __wasm_api = [ - "wasm", "dep:wasm-bindgen", "dep:js-sys", "dep:console_error_panic_hook", "dep:serde-wasm-bindgen", + "dep:getrandom", "getrandom/js", ] boolean-client-js-wasm-api = ["boolean", "__wasm_api"] diff --git a/tfhe/src/boolean/engine/bootstrapping.rs b/tfhe/src/boolean/engine/bootstrapping.rs index b99cff9aab..3b2de9444d 100644 --- a/tfhe/src/boolean/engine/bootstrapping.rs +++ b/tfhe/src/boolean/engine/bootstrapping.rs @@ -336,7 +336,7 @@ impl Bootstrapper { } pub(crate) fn new_compressed_server_key(&mut self, cks: &ClientKey) -> CompressedServerKey { - #[cfg(not(feature = "wasm"))] + #[cfg(not(target_arch = "wasm32"))] let bootstrapping_key = par_allocate_and_generate_new_seeded_lwe_bootstrap_key( &cks.lwe_secret_key, &cks.glwe_secret_key, @@ -347,7 +347,7 @@ impl Bootstrapper { &mut self.seeder, ); - #[cfg(feature = "wasm")] + #[cfg(target_arch = "wasm32")] let bootstrapping_key = allocate_and_generate_new_seeded_lwe_bootstrap_key( &cks.lwe_secret_key, &cks.glwe_secret_key, diff --git a/tfhe/src/boolean/engine/mod.rs b/tfhe/src/boolean/engine/mod.rs index 5930f9e53a..e57573850d 100644 --- a/tfhe/src/boolean/engine/mod.rs +++ b/tfhe/src/boolean/engine/mod.rs @@ -129,7 +129,7 @@ impl BooleanEngine { lwe_sk.lwe_dimension().to_lwe_size().0 * LOG2_Q_32 + 128, ); - #[cfg(not(feature = "wasm"))] + #[cfg(not(target_arch = "wasm32"))] let lwe_public_key: LwePublicKeyOwned = par_allocate_and_generate_new_lwe_public_key( &lwe_sk, zero_encryption_count, @@ -138,7 +138,7 @@ impl BooleanEngine { &mut self.encryption_generator, ); - #[cfg(feature = "wasm")] + #[cfg(target_arch = "wasm32")] let lwe_public_key: LwePublicKeyOwned = allocate_and_generate_new_lwe_public_key( &lwe_sk, zero_encryption_count, @@ -172,7 +172,7 @@ impl BooleanEngine { lwe_sk.lwe_dimension().to_lwe_size().0 * LOG2_Q_32 + 128, ); - #[cfg(not(feature = "wasm"))] + #[cfg(not(target_arch = "wasm32"))] let compressed_lwe_public_key = par_allocate_and_generate_new_seeded_lwe_public_key( &lwe_sk, zero_encryption_count, @@ -181,7 +181,7 @@ impl BooleanEngine { &mut self.bootstrapper.seeder, ); - #[cfg(feature = "wasm")] + #[cfg(target_arch = "wasm32")] let compressed_lwe_public_key = allocate_and_generate_new_seeded_lwe_public_key( &lwe_sk, zero_encryption_count, diff --git a/tfhe/src/core_crypto/seeders.rs b/tfhe/src/core_crypto/seeders.rs index aae0cb28fc..89ca175652 100644 --- a/tfhe/src/core_crypto/seeders.rs +++ b/tfhe/src/core_crypto/seeders.rs @@ -5,14 +5,14 @@ //! for cryptographically secure pseudo random number generators. pub use crate::core_crypto::commons::math::random::Seeder; -#[cfg(all(target_os = "macos", not(feature = "wasm")))] +#[cfg(all(target_os = "macos", not(target_arch = "wasm32")))] pub use concrete_csprng::seeders::AppleSecureEnclaveSeeder; #[cfg(feature = "seeder_x86_64_rdseed")] pub use concrete_csprng::seeders::RdseedSeeder; #[cfg(feature = "seeder_unix")] pub use concrete_csprng::seeders::UnixSeeder; -#[cfg(feature = "wasm")] +#[cfg(target_arch = "wasm32")] mod wasm_seeder { use crate::core_crypto::commons::math::random::{Seed, Seeder}; // This is used for web interfaces @@ -73,7 +73,7 @@ pub fn new_seeder() -> Box { let err_msg; - #[cfg(not(feature = "wasm"))] + #[cfg(not(target_arch = "wasm32"))] { #[cfg(feature = "seeder_x86_64_rdseed")] { @@ -110,7 +110,7 @@ pub fn new_seeder() -> Box { } } - #[cfg(feature = "wasm")] + #[cfg(target_arch = "wasm32")] { if seeder.is_none() && wasm_seeder::WasmSeeder::is_available() { seeder = Some(Box::new(wasm_seeder::WasmSeeder {})); diff --git a/tfhe/src/shortint/ciphertext/mod.rs b/tfhe/src/shortint/ciphertext/mod.rs index 8ff54f6dc5..53ab938178 100644 --- a/tfhe/src/shortint/ciphertext/mod.rs +++ b/tfhe/src/shortint/ciphertext/mod.rs @@ -600,14 +600,14 @@ impl CompactCiphertextList { ); // No parallelism allowed - #[cfg(all(feature = "wasm", not(feature = "parallel-wasm-api")))] + #[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))] { use crate::core_crypto::prelude::expand_lwe_compact_ciphertext_list; expand_lwe_compact_ciphertext_list(&mut output_lwe_ciphertext_list, &self.ct_list); } // Parallelism allowed - #[cfg(any(not(feature = "wasm"), feature = "parallel-wasm-api"))] + #[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))] { use crate::core_crypto::prelude::par_expand_lwe_compact_ciphertext_list; par_expand_lwe_compact_ciphertext_list(&mut output_lwe_ciphertext_list, &self.ct_list); diff --git a/tfhe/src/shortint/engine/public_side.rs b/tfhe/src/shortint/engine/public_side.rs index 0ad769f2d4..61ea204cc9 100644 --- a/tfhe/src/shortint/engine/public_side.rs +++ b/tfhe/src/shortint/engine/public_side.rs @@ -39,7 +39,7 @@ impl ShortintEngine { secret_encryption_key.lwe_dimension().to_lwe_size(), ); - #[cfg(any(not(feature = "wasm"), feature = "parallel-wasm-api"))] + #[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))] let lwe_public_key = par_allocate_and_generate_new_lwe_public_key( &secret_encryption_key, zero_encryption_count, @@ -48,7 +48,7 @@ impl ShortintEngine { &mut self.encryption_generator, ); - #[cfg(all(feature = "wasm", not(feature = "parallel-wasm-api")))] + #[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))] let lwe_public_key = allocate_and_generate_new_lwe_public_key( &secret_encryption_key, zero_encryption_count, @@ -85,7 +85,7 @@ impl ShortintEngine { secret_encryption_key.lwe_dimension().to_lwe_size(), ); - #[cfg(any(not(feature = "wasm"), feature = "parallel-wasm-api"))] + #[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))] let compressed_public_key = par_allocate_and_generate_new_seeded_lwe_public_key( &secret_encryption_key, zero_encryption_count, @@ -94,7 +94,7 @@ impl ShortintEngine { &mut self.seeder, ); - #[cfg(all(feature = "wasm", not(feature = "parallel-wasm-api")))] + #[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))] let compressed_public_key = allocate_and_generate_new_seeded_lwe_public_key( &secret_encryption_key, zero_encryption_count, diff --git a/tfhe/src/shortint/engine/server_side.rs b/tfhe/src/shortint/engine/server_side.rs index 8750d65457..e44ea2af42 100644 --- a/tfhe/src/shortint/engine/server_side.rs +++ b/tfhe/src/shortint/engine/server_side.rs @@ -193,7 +193,7 @@ impl ShortintEngine { ) -> CompressedServerKey { let bootstrapping_key = match cks.parameters.pbs_parameters().unwrap() { crate::shortint::PBSParameters::PBS(pbs_params) => { - #[cfg(not(feature = "wasm"))] + #[cfg(not(target_arch = "wasm32"))] let bootstrapping_key = par_allocate_and_generate_new_seeded_lwe_bootstrap_key( &cks.small_lwe_secret_key(), &cks.glwe_secret_key, @@ -204,7 +204,7 @@ impl ShortintEngine { &mut self.seeder, ); - #[cfg(feature = "wasm")] + #[cfg(target_arch = "wasm32")] let bootstrapping_key = allocate_and_generate_new_seeded_lwe_bootstrap_key( &cks.small_lwe_secret_key(), &cks.glwe_secret_key, @@ -218,7 +218,7 @@ impl ShortintEngine { ShortintCompressedBootstrappingKey::Classic(bootstrapping_key) } crate::shortint::PBSParameters::MultiBitPBS(pbs_params) => { - #[cfg(not(feature = "wasm"))] + #[cfg(not(target_arch = "wasm32"))] let bootstrapping_key = par_allocate_and_generate_new_seeded_lwe_multi_bit_bootstrap_key( &cks.small_lwe_secret_key(), @@ -231,7 +231,7 @@ impl ShortintEngine { &mut self.seeder, ); - #[cfg(feature = "wasm")] + #[cfg(target_arch = "wasm32")] let bootstrapping_key = allocate_and_generate_new_seeded_lwe_multi_bit_bootstrap_key( &cks.small_lwe_secret_key(), diff --git a/tfhe/src/shortint/public_key/compact.rs b/tfhe/src/shortint/public_key/compact.rs index 5b0b2f9a55..0e3e15844b 100644 --- a/tfhe/src/shortint/public_key/compact.rs +++ b/tfhe/src/shortint/public_key/compact.rs @@ -212,7 +212,7 @@ impl CompactPublicKey { ); // No parallelism allowed - #[cfg(all(feature = "wasm", not(feature = "parallel-wasm-api")))] + #[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))] { use crate::core_crypto::prelude::encrypt_lwe_compact_ciphertext_list_with_compact_public_key; ShortintEngine::with_thread_local_mut(|engine| { @@ -229,7 +229,7 @@ impl CompactPublicKey { } // Parallelism allowed - #[cfg(any(not(feature = "wasm"), feature = "parallel-wasm-api"))] + #[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))] { use crate::core_crypto::prelude::par_encrypt_lwe_compact_ciphertext_list_with_compact_public_key; ShortintEngine::with_thread_local_mut(|engine| { diff --git a/tfhe/src/shortint/public_key/standard.rs b/tfhe/src/shortint/public_key/standard.rs index 324799469e..697691a1b1 100644 --- a/tfhe/src/shortint/public_key/standard.rs +++ b/tfhe/src/shortint/public_key/standard.rs @@ -256,12 +256,12 @@ impl From for PublicKey { fn from(compressed_public_key: CompressedPublicKey) -> Self { let parameters = compressed_public_key.parameters; - #[cfg(any(not(feature = "wasm"), feature = "parallel-wasm-api"))] + #[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))] let decompressed_public_key = compressed_public_key .lwe_public_key .par_decompress_into_lwe_public_key(); - #[cfg(all(feature = "wasm", not(feature = "parallel-wasm-api")))] + #[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))] let decompressed_public_key = compressed_public_key .lwe_public_key .decompress_into_lwe_public_key();