Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(wasm): add support for wasm compilation without Javascript API #783

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions tfhe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
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_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,
Expand All @@ -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,
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_api"))]
#[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_api")]
#[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_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,
Expand All @@ -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,
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_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
Expand Down Expand Up @@ -73,7 +73,7 @@ pub fn new_seeder() -> Box<dyn Seeder> {

let err_msg;

#[cfg(not(feature = "__wasm_api"))]
#[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_api")]
#[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_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);
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_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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
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_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,
Expand All @@ -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,
Expand All @@ -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(),
Expand All @@ -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(),
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_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| {
Expand All @@ -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| {
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_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();
Expand Down
Loading