Skip to content

Commit

Permalink
Merge pull request #49 from zama-ai/encrypt-64bits
Browse files Browse the repository at this point in the history
Encrypt 64bits
  • Loading branch information
immortal-tofu authored Feb 7, 2024
2 parents bd9172b + af180d9 commit e29906b
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 5 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fhevmjs",
"version": "0.4.0-0",
"version": "0.4.0-1",
"description": "fhEVM SDK for blockchain using TFHE",
"main": "lib/node.cjs",
"types": "lib/node.d.ts",
Expand Down
14 changes: 13 additions & 1 deletion src/sdk/encrypt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@ import {
FheUint8,
FheUint16,
FheUint32,
FheUint64,
CompactFheUint8List,
CompactFheUint16List,
CompactFheUint32List,
CompactFheUint64List,
TfheCompactPublicKey,
TfheClientKey,
} from 'node-tfhe';
import { createTfheKeypair } from '../tfhe';
import { encrypt8, encrypt16, encrypt32 } from './encrypt';
import { encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';

describe('encrypt8', () => {
let clientKey: TfheClientKey;
Expand Down Expand Up @@ -80,4 +82,14 @@ describe('encrypt8', () => {
expect(decrypted).toBe(30210);
});
});

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

export const encrypt8 = (
Expand Down Expand Up @@ -40,3 +41,15 @@ export const encrypt32 = (
);
return encrypted.serialize();
};

export const encrypt64 = (
value: number,
publicKey: TfheCompactPublicKey,
): Uint8Array => {
const uint64Array = new BigUint64Array([BigInt(value)]);
const encrypted = CompactFheUint64List.encrypt_with_compact_public_key(
uint64Array,
publicKey,
);
return encrypted.serialize();
};
9 changes: 8 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 } from './encrypt';
import { encrypt8, encrypt16, encrypt32, encrypt64 } from './encrypt';
import {
EIP712,
GeneratePublicKeyParams,
Expand All @@ -14,6 +14,7 @@ export type FhevmInstance = {
encrypt8: (value: number) => Uint8Array;
encrypt16: (value: number) => Uint8Array;
encrypt32: (value: number) => Uint8Array;
encrypt64: (value: number) => Uint8Array;
generateToken: (
options: GeneratePublicKeyParams & {
force?: boolean;
Expand Down Expand Up @@ -120,6 +121,12 @@ export const createInstance = async (
return encrypt32(value, tfheCompactPublicKey);
},

encrypt64(value) {
if (value == null) throw new Error('Missing value');
if (typeof value !== 'number') throw new Error('Value must be a number');
return encrypt64(value, tfheCompactPublicKey);
},

/**
* @deprecated Since version 0.3.0. Will be deleted in version 0.4.0. Use generatePublicKey instead.
*/
Expand Down

0 comments on commit e29906b

Please sign in to comment.