node-seal icon indicating copy to clipboard operation
node-seal copied to clipboard

How to export ciphertext and apply addition outside node-seal

Open alexcostars opened this issue 1 year ago • 5 comments

Hi guys, I need some help.

I'm using this repository to perform great BVF operations, it's amazing!

When I use the entire lib to perform all operations works fine, but is possible to perform addition operations outside the lib? How can I export 2 ciphertexts in a way that I can perform a external addition (I should execute this into another programming language and restricted hardware)?

My SEAL config:

const schemeType = seal.SchemeType.bfv
const polyModulusDegree = 4096
const bitSize = 20

let parms = seal.EncryptionParameters(schemeType)
parms.setPolyModulusDegree(polyModulusDegree)
parms.setCoeffModulus(seal.CoeffModulus.BFVDefault(polyModulusDegree));
parms.setPlainModulus(seal.PlainModulus.Batching(polyModulusDegree, bitSize));

The result of ciphertext.saveArray():

Uint8Array(88518) [
   94, 161,  16,   4,   1,   2,   0,   0, 198,  89,   1,  0,
    0,   0,   0,   0,  40, 181,  47, 253, 160,  97,   0,  2,
    0, 156, 202,  10, 204, 150,  13,  52, 119, 156, 167, 65,
   60, 254,  25,   4,  42,   4,  70,  70,  88, 164, 155, 79,
  189, 172, 170, 221, 139,  98,  47,  74, 154, 185, 225, 42,
  183, 135,   2,   0,   2,   0,  16,   0, 240,  63,   1,  0,
   94, 161,  16,   4,  24,  64, 181,  55,  39,  69,   8,  0,
    0,   0,  24,  50, 145,  30,  11,   0,   0,   0, 121, 37,
  162,   6,  13,   0,
  ... 88418 more items
]

Sorry if this is a primary question, I'm not a cipher expert

Thanks!

alexcostars avatar Aug 04 '24 20:08 alexcostars

Did you solve your problem? trying to do something similar with angular and python flask (so two seal instances)

mebner98 avatar Aug 25 '24 17:08 mebner98

Hey sorry I missed this question.

So your goal is to create a cipher in one language and then import it in another runtime (even a different machine)?

If you're using this library, then it is pretty straight forward. There's a test showing how to save and load ciphertexts:

https://github.com/s0l0ist/node-seal/blob/main/src/tests/cipher-text.test.ts#L326

If you're using another language (python), then you'd need to lookup how to load it from a binary array on that platform. Thankfully, the SEAL library has its own serialization format (self-contained) so it should "just work".

s0l0ist avatar Sep 04 '24 07:09 s0l0ist

Your answer is helpful, @s0l0ist. This helps us export (and import) a cipher, but my question is more complex.

I need to understand how to perform an addition operation manually (without using the node-seal library).

For example, consider that I created two ciphers using node-seal. I used the mentioned code to export them as two arrays. If I send these two exported arrays to another programming language (like Python or Java), how can I perform an addition operation? What algorithm do I need to construct to sum of cipher_1_array and cipher_2_array?

alexcostars avatar Sep 06 '24 01:09 alexcostars

There are two ways to perform addition:

  1. decrypt, perform addition, re-encrypt
  2. perform HE Addition of the two cipher texts

I assume you want to do 2 as it is more common.

Unfortunately, you cannot simply add two cipher text arrays without using a derivative work from a Microsoft SEAL library. Mainly because the serialized output you have contains other information about the protocol such as the version of SEAL and some other metadata in addition to the underlying cipher text encrypted data.

s0l0ist avatar Sep 06 '24 02:09 s0l0ist

In general, you'd need to build the SEAL C++ library and create the appropriate bindings in whatever language you needed to deserialize, and recreate the same encryption parameters. Ideally, you would use an existing 3rd party library that already has bindings in the language you need.

I found this python lib, but I have not followed the build instructions. Assuming you are able to build it, you'd need to save the cipher with no compression (node-seal has this override when calling saveArray) since this python library doesn't build SEAL with compression support.

https://github.com/Huelse/SEAL-Python

s0l0ist avatar Sep 06 '24 02:09 s0l0ist