Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
alexcostars opened this issue Aug 4, 2024 · 5 comments
Open

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

alexcostars opened this issue Aug 4, 2024 · 5 comments

Comments

@alexcostars
Copy link

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
Copy link

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

@s0l0ist
Copy link
Owner

s0l0ist commented Sep 4, 2024

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
Copy link
Author

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
Copy link
Owner

s0l0ist commented Sep 6, 2024

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
Copy link
Owner

s0l0ist commented Sep 6, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants