Skip to content

Commit

Permalink
Merge pull request #77 from zama-ai/fix-copro
Browse files Browse the repository at this point in the history
fix: copro send()
  • Loading branch information
immortal-tofu authored Jun 25, 2024
2 parents 7a49c24 + 78136e4 commit a3f3365
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 17 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.5.0-10",
"version": "0.5.0-11",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.js",
"types": "lib/node/node.d.ts",
Expand Down
8 changes: 4 additions & 4 deletions src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe('encrypt', () => {
input.add128(BigInt(233938932390));
input.addAddress('0xa5e1defb98EFe38EBb2D958CEe052410247F4c80');
const buffer = input.encrypt();
const compactList = CompactFheUint160List.deserialize(buffer.data);
const compactList = CompactFheUint160List.deserialize(buffer.inputProof);
let encryptedList = compactList.expand();
expect(encryptedList.length).toBe(8);
encryptedList.forEach((v: FheUint160, i: number) => {
Expand Down Expand Up @@ -81,7 +81,7 @@ describe('encrypt', () => {
);
input.add128(BigInt(0));
const buffer = input.encrypt();
const compactList = CompactFheUint160List.deserialize(buffer.data);
const compactList = CompactFheUint160List.deserialize(buffer.inputProof);
let encryptedList = compactList.expand();
expect(encryptedList.length).toBe(1);
encryptedList.forEach((v: FheUint160, i: number) => {
Expand All @@ -103,7 +103,7 @@ describe('encrypt', () => {
data.set([255], 63);
input.addBytes256(data);
const buffer = input.encrypt();
const compactList = CompactFheUint2048List.deserialize(buffer.data);
const compactList = CompactFheUint2048List.deserialize(buffer.inputProof);
let encryptedList = compactList.expand();
expect(encryptedList.length).toBe(1);
encryptedList.forEach((v: FheUint2048, i: number) => {
Expand Down Expand Up @@ -240,4 +240,4 @@ describe('encrypt', () => {
// expect(res.contractAddress).toBe('0x8ba1f109551bD432803012645Ac136ddd64DBA72');
// expect(res.signature).toBeDefined();
// });
// });
// });
41 changes: 31 additions & 10 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
CompactFheUint2048List,
} from 'node-tfhe';

import { bytesToBigInt, toHexString } from '../utils';
import { bytesToBigInt, fromHexString, toHexString } from '../utils';
import { ENCRYPTION_TYPES } from './encryptionTypes';
import { fetchJSONRPC } from '../ethCall';

Expand All @@ -25,10 +25,10 @@ export type ZKInput = {
getBits: () => number[];
resetValues: () => ZKInput;
encrypt: () => {
inputs: Uint8Array[];
data: Uint8Array;
handles: Uint8Array[];
inputProof: Uint8Array;
};
send: () => Promise<{ inputs: string[]; signature: string }>;
send: () => Promise<{ handles: Uint8Array[]; inputProof: Uint8Array }>;
};

const checkEncryptedValue = (value: number | bigint, bits: number) => {
Expand Down Expand Up @@ -166,17 +166,17 @@ export const createEncryptedInput =
);
}

const data = encrypted.serialize();
const inputProof = encrypted.serialize();
const hash = createKeccakHash('keccak256')
.update(Buffer.from(data))
.update(Buffer.from(inputProof))
.digest();
// const encrypted = ProvenCompactFheUint160List.encrypt_with_compact_public_key(
// values,
// publicZkParams,
// publicKey,
// ZkComputeLoad.Proof,
// );
const inputs = bits.map((v, i) => {
const handles = bits.map((v, i) => {
const dataWithIndex = new Uint8Array(hash.length + 1);
dataWithIndex.set(hash, 0);
dataWithIndex.set([i], hash.length);
Expand All @@ -189,8 +189,8 @@ export const createEncryptedInput =
return dataInput;
});
return {
inputs,
data,
handles,
inputProof,
};
},
async send() {
Expand Down Expand Up @@ -223,7 +223,28 @@ export const createEncryptedInput =
},
body: JSON.stringify(payload),
};
return await fetchJSONRPC(coprocessorUrl, options);
const resJson = await fetchJSONRPC(coprocessorUrl, options);
const inputProof = convertToInputProof(resJson);
return {
handles: resJson.handles.map((handle: string) =>
fromHexString(handle),
),
inputProof: fromHexString(inputProof),
};
},
};
};

const convertToInputProof = (data: {
handlesList: string[];
signature: string;
}) => {
const { handlesList, signature } = data;
const lengthByte = handlesList.length.toString(16).padStart(2, '0');
const handlesString = handlesList
.map((handle: string) => handle.slice(2))
.join('');
const signatureString = signature.slice(2);
const inputProof = `0x${lengthByte}${handlesString}${signatureString}`;
return inputProof;
};

0 comments on commit a3f3365

Please sign in to comment.