Skip to content

Commit

Permalink
Merge pull request #145 from zama-ai/split-encrypt
Browse files Browse the repository at this point in the history
Add timings on encrypt
  • Loading branch information
immortal-tofu authored Dec 5, 2024
2 parents bb178f9 + c2e158d commit d92a19f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
37 changes: 32 additions & 5 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,12 @@ export type ZKInput = {
addBytes256: (value: Uint8Array) => ZKInput;
addAddress: (value: string) => ZKInput;
getBits: () => number[];
_getClosestPP: () => EncryptionTypes;
_prove: () => Promise<Buffer>;
_verify: (ciphertext: Buffer) => Promise<{
handles: Uint8Array[];
inputProof: Uint8Array;
}>;
encrypt: () => Promise<{
handles: Uint8Array[];
inputProof: Uint8Array;
Expand Down Expand Up @@ -219,13 +225,11 @@ export const createEncryptedInput =
getBits() {
return bits;
},
async encrypt() {
_getClosestPP() {
const getKeys = <T extends {}>(obj: T) =>
Object.keys(obj) as Array<keyof T>;

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,
Expand All @@ -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);
Expand All @@ -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,
Expand Down Expand Up @@ -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;
},
};
};

Expand Down

0 comments on commit d92a19f

Please sign in to comment.