PrivateKey.cpp 6.4 KB

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