- 注册时间
- 2008-2-23
- 最后登录
- 1970-1-1
- 威望
- 星
- 金币
- 枚
- 贡献
- 分
- 经验
- 点
- 鲜花
- 朵
- 魅力
- 点
- 上传
- 次
- 下载
- 次
- 积分
- 507
- 在线时间
- 小时
|
发表于 2008-5-1 20:14:48
|
显示全部楼层
这个也是我用来找NTT参数的,不过它同时找2组参数,并且增加对在NTT里使用CRT(中国余数定理)的支持
mulinv[a_,b_]:=Module[{p,f},p=
ExtendedGCD[a,b];p=p[[2]];f=Mod[p[[1]]*a,a*b];Return[f]]
findntt[e_,w_,fn_]:=Module[{n=2^e,i,j,z,
m,m1,m2,a1,a2,b1,b2,invm1,invm2,k1,k2,tmp,t1,t2},t1=n/2;
t2=n-1;
z=w*Sqrt[n];
z=Floor[z];
m=z;
z=Mod[z,n];
m-=z;
m+=1;
While[PrimeQ[m]≠True,m+=n];
m1=m;
invm1=(m*n-m+1)/n;
a1=findroot[m1,n];
b1=PowerMod[a1,t2,m];
m+=n;
While[PrimeQ[m]≠True,m+=n];
m2=m;
invm2=(m*n-m+1)/n;
a2=findroot[m2,n];
b2=PowerMod[a2,t2,m];
m=m1*m2;
k1=mulinv[m1,m2];
k2=mulinv[m2,m1];
PutAppend["___________________________________",fn];
PutAppend[{" N = ",n},fn];
PutAppend["___________________________________",fn];
PutAppend[{"M1=",m1," a1=",a1," 1/a1=",b1," invm1=",invm1},fn];
PutAppend[{"M2=",m2," a2=",a2," 1/a2=",b2," invm2=",invm2},fn];
PutAppend[{"M1*M2=",m," k1=",k1," k2=",k2},fn];
PutAppend[{"M1M2/NWW=",N[m/(n*w*w),4]},fn];
PutAppend["___________________________________",fn];]
findroot[p_,n_]:=Module[{i,j,l,q,b,v,f,d},
j=2;
i=p-1;
i=i/n;
q=0;
l=PowerMod[j,i,p];
While[( l<2) || (ispow[l]\[Equal]2),j++;q++;l=PowerMod[j,i,p];If[q>
200000,Break[]]];
If[q<200000,Return[l]];
q=0;
l=2;
b=n/2;
v=p-1;
f=PowerMod[tt,n,p];
d=PowerMod[tt,b,p];
While[(!(f== 1 )) || (!(d == v)) ,l++;q++;f=PowerMod[l,n,p];d=PowerMod[l,
b,p];If[q>1024,Break[]];];
If[(f\[Equal]1)&&(d\[Equal]v),Return[l]];
q=0;
l=PowerMod[j,i,p];
While[( l<2) || (ispow[l]\[Equal]2),j++;
q++;l=PowerMod[j,i,p];If[q>200000,Break[]]];
If[q<200000,Return[l]];
l=PowerMod[j,i,p];
While[( l<2) ,j++;q++;l=PowerMod[j,i,p];];
Return[l];
]
ispow[n_]:=Module[{xx=
n},If[xx\[Equal]1,Return[2]];While[Mod[xx,2]\[Equal]0,xx/=
2];If[xx\[Equal]1,Return[1],Return[2];];]
startfindntt[wordSize_,filename_]:=Module[{},
Put["Search For NTT Parameter",filename];
PutAppend[{"It's used for ",wordSize," word width,"},filename];
Do[findntt[i,wordSize,filename],{i,1,32}];]
startfindntt[10,"ntt_1.txt"];
startfindntt[100,"ntt_2.txt"];
startfindntt[1000,"ntt_3.txt"];
startfindntt[10000,"ntt_4.txt"];
startfindntt[100000,"ntt_5.txt"];
startfindntt[1000000,"ntt_6.txt"];
startfindntt[10000000,"ntt_7.txt"];
startfindntt[100000000,"ntt_8.txt"];
startfindntt[1000000000,"ntt_9.txt"]; |
|