simple-integers-serial-bgvrns.py 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. # Initial Settings
  2. from openfhe import *
  3. # import openfhe.PKESchemeFeature as Feature
  4. datafolder = 'demoData'
  5. print("This program requres the subdirectory `" + datafolder + "' to exist, otherwise you will get an error writing serializations.")
  6. # Sample Program: Step 1: Set CryptoContext
  7. parameters = CCParamsBGVRNS()
  8. parameters.SetPlaintextModulus(65537)
  9. parameters.SetMultiplicativeDepth(2)
  10. cryptoContext = GenCryptoContext(parameters)
  11. # Enable features that you wish to use
  12. cryptoContext.Enable(PKESchemeFeature.PKE)
  13. cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
  14. cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
  15. # Serialize cryptocontext
  16. if not SerializeToFile(datafolder + "/cryptocontext.txt", cryptoContext, BINARY):
  17. raise Exception("Error writing serialization of the crypto context to cryptocontext.txt")
  18. print("The cryptocontext has been serialized.")
  19. # Sample Program: Step 2: Key Generation
  20. # Generate a public/private key pair
  21. keypair = cryptoContext.KeyGen()
  22. print("The keypair has been generated.")
  23. # Serialize the public key
  24. if not SerializeToFile(datafolder + "/key-public.txt", keypair.publicKey, BINARY):
  25. raise Exception("Error writing serialization of the public key to key-public.txt")
  26. print("The public key has been serialized.")
  27. # Serialize the secret key
  28. if not SerializeToFile(datafolder + "/key-secret.txt", keypair.secretKey, BINARY):
  29. raise Exception("Error writing serialization of the secret key to key-secret.txt")
  30. print("The secret key has been serialized.")
  31. # Generate the relinearization key
  32. cryptoContext.EvalMultKeyGen(keypair.secretKey)
  33. print("The relinearization key has been generated.")
  34. # Serialize the relinearization key
  35. if not cryptoContext.SerializeEvalMultKey(datafolder + "/key-eval-mult.txt",BINARY):
  36. raise Exception("Error writing serialization of the eval mult keys to \"key-eval-mult.txt\"")
  37. print("The relinearization key has been serialized.")
  38. # Generate the rotation evaluation keys
  39. cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2])
  40. print("The rotation evaluation keys have been generated.")
  41. # Serialize the rotation evaluation keys
  42. if not cryptoContext.SerializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt",BINARY):
  43. raise Exception("Error writing serialization of the eval rotate keys to \"key-eval-rot.txt\"")
  44. print("The rotation evaluation keys have been serialized.")
  45. # Sample Program: Step 3: Encryption
  46. # First plaintext vector is encoded
  47. vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  48. plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
  49. # Second plaintext vector is encoded
  50. vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
  51. plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
  52. # Third plaintext vector is encoded
  53. vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
  54. plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
  55. # The encoded vectors are encrypted
  56. ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
  57. ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
  58. ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
  59. print("The plaintexts have been encrypted.")
  60. if not SerializeToFile(datafolder + "/ciphertext1.txt", ciphertext1, BINARY):
  61. raise Exception("Error writing serialization of ciphertext 1 to ciphertext1.txt")
  62. print("The first ciphertext has been serialized.")
  63. if not SerializeToFile(datafolder + "/ciphertext2.txt", ciphertext2, BINARY):
  64. raise Exception("Error writing serialization of ciphertext2 to ciphertext2.txt")
  65. print("The second ciphertext has been serialized.")
  66. if not SerializeToFile(datafolder + "/ciphertext3.txt", ciphertext3, BINARY):
  67. raise Exception("Error writing serialization of ciphertext3 to ciphertext3.txt")
  68. print("The third ciphertext has been serialized.")
  69. # Sample Program: Step 4: Evaluation
  70. # OpenFHE maintains an internal map of CryptoContext objects which are
  71. # indexed by a tag and the tag is applied to both the CryptoContext and some
  72. # of the keys. When deserializing a context, OpenFHE checks for the tag and
  73. # if it finds it in the CryptoContext map, it will return the stored version.
  74. # Hence, we need to clear the context and clear the keys.
  75. cryptoContext.ClearEvalMultKeys()
  76. cryptoContext.ClearEvalAutomorphismKeys()
  77. ReleaseAllContexts()
  78. # Deserialize the crypto context
  79. cc = CryptoContext()
  80. if not DeserializeFromFile(datafolder + "/cryptocontext.txt", cc, BINARY):
  81. raise Exception("Error reading serialization of the crypto context from cryptocontext.txt")
  82. print("The cryptocontext has been deserialized.")
  83. # Deserialize the public key
  84. pk = PublicKey()
  85. if not DeserializeFromFile(datafolder + "/key-public.txt", pk, BINARY):
  86. raise Exception("Error reading serialization of the public key from key-public.txt")
  87. print("The public key has been deserialized.")
  88. # if cryptoContext.DeserializeEvalMultKey(datafolder + "/key-eval-mult.txt",BINARY):
  89. # raise Exception("Could not deserialize the eval mult key file")
  90. # print("The relinearization key has been deserialized.")
  91. # if cryptoContext.DeserializeEvalAutomorphismKey(datafolder + "/key-eval-rot.txt",BINARY):
  92. # raise Exception("Could not deserialize the eval rotation key file")
  93. # print("Deserialized the eval rotation keys.")
  94. # Deserialize the ciphertexts
  95. ct1 = Ciphertext()
  96. ct2 = Ciphertext()
  97. ct3 = Ciphertext()
  98. if not DeserializeFromFile(datafolder + "/ciphertext1.txt", ct1, BINARY):
  99. raise Exception("Could not read the ciphertext")
  100. print("The first ciphertext has been deserialized.")
  101. if not DeserializeFromFile(datafolder + "/ciphertext2.txt", ct2, BINARY):
  102. raise Exception("Could not read the ciphertext")
  103. print("The second ciphertext has been deserialized.")
  104. if not DeserializeFromFile(datafolder + "/ciphertext3.txt", ct3, BINARY):
  105. raise Exception("Could not read the ciphertext")
  106. print("The third ciphertext has been deserialized.")
  107. # Homomorphic addition
  108. ciphertextAdd12 = cc.EvalAdd(ct1, ct2)
  109. ciphertextAddResult = cc.EvalAdd(ciphertextAdd12, ct3)
  110. # Homomorphic multiplication
  111. ciphertextMult12 = cc.EvalMult(ct1, ct2)
  112. ciphertextMultResult = cc.EvalMult(ciphertextMult12, ct3)
  113. # Homomorphic rotation
  114. ciphertextRot1 = cc.EvalRotate(ct1, 1)
  115. ciphertextRot2 = cc.EvalRotate(ct2, 2)
  116. ciphertextRot3 = cc.EvalRotate(ct3, -1)
  117. ciphertextRot4 = cc.EvalRotate(ct3, -2)