npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tsseal

v0.34.0

Published

A TypeScript library for performing homomorphic encryption using the SEAL library from Microsoft Research using the BFV scheme only for exact computations on encrypted integers and is best suited for applications requiring high precision and discrete oper

Downloads

406

Readme

TSSEAL Usage Guide

Overview

TSSEAL is a TypeScript-based library designed for advanced encryption utilities using homomorphic encryption leveraging SEAL. This guide explains how to:

  1. Install the library.
  2. Enable debugging.
  3. Use it effectively in an Angular project.

TSSEAL enables computations on encrypted data directly producing encrypted results that, when decrypted, match the output of operations performed on plaintext. It is efficient for polynomial arithmetic and modular arithmetic implementations. Currently it only upports BFV (Brakerski/Fan-Vercauteren) but is roadmapped to support CKKS (Cheon-Kim-Kim-Song) encryption schemes. Compliant with security standards such as IND-CPA (Indistinguishability under Chosen Plaintext Attack).

SEAL’s encryption schemes rely on the Ring Learning with Errors (RLWE) problem, which is a lattice-based cryptographic problem. This problem is believed to be difficult to solve, even for quantum computers, making it a strong foundation for security.

NOTE: The actual encrypted value is stored as polynomial coefficients within the CipherText object. These coefficients are not directly exposed in a view but are managed internally within any instance.

Applications:

  • Privacy-preserving machine learning.
  • Secure data analysis.
  • Federated learning.
  • Encrypted database queries.

Installation

To install the library, use the following command:

npm install tsseal

Component Setup

Import

Import the library with

import { TSSEAL } from 'tsseal';

Initialization

The library has to be initialized prior to calls being made. To do so do the following

export class AppComponent implements OnInit {
  sealLib: TSSEAL;

  constructor() {
    this.sealLib = new TSSEAL();
  }

  ngOnInit() {
    this.initializeLibrary();
  }

  async initializeLibrary(): Promise<void> {
    await this.sealLib.initializeLibrary();
    console.log('SEAL Library initialized successfully');
  }

Debugging

Basic console logging is enable by appending an option boolean set to true. Check the examples below for more.

Examples

Encrypt a single number

The encrypt function takes a number as input, converts it into a format that can be securely encrypted, and then encrypts it. This encrypted data can then be safely stored, transmitted, or used for computations without exposing the original value.

<button (click)="testEncryption()">Test Encryption</button>

async testEncryption() {
    console.clear();
    const input = 1234;
    console.log('Input:', input);
    const encrypted = await this.encrypt(input);
    console.log('Encrypted:', encrypted);
    const decrypted = await this.decrypt(encrypted);
    console.log('Decrypted:', decrypted);
    }

    async encrypt(input: number): Promise<any> {
    return this.sealLib.encrypt(input, true);
}

Add multiple numbers together

<button (click)="testAddition()">Test Addition</button>

async testAddition() {
    console.clear();
    const inputs = [1, 0, 0, 4, 5]; // Example array of numbers
    console.log('Inputs:', inputs);
    // Encrypt all inputs
    const encryptedInputs = [];
    for (const input of inputs) {
        encryptedInputs.push(await this.encrypt(input));
    }

    // Add all ciphertexts
    const sum = await this.sealLib.addCiphertexts(encryptedInputs, true);

    console.log('Sum:', sum);

    // Decrypt the sum
    const decryptedSum = await this.decrypt(sum);
    console.log('Decrypted Sum:', decryptedSum);
}

Add a number to an encrypted one

<button (click)="testAddPlainToCiphertext()">Test Add Plain To Ciphertext</button>

async testAddPlainToCiphertext() {
    console.clear();
    const input = 40;
    console.log('Input:', input);
    const encrypted = await this.encrypt(input);
    console.log('Encrypted:', encrypted);

    const plain = 15;
    console.log('Plain:', plain);
    const sum = await this.sealLib.addPlainToCiphertext(encrypted, plain, true);
    console.log('Sum:', sum);

    const decryptedSum = await this.decrypt(sum);
    console.log('Decrypted Sum:', decryptedSum);
}

Subtract encrypted values from each other

<button (click)="testSubtractCiphertexts()">Test Subtract Ciphertexts</button>

async testSubtractCiphertexts() {
    console.clear();
    const inputs = [30, 10, 5]; // Array of numbers to subtract

    // Encrypt all inputs
    const encryptedInputs = [];
    for (const input of inputs) {
        const encrypted = await this.encrypt(input);
        console.log(`Encrypted input (${input}):`, encrypted);
        encryptedInputs.push(encrypted);
    }

    // Subtract all encrypted values
    try {
        const difference = await this.sealLib.subtractCiphertexts(encryptedInputs, true);
        console.log('Subtraction Result (CipherText):', difference);

        // Decrypt the result
        const decryptedDifference = await this.decrypt(difference);
        console.log('Decrypted Difference:', decryptedDifference);
    } catch (error) {
        console.error('Error during subtraction:', error);
    }
}

Subtract a number from an encrypted one

<button (click)="testSubtractPlainFromCiphertext()">Test Subtract Plain From Ciphertext</button>

async testSubtractPlainFromCiphertext() {
    console.clear();
    const cipherInput = 50; // Ciphertext input
    const plainInput = 20;  // Plain value to subtract

    // Encrypt the cipher input
    const encryptedInput = await this.encrypt(cipherInput);
    console.log(`Encrypted input (${cipherInput}):`, encryptedInput);

    // Subtract the plain value from the ciphertext
    try {
        const difference = await this.sealLib.subtractPlainFromCiphertext(encryptedInput, plainInput, true);
        console.log(`Subtraction Result (CipherText):`, difference);

        // Decrypt the result to verify correctness
        const decryptedDifference = await this.decrypt(difference);
        console.log(`Decrypted Difference (${cipherInput} - ${plainInput}):`, decryptedDifference);
    } catch (error) {
        console.error('Error during subtractPlainFromCiphertext:', error);
    }
}

Get the current noise budget

<button (click)="testNoiseBudget()">Test Noise Budget</button>

async testNoiseBudget() {
    console.clear();
    const input = 1234;
    const encrypted = await this.encrypt(input);
    console.log('Encrypted:', encrypted);

    const noiseBudget = await this.sealLib.getNoiseBudget(encrypted);
    console.log('Noise Budget:', noiseBudget);
}