PrivateKey.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. #include "PrivateKey.hpp"
  2. #include "fpe2scalar.hpp"
  3. //to remove after debug
  4. //PrivateKey private_key;
  5. Fp PrivateKey::get(string name)
  6. {
  7. if (name == "i1") return i1;
  8. if (name == "i2") return i2;
  9. if (name == "j1") return j1;
  10. if (name == "j2") return j2;
  11. if (name == "k1") return k1;
  12. if (name == "k2") return k2;
  13. if (name == "l1") return l1;
  14. if (name == "l2") return l2;
  15. }
  16. PrivateKey::PrivateKey(Fp a, Fp b, Fp c, Fp d, Fp e, Fp f, Fp g, Fp h)
  17. {
  18. i1 = a;
  19. j1 = b;
  20. k1 = c;
  21. l1 = d;
  22. i2 = e;
  23. j2 = f;
  24. k2 = g;
  25. l2 = h;
  26. }
  27. void PrivateKey::set(Fp a, Fp b, Fp c, Fp d, Fp e, Fp f, Fp g, Fp h)
  28. {
  29. i1 = a;
  30. j1 = b;
  31. k1 = c;
  32. l1 = d;
  33. i2 = e;
  34. j2 = f;
  35. k2 = g;
  36. l2 = h;
  37. }
  38. void PrivateKey::print() const
  39. {
  40. zout(*i1.scalar_rep, *j1.scalar_rep, *k1.scalar_rep, *l1.scalar_rep, *i2.scalar_rep, *j2.scalar_rep, *k2.scalar_rep, *l2.scalar_rep);
  41. }
  42. Bipoint<curvepoint_fp_t> PrivateKey::pi_1(Bipoint<curvepoint_fp_t> op)
  43. {
  44. //what;
  45. Bipoint<curvepoint_fp_t> rop;
  46. curvepoint_fp_t rop0, rop1; // rop0=coeff1.op[0] + coeff2.op[1]=(-j1*k1)x +(i1*k1)y rop1=coeff3.op[0] + coeff4.op[1]=(-j1*l1)x + (i1*l1)y
  47. //cout << RED << "affichage du scalar_t i1" << RESET <<endl;
  48. //scalar_print(stdout, i1.scalar());
  49. //JUMP;
  50. //cout << RED << "affichage du scalar_t j1" << RESET <<endl;
  51. //scalar_print(stdout, j1.scalar());
  52. //JUMP;
  53. //cout << RED << "affichage du scalar_t k1" << RESET <<endl;
  54. //scalar_print(stdout, k1.scalar());
  55. //JUMP;
  56. //cout << "à modifier après débug" << endl;
  57. //Fp coeff1=-(j1*k1), coeff2=i1*k1,coeff3=-(j1*l1),coeff4=i1*l1; // en théorie, on peut faire les calculs dans Fp d'abord puis les multiplications scalaires, en pratique, on obtient des représentants différents à cause du modulo lors du produit
  58. //cout << RED << "affichage du scalar_t coeff1.scalar()" << RESET <<endl;
  59. //scalar_print(stdout, coeff1.scalar());
  60. //JUMP;
  61. //cout << RED << "affichage du scalar_t coeff2.scalar()" << RESET <<endl;
  62. //scalar_print(stdout, coeff2.scalar());
  63. //JUMP;
  64. //Fp test1=j1*k1*i1, test2=i1*k1*j1;
  65. //cout << RED << "affichage du scalar_t test1" << RESET <<endl;
  66. //scalar_print(stdout, test1.scalar());
  67. //JUMP;
  68. //cout << RED << "affichage du scalar_t test2" << RESET <<endl;
  69. //scalar_print(stdout, test2.scalar());
  70. //JUMP;
  71. //curvepoint_fp_t test3,test4;
  72. //curvepoint_fp_scalarmult_vartime(test3,bn_curvegen,test1.scalar());
  73. //curvepoint_fp_scalarmult_vartime(test4,bn_curvegen,test2.scalar());
  74. //curvepoint_fp_print(stdout,test3);
  75. //JUMP;
  76. //curvepoint_fp_print(stdout,test4);
  77. //JUMP;
  78. curvepoint_fp_t temp1a,temp2a,temp3a,temp4a,temp1b,temp2b,temp3b,temp4b,temp1c,temp3c;
  79. //scalar_print(stdout, coeff1.scalar()); JUMP;
  80. //scalar_print(stdout, coeff2.scalar()); JUMP;
  81. //curvepoint_fp_print(stdout,op[0]); JUMP;
  82. //curvepoint_fp_print(stdout,op[1]); JUMP;
  83. //cout << "coeff1[0] = " << coeff1.scalar()[0] << endl;
  84. //cout << "coeff1[1] = " << coeff1.scalar()[1] << endl;
  85. //cout << "coeff1[2] = " << coeff1.scalar()[2] << endl;
  86. //cout << "coeff1[3] = " << coeff1.scalar()[3] << endl;
  87. //scalar_t test5;
  88. //test5[0]=1; test5[1]=0; test5[2]=0; test5[3]=0;
  89. //curvepoint_fp_scalarmult_vartime(temp1,op[0],test5);
  90. //exit(0);
  91. //curvepoint_fp_scalarmult_vartime(temp1,op[0],coeff1.scalar());
  92. curvepoint_fp_scalarmult_vartime(temp1a,op[0],j1.scalar());
  93. curvepoint_fp_scalarmult_vartime(temp1b,temp1a,k1.scalar());
  94. curvepoint_fp_neg(temp1c,temp1b);
  95. curvepoint_fp_makeaffine(temp1c);
  96. //cout << RED << "si bit_urandom=0 et op=u1 affichage du curvepoint coeff1*u1[0]=-(j1*k1)u1[0]=-(j1*k1) (lambda1*i1*g)=" << RESET << endl; curvepoint_fp_print(stdout,temp1c); JUMP;
  97. //curvepoint_fp_scalarmult_vartime(temp2,op[1],coeff2.scalar());
  98. curvepoint_fp_scalarmult_vartime(temp2a,op[1],i1.scalar());
  99. curvepoint_fp_scalarmult_vartime(temp2b,temp2a,k1.scalar());
  100. curvepoint_fp_makeaffine(temp2b);
  101. //cout << RED "si bit_urandom=0 et op=u1 affichage du curvepoint coeff2*u1[1]=(i1*k1)u1[1]=(i1*k1) (lambda1*j1*g)=" << RESET << endl; curvepoint_fp_print(stdout,temp2b);JUMP;
  102. curvepoint_fp_add_vartime(rop0,temp1c,temp2b);
  103. curvepoint_fp_makeaffine(rop0);
  104. curvepoint_fp_set(rop[0],rop0);
  105. //cout << RED << "si bit_urandom=0 et op=u1 affichage du curvepoint pi_1(u1)[0]=-(j1*k1)u1[0]+(i1*k1)u1[1]=-(j1*k1) (lambda1*i1*g)+(i1*k1) (lambda1*j1*g)" << RESET << endl; curvepoint_fp_print(stdout,rop[0]); JUMP;
  106. //cout << RED << "affichage du scalar_t j1" << RESET <<endl;
  107. //scalar_print(stdout, j1.scalar());
  108. //JUMP;
  109. //cout << RED << "affichage du scalar_t -j1" << RESET <<endl;
  110. //scalar_print(stdout, (-j1).scalar());
  111. //JUMP;
  112. //cout << RED << "affichage du scalar_t l1" << RESET <<endl;
  113. //scalar_print(stdout, l1.scalar());
  114. //JUMP;
  115. //cout << RED << "affichage du scalar_t -l1" << RESET <<endl;
  116. //scalar_print(stdout, (-l1).scalar());
  117. //JUMP;
  118. //curvepoint_fp_scalarmult_vartime(temp3,op[0],coeff3.scalar());
  119. curvepoint_fp_scalarmult_vartime(temp3a,op[0],j1.scalar());
  120. curvepoint_fp_scalarmult_vartime(temp3b,temp3a,l1.scalar());
  121. curvepoint_fp_neg(temp3c,temp3b);
  122. curvepoint_fp_makeaffine(temp3c);
  123. //cout << RED << "si bit_urandom=0 et op=u1 affichage du curvepoint -(j1*l1)u1[0]=-(j1*l1) (lambda1*i1*g)=" << RESET << endl; curvepoint_fp_print(stdout,temp3c); JUMP;
  124. //curvepoint_fp_scalarmult_vartime(temp4,op[1],coeff4.scalar());
  125. curvepoint_fp_scalarmult_vartime(temp4a,op[1],i1.scalar());
  126. curvepoint_fp_scalarmult_vartime(temp4b,temp4a,l1.scalar());
  127. curvepoint_fp_makeaffine(temp4b);
  128. //cout << RED << "si bit_urandom=0 et op=u1 affichage du curvepoint (i1*l1)u1[1]=(i1*l1) (lambda1*j1*g)=" << RESET << endl; curvepoint_fp_print(stdout,temp4b); JUMP;
  129. curvepoint_fp_add_vartime(rop1,temp3c,temp4b);
  130. curvepoint_fp_makeaffine(rop1);
  131. curvepoint_fp_set(rop[1],rop1);
  132. //cout << RED << "si bit_urandom=0 et op=u1 affichage du curvepoint pi_1(u1)[1]=-(j1*l1)u1[0]+(i1*l1)u1[1]=-(j1*l1) lambda1*i1*g+(i1*l1) lambda1*j1*g" << RESET << endl; curvepoint_fp_print(stdout,rop[1]); JUMP;
  133. return rop;
  134. }
  135. Bipoint<twistpoint_fp2_t> PrivateKey::pi_2(Bipoint<twistpoint_fp2_t> op)
  136. {
  137. Bipoint<twistpoint_fp2_t> rop;
  138. twistpoint_fp2_t rop0, rop1; // rop0=coeff1.op[0] + coeff2.op[1]=(-j2*k2)x +(i2*k2)y rop1=coeff3.op[0] + coeff4.op[1]=(-j2*l2)x + (i2*l2)y
  139. //Fp coeff1 = -j2*k2, coeff2 = i2*k2, coeff3 = -j2*l2, coeff4 = i2*l2;
  140. twistpoint_fp2_t temp1a,temp2a,temp3a,temp4a,temp1b,temp2b,temp3b,temp4b,temp1c,temp3c;
  141. //twistpoint_fp2_scalarmult_vartime(temp1,op[0],coeff1.scalar());
  142. twistpoint_fp2_scalarmult_vartime(temp1a,op[0],j2.scalar());
  143. twistpoint_fp2_scalarmult_vartime(temp1b,temp1a,k2.scalar());
  144. twistpoint_fp2_neg(temp1c,temp1b);
  145. twistpoint_fp2_makeaffine(temp1c);
  146. //twistpoint_fp2_scalarmult_vartime(temp2,op[1],coeff2.scalar());
  147. twistpoint_fp2_scalarmult_vartime(temp2a,op[1],i2.scalar());
  148. twistpoint_fp2_scalarmult_vartime(temp2b,temp2a,k2.scalar());
  149. twistpoint_fp2_makeaffine(temp2b);
  150. twistpoint_fp2_add_vartime(rop0,temp1c,temp2b);
  151. twistpoint_fp2_makeaffine(rop0);
  152. twistpoint_fp2_set(rop[0],rop0);
  153. //twistpoint_fp2_scalarmult_vartime(temp3,op[0],coeff3.scalar());
  154. twistpoint_fp2_scalarmult_vartime(temp3a,op[0],j2.scalar());
  155. twistpoint_fp2_scalarmult_vartime(temp3b,temp3a,l2.scalar());
  156. twistpoint_fp2_neg(temp3c,temp3b);
  157. twistpoint_fp2_makeaffine(temp3c);
  158. //twistpoint_fp2_scalarmult_vartime(temp4,op[1],coeff4.scalar());
  159. twistpoint_fp2_scalarmult_vartime(temp4a,op[1],i2.scalar());
  160. twistpoint_fp2_scalarmult_vartime(temp4b,temp4a,l2.scalar());
  161. twistpoint_fp2_makeaffine(temp4b);
  162. twistpoint_fp2_add_vartime(rop1,temp3c,temp4b);
  163. twistpoint_fp2_makeaffine(rop1);
  164. twistpoint_fp2_set(rop[1],rop1);
  165. return rop;
  166. }
  167. Quadripoint PrivateKey::pi_T(Quadripoint op)
  168. {
  169. Quadripoint rop;
  170. fp12e_t rop1, rop2, rop3, rop4;
  171. fp12e_t temp1[20],temp2[20],temp3[20],temp4[20];
  172. //void fp12e_pow_vartime(fp12e_t rop, const fp12e_t op, const scalar_t exp);
  173. //void fp12e_mul(fp12e_t rop, const fp12e_t op1, const fp12e_t op2);
  174. //void fp12e_invert(fp12e_t rop, const fp12e_t op);
  175. //fpe_t un={139089155.,0,0,0,0,0,0,0,0,0,0,0};
  176. //fpe_t deux={13908915423.,0,0,0,0,0,0,0,0,0,0,0};
  177. //Fp UN,DEUX;
  178. //UN.set(un);
  179. //DEUX.set(deux);
  180. //zout(fpe2mpz(un),fpe2mpz(deux));
  181. //fp12e_pow_vartime(temp1[0],op[0],UN.scalar());
  182. //fp12e_pow_vartime(temp1[1],temp1[0],DEUX.scalar());
  183. //fp12e_print(stdout,temp1[1]);
  184. //JUMP;
  185. //fp12e_pow_vartime(temp1[2],op[0],(UN*DEUX).scalar());
  186. //fp12e_print(stdout,temp1[2]);
  187. //JUMP;
  188. //scalar_print(stdout,(UN*DEUX).scalar());
  189. //jump;
  190. //zout(scalar2mpz((UN*DEUX).scalar()))
  191. //exit(0);
  192. //composante 1
  193. //scalar1024 exp1={}, exp2={}, exp3={}, exp4={};
  194. //mpz2scalar1024(exp1,scalar2mpz(j1.scalar())*scalar2mpz(k1.scalar())*scalar2mpz(j2.scalar())*scalar2mpz(k2.scalar()));
  195. //mpz2scalar1024(exp2,scalar2mpz(j1.scalar())*scalar2mpz(k1.scalar())*scalar2mpz(i2.scalar())*scalar2mpz(k2.scalar()));
  196. //mpz2scalar1024(exp3,scalar2mpz(i1.scalar())*scalar2mpz(k1.scalar())*scalar2mpz(j2.scalar())*scalar2mpz(k2.scalar()));
  197. //mpz2scalar1024(exp4,scalar2mpz(i1.scalar())*scalar2mpz(k1.scalar())*scalar2mpz(i2.scalar())*scalar2mpz(k2.scalar()));
  198. //fp12e_pow_vartime1024(temp1[3],op[0],exp1);
  199. //fp12e_invert(temp1[4],op[1]);
  200. //fp12e_pow_vartime1024(temp1[8],temp1[4],exp2);
  201. //fp12e_invert(temp1[9],op[2]);
  202. //fp12e_pow_vartime1024(temp1[13],temp1[9],exp3);
  203. //fp12e_pow_vartime1024(temp1[17],op[3],exp4);
  204. fp12e_pow_vartime(temp1[0],op[0],j1.scalar()); // le produit d'entiers j1*k1 est trop grand pour aller dans un scalaire
  205. fp12e_pow_vartime(temp1[1],temp1[0],k1.scalar()); //le produit de fpe_t (j1*k1) est réduit modulo p
  206. fp12e_pow_vartime(temp1[2],temp1[1],j2.scalar());
  207. fp12e_pow_vartime(temp1[3],temp1[2],k2.scalar());
  208. fp12e_invert(temp1[4],op[1]);
  209. fp12e_pow_vartime(temp1[5],temp1[4],j1.scalar());
  210. fp12e_pow_vartime(temp1[6],temp1[5],k1.scalar());
  211. fp12e_pow_vartime(temp1[7],temp1[6],i2.scalar());
  212. fp12e_pow_vartime(temp1[8],temp1[7],k2.scalar());
  213. fp12e_invert(temp1[9],op[2]);
  214. fp12e_pow_vartime(temp1[10],temp1[9],i1.scalar());
  215. fp12e_pow_vartime(temp1[11],temp1[10],k1.scalar());
  216. fp12e_pow_vartime(temp1[12],temp1[11],j2.scalar());
  217. fp12e_pow_vartime(temp1[13],temp1[12],k2.scalar());
  218. fp12e_pow_vartime(temp1[14],op[3],i1.scalar());
  219. fp12e_pow_vartime(temp1[15],temp1[14],k1.scalar());
  220. fp12e_pow_vartime(temp1[16],temp1[15],i2.scalar());
  221. fp12e_pow_vartime(temp1[17],temp1[16],k2.scalar());
  222. fp12e_mul(temp1[18],temp1[3],temp1[8]);
  223. fp12e_mul(temp1[19],temp1[13],temp1[17]);
  224. fp12e_mul(rop1,temp1[18],temp1[19]);
  225. //composante 2
  226. fp12e_pow_vartime(temp2[0],op[0],j1.scalar());
  227. fp12e_pow_vartime(temp2[1],temp2[0],k1.scalar());
  228. fp12e_pow_vartime(temp2[2],temp2[1],j2.scalar());
  229. fp12e_pow_vartime(temp2[3],temp2[2],l2.scalar());
  230. fp12e_pow_vartime(temp2[4],op[1],j1.scalar());
  231. fp12e_invert(temp2[5],temp2[4]);
  232. fp12e_pow_vartime(temp2[6],temp2[5],k1.scalar());
  233. fp12e_pow_vartime(temp2[7],temp2[6],i2.scalar());
  234. fp12e_pow_vartime(temp2[8],temp2[7],l2.scalar());
  235. fp12e_pow_vartime(temp2[9],op[2],i1.scalar());
  236. fp12e_pow_vartime(temp2[10],temp2[9],k1.scalar());
  237. fp12e_pow_vartime(temp2[11],temp2[10],j2.scalar());
  238. fp12e_invert(temp2[12],temp2[11]);
  239. fp12e_pow_vartime(temp2[13],temp2[12],l2.scalar());
  240. //
  241. fp12e_pow_vartime(temp2[14],op[3],i1.scalar());
  242. fp12e_pow_vartime(temp2[15],temp2[14],k1.scalar());
  243. fp12e_pow_vartime(temp2[16],temp2[15],i2.scalar());
  244. fp12e_pow_vartime(temp2[17],temp2[16],l2.scalar());
  245. fp12e_mul(temp2[18],temp2[3],temp2[8]);
  246. fp12e_mul(temp2[19],temp2[13],temp2[17]);
  247. fp12e_mul(rop2,temp2[18],temp2[19]);
  248. //composante 3
  249. fp12e_pow_vartime(temp3[0],op[0],j1.scalar());
  250. fp12e_pow_vartime(temp3[1],temp3[0],l1.scalar());
  251. fp12e_pow_vartime(temp3[2],temp3[1],j2.scalar());
  252. fp12e_pow_vartime(temp3[3],temp3[2],k2.scalar());
  253. fp12e_pow_vartime(temp3[4],op[1],j1.scalar());
  254. fp12e_invert(temp3[5],temp3[4]);
  255. fp12e_pow_vartime(temp3[6],temp3[5],l1.scalar());
  256. fp12e_pow_vartime(temp3[7],temp3[6],i2.scalar());
  257. fp12e_pow_vartime(temp3[8],temp3[7],k2.scalar());
  258. fp12e_pow_vartime(temp3[9],op[2],i1.scalar());
  259. fp12e_pow_vartime(temp3[10],temp3[9],l1.scalar());
  260. fp12e_pow_vartime(temp3[11],temp3[10],j2.scalar());
  261. fp12e_invert(temp3[12],temp3[11]);
  262. fp12e_pow_vartime(temp3[13],temp3[12],k2.scalar());
  263. fp12e_pow_vartime(temp3[14],op[3],i1.scalar());
  264. fp12e_pow_vartime(temp3[15],temp3[14],l1.scalar());
  265. fp12e_pow_vartime(temp3[16],temp3[15],i2.scalar());
  266. fp12e_pow_vartime(temp3[17],temp3[16],k2.scalar());
  267. fp12e_mul(temp3[18],temp3[3],temp3[8]);
  268. fp12e_mul(temp3[19],temp3[13],temp3[17]);
  269. fp12e_mul(rop3,temp3[18],temp3[19]);
  270. //composante 4
  271. fp12e_pow_vartime(temp4[0],op[0],j1.scalar());
  272. fp12e_pow_vartime(temp4[1],temp4[0],l1.scalar());
  273. fp12e_pow_vartime(temp4[2],temp4[1],j2.scalar());
  274. fp12e_pow_vartime(temp4[3],temp4[2],l2.scalar());
  275. fp12e_pow_vartime(temp4[4],op[1],j1.scalar());
  276. fp12e_invert(temp4[5],temp4[4]);
  277. fp12e_pow_vartime(temp4[6],temp4[5],l1.scalar());
  278. fp12e_pow_vartime(temp4[7],temp4[6],i2.scalar());
  279. fp12e_pow_vartime(temp4[8],temp4[7],l2.scalar());
  280. fp12e_pow_vartime(temp4[9],op[2],i1.scalar());
  281. fp12e_pow_vartime(temp4[10],temp4[9],l1.scalar());
  282. fp12e_pow_vartime(temp4[11],temp4[10],j2.scalar());
  283. fp12e_invert(temp4[12],temp4[11]);
  284. fp12e_pow_vartime(temp4[13],temp4[12],l2.scalar());
  285. fp12e_pow_vartime(temp4[14],op[3],i1.scalar());
  286. fp12e_pow_vartime(temp4[15],temp4[14],l1.scalar());
  287. fp12e_pow_vartime(temp4[16],temp4[15],i2.scalar());
  288. fp12e_pow_vartime(temp4[17],temp4[16],l2.scalar());
  289. fp12e_mul(temp4[18],temp4[3],temp4[8]);
  290. fp12e_mul(temp4[19],temp4[13],temp4[17]);
  291. fp12e_mul(rop4,temp4[18],temp4[19]);
  292. rop.set(rop1,0);
  293. rop.set(rop2,1);
  294. rop.set(rop3,2);
  295. rop.set(rop4,3);
  296. return rop;
  297. }