同态加密算法实现


基于RWLE假设的同态加密算法的实现,简单易懂,自己写的。
资源截图
代码片段和文件信息
#include “FHE.h“


void uniform_rand(int maxint sk[]int n) //uniform random in [-maxmax]
{
srand((int)time(0));
for(int i=0; i sk[i] = rand()%(2*max+1)-max;
}

void uniform_rand2(ZZ maxZZ a[]int n) //uniform random in (-max/2max/2]Rq
{
ZZ tmp;
SetSeed(to_ZZ(int(time(0))));
for(int i=0; i {
RandomLen(tmp170);
a[i] = (tmp%max + 1);  //(0max]
if(abs(a[i]) > abs(a[i]-max))
a[i] = a[i] - max; //(-max/2max/2]
}
}

void uniform_rand3(int maxint e[]int n) //uniform random in [0max)Gaussian
{
srand((int)time(0));
for(int i=0; i e[i] = rand()%max;
}

void error(ZZ_pE &exint max)
{
int e[N];
uniform_rand3(maxeN);
ZZ_pX tmpx;
tmpx.rep.SetLength(N);
for(int i=0; i tmpx.rep[i] = to_ZZ_p(e[i]);
ex._ZZ_pE__rep = tmpx;
}

void error(ZZ_pE &uxZZ_pE &fxZZ_pE &gxint max)    //3*N
{
int e[3*N];
uniform_rand3(maxe3*N);
ZZ_pX tmpxtmpx2tmpx3;
tmpx.rep.SetLength(N);
tmpx2.rep.SetLength(N);
tmpx3.rep.SetLength(N);
for(int i=0; i {
tmpx.rep[i] = to_ZZ_p(e[i]);
tmpx2.rep[i] = to_ZZ_p(e[i+N]);
tmpx3.rep[i] = to_ZZ_p(e[i+2*N]);

}
ux._ZZ_pE__rep = tmpx;
fx._ZZ_pE__rep = tmpx2;
gx._ZZ_pE__rep = tmpx3;
}

void ufg(ZZ_pE &uxZZ_pE &fxZZ_pE &gxint max)
{
//error(uxmax); //ux
//sleep(1);
//error(fxmax); //fx
//sleep(1);
//error(gxmax); //gx
error(uxfxgxmax);
}

void readVni(mat_ZZ_p& Vni)
{
ifstream fin_Vni(“Vni.txt“);
fin_Vni>>Vni;
fin_Vni.close();
}

void readV(mat_ZZ_p& V)
{
ifstream fin_V(“V.txt“);
fin_V>>V;
fin_V.close();
}

void mapToPlain(const mat_ZZ_p &Vniconst vec_ZZ_p &vec_mx_crt_pZZ_pE &m) //crt to plain
{
//mat_ZZ_p Vni; //V^{-1}
//Vni.SetDims(NN);
//ifstream fin_Vni(“Vni.txt“);
//fin_Vni>>Vni;
//fin_Vni.close();

vec_ZZ_p vec_mx_p;
vec_mx_p.SetLength(N);
vec_mx_p = Vni*vec_mx_crt_p;
//mul_mat_vec(vec_mx_pVnivec_mx_crt_p);

m._ZZ_pE__rep.rep = vec_mx_p;
}

void mapToSlots(const mat_ZZ_p &Vconst ZZX &mxvec_ZZ_p &vec_result)
{
vec_ZZ_p mx2;
mx2.SetLength(N);
for(int i=0; i mx2[i] = to_ZZ_p(mx.rep[i]);
vec_result = V*mx2;
//mul_mat_vec(vec_resultVmx2);
}

void fileToPoly(char* fileint *data)
{
FILE *fp;
if((fp=fopen(file“r“)) == NULL)
{
cout<<“cannot open the file :“< exit(0);
}
int i = 0;
while(!feof(fp))
{
for(int j = 0; j < DIM; j++)
fscanf(fp“%d “data+i*DIM+j);
i++;
}
}

bool fileExist(char* file)
{
fstream _file;
    _file.open(fileios::in);
    if(!_file)
    {
        return false;
    }
    else
    {
        return true;
    }
}



void FHE::secretKeyGen()
{
int max = 2;    //01
int sk[N];
uniform_rand3(maxskN);
ZZ_pX tmpx;
tmpx.rep.SetLength(N);
for(int i=0; i tmpx.rep[i] =  to_ZZ_p(sk[i]);
skx._ZZ_pE__rep = tmpx;

ofstream fout_sk(“.\key\skx.txt“);

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----

     文件     329450  2014-06-09 23:31  HOMHEciphertextsadd0.txt

     文件     329614  2014-06-09 23:31  HOMHEciphertextsadd1.txt

     文件     329409  2014-06-09 23:31  HOMHEciphertextsc0x.txt

     文件     329414  2014-06-09 23:31  HOMHEciphertextsc0x2.txt

     文件     331001  2014-06-09 23:31  HOMHEciphertextsc1x.txt

     文件     327778  2014-06-09 23:31  HOMHEciphertextsc1x2.txt

     文件     329500  2014-06-09 23:31  HOMHEciphertextsmult0.txt

     文件     329531  2014-06-09 23:31  HOMHEciphertextsmult1.txt

     文件     329481  2014-06-09 23:31  HOMHEciphertextsmult2.txt

     文件     329435  2014-06-09 23:29  HOMHEciphertextssub0.txt

     文件     328912  2014-06-09 23:29  HOMHEciphertextssub1.txt

     文件    2055808  2014-06-09 21:55  HOMHEdata100k4d.txt

     文件       8411  2014-06-09 23:30  HOMHEFHE.cpp

     文件       3564  2014-06-09 23:08  HOMHEFHE.h

     文件      60165  2014-06-09 23:31  HOMHEFHE.o

     文件       1149  2014-06-09 21:50  HOMHEhe.dev

     文件    1174723  2014-06-09 23:36  HOMHEhe.exe

     文件     327212  2014-06-09 21:58  HOMHEkeypk0.txt

     文件     312681  2014-06-09 21:58  HOMHEkeypk1.txt

     文件      11877  2014-06-09 21:58  HOMHEkeyskx.txt

     文件       1048  2014-06-09 23:36  HOMHEMakefile.win

     文件       4787  2014-06-09 23:31  HOMHE est.cpp

     文件      20979  2014-06-09 23:31  HOMHE est.o

     文件        967  2009-08-14 20:53  HOM
tl-5.5.2docarrow1.gif

     文件        942  2009-08-14 20:53  HOM
tl-5.5.2docarrow2.gif

     文件        955  2009-08-14 20:53  HOM
tl-5.5.2docarrow3.gif

     文件      18494  2009-08-14 20:53  HOM
tl-5.5.2docconfig.txt

     文件       3603  2009-08-14 20:53  HOM
tl-5.5.2docconversions.txt

     文件      18180  2009-08-14 20:53  HOM
tl-5.5.2doccopying.txt

     文件       1379  2009-08-14 20:53  HOM
tl-5.5.2docflags.txt

............此处省略762个文件信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件举报,一经查实,本站将立刻删除。

发表评论

评论列表(条)