PrivateKey.cpp 7.3 KB

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