|
@@ -1,49 +1,51 @@
|
|
|
-# Initial Setting
|
|
|
+# Initial Settings
|
|
|
from openfhe import *
|
|
|
# import openfhe.PKESchemeFeature as Feature
|
|
|
-# Creating the parameters object
|
|
|
-parameters = CCParamsBFVRNS()
|
|
|
|
|
|
-# Printing Default Multip. Depth and Plaintext Modulus
|
|
|
-print("Default BFV Plaintext Modulus = " + str(parameters.GetPlaintextModulus()))
|
|
|
-print("Default BFV Multiplicative Depth = " + str(parameters.GetMultiplicativeDepth()))
|
|
|
|
|
|
-# Setting different values
|
|
|
+# Sample Program: Step 1: Set CryptoContext
|
|
|
+parameters = CCParamsBFVRNS()
|
|
|
parameters.SetPlaintextModulus(65537)
|
|
|
parameters.SetMultiplicativeDepth(2)
|
|
|
|
|
|
-# Getting new values
|
|
|
-print("New BFV Plaintext Modulus = " + str(parameters.GetPlaintextModulus()))
|
|
|
-print("New BFV Multiplicative Depth = " + str(parameters.GetMultiplicativeDepth()))
|
|
|
-
|
|
|
cryptoContext = GenCryptoContext(parameters)
|
|
|
-
|
|
|
-# cryptoContext.SetKeyGenLevel(2)
|
|
|
-# print(cryptoContext.GetKeyGenLevel())
|
|
|
-print(PKESchemeFeature.__members__)
|
|
|
+# Enable features that you wish to use
|
|
|
cryptoContext.Enable(PKESchemeFeature.PKE)
|
|
|
cryptoContext.Enable(PKESchemeFeature.KEYSWITCH)
|
|
|
cryptoContext.Enable(PKESchemeFeature.LEVELEDSHE)
|
|
|
|
|
|
+# Sample Program: Step 2: Key Generation
|
|
|
+
|
|
|
+# Generate a public/private key pair
|
|
|
keypair = cryptoContext.KeyGen()
|
|
|
-print("Public Key: " + str(keypair.publicKey))
|
|
|
|
|
|
+# Generate the relinearization key
|
|
|
cryptoContext.EvalMultKeyGen(keypair.secretKey)
|
|
|
-cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2]);
|
|
|
|
|
|
+# Generate the rotation evaluation keys
|
|
|
+cryptoContext.EvalRotateKeyGen(keypair.secretKey, [1, 2, -1, -2])
|
|
|
+
|
|
|
+# Sample Program: Step 3: Encryption
|
|
|
+
|
|
|
+# First plaintext vector is encoded
|
|
|
vectorOfInts1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
|
-vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
|
-vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
|
plaintext1 = cryptoContext.MakePackedPlaintext(vectorOfInts1)
|
|
|
-plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
|
|
|
-plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
|
|
|
|
|
|
+# Second plaintext vector is encoded
|
|
|
+vectorOfInts2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
|
+plaintext2 = cryptoContext.MakePackedPlaintext(vectorOfInts2)
|
|
|
|
|
|
+# Third plaintext vector is encoded
|
|
|
+vectorOfInts3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
|
|
|
+plaintext3 = cryptoContext.MakePackedPlaintext(vectorOfInts3)
|
|
|
|
|
|
+# The encoded vectors are encrypted
|
|
|
ciphertext1 = cryptoContext.Encrypt(keypair.publicKey, plaintext1)
|
|
|
ciphertext2 = cryptoContext.Encrypt(keypair.publicKey, plaintext2)
|
|
|
ciphertext3 = cryptoContext.Encrypt(keypair.publicKey, plaintext3)
|
|
|
|
|
|
+# Sample Program: Step 4: Evaluation
|
|
|
+
|
|
|
# Homomorphic additions
|
|
|
ciphertextAdd12 = cryptoContext.EvalAdd(ciphertext1, ciphertext2)
|
|
|
ciphertextAddResult = cryptoContext.EvalAdd(ciphertextAdd12, ciphertext3)
|
|
@@ -58,16 +60,20 @@ ciphertextRot2 = cryptoContext.EvalRotate(ciphertext1, 2)
|
|
|
ciphertextRot3 = cryptoContext.EvalRotate(ciphertext1, -1)
|
|
|
ciphertextRot4 = cryptoContext.EvalRotate(ciphertext1, -2)
|
|
|
|
|
|
-# Decrypting
|
|
|
+# Sample Program: Step 5: Decryption
|
|
|
|
|
|
+# Decrypt the result of additions
|
|
|
plaintextAddResult = Decrypt(ciphertextAddResult,keypair.secretKey)
|
|
|
+
|
|
|
+# Decrypt the result of multiplications
|
|
|
plaintextMultResult = Decrypt(ciphertextMultResult,keypair.secretKey)
|
|
|
+
|
|
|
+# Decrypt the result of rotations
|
|
|
plaintextRot1 = Decrypt(ciphertextRot1,keypair.secretKey)
|
|
|
plaintextRot2 = Decrypt(ciphertextRot2,keypair.secretKey)
|
|
|
plaintextRot3 = Decrypt(ciphertextRot3,keypair.secretKey)
|
|
|
plaintextRot4 = Decrypt(ciphertextRot4,keypair.secretKey)
|
|
|
|
|
|
-plaintextRot1 = Decrypt(ciphertextRot1,keypair.secretKey)
|
|
|
|
|
|
plaintextRot1.SetLength(len(vectorOfInts1))
|
|
|
plaintextRot2.SetLength(len(vectorOfInts1))
|