Skip to content

Commit

Permalink
Merge pull request #172 from openfheorg/dev
Browse files Browse the repository at this point in the history
Updates to v0.8.9
  • Loading branch information
yspolyakov authored Sep 10, 2024
2 parents 867a0bd + c1504f3 commit 2447d37
Show file tree
Hide file tree
Showing 24 changed files with 563 additions and 92 deletions.
34 changes: 19 additions & 15 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 8)
set(OPENFHE_PYTHON_VERSION_PATCH 9)
set(OPENFHE_PYTHON_VERSION ${OPENFHE_PYTHON_VERSION_MAJOR}.${OPENFHE_PYTHON_VERSION_MINOR}.${OPENFHE_PYTHON_VERSION_PATCH})

set(CMAKE_CXX_STANDARD 17)
Expand All @@ -14,7 +14,7 @@ if(APPLE)
set(CMAKE_CXX_VISIBILITY_PRESET default)
endif()

find_package(OpenFHE 1.2.0 REQUIRED)
find_package(OpenFHE 1.2.1 REQUIRED)
find_package(pybind11 REQUIRED)

set( CMAKE_CXX_FLAGS ${OpenFHE_CXX_FLAGS} )
Expand Down Expand Up @@ -75,20 +75,24 @@ 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
)
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
)
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})
if(CMAKE_INSTALL_PREFIX_INITIALIZED_TO_DEFAULT)
set(Python_Install_Location "${PYTHON_SITE_PACKAGES}")
else()
set(Python_Install_Location "${CMAKE_INSTALL_PREFIX}")
endif()
message("***** INSTALL IS AT ${Python_Install_Location}; to change, run cmake with -DCMAKE_INSTALL_PREFIX=/your/path")
install(TARGETS openfhe LIBRARY DESTINATION ${Python_Install_Location})
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ To install OpenFHE-python directly to your system, ensure the dependencies are s
pip install "pybind11[global]"
mkdir build
cd build
cmake .. # Alternatively, cmake .. -DOpenFHE_DIR=/path/to/installed/openfhe if you installed OpenFHE elsewhere
cmake .. # Alternatively, cmake .. -DCMAKE_PREFIX_PATH=/path/to/installed/openfhe if you installed OpenFHE elsewhere
make
make install # You may have to run sudo make install
```
Expand All @@ -50,11 +50,13 @@ If you see an error saying that one of OpenFHE .so files cannot be found when ru
add the path where the .so files reside to the `PYTHONPATH` environment variable:

```
export PYTHONPATH=(path_to_OpenFHE_so_files):$PYTHONPATH
export PYTHONPATH=(/path/to/installed/openfhe):$PYTHONPATH
```

In some environments (this happens rarely), it may also be necessary to add the OpenFHE libraries path to `LD_LIBRARY_PATH`.

If OpenFHE is not installed in the default location, then both `PYTHONPATH and LD_LIBRARY_PATH` must be set before running any Python example.

#### Conda

Alternatively you can install the library and handle the linking via Conda. Clone the repository, open a terminal in the repo folder and run the following commands:
Expand All @@ -73,7 +75,7 @@ Now, you would clone the repository, and run the following commands to install :
```bash
mkdir build
cd build
cmake .. # Add in -DOpenFHE_DIR=/path/to/installed/openfhe if you installed OpenFHE elsewhere
cmake .. # Add in -DCMAKE_PREFIX_PATH=/path/to/installed/openfhe if you installed OpenFHE elsewhere
make
make install # You may have to run sudo make install
```
Expand Down
6 changes: 3 additions & 3 deletions examples/pke/advanced-real-numbers-128.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,15 @@ def automatic_rescale_demo(scal_tech):
c_res3 = cc.EvalMult(cc.EvalAdd(c18,c9), 0.5) # Final result 3

result1 = cc.Decrypt(c_res1,keys.secretKey)
result.SetLength(batch_size)
result1.SetLength(batch_size)
print("x^18 + x^9 + 1 = ", result1)

result2 = cc.Decrypt(c_res2,keys.secretKey)
result.SetLength(batch_size)
result2.SetLength(batch_size)
print("x^18 + x^9 - 1 = ", result2)

result3 = cc.Decrypt(c_res3,keys.secretKey)
result.SetLength(batch_size)
result3.SetLength(batch_size)
print("0.5 * (x^18 + x^9) = ", result3)


Expand Down
12 changes: 8 additions & 4 deletions examples/pke/advanced-real-numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,8 @@ def hybrid_key_switching_demo1():
parameters.SetMultiplicativeDepth(5)
parameters.SetScalingModSize(50)
parameters.SetBatchSize(batch_size)
parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
if get_native_int()!=128:
parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
parameters.SetNumLargeDigits(dnum)

cc = GenCryptoContext(parameters)
Expand Down Expand Up @@ -167,7 +168,8 @@ def hybrid_key_switching_demo2():
parameters.SetMultiplicativeDepth(5)
parameters.SetScalingModSize(50)
parameters.SetBatchSize(batch_size)
parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
if get_native_int()!=128:
parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
parameters.SetNumLargeDigits(dnum)

cc = GenCryptoContext(parameters)
Expand Down Expand Up @@ -287,7 +289,8 @@ def fast_rotation_demo2():
parameters.SetMultiplicativeDepth(1)
parameters.SetScalingModSize(50)
parameters.SetBatchSize(batch_size)
parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
if get_native_int()!=128:
parameters.SetScalingTechnique(ScalingTechnique.FLEXIBLEAUTO)
parameters.SetKeySwitchTechnique(KeySwitchTechnique.BV)
parameters.SetFirstModSize(60)
parameters.SetDigitSize(digit_size)
Expand Down Expand Up @@ -361,7 +364,8 @@ def fast_rotation_demo2():


def main():
automatic_rescale_demo(ScalingTechnique.FLEXIBLEAUTO)
if get_native_int()!=128:
automatic_rescale_demo(ScalingTechnique.FLEXIBLEAUTO)
automatic_rescale_demo(ScalingTechnique.FIXEDAUTO)
manual_rescale_demo(ScalingTechnique.FIXEDMANUAL)
hybrid_key_switching_demo1()
Expand Down
18 changes: 10 additions & 8 deletions examples/pke/scheme-switching.py
Original file line number Diff line number Diff line change
Expand Up @@ -1002,16 +1002,17 @@ def ArgminViaSchemeSwitchingUnit():
slots = 32 # sparsely-packed
batchSize = slots
numValues = 32
scTech = FLEXIBLEAUTOEXT
multDepth = 9 + 3 + 1 + int(log2(numValues)) # 1 for CKKS to FHEW, 13 for FHEW to CKKS, log2(numValues) for argmin
if scTech == FLEXIBLEAUTOEXT:
multDepth += 1

parameters = CCParamsCKKSRNS()
if get_native_int()!=128:
scTech = FLEXIBLEAUTOEXT
multDepth += 1
parameters.SetScalingTechnique(scTech)

parameters.SetMultiplicativeDepth(multDepth)
parameters.SetScalingModSize(scaleModSize)
parameters.SetFirstModSize(firstModSize)
parameters.SetScalingTechnique(scTech)
parameters.SetSecurityLevel(sl)
parameters.SetRingDim(ringDim)
parameters.SetBatchSize(batchSize)
Expand Down Expand Up @@ -1119,16 +1120,17 @@ def ArgminViaSchemeSwitchingAltUnit():
slots = 32 # sparsely-packed
batchSize = slots
numValues = 32
scTech = FLEXIBLEAUTOEXT
multDepth = 9 + 3 + 1 + int(log2(numValues)) # 1 for CKKS to FHEW, 13 for FHEW to CKKS, log2(numValues) for argmin
if scTech == FLEXIBLEAUTOEXT:
multDepth += 1

parameters = CCParamsCKKSRNS()
if get_native_int()!=128:
scTech = FLEXIBLEAUTOEXT
multDepth += 1
parameters.SetScalingTechnique(scTech)

parameters.SetMultiplicativeDepth(multDepth)
parameters.SetScalingModSize(scaleModSize)
parameters.SetFirstModSize(firstModSize)
parameters.SetScalingTechnique(scTech)
parameters.SetSecurityLevel(sl)
parameters.SetRingDim(ringDim)
parameters.SetBatchSize(batchSize)
Expand Down
2 changes: 1 addition & 1 deletion examples/pke/simple-integers-serial-bgvrns.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def main_action():
# of the keys. When deserializing a context, OpenFHE checks for the tag and
# if it finds it in the CryptoContext map, it will return the stored version.
# Hence, we need to clear the context and clear the keys.
cryptoContext.ClearEvalMultKeys()
ClearEvalMultKeys()
cryptoContext.ClearEvalAutomorphismKeys()
ReleaseAllContexts()

Expand Down
2 changes: 1 addition & 1 deletion examples/pke/simple-integers-serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def main_action():
# of the keys. When deserializing a context, OpenFHE checks for the tag and
# if it finds it in the CryptoContext map, it will return the stored version.
# Hence, we need to clear the context and clear the keys.
cryptoContext.ClearEvalMultKeys()
ClearEvalMultKeys()
cryptoContext.ClearEvalAutomorphismKeys()
ReleaseAllContexts()

Expand Down
2 changes: 1 addition & 1 deletion examples/pke/simple-real-numbers-serial.py
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ def serverSetupAndWrite(multDepth, scaleModSize, batchSize):

def clientProcess():
# clientCC = CryptoContext()
# clientCC.ClearEvalMultKeys()
# clientCC.ClearEvalAutomorphismKeys()
ReleaseAllContexts()
ClearEvalMultKeys()

clientCC, res = DeserializeCryptoContext(datafolder + ccLocation, BINARY)
if not res:
Expand Down
17 changes: 9 additions & 8 deletions examples/pke/simple-real-numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,37 +51,38 @@ def main():
# Step 5: Decryption and output
# Decrypt the result of additions
ptAdd = cc.Decrypt(c_add, keys.secretKey)
print("\nResults of homomorphic additions: ")
print(ptAdd)

# We set the precision to 8 decimal digits for a nicer output.
# If you want to see the error/noise introduced by CKKS, bump it up
# to 15 and it should become visible.

precision = 8
print("Results of homomorphic computations:")
print("\nResults of homomorphic computations:")
result = cc.Decrypt(c1, keys.secretKey)
result.SetLength(batch_size)
print("x1 = " + str(result))
print("Estimated precision in bits: " + str(result.GetLogPrecision()))
print("x1 = " + result.GetFormattedValues(precision))

# Decrypt the result of scalar multiplication
result = cc.Decrypt(c_scalar, keys.secretKey)
result.SetLength(batch_size)
print("4 * x1 = " + str(result))
print("4 * x1 = " + result.GetFormattedValues(precision))

# Decrypt the result of multiplication
result = cc.Decrypt(c_mult, keys.secretKey)
result.SetLength(batch_size)
print("x1 * x2 = " + str(result))
print("x1 * x2 = " + result.GetFormattedValues(precision))

# Decrypt the result of rotations
result = cc.Decrypt(c_rot1, keys.secretKey)
result.SetLength(batch_size)
print("In rotations, very small outputs (~10^-10 here) correspond to 0's:")
print("x1 rotated by 1 = " + str(result))
print("\nIn rotations, very small outputs (~10^-10 here) correspond to 0's:")
print("x1 rotated by 1 = " + result.GetFormattedValues(precision))

result = cc.Decrypt(c_rot2, keys.secretKey)
result.SetLength(batch_size)
print("x1 rotated by -2 = " + str(result))
print("x1 rotated by -2 = " + result.GetFormattedValues(precision))


if __name__ == "__main__":
Expand Down
5 changes: 3 additions & 2 deletions examples/pke/tckks-interactive-mp-bootstrapping-Chebyschev.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ def main():
# Same test with different rescaling techniques in CKKS
TCKKSCollectiveBoot(FIXEDMANUAL)
TCKKSCollectiveBoot(FIXEDAUTO)
TCKKSCollectiveBoot(FLEXIBLEAUTO)
TCKKSCollectiveBoot(FLEXIBLEAUTOEXT)
if get_native_int()!=128:
TCKKSCollectiveBoot(FLEXIBLEAUTO)
TCKKSCollectiveBoot(FLEXIBLEAUTOEXT)

print("Interactive (3P) Bootstrapping Ciphertext [Chebyshev] (TCKKS) terminated gracefully!")

Expand Down
7 changes: 4 additions & 3 deletions examples/pke/tckks-interactive-mp-bootstrapping.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ def main():
# Same test with different rescaling techniques in CKKS
TCKKSCollectiveBoot(FIXEDMANUAL)
TCKKSCollectiveBoot(FIXEDAUTO)
TCKKSCollectiveBoot(FLEXIBLEAUTO)
TCKKSCollectiveBoot(FLEXIBLEAUTOEXT)
if get_native_int()!=128:
TCKKSCollectiveBoot(FLEXIBLEAUTO)
TCKKSCollectiveBoot(FLEXIBLEAUTOEXT)

print("Interactive Multi-Party Bootstrapping Ciphertext (TCKKS) terminated gracefully!\n")

Expand Down Expand Up @@ -170,4 +171,4 @@ def TCKKSCollectiveBoot(scaleTech):
print("\n============================ INTERACTIVE DECRYPTION ENDED ============================\n")

if __name__ == "__main__":
main()
main()
2 changes: 1 addition & 1 deletion src/include/binfhe/binfhecontext_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ std::vector<uint64_t> GenerateLUTviaFunctionWrapper(BinFHEContext &self, py::fun
NativeInteger StaticFunction(NativeInteger m, NativeInteger p);

// Define static variables to hold the state
extern py::function static_f;
// extern py::function static_f;

LWECiphertext EvalFuncWrapper(BinFHEContext &self, ConstLWECiphertext &ct, const std::vector<uint64_t> &LUT);
#endif // BINFHE_CRYPTOCONTEXT_BINDINGS_H
33 changes: 19 additions & 14 deletions src/include/pke/cryptocontext_wrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,32 +28,34 @@
#ifndef OPENFHE_CRYPTOCONTEXT_BINDINGS_H
#define OPENFHE_CRYPTOCONTEXT_BINDINGS_H

#include "bindings.h"
#include "openfhe.h"

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <vector>
#include <algorithm>
#include <complex>
#include "openfhe.h"
#include "bindings.h"


namespace py = pybind11;
using namespace lbcrypto;
using ParmType = typename DCRTPoly::Params;

Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly>& self,
ConstCiphertext<DCRTPoly> ciphertext);
Ciphertext<DCRTPoly> EvalFastRotationPrecomputeWrapper(CryptoContext<DCRTPoly> &self,
ConstCiphertext<DCRTPoly> ciphertext);

Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly>& self,
ConstCiphertext<DCRTPoly> ciphertext,
const usint index,
const usint m,
ConstCiphertext<DCRTPoly> digits);
Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly>& self,ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);
Ciphertext<DCRTPoly> EvalFastRotationWrapper(CryptoContext<DCRTPoly> &self,
ConstCiphertext<DCRTPoly> ciphertext,
const usint index,
const usint m,
ConstCiphertext<DCRTPoly> digits);
Ciphertext<DCRTPoly> EvalFastRotationExtWrapper(CryptoContext<DCRTPoly> &self, ConstCiphertext<DCRTPoly> ciphertext, const usint index, ConstCiphertext<DCRTPoly> digits, bool addFirst);

Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,
ConstCiphertext<DCRTPoly> ciphertext,const PrivateKey<DCRTPoly> privateKey);
Plaintext DecryptWrapper(CryptoContext<DCRTPoly>& self,
const PrivateKey<DCRTPoly> privateKey,ConstCiphertext<DCRTPoly> ciphertext);
Plaintext DecryptWrapper(CryptoContext<DCRTPoly> &self,
ConstCiphertext<DCRTPoly> ciphertext, const PrivateKey<DCRTPoly> privateKey);
Plaintext DecryptWrapper(CryptoContext<DCRTPoly> &self,
const PrivateKey<DCRTPoly> privateKey, ConstCiphertext<DCRTPoly> ciphertext);
Plaintext MultipartyDecryptFusionWrapper(CryptoContext<DCRTPoly>& self,const std::vector<Ciphertext<DCRTPoly>>& partialCiphertextVec);

const std::map<usint, EvalKey<DCRTPoly>> EvalAutomorphismKeyGenWrapper(CryptoContext<DCRTPoly>& self,const PrivateKey<DCRTPoly> privateKey,const std::vector<usint> &indexList);
Expand All @@ -65,4 +67,7 @@ const double GetScalingFactorRealWrapper(CryptoContext<DCRTPoly>& self, uint32_t
const uint64_t GetModulusCKKSWrapper(CryptoContext<DCRTPoly>& self);
const ScalingTechnique GetScalingTechniqueWrapper(CryptoContext<DCRTPoly>& self);
const usint GetDigitSizeWrapper(CryptoContext<DCRTPoly>& self);

void ClearEvalMultKeysWrapper();

#endif // OPENFHE_CRYPTOCONTEXT_BINDINGS_H
Loading

0 comments on commit 2447d37

Please sign in to comment.