JSPM

  • Created
  • Published
  • Downloads 784
  • Score
    100M100P100Q101628F
  • License MIT

Microsoft SEAL in Javascript

Package Exports

  • node-seal

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (node-seal) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

node-seal

A zero-dependency Web Assembly port of the C++ Microsoft SEAL library.

Now supporting Microsoft SEAL 3.4.5

It contains high level functions to make using this library easy. However, we highly encourage you to use the lower level API which is very close to the C++ calls from Microsoft SEAL.

Demo

Go to morfix.io/sandbox

This sandbox was built for users to experiment and learn how to use Microsoft SEAL. It uses node-seal inside a WebWorker.

Not all functionality is implemented. For example:

  • No Integer Encoder is present - Almost everything can be implemented with the Batch Encoder.
  • Generating and attempting to download Relin/Galois Keys at PolyModulus Degree of 16384 with 128 security will result in a crash due to the browser running out of memory and the page will need to be refreshed.
  • Downloading/Uploading files
    • You may now export all keys and variables you have created in the browser!
    • All objects will be serialized to a similar format as an SSH key for readability

Microsoft SEAL

Microsoft SEAL is an easy-to-use open-source (MIT licensed) homomorphic encryption library developed by the Cryptography and Privacy Research group at Microsoft. Microsoft SEAL is written in modern standard C++ and has no external dependencies, making it easy to compile and run in many different environments. For more information about the Microsoft SEAL project, see sealcrypto.org.

This document pertains to Microsoft SEAL version 3.4. Users of previous versions of the library should look at the list of changes.

License

MIT license

Installation

npm:

npm install node-seal

yarn:

yarn add node-seal

Usage

There are a lot of assumptions made to help ease the burden of learning SEAL all at once. You can refer to the sample code below.

Checkout the basics

For those who are curious about the security of Microsoft SEAL, please refer to HomomorphicEncryption.org

Examples

Check out this simple example below or for a more in-depth example, look here.

For low level API examples, please check the 'manual-...' tests here.

Simple Example

This showcases the most basic use-case of initializing the library, encrypt data, performing a simple evaluation, and then decrypting the result. This example works in browsers as well as NodeJS.

(async () => {

  /*
    First, import the library.
    
    Second, create the parameters for encryption. This configures the library
    to only encrypt / evaluate / decrypt for a given context. These parameters
    can be customized for advanced users in order to fine tune performance.
    
    Third, generate Public / Secret keys. A Public key can be shared and is 
    used to encrypt data. A Secret key should not be shared and is used to 
    decrypt data.
  */
  // If in a browser, skip this next line
  // import { Seal } from 'node-seal'
  const { Seal } = require('node-seal')

  
  const Morfix = await Seal
  
  /*
    Create our parameters with the helper function.
    
    We are using a 'low' `computationLevel` because we are not expecting to
    perform multiple evaluations in a row. This setting reduces the time it
    takes to initialize the library, generate keys, encryption, evaluation, 
    and decryption. In addition, the `security` is set to be 128 bits.
    
    For a list of available settings, please review the full-example in 
    this repository.  
  */
  const parms = Morfix.createParams({computationLevel: 'low', security: 128})
  
  /*
    We are initializing the library with the parameters generated above and 
    finally initializign the library to compute over signed or unsigned Integer
    arithmetic (Int32Array / Uint32Array). 
  */
  Morfix.initialize({...parms, schemeType: 'BFV'})
  
  /*
    This function generates and sets the public and secret keys internally.
    Both keys have helper methods to save to a base64 string and reinitialize
    them from these strings. These strings can be very large.
  */
  Morfix.genKeys()
   
  /* 
    Encrypt some data. We are using TypedArrays for consistency. Here, we
    are using Int32Arrays, but could easily switch to UintArray32.
  */
  const cipherText_a = Morfix.encrypt({array: Int32Array.from([4, 5, 6])})
  const cipherText_b = Morfix.encrypt({array: Int32Array.from([1, 2, 3])})
  
  /* 
    Perform an `Evaluation` (ex homomorphic addition)
    We show 3 methods, but there are more available:
    1. `add`
    2. `sub`
    3. `multiply`
  */
  const sumCipher = Morfix.add({a: cipherText_a, b: cipherText_b})
  const subCipher = Morfix.sub({a: cipherText_a, b: cipherText_b})
  const productCipher = Morfix.multiply({a: cipherText_a, b: cipherText_b})
  

  /*
    Decrypt the cipher text results
  */
  const decryptedSum = Morfix.decrypt({cipherText: sumCipher})
  const decryptedSub = Morfix.decrypt({cipherText: subCipher})
  const decryptedMultiply = Morfix.decrypt({cipherText: productCipher})
  
  console.log('decryptedSum', decryptedSum)
  // decryptedSum Int32Array(3) [5, 7, 9]
  
  console.log('decryptedSub', decryptedSub)
  // decryptedSub Int32Array(3) [3, 3, 3]

  console.log('decryptedMultiply', decryptedMultiply)
  // decryptedMultiply Int32Array(3) [4, 10, 18]
  
})()

Changes

See the version changes

Testing

You can find the list of tests in package.json. They can be useful to see different parameters and how they affect execution time. Some of the tests will take a long time to complete and consume a lot of memory.

Caveats

Conversion from C++ to Web Assembly has some limitations. In addition, the SEAL library itself demands some constraints on the size of arrays as well as their max / min values.

See limitations

Contributing

See CONTRIBUTING.md.