Homomorphic encryption for TypeScript and JavaScript, powered by Microsoft SEAL 4.7.2 and WebAssembly. Compute on encrypted data, then decrypt the result with the secret key.
npm install node-sealThis doubles three encrypted integers using BFV:
import SEAL from 'node-seal'
const seal = await SEAL()
const degree = 4096
const security = seal.SecLevelType.tc128
using parameters = new seal.EncryptionParameters(seal.SchemeType.bfv)
using coeffModulus = seal.CoeffModulus.BFVDefault(degree, security)
using plainModulus = seal.PlainModulus.Batching(degree, 20)
parameters.setPolyModulusDegree(degree)
parameters.setCoeffModulus(coeffModulus)
parameters.setPlainModulus(plainModulus)
using context = new seal.SEALContext(parameters, true, security)
if (!context.parametersSet()) {
throw new Error(context.parameterErrorMessage())
}
using keygen = new seal.KeyGenerator(context)
using publicKey = keygen.createPublicKey()
using secretKey = keygen.secretKey()
using encoder = new seal.BatchEncoder(context)
using encryptor = new seal.Encryptor(context, publicKey)
using evaluator = new seal.Evaluator(context)
using decryptor = new seal.Decryptor(context, secretKey)
using plain = new seal.Plaintext()
using cipher = new seal.Ciphertext()
encoder.encode(BigInt64Array.from([1n, 2n, 3n]), plain)
encryptor.encrypt(plain, cipher)
evaluator.addInplace(cipher, cipher)
decryptor.decrypt(cipher, plain)
console.log(Array.from(encoder.decodeBigInt64(plain).slice(0, 3)))
// [2n, 4n, 6n]The examples use using to release native objects at scope exit. See
object lifetime for cleanup details.
Initialize once and reuse the SEAL instance.
- Getting started: encrypt, serialize, compute, and decrypt a result.
- Advanced usage: schemes, parameters, key generation, compact serialization, and performance choices.
The package includes ES modules, Wasm binaries, and TypeScript declarations. The runtime needs WebAssembly exception handling and BigInt typed arrays. For bundlers that relocate the Wasm file, see Wasm deployment.
node-seal and node-seal/throws reject transparent ciphertext results.
Use node-seal/allows when your calculation needs those results, such as
subtracting a ciphertext from itself. Both variants expose the same API.
- Bindings and tests cover the available API.
- Microsoft SEAL examples explain the underlying library. Some C++ overloads are not available here.
- Contributing and release notes.
MIT.