s0l0ist / node-seal

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

How to export ciphertext and apply addition outside node-seal #153

Open alexcostars opened 1 month ago

alexcostars commented 1 month ago

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!

mebner98 commented 2 weeks ago

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

s0l0ist commented 1 week ago

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".

alexcostars commented 1 week ago

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?

s0l0ist commented 1 week ago

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 commented 1 week ago

In general, you'd need to build the SEAL C++ library and create the appropriate bindings.

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