PrivateKey.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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. std::ostream& operator<<(std::ostream& os, const BGNPrivateKey& output)
  70. {
  71. os << output.a1 << output.b1 << output.c1 << output.d1;
  72. os << output.a2 << output.b2 << output.c2 << output.d2;
  73. os << output.pi_1_curvegen;
  74. os << output.pi_2_twistgen;
  75. os << output.pi_T_pairgen;
  76. return os;
  77. }
  78. std::istream& operator>>(std::istream& is, BGNPrivateKey& input)
  79. {
  80. is >> input.a1 >> input.b1 >> input.c1 >> input.d1;
  81. is >> input.a2 >> input.b2 >> input.c2 >> input.d2;
  82. is >> input.pi_1_curvegen;
  83. is >> input.pi_2_twistgen;
  84. is >> input.pi_T_pairgen;
  85. input.curve_max_checked = Scalar(0);
  86. input.twist_max_checked = Scalar(0);
  87. input.pair_max_checked = Scalar(0);
  88. input.curve_memoizer.clear();
  89. input.twist_memoizer.clear();
  90. input.pair_memoizer.clear();
  91. input.curve_memoizer[input.pi_1_curvegen * input.curve_max_checked] = input.curve_max_checked;
  92. input.twist_memoizer[input.pi_2_twistgen * input.twist_max_checked] = input.twist_max_checked;
  93. input.pair_memoizer[input.pi_T_pairgen * input.pair_max_checked] = input.pair_max_checked;
  94. return is;
  95. }
  96. BGNPrivateKey::BGNPrivateKey()
  97. { }
  98. 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)
  99. {
  100. this->a1 = a1;
  101. this->b1 = b1;
  102. this->c1 = c1;
  103. this->d1 = d1;
  104. this->a2 = a2;
  105. this->b2 = b2;
  106. this->c2 = c2;
  107. this->d2 = d2;
  108. this->pi_1_curvegen = pi_1(pub_key.get_bipoint_curvegen());
  109. this->pi_2_twistgen = pi_2(pub_key.get_bipoint_twistgen());
  110. this->pi_T_pairgen = pi_T(pairing(pub_key.get_bipoint_curvegen(), pub_key.get_bipoint_twistgen()));
  111. this->curve_max_checked = Scalar(0);
  112. this->twist_max_checked = Scalar(0);
  113. this->pair_max_checked = Scalar(0);
  114. this->curve_memoizer[this->pi_1_curvegen * this->curve_max_checked] = this->curve_max_checked;
  115. this->twist_memoizer[this->pi_2_twistgen * this->twist_max_checked] = this->twist_max_checked;
  116. this->pair_memoizer[this->pi_T_pairgen * this->pair_max_checked] = this->pair_max_checked;
  117. }
  118. CurveBipoint BGNPrivateKey::pi_1(const CurveBipoint& input) const
  119. {
  120. CurveBipoint retval;
  121. curvepoint_fp_t temp0, temp1;
  122. b1.mult(temp0, input[0]);
  123. c1.mult(temp0, temp0);
  124. curvepoint_fp_neg(temp0, temp0);
  125. a1.mult(temp1, input[1]);
  126. c1.mult(temp1, temp1);
  127. curvepoint_fp_add_vartime(temp0, temp0, temp1);
  128. curvepoint_fp_set(retval[0], temp0);
  129. b1.mult(temp0, input[0]);
  130. d1.mult(temp0, temp0);
  131. curvepoint_fp_neg(temp0, temp0);
  132. a1.mult(temp1, input[1]);
  133. d1.mult(temp1, temp1);
  134. curvepoint_fp_add_vartime(temp0, temp0, temp1);
  135. curvepoint_fp_set(retval[1], temp0);
  136. return retval;
  137. }
  138. TwistBipoint BGNPrivateKey::pi_2(const TwistBipoint& input) const
  139. {
  140. TwistBipoint retval;
  141. twistpoint_fp2_t temp0, temp1;
  142. b2.mult(temp0, input[0]);
  143. c2.mult(temp0, temp0);
  144. twistpoint_fp2_neg(temp0, temp0);
  145. a2.mult(temp1, input[1]);
  146. c2.mult(temp1, temp1);
  147. twistpoint_fp2_add_vartime(temp0, temp0, temp1);
  148. twistpoint_fp2_set(retval[0], temp0);
  149. b2.mult(temp0, input[0]);
  150. d2.mult(temp0, temp0);
  151. twistpoint_fp2_neg(temp0, temp0);
  152. a2.mult(temp1, input[1]);
  153. d2.mult(temp1, temp1);
  154. twistpoint_fp2_add_vartime(temp0, temp0, temp1);
  155. twistpoint_fp2_set(retval[1], temp0);
  156. return retval;
  157. }
  158. Quadripoint BGNPrivateKey::pi_T(const Quadripoint& input) const
  159. {
  160. Quadripoint retval;
  161. fp12e_t temp0, temp1, temp2, temp3;
  162. b1.mult(temp0, input[0]);
  163. c1.mult(temp0, temp0);
  164. b2.mult(temp0, temp0);
  165. c2.mult(temp0, temp0);
  166. b1.mult(temp1, input[1]);
  167. c1.mult(temp1, temp1);
  168. a2.mult(temp1, temp1);
  169. c2.mult(temp1, temp1);
  170. fp12e_invert(temp1, temp1);
  171. a1.mult(temp2, input[2]);
  172. c1.mult(temp2, temp2);
  173. b2.mult(temp2, temp2);
  174. c2.mult(temp2, temp2);
  175. fp12e_invert(temp2, temp2);
  176. a1.mult(temp3, input[3]);
  177. c1.mult(temp3, temp3);
  178. a2.mult(temp3, temp3);
  179. c2.mult(temp3, temp3);
  180. fp12e_mul(temp0, temp0, temp1);
  181. fp12e_mul(temp1, temp2, temp3);
  182. fp12e_mul(temp0, temp0, temp1);
  183. fp12e_set(retval[0], temp0);
  184. b1.mult(temp0, input[0]);
  185. c1.mult(temp0, temp0);
  186. b2.mult(temp0, temp0);
  187. d2.mult(temp0, temp0);
  188. b1.mult(temp1, input[1]);
  189. c1.mult(temp1, temp1);
  190. a2.mult(temp1, temp1);
  191. d2.mult(temp1, temp1);
  192. fp12e_invert(temp1, temp1);
  193. a1.mult(temp2, input[2]);
  194. c1.mult(temp2, temp2);
  195. b2.mult(temp2, temp2);
  196. d2.mult(temp2, temp2);
  197. fp12e_invert(temp2, temp2);
  198. a1.mult(temp3, input[3]);
  199. c1.mult(temp3, temp3);
  200. a2.mult(temp3, temp3);
  201. d2.mult(temp3, temp3);
  202. fp12e_mul(temp0, temp0, temp1);
  203. fp12e_mul(temp1, temp2, temp3);
  204. fp12e_mul(temp0, temp0, temp1);
  205. fp12e_set(retval[1], temp0);
  206. b1.mult(temp0, input[0]);
  207. d1.mult(temp0, temp0);
  208. b2.mult(temp0, temp0);
  209. c2.mult(temp0, temp0);
  210. b1.mult(temp1, input[1]);
  211. d1.mult(temp1, temp1);
  212. a2.mult(temp1, temp1);
  213. c2.mult(temp1, temp1);
  214. fp12e_invert(temp1, temp1);
  215. a1.mult(temp2, input[2]);
  216. d1.mult(temp2, temp2);
  217. b2.mult(temp2, temp2);
  218. c2.mult(temp2, temp2);
  219. fp12e_invert(temp2, temp2);
  220. a1.mult(temp3, input[3]);
  221. d1.mult(temp3, temp3);
  222. a2.mult(temp3, temp3);
  223. c2.mult(temp3, temp3);
  224. fp12e_mul(temp0, temp0, temp1);
  225. fp12e_mul(temp1, temp2, temp3);
  226. fp12e_mul(temp0, temp0, temp1);
  227. fp12e_set(retval[2], temp0);
  228. b1.mult(temp0, input[0]);
  229. d1.mult(temp0, temp0);
  230. b2.mult(temp0, temp0);
  231. d2.mult(temp0, temp0);
  232. b1.mult(temp1, input[1]);
  233. d1.mult(temp1, temp1);
  234. a2.mult(temp1, temp1);
  235. d2.mult(temp1, temp1);
  236. fp12e_invert(temp1, temp1);
  237. a1.mult(temp2, input[2]);
  238. d1.mult(temp2, temp2);
  239. b2.mult(temp2, temp2);
  240. d2.mult(temp2, temp2);
  241. fp12e_invert(temp2, temp2);
  242. a1.mult(temp3, input[3]);
  243. d1.mult(temp3, temp3);
  244. a2.mult(temp3, temp3);
  245. d2.mult(temp3, temp3);
  246. fp12e_mul(temp0, temp0, temp1);
  247. fp12e_mul(temp1, temp2, temp3);
  248. fp12e_mul(temp0, temp0, temp1);
  249. fp12e_set(retval[3], temp0);
  250. return retval;
  251. }