Skip to content

Commit

Permalink
Merge pull request #142 from openfheorg/dev
Browse files Browse the repository at this point in the history
Updates to v0.8.7
  • Loading branch information
yspolyakov committed Jun 12, 2024
2 parents 8501b3f + 2b5314e commit 2ca3ce6
Show file tree
Hide file tree
Showing 25 changed files with 1,430 additions and 713 deletions.
13 changes: 7 additions & 6 deletions .github/workflows/manual.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ on:

workflow_dispatch:
inputs:
# # Selects the compiler to use, this choice will be used in the COMPILERS_MAP as the key to
# # retrieve the corresponding cmake compiler options to pass to the action
# Selects the compiler to use, this choice will be used in the COMPILERS_MAP as the key to
# retrieve the corresponding cmake compiler options to pass to the action
compiler:
description: 'Compiler type'
type: choice
Expand All @@ -54,6 +54,9 @@ on:
required: true
default: 'main'

# cmake_args_map_openfhe_lib holds job specific additional cmake options. As we are testing openfhe-python here
# and not openfhe-development, we do not link unittest, benchmarks, etc. for openfhe-development.
# compiler flags, native_backend flag and OpenMP flag are set in generic_workflow.yml
jobs:
call:
uses: openfheorg/openfhe-python/.github/workflows/generic_workflow.yml@github-ci
Expand All @@ -62,10 +65,8 @@ jobs:
compiler: "${{ inputs.compiler }}"
native_backend: "${{ inputs.native_backend }}"
openfhe_development_branch: "${{ inputs.openfhe_development_branch }}"
# cmake_args_map holds job specific additional cmake options. compiler flags, native_backend flag and
# OpenMP flag are set in generic_workflow.yml
cmake_args_map: '{
"default" : "-DBUILD_EXTRAS=ON",
cmake_args_map_openfhe_lib: '{
"default" : "-DBUILD_BENCHMARKS=OFF -DBUILD_UNITTESTS=OFF -DBUILD_EXAMPLES=OFF",
}'


22 changes: 22 additions & 0 deletions .readthedocs.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# .readthedocs.yaml
# Read the Docs configuration file
# See https://docs.readthedocs.io/en/stable/config-file/v2.html for details

# Required
version: 2

# Set the version of Python and other tools you might need
build:
os: ubuntu-22.04
tools:
python: "3.11"

# Build documentation in the docs/ directory with Sphinx
sphinx:
configuration: docs/conf.py

# We recommend specifying your dependencies to enable reproducible builds:
# https://docs.readthedocs.io/en/stable/guides/reproducible-builds.html
python:
install:
- requirements: docs/requirements.txt
19 changes: 17 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ project (OpenFHE-Python)

set(OPENFHE_PYTHON_VERSION_MAJOR 0)
set(OPENFHE_PYTHON_VERSION_MINOR 8)
set(OPENFHE_PYTHON_VERSION_PATCH 6)
set(OPENFHE_PYTHON_VERSION_PATCH 7)
set(OPENFHE_PYTHON_VERSION ${OPENFHE_PYTHON_VERSION_MAJOR}.${OPENFHE_PYTHON_VERSION_MINOR}.${OPENFHE_PYTHON_VERSION_PATCH})

set(CMAKE_CXX_STANDARD 17)
Expand Down Expand Up @@ -69,11 +69,26 @@ else()
# Set Python_EXECUTABLE to the specified path
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE_PATH}")
endif()

# Find Python interpreter
find_package(PythonInterp REQUIRED)

# Check Python version
if(${PYTHON_VERSION_MAJOR} EQUAL 3 AND ${PYTHON_VERSION_MINOR} GREATER_EQUAL 10)
execute_process(
COMMAND "${Python_EXECUTABLE}" -c "from sys import exec_prefix; print(exec_prefix)"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
OUTPUT_STRIP_TRAILING_WHITESPACE
)
else()
execute_process(
COMMAND "${Python_EXECUTABLE}" -c "from distutils.sysconfig import get_python_lib; print(get_python_lib())"
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
OUTPUT_STRIP_TRAILING_WHITESPACE
)
)
endif()



message(STATUS "Python site packages directory: ${PYTHON_SITE_PACKAGES}")
install(TARGETS openfhe LIBRARY DESTINATION ${PYTHON_SITE_PACKAGES})
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,14 @@ which creates a lib folder, moves the built `.so` file into that lib folder, and

**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.

## Running Tests

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.

```bash
pytest [--run-long]
```

## Code Examples

To get familiar with the OpenFHE Python API, check out the examples:
Expand Down
24 changes: 24 additions & 0 deletions docs/cryptoparams.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
CryptoParams
============

The following crypto parameter objects are available per scheme **BFV**, **BGV** and **CKKS** respectively.

CyrptoParamsBFVRNS
##################
.. autoclass:: openfhe.CCParamsBFVRNS
:members:
:show-inheritance:

CryptoParamsBGVRNS
##################
.. autoclass:: openfhe.CCParamsBGVRNS
:members:
:show-inheritance:


CryptoParamsCKKSRNS
###################
.. autoclass:: openfhe.CCParamsCKKSRNS
:members:
:show-inheritance:

3 changes: 2 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ Fully Homomorphic Encryption (FHE) is a powerful cryptographic primitive that en
while keeping the efficiency of C++ FHE operations.

.. toctree::
:maxdepth: 1
:maxdepth: 3
:caption: API Reference:

cryptocontext
cryptoparams
ciphertext
plaintext
keys
Expand Down
144 changes: 77 additions & 67 deletions examples/pke/simple-integers-bgvrns.py
Original file line number Diff line number Diff line change
@@ -1,94 +1,104 @@
# Initial Settings
from openfhe import *
import os

# import openfhe.PKESchemeFeature as Feature


# Sample Program: Step 1: Set CryptoContext
parameters = CCParamsBGVRNS()
parameters.SetPlaintextModulus(65537)
parameters.SetMultiplicativeDepth(2)
def main():
# Sample Program: Step 1: Set CryptoContext
parameters = CCParamsBGVRNS()
parameters.SetPlaintextModulus(65537)
parameters.SetMultiplicativeDepth(2)

crypto_context = GenCryptoContext(parameters)
# Enable features that you wish to use
crypto_context.Enable(PKESchemeFeature.PKE)
crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)

crypto_context = GenCryptoContext(parameters)
# Enable features that you wish to use
crypto_context.Enable(PKESchemeFeature.PKE)
crypto_context.Enable(PKESchemeFeature.KEYSWITCH)
crypto_context.Enable(PKESchemeFeature.LEVELEDSHE)
# Sample Program: Step 2: Key Generation

# Sample Program: Step 2: Key Generation
# Generate a public/private key pair
key_pair = crypto_context.KeyGen()

# Generate a public/private key pair
key_pair = crypto_context.KeyGen()
# Generate the relinearization key
crypto_context.EvalMultKeyGen(key_pair.secretKey)

# Generate the relinearization key
crypto_context.EvalMultKeyGen(key_pair.secretKey)
# Generate the rotation evaluation keys
crypto_context.EvalRotateKeyGen(key_pair.secretKey, [1, 2, -1, -2])

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

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

# First plaintext vector is encoded
vector_of_ints1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext1 = crypto_context.MakePackedPlaintext(vector_of_ints1)
# Second plaintext vector is encoded
vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)

# Second plaintext vector is encoded
vector_of_ints2 = [3, 2, 1, 4, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext2 = crypto_context.MakePackedPlaintext(vector_of_ints2)
# Third plaintext vector is encoded
vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)

# Third plaintext vector is encoded
vector_of_ints3 = [1, 2, 5, 2, 5, 6, 7, 8, 9, 10, 11, 12]
plaintext3 = crypto_context.MakePackedPlaintext(vector_of_ints3)
# The encoded vectors are encrypted
ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)

# The encoded vectors are encrypted
ciphertext1 = crypto_context.Encrypt(key_pair.publicKey, plaintext1)
ciphertext2 = crypto_context.Encrypt(key_pair.publicKey, plaintext2)
ciphertext3 = crypto_context.Encrypt(key_pair.publicKey, plaintext3)
# Sample Program: Step 4: Evaluation

# Sample Program: Step 4: Evaluation
# Homomorphic additions
ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)

# Homomorphic additions
ciphertext_add12 = crypto_context.EvalAdd(ciphertext1, ciphertext2)
ciphertext_add_result = crypto_context.EvalAdd(ciphertext_add12, ciphertext3)
# Homomorphic Multiplication
ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)

# Homomorphic Multiplication
ciphertext_mult12 = crypto_context.EvalMult(ciphertext1, ciphertext2)
ciphertext_mult_result = crypto_context.EvalMult(ciphertext_mult12, ciphertext3)
# Homomorphic Rotations
ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)

# Homomorphic Rotations
ciphertext_rot1 = crypto_context.EvalRotate(ciphertext1, 1)
ciphertext_rot2 = crypto_context.EvalRotate(ciphertext1, 2)
ciphertext_rot3 = crypto_context.EvalRotate(ciphertext1, -1)
ciphertext_rot4 = crypto_context.EvalRotate(ciphertext1, -2)
# Sample Program: Step 5: Decryption

# Sample Program: Step 5: Decryption
# Decrypt the result of additions
plaintext_add_result = crypto_context.Decrypt(
ciphertext_add_result, key_pair.secretKey
)

# Decrypt the result of additions
plaintext_add_result = crypto_context.Decrypt(ciphertext_add_result,key_pair.secretKey)
# Decrypt the result of multiplications
plaintext_mult_result = crypto_context.Decrypt(
ciphertext_mult_result, key_pair.secretKey
)

# Decrypt the result of multiplications
plaintext_mult_result = crypto_context.Decrypt(ciphertext_mult_result,key_pair.secretKey)
# Decrypt the result of rotations
plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1, key_pair.secretKey)
plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2, key_pair.secretKey)
plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3, key_pair.secretKey)
plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4, key_pair.secretKey)

# Decrypt the result of rotations
plaintextRot1 = crypto_context.Decrypt(ciphertext_rot1,key_pair.secretKey)
plaintextRot2 = crypto_context.Decrypt(ciphertext_rot2,key_pair.secretKey)
plaintextRot3 = crypto_context.Decrypt(ciphertext_rot3,key_pair.secretKey)
plaintextRot4 = crypto_context.Decrypt(ciphertext_rot4,key_pair.secretKey)
plaintextRot1.SetLength(len(vector_of_ints1))
plaintextRot2.SetLength(len(vector_of_ints1))
plaintextRot3.SetLength(len(vector_of_ints1))
plaintextRot4.SetLength(len(vector_of_ints1))

print("Plaintext #1: " + str(plaintext1))
print("Plaintext #2: " + str(plaintext2))
print("Plaintext #3: " + str(plaintext3))

plaintextRot1.SetLength(len(vector_of_ints1))
plaintextRot2.SetLength(len(vector_of_ints1))
plaintextRot3.SetLength(len(vector_of_ints1))
plaintextRot4.SetLength(len(vector_of_ints1))
# Output Results
print("\nResults of homomorphic computations")
print("#1 + #2 + #3 = " + str(plaintext_add_result))
print("#1 * #2 * #3 = " + str(plaintext_mult_result))
print("Left rotation of #1 by 1 = " + str(plaintextRot1))
print("Left rotation of #1 by 2 = " + str(plaintextRot2))
print("Right rotation of #1 by 1 = " + str(plaintextRot3))
print("Right rotation of #1 by 2 = " + str(plaintextRot4))

print("Plaintext #1: " + str(plaintext1))
print("Plaintext #2: " + str(plaintext2))
print("Plaintext #3: " + str(plaintext3))

# Output Results
print("\nResults of homomorphic computations")
print("#1 + #2 + #3 = " + str(plaintext_add_result))
print("#1 * #2 * #3 = " + str(plaintext_mult_result))
print("Left rotation of #1 by 1 = " + str(plaintextRot1))
print("Left rotation of #1 by 2 = " + str(plaintextRot2))
print("Right rotation of #1 by 1 = " + str(plaintextRot3))
print("Right rotation of #1 by 2 = " + str(plaintextRot4))
if __name__ == "__main__":
main()
Loading

0 comments on commit 2ca3ce6

Please sign in to comment.