Skip to content

Commit 2ca3ce6

Browse files
authored
Merge pull request #142 from openfheorg/dev
Updates to v0.8.7
2 parents 8501b3f + 2b5314e commit 2ca3ce6

25 files changed

+1430
-713
lines changed

.github/workflows/manual.yml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ on:
2828

2929
workflow_dispatch:
3030
inputs:
31-
# # Selects the compiler to use, this choice will be used in the COMPILERS_MAP as the key to
32-
# # retrieve the corresponding cmake compiler options to pass to the action
31+
# Selects the compiler to use, this choice will be used in the COMPILERS_MAP as the key to
32+
# retrieve the corresponding cmake compiler options to pass to the action
3333
compiler:
3434
description: 'Compiler type'
3535
type: choice
@@ -54,6 +54,9 @@ on:
5454
required: true
5555
default: 'main'
5656

57+
# cmake_args_map_openfhe_lib holds job specific additional cmake options. As we are testing openfhe-python here
58+
# and not openfhe-development, we do not link unittest, benchmarks, etc. for openfhe-development.
59+
# compiler flags, native_backend flag and OpenMP flag are set in generic_workflow.yml
5760
jobs:
5861
call:
5962
uses: openfheorg/openfhe-python/.github/workflows/generic_workflow.yml@github-ci
@@ -62,10 +65,8 @@ jobs:
6265
compiler: "${{ inputs.compiler }}"
6366
native_backend: "${{ inputs.native_backend }}"
6467
openfhe_development_branch: "${{ inputs.openfhe_development_branch }}"
65-
# cmake_args_map holds job specific additional cmake options. compiler flags, native_backend flag and
66-
# OpenMP flag are set in generic_workflow.yml
67-
cmake_args_map: '{
68-
"default" : "-DBUILD_EXTRAS=ON",
68+
cmake_args_map_openfhe_lib: '{
69+
"default" : "-DBUILD_BENCHMARKS=OFF -DBUILD_UNITTESTS=OFF -DBUILD_EXAMPLES=OFF",
6970
}'
7071

7172

.readthedocs.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# .readthedocs.yaml
2+
# Read the Docs configuration file
3+
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details
4+
5+
# Required
6+
version: 2
7+
8+
# Set the version of Python and other tools you might need
9+
build:
10+
os: ubuntu-22.04
11+
tools:
12+
python: "3.11"
13+
14+
# Build documentation in the docs/ directory with Sphinx
15+
sphinx:
16+
configuration: docs/conf.py
17+
18+
# We recommend specifying your dependencies to enable reproducible builds:
19+
# https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
20+
python:
21+
install:
22+
- requirements: docs/requirements.txt

CMakeLists.txt

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ project (OpenFHE-Python)
44

55
set(OPENFHE_PYTHON_VERSION_MAJOR 0)
66
set(OPENFHE_PYTHON_VERSION_MINOR 8)
7-
set(OPENFHE_PYTHON_VERSION_PATCH 6)
7+
set(OPENFHE_PYTHON_VERSION_PATCH 7)
88
set(OPENFHE_PYTHON_VERSION ${OPENFHE_PYTHON_VERSION_MAJOR}.${OPENFHE_PYTHON_VERSION_MINOR}.${OPENFHE_PYTHON_VERSION_PATCH})
99

1010
set(CMAKE_CXX_STANDARD 17)
@@ -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})

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,14 @@ which creates a lib folder, moves the built `.so` file into that lib folder, and
9191

9292
**Note** You may wish to copy the `.so` file to any projects of your own, or add it to your system path to source from.
9393

94+
## Running Tests
95+
96+
Run tests with [pytest](https://docs.pytest.org), which may be called `pytest-3` on your system. See the [testing readme](tests/README.md) for more information.
97+
98+
```bash
99+
pytest [--run-long]
100+
```
101+
94102
## Code Examples
95103

96104
To get familiar with the OpenFHE Python API, check out the examples:

docs/cryptoparams.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
CryptoParams
2+
============
3+
4+
The following crypto parameter objects are available per scheme **BFV**, **BGV** and **CKKS** respectively.
5+
6+
CyrptoParamsBFVRNS
7+
##################
8+
.. autoclass:: openfhe.CCParamsBFVRNS
9+
:members:
10+
:show-inheritance:
11+
12+
CryptoParamsBGVRNS
13+
##################
14+
.. autoclass:: openfhe.CCParamsBGVRNS
15+
:members:
16+
:show-inheritance:
17+
18+
19+
CryptoParamsCKKSRNS
20+
###################
21+
.. autoclass:: openfhe.CCParamsCKKSRNS
22+
:members:
23+
:show-inheritance:
24+

docs/index.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@ Fully Homomorphic Encryption (FHE) is a powerful cryptographic primitive that en
1414
while keeping the efficiency of C++ FHE operations.
1515

1616
.. toctree::
17-
:maxdepth: 1
17+
:maxdepth: 3
1818
:caption: API Reference:
1919

2020
cryptocontext
21+
cryptoparams
2122
ciphertext
2223
plaintext
2324
keys
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)