1+ # Initial Settings
2+ from openfhe import *
3+
4+ # Sample Program: Step 1: Set CryptoContext
5+ parameters = CCParamsBGVRNS ()
6+ parameters .SetPlaintextModulus (65537 )
7+ parameters .SetMultiplicativeDepth (2 )
8+
9+ cryptoContext = GenCryptoContext (parameters )
10+ # Enable features that you wish to use
11+ cryptoContext .Enable (PKESchemeFeature .PKE )
12+ cryptoContext .Enable (PKESchemeFeature .KEYSWITCH )
13+ cryptoContext .Enable (PKESchemeFeature .LEVELEDSHE )
14+
15+ # Sample Program: Step 2: Key Generation
16+
17+ # Generate a public/private key pair
18+ keypair = cryptoContext .KeyGen ()
19+
20+ # Generate the relinearization key
21+ cryptoContext .EvalMultKeyGen (keypair .secretKey )
22+
23+ # Generate the rotation evaluation keys
24+ cryptoContext .EvalRotateKeyGen (keypair .secretKey , [1 , 2 , - 1 , - 2 ])
25+
26+ # Sample Program: Step 3: Encryption
27+
28+ # First plaintext vector is encoded
29+ vectorOfInts1 = [1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]
30+ plaintext1 = cryptoContext .MakePackedPlaintext (vectorOfInts1 )
31+
32+ # Second plaintext vector is encoded
33+ vectorOfInts2 = [3 , 2 , 1 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]
34+ plaintext2 = cryptoContext .MakePackedPlaintext (vectorOfInts2 )
35+
36+ # Third plaintext vector is encoded
37+ vectorOfInts3 = [1 , 2 , 5 , 2 , 5 , 6 , 7 , 8 , 9 , 10 , 11 , 12 ]
38+ plaintext3 = cryptoContext .MakePackedPlaintext (vectorOfInts3 )
39+
40+ # The encoded vectors are encrypted
41+ ciphertext1 = cryptoContext .Encrypt (keypair .publicKey , plaintext1 )
42+ ciphertext2 = cryptoContext .Encrypt (keypair .publicKey , plaintext2 )
43+ ciphertext3 = cryptoContext .Encrypt (keypair .publicKey , plaintext3 )
44+
45+ # Sample Program: Step 4: Evaluation
46+
47+ # Homomorphic additions
48+ ciphertextAdd12 = cryptoContext .EvalAdd (ciphertext1 , ciphertext2 )
49+ ciphertextAddResult = cryptoContext .EvalAdd (ciphertextAdd12 , ciphertext3 )
50+
51+ # Homomorphic Multiplication
52+ ciphertextMult12 = cryptoContext .EvalMult (ciphertext1 , ciphertext2 )
53+ ciphertextMultResult = cryptoContext .EvalMult (ciphertextMult12 , ciphertext3 )
54+
55+ # Homomorphic Rotations
56+ ciphertextRot1 = cryptoContext .EvalRotate (ciphertext1 , 1 )
57+ ciphertextRot2 = cryptoContext .EvalRotate (ciphertext1 , 2 )
58+ ciphertextRot3 = cryptoContext .EvalRotate (ciphertext1 , - 1 )
59+ ciphertextRot4 = cryptoContext .EvalRotate (ciphertext1 , - 2 )
60+
61+ # Sample Program: Step 5: Decryption
62+
63+ # Decrypt the result of additions
64+ plaintextAddResult = Decrypt (ciphertextAddResult ,keypair .secretKey )
65+
66+ # Decrypt the result of multiplications
67+ plaintextMultResult = Decrypt (ciphertextMultResult ,keypair .secretKey )
68+
69+ # Decrypt the result of rotations
70+ plaintextRot1 = Decrypt (ciphertextRot1 ,keypair .secretKey )
71+ plaintextRot2 = Decrypt (ciphertextRot2 ,keypair .secretKey )
72+ plaintextRot3 = Decrypt (ciphertextRot3 ,keypair .secretKey )
73+ plaintextRot4 = Decrypt (ciphertextRot4 ,keypair .secretKey )
74+
75+
76+ plaintextRot1 .SetLength (len (vectorOfInts1 ))
77+ plaintextRot2 .SetLength (len (vectorOfInts1 ))
78+ plaintextRot3 .SetLength (len (vectorOfInts1 ))
79+ plaintextRot4 .SetLength (len (vectorOfInts1 ))
80+
81+ print ("Plaintext #1: " + str (plaintext1 ))
82+ print ("Plaintext #2: " + str (plaintext2 ))
83+ print ("Plaintext #3: " + str (plaintext3 ))
84+
85+ # Output Results
86+ print ("\n Results of homomorphic computations" )
87+ print ("#1 + #2 + #3 = " + str (plaintextAddResult ))
88+ print ("#1 * #2 * #3 = " + str (plaintextMultResult ))
89+ print ("Left rotation of #1 by 1 = " + str (plaintextRot1 ))
90+ print ("Left rotation of #1 by 2 = " + str (plaintextRot2 ))
91+ print ("Right rotation of #1 by 1 = " + str (plaintextRot3 ))
92+ print ("Right rotation of #1 by 2 = " + str (plaintextRot4 ))
0 commit comments