PrivateKey.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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;
  72. os << output.a2 << output.b2 << output.c2;
  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;
  81. input.d1 = ((input.b1).fieldMult(input.c1).fieldAdd(Scalar(1))).fieldMult((input.a1).fieldMultInverse());
  82. is >> input.a2 >> input.b2 >> input.c2;
  83. input.d2 = ((input.b2).fieldMult(input.c2).fieldAdd(Scalar(1))).fieldMult((input.a2).fieldMultInverse());
  84. is >> input.pi_1_curvegen;
  85. is >> input.pi_2_twistgen;
  86. is >> input.pi_T_pairgen;
  87. input.curve_max_checked = Scalar(0);
  88. input.twist_max_checked = Scalar(0);
  89. input.pair_max_checked = Scalar(0);
  90. input.curve_memoizer.clear();
  91. input.twist_memoizer.clear();
  92. input.pair_memoizer.clear();
  93. input.curve_memoizer[input.pi_1_curvegen * input.curve_max_checked] = input.curve_max_checked;
  94. input.twist_memoizer[input.pi_2_twistgen * input.twist_max_checked] = input.twist_max_checked;
  95. input.pair_memoizer[input.pi_T_pairgen * input.pair_max_checked] = input.pair_max_checked;
  96. return is;
  97. }
  98. BGNPrivateKey::BGNPrivateKey()
  99. { }
  100. 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)
  101. {
  102. this->a1 = a1;
  103. this->b1 = b1;
  104. this->c1 = c1;
  105. this->d1 = d1;
  106. this->a2 = a2;
  107. this->b2 = b2;
  108. this->c2 = c2;
  109. this->d2 = d2;
  110. this->pi_1_curvegen = pi_1(pub_key.get_bipoint_curvegen());
  111. this->pi_2_twistgen = pi_2(pub_key.get_bipoint_twistgen());
  112. this->pi_T_pairgen = pi_T(pairing(pub_key.get_bipoint_curvegen(), pub_key.get_bipoint_twistgen()));
  113. this->curve_max_checked = Scalar(0);
  114. this->twist_max_checked = Scalar(0);
  115. this->pair_max_checked = Scalar(0);
  116. this->curve_memoizer[this->pi_1_curvegen * this->curve_max_checked] = this->curve_max_checked;
  117. this->twist_memoizer[this->pi_2_twistgen * this->twist_max_checked] = this->twist_max_checked;
  118. this->pair_memoizer[this->pi_T_pairgen * this->pair_max_checked] = this->pair_max_checked;
  119. }
  120. CurveBipoint BGNPrivateKey::pi_1(const CurveBipoint& input) const
  121. {
  122. CurveBipoint retval;
  123. curvepoint_fp_t temp0, temp1;
  124. (-b1 * c1).mult(temp0, input[0]);
  125. (a1 * c1).mult(temp1, input[1]);
  126. curvepoint_fp_add_vartime(retval[0], temp0, temp1);
  127. (-b1 * d1).mult(temp0, input[0]);
  128. (a1 * d1).mult(temp1, input[1]);
  129. curvepoint_fp_add_vartime(retval[1], temp0, temp1);
  130. return retval;
  131. }
  132. TwistBipoint BGNPrivateKey::pi_2(const TwistBipoint& input) const
  133. {
  134. TwistBipoint retval;
  135. twistpoint_fp2_t temp0, temp1;
  136. (-b2 * c2).mult(temp0, input[0]);
  137. (a2 * c2).mult(temp1, input[1]);
  138. twistpoint_fp2_add_vartime(retval[0], temp0, temp1);
  139. (-b2 * d2).mult(temp0, input[0]);
  140. (a2 * d2).mult(temp1, input[1]);
  141. twistpoint_fp2_add_vartime(retval[1], temp0, temp1);
  142. return retval;
  143. }
  144. Quadripoint BGNPrivateKey::pi_T(const Quadripoint& input) const
  145. {
  146. Quadripoint retval;
  147. fp12e_t temp0, temp1, temp2;
  148. (b1 * c1 * b2 * c2).mult(temp0, input[0]);
  149. (-b1 * c1 * a2 * c2).mult(temp1, input[1]);
  150. fp12e_mul(temp0, temp0, temp1);
  151. (-a1 * c1 * b2 * c2).mult(temp1, input[2]);
  152. (a1 * c1 * a2 * c2).mult(temp2, input[3]);
  153. fp12e_mul(temp1, temp1, temp2);
  154. fp12e_mul(retval[0], temp0, temp1);
  155. (b1 * c1 * b2 * d2).mult(temp0, input[0]);
  156. (-b1 * c1 * a2 * d2).mult(temp1, input[1]);
  157. fp12e_mul(temp0, temp0, temp1);
  158. (-a1 * c1 * b2 * d2).mult(temp1, input[2]);
  159. (a1 * c1 * a2 * d2).mult(temp2, input[3]);
  160. fp12e_mul(temp1, temp1, temp2);
  161. fp12e_mul(retval[1], temp0, temp1);
  162. (b1 * d1 * b2 * c2).mult(temp0, input[0]);
  163. (-b1 * d1 * a2 * c2).mult(temp1, input[1]);
  164. fp12e_mul(temp0, temp0, temp1);
  165. (-a1 * d1 * b2 * c2).mult(temp1, input[2]);
  166. (a1 * d1 * a2 * c2).mult(temp2, input[3]);
  167. fp12e_mul(temp1, temp1, temp2);
  168. fp12e_mul(retval[2], temp0, temp1);
  169. (b1 * d1 * b2 * d2).mult(temp0, input[0]);
  170. (-b1 * d1 * a2 * d2).mult(temp1, input[1]);
  171. fp12e_mul(temp0, temp0, temp1);
  172. (-a1 * d1 * b2 * d2).mult(temp1, input[2]);
  173. (a1 * d1 * a2 * d2).mult(temp2, input[3]);
  174. fp12e_mul(temp1, temp1, temp2);
  175. fp12e_mul(retval[3], temp0, temp1);
  176. return retval;
  177. }