I am following the code described here, https://pypi.org/project/pi-heaan/
e.g. my code
import piheaan as heaan
# Step 1. Setting Parameters
params = heaan.ParameterPreset.SS7
context = heaan.make_context(params)
# Step 2. Generating Keys
key_dir_path = "./keys"
sk = heaan.SecretKey(context)
keygen = heaan.KeyGenerator(context, sk)
keygen.gen_common_keys()
pack = keygen.keypack
# Step 3. Encrypt Message to Ciphertext
enc = heaan.Encryptor(context)
log_slots = 2
msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
for i in range(4):
msg[i] = i+1
ctxt = heaan.Ciphertext(context)
enc.encrypt(msg, pack, ctxt)
# Step 4. Multiply ciphertexts(i.e. square a ciphertext)
eval = heaan.HomEvaluator(context, pack)
ctxt_out = heaan.Ciphertext(context)
eval.mult(ctxt, ctxt, ctxt_out)
# Step 5. Decrypt the ciphertext by Decryptor.
dec = heaan.Decryptor(context)
msg_out = heaan.Message()
dec.decrypt(ctxt_out, sk, msg_out)
msg_out # print out the result of operation performed on ciphertext
# [ (1.000000+0.000000j), (4.000000+0.000000j), (9.000000+0.000000j), (16.000000+0.000000j) ]
Which works fine, but I dont know how to replace the line
for i in range(4):
msg[i] = i+1
for a specific vector? e.g. I have msg = [2, 5, 4, 3].
I have tried to input it as a list and it does not work giving me an error.
--------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [28], in <cell line: 7>()
5 msg = [2,5,4,3]
6 ctxt = heaan.Ciphertext(context)
----> 7 enc.encrypt(msg, pack, ctxt)
TypeError: encrypt(): incompatible function arguments. The following argument types are supported:
1. (self: piheaan.Encryptor, msg: piheaan.Message, sk: piheaan.SecretKey, ctxt: piheaan.Ciphertext) -> None
2. (self: piheaan.Encryptor, msg: piheaan.Message, pack: piheaan.KeyPack, ctxt: piheaan.Ciphertext) -> None
3. (self: piheaan.Encryptor, ptxt: piheaan.Plaintext, sk: piheaan.SecretKey, ctxt: piheaan.Ciphertext) -> None
4. (self: piheaan.Encryptor, ptxt: piheaan.Plaintext, pack: piheaan.KeyPack, ctxt: piheaan.Ciphertext) -> None
Invoked with: <piheaan.Encryptor object at 0x000001C1A995B3B0>, [2, 5, 4, 3], <piheaan.KeyPack object at 0x000001C1A990C2F0>, (level: 7, log(num slots): 13, data: [ (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), ..., (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j) ])
if I try like this
for i in [2,5,4,3]:
msg[i] = i
it gives me a wrong output,
#[ (0.000000+0.000000j), (0.000000+0.000000j), (4.000000+0.000000j), (9.000000+0.000000j) ]
My actual data is a numpy.ndarray of specific vectors that I want to encrypt with this method.
Instead of (1):
for i in [2,5,4,3]:
msg[i] = i
# print(msg)
# [ (0.000000+0.000000j), (0.000000+0.000000j), (2.000000+0.000000j), (3.000000+0.000000j) ]
You want (2):
for i, value in enumerate([2, 5, 4, 3]):
msg[i] = value
# print(msg)
# [ (2.000000+0.000000j), (5.000000+0.000000j), (4.000000+0.000000j), (3.000000+0.000000j) ]
The two code snippets have different behaviors:
for i in [2, 5, 4, 3]:
In this case, the loop directly iterates over the elements in the list [2, 5, 4, 3]
. The variable i
takes on the values of the elements one by one. However, in this case, you are using the elements themselves as indices to access msg[i]
. This means that instead of assigning the values to the corresponding indices of msg
, you are actually overwriting the values at indices 2, 5, 4, and 3 of msg
.for i, value in enumerate([2, 5, 4, 3]):
In this case, enumerate
is used to iterate over the list [2, 5, 4, 3]
. On each iteration, it assigns the index to i
and the corresponding value to value
. The code msg[i] = value
then assigns each value to the i
-th slot of the msg
object. This approach allows you to access both the index and the value of each element in the list.