From 79ee4b476212135b7db4f0b54382b0b7da6a274e Mon Sep 17 00:00:00 2001 From: Nicolas Sarlin Date: Thu, 9 Jan 2025 14:43:41 +0100 Subject: [PATCH] chore: formatting uniformization for c and js --- Makefile | 6 +- tfhe/c_api_tests/test_high_level_zk.c | 2 +- tfhe/js_on_wasm_tests/.prettierignore | 4 + tfhe/js_on_wasm_tests/Makefile | 9 + tfhe/js_on_wasm_tests/package.json | 7 +- tfhe/js_on_wasm_tests/test-hlapi-signed.js | 1106 ++++++++++-------- tfhe/js_on_wasm_tests/test-hlapi-unsigned.js | 764 ++++++------ tfhe/js_on_wasm_tests/test.js | 385 +++--- tfhe/web_wasm_parallel_tests/Makefile | 2 +- 9 files changed, 1248 insertions(+), 1037 deletions(-) create mode 100644 tfhe/js_on_wasm_tests/.prettierignore create mode 100644 tfhe/js_on_wasm_tests/Makefile diff --git a/Makefile b/Makefile index 7d6a3abb05..6edfa85089 100644 --- a/Makefile +++ b/Makefile @@ -243,7 +243,8 @@ fmt_js: check_nvm_installed source ~/.nvm/nvm.sh && \ nvm install $(NODE_VERSION) && \ nvm use $(NODE_VERSION) && \ - $(MAKE) -C tfhe/web_wasm_parallel_tests fmt + $(MAKE) -C tfhe/web_wasm_parallel_tests fmt && \ + $(MAKE) -C tfhe/js_on_wasm_tests fmt .PHONY: fmt_gpu # Format rust and cuda code fmt_gpu: install_rs_check_toolchain @@ -272,7 +273,8 @@ check_fmt_js: check_nvm_installed source ~/.nvm/nvm.sh && \ nvm install $(NODE_VERSION) && \ nvm use $(NODE_VERSION) && \ - $(MAKE) -C tfhe/web_wasm_parallel_tests check_fmt + $(MAKE) -C tfhe/web_wasm_parallel_tests check_fmt && \ + $(MAKE) -C tfhe/js_on_wasm_tests check_fmt .PHONY: check_typos # Check for typos in codebase check_typos: install_typos_checker diff --git a/tfhe/c_api_tests/test_high_level_zk.c b/tfhe/c_api_tests/test_high_level_zk.c index 093dfbf48d..2d13551638 100644 --- a/tfhe/c_api_tests/test_high_level_zk.c +++ b/tfhe/c_api_tests/test_high_level_zk.c @@ -11,7 +11,7 @@ int main(void) { ShortintPBSParameters params = SHORTINT_PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; assert(params.encryption_key_choice == ShortintEncryptionKeyChoiceBig); - ShortintCompactPublicKeyEncryptionParameters pke_params = SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; + ShortintCompactPublicKeyEncryptionParameters pke_params = SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; int status; diff --git a/tfhe/js_on_wasm_tests/.prettierignore b/tfhe/js_on_wasm_tests/.prettierignore new file mode 100644 index 0000000000..a3797fd203 --- /dev/null +++ b/tfhe/js_on_wasm_tests/.prettierignore @@ -0,0 +1,4 @@ +dist/ +node_modules/ +pkg/ +test/benchmark_results/ diff --git a/tfhe/js_on_wasm_tests/Makefile b/tfhe/js_on_wasm_tests/Makefile new file mode 100644 index 0000000000..4e01c508cb --- /dev/null +++ b/tfhe/js_on_wasm_tests/Makefile @@ -0,0 +1,9 @@ +.PHONY: fmt # Format Javascript code +fmt: + npm install + npm run format + +.PHONY: check_fmt # Check Javascript code format +check_fmt: + npm install + npm run check-format diff --git a/tfhe/js_on_wasm_tests/package.json b/tfhe/js_on_wasm_tests/package.json index 350cb87754..4b4f6ef5d9 100644 --- a/tfhe/js_on_wasm_tests/package.json +++ b/tfhe/js_on_wasm_tests/package.json @@ -4,7 +4,12 @@ "description": "JS API tests", "main": "index.js", "scripts": { - "test": "node --test --test-reporter=tap" + "test": "node --test --test-reporter=tap", + "format": "prettier . --write", + "check-format": "prettier . --check" + }, + "devDependencies": { + "prettier": "^3.3.3" }, "author": "", "license": "BSD-3-Clause-Clear" diff --git a/tfhe/js_on_wasm_tests/test-hlapi-signed.js b/tfhe/js_on_wasm_tests/test-hlapi-signed.js index 8efd054349..3239bc1e47 100644 --- a/tfhe/js_on_wasm_tests/test-hlapi-signed.js +++ b/tfhe/js_on_wasm_tests/test-hlapi-signed.js @@ -1,604 +1,682 @@ -const test = require('node:test'); -const assert = require('node:assert').strict; -const { performance } = require('perf_hooks'); +const test = require("node:test"); +const assert = require("node:assert").strict; +const { performance } = require("perf_hooks"); const { - init_panic_hook, - ShortintParametersName, - ShortintParameters, - TfheClientKey, - TfhePublicKey, - TfheCompressedPublicKey, - TfheCompressedCompactPublicKey, - ShortintCompactPublicKeyEncryptionParametersName, - ShortintCompactPublicKeyEncryptionParameters, - TfheCompactPublicKey, - TfheConfigBuilder, - CompressedFheInt8, - FheInt8, - FheInt32, - FheTypes, - CompressedFheInt128, - FheInt128, - CompressedFheInt256, - FheInt256, - CompactCiphertextList, - ProvenCompactCiphertextList, - CompactPkeCrs, - ZkComputeLoad, - Shortint, - ShortintEncryptionKeyChoice, + init_panic_hook, + ShortintParametersName, + ShortintParameters, + TfheClientKey, + TfhePublicKey, + TfheCompressedPublicKey, + TfheCompressedCompactPublicKey, + ShortintCompactPublicKeyEncryptionParametersName, + ShortintCompactPublicKeyEncryptionParameters, + TfheCompactPublicKey, + TfheConfigBuilder, + CompressedFheInt8, + FheInt8, + FheInt32, + FheTypes, + CompressedFheInt128, + FheInt128, + CompressedFheInt256, + FheInt256, + CompactCiphertextList, + ProvenCompactCiphertextList, + CompactPkeCrs, + ZkComputeLoad, + Shortint, + ShortintEncryptionKeyChoice, } = require("../pkg/tfhe.js"); -const { - randomBytes, -} = require('node:crypto'); - -const I256_MIN = BigInt("-57896044618658097711785492504343953926634992332820282019728792003956564819968"); -const I256_MAX = BigInt("28948022309329048855892746252171976963317496166410141009864396001978282409983"); +const { randomBytes } = require("node:crypto"); + +const I256_MIN = BigInt( + "-57896044618658097711785492504343953926634992332820282019728792003956564819968", +); +const I256_MAX = BigInt( + "28948022309329048855892746252171976963317496166410141009864396001978282409983", +); const I128_MIN = BigInt("-170141183460469231731687303715884105728"); const I32_MIN = -2147483648; // This is useful to debug test init_panic_hook(); +test("hlapi_client_key_encrypt_decrypt_int8_big", (t) => { + let config = TfheConfigBuilder.default().build(); -test('hlapi_client_key_encrypt_decrypt_int8_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); + let clientKey = TfheClientKey.generate(config); - let clientKey = TfheClientKey.generate(config); - - let clear = -73; - let encrypted = FheInt8.encrypt_with_client_key(clear, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, clear); + let clear = -73; + let encrypted = FheInt8.encrypt_with_client_key(clear, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, clear); - let serialized = encrypted.serialize(); - let deserialized = FheInt8.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, clear); + let serialized = encrypted.serialize(); + let deserialized = FheInt8.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, clear); - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt8.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, clear); + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheInt8.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, clear); }); -test('hlapi_compressed_public_client_int8_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); - - let clientKey = TfheClientKey.generate(config); - - let clear = -128; - let compressed_encrypted = CompressedFheInt8.encrypt_with_client_key(clear, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheInt8.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() - - let decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, clear); - - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheInt8.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() - - let safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, clear); +test("hlapi_compressed_public_client_int8_big", (t) => { + let config = TfheConfigBuilder.default().build(); + + let clientKey = TfheClientKey.generate(config); + + let clear = -128; + let compressed_encrypted = CompressedFheInt8.encrypt_with_client_key( + clear, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheInt8.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + let decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, clear); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheInt8.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + let safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, clear); }); -test('hlapi_public_key_encrypt_decrypt_int32_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - let publicKey = TfhePublicKey.new(clientKey); - - let encrypted = FheInt32.encrypt_with_public_key(I32_MIN, publicKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I32_MIN); - - let serialized = encrypted.serialize(); - let deserialized = FheInt32.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, I32_MIN); - - let safe_serialized = encrypted.safe_serialize(BigInt(1000000)); - let safe_deserialized = FheInt32.safe_deserialize(safe_serialized, BigInt(1000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, I32_MIN); +test("hlapi_public_key_encrypt_decrypt_int32_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + let publicKey = TfhePublicKey.new(clientKey); + + let encrypted = FheInt32.encrypt_with_public_key(I32_MIN, publicKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I32_MIN); + + let serialized = encrypted.serialize(); + let deserialized = FheInt32.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, I32_MIN); + + let safe_serialized = encrypted.safe_serialize(BigInt(1000000)); + let safe_deserialized = FheInt32.safe_deserialize( + safe_serialized, + BigInt(1000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, I32_MIN); }); -test('hlapi_decompress_public_key_then_encrypt_decrypt_int32_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - - let clientKey = TfheClientKey.generate(config); - var startTime = performance.now() - let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); - var endTime = performance.now() - - let data = compressedPublicKey.serialize() - - let publicKey = compressedPublicKey.decompress(); - - - var startTime = performance.now() - let encrypted = FheInt32.encrypt_with_public_key(I32_MIN, publicKey); - var endTime = performance.now() - - let ser = encrypted.serialize(); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I32_MIN); +test("hlapi_decompress_public_key_then_encrypt_decrypt_int32_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + var startTime = performance.now(); + let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); + var endTime = performance.now(); + + let data = compressedPublicKey.serialize(); + + let publicKey = compressedPublicKey.decompress(); + + var startTime = performance.now(); + let encrypted = FheInt32.encrypt_with_public_key(I32_MIN, publicKey); + var endTime = performance.now(); + + let ser = encrypted.serialize(); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I32_MIN); + + let serialized = encrypted.serialize(); + let deserialized = FheInt32.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, I32_MIN); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheInt32.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, I32_MIN); +}); - let serialized = encrypted.serialize(); - let deserialized = FheInt32.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, I32_MIN); +test("hlapi_client_key_encrypt_decrypt_int128_big", (t) => { + let config = TfheConfigBuilder.default().build(); + + let clientKey = TfheClientKey.generate(config); + + let encrypted = FheInt128.encrypt_with_client_key(I128_MIN, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I128_MIN); + + let serialized = encrypted.serialize(); + let deserialized = FheInt128.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, I128_MIN); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheInt128.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, I128_MIN); + + // Compressed + let compressed_encrypted = CompressedFheInt128.encrypt_with_client_key( + I128_MIN, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheInt128.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I128_MIN); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheInt128.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, I128_MIN); +}); - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt32.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, I32_MIN); +test("hlapi_client_key_encrypt_decrypt_int128_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + + let encrypted = FheInt128.encrypt_with_client_key(I128_MIN, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I128_MIN); + + let serialized = encrypted.serialize(); + let deserialized = FheInt128.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, I128_MIN); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheInt128.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, I128_MIN); + + // Compressed + let compressed_encrypted = CompressedFheInt128.encrypt_with_client_key( + I128_MIN, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheInt128.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I128_MIN); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheInt128.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, I128_MIN); }); -test('hlapi_client_key_encrypt_decrypt_int128_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); +test("hlapi_client_key_encrypt_decrypt_int256_big", (t) => { + let config = TfheConfigBuilder.default().build(); - let clientKey = TfheClientKey.generate(config); + let clientKey = TfheClientKey.generate(config); - let encrypted = FheInt128.encrypt_with_client_key(I128_MIN, clientKey); + let round_trip_encrypt = (value) => { + let encrypted = FheInt256.encrypt_with_client_key(value, clientKey); let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I128_MIN); + assert.deepStrictEqual(decrypted, value); let serialized = encrypted.serialize(); - let deserialized = FheInt128.deserialize(serialized); + let deserialized = FheInt256.deserialize(serialized); let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, I128_MIN); + assert.deepStrictEqual(deserialized_decrypted, value); let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt128.safe_deserialize(safe_serialized, BigInt(10000000)); + let safe_deserialized = FheInt256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, I128_MIN); + assert.deepStrictEqual(safe_deserialized_decrypted, value); // Compressed - let compressed_encrypted = CompressedFheInt128.encrypt_with_client_key(I128_MIN, clientKey); + let compressed_encrypted = CompressedFheInt256.encrypt_with_client_key( + value, + clientKey, + ); let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheInt128.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() + let compressed_deserialized = CompressedFheInt256.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I128_MIN); + assert.deepStrictEqual(decrypted, value); - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheInt128.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheInt256.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, I128_MIN); + assert.deepStrictEqual(safe_decrypted, value); + }; + + round_trip_encrypt(I256_MIN); + round_trip_encrypt(I256_MAX); + round_trip_encrypt(BigInt(-1)); + round_trip_encrypt(BigInt(1)); + round_trip_encrypt(BigInt(-128)); + round_trip_encrypt(BigInt(128)); }); -test('hlapi_client_key_encrypt_decrypt_int128_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); +test("hlapi_client_key_encrypt_decrypt_int256_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); - let clientKey = TfheClientKey.generate(config); + let clientKey = TfheClientKey.generate(config); - let encrypted = FheInt128.encrypt_with_client_key(I128_MIN, clientKey); + let round_trip_encrypt = (value) => { + let encrypted = FheInt256.encrypt_with_client_key(value, clientKey); let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I128_MIN); + assert.deepStrictEqual(decrypted, value); let serialized = encrypted.serialize(); - let deserialized = FheInt128.deserialize(serialized); + let deserialized = FheInt256.deserialize(serialized); let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, I128_MIN); + assert.deepStrictEqual(deserialized_decrypted, value); let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt128.safe_deserialize(safe_serialized, BigInt(10000000)); + let safe_deserialized = FheInt256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, I128_MIN); + assert.deepStrictEqual(safe_deserialized_decrypted, value); // Compressed - let compressed_encrypted = CompressedFheInt128.encrypt_with_client_key(I128_MIN, clientKey); + let compressed_encrypted = CompressedFheInt256.encrypt_with_client_key( + value, + clientKey, + ); let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheInt128.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() + let compressed_deserialized = CompressedFheInt256.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I128_MIN); + assert.deepStrictEqual(decrypted, value); - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheInt128.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheInt256.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, I128_MIN); + assert.deepStrictEqual(safe_decrypted, value); + }; + + round_trip_encrypt(I256_MIN); + round_trip_encrypt(I256_MAX); + round_trip_encrypt(BigInt(-1)); + round_trip_encrypt(BigInt(1)); + round_trip_encrypt(BigInt(-128)); + round_trip_encrypt(BigInt(128)); }); -test('hlapi_client_key_encrypt_decrypt_int256_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); - - - let clientKey = TfheClientKey.generate(config); - - - let round_trip_encrypt = (value) => { - let encrypted = FheInt256.encrypt_with_client_key(value, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, value); - - let serialized = encrypted.serialize(); - let deserialized = FheInt256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, value); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, value); - - // Compressed - let compressed_encrypted = CompressedFheInt256.encrypt_with_client_key(value, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheInt256.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress(); - - decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, value); - - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheInt256.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() - - safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, value); - }; - - - round_trip_encrypt(I256_MIN); - round_trip_encrypt(I256_MAX); - round_trip_encrypt(BigInt(-1)); - round_trip_encrypt(BigInt(1)); - round_trip_encrypt(BigInt(-128)); - round_trip_encrypt(BigInt(128)); +test("hlapi_decompress_public_key_then_encrypt_decrypt_int256_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); + let publicKey = compressedPublicKey.decompress(); + + let encrypted = FheInt256.encrypt_with_public_key(I256_MIN, publicKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I256_MIN); + + let serialized = encrypted.serialize(); + let deserialized = FheInt256.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, I256_MIN); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheInt256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, I256_MIN); }); -test('hlapi_client_key_encrypt_decrypt_int256_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - - let clientKey = TfheClientKey.generate(config); - - let round_trip_encrypt = (value) => { - let encrypted = FheInt256.encrypt_with_client_key(value, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, value); - - let serialized = encrypted.serialize(); - let deserialized = FheInt256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, value); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, value); - - // Compressed - let compressed_encrypted = CompressedFheInt256.encrypt_with_client_key(value, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheInt256.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() - - decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, value); - - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheInt256.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() - - safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, value); - } - - round_trip_encrypt(I256_MIN); - round_trip_encrypt(I256_MAX); - round_trip_encrypt(BigInt(-1)); - round_trip_encrypt(BigInt(1)); - round_trip_encrypt(BigInt(-128)); - round_trip_encrypt(BigInt(128)); +test("hlapi_public_key_encrypt_decrypt_int256_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + let publicKey = TfhePublicKey.new(clientKey); + + let encrypted = FheInt256.encrypt_with_public_key(I256_MIN, publicKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I256_MIN); + + let serialized = encrypted.serialize(); + let deserialized = FheInt256.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, I256_MIN); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheInt256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, I256_MIN); }); -test('hlapi_decompress_public_key_then_encrypt_decrypt_int256_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - - let clientKey = TfheClientKey.generate(config); - let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); - let publicKey = compressedPublicKey.decompress(); - - - let encrypted = FheInt256.encrypt_with_public_key(I256_MIN, publicKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I256_MIN); - - let serialized = encrypted.serialize(); - let deserialized = FheInt256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, I256_MIN); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, I256_MIN); -}); - -test('hlapi_public_key_encrypt_decrypt_int256_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - let publicKey = TfhePublicKey.new(clientKey); - - let encrypted = FheInt256.encrypt_with_public_key(I256_MIN, publicKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I256_MIN); - - let serialized = encrypted.serialize(); - let deserialized = FheInt256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, I256_MIN); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, I256_MIN); -}); - - ////////////////////////////////////////////////////////////////////////////// /// 32 bits compact ////////////////////////////////////////////////////////////////////////////// function hlapi_compact_public_key_encrypt_decrypt_int32_single(config) { - let clientKey = TfheClientKey.generate(config); - let publicKey = TfheCompactPublicKey.new(clientKey); - - let builder = CompactCiphertextList.builder(publicKey); - builder.push_i32(I32_MIN); - let list = builder.build(); - let expander = list.expand(); - let encrypted = expander.get_int32(0); - - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, I32_MIN); - - let serialized = encrypted.serialize(); - let deserialized = FheInt32.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, I32_MIN); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheInt32.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, I32_MIN); + let clientKey = TfheClientKey.generate(config); + let publicKey = TfheCompactPublicKey.new(clientKey); + + let builder = CompactCiphertextList.builder(publicKey); + builder.push_i32(I32_MIN); + let list = builder.build(); + let expander = list.expand(); + let encrypted = expander.get_int32(0); + + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, I32_MIN); + + let serialized = encrypted.serialize(); + let deserialized = FheInt32.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, I32_MIN); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheInt32.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, I32_MIN); } -test('hlapi_compact_public_key_encrypt_decrypt_int32_big_single', (t) => { - const block_params = new ShortintParameters(ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS_GAUSSIAN_2M64); - let config = TfheConfigBuilder.default() - .use_custom_parameters(block_params) - .build(); +test("hlapi_compact_public_key_encrypt_decrypt_int32_big_single", (t) => { + const block_params = new ShortintParameters( + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_KS_PBS_GAUSSIAN_2M64, + ); + let config = TfheConfigBuilder.default() + .use_custom_parameters(block_params) + .build(); - hlapi_compact_public_key_encrypt_decrypt_int32_single(config); + hlapi_compact_public_key_encrypt_decrypt_int32_single(config); }); -test('hlapi_compact_public_key_encrypt_decrypt_int32_small_single', (t) => { - const block_params = new ShortintParameters(ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS_GAUSSIAN_2M64); - let config = TfheConfigBuilder.default() - .use_custom_parameters(block_params) - .build(); +test("hlapi_compact_public_key_encrypt_decrypt_int32_small_single", (t) => { + const block_params = new ShortintParameters( + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_COMPACT_PK_PBS_KS_GAUSSIAN_2M64, + ); + let config = TfheConfigBuilder.default() + .use_custom_parameters(block_params) + .build(); - hlapi_compact_public_key_encrypt_decrypt_int32_single(config); + hlapi_compact_public_key_encrypt_decrypt_int32_single(config); }); - function generateRandomBigInt(bitLength) { - const bytesNeeded = Math.ceil(bitLength / 8); - const randomBytesBuffer = randomBytes(bytesNeeded); + const bytesNeeded = Math.ceil(bitLength / 8); + const randomBytesBuffer = randomBytes(bytesNeeded); - // Convert random bytes to BigInt - const randomBigInt = BigInt(`0x${randomBytesBuffer.toString('hex')}`); + // Convert random bytes to BigInt + const randomBigInt = BigInt(`0x${randomBytesBuffer.toString("hex")}`); - return randomBigInt; + return randomBigInt; } -test('hlapi_compact_ciphertext_list', (t) => { - let config = TfheConfigBuilder.default() - .build(); - - let clientKey = TfheClientKey.generate(config); - let publicKey = TfheCompactPublicKey.new(clientKey); - - let clear_u2 = 3; - let clear_i32 = -3284; - let clear_bool = true; - let clear_u256 = generateRandomBigInt(256); - let clear_u2048 = generateRandomBigInt(2048); - - let builder = CompactCiphertextList.builder(publicKey); - builder.push_u2(clear_u2); - builder.push_i32(clear_i32); - builder.push_boolean(clear_bool); - builder.push_u256(clear_u256); - builder.push_u2048(clear_u2048); - let list = builder.build(); - - let serialized = list.safe_serialize(BigInt(10000000)); - let deserialized = CompactCiphertextList.safe_deserialize(serialized, BigInt(10000000)); - - assert.deepStrictEqual(deserialized.is_empty(), false); - assert.deepStrictEqual(deserialized.len(), 5); - assert.deepStrictEqual(deserialized.get_kind_of(0), FheTypes.Uint2); - assert.deepStrictEqual(deserialized.get_kind_of(1), FheTypes.Int32); - assert.deepStrictEqual(deserialized.get_kind_of(2), FheTypes.Bool); - assert.deepStrictEqual(deserialized.get_kind_of(3), FheTypes.Uint256); - assert.deepStrictEqual(deserialized.get_kind_of(4), FheTypes.Uint2048); - - let expander = deserialized.expand(); - - assert.deepStrictEqual(expander.is_empty(), false); - assert.deepStrictEqual(expander.len(), 5); - assert.deepStrictEqual(expander.get_kind_of(0), FheTypes.Uint2); - assert.deepStrictEqual(expander.get_kind_of(1), FheTypes.Int32); - assert.deepStrictEqual(expander.get_kind_of(2), FheTypes.Bool); - assert.deepStrictEqual(expander.get_kind_of(3), FheTypes.Uint256); - assert.deepStrictEqual(expander.get_kind_of(4), FheTypes.Uint2048); - - assert.deepStrictEqual( - expander.get_uint2(0).decrypt(clientKey), - clear_u2, - ); - - assert.deepStrictEqual( - expander.get_int32(1).decrypt(clientKey), - clear_i32, - ); - - assert.deepStrictEqual( - expander.get_bool(2).decrypt(clientKey), - clear_bool, - ); - - assert.deepStrictEqual( - expander.get_uint256(3).decrypt(clientKey), - clear_u256, - ); - - assert.deepStrictEqual( - expander.get_uint2048(4).decrypt(clientKey), - clear_u2048, - ); - +test("hlapi_compact_ciphertext_list", (t) => { + let config = TfheConfigBuilder.default().build(); + + let clientKey = TfheClientKey.generate(config); + let publicKey = TfheCompactPublicKey.new(clientKey); + + let clear_u2 = 3; + let clear_i32 = -3284; + let clear_bool = true; + let clear_u256 = generateRandomBigInt(256); + let clear_u2048 = generateRandomBigInt(2048); + + let builder = CompactCiphertextList.builder(publicKey); + builder.push_u2(clear_u2); + builder.push_i32(clear_i32); + builder.push_boolean(clear_bool); + builder.push_u256(clear_u256); + builder.push_u2048(clear_u2048); + let list = builder.build(); + + let serialized = list.safe_serialize(BigInt(10000000)); + let deserialized = CompactCiphertextList.safe_deserialize( + serialized, + BigInt(10000000), + ); + + assert.deepStrictEqual(deserialized.is_empty(), false); + assert.deepStrictEqual(deserialized.len(), 5); + assert.deepStrictEqual(deserialized.get_kind_of(0), FheTypes.Uint2); + assert.deepStrictEqual(deserialized.get_kind_of(1), FheTypes.Int32); + assert.deepStrictEqual(deserialized.get_kind_of(2), FheTypes.Bool); + assert.deepStrictEqual(deserialized.get_kind_of(3), FheTypes.Uint256); + assert.deepStrictEqual(deserialized.get_kind_of(4), FheTypes.Uint2048); + + let expander = deserialized.expand(); + + assert.deepStrictEqual(expander.is_empty(), false); + assert.deepStrictEqual(expander.len(), 5); + assert.deepStrictEqual(expander.get_kind_of(0), FheTypes.Uint2); + assert.deepStrictEqual(expander.get_kind_of(1), FheTypes.Int32); + assert.deepStrictEqual(expander.get_kind_of(2), FheTypes.Bool); + assert.deepStrictEqual(expander.get_kind_of(3), FheTypes.Uint256); + assert.deepStrictEqual(expander.get_kind_of(4), FheTypes.Uint2048); + + assert.deepStrictEqual(expander.get_uint2(0).decrypt(clientKey), clear_u2); + + assert.deepStrictEqual(expander.get_int32(1).decrypt(clientKey), clear_i32); + + assert.deepStrictEqual(expander.get_bool(2).decrypt(clientKey), clear_bool); + + assert.deepStrictEqual( + expander.get_uint256(3).decrypt(clientKey), + clear_u256, + ); + + assert.deepStrictEqual( + expander.get_uint2048(4).decrypt(clientKey), + clear_u2048, + ); }); -test('hlapi_compact_ciphertext_list_with_proof', (t) => { - const block_params = new ShortintParameters(ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64); - let publicKeyParams = new ShortintCompactPublicKeyEncryptionParameters( - ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - ); - - let config = TfheConfigBuilder.default() - .use_custom_parameters(block_params) - .use_dedicated_compact_public_key_parameters(publicKeyParams) - .build(); - - let clientKey = TfheClientKey.generate(config); - let publicKey = TfheCompactPublicKey.new(clientKey); - - let crs = CompactPkeCrs.from_config(config, 2 + 32 + 1 + 256); - - const compress = false; // We don't compress as it's too slow on wasm - let serialized_pke_crs = crs.serialize(compress); - let validate = false; // Also too slow on wasm - crs = CompactPkeCrs.deserialize(serialized_pke_crs, compress, validate); - - let clear_u2 = 3; - let clear_i32 = -3284; - let clear_bool = true; - let clear_u256 = generateRandomBigInt(256); - - let builder = CompactCiphertextList.builder(publicKey); - builder.push_u2(clear_u2); - builder.push_i32(clear_i32); - builder.push_boolean(clear_bool); - builder.push_u256(clear_u256); - let list = builder.build_with_proof_packed(crs, ZkComputeLoad.Proof); - - let serialized = list.safe_serialize(BigInt(10000000)); - let deserialized = ProvenCompactCiphertextList.safe_deserialize(serialized, BigInt(10000000)); - - assert.deepStrictEqual(deserialized.is_empty(), false); - assert.deepStrictEqual(deserialized.len(), 4); - assert.deepStrictEqual(deserialized.get_kind_of(0), FheTypes.Uint2); - assert.deepStrictEqual(deserialized.get_kind_of(1), FheTypes.Int32); - assert.deepStrictEqual(deserialized.get_kind_of(2), FheTypes.Bool); - assert.deepStrictEqual(deserialized.get_kind_of(3), FheTypes.Uint256); - - // Verifying and expanding is too slow for single threaded node tests. +test("hlapi_compact_ciphertext_list_with_proof", (t) => { + const block_params = new ShortintParameters( + ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ); + let publicKeyParams = new ShortintCompactPublicKeyEncryptionParameters( + ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ); + + let config = TfheConfigBuilder.default() + .use_custom_parameters(block_params) + .use_dedicated_compact_public_key_parameters(publicKeyParams) + .build(); + + let clientKey = TfheClientKey.generate(config); + let publicKey = TfheCompactPublicKey.new(clientKey); + + let crs = CompactPkeCrs.from_config(config, 2 + 32 + 1 + 256); + + const compress = false; // We don't compress as it's too slow on wasm + let serialized_pke_crs = crs.serialize(compress); + let validate = false; // Also too slow on wasm + crs = CompactPkeCrs.deserialize(serialized_pke_crs, compress, validate); + + let clear_u2 = 3; + let clear_i32 = -3284; + let clear_bool = true; + let clear_u256 = generateRandomBigInt(256); + + let builder = CompactCiphertextList.builder(publicKey); + builder.push_u2(clear_u2); + builder.push_i32(clear_i32); + builder.push_boolean(clear_bool); + builder.push_u256(clear_u256); + let list = builder.build_with_proof_packed(crs, ZkComputeLoad.Proof); + + let serialized = list.safe_serialize(BigInt(10000000)); + let deserialized = ProvenCompactCiphertextList.safe_deserialize( + serialized, + BigInt(10000000), + ); + + assert.deepStrictEqual(deserialized.is_empty(), false); + assert.deepStrictEqual(deserialized.len(), 4); + assert.deepStrictEqual(deserialized.get_kind_of(0), FheTypes.Uint2); + assert.deepStrictEqual(deserialized.get_kind_of(1), FheTypes.Int32); + assert.deepStrictEqual(deserialized.get_kind_of(2), FheTypes.Bool); + assert.deepStrictEqual(deserialized.get_kind_of(3), FheTypes.Uint256); + + // Verifying and expanding is too slow for single threaded node tests. }); - -test('hlapi_compact_pk_conformance', (t) => { - - const limit = BigInt(1 << 20); - - let blockParams = new ShortintParameters( - ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, - ); - let publicKeyParams = new ShortintCompactPublicKeyEncryptionParameters( - ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, +test("hlapi_compact_pk_conformance", (t) => { + const limit = BigInt(1 << 20); + + let blockParams = new ShortintParameters( + ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ); + let publicKeyParams = new ShortintCompactPublicKeyEncryptionParameters( + ShortintCompactPublicKeyEncryptionParametersName.SHORTINT_PARAM_PKE_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64, + ); + + let config = TfheConfigBuilder.default() + .use_custom_parameters(blockParams) + .use_dedicated_compact_public_key_parameters(publicKeyParams) + .build(); + + let clientKey = TfheClientKey.generate(config); + let compressedPublicKey = TfheCompressedCompactPublicKey.new(clientKey); + + let serializedCompressedPublicKey = compressedPublicKey.safe_serialize(limit); + let _compressedPublicKey = + TfheCompressedCompactPublicKey.safe_deserialize_conformant( + serializedCompressedPublicKey, + limit, + publicKeyParams, ); - let config = TfheConfigBuilder.default() - .use_custom_parameters(blockParams) - .use_dedicated_compact_public_key_parameters(publicKeyParams) - .build(); - - let clientKey = TfheClientKey.generate(config); - let compressedPublicKey = TfheCompressedCompactPublicKey.new(clientKey); - - let serializedCompressedPublicKey = compressedPublicKey.safe_serialize(limit); - let _compressedPublicKey = TfheCompressedCompactPublicKey.safe_deserialize_conformant( - serializedCompressedPublicKey, limit, publicKeyParams); - - let publicKey = compressedPublicKey.decompress(); - let serializedPublicKey = publicKey.safe_serialize(limit); - let _publicKey = TfheCompactPublicKey.safe_deserialize_conformant(serializedPublicKey, limit, publicKeyParams); - - const message_modulus = BigInt(4); - const carry_modulus = BigInt(4); - const modulus_pow_2_exponent = 64; - const ks_level = 5; - const ks_base_log = 3; - let incorrectPublicKeyParams = ShortintCompactPublicKeyEncryptionParameters.new_parameters( - 512, - Shortint.try_new_t_uniform(42), - message_modulus, - carry_modulus, - modulus_pow_2_exponent, - ks_base_log, - ks_level, - ShortintEncryptionKeyChoice.Small, + let publicKey = compressedPublicKey.decompress(); + let serializedPublicKey = publicKey.safe_serialize(limit); + let _publicKey = TfheCompactPublicKey.safe_deserialize_conformant( + serializedPublicKey, + limit, + publicKeyParams, + ); + + const message_modulus = BigInt(4); + const carry_modulus = BigInt(4); + const modulus_pow_2_exponent = 64; + const ks_level = 5; + const ks_base_log = 3; + let incorrectPublicKeyParams = + ShortintCompactPublicKeyEncryptionParameters.new_parameters( + 512, + Shortint.try_new_t_uniform(42), + message_modulus, + carry_modulus, + modulus_pow_2_exponent, + ks_base_log, + ks_level, + ShortintEncryptionKeyChoice.Small, ); - assert.throws( - () => { - let _compressedPublicKey = TfheCompressedCompactPublicKey.safe_deserialize_conformant( - serializedCompressedPublicKey, limit, incorrectPublicKeyParams); - }, - ) - - assert.throws( - () => { - let _publicKey = TfheCompactPublicKey.safe_deserialize_conformant( - serializedPublicKey, limit, incorrectPublicKeyParams); - }, - ) -}) + assert.throws(() => { + let _compressedPublicKey = + TfheCompressedCompactPublicKey.safe_deserialize_conformant( + serializedCompressedPublicKey, + limit, + incorrectPublicKeyParams, + ); + }); + + assert.throws(() => { + let _publicKey = TfheCompactPublicKey.safe_deserialize_conformant( + serializedPublicKey, + limit, + incorrectPublicKeyParams, + ); + }); +}); diff --git a/tfhe/js_on_wasm_tests/test-hlapi-unsigned.js b/tfhe/js_on_wasm_tests/test-hlapi-unsigned.js index f71da7014f..cb1e42530f 100644 --- a/tfhe/js_on_wasm_tests/test-hlapi-unsigned.js +++ b/tfhe/js_on_wasm_tests/test-hlapi-unsigned.js @@ -1,28 +1,28 @@ -const test = require('node:test'); -const assert = require('node:assert').strict; -const { performance } = require('perf_hooks'); +const test = require("node:test"); +const assert = require("node:assert").strict; +const { performance } = require("perf_hooks"); const { - init_panic_hook, - TfheClientKey, - TfhePublicKey, - TfheCompressedPublicKey, - TfheCompressedServerKey, - TfheConfigBuilder, - CompressedFheUint8, - FheUint8, - FheUint32, - CompressedFheUint128, - FheUint128, - CompressedFheUint256, - FheUint256, - ShortintParameters, - ShortintParametersName + init_panic_hook, + TfheClientKey, + TfhePublicKey, + TfheCompressedPublicKey, + TfheCompressedServerKey, + TfheConfigBuilder, + CompressedFheUint8, + FheUint8, + FheUint32, + CompressedFheUint128, + FheUint128, + CompressedFheUint256, + FheUint256, + ShortintParameters, + ShortintParametersName, } = require("../pkg/tfhe.js"); -const { - randomBytes, -} = require('node:crypto'); +const { randomBytes } = require("node:crypto"); -const U256_MAX = BigInt("115792089237316195423570985008687907853269984665640564039457584007913129639935"); +const U256_MAX = BigInt( + "115792089237316195423570985008687907853269984665640564039457584007913129639935", +); const U128_MAX = BigInt("340282366920938463463374607431768211455"); const U32_MAX = 4294967295; @@ -33,373 +33,439 @@ const U32_MAX = 4294967295; // will be printed and test will be ok init_panic_hook(); function generateRandomBigInt(bitLength) { - const bytesNeeded = Math.ceil(bitLength / 8); - const randomBytesBuffer = randomBytes(bytesNeeded); + const bytesNeeded = Math.ceil(bitLength / 8); + const randomBytesBuffer = randomBytes(bytesNeeded); - // Convert random bytes to BigInt - return BigInt(`0x${randomBytesBuffer.toString('hex')}`); + // Convert random bytes to BigInt + return BigInt(`0x${randomBytesBuffer.toString("hex")}`); } - // Here integers are not enabled // but we try to use them, so an error should be returned // as the underlying panic should have been trapped -test('hlapi_panic', (t) => { - let config = TfheConfigBuilder.default() - .build(); +test("hlapi_panic", (t) => { + let config = TfheConfigBuilder.default().build(); - let clientKey = TfheClientKey.generate(config); + let clientKey = TfheClientKey.generate(config); - let clear = 73; + let clear = 73; - console.log("\nThe following log is an expected error log:\n=======================\n") + console.log( + "\nThe following log is an expected error log:\n=======================\n", + ); - try { - let _ = FheUint8.encrypt_with_client_key(clear, clientKey); - assert(false); - } catch (e) { - assert(true); - } + try { + let _ = FheUint8.encrypt_with_client_key(clear, clientKey); + assert(false); + } catch (e) { + assert(true); + } }); -test('hlapi_key_gen_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); - +test("hlapi_key_gen_big", (t) => { + let config = TfheConfigBuilder.default().build(); - let clientKey = TfheClientKey.generate(config); - let compressedServerKey = TfheCompressedServerKey.new(clientKey); - try { - let publicKey = TfhePublicKey.new(clientKey); - assert(false); - } catch (e) { - assert(true) - } - - let serializedClientKey = clientKey.serialize(); - let serializedCompressedServerKey = compressedServerKey.serialize(); -}); - -test('hlapi_key_gen_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - let compressedServerKey = TfheCompressedServerKey.new(clientKey); + let clientKey = TfheClientKey.generate(config); + let compressedServerKey = TfheCompressedServerKey.new(clientKey); + try { let publicKey = TfhePublicKey.new(clientKey); + assert(false); + } catch (e) { + assert(true); + } - let serializedClientKey = clientKey.serialize(); - let serializedCompressedServerKey = compressedServerKey.serialize(); - let serializedPublicKey = publicKey.serialize(); + let serializedClientKey = clientKey.serialize(); + let serializedCompressedServerKey = compressedServerKey.serialize(); }); -test('hlapi_client_key_encrypt_decrypt_uint8_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); - - - let clientKey = TfheClientKey.generate(config); +test("hlapi_key_gen_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); - let clear = 73; - let encrypted = FheUint8.encrypt_with_client_key(clear, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, clear); + let clientKey = TfheClientKey.generate(config); + let compressedServerKey = TfheCompressedServerKey.new(clientKey); + let publicKey = TfhePublicKey.new(clientKey); - let serialized = encrypted.serialize(); - let deserialized = FheUint8.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, clear); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint8.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, clear); + let serializedClientKey = clientKey.serialize(); + let serializedCompressedServerKey = compressedServerKey.serialize(); + let serializedPublicKey = publicKey.serialize(); }); -test('hlapi_compressed_public_client_uint8_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); - - let clientKey = TfheClientKey.generate(config); +test("hlapi_client_key_encrypt_decrypt_uint8_big", (t) => { + let config = TfheConfigBuilder.default().build(); - let clear = 73; - let compressed_encrypted = CompressedFheUint8.encrypt_with_client_key(clear, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheUint8.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() + let clientKey = TfheClientKey.generate(config); - let decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, clear); + let clear = 73; + let encrypted = FheUint8.encrypt_with_client_key(clear, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, clear); - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheUint8.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() + let serialized = encrypted.serialize(); + let deserialized = FheUint8.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, clear); - let safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, clear); + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint8.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, clear); }); -test('hlapi_public_key_encrypt_decrypt_uint32_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - let publicKey = TfhePublicKey.new(clientKey); - - let encrypted = FheUint32.encrypt_with_public_key(U32_MAX, publicKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U32_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint32.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U32_MAX); - - let safe_serialized = encrypted.safe_serialize(BigInt(1000000)); - let safe_deserialized = FheUint32.safe_deserialize(safe_serialized, BigInt(1000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U32_MAX); +test("hlapi_compressed_public_client_uint8_big", (t) => { + let config = TfheConfigBuilder.default().build(); + + let clientKey = TfheClientKey.generate(config); + + let clear = 73; + let compressed_encrypted = CompressedFheUint8.encrypt_with_client_key( + clear, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheUint8.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + let decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, clear); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheUint8.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + let safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, clear); }); -test('hlapi_decompress_public_key_then_encrypt_decrypt_uint32_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - var startTime = performance.now() - let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); - var endTime = performance.now() - - let data = compressedPublicKey.serialize() - - let publicKey = compressedPublicKey.decompress(); - - - var startTime = performance.now() - let encrypted = FheUint32.encrypt_with_public_key(U32_MAX, publicKey); - var endTime = performance.now() - - let ser = encrypted.serialize(); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U32_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint32.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U32_MAX); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint32.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U32_MAX); +test("hlapi_public_key_encrypt_decrypt_uint32_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + let publicKey = TfhePublicKey.new(clientKey); + + let encrypted = FheUint32.encrypt_with_public_key(U32_MAX, publicKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U32_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint32.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U32_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(1000000)); + let safe_deserialized = FheUint32.safe_deserialize( + safe_serialized, + BigInt(1000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U32_MAX); }); -test('hlapi_client_key_encrypt_decrypt_uint128_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); - - - let clientKey = TfheClientKey.generate(config); - - let encrypted = FheUint128.encrypt_with_client_key(U128_MAX, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U128_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint128.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U128_MAX); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint128.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U128_MAX); - - // Compressed - let compressed_encrypted = CompressedFheUint128.encrypt_with_client_key(U128_MAX, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheUint128.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() - - decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U128_MAX); - - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheUint128.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() - - safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, U128_MAX); +test("hlapi_decompress_public_key_then_encrypt_decrypt_uint32_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + var startTime = performance.now(); + let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); + var endTime = performance.now(); + + let data = compressedPublicKey.serialize(); + + let publicKey = compressedPublicKey.decompress(); + + var startTime = performance.now(); + let encrypted = FheUint32.encrypt_with_public_key(U32_MAX, publicKey); + var endTime = performance.now(); + + let ser = encrypted.serialize(); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U32_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint32.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U32_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint32.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U32_MAX); }); -test('hlapi_client_key_encrypt_decrypt_uint128_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - - let encrypted = FheUint128.encrypt_with_client_key(U128_MAX, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U128_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint128.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U128_MAX); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint128.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U128_MAX); - - // Compressed - let compressed_encrypted = CompressedFheUint128.encrypt_with_client_key(U128_MAX, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheUint128.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() - - decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U128_MAX); - - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheUint128.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() - - safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, U128_MAX); +test("hlapi_client_key_encrypt_decrypt_uint128_big", (t) => { + let config = TfheConfigBuilder.default().build(); + + let clientKey = TfheClientKey.generate(config); + + let encrypted = FheUint128.encrypt_with_client_key(U128_MAX, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U128_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint128.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U128_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint128.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U128_MAX); + + // Compressed + let compressed_encrypted = CompressedFheUint128.encrypt_with_client_key( + U128_MAX, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheUint128.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U128_MAX); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheUint128.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, U128_MAX); }); -test('hlapi_client_key_encrypt_decrypt_uint256_big', (t) => { - let config = TfheConfigBuilder.default() - .build(); - - - let clientKey = TfheClientKey.generate(config); - - let encrypted = FheUint256.encrypt_with_client_key(U256_MAX, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U256_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U256_MAX); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); - - // Compressed - let compressed_encrypted = CompressedFheUint256.encrypt_with_client_key(U256_MAX, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheUint256.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() - - decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U256_MAX); - - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheUint256.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() - - safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, U256_MAX); +test("hlapi_client_key_encrypt_decrypt_uint128_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + + let encrypted = FheUint128.encrypt_with_client_key(U128_MAX, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U128_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint128.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U128_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint128.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U128_MAX); + + // Compressed + let compressed_encrypted = CompressedFheUint128.encrypt_with_client_key( + U128_MAX, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheUint128.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U128_MAX); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheUint128.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, U128_MAX); }); -test('hlapi_client_key_encrypt_decrypt_uint256_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - - let encrypted = FheUint256.encrypt_with_client_key(U256_MAX, clientKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U256_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U256_MAX); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); - - // Compressed - let compressed_encrypted = CompressedFheUint256.encrypt_with_client_key(U256_MAX, clientKey); - let compressed_serialized = compressed_encrypted.serialize(); - let compressed_deserialized = CompressedFheUint256.deserialize(compressed_serialized); - let decompressed = compressed_deserialized.decompress() - - decrypted = decompressed.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U256_MAX); - - let compressed_safe_serialized = compressed_encrypted.safe_serialize(BigInt(10000000)); - let compressed_safe_deserialized = CompressedFheUint256.safe_deserialize(compressed_safe_serialized, BigInt(10000000)); - let safe_decompressed = compressed_safe_deserialized.decompress() - - safe_decrypted = safe_decompressed.decrypt(clientKey); - assert.deepStrictEqual(safe_decrypted, U256_MAX); +test("hlapi_client_key_encrypt_decrypt_uint256_big", (t) => { + let config = TfheConfigBuilder.default().build(); + + let clientKey = TfheClientKey.generate(config); + + let encrypted = FheUint256.encrypt_with_client_key(U256_MAX, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U256_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint256.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U256_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); + + // Compressed + let compressed_encrypted = CompressedFheUint256.encrypt_with_client_key( + U256_MAX, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheUint256.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U256_MAX); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheUint256.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, U256_MAX); }); -test('hlapi_decompress_public_key_then_encrypt_decrypt_uint256_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); - let publicKey = compressedPublicKey.decompress(); - - - let encrypted = FheUint256.encrypt_with_public_key(U256_MAX, publicKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U256_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U256_MAX); - - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); +test("hlapi_client_key_encrypt_decrypt_uint256_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + + let encrypted = FheUint256.encrypt_with_client_key(U256_MAX, clientKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U256_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint256.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U256_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); + + // Compressed + let compressed_encrypted = CompressedFheUint256.encrypt_with_client_key( + U256_MAX, + clientKey, + ); + let compressed_serialized = compressed_encrypted.serialize(); + let compressed_deserialized = CompressedFheUint256.deserialize( + compressed_serialized, + ); + let decompressed = compressed_deserialized.decompress(); + + decrypted = decompressed.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U256_MAX); + + let compressed_safe_serialized = compressed_encrypted.safe_serialize( + BigInt(10000000), + ); + let compressed_safe_deserialized = CompressedFheUint256.safe_deserialize( + compressed_safe_serialized, + BigInt(10000000), + ); + let safe_decompressed = compressed_safe_deserialized.decompress(); + + safe_decrypted = safe_decompressed.decrypt(clientKey); + assert.deepStrictEqual(safe_decrypted, U256_MAX); }); -test('hlapi_public_key_encrypt_decrypt_uint256_small', (t) => { - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let config = TfheConfigBuilder.with_custom_parameters(params_small) - .build(); - - let clientKey = TfheClientKey.generate(config); - let publicKey = TfhePublicKey.new(clientKey); - - let encrypted = FheUint256.encrypt_with_public_key(U256_MAX, publicKey); - let decrypted = encrypted.decrypt(clientKey); - assert.deepStrictEqual(decrypted, U256_MAX); - - let serialized = encrypted.serialize(); - let deserialized = FheUint256.deserialize(serialized); - let deserialized_decrypted = deserialized.decrypt(clientKey); - assert.deepStrictEqual(deserialized_decrypted, U256_MAX); +test("hlapi_decompress_public_key_then_encrypt_decrypt_uint256_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + let compressedPublicKey = TfheCompressedPublicKey.new(clientKey); + let publicKey = compressedPublicKey.decompress(); + + let encrypted = FheUint256.encrypt_with_public_key(U256_MAX, publicKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U256_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint256.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U256_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); +}); - let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); - let safe_deserialized = FheUint256.safe_deserialize(safe_serialized, BigInt(10000000)); - let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); - assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); +test("hlapi_public_key_encrypt_decrypt_uint256_small", (t) => { + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let config = TfheConfigBuilder.with_custom_parameters(params_small).build(); + + let clientKey = TfheClientKey.generate(config); + let publicKey = TfhePublicKey.new(clientKey); + + let encrypted = FheUint256.encrypt_with_public_key(U256_MAX, publicKey); + let decrypted = encrypted.decrypt(clientKey); + assert.deepStrictEqual(decrypted, U256_MAX); + + let serialized = encrypted.serialize(); + let deserialized = FheUint256.deserialize(serialized); + let deserialized_decrypted = deserialized.decrypt(clientKey); + assert.deepStrictEqual(deserialized_decrypted, U256_MAX); + + let safe_serialized = encrypted.safe_serialize(BigInt(10000000)); + let safe_deserialized = FheUint256.safe_deserialize( + safe_serialized, + BigInt(10000000), + ); + let safe_deserialized_decrypted = safe_deserialized.decrypt(clientKey); + assert.deepStrictEqual(safe_deserialized_decrypted, U256_MAX); }); diff --git a/tfhe/js_on_wasm_tests/test.js b/tfhe/js_on_wasm_tests/test.js index 6e93cde732..22224d6762 100644 --- a/tfhe/js_on_wasm_tests/test.js +++ b/tfhe/js_on_wasm_tests/test.js @@ -1,243 +1,290 @@ -const crypto = require('crypto'); -const test = require('node:test'); -const assert = require('node:assert').strict; -const { Boolean, Shortint, BooleanParameterSet, ShortintParametersName, ShortintParameters } = require("../pkg"); +const crypto = require("crypto"); +const test = require("node:test"); +const assert = require("node:assert").strict; +const { + Boolean, + Shortint, + BooleanParameterSet, + ShortintParametersName, + ShortintParameters, +} = require("../pkg"); function genRandomBigIntWithBytes(byteCount) { - return BigInt('0x' + crypto.randomBytes(byteCount).toString('hex')) + return BigInt("0x" + crypto.randomBytes(byteCount).toString("hex")); } // Boolean tests -test('boolean_encrypt_decrypt', (t) => { - let params = Boolean.get_parameters(BooleanParameterSet.Default); - let cks = Boolean.new_client_key(params); - let ct = Boolean.encrypt(cks, true); +test("boolean_encrypt_decrypt", (t) => { + let params = Boolean.get_parameters(BooleanParameterSet.Default); + let cks = Boolean.new_client_key(params); + let ct = Boolean.encrypt(cks, true); - let serialized_cks = Boolean.serialize_client_key(cks); - let deserialized_cks = Boolean.deserialize_client_key(serialized_cks); + let serialized_cks = Boolean.serialize_client_key(cks); + let deserialized_cks = Boolean.deserialize_client_key(serialized_cks); - let serialized_ct = Boolean.serialize_ciphertext(ct); - let deserialized_ct = Boolean.deserialize_ciphertext(serialized_ct); + let serialized_ct = Boolean.serialize_ciphertext(ct); + let deserialized_ct = Boolean.deserialize_ciphertext(serialized_ct); - let decrypted = Boolean.decrypt(deserialized_cks, deserialized_ct); - assert.deepStrictEqual(decrypted, true); + let decrypted = Boolean.decrypt(deserialized_cks, deserialized_ct); + assert.deepStrictEqual(decrypted, true); - let sks = Boolean.new_compressed_server_key(cks); + let sks = Boolean.new_compressed_server_key(cks); - let serialized_sks = Boolean.serialize_compressed_server_key(sks); - let deserialized_sks = Boolean.deserialize_compressed_server_key(serialized_sks); + let serialized_sks = Boolean.serialize_compressed_server_key(sks); + let deserialized_sks = + Boolean.deserialize_compressed_server_key(serialized_sks); - // No equality tests here, as wasm stores pointers which will always differ + // No equality tests here, as wasm stores pointers which will always differ }); -test('boolean_compressed_encrypt_decrypt', (t) => { - let params = Boolean.get_parameters(BooleanParameterSet.Default); - let cks = Boolean.new_client_key(params); - let ct = Boolean.encrypt_compressed(cks, true); +test("boolean_compressed_encrypt_decrypt", (t) => { + let params = Boolean.get_parameters(BooleanParameterSet.Default); + let cks = Boolean.new_client_key(params); + let ct = Boolean.encrypt_compressed(cks, true); - let serialized_cks = Boolean.serialize_client_key(cks); - let deserialized_cks = Boolean.deserialize_client_key(serialized_cks); + let serialized_cks = Boolean.serialize_client_key(cks); + let deserialized_cks = Boolean.deserialize_client_key(serialized_cks); - let serialized_ct = Boolean.serialize_compressed_ciphertext(ct); - let deserialized_ct = Boolean.deserialize_compressed_ciphertext(serialized_ct); + let serialized_ct = Boolean.serialize_compressed_ciphertext(ct); + let deserialized_ct = + Boolean.deserialize_compressed_ciphertext(serialized_ct); - let decompressed_ct = Boolean.decompress_ciphertext(deserialized_ct); + let decompressed_ct = Boolean.decompress_ciphertext(deserialized_ct); - let decrypted = Boolean.decrypt(deserialized_cks, decompressed_ct); - assert.deepStrictEqual(decrypted, true); + let decrypted = Boolean.decrypt(deserialized_cks, decompressed_ct); + assert.deepStrictEqual(decrypted, true); }); -test('boolean_public_encrypt_decrypt', (t) => { - let params = Boolean.get_parameters(BooleanParameterSet.Default); - let cks = Boolean.new_client_key(params); - let pk = Boolean.new_public_key(cks); +test("boolean_public_encrypt_decrypt", (t) => { + let params = Boolean.get_parameters(BooleanParameterSet.Default); + let cks = Boolean.new_client_key(params); + let pk = Boolean.new_public_key(cks); - let serialized_pk = Boolean.serialize_public_key(pk); - let deserialized_pk = Boolean.deserialize_public_key(serialized_pk); + let serialized_pk = Boolean.serialize_public_key(pk); + let deserialized_pk = Boolean.deserialize_public_key(serialized_pk); - let ct = Boolean.encrypt_with_public_key(deserialized_pk, true); + let ct = Boolean.encrypt_with_public_key(deserialized_pk, true); - let serialized_ct = Boolean.serialize_ciphertext(ct); - let deserialized_ct = Boolean.deserialize_ciphertext(serialized_ct); + let serialized_ct = Boolean.serialize_ciphertext(ct); + let deserialized_ct = Boolean.deserialize_ciphertext(serialized_ct); - let decrypted = Boolean.decrypt(cks, deserialized_ct); - assert.deepStrictEqual(decrypted, true); + let decrypted = Boolean.decrypt(cks, deserialized_ct); + assert.deepStrictEqual(decrypted, true); }); -test('boolean_deterministic_keygen', (t) => { - const TEST_LOOP_COUNT = 128; - - let seed_high_bytes = genRandomBigIntWithBytes(8); - let seed_low_bytes = genRandomBigIntWithBytes(8); - - let params = Boolean.get_parameters(BooleanParameterSet.Default); - let cks = Boolean.new_client_key_from_seed_and_parameters(seed_high_bytes, seed_low_bytes, params); - let other_cks = Boolean.new_client_key_from_seed_and_parameters(seed_high_bytes, seed_low_bytes, params); - - for (let i = 0; i < TEST_LOOP_COUNT; i++) { - let ct_true = Boolean.encrypt(cks, true); - let decrypt_true_other = Boolean.decrypt(other_cks, ct_true); - assert.deepStrictEqual(decrypt_true_other, true); - - let ct_false = Boolean.encrypt(cks, false); - let decrypt_false_other = Boolean.decrypt(other_cks, ct_false); - assert.deepStrictEqual(decrypt_false_other, false); - } +test("boolean_deterministic_keygen", (t) => { + const TEST_LOOP_COUNT = 128; + + let seed_high_bytes = genRandomBigIntWithBytes(8); + let seed_low_bytes = genRandomBigIntWithBytes(8); + + let params = Boolean.get_parameters(BooleanParameterSet.Default); + let cks = Boolean.new_client_key_from_seed_and_parameters( + seed_high_bytes, + seed_low_bytes, + params, + ); + let other_cks = Boolean.new_client_key_from_seed_and_parameters( + seed_high_bytes, + seed_low_bytes, + params, + ); + + for (let i = 0; i < TEST_LOOP_COUNT; i++) { + let ct_true = Boolean.encrypt(cks, true); + let decrypt_true_other = Boolean.decrypt(other_cks, ct_true); + assert.deepStrictEqual(decrypt_true_other, true); + + let ct_false = Boolean.encrypt(cks, false); + let decrypt_false_other = Boolean.decrypt(other_cks, ct_false); + assert.deepStrictEqual(decrypt_false_other, false); + } }); - // Shortint tests -test('shortint_encrypt_decrypt', (t) => { - let params_name = ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; - let params = new ShortintParameters(params_name); - let cks = Shortint.new_client_key(params); - let ct = Shortint.encrypt(cks, BigInt(3)); +test("shortint_encrypt_decrypt", (t) => { + let params_name = + ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; + let params = new ShortintParameters(params_name); + let cks = Shortint.new_client_key(params); + let ct = Shortint.encrypt(cks, BigInt(3)); - let serialized_cks = Shortint.serialize_client_key(cks); - let deserialized_cks = Shortint.deserialize_client_key(serialized_cks); + let serialized_cks = Shortint.serialize_client_key(cks); + let deserialized_cks = Shortint.deserialize_client_key(serialized_cks); - let serialized_ct = Shortint.serialize_ciphertext(ct); - let deserialized_ct = Shortint.deserialize_ciphertext(serialized_ct); + let serialized_ct = Shortint.serialize_ciphertext(ct); + let deserialized_ct = Shortint.deserialize_ciphertext(serialized_ct); - let decrypted = Shortint.decrypt(deserialized_cks, deserialized_ct); - assert.deepStrictEqual(decrypted, BigInt(3)); + let decrypted = Shortint.decrypt(deserialized_cks, deserialized_ct); + assert.deepStrictEqual(decrypted, BigInt(3)); - let sks = Shortint.new_compressed_server_key(cks); + let sks = Shortint.new_compressed_server_key(cks); - let serialized_sks = Shortint.serialize_compressed_server_key(sks); - let deserialized_sks = Shortint.deserialize_compressed_server_key(serialized_sks); + let serialized_sks = Shortint.serialize_compressed_server_key(sks); + let deserialized_sks = + Shortint.deserialize_compressed_server_key(serialized_sks); - // No equality tests here, as wasm stores pointers which will always differ + // No equality tests here, as wasm stores pointers which will always differ - // Encryption using small keys - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let cks_small = Shortint.new_client_key(params_small); + // Encryption using small keys + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let cks_small = Shortint.new_client_key(params_small); - let ct_small = Shortint.encrypt(cks_small, BigInt(3)); + let ct_small = Shortint.encrypt(cks_small, BigInt(3)); - let serialized_ct_small = Shortint.serialize_ciphertext(ct_small); - let deserialized_ct_small = Shortint.deserialize_ciphertext(serialized_ct_small); + let serialized_ct_small = Shortint.serialize_ciphertext(ct_small); + let deserialized_ct_small = + Shortint.deserialize_ciphertext(serialized_ct_small); - let decrypted_small = Shortint.decrypt(cks_small, deserialized_ct_small); - assert.deepStrictEqual(decrypted_small, BigInt(3)); + let decrypted_small = Shortint.decrypt(cks_small, deserialized_ct_small); + assert.deepStrictEqual(decrypted_small, BigInt(3)); }); -test('shortint_compressed_encrypt_decrypt', (t) => { - let params_name = ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; - let params = new ShortintParameters(params_name); - let cks = Shortint.new_client_key(params); - let ct = Shortint.encrypt_compressed(cks, BigInt(3)); +test("shortint_compressed_encrypt_decrypt", (t) => { + let params_name = + ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; + let params = new ShortintParameters(params_name); + let cks = Shortint.new_client_key(params); + let ct = Shortint.encrypt_compressed(cks, BigInt(3)); - let serialized_cks = Shortint.serialize_client_key(cks); - let deserialized_cks = Shortint.deserialize_client_key(serialized_cks); + let serialized_cks = Shortint.serialize_client_key(cks); + let deserialized_cks = Shortint.deserialize_client_key(serialized_cks); - let serialized_ct = Shortint.serialize_compressed_ciphertext(ct); - let deserialized_ct = Shortint.deserialize_compressed_ciphertext(serialized_ct); + let serialized_ct = Shortint.serialize_compressed_ciphertext(ct); + let deserialized_ct = + Shortint.deserialize_compressed_ciphertext(serialized_ct); - let decompressed_ct = Shortint.decompress_ciphertext(deserialized_ct); + let decompressed_ct = Shortint.decompress_ciphertext(deserialized_ct); - let decrypted = Shortint.decrypt(deserialized_cks, decompressed_ct); - assert.deepStrictEqual(decrypted, BigInt(3)); + let decrypted = Shortint.decrypt(deserialized_cks, decompressed_ct); + assert.deepStrictEqual(decrypted, BigInt(3)); - // Encryption using small keys - // We don't have TUniform small params so use previous gaussian ones. - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let cks_small = Shortint.new_client_key(params_small); + // Encryption using small keys + // We don't have TUniform small params so use previous gaussian ones. + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let cks_small = Shortint.new_client_key(params_small); - let ct_small = Shortint.encrypt_compressed(cks_small, BigInt(3)); + let ct_small = Shortint.encrypt_compressed(cks_small, BigInt(3)); - let serialized_ct_small = Shortint.serialize_compressed_ciphertext(ct_small); - let deserialized_ct_small = Shortint.deserialize_compressed_ciphertext(serialized_ct_small); + let serialized_ct_small = Shortint.serialize_compressed_ciphertext(ct_small); + let deserialized_ct_small = + Shortint.deserialize_compressed_ciphertext(serialized_ct_small); - let decompressed_ct_small = Shortint.decompress_ciphertext(deserialized_ct_small); + let decompressed_ct_small = Shortint.decompress_ciphertext( + deserialized_ct_small, + ); - let decrypted_small = Shortint.decrypt(cks_small, decompressed_ct_small); - assert.deepStrictEqual(decrypted_small, BigInt(3)); + let decrypted_small = Shortint.decrypt(cks_small, decompressed_ct_small); + assert.deepStrictEqual(decrypted_small, BigInt(3)); }); -test('shortint_public_encrypt_decrypt', (t) => { - let params_name_2_0 = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_0_KS_PBS_GAUSSIAN_2M64; - let params_2_0 = new ShortintParameters(params_name_2_0); - let cks = Shortint.new_client_key(params_2_0); - let pk = Shortint.new_public_key(cks); +test("shortint_public_encrypt_decrypt", (t) => { + let params_name_2_0 = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_0_KS_PBS_GAUSSIAN_2M64; + let params_2_0 = new ShortintParameters(params_name_2_0); + let cks = Shortint.new_client_key(params_2_0); + let pk = Shortint.new_public_key(cks); - let ct = Shortint.encrypt_with_public_key(pk, BigInt(3)); + let ct = Shortint.encrypt_with_public_key(pk, BigInt(3)); - let serialized_ct = Shortint.serialize_ciphertext(ct); - let deserialized_ct = Shortint.deserialize_ciphertext(serialized_ct); + let serialized_ct = Shortint.serialize_ciphertext(ct); + let deserialized_ct = Shortint.deserialize_ciphertext(serialized_ct); - let decrypted = Shortint.decrypt(cks, deserialized_ct); - assert.deepStrictEqual(decrypted, BigInt(3)); + let decrypted = Shortint.decrypt(cks, deserialized_ct); + assert.deepStrictEqual(decrypted, BigInt(3)); - // Small - let params_name_2_2_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_2_2_small = new ShortintParameters(params_name_2_2_small); - let cks_small = Shortint.new_client_key(params_2_2_small); + // Small + let params_name_2_2_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_2_2_small = new ShortintParameters(params_name_2_2_small); + let cks_small = Shortint.new_client_key(params_2_2_small); - let pk_small = Shortint.new_public_key(cks_small); + let pk_small = Shortint.new_public_key(cks_small); - let ct_small = Shortint.encrypt_with_public_key(pk_small, BigInt(3)); + let ct_small = Shortint.encrypt_with_public_key(pk_small, BigInt(3)); - let serialized_ct_small = Shortint.serialize_ciphertext(ct_small); - let deserialized_ct_small = Shortint.deserialize_ciphertext(serialized_ct_small); + let serialized_ct_small = Shortint.serialize_ciphertext(ct_small); + let deserialized_ct_small = + Shortint.deserialize_ciphertext(serialized_ct_small); - let decrypted_small = Shortint.decrypt(cks_small, deserialized_ct_small); - assert.deepStrictEqual(decrypted_small, BigInt(3)); + let decrypted_small = Shortint.decrypt(cks_small, deserialized_ct_small); + assert.deepStrictEqual(decrypted_small, BigInt(3)); }); -test('shortint_compressed_public_encrypt_decrypt', (t) => { - let params_name = ShortintParametersName.PARAM_MESSAGE_1_CARRY_1_KS_PBS; - let params = new ShortintParameters(params_name); - let cks = Shortint.new_client_key(params); - let pk = Shortint.new_compressed_public_key(cks); +test("shortint_compressed_public_encrypt_decrypt", (t) => { + let params_name = ShortintParametersName.PARAM_MESSAGE_1_CARRY_1_KS_PBS; + let params = new ShortintParameters(params_name); + let cks = Shortint.new_client_key(params); + let pk = Shortint.new_compressed_public_key(cks); - let serialized_pk = Shortint.serialize_compressed_public_key(pk); - let deserialized_pk = Shortint.deserialize_compressed_public_key(serialized_pk); + let serialized_pk = Shortint.serialize_compressed_public_key(pk); + let deserialized_pk = + Shortint.deserialize_compressed_public_key(serialized_pk); - let ct = Shortint.encrypt_with_compressed_public_key(deserialized_pk, BigInt(1)); + let ct = Shortint.encrypt_with_compressed_public_key( + deserialized_pk, + BigInt(1), + ); - let serialized_ct = Shortint.serialize_ciphertext(ct); - let deserialized_ct = Shortint.deserialize_ciphertext(serialized_ct); + let serialized_ct = Shortint.serialize_ciphertext(ct); + let deserialized_ct = Shortint.deserialize_ciphertext(serialized_ct); - let decrypted = Shortint.decrypt(cks, deserialized_ct); - assert.deepStrictEqual(decrypted, BigInt(1)); + let decrypted = Shortint.decrypt(cks, deserialized_ct); + assert.deepStrictEqual(decrypted, BigInt(1)); - // Small - let params_name_small = ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; - let params_small = new ShortintParameters(params_name_small); - let cks_small = Shortint.new_client_key(params_small); + // Small + let params_name_small = + ShortintParametersName.V0_11_PARAM_MESSAGE_2_CARRY_2_PBS_KS_GAUSSIAN_2M64; + let params_small = new ShortintParameters(params_name_small); + let cks_small = Shortint.new_client_key(params_small); - let pk_small = Shortint.new_compressed_public_key(cks_small); + let pk_small = Shortint.new_compressed_public_key(cks_small); - let serialized_pk_small = Shortint.serialize_compressed_public_key(pk_small); - let deserialized_pk_small = Shortint.deserialize_compressed_public_key(serialized_pk_small); + let serialized_pk_small = Shortint.serialize_compressed_public_key(pk_small); + let deserialized_pk_small = + Shortint.deserialize_compressed_public_key(serialized_pk_small); - let ct_small = Shortint.encrypt_with_compressed_public_key(deserialized_pk_small, BigInt(1)); + let ct_small = Shortint.encrypt_with_compressed_public_key( + deserialized_pk_small, + BigInt(1), + ); - let serialized_ct_small = Shortint.serialize_ciphertext(ct_small); - let deserialized_ct_small = Shortint.deserialize_ciphertext(serialized_ct_small); + let serialized_ct_small = Shortint.serialize_ciphertext(ct_small); + let deserialized_ct_small = + Shortint.deserialize_ciphertext(serialized_ct_small); - let decrypted_small = Shortint.decrypt(cks_small, deserialized_ct_small); - assert.deepStrictEqual(decrypted_small, BigInt(1)); + let decrypted_small = Shortint.decrypt(cks_small, deserialized_ct_small); + assert.deepStrictEqual(decrypted_small, BigInt(1)); }); -test('shortint_deterministic_keygen', (t) => { - const TEST_LOOP_COUNT = 128; - - let seed_high_bytes = genRandomBigIntWithBytes(8); - let seed_low_bytes = genRandomBigIntWithBytes(8); - - let params_name = ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; - let params = new ShortintParameters(params_name); - let cks = Shortint.new_client_key_from_seed_and_parameters(seed_high_bytes, seed_low_bytes, params); - let other_cks = Shortint.new_client_key_from_seed_and_parameters(seed_high_bytes, seed_low_bytes, params); - - for (let i = 0; i < TEST_LOOP_COUNT; i++) { - let random_message = genRandomBigIntWithBytes(4) % BigInt(4); - let ct = Shortint.encrypt(cks, random_message); - let decrypt_other = Shortint.decrypt(other_cks, ct); - assert.deepStrictEqual(decrypt_other, random_message); - } +test("shortint_deterministic_keygen", (t) => { + const TEST_LOOP_COUNT = 128; + + let seed_high_bytes = genRandomBigIntWithBytes(8); + let seed_low_bytes = genRandomBigIntWithBytes(8); + + let params_name = + ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_KS_PBS_TUNIFORM_2M64; + let params = new ShortintParameters(params_name); + let cks = Shortint.new_client_key_from_seed_and_parameters( + seed_high_bytes, + seed_low_bytes, + params, + ); + let other_cks = Shortint.new_client_key_from_seed_and_parameters( + seed_high_bytes, + seed_low_bytes, + params, + ); + + for (let i = 0; i < TEST_LOOP_COUNT; i++) { + let random_message = genRandomBigIntWithBytes(4) % BigInt(4); + let ct = Shortint.encrypt(cks, random_message); + let decrypt_other = Shortint.decrypt(other_cks, ct); + assert.deepStrictEqual(decrypt_other, random_message); + } }); diff --git a/tfhe/web_wasm_parallel_tests/Makefile b/tfhe/web_wasm_parallel_tests/Makefile index 20dda378b9..079e78ed41 100644 --- a/tfhe/web_wasm_parallel_tests/Makefile +++ b/tfhe/web_wasm_parallel_tests/Makefile @@ -9,7 +9,7 @@ fmt: npm install npm run format -.PHONY: fmt # Check Javascript code format +.PHONY: check_fmt # Check Javascript code format check_fmt: npm install npm run check-format