From 68a51632b9a42469367a3a954cdb628a3e219009 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Thu, 5 Dec 2024 07:13:39 +0100 Subject: [PATCH 1/3] feat: split encrypt in 2 steps to allow benchmarks --- src/sdk/encrypt.ts | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/sdk/encrypt.ts b/src/sdk/encrypt.ts index 76e1953..3885456 100644 --- a/src/sdk/encrypt.ts +++ b/src/sdk/encrypt.ts @@ -42,6 +42,12 @@ export type ZKInput = { addBytes256: (value: Uint8Array) => ZKInput; addAddress: (value: string) => ZKInput; getBits: () => number[]; + _getClosestPP: () => EncryptionTypes; + _prove: () => Promise; + _verify: (ciphertext: Buffer) => Promise<{ + handles: Uint8Array[]; + inputProof: Uint8Array; + }>; encrypt: () => Promise<{ handles: Uint8Array[]; inputProof: Uint8Array; @@ -219,13 +225,11 @@ export const createEncryptedInput = getBits() { return bits; }, - async encrypt() { + _getClosestPP() { const getKeys = (obj: T) => Object.keys(obj) as Array; const totalBits = bits.reduce((total, v) => total + v, 0); - const now = Date.now(); - // const ppTypes = getKeys(publicParams); const ppTypes = getKeys(publicParams); const closestPP: EncryptionTypes | undefined = ppTypes.find( (k) => Number(k) >= totalBits, @@ -237,8 +241,11 @@ export const createEncryptedInput = }.`, ); } + return closestPP; + }, + async _prove() { + const closestPP = this._getClosestPP(); const pp = publicParams[closestPP]!.publicParams; - const ppId = publicParams[closestPP]!.publicParamsId; const buffContract = fromHexString(contractAddress); const buffUser = fromHexString(callerAddress); const buffAcl = fromHexString(aclContractAddress); @@ -259,7 +266,11 @@ export const createEncryptedInput = const ciphertext = Buffer.from( encrypted.safe_serialize(SERIALIZED_SIZE_LIMIT_CIPHERTEXT), ); - + return ciphertext; + }, + async _verify(ciphertext: Buffer) { + const closestPP = this._getClosestPP(); + const ppId = publicParams[closestPP]!.publicParamsId; const payload = { contract_address: contractAddress, caller_address: callerAddress, @@ -324,6 +335,10 @@ export const createEncryptedInput = inputProof: fromHexString(inputProof), }; }, + async encrypt() { + const ciphertext = await this._prove(); + return this._verify(ciphertext); + }, }; }; From 87360e29248d0962d65da66eeda7121c4c019dba Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Thu, 5 Dec 2024 07:22:32 +0100 Subject: [PATCH 2/3] chore: add log for timings --- src/sdk/encrypt.ts | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/sdk/encrypt.ts b/src/sdk/encrypt.ts index 3885456..7f4647c 100644 --- a/src/sdk/encrypt.ts +++ b/src/sdk/encrypt.ts @@ -336,8 +336,20 @@ export const createEncryptedInput = }; }, async encrypt() { + let start = Date.now(); const ciphertext = await this._prove(); - return this._verify(ciphertext); + console.log( + `Encrypting and proving in ${ + Math.round((Date.now() - start) / 100) / 10 + }s`, + ); + + start = Date.now(); + const verification = await this._verify(ciphertext); + console.log( + `Verifying in ${Math.round((Date.now() - start) / 100) / 10}s`, + ); + return verification; }, }; }; From c2e158d8255e0eb7a03b969da0bd52313a2fe3a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cl=C3=A9ment=20=27birdy=27=20Danjou?= Date: Thu, 5 Dec 2024 07:30:28 +0100 Subject: [PATCH 3/3] 0.6.0-18 --- package-lock.json | 4 ++-- package.json | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/package-lock.json b/package-lock.json index e710971..80dbede 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "fhevmjs", - "version": "0.6.0-17", + "version": "0.6.0-18", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "fhevmjs", - "version": "0.6.0-17", + "version": "0.6.0-18", "license": "BSD-3-Clause-Clear", "dependencies": { "bigint-buffer": "^1.1.5", diff --git a/package.json b/package.json index f000532..5c1114f 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "fhevmjs", - "version": "0.6.0-17", + "version": "0.6.0-18", "description": "fhEVM SDK for blockchain using TFHE", "main": "lib/node.js", "types": "lib/node/node.d.ts",