PrivateKey.cpp 5.8 KB

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