simple-integers.py 3.5 KB

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