Skip to content

Commit

Permalink
feat: add test and export encrypt4
Browse files Browse the repository at this point in the history
  • Loading branch information
immortal-tofu committed Feb 19, 2024
1 parent 57cf92f commit 43a1b3f
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
24 changes: 23 additions & 1 deletion src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import {
FheUint4,
FheUint8,
FheUint16,
FheUint32,
FheUint64,
CompactFheUint4List,
CompactFheUint8List,
CompactFheUint16List,
CompactFheUint32List,
Expand All @@ -11,7 +13,7 @@ import {
TfheClientKey,
} from 'node-tfhe';
import { createTfheKeypair } from '../tfhe';
import { encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';
import { encrypt4, encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';

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

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

it('encrypt/decrypt 4bits', async () => {
const buffer = encrypt4(7, publicKey);
const compactList = CompactFheUint4List.deserialize(buffer);
let encryptedList = compactList.expand();
encryptedList.forEach((v: FheUint4) => {
const decrypted = v.decrypt(clientKey);
expect(decrypted).toBe(7);
});
});

it('encrypt/decrypt 0 8bits', async () => {
const buffer = encrypt8(0, publicKey);
const compactList = CompactFheUint8List.deserialize(buffer);
Expand Down
12 changes: 11 additions & 1 deletion src/sdk/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { TfheCompactPublicKey } from 'node-tfhe';
import sodium from 'libsodium-wrappers';
import { encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';
import { encrypt4, encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';
import {
EIP712,
GeneratePublicKeyParams,
Expand All @@ -11,6 +11,7 @@ import { fromHexString, isAddress, toHexString } from '../utils';
import { ContractKeypairs } from './types';

export type FhevmInstance = {
encrypt4: (value: number) => Uint8Array;
encrypt8: (value: number) => Uint8Array;
encrypt16: (value: number) => Uint8Array;
encrypt32: (value: number) => Uint8Array;
Expand Down Expand Up @@ -107,6 +108,15 @@ export const createInstance = async (

return {
// Parameters
encrypt4(value) {
if (value == null) throw new Error('Missing value');
if (typeof value !== 'number') throw new Error('Value must be a number');
if (!tfheCompactPublicKey)
throw new Error(
'Your instance has been created without the public blockchain key',
);
return encrypt4(value, tfheCompactPublicKey);
},
encrypt8(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 43a1b3f

Please sign in to comment.