如何使用Python和piheean编码一个数字数组?

huangapple go评论126阅读模式
英文:

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:

我有这段加密数字列表的代码。

  1. import piheaan as heaan
  2. import numpy as np
  3. # 第一步:设置参数
  4. params = heaan.ParameterPreset.SS7
  5. context = heaan.make_context(params)
  6. # 第二步:生成密钥
  7. key_dir_path = "./keys"
  8. sk = heaan.SecretKey(context)
  9. keygen = heaan.KeyGenerator(context, sk)
  10. keygen.gen_common_keys()
  11. pack = keygen.keypack
  12. # 第三步:将消息加密为密文 - 使用特定向量
  13. enc = heaan.Encryptor(context)
  14. log_slots = 3
  15. msg = heaan.Message(log_slots) # 槽位数 = 2 的 log_slots 次方
  16. for i, value in enumerate([2,5,4,3]):
  17. msg[i] = value
  18. ctxt = heaan.Ciphertext(context)
  19. enc.encrypt(msg, pack, ctxt)
  20. # 第四步:将密文相乘(即将密文平方)
  21. eval = heaan.HomEvaluator(context, pack)
  22. ctxt_out = heaan.Ciphertext(context)
  23. eval.mult(ctxt, ctxt, ctxt_out)
  24. # 第五步:通过解密器解密密文
  25. dec = heaan.Decryptor(context)
  26. msg_out = heaan.Message()
  27. dec.decrypt(ctxt_out, sk, msg_out)
  28. # 输出 - 它有 9 个槽位(2 的 3 次方),因此只在这种情况下加密前 4 个。
  29. msg_out
  30. [ (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) ]

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

  1. l1 = [2, 5, 4, 3]
  2. l2 = [1, 2, 2, 3]
  3. # ..
  4. arr_in = np.array([l1,l2])
  5. arr_in
  6. # array([[2, 5, 4, 3],
  7. # [1, 2, 2, 3]])

我尝试过以下方法:

  1. for i, value in np.ndenumerate(arr):
  2. msg[i] = value

但它不适用于 piheean 的消息格式,会导致错误。如下所示:

  1. ---------------------------------------------------------------------------
  2. TypeError Traceback (most recent call last)
  3. Input In [44], in <cell line: 5>()
  4. 4 msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
  5. 5 for i, value in np.ndenumerate(arr):
  6. ----> 6 msg[i] = value
  7. 7 ctxt = heaan.Ciphertext(context)
  8. 8 enc.encrypt(msg, pack, ctxt)
  9. TypeError: __setitem__(): incompatible function arguments. The following argument types are supported:
  10. 1. (self: piheaan.Message, idx: int, value: complex) -> None
  11. 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.

  1. import piheaan as heaan
  2. import numpy as np
  3. # Step 1. Setting Parameters
  4. params = heaan.ParameterPreset.SS7
  5. context = heaan.make_context(params)
  6. # Step 2. Generating Keys
  7. key_dir_path = &quot;./keys&quot;
  8. sk = heaan.SecretKey(context)
  9. keygen = heaan.KeyGenerator(context, sk)
  10. keygen.gen_common_keys()
  11. pack = keygen.keypack
  12. # Step 3. Encrypt Message to Ciphertext - with a specific vector
  13. enc = heaan.Encryptor(context)
  14. log_slots = 3
  15. msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
  16. for i, value in in enumerate([2,5,4,3]):
  17. msg[i] = value
  18. ctxt = heaan.Ciphertext(context)
  19. enc.encrypt(msg, pack, ctxt)
  20. # Step 4. multiply ciphertexts(i.e. square a ciphertext)
  21. eval = heaan.HomEvaluator(context, pack)
  22. ctxt_out = heaan.Ciphertext(context)
  23. eval.mult(ctxt, ctxt, ctxt_out)
  24. # Step 5. decrypt the ciphertext by Decryptor.
  25. dec = heaan.Decryptor(context)
  26. msg_out = heaan.Message()
  27. dec.decrypt(ctxt_out, sk, msg_out)
  28. #output - it has 9 slots (2 to the power of 3), so only encrypts the first 4 in this case.
  29. msg_out
  30. [ (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.:

  1. l1 = [2, 5, 4, 3]
  2. l2 = [1, 2, 2, 3]
  3. ..
  4. arr_in = np.array([l1,l2])
  5. arr_in
  6. # array([[2, 5, 4, 3],
  7. # [1, 2, 2, 3]])

I have tried this

  1. for i, value in np.ndenumerate(arr):
  2. msg[i] = value

but it does not work with the piheean's message format giving an error.

  1. ---------------------------------------------------------------------------
  2. TypeError Traceback (most recent call last)
  3. Input In [44], in &lt;cell line: 5&gt;()
  4. 4 msg = heaan.Message(log_slots) # number_of slots = pow(2, log_slots)
  5. 5 for i, value in np.ndenumerate(arr):
  6. ----&gt; 6 msg[i] = value
  7. 7 ctxt = heaan.Ciphertext(context)
  8. 8 enc.encrypt(msg, pack, ctxt)
  9. TypeError: __setitem__(): incompatible function arguments. The following argument types are supported:
  10. 1. (self: piheaan.Message, idx: int, value: complex) -&gt; None
  11. 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

以下是您要求的翻译:

  1. 如果您打印这个
  2. for i, value in np.ndenumerate(arr_in):
  3. print(i,value)
  4. (0, 0) 2
  5. (0, 1) 1
  6. (0, 2) 5
  7. (0, 3) 2
  8. (1, 0) 4
  9. (1, 1) 2
  10. (1, 2) 3
  11. (1, 3) 3
  12. 您试图将`(0,0)..(1,3)`元组分配给`msg[]`这是您遇到错误的原因
  13. 而您可以这样做
  14. l1 = [2, 5, 4, 3]
  15. l2 = [1, 2, 2, 3]
  16. arr_in = np.array(list(zip(l1, l2))).reshape((2, 4))
  17. # 输出
  18. array([[2, 1, 5, 2],
  19. [4, 2, 3, 3]])
  20. new_arr = arr_in.flatten()
  21. for i,value in enumerate(new_arr):
  22. print(i,value) # 您可以获得索引和值
  23. 0 2
  24. 1 1
  25. 2 5
  26. 3 2
  27. 4 4
  28. 5 2
  29. 6 3
  30. 7 3
  31. `msg` `2**3` `8` 个值
  32. [ (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) ]
  33. for i, value in enumerate(new_arr):
  34. msg[i] = value
  35. print(msg)
  36. [ (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 :

  1. for i, value in np.ndenumerate(arr_in):
  2. print(i,value)
  3. (0, 0) 2
  4. (0, 1) 1
  5. (0, 2) 5
  6. (0, 3) 2
  7. (1, 0) 4
  8. (1, 1) 2
  9. (1, 2) 3
  10. (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:

  1. l1 = [2, 5, 4, 3]
  2. l2 = [1, 2, 2, 3]
  3. arr_in = np.array(list(zip(l1, l2))).reshape((2, 4))
  4. #output
  5. array([[2, 1, 5, 2],
  6. [4, 2, 3, 3]])
  7. new_arr = arr_in.flatten()
  8. for i,value in enumerate(new_arr):
  9. print(i,value) # you get the index as well as the value
  10. 0 2
  11. 1 1
  12. 2 5
  13. 3 2
  14. 4 4
  15. 5 2
  16. 6 3
  17. 7 3

msg is 2**3 .i.e. 8 values

  1. [ (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) ]
  2. for i, value in enumerate(new_arr):
  3. msg[i] = value
  4. print(msg)
  5. [ (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) ]

huangapple
  • 本文由 发表于 2023年6月25日 22:25:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/76550877.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定