英文:
How to encode an array of numbers in python with piheean?
问题
I can help you with the translation, but I won't provide code for this question. Here's the translated content:
我有这段加密数字列表的代码。
import piheaan as heaan
import numpy as np
# 第一步:设置参数
params = heaan.ParameterPreset.SS7
context = heaan.make_context(params)
# 第二步:生成密钥
key_dir_path = "./keys"
sk = heaan.SecretKey(context)
keygen = heaan.KeyGenerator(context, sk)
keygen.gen_common_keys()
pack = keygen.keypack
# 第三步:将消息加密为密文 - 使用特定向量
enc = heaan.Encryptor(context)
log_slots = 3
msg = heaan.Message(log_slots) # 槽位数 = 2 的 log_slots 次方
for i, value in enumerate([2,5,4,3]):
msg[i] = value
ctxt = heaan.Ciphertext(context)
enc.encrypt(msg, pack, ctxt)
# 第四步:将密文相乘(即将密文平方)
eval = heaan.HomEvaluator(context, pack)
ctxt_out = heaan.Ciphertext(context)
eval.mult(ctxt, ctxt, ctxt_out)
# 第五步:通过解密器解密密文
dec = heaan.Decryptor(context)
msg_out = heaan.Message()
dec.decrypt(ctxt_out, sk, msg_out)
# 输出 - 它有 9 个槽位(2 的 3 次方),因此只在这种情况下加密前 4 个。
msg_out
[ (4.000000+0.000000j), (25.000000+0.000000j), (16.000000+0.000000j), (9.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j) ]
然而,我的实际数据是以数组的形式存在,例如:
l1 = [2, 5, 4, 3]
l2 = [1, 2, 2, 3]
# ..
arr_in = np.array([l1,l2])
arr_in
# array([[2, 5, 4, 3],
# [1, 2, 2, 3]])
我尝试过以下方法:
for i, value in np.ndenumerate(arr):
msg[i] = value
但它不适用于 piheean 的消息格式,会导致错误。如下所示:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [44], in <cell line: 5>()
4 msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
5 for i, value in np.ndenumerate(arr):
----> 6 msg[i] = value
7 ctxt = heaan.Ciphertext(context)
8 enc.encrypt(msg, pack, ctxt)
TypeError: __setitem__(): incompatible function arguments. The following argument types are supported:
1. (self: piheaan.Message, idx: int, value: complex) -> None
Invoked with: [ (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, 0), 2
你如何编写代码,以便接受数组 arr_in
中的每个列表,加密它们,并将其输出为 arr_out
形式的数组,即 array([[4, 25, 16, 9], [1, 4, 4, 9]])
?
英文:
I have this code which encrypts a list of numbers.
import piheaan as heaan
import numpy as np
# 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 - with a specific vector
enc = heaan.Encryptor(context)
log_slots = 3
msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
for i, value in in enumerate([2,5,4,3]):
msg[i] = value
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)
#output - it has 9 slots (2 to the power of 3), so only encrypts the first 4 in this case.
msg_out
[ (4.000000+0.000000j), (25.000000+0.000000j), (16.000000+0.000000j), (9.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j), (0.000000+0.000000j) ]
However, my actual data is in the form of an array, e.g.:
l1 = [2, 5, 4, 3]
l2 = [1, 2, 2, 3]
..
arr_in = np.array([l1,l2])
arr_in
# array([[2, 5, 4, 3],
# [1, 2, 2, 3]])
I have tried this
for i, value in np.ndenumerate(arr):
msg[i] = value
but it does not work with the piheean's message format giving an error.
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
Input In [44], in <cell line: 5>()
4 msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
5 for i, value in np.ndenumerate(arr):
----> 6 msg[i] = value
7 ctxt = heaan.Ciphertext(context)
8 enc.encrypt(msg, pack, ctxt)
TypeError: __setitem__(): incompatible function arguments. The following argument types are supported:
1. (self: piheaan.Message, idx: int, value: complex) -> None
Invoked with: [ (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, 0), 2
how can I write the code which essentially takes each list from my array, arr_in, encrypts it and outputs it as an array of the form, arr_out, array([[4, 25,16,9], [1,4,4,9]]) ?
答案1
得分: 1
以下是您要求的翻译:
如果您打印这个:
for i, value in np.ndenumerate(arr_in):
print(i,value)
(0, 0) 2
(0, 1) 1
(0, 2) 5
(0, 3) 2
(1, 0) 4
(1, 1) 2
(1, 2) 3
(1, 3) 3
您试图将`(0,0)..(1,3)`元组分配给`msg[]`。这是您遇到错误的原因。
而您可以这样做:
l1 = [2, 5, 4, 3]
l2 = [1, 2, 2, 3]
arr_in = np.array(list(zip(l1, l2))).reshape((2, 4))
# 输出
array([[2, 1, 5, 2],
[4, 2, 3, 3]])
new_arr = arr_in.flatten()
for i,value in enumerate(new_arr):
print(i,value) # 您可以获得索引和值
0 2
1 1
2 5
3 2
4 4
5 2
6 3
7 3
`msg` 是 `2**3`,即 `8` 个值
[ (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) ]
for i, value in enumerate(new_arr):
msg[i] = value
print(msg)
[ (2.000000+0.000000j), (1.000000+0.000000j), (5.000000+0.000000j), (2.000000+0.000000j), (4.000000+0.000000j), (2.000000+0.000000j), (3.000000+0.000000j), (3.000000+0.000000j) ]
英文:
If you print this :
for i, value in np.ndenumerate(arr_in):
print(i,value)
(0, 0) 2
(0, 1) 1
(0, 2) 5
(0, 3) 2
(1, 0) 4
(1, 1) 2
(1, 2) 3
(1, 3) 3
You were trying to assign (0,0)..(1,3)
tuples to msg[]
. That is the reason you were getting error.
Instead you can do:
l1 = [2, 5, 4, 3]
l2 = [1, 2, 2, 3]
arr_in = np.array(list(zip(l1, l2))).reshape((2, 4))
#output
array([[2, 1, 5, 2],
[4, 2, 3, 3]])
new_arr = arr_in.flatten()
for i,value in enumerate(new_arr):
print(i,value) # you get the index as well as the value
0 2
1 1
2 5
3 2
4 4
5 2
6 3
7 3
msg
is 2**3
.i.e. 8
values
[ (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) ]
for i, value in enumerate(new_arr):
msg[i] = value
print(msg)
[ (2.000000+0.000000j), (1.000000+0.000000j), (5.000000+0.000000j), (2.000000+0.000000j), (4.000000+0.000000j), (2.000000+0.000000j), (3.000000+0.000000j), (3.000000+0.000000j) ]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论