numpy 如何在python中用piheean编码一个数字数组?

bbuxkriu  于 2023-06-29  发布在  Python
关注(0)|答案(1)|浏览(121)

我有一个密码可以加密一串数字。

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) ]

然而,我的实际数据是数组的形式,例如:

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]])的数组?

bnlyeluc

bnlyeluc1#

如果你打印这个:

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))

#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

msg2**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) ]

相关问题