Skip to content

Commit 6254fbe

Browse files
committed
feat(wasm): added support for wasm compilation without Javascript API
1 parent 6060882 commit 6254fbe

File tree

9 files changed

+27
-24
lines changed

9 files changed

+27
-24
lines changed

tfhe/Cargo.toml

+3
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ serde-wasm-bindgen = { version = "0.6.0", optional = true }
8181
getrandom = { version = "0.2.8", optional = true }
8282
bytemuck = "1.13.1"
8383

84+
[target.'cfg(target_arch = "wasm32")'.dependencies.getrandom]
85+
optional = false
86+
8487
[features]
8588
boolean = []
8689
shortint = []

tfhe/src/boolean/engine/bootstrapping.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ impl Bootstrapper {
336336
}
337337

338338
pub(crate) fn new_compressed_server_key(&mut self, cks: &ClientKey) -> CompressedServerKey {
339-
#[cfg(not(feature = "__wasm_api"))]
339+
#[cfg(not(target_arch = "wasm32"))]
340340
let bootstrapping_key = par_allocate_and_generate_new_seeded_lwe_bootstrap_key(
341341
&cks.lwe_secret_key,
342342
&cks.glwe_secret_key,
@@ -347,7 +347,7 @@ impl Bootstrapper {
347347
&mut self.seeder,
348348
);
349349

350-
#[cfg(feature = "__wasm_api")]
350+
#[cfg(target_arch = "wasm32")]
351351
let bootstrapping_key = allocate_and_generate_new_seeded_lwe_bootstrap_key(
352352
&cks.lwe_secret_key,
353353
&cks.glwe_secret_key,

tfhe/src/boolean/engine/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl BooleanEngine {
129129
lwe_sk.lwe_dimension().to_lwe_size().0 * LOG2_Q_32 + 128,
130130
);
131131

132-
#[cfg(not(feature = "__wasm_api"))]
132+
#[cfg(not(target_arch = "wasm32"))]
133133
let lwe_public_key: LwePublicKeyOwned<u32> = par_allocate_and_generate_new_lwe_public_key(
134134
&lwe_sk,
135135
zero_encryption_count,
@@ -138,7 +138,7 @@ impl BooleanEngine {
138138
&mut self.encryption_generator,
139139
);
140140

141-
#[cfg(feature = "__wasm_api")]
141+
#[cfg(target_arch = "wasm32")]
142142
let lwe_public_key: LwePublicKeyOwned<u32> = allocate_and_generate_new_lwe_public_key(
143143
&lwe_sk,
144144
zero_encryption_count,
@@ -172,7 +172,7 @@ impl BooleanEngine {
172172
lwe_sk.lwe_dimension().to_lwe_size().0 * LOG2_Q_32 + 128,
173173
);
174174

175-
#[cfg(not(feature = "__wasm_api"))]
175+
#[cfg(not(target_arch = "wasm32"))]
176176
let compressed_lwe_public_key = par_allocate_and_generate_new_seeded_lwe_public_key(
177177
&lwe_sk,
178178
zero_encryption_count,
@@ -181,7 +181,7 @@ impl BooleanEngine {
181181
&mut self.bootstrapper.seeder,
182182
);
183183

184-
#[cfg(feature = "__wasm_api")]
184+
#[cfg(target_arch = "wasm32")]
185185
let compressed_lwe_public_key = allocate_and_generate_new_seeded_lwe_public_key(
186186
&lwe_sk,
187187
zero_encryption_count,

tfhe/src/core_crypto/seeders.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55
//! for cryptographically secure pseudo random number generators.
66
77
pub use crate::core_crypto::commons::math::random::Seeder;
8-
#[cfg(all(target_os = "macos", not(feature = "__wasm_api")))]
8+
#[cfg(all(target_os = "macos", not(target_arch = "wasm32")))]
99
pub use concrete_csprng::seeders::AppleSecureEnclaveSeeder;
1010
#[cfg(feature = "seeder_x86_64_rdseed")]
1111
pub use concrete_csprng::seeders::RdseedSeeder;
1212
#[cfg(feature = "seeder_unix")]
1313
pub use concrete_csprng::seeders::UnixSeeder;
1414

15-
#[cfg(feature = "__wasm_api")]
15+
#[cfg(target_arch = "wasm32")]
1616
mod wasm_seeder {
1717
use crate::core_crypto::commons::math::random::{Seed, Seeder};
1818
// This is used for web interfaces
@@ -73,7 +73,7 @@ pub fn new_seeder() -> Box<dyn Seeder> {
7373

7474
let err_msg;
7575

76-
#[cfg(not(feature = "__wasm_api"))]
76+
#[cfg(not(target_arch = "wasm32"))]
7777
{
7878
#[cfg(feature = "seeder_x86_64_rdseed")]
7979
{
@@ -110,7 +110,7 @@ pub fn new_seeder() -> Box<dyn Seeder> {
110110
}
111111
}
112112

113-
#[cfg(feature = "__wasm_api")]
113+
#[cfg(target_arch = "wasm32")]
114114
{
115115
if seeder.is_none() && wasm_seeder::WasmSeeder::is_available() {
116116
seeder = Some(Box::new(wasm_seeder::WasmSeeder {}));

tfhe/src/shortint/ciphertext/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -600,14 +600,14 @@ impl CompactCiphertextList {
600600
);
601601

602602
// No parallelism allowed
603-
#[cfg(all(feature = "__wasm_api", not(feature = "parallel-wasm-api")))]
603+
#[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))]
604604
{
605605
use crate::core_crypto::prelude::expand_lwe_compact_ciphertext_list;
606606
expand_lwe_compact_ciphertext_list(&mut output_lwe_ciphertext_list, &self.ct_list);
607607
}
608608

609609
// Parallelism allowed
610-
#[cfg(any(not(feature = "__wasm_api"), feature = "parallel-wasm-api"))]
610+
#[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))]
611611
{
612612
use crate::core_crypto::prelude::par_expand_lwe_compact_ciphertext_list;
613613
par_expand_lwe_compact_ciphertext_list(&mut output_lwe_ciphertext_list, &self.ct_list);

tfhe/src/shortint/engine/public_side.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl ShortintEngine {
3939
secret_encryption_key.lwe_dimension().to_lwe_size(),
4040
);
4141

42-
#[cfg(any(not(feature = "__wasm_api"), feature = "parallel-wasm-api"))]
42+
#[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))]
4343
let lwe_public_key = par_allocate_and_generate_new_lwe_public_key(
4444
&secret_encryption_key,
4545
zero_encryption_count,
@@ -48,7 +48,7 @@ impl ShortintEngine {
4848
&mut self.encryption_generator,
4949
);
5050

51-
#[cfg(all(feature = "__wasm_api", not(feature = "parallel-wasm-api")))]
51+
#[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))]
5252
let lwe_public_key = allocate_and_generate_new_lwe_public_key(
5353
&secret_encryption_key,
5454
zero_encryption_count,
@@ -85,7 +85,7 @@ impl ShortintEngine {
8585
secret_encryption_key.lwe_dimension().to_lwe_size(),
8686
);
8787

88-
#[cfg(any(not(feature = "__wasm_api"), feature = "parallel-wasm-api"))]
88+
#[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))]
8989
let compressed_public_key = par_allocate_and_generate_new_seeded_lwe_public_key(
9090
&secret_encryption_key,
9191
zero_encryption_count,
@@ -94,7 +94,7 @@ impl ShortintEngine {
9494
&mut self.seeder,
9595
);
9696

97-
#[cfg(all(feature = "__wasm_api", not(feature = "parallel-wasm-api")))]
97+
#[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))]
9898
let compressed_public_key = allocate_and_generate_new_seeded_lwe_public_key(
9999
&secret_encryption_key,
100100
zero_encryption_count,

tfhe/src/shortint/engine/server_side.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ impl ShortintEngine {
193193
) -> CompressedServerKey {
194194
let bootstrapping_key = match cks.parameters.pbs_parameters().unwrap() {
195195
crate::shortint::PBSParameters::PBS(pbs_params) => {
196-
#[cfg(not(feature = "__wasm_api"))]
196+
#[cfg(not(target_arch = "wasm32"))]
197197
let bootstrapping_key = par_allocate_and_generate_new_seeded_lwe_bootstrap_key(
198198
&cks.small_lwe_secret_key(),
199199
&cks.glwe_secret_key,
@@ -204,7 +204,7 @@ impl ShortintEngine {
204204
&mut self.seeder,
205205
);
206206

207-
#[cfg(feature = "__wasm_api")]
207+
#[cfg(target_arch = "wasm32")]
208208
let bootstrapping_key = allocate_and_generate_new_seeded_lwe_bootstrap_key(
209209
&cks.small_lwe_secret_key(),
210210
&cks.glwe_secret_key,
@@ -218,7 +218,7 @@ impl ShortintEngine {
218218
ShortintCompressedBootstrappingKey::Classic(bootstrapping_key)
219219
}
220220
crate::shortint::PBSParameters::MultiBitPBS(pbs_params) => {
221-
#[cfg(not(feature = "__wasm_api"))]
221+
#[cfg(not(target_arch = "wasm32"))]
222222
let bootstrapping_key =
223223
par_allocate_and_generate_new_seeded_lwe_multi_bit_bootstrap_key(
224224
&cks.small_lwe_secret_key(),
@@ -231,7 +231,7 @@ impl ShortintEngine {
231231
&mut self.seeder,
232232
);
233233

234-
#[cfg(feature = "__wasm_api")]
234+
#[cfg(target_arch = "wasm32")]
235235
let bootstrapping_key =
236236
allocate_and_generate_new_seeded_lwe_multi_bit_bootstrap_key(
237237
&cks.small_lwe_secret_key(),

tfhe/src/shortint/public_key/compact.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ impl CompactPublicKey {
212212
);
213213

214214
// No parallelism allowed
215-
#[cfg(all(feature = "__wasm_api", not(feature = "parallel-wasm-api")))]
215+
#[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))]
216216
{
217217
use crate::core_crypto::prelude::encrypt_lwe_compact_ciphertext_list_with_compact_public_key;
218218
ShortintEngine::with_thread_local_mut(|engine| {
@@ -229,7 +229,7 @@ impl CompactPublicKey {
229229
}
230230

231231
// Parallelism allowed
232-
#[cfg(any(not(feature = "__wasm_api"), feature = "parallel-wasm-api"))]
232+
#[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))]
233233
{
234234
use crate::core_crypto::prelude::par_encrypt_lwe_compact_ciphertext_list_with_compact_public_key;
235235
ShortintEngine::with_thread_local_mut(|engine| {

tfhe/src/shortint/public_key/standard.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -256,12 +256,12 @@ impl From<CompressedPublicKey> for PublicKey {
256256
fn from(compressed_public_key: CompressedPublicKey) -> Self {
257257
let parameters = compressed_public_key.parameters;
258258

259-
#[cfg(any(not(feature = "__wasm_api"), feature = "parallel-wasm-api"))]
259+
#[cfg(any(not(target_arch = "wasm32"), feature = "parallel-wasm-api"))]
260260
let decompressed_public_key = compressed_public_key
261261
.lwe_public_key
262262
.par_decompress_into_lwe_public_key();
263263

264-
#[cfg(all(feature = "__wasm_api", not(feature = "parallel-wasm-api")))]
264+
#[cfg(all(target_arch = "wasm32", not(feature = "parallel-wasm-api")))]
265265
let decompressed_public_key = compressed_public_key
266266
.lwe_public_key
267267
.decompress_into_lwe_public_key();

0 commit comments

Comments
 (0)