毕业设计日志——HElib同态加密库学习笔记(四)


安装完成了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


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM