function-evaluation.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. from openfhe import *
  2. import math
  3. def main():
  4. eval_logistic_example()
  5. eval_function_example()
  6. def eval_logistic_example():
  7. print("--------------------------------- EVAL LOGISTIC FUNCTION ---------------------------------\n")
  8. parameters = CCParamsCKKSRNS()
  9. parameters.SetSecurityLevel(SecurityLevel.HEStd_NotSet)
  10. parameters.SetRingDim(1 << 10)
  11. scaling_mod_size = 59
  12. first_mod_size = 60
  13. parameters.SetScalingModSize(scaling_mod_size)
  14. parameters.SetFirstModSize(first_mod_size)
  15. poly_degree = 16
  16. mult_depth = 6
  17. parameters.SetMultiplicativeDepth(mult_depth)
  18. cc = GenCryptoContext(parameters)
  19. cc.Enable(PKESchemeFeature.PKE)
  20. cc.Enable(PKESchemeFeature.KEYSWITCH)
  21. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  22. cc.Enable(PKESchemeFeature.ADVANCEDSHE)
  23. key_pair = cc.KeyGen()
  24. cc.EvalMultKeyGen(key_pair.secretKey)
  25. input = [-4, -3, -2, -1, 0, 1, 2, 3, 4]
  26. encoded_length = len(input)
  27. plaintext = cc.MakeCKKSPackedPlaintext(input)
  28. ciphertext = cc.Encrypt(key_pair.publicKey, plaintext)
  29. lower_bound = -4
  30. upper_bound = 4
  31. result = cc.EvalLogistic(ciphertext, lower_bound, upper_bound, poly_degree)
  32. plaintext_dec = cc.Decrypt(result, key_pair.secretKey)
  33. plaintext_dec.SetLength(encoded_length)
  34. expected_output = [0.0179885, 0.0474289, 0.119205, 0.268936, 0.5, 0.731064, 0.880795, 0.952571, 0.982011]
  35. print(f"Expected output\n\t {expected_output}\n")
  36. final_result = plaintext_dec.GetCKKSPackedValue()
  37. print(f"Actual output\n\t {final_result}\n")
  38. def eval_function_example():
  39. print("--------------------------------- EVAL SQUARE ROOT FUNCTION ---------------------------------\n")
  40. parameters = CCParamsCKKSRNS()
  41. parameters.SetSecurityLevel(SecurityLevel.HEStd_NotSet)
  42. parameters.SetRingDim(1 << 10)
  43. if get_native_int() == 128:
  44. scaling_mod_size = 78
  45. first_mod_size = 89
  46. else:
  47. scaling_mod_size = 50
  48. first_mod_size = 60
  49. parameters.SetScalingModSize(scaling_mod_size)
  50. parameters.SetFirstModSize(first_mod_size)
  51. poly_degree = 50
  52. mult_depth = 7
  53. parameters.SetMultiplicativeDepth(mult_depth)
  54. cc = GenCryptoContext(parameters)
  55. cc.Enable(PKESchemeFeature.PKE)
  56. cc.Enable(PKESchemeFeature.KEYSWITCH)
  57. cc.Enable(PKESchemeFeature.LEVELEDSHE)
  58. cc.Enable(PKESchemeFeature.ADVANCEDSHE)
  59. key_pair = cc.KeyGen()
  60. cc.EvalMultKeyGen(key_pair.secretKey)
  61. input = [1, 2, 3, 4, 5, 6, 7, 8, 9]
  62. encoded_length = len(input)
  63. plaintext = cc.MakeCKKSPackedPlaintext(input)
  64. ciphertext = cc.Encrypt(key_pair.publicKey, plaintext)
  65. lower_bound = 0
  66. upper_bound = 10
  67. result = cc.EvalChebyshevFunction(math.sqrt,ciphertext, lower_bound, upper_bound, poly_degree)
  68. plaintext_dec = cc.Decrypt(result, key_pair.secretKey)
  69. plaintext_dec.SetLength(encoded_length)
  70. expected_output = [1, 1.414213, 1.732050, 2, 2.236067, 2.449489, 2.645751, 2.828427, 3]
  71. print(f"Expected output\n\t {expected_output}\n")
  72. final_result = plaintext_dec.GetCKKSPackedValue()
  73. print(f"Actual output\n\t {final_result}\n")
  74. if __name__ == "__main__":
  75. main()