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);
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.
make build_web_js_api
cp pkg/tfhe* tfhe-rs/browser/
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.