Skip to content

Commit

Permalink
feat(tfhe): allow unpacking packed compact ciphertext lists in js/wasm
Browse files Browse the repository at this point in the history
  • Loading branch information
IceTDrinker committed Sep 4, 2024
1 parent 15e3474 commit 78b0e83
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 11 deletions.
2 changes: 1 addition & 1 deletion tfhe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ lazy_static = { version = "1.4.0", optional = true }
serde = { version = "1.0", features = ["derive"] }
rayon = { version = "1.5.0" }
bincode = "1.3.3"
concrete-fft = { version = "0.5.0", features = ["serde", "fft128"] }
concrete-fft = { version = "0.5.1", features = ["serde", "fft128"] }
concrete-ntt = { version = "0.2.0" }
pulp = "0.18.22"
tfhe-cuda-backend = { version = "0.4.0-alpha.0", path = "../backends/tfhe-cuda-backend", optional = true }
Expand Down
2 changes: 1 addition & 1 deletion tfhe/js_on_wasm_tests/test-hlapi-signed.js
Original file line number Diff line number Diff line change
Expand Up @@ -520,5 +520,5 @@ test('hlapi_compact_ciphertext_list_with_proof', (t) => {
assert.deepStrictEqual(deserialized.get_kind_of(2), FheTypes.Bool);
assert.deepStrictEqual(deserialized.get_kind_of(3), FheTypes.Uint256);

// We cannot verify packed ZK in wasm
// Verifying and expanding is too slow for single threaded node tests.
});
19 changes: 19 additions & 0 deletions tfhe/src/js_on_wasm_api/js_high_level_api/keys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,25 @@ impl TfheCompressedServerKey {
}
}

#[wasm_bindgen]
pub struct TfheServerKey(pub(crate) hlapi::ServerKey);

#[wasm_bindgen]
impl TfheServerKey {
#[wasm_bindgen]
pub fn new(client_key: &TfheClientKey) -> Result<TfheServerKey, JsError> {
catch_panic_result(|| Ok(Self(hlapi::ServerKey::new(&client_key.0))))
}

#[wasm_bindgen]
pub fn set_as_server_key(&self) -> Result<(), JsError> {
catch_panic_result(|| {
crate::set_server_key(self.0.clone());
Ok(())
})
}
}

#[wasm_bindgen]
pub struct TfhePublicKey(pub(crate) hlapi::PublicKey);

Expand Down
63 changes: 54 additions & 9 deletions tfhe/web_wasm_parallel_tests/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import init, {
ShortintParameters,
TfheClientKey,
TfhePublicKey,
TfheServerKey,
TfheCompressedPublicKey,
TfheCompressedServerKey,
TfheCompressedCompactPublicKey,
Expand Down Expand Up @@ -190,6 +191,12 @@ async function compressedCompactPublicKeyTest256BitOnConfig(config) {
let clientKey = TfheClientKey.generate(config);
console.timeEnd("ClientKey Gen");

console.time("SK Gen");
let serverKey = TfheServerKey.new(clientKey);
console.timeEnd("SK Gen");

serverKey.set_as_server_key();

console.time("CompressedCompactPublicKey Gen");
let publicKey = TfheCompressedCompactPublicKey.new(clientKey);
console.timeEnd("CompressedCompactPublicKey Gen");
Expand Down Expand Up @@ -219,7 +226,7 @@ async function compressedCompactPublicKeyTest256BitOnConfig(config) {
console.log("Numb bits in compact list: ", num_bits_encrypted);

console.time("CompactCiphertextList Encrypt");
let list = builder.build();
let list = builder.build_packed();
console.timeEnd("CompactCiphertextList Encrypt");

let serialized = list.safe_serialize(BigInt(10000000));
Expand All @@ -229,7 +236,9 @@ async function compressedCompactPublicKeyTest256BitOnConfig(config) {
BigInt(10000000),
);

console.time("Expand");
let expander = deserialized.expand();
console.timeEnd("Expand");

assert_eq(expander.get_uint2(0).decrypt(clientKey), clear_u2);

Expand All @@ -254,8 +263,11 @@ async function compactPublicKeyWithCastingTest256Bit() {
.build();

let clientKey = TfheClientKey.generate(config);
let serverKey = TfheServerKey.new(clientKey);
let publicKey = TfheCompactPublicKey.new(clientKey);

serverKey.set_as_server_key();

let clear_u2 = 3;
let clear_i32 = -3284;
let clear_bool = true;
Expand All @@ -281,7 +293,15 @@ async function compactPublicKeyWithCastingTest256Bit() {
BigInt(10000000),
);

// Cannot expand
let expander = deserialized.expand();

assert_eq(expander.get_uint2(0).decrypt(clientKey), clear_u2);

assert_eq(expander.get_int32(1).decrypt(clientKey), clear_i32);

assert_eq(expander.get_bool(2).decrypt(clientKey), clear_bool);

assert_eq(expander.get_uint256(3).decrypt(clientKey), clear_u256);
}

async function compressedCompactPublicKeyWithCastingTest256Bit() {
Expand All @@ -298,9 +318,12 @@ async function compressedCompactPublicKeyWithCastingTest256Bit() {
.build();

let clientKey = TfheClientKey.generate(config);
let serverKey = TfheServerKey.new(clientKey);
let compressedPublicKey = TfheCompressedCompactPublicKey.new(clientKey);
let publicKey = compressedPublicKey.decompress();

serverKey.set_as_server_key();

let clear_u2 = 3;
let clear_i32 = -3284;
let clear_bool = true;
Expand All @@ -326,7 +349,15 @@ async function compressedCompactPublicKeyWithCastingTest256Bit() {
BigInt(10000000),
);

// Cannot expand
let expander = deserialized.expand();

assert_eq(expander.get_uint2(0).decrypt(clientKey), clear_u2);

assert_eq(expander.get_int32(1).decrypt(clientKey), clear_i32);

assert_eq(expander.get_bool(2).decrypt(clientKey), clear_bool);

assert_eq(expander.get_uint256(3).decrypt(clientKey), clear_u256);
}

async function compactPublicKeyZeroKnowledge() {
Expand All @@ -343,8 +374,11 @@ async function compactPublicKeyZeroKnowledge() {
.build();

let clientKey = TfheClientKey.generate(config);
let serverKey = TfheServerKey.new(clientKey);
let publicKey = TfheCompactPublicKey.new(clientKey);

serverKey.set_as_server_key();

console.log("Start CRS generation");
console.time("CRS generation");
let crs = CompactPkeCrs.from_config(config, 4 * 64);
Expand All @@ -368,10 +402,13 @@ async function compactPublicKeyZeroKnowledge() {
" ms",
);

let bytes = list.serialize();
console.log("CompactCiphertextList size:", bytes.length);
let serialized = list.serialize();
console.log("CompactCiphertextList size:", serialized.length);
let deserialized = ProvenCompactCiphertextList.deserialize(serialized);

let expander = deserialized.verify_and_expand(public_params, publicKey);

// We cannot expand a packed list in WASM
assert_eq(expander.get_uint64(0).decrypt(clientKey), input);
}

{
Expand All @@ -397,13 +434,21 @@ async function compactPublicKeyZeroKnowledge() {
" ms",
);

// We cannot expand a packed list in WASM
let expander = encrypted.verify_and_expand(public_params, publicKey);

assert_eq(expander.get_uint64(0).decrypt(clientKey), inputs[0]);

assert_eq(expander.get_uint64(1).decrypt(clientKey), inputs[1]);

assert_eq(expander.get_uint64(2).decrypt(clientKey), inputs[2]);

assert_eq(expander.get_uint64(3).decrypt(clientKey), inputs[3]);
}
}

async function compressedCompactPublicKeyTest256BitBig() {
const block_params = new ShortintParameters(
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS,
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
);
let config = TfheConfigBuilder.default()
.use_custom_parameters(block_params)
Expand All @@ -413,7 +458,7 @@ async function compressedCompactPublicKeyTest256BitBig() {

async function compressedCompactPublicKeyTest256BitSmall() {
const block_params = new ShortintParameters(
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS,
ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64,
);
let config = TfheConfigBuilder.default()
.use_custom_parameters(block_params)
Expand Down

0 comments on commit 78b0e83

Please sign in to comment.