s0l0ist / node-seal

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

PlainText seem not working #120

Closed bbzam closed 2 years ago

bbzam commented 2 years ago

Hi,

I have tried using both plaintext.load (commented below) and encoder.encode to construct a PlainText object that I would be using for encryption. However, it seems that the code isnt executing as when I do a console.log("here2") after said lines, it doesn't go there. Please advise... Btw, init() is used by my other function generateKey (which generates both public and privatekey) and it works fine.

Thanks, Benjie

async function encrypt(file, keyfromDB) {
  await init();

  //encode plaintext to be encrypted
  let buff = fs.readFileSync("uploads/" + file);
  const plaintext = seal.PlainText();

  /* let base64data = buff.toString('base64');
  console.log(base64data);
  plaintext.load(context, base64data);
  console.log("here2");*/

  const encoder = seal.BatchEncoder(context);
  const uint32array = new Uint32Array(buff);
  console.log(uint32array);
  plaintext = encoder.encode(uint32array);
  console.log("here2");`
s0l0ist commented 2 years ago

I cannot reproduce with a simple example:

    const schemeType = seal.SchemeType.bfv
    const securityLevel = seal.SecurityLevel.tc128
    const polyModulusDegree = 4096
    const bitSizes = [36, 36, 37]
    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.
    parms.setPlainModulus(
      seal.PlainModulus.Batching(polyModulusDegree, bitSize)
    )

    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)

      // Create data to be encrypted
  const array = Uint32Array.from([1, 2, 3, 4, 5])

  // Encode the Array
//   const plainText = encoder.encode(array)
//   const base64 = plainText.save()
//   console.log('base64', base64);
  const plainText = seal.PlainText();
  plainText.load(context, "XqEQAwcCAADfMwAAAAAAACi1L/1gSH8tngHaR0qLLxBwPAaQqYdpdBzK2qxIkLEPcWNFZ776Iw3bK0WDv3+6c4RR1azqZmZmZmZmZko/igihCLEIWDLz0RV1+xuZOYkGNkSBb+l4veQ7GO9PBvSLteJeKzGWu/BOaNXE+t4OpgmB16rec6feIMbhG/bktrRFodeWxog3PL0fDr4D945By2JE9Qvd8tVC8j7YuqvQ1RRq5I4fcgH0KyLB57Ggg9bBiQ/4H2+uVfT1+vQVTfHcV6bVmBGv7FBOeCfZJqPtJji3cJIfWJvqoVuA+KOGUm5ytbS/4IaNONx3SX9LmSQ0clGXvIdnpnlPGNUhzXpFNljvCD1hviWF7Lo7H2R2t+WIP4iYavZFLMT0PoOjJmWV3+qeqPO/xb/Ju3M4xcxPseoEqPoLqR/h59UR+Y0ugxgV4AJquLwcvsxXUquGt+B+j3AzUjsh2ImMutS5gaNSsoDRXsDm/iTFb0CScUSaVPXlvHH/z/4mgjkPuVCznGMEymFQ9INYd+oR3MFLTM/CJIDYFjbpXDA6RBj7AJNOeuR/4UA3IENf+rwLgHovG2aScgUwmL67yusYiMytRgD1NcJUj3ncC+c+354ZGcok/6dYG7KNgshzvnCcuaUi3LQR/YLXrzm7vfD84ayuw/UNx6DOPe8Yd3fyNhd53qtW8ZVQuMrkn0LhpbHQH6DdBUHrVnwubOsH0NVFGHo7eCy2eoKHeOahPvnW6S4ETO0i7S5rzNss9ykAUO9LtUz5hJKdoEkNxAqH0lYzufBjxdSi8krFmGa3d6X8CJhbb8hlrNybRRAG89LdPeutvxj4tVuzzCPDktmfP24AGeYbSHkQWrpvtgCNYUomTVCLSpmZuMKaT3fkHoq5Xk8IDEFMfyMS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vMVZ/UxlAJxOVj6mBw7o7+v3GNKBbceROP9pqdtVtMqdzdH17yddMVmc7j0YNlzzNTwx2WbjchwgkgvYdicxHbqRQ8NhERDgeM8MfksxA4LFomErIeiJBvOP/aBccJ96uYy7gaAY7Y1M/6W3NUnOseMJSl5sjqT2WyIy3x/84u0epJisFGEPh4NQwKta7b9v9M8E4t0XQO4GXqG9AJuhp4hPcDN2lLJ1RZtTI57w34dkq9XY7pol51s2nKaUWSbq9FE1aAVafQY+8IIj41Ppr/pc2k+aB1d3Rdk76Edzjh5AImirWvEDGZ6zPUTOqkgAwDuZawInYX7w55lRx1sVuHyqGaVDvMAvOMbGDB6a7ccjfbCOTobjLBoV4rZpze5NfVR94rnLnv3ybAmoKMtvup0N5P3CLxGoQPPHuvi8/r/+RJkCjdcXxaFa88r5F4Zt9OHHJq3gx3rct3eJuuBENZQvHO117vuhz0NE1+/wkdKav1UPX2A2Y7lRSxt3Te+dgs0zO1cSeyW6nSMZsm8M6WSyYT14Prszu5X8g9/N1qt0k9jtP9VYxrC7/+L/1TdAQ==")
  // Encrypt the PlainText
  const cipherText = encryptor.encrypt(plainText)

  // Add the CipherText to itself and store it in the destination parameter (itself)
  evaluator.add(cipherText, cipherText, cipherText) // Op (A), Op (B), Op (Dest)

  // Or create return a new cipher with the result (omitting destination parameter)
  // const cipher2x = evaluator.add(cipherText, cipherText)

  // Decrypt the CipherText
  const decryptedPlainText = decryptor.decrypt(cipherText)

  // Decode the PlainText
  const decodedArray = encoder.decode(decryptedPlainText)

  console.log('decodedArray', decodedArray)

I am able to serialize and deserialize normally. Could you post your encryption parameters? Or the serialized plaintext that you're attempting to load?

bbzam commented 2 years ago

Thanks @s0l0ist. Please see below my init function (contains my encryption parameters). The plaintext is from a buffer which I read from a file:

async function encrypt(file, keyfromDB) { await init();

//encode plaintext to be encrypted let buff = fs.readFileSync("uploads/" + file); const plaintext = seal.PlainText();

const encoder = seal.BatchEncoder(context); const uint32array = new Uint32Array(buff); console.log(uint32array); plaintext = encoder.encode(uint32array); console.log("here2"); .... }

async function init() {
    seal = await SEAL()
    const schemeType = seal.SchemeType.bfv
    const securityLevel = seal.SecurityLevel.tc128
    const polyModulusDegree = 4096
    const bitSizes = [36, 36, 37]
    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.
    parms.setPlainModulus(
        seal.PlainModulus.Batching(polyModulusDegree, bitSize)
    )

    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.'
        )
    }
    console.log("init done");
}
s0l0ist commented 2 years ago

Ah you're creating a const plaintext:

const plaintext = seal.PlainText();

But later, there's an attempt to write to it:

plaintext = encoder.encode(uint32array);

Instead, try passing in the plaintext into the encode function as the second parameter which writes to the underlying WASM instance inside the const object (and returns nothing):

encoder.encode(uint32array, plaintext);

Or more simply, just remove the line where you construct the plaintext and let the encoder.encode(uint32array) create it for you.

// const plaintext = seal.PlainText();
...
const plaintext = encoder.encode(uint32array);
bbzam commented 2 years ago

Thanks @s0l0ist. Ive tried both suggestions - doing the 2nd parameter (screenshot below) and just commenting our the const plaintext line. However, my code seems not to go through, i.e. the console.log("here2") is not executed

image

s0l0ist commented 2 years ago

In your screenshot, you're still assigning the return value (which is now null) to the const plaintext.

Don't assign anything if you're going to pass in the plaintext as the second parameter.

Replace line 77 with:

encoder.encode(uint32array, plaintext);
bbzam commented 2 years ago

Hi @s0l0ist, tried doing that and even removed the const line but still no cigar: image

Next, tried your original suggestion of doing const plaintext = encoder.encode(uint32array); but still code doesn't go to the console.log("here2") line

NuitNoire7 commented 2 years ago

Hi @s0l0ist,

I am working under @bbzam, we've tried the code for encryption and it seems to be working on smaller files but I am getting an error while trying to encrypt larger files. I attached here the screenshots I have taken while encrypting the JSON files with 1KB and 830 KB in size. I would like to ask if you have already encountered this problem and if you could kindly suggest any solution we can try.

image image image image image image image

Thanks, Krizzel

s0l0ist commented 2 years ago

Ah, I see the problem, you're attempting to encode an array of elements (Uint32) that is larger than the encryption parameters support.

The limit for the number of items you can encode/decode is equal to the Polymodulus Degree. In your case, that is 4096. You cannot encode more elements than that limit unless you increase the polymodulus degree to 8192 (or another power of 2). However, doing so slows down encryption and key generation tremendously.

You could split your files into 4096 element chunks and have N plaintexts to accomplish the same task.