Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fhevmjs fixes to work with local node #70

Merged
merged 5 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/ethCall.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ export const fetchJSONRPC = async (url: string, options: RequestInit) => {
if (data && data.result) {
// The result is usually prefixed with '0x' and is in hex format
const hexResult = data.result;
if (typeof hexResult == 'object') {
return hexResult;
}
const decodedBytes = decodeAbiBytes(hexResult);
return `0x${toHexString(decodedBytes)}`;
} else {
Expand Down
50 changes: 50 additions & 0 deletions src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
} from 'node-tfhe';
import { createTfheKeypair } from '../tfhe';
import { createEncryptedInput } from './encrypt';
import { fetchJSONRPC } from '../ethCall';
import { fromHexString } from '../utils';

describe('encrypt', () => {
let clientKey: TfheClientKey;
Expand Down Expand Up @@ -187,3 +189,51 @@ describe('encrypt', () => {
);
});
});

describe('encryptWithCoprocessor', () => {
let publicKey: TfheCompactPublicKey;
const coprocessorNode = 'http://127.0.0.1:8745';

beforeAll(async () => {
const pkeyOptions = {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
method: "eth_getPublicFhevmKey",
params: [],
id: 1,
jsonrpc: "2.0",
}),
};
const pkeyRes = await fetchJSONRPC(
coprocessorNode,
pkeyOptions,
);

publicKey = TfheCompactPublicKey.deserialize(fromHexString(pkeyRes.publicKey));
});

it('encrypt with coprocessor', async () => {
const input = createEncryptedInput(publicKey, coprocessorNode)(
'0x8ba1f109551bd432803012645ac136ddd64dba72',
'0xa5e1defb98EFe38EBb2D958CEe052410247F4c80',
);
input.addBool(BigInt(0));
input.add4(2);
input.add8(BigInt(43));
input.add16(BigInt(87));
input.add32(BigInt(2339389323));
input.add64(BigInt(23393893233));
//input.add128(BigInt(233938932390)); // 128 bits not supported yet in coprocessor
input.addAddress('0xa5e1defb98EFe38EBb2D958CEe052410247F4c80');

const res = await input.send();
expect(res.handlesList).toBeDefined();
expect(res.handlesList.length).toBe(7);
expect(res.callerAddress).toBe('0xa5e1defb98EFe38EBb2D958CEe052410247F4c80');
expect(res.contractAddress).toBe('0x8ba1f109551bD432803012645Ac136ddd64DBA72');
expect(res.signature).toBeDefined();
});
});
14 changes: 7 additions & 7 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -202,14 +202,16 @@ export const createEncryptedInput =
const ciphertext = encrypted.serialize();

const data = new Uint8Array(1 + bits.length + ciphertext.length);
data.set([data.length], 0);
data.set(bits, 1);
data.set(ciphertext, data.length + 1);
data.set([bits.length], 0);
bits.forEach((value, index) => {
data.set([ENCRYPTION_TYPES[value] & 0xff], 1 + index);
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is the "& 0xff" needed here?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ENCRYPTION_TYPES[value] should never exceed 255 from definition already

Copy link
Contributor Author

@david-zk david-zk Jun 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The value in that map is of type number which has a big range. I'm not sure how to best approach this, ideally this should be a compile time error, if someone adds a larger number things will "work" but write bad values.

});
data.set(ciphertext, bits.length + 1);

const payload = {
jsonrpc: '2.0',
method: 'eth_addUserCiphertext',
params: [toHexString(data), contractAddress, callerAddress],
params: ['0x' + toHexString(data), contractAddress, callerAddress],
id: 1,
};

Expand All @@ -221,9 +223,7 @@ export const createEncryptedInput =
},
body: JSON.stringify(payload),
};
const response = await fetchJSONRPC(coprocessorUrl, options);
if (!response) throw new Error('Invalid input');
return JSON.parse(response);
return await fetchJSONRPC(coprocessorUrl, options);
},
};
};
Loading