Skip to content

Commit 1d4c690

Browse files
committed
0-parameter fully tested for BFV
1 parent 012fb12 commit 1d4c690

File tree

3 files changed

+73
-58
lines changed

3 files changed

+73
-58
lines changed

src/pke/udf/hpdic_hermes.cpp

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
#include <mysql.h>
2+
#include <iostream>
3+
#include "openfhe.h"
4+
5+
using namespace lbcrypto;
6+
7+
extern "C" {
8+
9+
// MySQL UDF initialization function (optional)
10+
bool hermes_udf_init(UDF_INIT* initid, UDF_ARGS* args, char* message) {
11+
return 0;
12+
}
13+
14+
// Cleanup function (optional)
15+
void hermes_udf_deinit(UDF_INIT* initid) {}
16+
17+
// Main UDF logic
18+
long long hermes_udf(UDF_INIT* initid, UDF_ARGS* args, char* is_null, char* error) {
19+
try {
20+
// Step 1: CryptoContext setup
21+
CCParams<CryptoContextBFVRNS> parameters;
22+
parameters.SetPlaintextModulus(65537);
23+
parameters.SetMultiplicativeDepth(2);
24+
CryptoContext<DCRTPoly> cc = GenCryptoContext(parameters);
25+
cc->Enable(PKE);
26+
cc->Enable(LEVELEDSHE);
27+
cc->Enable(ADVANCEDSHE); // 💥关键:EvalSum 等需要这个!
28+
29+
// Step 2: Key generation
30+
auto kp = cc->KeyGen();
31+
cc->EvalMultKeyGen(kp.secretKey);
32+
cc->EvalSumKeyGen(kp.secretKey); // Needed for inner product
33+
34+
// Step 3: Plaintext vectors
35+
std::vector<int64_t> v1 = {3, 4, 5}; // Simulated column 1
36+
std::vector<int64_t> v2 = {6, 7, 8}; // Simulated column 2
37+
38+
auto pt1 = cc->MakePackedPlaintext(v1);
39+
auto pt2 = cc->MakePackedPlaintext(v2);
40+
41+
// Step 4: Encrypt
42+
auto ct1 = cc->Encrypt(kp.publicKey, pt1);
43+
auto ct2 = cc->Encrypt(kp.publicKey, pt2);
44+
45+
// Step 5: Multiply elementwise
46+
auto ct_mul = cc->EvalMult(ct1, ct2);
47+
48+
// Step 6: Sum all slots to get inner product
49+
auto ct_inner = cc->EvalSum(ct_mul, v1.size());
50+
51+
// Step 7: Decrypt
52+
Plaintext pt_result;
53+
cc->Decrypt(kp.secretKey, ct_inner, &pt_result);
54+
pt_result->SetLength(1); // All slots contain the same inner product after EvalSum
55+
56+
int64_t result = pt_result->GetPackedValue()[0];
57+
58+
std::cerr << "[hermes_udf] Inner product = " << result << std::endl;
59+
60+
return result;
61+
62+
} catch (const std::exception& e) {
63+
std::cerr << "[hermes_udf] Exception: " << e.what() << std::endl;
64+
*is_null = 1;
65+
return 0;
66+
} catch (...) {
67+
std::cerr << "[hermes_udf] Unknown error" << std::endl;
68+
*is_null = 1;
69+
return 0;
70+
}
71+
}
72+
73+
}

src/pke/udf/hpdic_hermes_eval.cpp

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

0 commit comments

Comments
 (0)