s0l0ist / node-seal

Homomorphic Encryption for TypeScript or JavaScript - Microsoft SEAL
https://s0l0ist.github.io/node-seal/
MIT License
192 stars 24 forks source link

Can't encode integer #39

Closed boyuan-chen closed 4 years ago

boyuan-chen commented 4 years ago

Here is my code:

(async () => {
    // ES6 or CommonJS
    // import { Seal } from 'node-seal'
    // const { Seal } = require('node-seal')

    // Using CommonJS for RunKit
    const { Seal } = require('node-seal')

    const Morfix = await Seal

    const parms = Morfix.EncryptionParameters({
      schemeType: Morfix.SchemeType.BFV
    })

    parms.setPolyModulusDegree({
      polyModulusDegree: 8192
    })

    // Create a suitable vector of CoeffModulus primes
    parms.setCoeffModulus({
      coeffModulus: Morfix.CoeffModulus.Create({
        polyModulusDegree: 8192,
        bitSizes: Morfix.Vector({array: new Int32Array([36,36,37]) }),
        //securityLevel: Morfix.SecurityLevel.tc128
      })
    })

    // Set the PlainModulus to a prime of bitSize 20.
    parms.setPlainModulus({
      plainModulus: Morfix.PlainModulus.Batching({
        polyModulusDegree: 8192,
        bitSize: 20
      })
    })

    const context = Morfix.Context({
      encryptionParams: parms,
      expandModChain: true,
      securityLevel: Morfix.SecurityLevel.tc128
    })

    if (!context.parametersSet) {
      throw new Error('Could not set the parameters in the given context. Please try different encryption parameters.')
    }

    const encoder = Morfix.IntegerEncoder({context})

    const keyGenerator = Morfix.KeyGenerator({
      context: context
    })

    const publicKey = keyGenerator.getPublicKey()
    const secretKey = keyGenerator.getSecretKey()
    const encryptor = Morfix.Encryptor({
      context: context,
      publicKey: publicKey
    })
    const decryptor = Morfix.Decryptor({
      context: context,
      secretKey: secretKey
    })

    // Create a plainText variable and encode the vector to it
    const plainText = Morfix.PlainText()
    encoder.encodeInt32({value: 6, destination: plainText})
  })()

The error message is

'function IntegerEncoder.encodeInt32 called with 2 arguments, expected 1 args!'

Not quite sure how to encode an integer. I didn't find any instructions to encode and decode integer, so I followed the API doc and gave it 1 argument.

s0l0ist commented 4 years ago

@cby3149 Thank you for reporting this bug.

It has been fixed and an updated version of this library has been published.

You could use the IntegerEncoder like the following:

...
const plainText = Morfix.PlainText()
encoder.encodeInt32({value: 6, destination: plainText})
const decoded = encoder.decodeInt32({ plainText })
// decoded = 6