simple-integers-bgvrns.py 3.8 KB

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