Skip to content

Commit

Permalink
docs(js): update JS example
Browse files Browse the repository at this point in the history
The example was still using CompactFheUint32List
which as been removed in favor of the more generic CompactCiphertextList
  • Loading branch information
tmontaigu committed Feb 27, 2025
1 parent 8905c01 commit 4cee2f3
Showing 1 changed file with 46 additions and 35 deletions.
81 changes: 46 additions & 35 deletions tfhe/docs/integration/js_on_wasm_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,56 @@ The core of the API remains the same, requiring only minor changes in the initia
Example:

```javascript

const {
init_panic_hook,
ShortintParametersName,
ShortintParameters,
TfheClientKey,
TfheCompactPublicKey,
TfheCompressedServerKey,
TfheConfigBuilder,
CompactFheUint32List
} = require("./pkg/tfhe.js");
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 block_params = new ShortintParameters(ShortintParametersName.V1_0_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 compact_list = CompactFheUint32List.encrypt_with_compact_public_key(values, publicKey);

let serialized_list = compact_list.serialize();
let deserialized_list = CompactFheUint32List.deserialize(serialized_list);
let encrypted_list = deserialized_list.expand();
assert.deepStrictEqual(encrypted_list.length, values.length);

for (let i = 0; i < values.length; i++)
{
let decrypted = encrypted_list[i].decrypt(clientKey);
assert.deepStrictEqual(decrypted, values[i]);
}
// 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_0_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
Expand Down

0 comments on commit 4cee2f3

Please sign in to comment.