diff --git a/src/sdk/encrypt.test.ts b/src/sdk/encrypt.test.ts index fb5aa22..35b7d61 100644 --- a/src/sdk/encrypt.test.ts +++ b/src/sdk/encrypt.test.ts @@ -1,4 +1,5 @@ import { + FheBool, FheUint4, FheUint8, FheUint16, @@ -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; @@ -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); diff --git a/src/sdk/encrypt.ts b/src/sdk/encrypt.ts index 7018f06..4472f48 100644 --- a/src/sdk/encrypt.ts +++ b/src/sdk/encrypt.ts @@ -1,5 +1,6 @@ import { TfheCompactPublicKey, + CompactFheBoolList, CompactFheUint4List, CompactFheUint8List, CompactFheUint16List, @@ -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, diff --git a/src/sdk/index.test.ts b/src/sdk/index.test.ts index 30a2526..4a63e2a 100644 --- a/src/sdk/index.test.ts +++ b/src/sdk/index.test.ts @@ -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(); @@ -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', ); diff --git a/src/sdk/index.ts b/src/sdk/index.ts index 7afd1fc..259aba1 100644 --- a/src/sdk/index.ts +++ b/src/sdk/index.ts @@ -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, @@ -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; @@ -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');