安裝完成了HElib庫后,上網查閱相關資料,掌握了了基本的使用方法。本章中我記錄了一些HElib庫中重要的類,以及如何使用它們。同時我會講解一些代碼案例來說明具體的使用方法。
首先,進行參數的初始化:
long p = 1021; // Plaintext base [default=2], should be a prime number long r = 1; // Lifting [default=1] long L = 16; // Number of levels in the modulus chain [default=heuristic] long c = 3; // Number of columns in key-switching matrix [default=2] long w = 64; // Hamming weight of secret key long d = 0; // Degree of the field extension [default=1] long k = 128; // Security parameter [default=80] long s = 0; // Minimum number of slots [default=0] long m = FindM(k,L,c,p, d, s, 0); //Specific modulus
然后,創建context上下文對象:
FHEcontext context(m, p, r);
buildModChain(context, L, c);
補充:FHEcontext類:初始化所必須的類,程序根據初始化的參數生成FHEcontext對象,進而通過FHEcontext對象生成同態加密的公私鑰。
然后,創建一個多項式用來加密:
ZZX G = context.alMod.getFactorsOverZZ()[0];
補充:ZZX類:生成EncryptedArray所必須的。
然后,使用context對象生成公私鑰:
FHESecKey secretKey(context); const FHEPubKey& publicKey = secretKey; secretKey.GenSecKey(w);
補充:
FHESecKey類:同態加密私鑰類
FHEPubKey類:同態加密公鑰類
然后,使用公鑰加密,同時使用Ctxt類來保存密文:
Ctxt ctx1(publicKey); Ctxt ctx2(publicKey); publicKey.Encrypt(ctx1, to_ZZX(2)); //對“2”進行加密 publicKey.Encrypt(ctx2, to_ZZX(3)); //對“3”進行加密
補充:Ctxt是同態加密的密文類。
然后,進行密文運算,這里將2和3的密文相加:
Ctxt ctSum = ctx1;
ctSum += ctx2;
然后,對結果密文進行解密,要創造一個多項式來保存解密結果:
ZZX ptSum;
secretKey.Decrypt(ptSum, ctSum);
補充:EncryptedArray類:貫穿整個同態加密的代碼,用於將vector<long>編碼成同態加密的明文對象,對於數據進行同態加密和解密,用於將明文對象解碼成vector<long>,說白了就是在用戶的輸入內容(即需要被加密的信息)和HElib庫中可以進行加密解密操作的對象(Ctxt和ZZX)建立了橋梁。
以下是完整的代碼,編寫文件SimpleFHESum.cpp:
#include "FHE.h" #include <iostream> using namespace std; int main(int argc, char **argv) { long p = 1021; long r = 1; long L = 4; long c = 2; long k = 80; long s = 0; long d = 0; long w = 64; cout << "finding m..." << flush; long m = FindM(k,L,c,p,d,s,0); cout << "m = "<< m << endl; cout << "Initializing context..." << flush; FHEcontext context(m,p,r); //initialize context buildModChain(context, L, c); //modify the context cout << "OK!" << endl; cout << "Creating polynomial..." << flush; ZZX G = context.alMod.getFactorsOverZZ()[0]; //creates the polynomial used to encrypted the data cout << "OK!" << endl; cout << "Generating keys..." << flush; FHESecKey secretKey(context); //construct a secret key structure const FHEPubKey& publicKey = secretKey; //An "upcast": FHESecKey is a subclass of FHEPubKey secretKey.GenSecKey(w); //actually generate a secret key with Hamming weight w cout << "OK!" << endl; Ctxt ctxt1(publicKey); Ctxt ctxt2(publicKey); publicKey.Encrypt(ctxt1, to_ZZX(2)); //encrypt the value 2 publicKey.Encrypt(ctxt2, to_ZZX(3)); //encrypt the value 3 Ctxt ctSum = ctxt1; //create a ciphertext to hold the sum and initialize it with Enc(2) ctSum += ctxt2; ZZX ptSum; //create a ciphertext to hold the plaintext of the sum secretKey.Decrypt(ptSum, ctSum); cout << "2 + 3 = " << ptSum[0] << endl; return 0;
至於如何編譯運行這段代碼,可以參考我的上一篇博客,點擊這里
要想深入理解HElib庫,必須認真學習同態加密的知識,同態加密是密碼學領域里的一顆璀璨的明珠,想要深入學習的同學可以參考這里。
之后的一些筆記我也會記錄一些我學習同態加密的筆記,希望能與大家分享,多多交流~~~~~~
參考資料:
https://blog.csdn.net/qi_1221/article/details/80065714
https://blog.csdn.net/LOVETEDA/article/details/84952037
https://www.8btc.com/article/547923