Bipoint.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 affine_op1(op1), affine_op2(op2);
  117. affine_op1.make_affine();
  118. affine_op2.make_affine();
  119. retval = fpe_iseq(affine_op1.point->m_x, affine_op2.point->m_x);
  120. retval = retval && fpe_iseq(affine_op1.point->m_y, affine_op2.point->m_y);
  121. retval = retval || (fpe_iszero(affine_op1.point->m_z) && fpe_iszero(affine_op2.point->m_z));
  122. return retval;
  123. }
  124. bool TwistBipoint::equal(const twistpoint_fp2_t& op1, const twistpoint_fp2_t& op2) const
  125. {
  126. bool retval;
  127. Twistpoint affine_op1(op1), affine_op2(op2);
  128. affine_op1.make_affine();
  129. affine_op2.make_affine();
  130. retval = fp2e_iseq(affine_op1.point->m_x, affine_op2.point->m_x);
  131. retval = retval && fp2e_iseq(affine_op1.point->m_y, affine_op2.point->m_y);
  132. retval = retval || (fp2e_iszero(affine_op1.point->m_z) && fp2e_iszero(affine_op2.point->m_z));
  133. return retval;
  134. }
  135. bool CurveBipoint::operator==(const CurveBipoint& b) const
  136. {
  137. return equal(point[0], b[0]) && equal(point[1], b[1]);
  138. }
  139. bool TwistBipoint::operator==(const TwistBipoint& b) const
  140. {
  141. return equal(point[0], b[0]) && equal(point[1], b[1]);
  142. }
  143. bool CurveBipoint::operator!=(const CurveBipoint& b) const
  144. {
  145. return !(*this == b);
  146. }
  147. bool TwistBipoint::operator!=(const TwistBipoint& b) const
  148. {
  149. return !(*this == b);
  150. }
  151. void CurveBipoint::make_affine()
  152. {
  153. if (!(fpe_isone(point[0]->m_z) || fpe_iszero(point[0]->m_z)))
  154. curvepoint_fp_makeaffine(point[0]);
  155. if (!(fpe_isone(point[1]->m_z) || fpe_iszero(point[1]->m_z)))
  156. curvepoint_fp_makeaffine(point[1]);
  157. fpe_short_coeffred(point[0]->m_x);
  158. fpe_short_coeffred(point[0]->m_y);
  159. fpe_short_coeffred(point[0]->m_z);
  160. fpe_short_coeffred(point[1]->m_x);
  161. fpe_short_coeffred(point[1]->m_y);
  162. fpe_short_coeffred(point[1]->m_z);
  163. }
  164. void TwistBipoint::make_affine()
  165. {
  166. if (!(fp2e_isone(point[0]->m_z) || fp2e_iszero(point[0]->m_z)))
  167. twistpoint_fp2_makeaffine(point[0]);
  168. if (!(fp2e_isone(point[1]->m_z) || fp2e_iszero(point[1]->m_z)))
  169. twistpoint_fp2_makeaffine(point[1]);
  170. fp2e_short_coeffred(point[0]->m_x);
  171. fp2e_short_coeffred(point[0]->m_y);
  172. fp2e_short_coeffred(point[0]->m_z);
  173. fp2e_short_coeffred(point[1]->m_x);
  174. fp2e_short_coeffred(point[1]->m_y);
  175. fp2e_short_coeffred(point[1]->m_z);
  176. }
  177. std::ostream& operator<<(std::ostream& os, const CurveBipoint& output)
  178. {
  179. CurveBipoint affine_out = output;
  180. affine_out.make_affine();
  181. for (int i = 0; i < 2; i++)
  182. {
  183. if ((os.flags() & std::ios::hex) && fpe_iszero(affine_out[i]->m_z))
  184. os << "Infinity";
  185. else
  186. os << Fpe(affine_out[i]->m_x) << Fpe(affine_out[i]->m_y) << Fpe(affine_out[i]->m_z);
  187. }
  188. return os;
  189. }
  190. std::istream& operator>>(std::istream& is, CurveBipoint& input)
  191. {
  192. Fpe x, y, z;
  193. for (int i = 0; i < 2; i++)
  194. {
  195. is >> x >> y >> z;
  196. fpe_set(input[i]->m_x, x.data);
  197. fpe_set(input[i]->m_y, y.data);
  198. fpe_set(input[i]->m_z, z.data);
  199. }
  200. return is;
  201. }
  202. std::ostream& operator<<(std::ostream& os, const TwistBipoint& output)
  203. {
  204. TwistBipoint affine_out = output;
  205. affine_out.make_affine();
  206. for (int i = 0; i < 2; i++)
  207. {
  208. if ((os.flags() & std::ios::hex) && fp2e_iszero(affine_out[i]->m_z))
  209. os << "Infinity";
  210. else
  211. os << Fp2e(affine_out[i]->m_x) << Fp2e(affine_out[i]->m_y) << Fp2e(affine_out[i]->m_z);
  212. }
  213. return os;
  214. }
  215. std::istream& operator>>(std::istream& is, TwistBipoint& input)
  216. {
  217. Fp2e x, y, z;
  218. for (int i = 0; i < 2; i++)
  219. {
  220. is >> x >> y >> z;
  221. fp2e_set(input[i]->m_x, x.data);
  222. fp2e_set(input[i]->m_y, y.data);
  223. fp2e_set(input[i]->m_z, z.data);
  224. }
  225. return is;
  226. }
  227. size_t CurveBipointHash::operator()(const CurveBipoint& x) const
  228. {
  229. size_t retval[2];
  230. bool infinity[2];
  231. CurveBipoint affine_x = x;
  232. std::hash<double> hasher;
  233. if (fpe_iszero(affine_x[0]->m_z))
  234. {
  235. retval[0] = 0;
  236. infinity[0] = true;
  237. }
  238. else
  239. {
  240. retval[0] = 0;
  241. infinity[0] = false;
  242. }
  243. if (fpe_iszero(affine_x[1]->m_z))
  244. {
  245. retval[1] = 0;
  246. infinity[1] = true;
  247. }
  248. else
  249. {
  250. retval[1] = 0;
  251. infinity[1] = false;
  252. }
  253. affine_x.make_affine();
  254. for (int i = 0; i < 2; i++)
  255. {
  256. for (int j = 0; j < 12 && !infinity[i]; j++)
  257. {
  258. retval[i] ^= hasher(affine_x[i]->m_x->v[j]);
  259. retval[i] ^= hasher(affine_x[i]->m_y->v[j]);
  260. }
  261. }
  262. return retval[0] ^ retval[1];
  263. }
  264. size_t TwistBipointHash::operator()(const TwistBipoint& x) const
  265. {
  266. size_t retval[2];
  267. bool infinity[2];
  268. TwistBipoint affine_x = x;
  269. std::hash<double> hasher;
  270. if (fp2e_iszero(affine_x[0]->m_z))
  271. {
  272. retval[0] = 0;
  273. infinity[0] = true;
  274. }
  275. else
  276. {
  277. retval[0] = 0;
  278. infinity[0] = false;
  279. }
  280. if (fp2e_iszero(affine_x[1]->m_z))
  281. {
  282. retval[1] = 0;
  283. infinity[1] = true;
  284. }
  285. else
  286. {
  287. retval[1] = 0;
  288. infinity[1] = false;
  289. }
  290. affine_x.make_affine();
  291. for (int i = 0; i < 2; i++)
  292. {
  293. for (int j = 0; j < 24 && !infinity[i]; j++)
  294. {
  295. retval[i] ^= hasher(affine_x[i]->m_x->v[j]);
  296. retval[i] ^= hasher(affine_x[i]->m_y->v[j]);
  297. }
  298. }
  299. return retval[0] ^ retval[1];
  300. }