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", diff --git a/src/sdk/encrypt.ts b/src/sdk/encrypt.ts index 76e1953..7f4647c 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,22 @@ export const createEncryptedInput = inputProof: fromHexString(inputProof), }; }, + async encrypt() { + let start = Date.now(); + const ciphertext = await this._prove(); + 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; + }, }; };