simple-real-numbers.py 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from openfhe import *
  2. def main():
  3. mult_depth = 1
  4. scale_mod_size = 50
  5. batch_size = 8
  6. parameters = CCParamsCKKSRNS()
  7. parameters.SetMultiplicativeDepth(mult_depth)
  8. parameters.SetScalingModSize(scale_mod_size)
  9. parameters.SetBatchSize(batch_size)
  10. cc = GenCryptoContext(parameters)
  11. cc.Enable(PKESchemeFeature.PKE)
  12. cc.Enable(PKESchemeFeature.KEYSWITCH)
  13. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  14. print("The CKKS scheme is using ring dimension: " + str(cc.GetRingDimension()))
  15. keys = cc.KeyGen()
  16. cc.EvalMultKeyGen(keys.secretKey)
  17. cc.EvalRotateKeyGen(keys.secretKey, [1, -2])
  18. x1 = [0.25, 0.5, 0.75, 1.0, 2.0, 3.0, 4.0, 5.0]
  19. x2 = [5.0, 4.0, 3.0, 2.0, 1.0, 0.75, 0.5, 0.25]
  20. ptx1 = cc.MakeCKKSPackedPlaintext(x1)
  21. ptx2 = cc.MakeCKKSPackedPlaintext(x2)
  22. print("Input x1: " + str(ptx1))
  23. print("Input x2: " + str(ptx2))
  24. # Encrypt the encoded vectors
  25. c1 = cc.Encrypt(keys.publicKey, ptx1)
  26. c2 = cc.Encrypt(keys.publicKey, ptx2)
  27. # Step 4: Evaluation
  28. # Homomorphic additions
  29. c_add = cc.EvalAdd(c1, c2)
  30. # Homomorphic subtraction
  31. c_sub = cc.EvalSub(c1, c2)
  32. # Homomorphic scalar multiplication
  33. c_scalar = cc.EvalMult(c1, 4)
  34. # Homomorphic multiplication
  35. c_mult = cc.EvalMult(c1, c2)
  36. # Homomorphic rotations
  37. c_rot1 = cc.EvalRotate(c1, 1)
  38. c_rot2 = cc.EvalRotate(c1, -2)
  39. # Step 5: Decryption and output
  40. # Decrypt the result of additions
  41. ptAdd = cc.Decrypt(c_add, keys.secretKey)
  42. # We set the precision to 8 decimal digits for a nicer output.
  43. # If you want to see the error/noise introduced by CKKS, bump it up
  44. # to 15 and it should become visible.
  45. precision = 8
  46. print("Results of homomorphic computations:")
  47. result = cc.Decrypt(c1, keys.secretKey)
  48. result.SetLength(batch_size)
  49. print("x1 = " + str(result))
  50. print("Estimated precision in bits: " + str(result.GetLogPrecision()))
  51. # Decrypt the result of scalar multiplication
  52. result = cc.Decrypt(c_scalar, keys.secretKey)
  53. result.SetLength(batch_size)
  54. print("4 * x1 = " + str(result))
  55. # Decrypt the result of multiplication
  56. result = cc.Decrypt(c_mult, keys.secretKey)
  57. result.SetLength(batch_size)
  58. print("x1 * x2 = " + str(result))
  59. # Decrypt the result of rotations
  60. result = cc.Decrypt(c_rot1, keys.secretKey)
  61. result.SetLength(batch_size)
  62. print("In rotations, very small outputs (~10^-10 here) correspond to 0's:")
  63. print("x1 rotated by 1 = " + str(result))
  64. result = cc.Decrypt(c_rot2, keys.secretKey)
  65. result.SetLength(batch_size)
  66. print("x1 rotated by -2 = " + str(result))
  67. if __name__ == "__main__":
  68. main()