Skip to content

Commit

Permalink
changed wasm32 support from feature-based to target-based compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroMBB committed Jan 23, 2024
1 parent d8ea9af commit 5497a95
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 28 deletions.
5 changes: 1 addition & 4 deletions tfhe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/boolean/engine/bootstrapping.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tfhe/src/boolean/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<u32> = par_allocate_and_generate_new_lwe_public_key(
&lwe_sk,
zero_encryption_count,
Expand All @@ -138,7 +138,7 @@ impl BooleanEngine {
&mut self.encryption_generator,
);

#[cfg(feature = "wasm")]
#[cfg(target_arch = "wasm32")]
let lwe_public_key: LwePublicKeyOwned<u32> = allocate_and_generate_new_lwe_public_key(
&lwe_sk,
zero_encryption_count,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tfhe/src/core_crypto/seeders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn new_seeder() -> Box<dyn Seeder> {

let err_msg;

#[cfg(not(feature = "wasm"))]
#[cfg(not(target_arch = "wasm32"))]
{
#[cfg(feature = "seeder_x86_64_rdseed")]
{
Expand Down Expand Up @@ -110,7 +110,7 @@ pub fn new_seeder() -> Box<dyn Seeder> {
}
}

#[cfg(feature = "wasm")]
#[cfg(target_arch = "wasm32")]
{
if seeder.is_none() && wasm_seeder::WasmSeeder::is_available() {
seeder = Some(Box::new(wasm_seeder::WasmSeeder {}));
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/shortint/ciphertext/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions tfhe/src/shortint/engine/public_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
8 changes: 4 additions & 4 deletions tfhe/src/shortint/engine/server_side.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/shortint/public_key/compact.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand All @@ -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| {
Expand Down
4 changes: 2 additions & 2 deletions tfhe/src/shortint/public_key/standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -256,12 +256,12 @@ impl From<CompressedPublicKey> 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();
Expand Down

0 comments on commit 5497a95

Please sign in to comment.