JS on WASM API
Node.js
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_5_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
Compiling the WASM API
Extra steps for web bundlers
Usage with Webpack
Usage with Parcel
Usage with Rollup
Using the JS on WASM API
First steps using TFHE-rs JS on WASM API
Setting up TFHE-rs JS on WASM API for Node.js programs.
Commented code to generate keys for shortint and encrypt a ciphertext
Last updated
Was this helpful?