Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions examples/binfhe/eval-function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
from openfhe import *

## Sample Program: Step 1: Set CryptoContext

cc = BinFHEContext()
cc.GenerateBinFHEContext(STD128, True, 12)

## Sample Program: Step 2: Key Generation
# Generate the secret key
sk = cc.KeyGen()

print("Generating the bootstrapping keys...")

# Generate the bootstrapping keys (refresh and switching keys)
cc.BTKeyGen(sk)

print("Completed the key generation.")

## Sample Program: Step 3: Create the to-be-evaluated funciton and obtain its corresponding LUT
p = cc.GetMaxPlaintextSpace() # Obtain the maximum plaintext space

# Initialize Function f(x) = x^3 % p
def fp(m,p1):
if m<p1:
return m**3 % p1
else:
return (m-p1//2)**3 % p1

# Generate LUT from function f(x)
lut = cc.GenerateLUTviaFunction(fp, p)

## Sample Program: Step 4: evalute f(x) homomorphically and decrypt
# Note that we check for all the possible plaintexts.
for i in range(p):
ct1 = cc.Encrypt(sk, i % p, BINFHE_OUTPUT.LARGE_DIM, p)

ct_cube = cc.EvalFunc(ct1, lut)

result = cc.Decrypt(sk, ct_cube, p)

print(f"input :{i}, expected :{fp(i, p)}, evaluated: {result}")
47 changes: 47 additions & 0 deletions examples/binfhe/eval-sign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
from openfhe import *
from math import log2

## Sample Program: Step 1: Set CryptoContext

cc = BinFHEContext()

"""
Set the ciphertext modulus to be 1 << 17
Note that normally we do not use this way to obtain the input ciphertext.
Instead, we assume that an LWE ciphertext with large ciphertext
modulus is already provided (e.g., by extracting from a CKKS ciphertext).
However, we do not provide such a step in this example.
Therefore, we use a brute force way to create a large LWE ciphertext.
"""
logQ = 17
cc.GenerateBinFHEContext(STD128, False, logQ, method=BINFHE_METHOD.GINX, timeOptimization=False)

Q = 1 << logQ

q = 4096
factor = 1 << int(logQ - log2(q))
p = cc.GetMaxPlaintextSpace() * factor

## Sample Program: Step 2: Key Generation
# Generate the secret key
sk = cc.KeyGen()

print("Generating the bootstrapping keys...")

# Generate the bootstrapping keys (refresh and switching keys)
cc.BTKeyGen(sk)

print("Completed the key generation...")

## Sample Program: Step 3: Extract the MSB and decrypt to check the result
# Note that we check for 8 different numbers
for i in range(8):
# We first encrypt with large Q
ct1 = cc.Encrypt(sk, int(p // 2 + i - 3), output=BINFHE_OUTPUT.LARGE_DIM, p=p, mod=Q)

# Get the MSB
ct1 = cc.EvalSign(ct1)

result = cc.Decrypt(sk, ct1, 2)

print(f"Input :{i}. Expected sign :{int(i >= 3)}. Evaluated sign: {result}")
6 changes: 5 additions & 1 deletion src/lib/binfhe_bindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,14 @@ void bind_binfhe_enums(py::module &m) {
py::enum_<BINFHE_OUTPUT>(m, "BINFHE_OUTPUT")
.value("INVALID_OUTPUT", BINFHE_OUTPUT::INVALID_OUTPUT)
.value("FRESH", BINFHE_OUTPUT::FRESH)
.value("BOOTSTRAPPED", BINFHE_OUTPUT::BOOTSTRAPPED);
.value("BOOTSTRAPPED", BINFHE_OUTPUT::BOOTSTRAPPED)
.value("LARGE_DIM", BINFHE_OUTPUT::LARGE_DIM)
.value("SMALL_DIM", BINFHE_OUTPUT::SMALL_DIM);
m.attr("INVALID_OUTPUT") = py::cast(BINFHE_OUTPUT::INVALID_OUTPUT);
m.attr("FRESH") = py::cast(BINFHE_OUTPUT::FRESH);
m.attr("BOOTSTRAPPED") = py::cast(BINFHE_OUTPUT::BOOTSTRAPPED);
m.attr("LARGE_DIM") = py::cast(BINFHE_OUTPUT::LARGE_DIM);
m.attr("SMALL_DIM") = py::cast(BINFHE_OUTPUT::SMALL_DIM);

py::enum_<BINGATE>(m, "BINGATE")
.value("OR", BINGATE::OR)
Expand Down