Browse Source

binfhe cryptocontext + keys + encryption

Rener Oliveira (Ubuntu WSL) 1 year ago
parent
commit
4dfcd54b5c
4 changed files with 135 additions and 7 deletions
  1. 2 1
      include/binfhe_bindings.h
  2. 5 0
      src/bindings.cpp
  3. 35 0
      src/binfhe/examples/boolean.py
  4. 93 6
      src/binfhe_bindings.cpp

+ 2 - 1
include/binfhe_bindings.h

@@ -5,5 +5,6 @@
 
 void bind_binfhe_enums(pybind11::module &m);
 void bind_binfhe_context(pybind11::module &m);
-
+void bind_binfhe_keys(pybind11::module &m);
+void bind_binfhe_ciphertext(pybind11::module &m);
 #endif // BINFHE_BINDINGS_H

+ 5 - 0
src/bindings.cpp

@@ -281,6 +281,7 @@ void bind_schemes(py::module &m){
 PYBIND11_MODULE(openfhe, m)
 {
     m.doc() = "Open-Source Fully Homomorphic Encryption Library";
+    // pke library
     bind_parameters(m);
     bind_crypto_context(m);
     bind_enums_and_constants(m);
@@ -290,5 +291,9 @@ PYBIND11_MODULE(openfhe, m)
     bind_decryption(m);
     bind_serialization(m);
     bind_schemes(m);
+    // binfhe library
+    bind_binfhe_enums(m);
     bind_binfhe_context(m);
+    bind_binfhe_keys(m);
+    bind_binfhe_ciphertext(m);
 }

+ 35 - 0
src/binfhe/examples/boolean.py

@@ -0,0 +1,35 @@
+from openfhe import *
+
+## Sample Program: Step 1: Set CryptoContext
+
+cc = BinFHEContext()
+
+"""
+STD128 is the security level of 128 bits of security based on LWE Estimator
+and HE standard. Other common options are TOY, MEDIUM, STD192, and STD256.
+MEDIUM corresponds to the level of more than 100 bits for both quantum and
+classical computer attacks
+"""
+cc.GenerateBinFHEContext(STD128,GINX)
+
+## Sample Program: Step 2: Key Generation
+
+# Generate the secret key
+sk = cc.KeyGen()
+
+print("Generating the bootstrapping keys...\n")
+
+# Generate the bootstrapping keys (refresh and switching keys)
+cc.BTKeyGen(sk)
+
+# Sample Program: Step 3: Encryption
+"""
+Encrypt two ciphertexts representing Boolean True (1).
+By default, freshly encrypted ciphertexts are bootstrapped.
+If you wish to get a fresh encryption without bootstrapping, write
+ct1 = cc.Encrypt(sk, 1, FRESH)
+"""
+ct1 = cc.Encrypt(sk, 1)
+ct2 = cc.Encrypt(sk, 1)
+
+# Sample Program: Step 4: Evaluation

+ 93 - 6
src/binfhe_bindings.cpp

@@ -1,4 +1,5 @@
 #include <pybind11/pybind11.h>
+#include <pybind11/operators.h>
 #include <iostream>
 #include "openfhe.h"
 #include "binfhe_bindings.h"
@@ -7,12 +8,98 @@
 using namespace lbcrypto;
 namespace py = pybind11;
 
-void bind_binfhe_enums(py::module &m) {
-    //just print hello world
-    std::cout << "Hello World!" << std::endl;
+LWECiphertext binfhe_EncryptWrapper(BinFHEContext &self, ConstLWEPrivateKey sk, const LWEPlaintext &m, BINFHE_OUTPUT output = BOOTSTRAPPED,
+                                    LWEPlaintextModulus p = 4, uint64_t mod = 0)
+{
+    NativeInteger mod_native_int = NativeInteger(mod);
+    return self.Encrypt(sk, m, output, p, mod_native_int);
 }
 
-void bind_binfhe_context(py::module &m) {
-    py::class_<BinFHEContext>(m,"BinFHEContext")
-        .def(py::init<>());
+void bind_binfhe_enums(py::module &m)
+{
+    py::enum_<BINFHE_PARAMSET>(m, "BINFHE_PARAMSET")
+        .value("TOY", BINFHE_PARAMSET::TOY)
+        .value("MEDIUM", BINFHE_PARAMSET::MEDIUM)
+        .value("STD128_AP", BINFHE_PARAMSET::STD128_AP)
+        .value("STD128_APOPT", BINFHE_PARAMSET::STD128_APOPT)
+        .value("STD128", BINFHE_PARAMSET::STD128)
+        .value("STD128_OPT", BINFHE_PARAMSET::STD128_OPT)
+        .value("STD192", BINFHE_PARAMSET::STD192)
+        .value("STD192_OPT", BINFHE_PARAMSET::STD192_OPT)
+        .value("STD256", BINFHE_PARAMSET::STD256)
+        .value("STD256_OPT", BINFHE_PARAMSET::STD256_OPT)
+        .value("STD128Q", BINFHE_PARAMSET::STD128Q)
+        .value("STD128Q_OPT", BINFHE_PARAMSET::STD128Q_OPT)
+        .value("STD192Q", BINFHE_PARAMSET::STD192Q)
+        .value("STD192Q_OPT", BINFHE_PARAMSET::STD192Q_OPT)
+        .value("STD256Q", BINFHE_PARAMSET::STD256Q)
+        .value("STD256Q_OPT", BINFHE_PARAMSET::STD256Q_OPT)
+        .value("SIGNED_MOD_TEST", BINFHE_PARAMSET::SIGNED_MOD_TEST);
+    m.attr("TOY") = py::cast(BINFHE_PARAMSET::TOY);
+    m.attr("MEDIUM") = py::cast(BINFHE_PARAMSET::MEDIUM);
+    m.attr("STD128_AP") = py::cast(BINFHE_PARAMSET::STD128_AP);
+    m.attr("STD128_APOPT") = py::cast(BINFHE_PARAMSET::STD128_APOPT);
+    m.attr("STD128") = py::cast(BINFHE_PARAMSET::STD128);
+    m.attr("STD128_OPT") = py::cast(BINFHE_PARAMSET::STD128_OPT);
+    m.attr("STD192") = py::cast(BINFHE_PARAMSET::STD192);
+    m.attr("STD192_OPT") = py::cast(BINFHE_PARAMSET::STD192_OPT);
+    m.attr("STD256") = py::cast(BINFHE_PARAMSET::STD256);
+    m.attr("STD256_OPT") = py::cast(BINFHE_PARAMSET::STD256_OPT);
+    m.attr("STD128Q") = py::cast(BINFHE_PARAMSET::STD128Q);
+    m.attr("STD128Q_OPT") = py::cast(BINFHE_PARAMSET::STD128Q_OPT);
+    m.attr("STD192Q") = py::cast(BINFHE_PARAMSET::STD192Q);
+    m.attr("STD192Q_OPT") = py::cast(BINFHE_PARAMSET::STD192Q_OPT);
+    m.attr("STD256Q") = py::cast(BINFHE_PARAMSET::STD256Q);
+    m.attr("STD256Q_OPT") = py::cast(BINFHE_PARAMSET::STD256Q_OPT);
+    m.attr("SIGNED_MOD_TEST") = py::cast(BINFHE_PARAMSET::SIGNED_MOD_TEST);
+
+    py::enum_<BINFHE_METHOD>(m, "BINFHE_METHOD")
+        .value("INVALID_METHOD", BINFHE_METHOD::INVALID_METHOD)
+        .value("AP", BINFHE_METHOD::AP)
+        .value("GINX", BINFHE_METHOD::GINX);
+    m.attr("INVALID_METHOD") = py::cast(BINFHE_METHOD::INVALID_METHOD);
+    m.attr("GINX") = py::cast(BINFHE_METHOD::GINX);
+    m.attr("AP") = py::cast(BINFHE_METHOD::AP);
+
+    py::enum_<BINFHE_OUTPUT>(m, "BINFHE_OUTPUT")
+        .value("INVALID_OUTPUT", BINFHE_OUTPUT::INVALID_OUTPUT)
+        .value("FRESH", BINFHE_OUTPUT::FRESH)
+        .value("BOOTSTRAPPED", BINFHE_OUTPUT::BOOTSTRAPPED);
+    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);
+}
+
+void bind_binfhe_keys(py::module &m)
+{
+    py::class_<LWEPrivateKeyImpl, std::shared_ptr<LWEPrivateKeyImpl>>(m, "LWEPrivateKey")
+        .def(py::init<>())
+        .def("GetLength", &LWEPrivateKeyImpl::GetLength)
+        .def(py::self == py::self)
+        .def(py::self != py::self);
+}
+void bind_binfhe_ciphertext(py::module &m)
+{
+    py::class_<LWECiphertextImpl, std::shared_ptr<LWECiphertextImpl>>(m, "LWECiphertext")
+        .def(py::init<>())
+        .def("GetLength", &LWECiphertextImpl::GetLength)
+        .def(py::self == py::self)
+        .def(py::self != py::self);
+}
+
+void bind_binfhe_context(py::module &m)
+{
+    py::class_<BinFHEContext>(m, "BinFHEContext")
+        .def(py::init<>())
+        .def("GenerateBinFHEContext", static_cast<void (BinFHEContext::*)(BINFHE_PARAMSET, BINFHE_METHOD)>(&BinFHEContext::GenerateBinFHEContext),
+             py::arg("set"), 
+             py::arg("method") = GINX)
+        .def("KeyGen", &BinFHEContext::KeyGen)
+        .def("BTKeyGen", &BinFHEContext::BTKeyGen)
+        .def("Encrypt", &binfhe_EncryptWrapper,
+             py::arg("sk"),
+             py::arg("m"),
+             py::arg("output") = BOOTSTRAPPED,
+             py::arg("p") = 4, 
+             py::arg("mod") = 0);
 }