simple-integers.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. # Initial Settings
  2. from openfhe import *
  3. # import openfhe.PKESchemeFeature as Feature
  4. def main():
  5. # Sample Program: Step 1: Set CryptoContext
  6. parameters = CCParamsBFVRNS()
  7. parameters.SetPlaintextModulus(65537)
  8. parameters.SetMultiplicativeDepth(2)
  9. crypto_context = GenCryptoContext(parameters)
  10. # Enable features that you wish to use
  11. crypto_context.Enable(PKESchemeFeature.PKE)
  12. crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
  13. crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
  14. # Sample Program: Step 2: Key Generation
  15. # Generate a public/private key pair
  16. key_pair = crypto_context.KeyGen()
  17. # Generate the relinearization key
  18. crypto_context.EvalMultKeyGen(key_pair.secretKey)
  19. # Generate the rotation evaluation keys
  20. crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])
  21. # Sample Program: Step 3: Encryption
  22. # First plaintext vector is encoded
  23. vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  24. plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
  25. # Second plaintext vector is encoded
  26. vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  27. plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
  28. # Third plaintext vector is encoded
  29. vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
  30. plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
  31. # The encoded vectors are encrypted
  32. ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
  33. ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
  34. ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
  35. # Sample Program: Step 4: Evaluation
  36. # Homomorphic additions
  37. ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
  38. ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
  39. # Homomorphic Multiplication
  40. ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
  41. ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
  42. # Homomorphic Rotations
  43. ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
  44. ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
  45. ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
  46. ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
  47. # Sample Program: Step 5: Decryption
  48. # Decrypt the result of additions
  49. plaintext_add_result = crypto_context.Decrypt(
  50. ciphertext_add_result, key_pair.secretKey
  51. )
  52. # Decrypt the result of multiplications
  53. plaintext_mult_result = crypto_context.Decrypt(
  54. ciphertext_mult_result, key_pair.secretKey
  55. )
  56. # Decrypt the result of rotations
  57. plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1, key_pair.secretKey)
  58. plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2, key_pair.secretKey)
  59. plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3, key_pair.secretKey)
  60. plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4, key_pair.secretKey)
  61. plaintextRot1.SetLength(len(vector_of_ints1))
  62. plaintextRot2.SetLength(len(vector_of_ints1))
  63. plaintextRot3.SetLength(len(vector_of_ints1))
  64. plaintextRot4.SetLength(len(vector_of_ints1))
  65. print("Plaintext #1: " + str(plaintext1))
  66. print("Plaintext #2: " + str(plaintext2))
  67. print("Plaintext #3: " + str(plaintext3))
  68. # Output Results
  69. print("\nResults of homomorphic computations")
  70. print("#1 + #2 + #3 = " + str(plaintext_add_result))
  71. print("#1 * #2 * #3 = " + str(plaintext_mult_result))
  72. print("Left rotation of #1 by 1 = " + str(plaintextRot1))
  73. print("Left rotation of #1 by 2 = " + str(plaintextRot2))
  74. print("Right rotation of #1 by 1 = " + str(plaintextRot3))
  75. print("Right rotation of #1 by 2 = " + str(plaintextRot4))
  76. if __name__ == "__main__":
  77. main()