Skip to content

Commit

Permalink
Merge pull request #50 from zama-ai/feat/support-64bits-reencrypt
Browse files Browse the repository at this point in the history
Feat/support 64bits reencrypt
  • Loading branch information
immortal-tofu authored Feb 8, 2024
2 parents e29906b + b2f3fe6 commit 1456a21
Show file tree
Hide file tree
Showing 8 changed files with 390 additions and 220 deletions.
572 changes: 371 additions & 201 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhevmjs",
"version": "0.4.0-1",
"version": "0.4.0-2",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.cjs",
"types": "lib/node.d.ts",
Expand Down Expand Up @@ -39,13 +39,13 @@
},
"homepage": "https://github.com/zama-ai/fhevmjs#readme",
"dependencies": {
"bigint-buffer": "^1.1.5",
"commander": "^11.0.0",
"crypto-js": "^4.1.1",
"ethers": "^6.6.4",
"libsodium": "^0.7.11",
"libsodium-wrappers": "^0.7.11",
"node-tfhe": "^0.5.1",
"sha3": "^2.1.4",
"tfhe": "^0.5.1"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions src/sdk/decrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ describe('decrypt', () => {
'hex',
);
const cleartext = decrypt(keypair, ciphertext);
expect(cleartext).toBe(value);
expect(cleartext.toString()).toBe(`${value}`);
});

it('decrypts a Uint8Array value', async () => {
Expand All @@ -29,6 +29,6 @@ describe('decrypt', () => {
keypair.publicKey,
);
const cleartext = decrypt(keypair, ciphertext);
expect(cleartext).toBe(value);
expect(cleartext.toString()).toBe(`${value}`);
});
});
6 changes: 3 additions & 3 deletions src/sdk/decrypt.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import sodium from 'libsodium-wrappers';
import { bytesToNumber, fromHexString } from '../utils';
import { bytesToBigInt, fromHexString } from '../utils';
import { ContractKeypair } from './types';

export const decrypt = (
keypair: ContractKeypair,
ciphertext: string | Uint8Array,
): number => {
): bigint => {
const toDecrypt =
typeof ciphertext === 'string' ? fromHexString(ciphertext) : ciphertext;
const decrypted = sodium.crypto_box_seal_open(
toDecrypt,
keypair.publicKey,
keypair.privateKey,
);
return bytesToNumber(decrypted);
return bytesToBigInt(decrypted);
};
4 changes: 2 additions & 2 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ describe('token', () => {
);

const cleartext = instance.decrypt(contractAddress, ciphertext);
expect(cleartext).toBe(value);
expect(cleartext.toString()).toBe(`${value}`);
});

it('controls encrypt', async () => {
Expand Down Expand Up @@ -202,6 +202,6 @@ describe('token', () => {
'hex',
);
const cleartext = instance.decrypt(contractAddress, ciphertext);
expect(cleartext).toBe(value);
expect(cleartext.toString()).toBe(`${value}`);
});
});
2 changes: 1 addition & 1 deletion src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export type FhevmInstance = {
contractAddress: string,
) => { publicKey: Uint8Array; signature: string } | null;
hasKeypair: (contractAddress: string) => boolean;
decrypt: (contractAddress: string, ciphertext: string) => number;
decrypt: (contractAddress: string, ciphertext: string) => bigint;
serializeKeypairs: () => ExportedContractKeypairs;
};

Expand Down
10 changes: 5 additions & 5 deletions src/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { numberToBytes, bytesToNumber } from './utils';
import { numberToBytes, bytesToBigInt } from './utils';

describe('decrypt', () => {
it('converts a number to bytes', async () => {
Expand All @@ -13,11 +13,11 @@ describe('decrypt', () => {

it('converts bytes to number', async () => {
const value = new Uint8Array([23, 200, 15]);
const bytes = bytesToNumber(value);
expect(bytes).toBe(1558543);
const bytes = bytesToBigInt(value);
expect(bytes.toString()).toBe('1558543');

const value2 = new Uint8Array();
const bytes2 = bytesToNumber(value2);
expect(bytes2).toBe(0);
const bytes2 = bytesToBigInt(value2);
expect(bytes2.toString()).toBe('0');
});
});
8 changes: 4 additions & 4 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import sha3 from 'sha3';
import { toBigIntBE } from 'bigint-buffer';

export const fromHexString = (hexString: string): Uint8Array => {
const arr = hexString.replace(/^(0x)/, '').match(/.{1,2}/g);
Expand All @@ -21,15 +21,15 @@ export const numberToBytes = (uint32Value: number) => {
return byteArray;
};

export const bytesToNumber = function (byteArray: Uint8Array): number {
export const bytesToBigInt = function (byteArray: Uint8Array): bigint {
if (!byteArray || byteArray?.length === 0) {
return 0;
return BigInt(0);
}

const length = byteArray.length;

const buffer = Buffer.from(byteArray);
const result = buffer.readUIntBE(0, length);
const result = toBigIntBE(buffer);

return result;
};
Expand Down

0 comments on commit 1456a21

Please sign in to comment.