Bipoint.cpp 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. #include "Bipoint.hpp"
  2. CurveBipoint::CurveBipoint()
  3. {
  4. curvepoint_fp_setneutral(point[0]);
  5. curvepoint_fp_setneutral(point[1]);
  6. }
  7. TwistBipoint::TwistBipoint()
  8. {
  9. twistpoint_fp2_setneutral(point[0]);
  10. twistpoint_fp2_setneutral(point[1]);
  11. }
  12. CurveBipoint::CurveBipoint(curvepoint_fp_t p1, curvepoint_fp_t p2)
  13. {
  14. curvepoint_fp_set(point[0], p1);
  15. curvepoint_fp_set(point[1], p2);
  16. }
  17. TwistBipoint::TwistBipoint(twistpoint_fp2_t p1, twistpoint_fp2_t p2)
  18. {
  19. twistpoint_fp2_set(point[0], p1);
  20. twistpoint_fp2_set(point[1], p2);
  21. }
  22. CurveBipoint::CurveBipoint(const Curvepoint& p1, const Curvepoint& p2)
  23. {
  24. curvepoint_fp_set(point[0], p1.toCurvepointFpT());
  25. curvepoint_fp_set(point[1], p2.toCurvepointFpT());
  26. }
  27. curvepoint_fp_t& CurveBipoint::operator[](int n)
  28. {
  29. return point[n];
  30. }
  31. twistpoint_fp2_t& TwistBipoint::operator[](int n)
  32. {
  33. return point[n];
  34. }
  35. const curvepoint_fp_t& CurveBipoint::operator[](int n) const
  36. {
  37. return point[n];
  38. }
  39. const twistpoint_fp2_t& TwistBipoint::operator[](int n) const
  40. {
  41. return point[n];
  42. }
  43. CurveBipoint CurveBipoint::operator+(const CurveBipoint& b) const
  44. {
  45. CurveBipoint retval;
  46. if (equal(point[0], b[0]))
  47. curvepoint_fp_double(retval[0], point[0]);
  48. else
  49. curvepoint_fp_add_vartime(retval[0], point[0], b[0]);
  50. if (equal(point[1], b[1]))
  51. curvepoint_fp_double(retval[1], point[1]);
  52. else
  53. curvepoint_fp_add_vartime(retval[1], point[1], b[1]);
  54. return retval;
  55. }
  56. TwistBipoint TwistBipoint::operator+(const TwistBipoint& b) const
  57. {
  58. TwistBipoint retval;
  59. if (equal(point[0], b[0]))
  60. twistpoint_fp2_double(retval[0], point[0]);
  61. else
  62. twistpoint_fp2_add_vartime(retval[0], point[0], b[0]);
  63. if (equal(point[1], b[1]))
  64. twistpoint_fp2_double(retval[1], point[1]);
  65. else
  66. twistpoint_fp2_add_vartime(retval[1], point[1], b[1]);
  67. return retval;
  68. }
  69. CurveBipoint CurveBipoint::operator-(const CurveBipoint& b) const
  70. {
  71. CurveBipoint retval, inverseB;
  72. if (!equal(point[0], b[0]))
  73. {
  74. curvepoint_fp_neg(inverseB[0], b[0]);
  75. curvepoint_fp_add_vartime(retval[0], point[0], inverseB[0]);
  76. }
  77. if (!equal(point[1], b[1]))
  78. {
  79. curvepoint_fp_neg(inverseB[1], b[1]);
  80. curvepoint_fp_add_vartime(retval[1], point[1], inverseB[1]);
  81. }
  82. return retval;
  83. }
  84. TwistBipoint TwistBipoint::operator-(const TwistBipoint& b) const
  85. {
  86. TwistBipoint retval, inverseB;
  87. if (!equal(point[0], b[0]))
  88. {
  89. twistpoint_fp2_neg(inverseB[0], b[0]);
  90. twistpoint_fp2_add_vartime(retval[0], point[0], inverseB[0]);
  91. }
  92. if (!equal(point[1], b[1]))
  93. {
  94. twistpoint_fp2_neg(inverseB[1], b[1]);
  95. twistpoint_fp2_add_vartime(retval[1], point[1], inverseB[1]);
  96. }
  97. return retval;
  98. }
  99. CurveBipoint CurveBipoint::operator*(const Scalar& exp) const
  100. {
  101. CurveBipoint retval;
  102. exp.mult(retval[0], point[0]);
  103. exp.mult(retval[1], point[1]);
  104. return retval;
  105. }
  106. TwistBipoint TwistBipoint::operator*(const Scalar& exp) const
  107. {
  108. TwistBipoint retval;
  109. exp.mult(retval[0], point[0]);
  110. exp.mult(retval[1], point[1]);
  111. return retval;
  112. }
  113. bool CurveBipoint::equal(const curvepoint_fp_t& op1, const curvepoint_fp_t& op2) const
  114. {
  115. bool retval;
  116. curvepoint_fp_t affine_op1, affine_op2;
  117. curvepoint_fp_set(affine_op1, op1);
  118. curvepoint_fp_set(affine_op2, op2);
  119. if (!(fpe_isone(affine_op1->m_z) || fpe_iszero(affine_op1->m_z)))
  120. curvepoint_fp_makeaffine(affine_op1);
  121. if (!(fpe_isone(affine_op2->m_z) || fpe_iszero(affine_op2->m_z)))
  122. curvepoint_fp_makeaffine(affine_op2);
  123. retval = fpe_iseq(affine_op1->m_x, affine_op2->m_x);
  124. retval = retval && fpe_iseq(affine_op1->m_y, affine_op2->m_y);
  125. retval = retval || (fpe_iszero(affine_op1->m_z) && fpe_iszero(affine_op2->m_z));
  126. return retval;
  127. }
  128. bool TwistBipoint::equal(const twistpoint_fp2_t& op1, const twistpoint_fp2_t& op2) const
  129. {
  130. bool retval;
  131. twistpoint_fp2_t affine_op1, affine_op2;
  132. twistpoint_fp2_set(affine_op1, op1);
  133. twistpoint_fp2_set(affine_op2, op2);
  134. if (!(fp2e_isone(affine_op1->m_z) || fp2e_iszero(affine_op1->m_z)))
  135. twistpoint_fp2_makeaffine(affine_op1);
  136. if (!(fp2e_isone(affine_op2->m_z) || fp2e_iszero(affine_op2->m_z)))
  137. twistpoint_fp2_makeaffine(affine_op2);
  138. retval = fp2e_iseq(affine_op1->m_x, affine_op2->m_x);
  139. retval = retval && fp2e_iseq(affine_op1->m_y, affine_op2->m_y);
  140. retval = retval || (fp2e_iszero(affine_op1->m_z) && fp2e_iszero(affine_op2->m_z));
  141. return retval;
  142. }
  143. bool CurveBipoint::operator==(const CurveBipoint& b) const
  144. {
  145. return equal(point[0], b[0]) && equal(point[1], b[1]);
  146. }
  147. bool TwistBipoint::operator==(const TwistBipoint& b) const
  148. {
  149. return equal(point[0], b[0]) && equal(point[1], b[1]);
  150. }
  151. bool CurveBipoint::operator!=(const CurveBipoint& b) const
  152. {
  153. return !(*this == b);
  154. }
  155. bool TwistBipoint::operator!=(const TwistBipoint& b) const
  156. {
  157. return !(*this == b);
  158. }
  159. void CurveBipoint::make_affine()
  160. {
  161. if (!(fpe_isone(point[0]->m_z) || fpe_iszero(point[0]->m_z)))
  162. curvepoint_fp_makeaffine(point[0]);
  163. if (!(fpe_isone(point[1]->m_z) || fpe_iszero(point[1]->m_z)))
  164. curvepoint_fp_makeaffine(point[1]);
  165. }
  166. void TwistBipoint::make_affine()
  167. {
  168. if (!(fp2e_isone(point[0]->m_z) || fp2e_iszero(point[0]->m_z)))
  169. twistpoint_fp2_makeaffine(point[0]);
  170. if (!(fp2e_isone(point[1]->m_z) || fp2e_iszero(point[1]->m_z)))
  171. twistpoint_fp2_makeaffine(point[1]);
  172. }
  173. std::ostream& operator<<(std::ostream& os, const CurveBipoint& output)
  174. {
  175. CurveBipoint affine_out = output;
  176. affine_out.make_affine();
  177. for (int i = 0; i < 2; i++)
  178. {
  179. if (fpe_iszero(affine_out[i]->m_z))
  180. os << "Infinity";
  181. else
  182. os << Fpe(affine_out[i]->m_x) << Fpe(affine_out[i]->m_y) << Fpe(affine_out[i]->m_z);
  183. }
  184. return os;
  185. }
  186. std::istream& operator>>(std::istream& is, CurveBipoint& input)
  187. {
  188. Fpe x, y, z;
  189. for (int i = 0; i < 2; i++)
  190. {
  191. is >> x >> y >> z;
  192. // std::cout << "x: " << std::hex << x << std::dec << std::endl;
  193. // std::cout << "y: " << std::hex << y << std::dec << std::endl;
  194. // std::cout << "z: " << std::hex << z << std::dec << std::endl;
  195. fpe_set(input[i]->m_x, x.data);
  196. fpe_set(input[i]->m_y, y.data);
  197. fpe_set(input[i]->m_z, z.data);
  198. }
  199. return is;
  200. }
  201. std::ostream& operator<<(std::ostream& os, const TwistBipoint& output)
  202. {
  203. TwistBipoint affine_out = output;
  204. affine_out.make_affine();
  205. for (int i = 0; i < 2; i++)
  206. {
  207. if (fp2e_iszero(affine_out[i]->m_z))
  208. os << "Infinity";
  209. else
  210. os << Fp2e(affine_out[i]->m_x) << Fp2e(affine_out[i]->m_y) << Fp2e(affine_out[i]->m_z);
  211. }
  212. return os;
  213. }
  214. std::istream& operator>>(std::istream& is, TwistBipoint& input)
  215. {
  216. Fp2e x, y, z;
  217. for (int i = 0; i < 2; i++)
  218. {
  219. is >> x >> y >> z;
  220. fp2e_set(input[i]->m_x, x.data);
  221. fp2e_set(input[i]->m_y, y.data);
  222. fp2e_set(input[i]->m_z, z.data);
  223. }
  224. return is;
  225. }
  226. size_t CurveBipointHash::operator()(const CurveBipoint& x) const
  227. {
  228. size_t retval[2];
  229. bool infinity[2];
  230. CurveBipoint affine_x = x;
  231. std::hash<double> hasher;
  232. if (fpe_iszero(affine_x[0]->m_z))
  233. {
  234. retval[0] = 0;
  235. infinity[0] = true;
  236. }
  237. else
  238. {
  239. retval[0] = 0;
  240. infinity[0] = false;
  241. }
  242. if (fpe_iszero(affine_x[1]->m_z))
  243. {
  244. retval[1] = 0;
  245. infinity[1] = true;
  246. }
  247. else
  248. {
  249. retval[1] = 0;
  250. infinity[1] = false;
  251. }
  252. affine_x.make_affine();
  253. for (int i = 0; i < 2; i++)
  254. {
  255. for (int j = 0; j < 12 && !infinity[i]; j++)
  256. {
  257. retval[i] ^= hasher(affine_x[i]->m_x->v[j]);
  258. retval[i] ^= hasher(affine_x[i]->m_y->v[j]);
  259. }
  260. }
  261. return retval[0] ^ retval[1];
  262. }
  263. size_t TwistBipointHash::operator()(const TwistBipoint& x) const
  264. {
  265. size_t retval[2];
  266. bool infinity[2];
  267. TwistBipoint affine_x = x;
  268. std::hash<double> hasher;
  269. if (fp2e_iszero(affine_x[0]->m_z))
  270. {
  271. retval[0] = 0;
  272. infinity[0] = true;
  273. }
  274. else
  275. {
  276. retval[0] = 0;
  277. infinity[0] = false;
  278. }
  279. if (fp2e_iszero(affine_x[1]->m_z))
  280. {
  281. retval[1] = 0;
  282. infinity[1] = true;
  283. }
  284. else
  285. {
  286. retval[1] = 0;
  287. infinity[1] = false;
  288. }
  289. affine_x.make_affine();
  290. for (int i = 0; i < 2; i++)
  291. {
  292. for (int j = 0; j < 24 && !infinity[i]; j++)
  293. {
  294. retval[i] ^= hasher(affine_x[i]->m_x->v[j]);
  295. retval[i] ^= hasher(affine_x[i]->m_y->v[j]);
  296. }
  297. }
  298. return retval[0] ^ retval[1];
  299. }