OpenMined / TenSEAL

A library for doing homomorphic encryption operations on tensors

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How can I save the result of an encrypted data in a database?

jtovartr opened this issue · comments

How can I save the result of an encrypted data in a database?

I have the following code that encrypts a vector containing the number 70.

context = ts.context(ts.SCHEME_TYPE.BFV, poly_modulus_degree=4096, plain_modulus=1032193)
plain_vector = [70]
encrypted_vector = ts.bfv_vector(context, plain_vector)
print(encrypted_vector.serialize())

When I call the serialize() method to display the encrypted vector I get the following output. (I had to copy only a part of the output because the returned value is very large)

... xee\x131\xb7\x89\x1b\x7f\x11\x90Q&\xe7\x94\x8a\x94\xd1\x80\xc36\x85\x80\xbf[\xa0\xea\x9ba\x05(\x8d\x8aZ\xed\x8a*\x1b\x8b\xe8\x96\xf0\xa2\xa8\xdf\xedP\x9c\xbf\x1f\x93\x19\xe0d\x10\xc9q_\xdb{b\xae\x89H!N\xdbRY\xac\xed\xcb$\x12C\x97\x93T\xed\xb4UhZ7P\xd3\xe25\xfc\xa5\xa6@\xbd\xd6i\xef\xca\x98~\xbd\xb1z\x8a\xd1d\x85\xbc4\x1c\x1c\xd0:=\x97!$\x07\xc1Q\xd4\xc5W\xf4\x94,\x05#A\\\xbc\xbb\xfb\t\x88\x01\xd7\xec\xb5)\xa7;\tT_(v\x7f~\xbe\x8c\xc8a\x1a\x00pc\xa0\xda\xab\x96b\xef|\xbd\xa8y7\x9e\xd3@\xcd\\\xa6\xcf=FFb\xf3D%T\xce\x0b\xc9}9\xff\xbe\xa9\x90\x90\xc9T\x1a_lS\xdd,U\xbb\xc4\xb1\x82.\xa1\xfb\r\x80R\x86\xbfc\x1f\xd5t\xff\x9c\x10\xb0\xd3\x7f;\x1co\x04\x17\x8e\xb3\x02\xa4\x9eAm\x02\xfd8g$\x1a\x08w\xf1\x06\nyK\x945\xa1{6\xd6\x98\xa4c\x00\x8c\x94\xe4\xbd\xeb\xad\x19\xe6\x8e\x9c\xcfX\xa1\x01\xf1\x08\xd3\x96E\x1aO\x19\xd4\xcdU\x96R\x1c\xcb0p\xa3\xd2\xab\xecN\x1b\xcbf~\xf3\x8b\xd0V\xeb+,\x02&",\xa2\xb9\x90\xb8yn\xb3]\xd71bJ\xab\x17\x8dq:A\xc5N\xb5\xe4\xee\xa5@+\x9b\x88\x0cbs$)\x00\xc2\x03E\xe6S\x88\x82\xf1\\\x96\xdfr\xb3\x15\x12\x03\xc1\xff\xbe\x15p\xd6.*k9\xda\xe1\x19\x1a\x1f\x0f\x96\r\xd1\x91\x01\x8d\x04%\x1eQ\xf3\xd8\x88\xfe\xdc1\x07x\x1b\x97X\x0f\xc6\xb8X\xb8\xda1G\x9d l\xa8\x8a\x9c\x17\xc7\x87\xc3\xfcM{\xc8o\x97TT\x8a\x8fe<P"\xb4\x9a\xbc\xc78\xf9\x19,\xeeT\x98\xc4{\x16\xf0\x16\x18\x91\xeb\xd2\x0e\x9e\xe9\x8a|\xd6\x1d#\xfa\x0f\xa10{l\xe0\x9a\xff\xa5n\x1d\xcc\x91\x0eE`T\x08\x18v\xf3\x02\xd4\xa8\xc7\xd3\xdc\x05\xe09\xff ...

You can see that it is a very large value and my goal is to store these values in a database, how can I do this?

For simplicity and speed you can a redis database (glorified Python dictionary). Serialize your encrypted vector to bytes and create a key with the corresponding value. See below:

# Serialize 
enc_vec = [ts.ckks_vector(secret_ctx, i.tolist()).serialize() for i in data]

# Connect to db
r = redis.Redis(localhost, 6379, 1)

# Upload list to db
r.rpush(myencryptedVector, *enc_vec)

@efriel94 Thanks for your answer. I understand your point but I still have the same doubt, the value of enc_vec will be very large and if I store it in a database like MySQL I don't think it will be scalable in the future

@jtovartr your question was merely asking how to store data onto a database there was no mention of scalability or computational resources. You have three options: 1) write to disk, 2) store in-memory, 3) offload to cloud or third party storage. Hard drives are cheap meaning that you can always expand your SQL db. In-memory is ok for small workloads using Redis db and cloud allows you to scale up or down on demand.

Thank you very much for your answer, it is clear to me, thanks again.