simple-real-numbers.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. print("\nResults of homomorphic additions: ")
  43. print(ptAdd)
  44. # We set the precision to 8 decimal digits for a nicer output.
  45. # If you want to see the error/noise introduced by CKKS, bump it up
  46. # to 15 and it should become visible.
  47. precision = 8
  48. print("\nResults of homomorphic computations:")
  49. result = cc.Decrypt(c1, keys.secretKey)
  50. result.SetLength(batch_size)
  51. print("x1 = " + result.GetFormattedValues(precision))
  52. # Decrypt the result of scalar multiplication
  53. result = cc.Decrypt(c_scalar, keys.secretKey)
  54. result.SetLength(batch_size)
  55. print("4 * x1 = " + result.GetFormattedValues(precision))
  56. # Decrypt the result of multiplication
  57. result = cc.Decrypt(c_mult, keys.secretKey)
  58. result.SetLength(batch_size)
  59. print("x1 * x2 = " + result.GetFormattedValues(precision))
  60. # Decrypt the result of rotations
  61. result = cc.Decrypt(c_rot1, keys.secretKey)
  62. result.SetLength(batch_size)
  63. print("\nIn rotations, very small outputs (~10^-10 here) correspond to 0's:")
  64. print("x1 rotated by 1 = " + result.GetFormattedValues(precision))
  65. result = cc.Decrypt(c_rot2, keys.secretKey)
  66. result.SetLength(batch_size)
  67. print("x1 rotated by -2 = " + result.GetFormattedValues(precision))
  68. if __name__ == "__main__":
  69. main()