0.1.2 • Published 1 year ago

@zama-fhe/tfhe-js v0.1.2

Weekly downloads
-
License
BSD-3-Clause-Clea...
Repository
github
Last release
1 year ago

TFHE-js

TFHE-js is a JS wrapper for TFHE-rs client-side WASM API.

This package includes:

  • High-level API which simplifies usage of TFHE-rs WASM (at the moment, only number)
  • TFHE-rs native wasm export

Install

npm i --save @zama-fhe/tfhe-js
yarn add @zama-fhe/tfhe-js

Usage

Importing the cks from passphrase and private enc/dec

import { createKeyFromPassPhrase } from '@zama-fhe/tfhe-js';

const instance1 = await createKeyFromPassPhrase({ passPhrase: 'beyond sorry curtain ...', withPublicKey: false });
let input = 3n;
let c1 = instance1.encryptToBase64(input);
let plaintext = instance1.decryptFromBase64(c1);
expect(input).toBe(plaintext);

Importing the cks from passphrase and pub enc private dec

Another example, importing a base64 secret key:

import { createKeyFromPassPhrase } from '@zama-fhe/tfhe-js';

const instance1 = await createKeyFromPassPhrase({ passPhrase: 'beyond sorry curtain ...', withPublicKey: true });
let input = 3n;
let c1 = instance1.encryptWithPublicKeyToBase64(input);
let plaintext = instance1.decryptFromBase64(c1);
expect(input).toBe(plaintext);

Importing a key and create a Key object

import { importKey, Key } from '@zama-fhe/tfhe-js';

const createKey = (cks) => {
  let secretKey = importKey(cks, 'hex');
  let input = 3n;
  const key = new Key({ secretKey });
  return key;
};

Generate cks and pks from passphrase

import { createKeyFromPassPhrase } from '@zama-fhe/tfhe-js';

const instance1 = await createKeyFromPassPhrase({
  passPhrase: 'churn agent language session winner drip present morning skirt early fiscal grit',
  withPublicKey: true,
});
let pks = instance1.exportPublicKey('hex');
let cks = instance1.exportKey('hex');
fs.writeFileSync('alice_pks.hex', pks as string);
fs.writeFileSync('alice_cks.hex', cks as string);

Use directly TFHE-rs WASM binding

import { TFHERs } from '@zama-fhe/tfhe-js';

const importKey = (key) => {
  let serialized_cks = Buffer.from(key, 'base64');
  return TFHERs.Shortint.deserialize_shortint_client_key(serialized_cks);
};

Use browser version

To use the browser version, first you need to load the wasm. For this, you need to use the initSDK method.

import { initSDK, createKeyFromPassPhrase } from '@zama-fhe/tfhe-js/browser';

const generateKey = async (key) => {
  let path = undefined;
  await initSDK(path); // you can replace path with a custom path if you store the wasm on a specific address

  const key = await createKeyFromPassPhrase({
    passPhrase: 'churn agent language session winner drip present morning skirt early fiscal grit',
  });
  return key;
};

Development

Install dependencies

npm i

Run test

npm run test

Up to date pkg

To get the last version of this package which contains the wasm package and the JS API, check this repo that explains how to generate it.

#web
make build_web_js_api
cp pkg/tfhe* tfhe-rs/browser/

#node
make build_node_js_api
cp pkg/tfhe* tfhe-rs/node/

Once generated copy web version in vendors/pkg folder and the nodejs version into vendors/pkg/mocks folder.