Created
September 21, 2024 16:54
-
-
Save tychobrailleur/03e1df729da48407fcec1e64e8115bf0 to your computer and use it in GitHub Desktop.
node-seal example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/s0l0ist/node-seal/blob/main/FULL-EXAMPLE.md | |
// https://github.com/s0l0ist/node-seal/blob/main/USAGE.md | |
// See also https://inferati.com/blog/fhe-schemes-bfv | |
;(async () => { | |
const SEAL = require('node-seal') | |
const seal = await SEAL() | |
const schemeType = seal.SchemeType.bgv | |
const securityLevel = seal.SecurityLevel.tc128 | |
const polyModulusDegree = 4096 | |
// Bit sizes for each prime of the coeffModulus | |
const bitSizes = [36, 36, 37] | |
// Bit size representing plainModulus | |
const bitSize = 20 | |
const parms = seal.EncryptionParameters(schemeType) | |
// Set the PolyModulusDegree | |
parms.setPolyModulusDegree(polyModulusDegree) | |
// Create a suitable set of CoeffModulus primes | |
parms.setCoeffModulus( | |
seal.CoeffModulus.Create(polyModulusDegree, Int32Array.from(bitSizes)) | |
) | |
// Set the PlainModulus to a prime of bitSize 20. | |
// i.e. plaintext modulus, of bit size 20 (i.e < 1,048,576) | |
parms.setPlainModulus( | |
seal.PlainModulus.Batching(polyModulusDegree, bitSize) | |
) | |
console.log(parms.plainModulus.value) | |
console.log(parms.coeffModulus) | |
const context = seal.Context( | |
parms, // Encryption Parameters | |
true, // ExpandModChain | |
securityLevel // Enforce a security level | |
) | |
if (!context.parametersSet()) { | |
throw new Error( | |
'Could not set the parameters in the given context. Please try different encryption parameters.' | |
) | |
} | |
const encoder = seal.BatchEncoder(context) | |
const keyGenerator = seal.KeyGenerator(context) | |
const publicKey = keyGenerator.createPublicKey() | |
const secretKey = keyGenerator.secretKey() | |
const encryptor = seal.Encryptor(context, publicKey) | |
const decryptor = seal.Decryptor(context, secretKey) | |
const evaluator = seal.Evaluator(context) | |
var in1 = "2,5,3,4,5" | |
var in2 = "3,5,9,11,2" | |
const array1 = Int32Array.from(in1.split(',')) | |
const array2 = Int32Array.from(in2.split(',')) | |
const plainText1 = encoder.encode(array1) | |
const plainText2 = encoder.encode(array2) | |
var cipherText1 = encryptor.encrypt(plainText1) | |
var cipherText2 = encryptor.encrypt(plainText2) | |
console.log('Plaintext 1:', array1) | |
console.log('Plaintext 2:', array2) | |
evaluator.add(cipherText1, cipherText2, cipherText1) | |
var decryptedPlainText = decryptor.decrypt(cipherText1) | |
var decodedArray = encoder.decode(decryptedPlainText) | |
console.log('Decrypted (Homomorphic Add): ', decodedArray) | |
cipherText1 = encryptor.encrypt(plainText1) | |
cipherText2 = encryptor.encrypt(plainText2) | |
evaluator.sub(cipherText1, cipherText2, cipherText1) | |
decryptedPlainText = decryptor.decrypt(cipherText1) | |
decodedArray = encoder.decode(decryptedPlainText) | |
console.log('Decrypted (Homomorphic Subtract): ', decodedArray) | |
cipherText1 = encryptor.encrypt(plainText1) | |
cipherText2 = encryptor.encrypt(plainText2) | |
evaluator.multiply(cipherText1, cipherText2, cipherText1) | |
decryptedPlainText = decryptor.decrypt(cipherText1) | |
decodedArray = encoder.decode(decryptedPlainText) | |
console.log('Decrypted (Homomorphic Multiplication): ', decodedArray) | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment