JS on WASM API
This document outlines how to use the TFHE-rs WebAssembly (WASM) client API for key generation, encryption, and decryption, providing setup examples for Node.js and web browsers.
TFHE-rs supports WASM client API, which includes functionality for key generation, encryption, and decryption. However, it does not support FHE computations.
TFHE-rs supports 3 WASM targets:
Node.js: For use in Node.js applications or packages
Web: For use in web browsers
Web-parallel: For use in web browsers with multi-threading support
The core of the API remains the same, requiring only minor changes in the initialization functions.
Node.js
Example:
const {
init_panic_hook,
ShortintParametersName,
ShortintParameters,
TfheClientKey,
TfheCompactPublicKey,
TfheCompressedServerKey,
TfheConfigBuilder,
CompactCiphertextList
} = require("/path/to/built/pkg/tfhe.js");
const assert = require("node:assert").strict;
function fhe_uint32_example() {
// Makes it so that if a rust thread panics,
// the error message will be displayed in the console
init_panic_hook();
const U32_MAX = 4294967295;
const block_params = new ShortintParameters(ShortintParametersName.V1_4_PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS_GAUSSIAN_2M64);
let config = TfheConfigBuilder.default()
.build();
let clientKey = TfheClientKey.generate(config);
let compressedServerKey = TfheCompressedServerKey.new(clientKey);
let publicKey = TfheCompactPublicKey.new(clientKey);
let values = [0, 1, 2394, U32_MAX];
let builder = CompactCiphertextList.builder(publicKey);
for (let i = 0; i < values.length; i++) {
builder.push_u32(values[i]);
}
let compact_list = builder.build();
let serialized_list = compact_list.serialize();
let deserialized_list = CompactCiphertextList.deserialize(serialized_list);
let encrypted_list = deserialized_list.expand();
assert.deepStrictEqual(encrypted_list.len(), values.length);
for (let i = 0; i < values.length; i++)
{
let decrypted = encrypted_list.get_uint32(i).decrypt(clientKey);
assert.deepStrictEqual(decrypted, values[i]);
}
}
fhe_uint32_example();
Web
When using the Web WASM target, you should call an additional init function. With parallelism enabled, you need to call another additional initThreadPool function.
Example:
Compiling the WASM API
Use the provided Makefile in the TFHE-rs repository to compile for the desired target:
make build_node_js_apifor the Node.js APImake build_web_js_apifor the browser APImake build_web_js_api_parallelfor the browser API with parallelism
The compiled WASM packages are located in tfhe/pkg.
Extra steps for web bundlers
When using the browser API with parallelism, some extra step might be needed depending on the bundler used:
Usage with Webpack
If you're using Webpack v5 (version >= 5.25.1), you don't need to do anything special, as it already supports bundling Workers out of the box.
Usage with Parcel
Parcel v2 also recognises the used syntax and works out of the box.
Usage with Rollup
For Rollup, you'll need @surma/rollup-plugin-off-main-thread plugin (version >= 2.1.0) which brings the same functionality and was tested with this crate.
Alternatively, you can use Vite which has necessary plugins built-in.
(Taken from RReverser/wasm-bindgen-rayon)
Using the JS on WASM API
TFHE-rs uses WASM to provide a JavaScript (JS) binding to the client-side primitives, like key generation and encryption within the Boolean and shortint modules.
Currently, there are several limitations. Due to a lack of threading support in WASM, key generation can be too slow to be practical for bigger parameter sets.
Some parameter sets lead to the FHE keys exceeding the 2GB memory limit of WASM, making these parameter sets virtually unusable.
First steps using TFHE-rs JS on WASM API
Setting up TFHE-rs JS on WASM API for Node.js programs.
To build the JS on WASM bindings for TFHE-rs, install wasm-pack and the necessary rust toolchain. Cone the TFHE-rs repository and build using the following commands (this will build using the default branch, you can check out a specific tag depending on your requirements):
The command above targets Node.js. To generate a binding for a web browser, use --target=web. However, this tutorial does not cover that particular use case.
Both Boolean and shortint features are enabled here, but it's possible to use them individually.
After the build, a new directory pkg is available in the tfhe directory.
Commented code to generate keys for shortint and encrypt a ciphertext
Then, you can run the example.js script using node as follows:
Last updated
Was this helpful?