PrivateKey.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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 * c1).mult(temp0, input[0]);
  123. (a1 * c1).mult(temp1, input[1]);
  124. curvepoint_fp_add_vartime(retval[0], temp0, temp1);
  125. (-b1 * d1).mult(temp0, input[0]);
  126. (a1 * d1).mult(temp1, input[1]);
  127. curvepoint_fp_add_vartime(retval[1], temp0, temp1);
  128. return retval;
  129. }
  130. TwistBipoint BGNPrivateKey::pi_2(const TwistBipoint& input) const
  131. {
  132. TwistBipoint retval;
  133. twistpoint_fp2_t temp0, temp1;
  134. (-b2 * c2).mult(temp0, input[0]);
  135. (a2 * c2).mult(temp1, input[1]);
  136. twistpoint_fp2_add_vartime(retval[0], temp0, temp1);
  137. (-b2 * d2).mult(temp0, input[0]);
  138. (a2 * d2).mult(temp1, input[1]);
  139. twistpoint_fp2_add_vartime(retval[1], temp0, temp1);
  140. return retval;
  141. }
  142. Quadripoint BGNPrivateKey::pi_T(const Quadripoint& input) const
  143. {
  144. Quadripoint retval;
  145. fp12e_t temp0, temp1, temp2;
  146. (b1 * c1 * b2 * c2).mult(temp0, input[0]);
  147. (-b1 * c1 * a2 * c2).mult(temp1, input[1]);
  148. fp12e_mul(temp0, temp0, temp1);
  149. (-a1 * c1 * b2 * c2).mult(temp1, input[2]);
  150. (a1 * c1 * a2 * c2).mult(temp2, input[3]);
  151. fp12e_mul(temp1, temp1, temp2);
  152. fp12e_mul(retval[0], temp0, temp1);
  153. (b1 * c1 * b2 * d2).mult(temp0, input[0]);
  154. (-b1 * c1 * a2 * d2).mult(temp1, input[1]);
  155. fp12e_mul(temp0, temp0, temp1);
  156. (-a1 * c1 * b2 * d2).mult(temp1, input[2]);
  157. (a1 * c1 * a2 * d2).mult(temp2, input[3]);
  158. fp12e_mul(temp1, temp1, temp2);
  159. fp12e_mul(retval[1], temp0, temp1);
  160. (b1 * d1 * b2 * c2).mult(temp0, input[0]);
  161. (-b1 * d1 * a2 * c2).mult(temp1, input[1]);
  162. fp12e_mul(temp0, temp0, temp1);
  163. (-a1 * d1 * b2 * c2).mult(temp1, input[2]);
  164. (a1 * d1 * a2 * c2).mult(temp2, input[3]);
  165. fp12e_mul(temp1, temp1, temp2);
  166. fp12e_mul(retval[2], temp0, temp1);
  167. (b1 * d1 * b2 * d2).mult(temp0, input[0]);
  168. (-b1 * d1 * a2 * d2).mult(temp1, input[1]);
  169. fp12e_mul(temp0, temp0, temp1);
  170. (-a1 * d1 * b2 * d2).mult(temp1, input[2]);
  171. (a1 * d1 * a2 * d2).mult(temp2, input[3]);
  172. fp12e_mul(temp1, temp1, temp2);
  173. fp12e_mul(retval[3], temp0, temp1);
  174. return retval;
  175. }