{"id":365,"date":"2018-04-23T16:50:33","date_gmt":"2018-04-23T07:50:33","guid":{"rendered":"http:\/\/tamatoyaku.com\/b\/?p=365"},"modified":"2018-04-23T16:50:33","modified_gmt":"2018-04-23T07:50:33","slug":"365","status":"publish","type":"post","link":"https:\/\/p-0.me\/b\/p\/365\/","title":{"rendered":"PARI GP\u3067Montgomery curves\u3092\u7528\u3044\u305fECDSA\u306e\u5b9f\u88c5(1)"},"content":{"rendered":"<p>[mathjax]Weierstrass\u6a19\u6e96\u5f62\u3092\u4f75\u7528\u3057\u306a\u304c\u3089\u30b9\u30ab\u30e9\u30fc\u500d\uff0c\u70b9\u306e\u52a0\u7b97\u306bMontgomery curves\u3092\u4f7f\u3063\u305f\u8a71\uff0e<br \/>\n<!--more--><br \/>\n<a href=\"https:\/\/tamatoyaku.com\/b\/p\/355\">\u4ee5\u524d<\/a>\u306fPARI GP\u306e\u6a5f\u80fd\u3092\u4f7f\u3063\u3066\uff0cWeierstrass\u6a19\u6e96\u5f62\u3067ECDSA\u3092\u5b9f\u88c5\u3057\u305f\uff0e\u4eca\u56de\u306f\u65b0\u3057\u304fMontgomery curves\u3092\u7528\u3044\u305f\u30b9\u30ab\u30e9\u30fc\u500d\uff0c\u70b9\u306e\u52a0\u7b97\u3092\u5b9f\u88c5\u3057\u305f\uff0e\u305d\u306e\u305f\u3081\uff0c\u4ee5\u524d\u884c\u3063\u305fWeierstrass\u6a19\u6e96\u5f62\u3067\u306e\u5b9f\u88c5\u3068\uff0c\u65b0\u3057\u304f\u3064\u304f\u308bMontgomery curves\u3067\u306e\u5b9f\u88c5\u3092\u898b\u6bd4\u3079\u3066\u307f\u308b\uff0e<br \/>\n&nbsp;<br \/>\n<strong>1.\u30b9\u30ab\u30e9\u30fc\u500d<\/strong><br \/>\n\u4f5c\u6210\u3057\u305fMontgomery ladder\u306e\u30e9\u30c3\u30d1\u30fc\u95a2\u6570\u3068\u3057\u3066scalar\u3068\u3044\u3046\u95a2\u6570\u3092\u4f5c\u6210\u3057\u305f\uff0e\u5165\u529b\u306f\u6955\u5186\u66f2\u7dda\u306e\u30d1\u30e9\u30e1\u30fc\u30bf\\(A,B\\)\u3068\u30a2\u30d5\u30a3\u30f3\u5ea7\u6a19\u306e\u70b9\\(P\\)\uff0c\u30b9\u30ab\u30e9\u30fc\\(k\\)\uff0c\u6709\u9650\u4f53\u306e\u4f4d\u6570\\(p\\)\u3067\u3042\u308b\uff0e\u51fa\u529b\u306f\u30a2\u30d5\u30a3\u30f3\u5ea7\u6a19\u306e\u70b9\\(Q=kP\\)\u3067\u3042\u308b\uff0e<\/p>\n<pre class=\"lang:default decode:true \">scalar(A,B,P,k,p)={\n\tlocal(P_M,pair,kP_M,k1P_M,x_k,x_k1,y_k);\n\tP_M=[P[1],1];\n\tpair=ladder(A,P_M,k,p);\n\tkP_M=pair[2];k1P_M=pair[1];\n\tif(kP_M!=[0]&amp;&amp;kP_M[2]==0,kP_M=[0]);\n\tif(k1P_M!=[0]&amp;&amp;k1P_M[2]==0,k1P_M=[0]);\n\tif(kP_M==[0],\n\t\treturn([0]);\n\t);\n\tif(kP_M!=[0],\n\t\tx_k=Mod(kP_M[1]\/kP_M[2],p);\n\t\tif(k1P_M==[0]||k1P_M[2]==0,\n\t\t\tx_k1=-1;,\n\t\t\tx_k1=Mod(k1P_M[1]\/k1P_M[2],p);\n\t\t);\n\t\ty_k=recover(A,B,P_M[1],P[2],x_k,x_k1,p);\n\t);\n\treturn([x_k,y_k]);\n}<\/pre>\n<p>&nbsp;<br \/>\n<strong>2.\u70b9\u306e\u52a0\u7b97<\/strong><br \/>\n\u7f72\u540d\u691c\u8a3c\u3067\u70b9\u306e\u52a0\u7b97\u304c\u5fc5\u8981\u3068\u306a\u308b\u305f\u3081\uff0c\u70b9\u306e\u52a0\u7b97\u3092\u5b9f\u88c5\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\uff0e<a href=\"https:\/\/en.wikipedia.org\/wiki\/Montgomery_curve#Addition\">\u30a2\u30eb\u30b4\u30ea\u30ba\u30e0<\/a>\u3092\u305d\u306e\u307e\u307e\u30b3\u30fc\u30c9\u306b\u3057\u3066\u5b9f\u88c5\u3059\u308b\uff0e\u305f\u3060\u3057\uff0c\\(P_3=P_1+P_2\\)\u3068\u3059\u308b\u969b\u306b\\(P_1=-P_2\\)\u3068\u306a\u3063\u3066\u3044\u308b\u5834\u5408\u306f\\(P-P=O\\)\u3068\u306a\u308b\u305f\u3081\u7121\u9650\u9060\u70b9\u3092\u8fd4\u3059\uff0e\u307e\u305f\uff0c\\(P_1=P_2\\)\u306e\u5834\u5408\u306f\\(P+P=2P\\)\u3068\u306a\u308b\u305f\u3081<a href=\"https:\/\/en.wikipedia.org\/wiki\/Montgomery_curve#Doubling\">2\u500d\u7b97<\/a>\u3092\u884c\u3046\uff0e<\/p>\n<pre class=\"lang:default decode:true\">mdbl(A,B,P,p)={\n\tlocal(x,y);\n\tx=Mod(((P[1]^2-1)^2)\/(4*B*(P[2]^2)),p);\n\ty=Mod(((2*P[1]+P[1]+A)*(3*(P[1]^2)+2*A*P[1]+1))\/(2*B*P[2])-(B*(3*(((P[1]^2)+2*A*P[1]+1)^3)))\/((2*B*P[2])^3)-P[2],p);\n\treturn([x,y]);\n}\nmadd(A,B,P1,P2,p)={\n\tlocal(x,y);\n\tif(P1==[0],return(P2));\n\tif(P2==[0],return(P1));\n\tif(P1[1]==P2[1],\n\t\tif(P1[2]==-P2[2],\n\t\t\treturn([0]);,\n\t\t\treturn(mdbl(A,B,P1,p));\n\t\t);\n\t);\n\tx=Mod((B*((P2[2]-P1[2])^2))\/((P2[1]-P1[1])^2)-A-P1[1]-P2[1],p);\n\ty=Mod(((2*P1[1]+P2[1]+A)*(P2[2]-P1[2]))\/(P2[1]-P1[1])-(B*((P2[2]-P1[2])^3))\/((P2[1]-P1[1])^3)-P1[2],p);\n\treturn([x,y]);\n}<\/pre>\n<p>&nbsp;<br \/>\n<strong>3.\u52d5\u4f5c\u78ba\u8a8d<\/strong><br \/>\n\u52d5\u4f5c\u78ba\u8a8d\u306b\u306f\\(F_{30011}\\)\u4e0a\u306eMontgomery curves \\(y^2=x^3+84x^2+x\\)\u3068Weierstrass\u6a19\u6e96\u5f62 \\(y^2=x^3-2351x+43876\\)\u3092\u7528\u3044\u308b\uff0e\u30d9\u30fc\u30b9\u30dd\u30a4\u30f3\u30c8\u306b\u3064\u3044\u3066\u306fWeierstrass\u6a19\u6e96\u5f62\u3067\u306f\\((7796,9454)\\)\uff0cMongtomery curves\u3067\u306f\\((7768,9454)\\)\u3092\u7528\u3044\u308b\uff0e\u306a\u304a\uff0c\u70b9\u4f4d\u6570\\(l=1249\\)\u306f\u3068\u306a\u308b\uff0e\u57fa\u672c\u7684\u306b\u306f\u4ee5\u524d\u4f5c\u6210\u3057\u305fWeierstrass\u6a19\u6e96\u5f62\u306e\u30b3\u30fc\u30c9\u3068\u540c\u6642\u306bMontgomery curves\u306e\u30b3\u30fc\u30c9\u3092\u5b9f\u884c\u3059\u308b\uff0e\u4ee5\u4e0b\u306b\u30b3\u30fc\u30c9\u3068\u5b9f\u884c\u4f8b\u3092\u793a\u3059\uff0e<br \/>\n\u5b9f\u884c\u4f8b\u304b\u3089Weierstrass\u6a19\u6e96\u5f62\u306e\u70b9\\(P_W=(x_W,y_W)\\)\u3068Montgomery curves\u306e\u70b9\\(P_M=(x_M,y_M)\\)\u306b\u3064\u3044\u3066\\((x_M,y_M)=(x_W-28,y_W)\\)\u304c\u6210\u308a\u7acb\u3063\u3066\u304a\u308a\uff0c\u30b9\u30ab\u30e9\u30fc\u500d\u3084\u70b9\u306e\u52a0\u7b97\u304c\u6b63\u3057\u304f\u884c\u3048\u3066\u3044\u308b\u3053\u3068\u304c\u5206\u304b\u308b\uff0e<br \/>\n&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">\\\\m-n==1\nadd(P1,Pm,Pn,p)={\n\tlocal(a,b,c,d,Xmn,Zmn);\n\tif(Pm==0||Pm[2]==0,return(Pn));\n\tif(Pn==0||Pn[2]==0,return(Pm));\n\ta=Pm[1]+Pm[2];\n\tb=Pm[1]-Pm[2];\n\tc=Pn[1]+Pn[2];\n\td=Pn[1]-Pn[2];\n\tXmn=Mod(P1[2]*((b*c+a*d)^2),p);\n\tZmn=Mod(P1[1]*((b*c-a*d)^2),p);\n\treturn([Xmn,Zmn]);\n}\ndbl(A,P,p)={\n\tlocal(tmp1,tmp2,tmp3,X2,Z2);\n\tif(P==0||P[2]==0,return([0]));\n\ttmp1=(P[1]+P[2])^2;\n\ttmp2=(P[1]-P[2])^2;\n\ttmp3=tmp1-tmp2;\n\tX2=Mod(tmp1*tmp2,p);\n\tZ2=Mod(tmp3*(tmp2+((tmp3*(A+2))\/4)),p);\n\treturn([X2,Z2]);\n}\nrecover(A,B,x1,y1,xn,xn1,p)={\n\tlocal(up,down,res);\n\tif(xn1==-1,\n\t\treturn(-y1);\n\t);\n\tup=(x1*xn+1)*(x1+xn+2*A)-2*A-((x1-xn)^2)*xn1;\n\tdown=2*B*y1;\n\tif(down==0,\n\t\tres=0;,\n\t\tres=Mod(up\/down,p);\n\t);\n\treturn(res);\n}\nladder(A,P,k,p)={\n\tlocal(R0,R1,i,bin);\n\tbin=binary(k);\n\tR0=[0];\n\tR1=P;\n\tfor(i=1,length(bin),\n\t\tif(bin[i]==0,\n\t\t\tR1=add(P,R1,R0,p);\n\t\t\tR0=dbl(A,R0,p);,\n\t\t\tR0=add(P,R1,R0,p);\n\t\t\tR1=dbl(A,R1,p);\n\t\t);\n\t);\n\treturn([R1,R0]);\n}\n\\\\input and output is affine point P=(x,y)\nscalar(A,B,P,k,p)={\n\tlocal(P_M,pair,kP_M,k1P_M,x_k,x_k1,y_k);\n\tP_M=[P[1],1];\n\tpair=ladder(A,P_M,k,p);\n\tkP_M=pair[2];k1P_M=pair[1];\n\tif(kP_M!=[0]&amp;&amp;kP_M[2]==0,kP_M=[0]);\n\tif(k1P_M!=[0]&amp;&amp;k1P_M[2]==0,k1P_M=[0]);\n\tif(kP_M==[0],\n\t\treturn([0]);\n\t);\n\tif(kP_M!=[0],\n\t\tx_k=Mod(kP_M[1]\/kP_M[2],p);\n\t\tif(k1P_M==[0]||k1P_M[2]==0,\n\t\t\tx_k1=-1;,\n\t\t\tx_k1=Mod(k1P_M[1]\/k1P_M[2],p);\n\t\t);\n\t\ty_k=recover(A,B,P_M[1],P[2],x_k,x_k1,p);\n\t);\n\treturn([x_k,y_k]);\n}\nmdbl(A,B,P,p)={\n\tlocal(x,y);\n\tx=Mod(((P[1]^2-1)^2)\/(4*B*(P[2]^2)),p);\n\ty=Mod(((2*P[1]+P[1]+A)*(3*(P[1]^2)+2*A*P[1]+1))\/(2*B*P[2])-(B*(3*(((P[1]^2)+2*A*P[1]+1)^3)))\/((2*B*P[2])^3)-P[2],p);\n\treturn([x,y]);\n}\nmadd(A,B,P1,P2,p)={\n\tlocal(x,y);\n\tif(P1==[0],return(P2));\n\tif(P2==[0],return(P1));\n\tif(P1[1]==P2[1],\n\t\tif(P1[2]==-P2[2],\n\t\t\treturn([0]);,\n\t\t\treturn(mdbl(A,B,P1,p));\n\t\t);\n\t);\n\tx=Mod((B*((P2[2]-P1[2])^2))\/((P2[1]-P1[1])^2)-A-P1[1]-P2[1],p);\n\ty=Mod(((2*P1[1]+P2[1]+A)*(P2[2]-P1[2]))\/(P2[1]-P1[1])-(B*((P2[2]-P1[2])^3))\/((P2[1]-P1[1])^3)-P1[2],p);\n\treturn([x,y]);\n}\n{\n\t\\\\\u6e96\u5099\n\ta=-2351;b=43876;A=84;B=1;p=30011;\n\te=ellinit([0,0,0,a,b],p);\n\tP_W=[Mod(7796,p),Mod(9454,p)];\n\tP=[P_W[1]-28,P_W[2]];\n\tl=ellorder(e,P_W);\n\tprint(\"pripare\");\n\tprint(\"\\ty^2=x^3+\",a,\"x+\",b,\" on F_\",p);\n\tprint(\"\\tbase point P_W=\",P_W);\n\tprint(\"\\tbase point P  =\",P);\n\tprint(\"\\torder l=\",l,\"\\n\");\n\t\\\\\u9375\u751f\u6210\n\td_B=random(l-2)+2;\n\tP_WB=ellmul(e,P_W,d_B);\n\tP_B=scalar(A,B,P,d_B,p);\n\tprint(\"key generation\");\n\tprint(\"\\tsecret key d_B =\",d_B);\n\tprint(\"\\tpublic key P_B =\",P_B);\n\tprint(\"\\tpublic key P_WB=\",P_WB,\"\\n\");\n\t\\\\\u7f72\u540d\u751f\u6210\n\tuntil(u!=0&amp;&amp;v!=0,\n\t\tr=random(l-1)+1;\n\t\tU_W=ellmul(e,P_W,r);\n\t\tU=scalar(A,B,P,r,p);\n\t\tm=random(p);\n\t\tu=lift(lift(U[1])%l);\n\t\tv=((m+u*d_B)\/r)%l;\n\t);\n\tprint(\"signature generation\");\n\tprint(\"\\trandom value r=\",r);\n\tprint(\"\\tU  =\",U);\n\tprint(\"\\tU_W=\",U_W);\n\tprint(\"\\tm=\",m);\n\tprint(\"\\tsignature (u,v)=(\",u,\",\",v,\")\",\"\\n\");\n\t\\\\\u7f72\u540d\u691c\u8a3c\n\td=(1\/v)%l;\n\tV_W=elladd(e,ellmul(e,P_W,d*m),ellmul(e,P_WB,d*u));\n\tV=madd(A,B,scalar(A,B,P,d*m,p),scalar(A,B,P_B,d*u,p),p);\n\tprint(\"signature verification\");\n\tprint(\"\\td=\",d);\n\tprint(\"\\tV  =\",V);\n\tprint(\"\\tV_W=\",V_W,\"\\n\");\n\t\\\\\u7d50\u679c\u8868\u793a\n\tprint(\"result\");\n\tprint(\"\\t(u,Vx)=(\",u,\",\",lift(V[1])%l,\")\");\n\tif(u==(lift(V[1])%l),\n\t\tprint(\"\\tOK\");,\n\t\tprint(\"\\tNG\");\n\t);\n}\n<\/pre>\n<p>&nbsp;<\/p>\n<pre class=\"lang:default decode:true \">(17:34) gp &gt; \\r m_ecdsa.gp\npripare\n        y^2=x^3+-2351x+43876 on F_30011\n        base point P_W=[Mod(7796, 30011), Mod(9454, 30011)]\n        base point P  =[Mod(7768, 30011), Mod(9454, 30011)]\n        order l=1249\nkey generation\n        secret key d_B =1094\n        public key P_B =[Mod(12663, 30011), Mod(3408, 30011)]\n        public key P_WB=[Mod(12691, 30011), Mod(3408, 30011)]\nsignature generation\n        random value r=1112\n        U  =[Mod(12043, 30011), Mod(16108, 30011)]\n        U_W=[Mod(12071, 30011), Mod(16108, 30011)]\n        m=18826\n        signature (u,v)=(802,305)\nsignature verification\n        d=86\n        V  =[Mod(12043, 30011), Mod(16108, 30011)]\n        V_W=[Mod(12071, 30011), Mod(16108, 30011)]\nresult\n        (u,Vx)=(802,802)\n        OK<\/pre>\n<p>&nbsp;<br \/>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>[mathjax]Weierstrass\u6a19\u6e96\u5f62\u3092\u4f75\u7528\u3057\u306a\u304c\u3089\u30b9\u30ab\u30e9\u30fc\u500d\uff0c\u70b9\u306e\u52a0\u7b97\u306bMontgomery curves\u3092\u4f7f\u3063\u305f\u8a71\uff0e<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[],"class_list":["post-365","post","type-post","status-publish","format-standard","hentry","category-4"],"_links":{"self":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/posts\/365","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/comments?post=365"}],"version-history":[{"count":0,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/posts\/365\/revisions"}],"wp:attachment":[{"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/media?parent=365"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/categories?post=365"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/p-0.me\/b\/wp-json\/wp\/v2\/tags?post=365"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}