Skip to content

Commit c53732a

Browse files
authored
Merge pull request #141 from openfheorg/ma-dev-issue-53-unittest
#53: add initial unittests
2 parents 87700c2 + ad69449 commit c53732a

File tree

9 files changed

+932
-644
lines changed

9 files changed

+932
-644
lines changed

CMakeLists.txt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,26 @@ else()
6969
# Set Python_EXECUTABLE to the specified path
7070
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE_PATH}")
7171
endif()
72+
73+
# Find Python interpreter
74+
find_package(PythonInterp REQUIRED)
75+
76+
# Check Python version
77+
if(${PYTHON_VERSION_MAJOR} EQUAL 3 AND ${PYTHON_VERSION_MINOR} GREATER_EQUAL 10)
78+
execute_process(
79+
COMMAND "${Python_EXECUTABLE}" -c "from sys import exec_prefix; print(exec_prefix)"
80+
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
81+
OUTPUT_STRIP_TRAILING_WHITESPACE
82+
)
83+
else()
7284
execute_process(
7385
COMMAND "${Python_EXECUTABLE}" -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
7486
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
7587
OUTPUT_STRIP_TRAILING_WHITESPACE
76-
)
88+
)
89+
endif()
90+
91+
7792

7893
message(STATUS "Python site packages directory: ${PYTHON_SITE_PACKAGES}")
7994
install(TARGETS openfhe LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES})
Lines changed: 77 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,104 @@
11
# Initial Settings
22
from openfhe import *
3+
import os
4+
35
# import openfhe.PKESchemeFeature as Feature
46

57

6-
# Sample Program: Step 1: Set CryptoContext
7-
parameters = CCParamsBGVRNS()
8-
parameters.SetPlaintextModulus(65537)
9-
parameters.SetMultiplicativeDepth(2)
8+
def main():
9+
# Sample Program: Step 1: Set CryptoContext
10+
parameters = CCParamsBGVRNS()
11+
parameters.SetPlaintextModulus(65537)
12+
parameters.SetMultiplicativeDepth(2)
13+
14+
crypto_context = GenCryptoContext(parameters)
15+
# Enable features that you wish to use
16+
crypto_context.Enable(PKESchemeFeature.PKE)
17+
crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
18+
crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
1019

11-
crypto_context = GenCryptoContext(parameters)
12-
# Enable features that you wish to use
13-
crypto_context.Enable(PKESchemeFeature.PKE)
14-
crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
15-
crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
20+
# Sample Program: Step 2: Key Generation
1621

17-
# Sample Program: Step 2: Key Generation
22+
# Generate a public/private key pair
23+
key_pair = crypto_context.KeyGen()
1824

19-
# Generate a public/private key pair
20-
key_pair = crypto_context.KeyGen()
25+
# Generate the relinearization key
26+
crypto_context.EvalMultKeyGen(key_pair.secretKey)
2127

22-
# Generate the relinearization key
23-
crypto_context.EvalMultKeyGen(key_pair.secretKey)
28+
# Generate the rotation evaluation keys
29+
crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])
2430

25-
# Generate the rotation evaluation keys
26-
crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])
31+
# Sample Program: Step 3: Encryption
2732

28-
# Sample Program: Step 3: Encryption
33+
# First plaintext vector is encoded
34+
vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
35+
plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
2936

30-
# First plaintext vector is encoded
31-
vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
32-
plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
37+
# Second plaintext vector is encoded
38+
vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
39+
plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
3340

34-
# Second plaintext vector is encoded
35-
vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
36-
plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
41+
# Third plaintext vector is encoded
42+
vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
43+
plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
3744

38-
# Third plaintext vector is encoded
39-
vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
40-
plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
45+
# The encoded vectors are encrypted
46+
ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
47+
ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
48+
ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
4149

42-
# The encoded vectors are encrypted
43-
ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
44-
ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
45-
ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
50+
# Sample Program: Step 4: Evaluation
4651

47-
# Sample Program: Step 4: Evaluation
52+
# Homomorphic additions
53+
ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
54+
ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
4855

49-
# Homomorphic additions
50-
ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
51-
ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
56+
# Homomorphic Multiplication
57+
ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
58+
ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
5259

53-
# Homomorphic Multiplication
54-
ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
55-
ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
60+
# Homomorphic Rotations
61+
ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
62+
ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
63+
ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
64+
ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
5665

57-
# Homomorphic Rotations
58-
ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
59-
ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
60-
ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
61-
ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
66+
# Sample Program: Step 5: Decryption
6267

63-
# Sample Program: Step 5: Decryption
68+
# Decrypt the result of additions
69+
plaintext_add_result = crypto_context.Decrypt(
70+
ciphertext_add_result, key_pair.secretKey
71+
)
6472

65-
# Decrypt the result of additions
66-
plaintext_add_result = crypto_context.Decrypt(ciphertext_add_result,key_pair.secretKey)
73+
# Decrypt the result of multiplications
74+
plaintext_mult_result = crypto_context.Decrypt(
75+
ciphertext_mult_result, key_pair.secretKey
76+
)
6777

68-
# Decrypt the result of multiplications
69-
plaintext_mult_result = crypto_context.Decrypt(ciphertext_mult_result,key_pair.secretKey)
78+
# Decrypt the result of rotations
79+
plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1, key_pair.secretKey)
80+
plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2, key_pair.secretKey)
81+
plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3, key_pair.secretKey)
82+
plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4, key_pair.secretKey)
7083

71-
# Decrypt the result of rotations
72-
plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1,key_pair.secretKey)
73-
plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2,key_pair.secretKey)
74-
plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3,key_pair.secretKey)
75-
plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4,key_pair.secretKey)
84+
plaintextRot1.SetLength(len(vector_of_ints1))
85+
plaintextRot2.SetLength(len(vector_of_ints1))
86+
plaintextRot3.SetLength(len(vector_of_ints1))
87+
plaintextRot4.SetLength(len(vector_of_ints1))
7688

89+
print("Plaintext #1: " + str(plaintext1))
90+
print("Plaintext #2: " + str(plaintext2))
91+
print("Plaintext #3: " + str(plaintext3))
7792

78-
plaintextRot1.SetLength(len(vector_of_ints1))
79-
plaintextRot2.SetLength(len(vector_of_ints1))
80-
plaintextRot3.SetLength(len(vector_of_ints1))
81-
plaintextRot4.SetLength(len(vector_of_ints1))
93+
# Output Results
94+
print("\nResults of homomorphic computations")
95+
print("#1 + #2 + #3 = " + str(plaintext_add_result))
96+
print("#1 * #2 * #3 = " + str(plaintext_mult_result))
97+
print("Left rotation of #1 by 1 = " + str(plaintextRot1))
98+
print("Left rotation of #1 by 2 = " + str(plaintextRot2))
99+
print("Right rotation of #1 by 1 = " + str(plaintextRot3))
100+
print("Right rotation of #1 by 2 = " + str(plaintextRot4))
82101

83-
print("Plaintext #1: " + str(plaintext1))
84-
print("Plaintext #2: " + str(plaintext2))
85-
print("Plaintext #3: " + str(plaintext3))
86102

87-
# Output Results
88-
print("\nResults of homomorphic computations")
89-
print("#1 + #2 + #3 = " + str(plaintext_add_result))
90-
print("#1 * #2 * #3 = " + str(plaintext_mult_result))
91-
print("Left rotation of #1 by 1 = " + str(plaintextRot1))
92-
print("Left rotation of #1 by 2 = " + str(plaintextRot2))
93-
print("Right rotation of #1 by 1 = " + str(plaintextRot3))
94-
print("Right rotation of #1 by 2 = " + str(plaintextRot4))
103+
if __name__ == "__main__":
104+
main()

0 commit comments

Comments
 (0)