Skip to content

Commit 012fb12

Browse files
author
Dongfang Zhao
committed
0-parameter OpenFHE works fine for MySQL
1 parent 938b616 commit 012fb12

File tree

7 files changed

+190
-1
lines changed

7 files changed

+190
-1
lines changed

src/pke/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ if( BUILD_STATIC )
121121
set(_pal_pke_compile_defs_static ${_compile_defs_static} PARENT_SCOPE)
122122
endif()
123123

124-
# === MySQL UDF Plugin ===
124+
# === HPDIC MOD: MySQL UDF Plugin ===
125125
add_library(hpdic_hermes SHARED udf/hpdic_hermes.cpp)
126126

127127
target_include_directories(hpdic_hermes PRIVATE

src/pke/udf/deploy_hermes_udf.sh

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash
2+
set -e
3+
4+
MYSQL_USER="hpdic"
5+
MYSQL_PASS="hpdic2023"
6+
7+
PLUGIN_NAME="libhpdic_hermes.so"
8+
PLUGIN_DIR="/usr/lib/mysql/plugin"
9+
OPENFHE_PLUGIN_LIB_DIR="$(pwd)/../../../build/lib/mysqlplugin"
10+
OPENFHE_CORE_LIB_DIR="$(pwd)/../../../build/lib"
11+
12+
SYSTEMD_OVERRIDE_DIR="/etc/systemd/system/mysql.service.d"
13+
SYSTEMD_OVERRIDE_FILE="${SYSTEMD_OVERRIDE_DIR}/openfhe-env.conf"
14+
15+
echo "[*] Step 1: Copy shared object and OpenFHE libs into MySQL plugin directory..."
16+
sudo cp -v "${OPENFHE_PLUGIN_LIB_DIR}/${PLUGIN_NAME}" "${PLUGIN_DIR}"
17+
18+
# Copy OpenFHE core libraries
19+
sudo cp -v ${OPENFHE_CORE_LIB_DIR}/libOPENFHE*.so* "${PLUGIN_DIR}"
20+
21+
echo "[*] Step 2: Disable AppArmor for mysqld..."
22+
if [ -e /etc/apparmor.d/usr.sbin.mysqld ]; then
23+
sudo ln -sf /etc/apparmor.d/usr.sbin.mysqld /etc/apparmor.d/disable/ || true
24+
sudo apparmor_parser -R /etc/apparmor.d/usr.sbin.mysqld || echo "[!] Warning: AppArmor profile removal reported already removed."
25+
else
26+
echo " [✓] No AppArmor profile found for mysqld. Skipping."
27+
fi
28+
29+
echo "[*] Step 3: Set LD_LIBRARY_PATH for MySQL via systemd override..."
30+
sudo mkdir -p "${SYSTEMD_OVERRIDE_DIR}"
31+
sudo tee "${SYSTEMD_OVERRIDE_FILE}" >/dev/null <<EOF
32+
[Service]
33+
Environment=LD_LIBRARY_PATH=${PLUGIN_DIR}
34+
EOF
35+
36+
echo "[*] Step 4: Reload systemd and restart MySQL..."
37+
sudo systemctl daemon-reexec
38+
sudo systemctl daemon-reload
39+
sudo systemctl restart mysql
40+
41+
echo "[*] Step 5: Register UDF in MySQL..."
42+
mysql -u "${MYSQL_USER}" -p"${MYSQL_PASS}" <<EOF
43+
DROP FUNCTION IF EXISTS hermes_udf;
44+
CREATE FUNCTION hermes_udf RETURNS INTEGER SONAME '${PLUGIN_NAME}';
45+
SELECT hermes_udf();
46+
EOF

src/pke/udf/hpdic_hermes_enc.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#include <mysql.h>
2+
#include <iostream>
3+
#include "openfhe.h"
4+
5+
using namespace lbcrypto;
6+
7+
extern "C" {
8+
9+
bool hermes_udf_init(UDF_INIT* initid, UDF_ARGS* args, char* message) {
10+
return 0;
11+
}
12+
13+
void hermes_udf_deinit(UDF_INIT* initid) {}
14+
15+
long long hermes_udf(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error) {
16+
// Step 1: Create BFV context
17+
CCParams<CryptoContextBFVRNS> parameters;
18+
parameters.SetPlaintextModulus(65537); // Large enough for simple integers
19+
parameters.SetMultiplicativeDepth(1);
20+
CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
21+
cc->Enable(PKE);
22+
23+
// Step 2: Key generation
24+
auto kp = cc->KeyGen();
25+
26+
// Step 3: Encrypt a constant
27+
Plaintext pt = cc->MakePackedPlaintext(std::vector<int64_t>{123});
28+
auto ct = cc->Encrypt(kp.publicKey, pt);
29+
30+
// Step 4: Decrypt
31+
Plaintext result;
32+
cc->Decrypt(kp.secretKey, ct, &result);
33+
result->SetLength(1);
34+
35+
// Step 5: Return the first slot value to MySQL
36+
return result->GetPackedValue()[0];
37+
}
38+
39+
}

src/pke/udf/hpdic_hermes_eval.cpp

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
#include <mysql.h>
2+
#include <iostream>
3+
#include "openfhe.h"
4+
5+
using namespace lbcrypto;
6+
7+
extern "C" {
8+
9+
// MySQL plugin init function (can remain minimal)
10+
bool hermes_udf_init(UDF_INIT* initid, UDF_ARGS* args, char* message) {
11+
return 0;
12+
}
13+
14+
void hermes_udf_deinit(UDF_INIT* initid) {}
15+
16+
// Main UDF function
17+
long long hermes_udf(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error) {
18+
// Step 1: CryptoContext setup
19+
CCParams<CryptoContextBFVRNS> parameters;
20+
parameters.SetPlaintextModulus(65537);
21+
parameters.SetMultiplicativeDepth(2);
22+
CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
23+
cc->Enable(PKE);
24+
cc->Enable(LEVELEDSHE);
25+
26+
// Step 2: KeyGen
27+
auto kp = cc->KeyGen();
28+
cc->EvalMultKeyGen(kp.secretKey);
29+
30+
// Step 3: Encode and Encrypt input vectors
31+
std::vector<int64_t> v1 = {3, 4, 5};
32+
std::vector<int64_t> v2 = {6, 7, 8};
33+
auto pt1 = cc->MakePackedPlaintext(v1);
34+
auto pt2 = cc->MakePackedPlaintext(v2);
35+
auto ct1 = cc->Encrypt(kp.publicKey, pt1);
36+
auto ct2 = cc->Encrypt(kp.publicKey, pt2);
37+
38+
// Step 4: Homomorphic operations
39+
auto ct_add = cc->EvalAdd(ct1, ct2);
40+
auto ct_mul = cc->EvalMult(ct1, ct2);
41+
42+
// Step 5: Decrypt
43+
Plaintext pt_add, pt_mul;
44+
cc->Decrypt(kp.secretKey, ct_add, &pt_add);
45+
cc->Decrypt(kp.secretKey, ct_mul, &pt_mul);
46+
47+
pt_add->SetLength(v1.size());
48+
pt_mul->SetLength(v1.size());
49+
50+
// Optional: Print for logging (visible only in syslog or stderr)
51+
std::cerr << "Addition result: " << pt_add << std::endl;
52+
std::cerr << "Multiplication result: " << pt_mul << std::endl;
53+
54+
// Step 6: Return first slot of addition as example
55+
return pt_add->GetPackedValue()[0];
56+
}
57+
58+
}
File renamed without changes.

src/pke/udf/rebuild_and_test.sh

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/bin/bash
2+
set -e
3+
4+
# Resolve current script directory
5+
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
6+
PROJECT_ROOT="$(realpath "${SCRIPT_DIR}/../../../")"
7+
BUILD_DIR="${PROJECT_ROOT}/build"
8+
UDF_CPP_PATH="${PROJECT_ROOT}/src/pke/udf/hpdic_hermes.cpp"
9+
DEPLOY_SCRIPT="${PROJECT_ROOT}/src/pke/udf/deploy_hermes_udf.sh"
10+
MYSQL_TEST_SQL="/tmp/hermes_udf_test.sql"
11+
MYSQL_USER="hpdic"
12+
MYSQL_PASS="hpdic2023"
13+
14+
echo "[*] Project root: ${PROJECT_ROOT}"
15+
echo "[*] Build dir: ${BUILD_DIR}"
16+
echo "[*] UDF source: ${UDF_CPP_PATH}"
17+
18+
# Step 0: Validate
19+
if [ ! -f "${UDF_CPP_PATH}" ]; then
20+
echo "[!] Source file not found: ${UDF_CPP_PATH}"
21+
exit 1
22+
fi
23+
24+
# Step 1: Rebuild
25+
echo "[*] Rebuilding OpenFHE plugin..."
26+
cd "${BUILD_DIR}"
27+
make -j $(nproc)
28+
29+
# Step 2: Re-deploy
30+
echo "[*] Deploying updated .so to MySQL plugin directory..."
31+
cd "${PROJECT_ROOT}/src/pke/udf"
32+
bash "${DEPLOY_SCRIPT}"
33+
34+
# Step 3: Register UDF and test
35+
echo "[*] Registering UDF and executing test query..."
36+
37+
cat <<EOF > ${MYSQL_TEST_SQL}
38+
DROP FUNCTION IF EXISTS hermes_udf;
39+
CREATE FUNCTION hermes_udf RETURNS INTEGER SONAME 'libhpdic_hermes.so';
40+
SELECT hermes_udf();
41+
EOF
42+
43+
mysql -u "${MYSQL_USER}" -p"${MYSQL_PASS}" < "${MYSQL_TEST_SQL}"
44+
rm -f "${MYSQL_TEST_SQL}"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DROP FUNCTION IF EXISTS hermes_udf;
2+
CREATE FUNCTION hermes_udf RETURNS STRING SONAME 'libhpdic_hermes.so';

0 commit comments

Comments
 (0)