Curvepoint.cpp 9.1 KB

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