From 6254fbec680568817ee3a42976a5f07a7ba577d3 Mon Sep 17 00:00:00 2001 From: Pedro Barbosa <1180592@isep.ipp.pt> Date: Wed, 17 Jan 2024 17:09:03 +0000 Subject: [PATCH] feat(wasm): added support for wasm compilation without Javascript API --- tfhe/Cargo.toml | 3 +++ 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, 27 insertions(+), 24 deletions(-) diff --git a/tfhe/Cargo.toml b/tfhe/Cargo.toml index f1b0c8956c..64cd94921f 100644 --- a/tfhe/Cargo.toml +++ b/tfhe/Cargo.toml @@ -81,6 +81,9 @@ serde-wasm-bindgen = { version = "0.6.0", optional = true } getrandom = { version = "0.2.8", optional = true } bytemuck = "1.13.1" +[target.'cfg(target_arch = "wasm32")'.dependencies.getrandom] +optional = false + [features] boolean = [] shortint = [] diff --git a/tfhe/src/boolean/engine/bootstrapping.rs b/tfhe/src/boolean/engine/bootstrapping.rs index 77947e8ee6..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_api"))] + #[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_api")] + #[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 3ef5b8f9bb..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_api"))] + #[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_api")] + #[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_api"))] + #[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_api")] + #[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 9f07e413ac..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_api")))] +#[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_api")] +#[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_api"))] + #[cfg(not(target_arch = "wasm32"))] { #[cfg(feature = "seeder_x86_64_rdseed")] { @@ -110,7 +110,7 @@ pub fn new_seeder() -> Box { } } - #[cfg(feature = "__wasm_api")] + #[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 e3d99e1f4f..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_api", 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_api"), 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 19e3b58c74..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_api"), 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_api", 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_api"), 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_api", 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 d99cda8b83..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_api"))] + #[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_api")] + #[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_api"))] + #[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_api")] + #[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 1e0377aba9..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_api", 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_api"), 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 d5d229ee06..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_api"), 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_api", 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();