keygen.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. #include "keygen.hpp"
  2. const scalar_t &bn_r=bn_n;
  3. void keygen(PublicKey& public_key, PrivateKey& private_key)
  4. {
  5. //signature;
  6. /** générer des données privées avant la création d'une paire de clefs **/
  7. Fp i1,j1,k1,l1,i2,j2,k2,l2;
  8. do
  9. {
  10. ////cout << "j1 = " << endl;
  11. j1.set_random();
  12. //cout << "k1 = " << endl;
  13. k1.set_random();
  14. //cout << "l1 = " << endl;
  15. l1.set_random();
  16. //cout << "i1 = " << endl;
  17. i1.set_ad_minus_bc(j1,k1,l1);
  18. }
  19. while (scalar2mpz(i1.scalar())<0);
  20. do
  21. {
  22. //cout << "j2 = " << endl;
  23. j2.set_random();
  24. //cout << "k2 = " << endl;
  25. k2.set_random();
  26. //cout << "l2 = " << endl;
  27. l2.set_random();
  28. //cout << "i2 = " << endl;
  29. i2.set_ad_minus_bc(j2,k2,l2);
  30. }
  31. while (scalar2mpz(i2.scalar())<0);
  32. /** génération de la clef privée **/
  33. //cout << YELLOW << "Private Key Generation" << RESET << endl;
  34. //cout << "sk:=(pi_1,pi_2,pi_T)" << endl;
  35. // Pi_1(x,y)=((-j1*k1)x + (i1*k1)y ,(-j1*l1)x + (i1*l1)y)
  36. //PrivateKey private_key(i1,j1,k1,l1,i2,j2,k2,l2);
  37. private_key.set(i1,j1,k1,l1,i2,j2,k2,l2);
  38. //fpe_t test;
  39. //int i;
  40. //for(i=1;i<12;i++)
  41. //{
  42. //test->v[i] = 0.;
  43. //}
  44. //test->v[0] = 2;
  45. //i1.set(test);
  46. //private_key.Pi_1(x,y);
  47. /** génération de la clef publique **/
  48. //cout << YELLOW << "Public Key Generation" << RESET << endl;
  49. //cout << "pk:=(p, G, H, G_T, e, g, h, (i_1*g,j_1*g), (i_2*h,j_2*h))" << endl;
  50. // 1) (i_1*g,j_1*g) public_key.bipoint_curve (i_2*h,j_2*h) public_key:bipoint_twist
  51. // 2) on utilise bn_curvegen comme générateur g, on pourrait (devrait?) utiliser un générateur aléatoire plutôt qu'un générateur fixé
  52. curvepoint_fp_t c1,c2,c3,c4;
  53. //cout << RED << "affichage du curvepoint g = " << RESET << endl;
  54. //curvepoint_fp_print(stdout,bn_curvegen);
  55. //JUMP;
  56. // calcul du générateur (i1g,j1g) du sous groupe des bipoints de la courbe
  57. //ecris(g);
  58. //curvepoint_fp_print(stdout,bn_curvegen);
  59. //jump;
  60. //representation(bn_curvegen);
  61. curvepoint_fp_scalarmult_vartime(c1, bn_curvegen,i1.scalar());
  62. curvepoint_fp_scalarmult_vartime(c2, bn_curvegen,j1.scalar());
  63. curvepoint_fp_makeaffine(c1); //il faut homogénéiser pour pouvoir faire des tests lors du déchiffrement
  64. curvepoint_fp_makeaffine(c2);
  65. //cout << RED << "affichage du scalar_t i1" << RESET <<endl;
  66. //scalar_print(stdout, i1.scalar());
  67. //JUMP;
  68. //cout << RED << "affichage du scalar_t j1" << RESET <<endl;
  69. //scalar_print(stdout, j1.scalar());
  70. //JUMP;
  71. //cout << RED << "affichage du curvepoint i1 g = " << RESET << endl;
  72. //curvepoint_fp_print(stdout,c1);
  73. //JUMP;
  74. //representation(c1);
  75. //JUMP;
  76. //cout << RED << "affichage du curvepoint j1 g = " << RESET << endl;
  77. //curvepoint_fp_print(stdout,c2);
  78. //representation(c2);
  79. //JUMP;
  80. //ecris(2g);
  81. //curvepoint_fp_t temp;
  82. //curvepoint_fp_double(temp,bn_curvegen);
  83. //curvepoint_fp_makeaffine(temp);
  84. //curvepoint_fp_print(stdout,temp);
  85. //jump;
  86. //representation(temp);
  87. //ecris(g+g);
  88. //curvepoint_fp_add_vartime(temp,bn_curvegen,bn_curvegen);
  89. //curvepoint_fp_makeaffine(temp);
  90. //curvepoint_fp_print(stdout,temp);
  91. //jump;
  92. //representation(temp);
  93. //curvepoint_fp_t test1,test2;
  94. //curvepoint_fp_scalarmult_vartime(test1, c1,j1.scalar());
  95. //curvepoint_fp_scalarmult_vartime(test2, c2,i1.scalar());
  96. //cout << RED << "affichage du curvepoint j1 (i1 g) = " << RESET << endl;
  97. //curvepoint_fp_makeaffine(test1);
  98. //curvepoint_fp_print(stdout,test1);
  99. //JUMP;
  100. //cout << RED << "affichage du curvepoint i1 (j1 g) = " << RESET << endl;
  101. //curvepoint_fp_makeaffine(test2);
  102. //curvepoint_fp_print(stdout,test2);
  103. //JUMP;
  104. //Fp test3=j1*i1, test4=i1*j1;
  105. //cout << RED << "affichage du scalar_t (i1 j1) =" << RESET <<endl;
  106. //scalar_print(stdout, test3.scalar());
  107. //JUMP;
  108. //cout << RED << "affichage du scalar_t (j1 i1) =" << RESET <<endl;
  109. //scalar_print(stdout, test4.scalar());
  110. //JUMP;
  111. //curvepoint_fp_t test5,test6;
  112. //curvepoint_fp_scalarmult_vartime(test5,bn_curvegen,test3.scalar());
  113. //curvepoint_fp_scalarmult_vartime(test6,bn_curvegen,test4.scalar());
  114. //cout << RED << "affichage du curvepoint (i1j1) g = " << RESET << endl;
  115. //curvepoint_fp_makeaffine(test5);
  116. //curvepoint_fp_print(stdout,test5);
  117. //test5.get_fpe();
  118. //JUMP;
  119. //curvepoint_fp_makeaffine(test6);
  120. //curvepoint_fp_print(stdout,test6);
  121. //JUMP;
  122. //curvepoint_fp_t temp1,temp2, temp3, temp4;
  123. //curvepoint_fp_neg(temp1,test1);
  124. //curvepoint_fp_add_vartime(temp2,temp1,test5);
  125. //cout << RED << "affichage du curvepoint = test5 - test1 = (i1j1) g - j1 (i1 g)" << RESET << endl;
  126. //curvepoint_fp_makeaffine(temp2);
  127. //curvepoint_fp_print(stdout,temp2);
  128. //JUMP;
  129. //curvepoint_fp_neg(temp3,test5);
  130. //curvepoint_fp_add_vartime(temp4,test1,temp3);
  131. //cout << RED << "affichage du curvepoint = test1 - test5" << RESET << endl;
  132. //curvepoint_fp_makeaffine(temp4);
  133. //curvepoint_fp_print(stdout,temp4);
  134. //exit(0);
  135. //curvepoint_fp_t test6,test7;
  136. //curvepoint_fp_neg(test6,bn_curvegen);
  137. //curvepoint_fp_add_vartime(test7,bn_curvegen,test6);
  138. //cout << RED << "affichage du curvepoint = test7 =g-g" << RESET << endl;
  139. //curvepoint_fp_print(stdout,test7);
  140. //JUMP;
  141. Bipoint<curvepoint_fp_t> b1(c1,c2);
  142. // calcul du générateur (i2h,j2h) du sous groupe des bipoints du twist
  143. twistpoint_fp2_t t1,t2,t3,t4;
  144. twistpoint_fp2_scalarmult_vartime(t1, bn_twistgen,i2.scalar());
  145. twistpoint_fp2_scalarmult_vartime(t2, bn_twistgen,j2.scalar());
  146. twistpoint_fp2_makeaffine(t1);
  147. twistpoint_fp2_makeaffine(t2);
  148. Bipoint<twistpoint_fp2_t> b2(t1,t2);
  149. scalar_t s1, s2, s3, s4;
  150. //scalar_print(stdout,bn_r);
  151. scalar_setrandom(s1, bn_r);
  152. scalar_setrandom(s2, bn_r);
  153. scalar_setrandom(s3, bn_r);
  154. scalar_setrandom(s4, bn_r);
  155. // calcul de u, un bipoint de la courbe dans le groupe
  156. curvepoint_fp_scalarmult_vartime(c3, bn_curvegen,s1);
  157. curvepoint_fp_scalarmult_vartime(c4, bn_curvegen,s2);
  158. Bipoint<curvepoint_fp_t> b3(c3,c4);
  159. b3.makeaffine();
  160. // calcul de v, un bipoint du twist dans le groupe
  161. twistpoint_fp2_scalarmult_vartime(t3, bn_twistgen,s3);
  162. twistpoint_fp2_scalarmult_vartime(t4, bn_twistgen,s4);
  163. Bipoint<twistpoint_fp2_t> b4(t3,t4);
  164. b4.makeaffine();
  165. //PublicKey public_key(b1,b2,b3,b4);
  166. public_key.set(b1,b2,b3,b4);
  167. //public_key.bipoint_curvegen.set_point(p1,0);
  168. //public_key.bipoint_curvegen.set_point(p2,1);
  169. }