PrivateKey.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  1. #include "PrivateKey.hpp"
  2. Scalar PrivateKey::decrypt(const CurveBipoint& ciphertext)
  3. {
  4. CurveBipoint pi_1_ciphertext = pi_1(ciphertext);
  5. auto lookup = curve_memoizer.find(pi_1_ciphertext);
  6. if (lookup != curve_memoizer.end())
  7. {
  8. return lookup->second;
  9. }
  10. curve_max_checked++;
  11. CurveBipoint i = pi_1_curvegen * curve_max_checked;
  12. while (i != pi_1_ciphertext)
  13. {
  14. curve_memoizer[i] = curve_max_checked;
  15. i = i + pi_1_curvegen;
  16. curve_max_checked++;
  17. }
  18. curve_memoizer[i] = curve_max_checked;
  19. return curve_max_checked;
  20. }
  21. Scalar PrivateKey::decrypt(const TwistBipoint& ciphertext)
  22. {
  23. TwistBipoint pi_2_ciphertext = pi_2(ciphertext);
  24. auto lookup = twist_memoizer.find(pi_2_ciphertext);
  25. if (lookup != twist_memoizer.end())
  26. {
  27. return lookup->second;
  28. }
  29. twist_max_checked++;
  30. TwistBipoint i = pi_2_twistgen * twist_max_checked;
  31. while (i != pi_2_ciphertext)
  32. {
  33. twist_memoizer[i] = twist_max_checked;
  34. i = i + pi_2_twistgen;
  35. twist_max_checked++;
  36. }
  37. twist_memoizer[i] = twist_max_checked;
  38. return twist_max_checked;
  39. }
  40. Scalar PrivateKey::decrypt(const Quadripoint& ciphertext)
  41. {
  42. Quadripoint pi_T_ciphertext = pi_T(ciphertext);
  43. auto lookup = pair_memoizer.find(pi_T_ciphertext);
  44. if (lookup != pair_memoizer.end())
  45. {
  46. return lookup->second;
  47. }
  48. pair_max_checked++;
  49. Quadripoint i = pi_T_pairgen * pair_max_checked;
  50. while (i != pi_T_ciphertext)
  51. {
  52. pair_memoizer[i] = pair_max_checked;
  53. i = i + pi_T_pairgen;
  54. pair_max_checked++;
  55. }
  56. pair_memoizer[i] = pair_max_checked;
  57. return pair_max_checked;
  58. }
  59. PrivateKey::PrivateKey()
  60. { }
  61. void PrivateKey::set(const PublicKey& pub_key, const Scalar& a1, const Scalar& b1, const Scalar& c1, const Scalar& d1, const Scalar& a2, const Scalar& b2, const Scalar& c2, const Scalar& d2)
  62. {
  63. this->a1 = a1;
  64. this->b1 = b1;
  65. this->c1 = c1;
  66. this->d1 = d1;
  67. this->a2 = a2;
  68. this->b2 = b2;
  69. this->c2 = c2;
  70. this->d2 = d2;
  71. this->pi_1_curvegen = pi_1(pub_key.get_bipoint_curvegen());
  72. this->pi_2_twistgen = pi_2(pub_key.get_bipoint_twistgen());
  73. this->pi_T_pairgen = pi_T(pairing(pub_key.get_bipoint_curvegen(), pub_key.get_bipoint_twistgen()));
  74. this->curve_max_checked = Scalar(0);
  75. this->twist_max_checked = Scalar(0);
  76. this->pair_max_checked = Scalar(0);
  77. this->curve_memoizer[this->pi_1_curvegen * this->curve_max_checked] = this->curve_max_checked;
  78. this->twist_memoizer[this->pi_2_twistgen * this->twist_max_checked] = this->twist_max_checked;
  79. this->pair_memoizer[this->pi_T_pairgen * this->pair_max_checked] = this->pair_max_checked;
  80. }
  81. CurveBipoint PrivateKey::pi_1(const CurveBipoint& input) const
  82. {
  83. CurveBipoint retval;
  84. curvepoint_fp_t temp0, temp1;
  85. b1.mult(temp0, input[0]);
  86. c1.mult(temp0, temp0);
  87. curvepoint_fp_neg(temp0, temp0);
  88. a1.mult(temp1, input[1]);
  89. c1.mult(temp1, temp1);
  90. curvepoint_fp_add_vartime(temp0, temp0, temp1);
  91. curvepoint_fp_set(retval[0], temp0);
  92. b1.mult(temp0, input[0]);
  93. d1.mult(temp0, temp0);
  94. curvepoint_fp_neg(temp0, temp0);
  95. a1.mult(temp1, input[1]);
  96. d1.mult(temp1, temp1);
  97. curvepoint_fp_add_vartime(temp0, temp0, temp1);
  98. curvepoint_fp_set(retval[1], temp0);
  99. return retval;
  100. }
  101. TwistBipoint PrivateKey::pi_2(const TwistBipoint& input) const
  102. {
  103. TwistBipoint retval;
  104. twistpoint_fp2_t temp0, temp1;
  105. b2.mult(temp0, input[0]);
  106. c2.mult(temp0, temp0);
  107. twistpoint_fp2_neg(temp0, temp0);
  108. a2.mult(temp1, input[1]);
  109. c2.mult(temp1, temp1);
  110. twistpoint_fp2_add_vartime(temp0, temp0, temp1);
  111. twistpoint_fp2_set(retval[0], temp0);
  112. b2.mult(temp0, input[0]);
  113. d2.mult(temp0, temp0);
  114. twistpoint_fp2_neg(temp0, temp0);
  115. a2.mult(temp1, input[1]);
  116. d2.mult(temp1, temp1);
  117. twistpoint_fp2_add_vartime(temp0, temp0, temp1);
  118. twistpoint_fp2_set(retval[1], temp0);
  119. return retval;
  120. }
  121. Quadripoint PrivateKey::pi_T(const Quadripoint& input) const
  122. {
  123. Quadripoint retval;
  124. fp12e_t temp0, temp1, temp2, temp3;
  125. b1.mult(temp0, input[0]);
  126. c1.mult(temp0, temp0);
  127. b2.mult(temp0, temp0);
  128. c2.mult(temp0, temp0);
  129. b1.mult(temp1, input[1]);
  130. c1.mult(temp1, temp1);
  131. a2.mult(temp1, temp1);
  132. c2.mult(temp1, temp1);
  133. fp12e_invert(temp1, temp1);
  134. a1.mult(temp2, input[2]);
  135. c1.mult(temp2, temp2);
  136. b2.mult(temp2, temp2);
  137. c2.mult(temp2, temp2);
  138. fp12e_invert(temp2, temp2);
  139. a1.mult(temp3, input[3]);
  140. c1.mult(temp3, temp3);
  141. a2.mult(temp3, temp3);
  142. c2.mult(temp3, temp3);
  143. fp12e_mul(temp0, temp0, temp1);
  144. fp12e_mul(temp1, temp2, temp3);
  145. fp12e_mul(temp0, temp0, temp1);
  146. fp12e_set(retval[0], temp0);
  147. b1.mult(temp0, input[0]);
  148. c1.mult(temp0, temp0);
  149. b2.mult(temp0, temp0);
  150. d2.mult(temp0, temp0);
  151. b1.mult(temp1, input[1]);
  152. c1.mult(temp1, temp1);
  153. a2.mult(temp1, temp1);
  154. d2.mult(temp1, temp1);
  155. fp12e_invert(temp1, temp1);
  156. a1.mult(temp2, input[2]);
  157. c1.mult(temp2, temp2);
  158. b2.mult(temp2, temp2);
  159. d2.mult(temp2, temp2);
  160. fp12e_invert(temp2, temp2);
  161. a1.mult(temp3, input[3]);
  162. c1.mult(temp3, temp3);
  163. a2.mult(temp3, temp3);
  164. d2.mult(temp3, temp3);
  165. fp12e_mul(temp0, temp0, temp1);
  166. fp12e_mul(temp1, temp2, temp3);
  167. fp12e_mul(temp0, temp0, temp1);
  168. fp12e_set(retval[1], temp0);
  169. b1.mult(temp0, input[0]);
  170. d1.mult(temp0, temp0);
  171. b2.mult(temp0, temp0);
  172. c2.mult(temp0, temp0);
  173. b1.mult(temp1, input[1]);
  174. d1.mult(temp1, temp1);
  175. a2.mult(temp1, temp1);
  176. c2.mult(temp1, temp1);
  177. fp12e_invert(temp1, temp1);
  178. a1.mult(temp2, input[2]);
  179. d1.mult(temp2, temp2);
  180. b2.mult(temp2, temp2);
  181. c2.mult(temp2, temp2);
  182. fp12e_invert(temp2, temp2);
  183. a1.mult(temp3, input[3]);
  184. d1.mult(temp3, temp3);
  185. a2.mult(temp3, temp3);
  186. c2.mult(temp3, temp3);
  187. fp12e_mul(temp0, temp0, temp1);
  188. fp12e_mul(temp1, temp2, temp3);
  189. fp12e_mul(temp0, temp0, temp1);
  190. fp12e_set(retval[2], temp0);
  191. b1.mult(temp0, input[0]);
  192. d1.mult(temp0, temp0);
  193. b2.mult(temp0, temp0);
  194. d2.mult(temp0, temp0);
  195. b1.mult(temp1, input[1]);
  196. d1.mult(temp1, temp1);
  197. a2.mult(temp1, temp1);
  198. d2.mult(temp1, temp1);
  199. fp12e_invert(temp1, temp1);
  200. a1.mult(temp2, input[2]);
  201. d1.mult(temp2, temp2);
  202. b2.mult(temp2, temp2);
  203. d2.mult(temp2, temp2);
  204. fp12e_invert(temp2, temp2);
  205. a1.mult(temp3, input[3]);
  206. d1.mult(temp3, temp3);
  207. a2.mult(temp3, temp3);
  208. d2.mult(temp3, temp3);
  209. fp12e_mul(temp0, temp0, temp1);
  210. fp12e_mul(temp1, temp2, temp3);
  211. fp12e_mul(temp0, temp0, temp1);
  212. fp12e_set(retval[3], temp0);
  213. return retval;
  214. }