Skip to content

Commit b5595eb

Browse files
Changes required by openfhe-numpy (#230)
* Fixes for openfhe-numpy * Exposed API functions for openfhe-numpy * Small bug fix and formatting * Exposed more APIs * Changes to __init__.py and cryptocontext_wrapper.h * Changes to __init__.py v2 * Changes to binding * Cleanup * Corrected find_package(Python...) and added a new variable OPENFHE_REQUIRED_VERSION to specify the OpenFHE version externally" * Minor change for OPENFHE_REQUIRED_VERSION --------- Co-authored-by: Dmitriy Suponitskiy <[email protected]>
1 parent b161f73 commit b5595eb

File tree

7 files changed

+235
-233
lines changed

7 files changed

+235
-233
lines changed

CMakeLists.txt

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,24 @@ set(OPENFHE_PYTHON_VERSION_PATCH 0)
88
set(OPENFHE_PYTHON_VERSION_TWEAK 0)
99
set(OPENFHE_PYTHON_VERSION ${OPENFHE_PYTHON_VERSION_MAJOR}.${OPENFHE_PYTHON_VERSION_MINOR}.${OPENFHE_PYTHON_VERSION_PATCH}.${OPENFHE_PYTHON_VERSION_TWEAK})
1010

11+
# OpenFHE version can be specified externally (-DOPENFHE_REQUIRED_VERSION=1.3.0)
12+
if(NOT DEFINED OPENFHE_REQUIRED_VERSION)
13+
set(OPENFHE_REQUIRED_VERSION "1.3.0" CACHE STRING "Required OpenFHE version")
14+
else()
15+
# User provided OPENFHE_REQUIRED_VERSION via -D
16+
message(STATUS "Using user-specified OpenFHE version: ${OPENFHE_REQUIRED_VERSION}")
17+
endif()
18+
1119
set(CMAKE_CXX_STANDARD 17)
1220
option( BUILD_STATIC "Set to ON to include static versions of the library" OFF)
1321

1422
if(APPLE)
1523
set(CMAKE_CXX_VISIBILITY_PRESET default)
1624
endif()
1725

18-
find_package(OpenFHE 1.3.0 REQUIRED)
26+
find_package(OpenFHE ${OPENFHE_REQUIRED_VERSION} REQUIRED)
27+
message(STATUS "Building with OpenFHE version: ${OPENFHE_REQUIRED_VERSION}")
28+
1929
set(PYBIND11_FINDPYTHON ON)
2030
find_package(pybind11 REQUIRED)
2131

@@ -66,20 +76,13 @@ pybind11_add_module(openfhe
6676
### Python installation
6777
# Allow the user to specify the path to Python executable (if not provided, find it)
6878
option(PYTHON_EXECUTABLE_PATH "Path to Python executable" "")
69-
70-
if(NOT PYTHON_EXECUTABLE_PATH)
71-
# Find Python and its development components
72-
find_package(Python REQUIRED COMPONENTS Interpreter Development)
73-
else()
74-
# Set Python_EXECUTABLE to the specified path
79+
if(PYTHON_EXECUTABLE_PATH)
7580
set(Python_EXECUTABLE "${PYTHON_EXECUTABLE_PATH}")
7681
endif()
77-
78-
# Find Python interpreter
79-
find_package(PythonInterp REQUIRED)
82+
find_package(Python REQUIRED COMPONENTS Interpreter Development)
8083

8184
# Check Python version
82-
if(${PYTHON_VERSION_MAJOR} EQUAL 3 AND ${PYTHON_VERSION_MINOR} GREATER_EQUAL 10)
85+
if(${Python_VERSION_MAJOR} EQUAL 3 AND ${Python_VERSION_MINOR} GREATER_EQUAL 10)
8386
execute_process(
8487
COMMAND "${Python_EXECUTABLE}" -c "from sys import exec_prefix; print(exec_prefix)"
8588
OUTPUT_VARIABLE PYTHON_SITE_PACKAGES
@@ -101,3 +104,5 @@ else()
101104
endif()
102105
message("***** INSTALL IS AT ${Python_Install_Location}; to change, run cmake with -DCMAKE_INSTALL_PREFIX=/your/path")
103106
install(TARGETS openfhe LIBRARY DESTINATION ${Python_Install_Location})
107+
install(FILES ${CMAKE_SOURCE_DIR}/__init__.py DESTINATION ${Python_Install_Location})
108+

__init__.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import os
2+
import ctypes
3+
4+
5+
def load_shared_library(libname, paths):
6+
for path in paths:
7+
lib_path = os.path.join(path, libname)
8+
if os.path.exists(lib_path):
9+
return ctypes.CDLL(lib_path, mode=ctypes.RTLD_GLOBAL)
10+
11+
raise FileNotFoundError(
12+
f"Shared library {libname} not found in {paths}"
13+
)
14+
15+
# Search LD_LIBRARY_PATH
16+
ld_paths = os.environ.get("LD_LIBRARY_PATH", "").split(":")
17+
18+
if not any(ld_paths):
19+
# Path to the bundled `lib/` directory inside site-packages
20+
package_dir = os.path.abspath(os.path.dirname(__file__))
21+
internal_lib_dir = [os.path.join(package_dir, 'lib')]
22+
23+
# Shared libraries required
24+
shared_libs = [
25+
'libgomp.so',
26+
'libOPENFHEcore.so.1',
27+
'libOPENFHEbinfhe.so.1',
28+
'libOPENFHEpke.so.1',
29+
]
30+
31+
for libname in shared_libs:
32+
load_shared_library(libname, internal_lib_dir)
33+
34+
from .openfhe import *
35+
36+
else:
37+
# Shared libraries required
38+
# skip 'libgomp.so' if LD_LIBRARY_PATH is set as we should get it from the libgomp.so location
39+
shared_libs = [
40+
'libOPENFHEcore.so.1',
41+
'libOPENFHEbinfhe.so.1',
42+
'libOPENFHEpke.so.1',
43+
]
44+
45+
for libname in shared_libs:
46+
load_shared_library(libname, ld_paths)
47+
48+
# from .openfhe import *

openfhe/__init__.py

Lines changed: 0 additions & 1 deletion
This file was deleted.

setup.py

Lines changed: 0 additions & 81 deletions
This file was deleted.

src/include/pke/cryptocontext_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Plaintext MultipartyDecryptFusionWrapper(CryptoContext<DCRTPoly>& self,const std
5454

5555
const std::shared_ptr<std::map<uint32_t, EvalKey<DCRTPoly>>> GetEvalSumKeyMapWrapper(CryptoContext<DCRTPoly>& self, const std::string &id);
5656
PlaintextModulus GetPlaintextModulusWrapper(CryptoContext<DCRTPoly>& self);
57+
uint32_t GetBatchSizeWrapper(CryptoContext<DCRTPoly>& self);
5758
double GetModulusWrapper(CryptoContext<DCRTPoly>& self);
5859
void RemoveElementWrapper(Ciphertext<DCRTPoly>& self, uint32_t index);
5960
double GetScalingFactorRealWrapper(CryptoContext<DCRTPoly>& self, uint32_t l);

0 commit comments

Comments
 (0)