mirror of
https://github.com/zama-ai/tfhe-rs.git
synced 2026-01-09 14:47:56 -05:00
chore(hlapi): add example to measure CPK and CCTL sizes
This also includes key generation time in WASM web client side
This commit is contained in:
committed by
David Testé
parent
f8b497a4b8
commit
9e307a8945
@@ -25,17 +25,25 @@ import init, {
|
||||
CompactFheUint256List,
|
||||
} from "./pkg/tfhe.js";
|
||||
|
||||
function assert(cond, text){
|
||||
if( cond ) return;
|
||||
if( console.assert.useDebugger ) debugger;
|
||||
throw new Error(text || "Assertion failed!");
|
||||
};
|
||||
function assert(cond, text) {
|
||||
if (cond) return;
|
||||
if (console.assert.useDebugger) debugger;
|
||||
throw new Error(text || "Assertion failed!");
|
||||
}
|
||||
|
||||
function assert_eq(a, b, text){
|
||||
if( a === b ) return;
|
||||
if( console.assert.useDebugger ) debugger;
|
||||
throw new Error(text || `Equality assertion failed!: ${a} != ${b}`);
|
||||
};
|
||||
function assert_eq(a, b, text) {
|
||||
if (a === b) return;
|
||||
if (console.assert.useDebugger) debugger;
|
||||
throw new Error(text || `Equality assertion failed!: ${a} != ${b}`);
|
||||
}
|
||||
|
||||
function append_param_name(bench_results, params_name) {
|
||||
let results = {};
|
||||
for (const bench_name in bench_results) {
|
||||
results[`${bench_name}_${params_name}`] = bench_results[bench_name];
|
||||
}
|
||||
return results
|
||||
}
|
||||
|
||||
async function compressedPublicKeyTest() {
|
||||
let config = TfheConfigBuilder.all_disabled()
|
||||
@@ -45,7 +53,7 @@ async function compressedPublicKeyTest() {
|
||||
console.time('ClientKey Gen')
|
||||
let clientKey = TfheClientKey.generate(config);
|
||||
console.timeEnd('ClientKey Gen')
|
||||
|
||||
|
||||
console.time('CompressedPublicKey Gen')
|
||||
let compressedPublicKey = TfheCompressedPublicKey.new(clientKey);
|
||||
console.timeEnd('CompressedPublicKey Gen')
|
||||
@@ -119,8 +127,7 @@ async function compactPublicKeyTest32BitOnConfig(config) {
|
||||
|
||||
assert_eq(encrypted_list.length, values.length);
|
||||
|
||||
for (let i = 0; i < values.length; i++)
|
||||
{
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
let decrypted = encrypted_list[i].decrypt(clientKey);
|
||||
assert_eq(decrypted, values[i]);
|
||||
}
|
||||
@@ -133,8 +140,7 @@ async function compactPublicKeyTest32BitOnConfig(config) {
|
||||
let encrypted_list = deserialized_list.expand();
|
||||
assert_eq(encrypted_list.length, values.length);
|
||||
|
||||
for (let i = 0; i < values.length; i++)
|
||||
{
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
let decrypted = encrypted_list[i].decrypt(clientKey);
|
||||
assert_eq(decrypted, values[i]);
|
||||
}
|
||||
@@ -156,6 +162,84 @@ async function compactPublicKeyTest32BitSmall() {
|
||||
await compactPublicKeyTest32BitOnConfig(config)
|
||||
}
|
||||
|
||||
async function compactPublicKeyBench32BitOnConfig(config) {
|
||||
const bench_loops = 100;
|
||||
let bench_results = {};
|
||||
|
||||
console.time('ClientKey Gen')
|
||||
let clientKey = TfheClientKey.generate(config);
|
||||
console.timeEnd('ClientKey Gen')
|
||||
|
||||
// Generate PK for encryption for later
|
||||
console.time('CompactPublicKey Gen')
|
||||
let publicKey = TfheCompactPublicKey.new(clientKey);
|
||||
console.timeEnd('CompactPublicKey Gen')
|
||||
|
||||
// Bench the pk generation for bench_loops iterations
|
||||
let start = performance.now();
|
||||
for (let i = 0; i < bench_loops; i++) {
|
||||
let _ = TfheCompactPublicKey.new(clientKey);
|
||||
}
|
||||
let end = performance.now();
|
||||
const timing_1 = (end - start) / bench_loops
|
||||
console.log('CompactPublicKey Gen bench: ', timing_1, ' ms');
|
||||
bench_results["compact_public_key_gen_32bit_mean"] = timing_1;
|
||||
|
||||
let values = [0, 1, 2, 2394, U32_MAX];
|
||||
|
||||
// Encrypt compact CT list for serialization for later
|
||||
console.time('CompactFheUint32List Encrypt')
|
||||
let compact_list = CompactFheUint32List.encrypt_with_compact_public_key(values, publicKey);
|
||||
console.timeEnd('CompactFheUint32List Encrypt')
|
||||
|
||||
// Bench the encryption for bench_loops iterations
|
||||
start = performance.now();
|
||||
for (let i = 0; i < bench_loops; i++) {
|
||||
let _ = CompactFheUint32List.encrypt_with_compact_public_key(values, publicKey);
|
||||
}
|
||||
end = performance.now();
|
||||
const timing_2 = (end - start) / bench_loops
|
||||
console.log('CompactFheUint32List Encrypt bench: ', timing_2, ' ms');
|
||||
bench_results["compact_fheunit32_list_encrypt_mean"] = timing_2;
|
||||
|
||||
let serialized_list = compact_list.serialize();
|
||||
console.log("Serialized CompactFheUint32List size: ", serialized_list.length);
|
||||
|
||||
// Bench the serialization for bench_loops iterations
|
||||
start = performance.now();
|
||||
for (let i = 0; i < bench_loops; i++) {
|
||||
let _ = compact_list.serialize();
|
||||
}
|
||||
end = performance.now();
|
||||
const timing_3 = (end - start) / bench_loops
|
||||
console.log('CompactFheUint32List serialization bench: ', timing_3, ' ms');
|
||||
bench_results["compact_fheunit32_list_serialization_mean"] = timing_3;
|
||||
|
||||
return bench_results;
|
||||
}
|
||||
|
||||
async function compactPublicKeyBench32BitBig() {
|
||||
const block_params = new ShortintParameters(ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_COMPACT_PK);
|
||||
let config = TfheConfigBuilder.all_disabled()
|
||||
.enable_custom_integers(block_params)
|
||||
.build();
|
||||
return append_param_name(
|
||||
await compactPublicKeyBench32BitOnConfig(config),
|
||||
"PARAM_MESSAGE_2_CARRY_2_COMPACT_PK"
|
||||
);
|
||||
}
|
||||
|
||||
async function compactPublicKeyBench32BitSmall() {
|
||||
const block_params = new ShortintParameters(ShortintParametersName.PARAM_SMALL_MESSAGE_2_CARRY_2_COMPACT_PK);
|
||||
let config = TfheConfigBuilder.all_disabled()
|
||||
.enable_custom_integers(block_params)
|
||||
.build();
|
||||
return append_param_name(
|
||||
await compactPublicKeyBench32BitOnConfig(config),
|
||||
"PARAM_SMALL_MESSAGE_2_CARRY_2_COMPACT_PK"
|
||||
);
|
||||
}
|
||||
|
||||
async function compactPublicKeyTest256BitOnConfig(config) {
|
||||
console.time('ClientKey Gen')
|
||||
let clientKey = TfheClientKey.generate(config);
|
||||
@@ -181,8 +265,7 @@ async function compactPublicKeyTest256BitOnConfig(config) {
|
||||
|
||||
assert_eq(encrypted_list.length, values.length);
|
||||
|
||||
for (let i = 0; i < values.length; i++)
|
||||
{
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
let decrypted = encrypted_list[i].decrypt(clientKey);
|
||||
assert_eq(decrypted, values[i]);
|
||||
}
|
||||
@@ -195,8 +278,7 @@ async function compactPublicKeyTest256BitOnConfig(config) {
|
||||
let encrypted_list = deserialized_list.expand();
|
||||
assert_eq(encrypted_list.length, values.length);
|
||||
|
||||
for (let i = 0; i < values.length; i++)
|
||||
{
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
let decrypted = encrypted_list[i].decrypt(clientKey);
|
||||
assert_eq(decrypted, values[i]);
|
||||
}
|
||||
@@ -248,8 +330,7 @@ async function compressedCompactPublicKeyTest256BitOnConfig(config) {
|
||||
|
||||
assert_eq(encrypted_list.length, values.length);
|
||||
|
||||
for (let i = 0; i < values.length; i++)
|
||||
{
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
let decrypted = encrypted_list[i].decrypt(clientKey);
|
||||
assert_eq(decrypted, values[i]);
|
||||
}
|
||||
@@ -262,8 +343,7 @@ async function compressedCompactPublicKeyTest256BitOnConfig(config) {
|
||||
let encrypted_list = deserialized_list.expand();
|
||||
assert_eq(encrypted_list.length, values.length);
|
||||
|
||||
for (let i = 0; i < values.length; i++)
|
||||
{
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
let decrypted = encrypted_list[i].decrypt(clientKey);
|
||||
assert_eq(decrypted, values[i]);
|
||||
}
|
||||
@@ -285,7 +365,83 @@ async function compressedCompactPublicKeyTest256BitSmall() {
|
||||
await compressedCompactPublicKeyTest256BitOnConfig(config)
|
||||
}
|
||||
|
||||
async function compactPublicKeyBench256BitOnConfig(config) {
|
||||
const bench_loops = 100;
|
||||
let bench_results = {}
|
||||
|
||||
console.time('ClientKey Gen')
|
||||
let clientKey = TfheClientKey.generate(config);
|
||||
console.timeEnd('ClientKey Gen')
|
||||
|
||||
// Generate PK for encryption for later
|
||||
console.time('CompactPublicKey Gen')
|
||||
let publicKey = TfheCompactPublicKey.new(clientKey);
|
||||
console.timeEnd('CompactPublicKey Gen')
|
||||
|
||||
// Bench the pk generation for bench_loops iterations
|
||||
let start = performance.now();
|
||||
for (let i = 0; i < bench_loops; i++) {
|
||||
let _ = TfheCompactPublicKey.new(clientKey);
|
||||
}
|
||||
let end = performance.now();
|
||||
const timing_1 = (end - start) / bench_loops
|
||||
console.log('CompactPublicKey Gen bench: ', timing_1, ' ms');
|
||||
bench_results["compact_public_key_gen_256bit_mean"] = timing_1;
|
||||
|
||||
let values = [0, 1, 2, 2394, U32_MAX].map((e) => BigInt(e));
|
||||
|
||||
// Encrypt compact CT list for serialization for later
|
||||
console.time('CompactFheUint256List Encrypt')
|
||||
let compact_list = CompactFheUint256List.encrypt_with_compact_public_key(values, publicKey);
|
||||
console.timeEnd('CompactFheUint256List Encrypt')
|
||||
|
||||
// Bench the encryption for bench_loops iterations
|
||||
start = performance.now();
|
||||
for (let i = 0; i < bench_loops; i++) {
|
||||
let _ = CompactFheUint256List.encrypt_with_compact_public_key(values, publicKey);
|
||||
}
|
||||
end = performance.now();
|
||||
const timing_2 = (end - start) / bench_loops
|
||||
console.log('CompactFheUint256List Encrypt bench: ', timing_2, ' ms');
|
||||
bench_results["compact_fheunit256_list_encrypt_mean"] = timing_2;
|
||||
|
||||
let serialized_list = compact_list.serialize();
|
||||
console.log("Serialized CompactFheUint256List size: ", serialized_list.length);
|
||||
|
||||
// Bench the serialization for bench_loops iterations
|
||||
start = performance.now();
|
||||
for (let i = 0; i < bench_loops; i++) {
|
||||
let _ = compact_list.serialize();
|
||||
}
|
||||
end = performance.now();
|
||||
const timing_3 = (end - start) / bench_loops
|
||||
console.log('CompactFheUint256List serialization bench: ', timing_3, ' ms');
|
||||
bench_results["compact_fheunit256_list_serialization_mean"] = timing_3;
|
||||
|
||||
return bench_results
|
||||
}
|
||||
|
||||
async function compactPublicKeyBench256BitBig() {
|
||||
const block_params = new ShortintParameters(ShortintParametersName.PARAM_MESSAGE_2_CARRY_2_COMPACT_PK);
|
||||
let config = TfheConfigBuilder.all_disabled()
|
||||
.enable_custom_integers(block_params)
|
||||
.build();
|
||||
return append_param_name(
|
||||
await compactPublicKeyBench256BitOnConfig(config),
|
||||
"PARAM_MESSAGE_2_CARRY_2_COMPACT_PK"
|
||||
);
|
||||
}
|
||||
|
||||
async function compactPublicKeyBench256BitSmall() {
|
||||
const block_params = new ShortintParameters(ShortintParametersName.PARAM_SMALL_MESSAGE_2_CARRY_2_COMPACT_PK);
|
||||
let config = TfheConfigBuilder.all_disabled()
|
||||
.enable_custom_integers(block_params)
|
||||
.build();
|
||||
return append_param_name(
|
||||
await compactPublicKeyBench256BitOnConfig(config),
|
||||
"PARAM_SMALL_MESSAGE_2_CARRY_2_COMPACT_PK"
|
||||
);
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await init()
|
||||
@@ -301,6 +457,10 @@ async function main() {
|
||||
compactPublicKeyTest256BitBig,
|
||||
compressedCompactPublicKeyTest256BitSmall,
|
||||
compressedCompactPublicKeyTest256BitBig,
|
||||
compactPublicKeyBench32BitBig,
|
||||
compactPublicKeyBench32BitSmall,
|
||||
compactPublicKeyBench256BitBig,
|
||||
compactPublicKeyBench256BitSmall,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user