Skip to content

Commit

Permalink
Merge pull request #54 from zama-ai/feature/encrypt-bool
Browse files Browse the repository at this point in the history
feat: add encrypt bool
  • Loading branch information
immortal-tofu authored Feb 28, 2024
2 parents ff26268 + 41cab13 commit 7578ac6
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 2 deletions.
30 changes: 29 additions & 1 deletion src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
FheBool,
FheUint4,
FheUint8,
FheUint16,
Expand All @@ -13,7 +14,14 @@ import {
TfheClientKey,
} from 'node-tfhe';
import { createTfheKeypair } from '../tfhe';
import { encrypt4, encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';
import {
encryptBool,
encrypt4,
encrypt8,
encrypt16,
encrypt32,
encrypt64,
} from './encrypt';

describe('encrypt8', () => {
let clientKey: TfheClientKey;
Expand All @@ -25,6 +33,26 @@ describe('encrypt8', () => {
publicKey = keypair.publicKey;
});

it('encrypt/decrypt 0 bool', async () => {
const buffer = encryptBool(false, publicKey);
const compactList = CompactFheUint4List.deserialize(buffer);
let encryptedList = compactList.expand();
encryptedList.forEach((v: FheBool) => {
const decrypted = v.decrypt(clientKey);
expect(decrypted).toBe(0);
});
});

it('encrypt/decrypt bool', async () => {
const buffer = encryptBool(true, publicKey);
const compactList = CompactFheUint4List.deserialize(buffer);
let encryptedList = compactList.expand();
encryptedList.forEach((v: FheBool) => {
const decrypted = v.decrypt(clientKey);
expect(decrypted).toBe(1);
});
});

it('encrypt/decrypt 0 4bits', async () => {
const buffer = encrypt4(0, publicKey);
const compactList = CompactFheUint4List.deserialize(buffer);
Expand Down
12 changes: 12 additions & 0 deletions src/sdk/encrypt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
TfheCompactPublicKey,
CompactFheBoolList,
CompactFheUint4List,
CompactFheUint8List,
CompactFheUint16List,
Expand All @@ -19,6 +20,17 @@ export const encrypt4 = (
return encrypted.serialize();
};

export const encryptBool = (
value: boolean,
publicKey: TfheCompactPublicKey,
): Uint8Array => {
const encrypted = CompactFheBoolList.encrypt_with_compact_public_key(
[value],
publicKey,
);
return encrypted.serialize();
};

export const encrypt8 = (
value: number,
publicKey: TfheCompactPublicKey,
Expand Down
16 changes: 16 additions & 0 deletions src/sdk/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe('index', () => {
chainId: 1234,
publicKey: tfhePublicKey,
});
expect(instance.encryptBool).toBeDefined();
expect(instance.encrypt4).toBeDefined();
expect(instance.encrypt8).toBeDefined();
expect(instance.encrypt16).toBeDefined();
expect(instance.encrypt32).toBeDefined();
Expand Down Expand Up @@ -100,14 +102,28 @@ describe('index', () => {
chainId: 1234,
publicKey: tfhePublicKey,
});
expect(instance.encryptBool(true)).toBeTruthy();
expect(instance.encrypt4(2)).toBeTruthy();
expect(instance.encrypt8(34)).toBeTruthy();
expect(instance.encrypt16(344)).toBeTruthy();
expect(instance.encrypt32(3422)).toBeTruthy();
expect(instance.encrypt64(BigInt(34))).toBeTruthy();

expect(() => instance.encryptBool(undefined as any)).toThrow(
'Missing value',
);
expect(() => instance.encrypt4(undefined as any)).toThrow('Missing value');
expect(() => instance.encrypt8(undefined as any)).toThrow('Missing value');
expect(() => instance.encrypt16(undefined as any)).toThrow('Missing value');
expect(() => instance.encrypt32(undefined as any)).toThrow('Missing value');
expect(() => instance.encrypt64(undefined as any)).toThrow('Missing value');

expect(() => instance.encryptBool('wrong value' as any)).toThrow(
'Value must be a boolean',
);
expect(() => instance.encrypt4('wrong value' as any)).toThrow(
'Value must be a number',
);
expect(() => instance.encrypt8('wrong value' as any)).toThrow(
'Value must be a number',
);
Expand Down
20 changes: 19 additions & 1 deletion src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { TfheCompactPublicKey } from 'node-tfhe';
import sodium from 'libsodium-wrappers';
import { encrypt4, encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';
import {
encrypt4,
encrypt8,
encrypt16,
encrypt32,
encrypt64,
encryptBool,
} from './encrypt';
import {
EIP712,
GeneratePublicKeyParams,
Expand All @@ -11,6 +18,7 @@ import { fromHexString, isAddress, toHexString } from '../utils';
import { ContractKeypairs } from './types';

export type FhevmInstance = {
encryptBool: (value: boolean) => Uint8Array;
encrypt4: (value: number) => Uint8Array;
encrypt8: (value: number) => Uint8Array;
encrypt16: (value: number) => Uint8Array;
Expand Down Expand Up @@ -96,6 +104,16 @@ export const createInstance = async (

return {
// Parameters
encryptBool(value) {
if (value == null) throw new Error('Missing value');
if (typeof value !== 'boolean')
throw new Error('Value must be a boolean');
if (!tfheCompactPublicKey)
throw new Error(
'Your instance has been created without the public blockchain key',
);
return encryptBool(value, tfheCompactPublicKey);
},
encrypt4(value) {
if (value == null) throw new Error('Missing value');
if (typeof value !== 'number') throw new Error('Value must be a number');
Expand Down

0 comments on commit 7578ac6

Please sign in to comment.