HElib测试—多组数计算

#include 
#include 
#include "FHE.h"
#include "timing.h"
#include "EncryptedArray.h"
#include 
#include 
#include 
#include 
#include "ArgMap.h"
#include "fhe_stats.h" 
#include 

using namespace helib;
using namespace NTL;
using namespace std;

int main(){
	long R=1;//number of rounds  [ default=1 ]
	long p=29;//plaintext base,p决定了进行同态加密的数值可以有多大
	long r=1;//lifting  [ default=1 ]
	long d=1;//degree of the field extension  [ default=1 ]
                 // d == 0 => factors[0] defines extension
	long c=2; //number of columns in the key-switching matrices  [ default=2 ]
	long k=80;  //security parameter  [ default=80 ]
	long w=64;// Hamming weight of secret key
  	long L=6;//# of bits in the modulus chain  [ default=heuristic ]
	long m=7781;//use specified value as modulus
	vector<long> gens;
	vector<long> ords;
	//以上都是进行同态加密初始化的参数
	vector<long> gens1, ords1;
	convert(gens1, gens);
	convert(ords1, ords);

	std::cout << "Initialising context object..." << std::endl;
  	FHEcontext context(m, p, r, gens1, ords1);// Intialise context
	std::cout  << "Building modulus chain..." << std::endl;
  	buildModChain(context, L, c);// Modify the context, adding primes to the modulus chain
 
// Print the context
  context.zMStar.printout();
  std::cout << std::endl;
  
// Print the security level?????
  std::cout << "Security: " << context.securityLevel() << std::endl;
  
	//进行HElib的初始化
  	ZZX G;//生成用来加密的多项式
  	if (d == 0)
    	G = context.alMod.getFactorsOverZZ()[0];
  	else
    	G = makeIrredPoly(p, d);
 
  	//生成同态加密的公私钥,进行同态加密运算的密文对象必须是经过同一公钥加密的数据,否则会报错
	// Secret key management
	std::cout << "Creating secret key..." << std::endl;
	// Create a secret key associated with the context
        FHESecKey secretKey(context);
  	secretKey.GenSecKey(w); // A Hamming-weight-w secret key???// Generate the secret key
 	std::cout << "Generating key-switching matrices..." << std::endl;
  	addSome1DMatrices(secretKey); // compute key-switching matrices that we need密钥交换矩阵
 
	// Public key management
	// Set the secret key (upcast: FHESecKey is a subclass of FHEPubKey)
	const FHEPubKey& publicKey = secretKey;

	// Get the EncryptedArray of the context
  	EncryptedArray ea(context, G);
	// Get the number of slot (phi(m))明文槽的数量跟什么有关??
  	long nslots = ea.size();
	std::cout << "Number of slots: " << nslots << std::endl;

		
	vector<long> data;//建立data1容器
	data.resize(nslots);//相当于数组长度,不能超过明文槽的个数

//第一组
	PlaintextArray ptxt1(ea);
	data[0]=1;
	data[1]=1;
	data[2]=3;
	encode(ea,ptxt1,data);
	cout << "初始数组1: " << vecToStr(data) << endl;
	Ctxt ctxt1(publicKey);// Create a ciphertext
	cout<<"加密第1组数"<<endl;
	ea.encrypt(ctxt1, publicKey,ptxt1);//Encrypt the plaintext using the publickey
	
//第二组
	vector<long> data2;
	data2.resize(nslots);
	PlaintextArray ptxt2(ea);
	data2[0]=1;
	data2[1]=1;
	//data2[2]=0;
	encode(ea,ptxt2,data2);
	cout << "初始数组2: " << vecToStr(data2) << endl;
	Ctxt ctxt2(publicKey);
	cout<<"加密第1组数"<<endl;
	ea.encrypt(ctxt2,publicKey,ptxt2);

//第三组
	vector<long> data3;
	data3.resize(nslots);
	PlaintextArray ptxt3(ea);
	data3[0]=1;
	data3[1]=2;
	//data3[2]=0;
	encode(ea,ptxt3,data3);
	cout << "初始数组3: " << vecToStr(data3) << endl;
	Ctxt ctxt3(publicKey);
	cout<<"加密第3组数"<<endl;
	ea.encrypt(ctxt3,publicKey,ptxt3);

//计算
	cout<<"密文相加"<<endl;
	ctxt1+=ctxt2;
	ctxt1+=ctxt3;
	cout<<"结束"<<endl;

//解密
	PlaintextArray de(ea);
	cout<<"解密:"<<endl;
	ea.decrypt(ctxt1, secretKey, de);
	vector<long> array2;
	array2.resize(ea.size());
	decode(ea,array2,de);//把解密的结果还原成向量
	cout<<"解密结果:"<<array2<<endl;
 
}

你可能感兴趣的:(HElib测试—多组数计算)