Skip to content

Commit

Permalink
Merge pull request #58 from zama-ai/feat/encrypt-address
Browse files Browse the repository at this point in the history
Feat/encrypt address
  • Loading branch information
immortal-tofu authored Mar 7, 2024
2 parents 434b595 + e0b59ab commit af54f37
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 3 deletions.
4 changes: 4 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@
"./web": {
"import": "./lib/web.js",
"require": "./lib/web.js"
},
"./node": {
"import": "./lib/node.cjs",
"require": "./lib/node.cjs"
}
},
"scripts": {
Expand Down
33 changes: 31 additions & 2 deletions src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CompactFheUint16List,
CompactFheUint32List,
CompactFheUint64List,
CompactFheUint160List,
TfheCompactPublicKey,
TfheClientKey,
} from 'node-tfhe';
Expand All @@ -21,9 +22,10 @@ import {
encrypt16,
encrypt32,
encrypt64,
encryptAddress,
} from './encrypt';

describe('encrypt8', () => {
describe('encrypt', () => {
let clientKey: TfheClientKey;
let publicKey: TfheCompactPublicKey;

Expand Down Expand Up @@ -153,7 +155,7 @@ describe('encrypt8', () => {
});
});

it('encrypt/decrypt 64bits', async () => {
it('encrypt/decrypt bigint 64bits', async () => {
const buffer = encrypt64(BigInt('18446744073709551615'), publicKey);
const compactList = CompactFheUint64List.deserialize(buffer);
let encryptedList = compactList.expand();
Expand All @@ -162,4 +164,31 @@ describe('encrypt8', () => {
expect(decrypted.toString()).toBe('18446744073709551615');
});
});
it('encrypt/decrypt 0x000... 160bits', async () => {
const buffer = encryptAddress(
'0x0000000000000000000000000000000000000000',
publicKey,
);
const compactList = CompactFheUint160List.deserialize(buffer);
let encryptedList = compactList.expand();
encryptedList.forEach((v: FheUint64) => {
const decrypted = v.decrypt(clientKey);
expect(decrypted.toString()).toBe('0');
});
});

it('encrypt/decrypt 160bits', async () => {
const buffer = encryptAddress(
'0x8ba1f109551bd432803012645ac136ddd64dba72',
publicKey,
);
const compactList = CompactFheUint160List.deserialize(buffer);
let encryptedList = compactList.expand();
encryptedList.forEach((v: FheUint64) => {
const decrypted = v.decrypt(clientKey);
expect(decrypted.toString()).toBe(
'797161134358056856230896843146392277790002887282',
);
});
});
});
14 changes: 14 additions & 0 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import {
CompactFheUint16List,
CompactFheUint32List,
CompactFheUint64List,
CompactFheUint160List,
} from 'node-tfhe';
import { fromHexString } from '../utils';

export const encrypt4 = (
value: number,
Expand Down Expand Up @@ -78,3 +80,15 @@ export const encrypt64 = (
);
return encrypted.serialize();
};

export const encryptAddress = (
value: string,
publicKey: TfheCompactPublicKey,
): Uint8Array => {
// value is something like 0x8ba1f109551bd432803012645ac136ddd64dba72
const encrypted = CompactFheUint160List.encrypt_with_compact_public_key(
[BigInt(value)],
publicKey,
);
return encrypted.serialize();
};
10 changes: 10 additions & 0 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,10 @@ describe('index', () => {
expect(instance.encrypt32(BigInt(838392))).toBeTruthy();
expect(instance.encrypt64(BigInt(3433434343))).toBeTruthy();

expect(
instance.encryptAddress('0x8ba1f109551bd432803012645ac136ddd64dba72'),
).toBeTruthy();

expect(() => instance.encryptBool(undefined as any)).toThrow(
'Missing value',
);
Expand Down Expand Up @@ -145,6 +149,12 @@ describe('index', () => {
expect(() => instance.encrypt64('wrong value' as any)).toThrow(
'Value must be a number or a bigint.',
);
expect(() => instance.encryptAddress('wrong value' as any)).toThrow(
'Value must be a valid address.',
);
expect(() => instance.encryptAddress(BigInt(32) as any)).toThrow(
'Value must be a string.',
);

// Check limit
expect(instance.encryptBool(1)).toBeTruthy();
Expand Down
17 changes: 17 additions & 0 deletions src/sdk/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
encrypt16,
encrypt32,
encrypt64,
encryptAddress,
encryptBool,
} from './encrypt';
import {
Expand All @@ -24,6 +25,7 @@ export type FhevmInstance = {
encrypt16: (value: number | bigint) => Uint8Array;
encrypt32: (value: number | bigint) => Uint8Array;
encrypt64: (value: number | bigint) => Uint8Array;
encryptAddress: (value: string) => Uint8Array;
generatePublicKey: (
options: GeneratePublicKeyParams & {
force?: boolean;
Expand Down Expand Up @@ -60,6 +62,11 @@ export type FhevmInstanceParams = {
keypairs?: ExportedContractKeypairs;
};

export const getPublicKeyCallParams = () => ({
to: '0x000000000000000000000000000000000000005d',
data: '0xd9d47bb001',
});

export const createInstance = async (
params: FhevmInstanceParams,
): Promise<FhevmInstance> => {
Expand Down Expand Up @@ -180,6 +187,16 @@ export const createInstance = async (
return encrypt64(value, tfheCompactPublicKey);
},

encryptAddress(value) {
if (!tfheCompactPublicKey)
throw new Error(
'Your instance has been created without the public blockchain key.',
);
if (typeof value !== 'string') throw new Error('Value must be a string.');
if (!isAddress(value)) throw new Error('Value must be a valid address.');
return encryptAddress(value, tfheCompactPublicKey);
},

// Reencryption
generatePublicKey(options) {
if (!options || !options.verifyingContract)
Expand Down
2 changes: 1 addition & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export const bytesToBigInt = function (byteArray: Uint8Array): bigint {
};

export const isAddress = function (address: string) {
if (/^(0x)?[0-9a-f]{40}$/i.test(address.toLowerCase())) {
if (address.match(/^0x[0-9a-fA-F]{40}$/)) {
// check if it has the basic requirements of an address
return true;
}
Expand Down

0 comments on commit af54f37

Please sign in to comment.