You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

504 lines
9.5 KiB

  1. /**
  2. * @file ec_point.h
  3. * @copyright
  4. * Copyright (c) 2014 Cryptography Research, Inc. \n
  5. * Released under the MIT License. See LICENSE.txt for license information.
  6. * @author Mike Hamburg
  7. * @warning This file was automatically generated.
  8. */
  9. #ifndef __CC_INCLUDED_EC_POINT_H__
  10. #define __CC_INCLUDED_EC_POINT_H__
  11. #include "p448.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /**
  16. * Affine point on an Edwards curve.
  17. */
  18. struct affine_t {
  19. struct p448_t x, y;
  20. };
  21. /**
  22. * Affine point on a twisted Edwards curve.
  23. */
  24. struct tw_affine_t {
  25. struct p448_t x, y;
  26. };
  27. /**
  28. * Montgomery buffer.
  29. */
  30. struct montgomery_t {
  31. struct p448_t z0, xd, zd, xa, za;
  32. };
  33. /**
  34. * Extensible coordinates for Edwards curves, suitable for
  35. * accumulators.
  36. *
  37. * Represents the point (x/z, y/z). The extra coordinates
  38. * t,u satisfy xy = tuz, allowing for conversion to Extended
  39. * form by multiplying t and u.
  40. *
  41. * The idea is that you don't have to do this multiplication
  42. * when doubling the accumulator, because the t-coordinate
  43. * isn't used there. At the same time, as long as you only
  44. * have one point in extensible form, additions don't cost
  45. * extra.
  46. *
  47. * This is essentially a lazier version of Hisil et al's
  48. * lookahead trick. It might be worth considering that trick
  49. * instead.
  50. */
  51. struct extensible_t {
  52. struct p448_t x, y, z, t, u;
  53. };
  54. /**
  55. * Extensible coordinates for twisted Edwards curves,
  56. * suitable for accumulators.
  57. */
  58. struct tw_extensible_t {
  59. struct p448_t x, y, z, t, u;
  60. };
  61. /**
  62. * Niels coordinates for twisted Edwards curves.
  63. *
  64. * Good for mixed readdition; suitable for fixed tables.
  65. */
  66. struct tw_niels_t {
  67. struct p448_t a, b, c;
  68. };
  69. /**
  70. * Projective niels coordinates for twisted Edwards curves.
  71. *
  72. * Good for readdition; suitable for temporary tables.
  73. */
  74. struct tw_pniels_t {
  75. struct tw_niels_t n;
  76. struct p448_t z;
  77. };
  78. /**
  79. * Auto-generated copy method.
  80. */
  81. static __inline__ void
  82. copy_affine (
  83. struct affine_t* a,
  84. const struct affine_t* ds
  85. ) __attribute__((unused,always_inline));
  86. /**
  87. * Auto-generated copy method.
  88. */
  89. static __inline__ void
  90. copy_tw_affine (
  91. struct tw_affine_t* a,
  92. const struct tw_affine_t* ds
  93. ) __attribute__((unused,always_inline));
  94. /**
  95. * Auto-generated copy method.
  96. */
  97. static __inline__ void
  98. copy_montgomery (
  99. struct montgomery_t* a,
  100. const struct montgomery_t* ds
  101. ) __attribute__((unused,always_inline));
  102. /**
  103. * Auto-generated copy method.
  104. */
  105. static __inline__ void
  106. copy_extensible (
  107. struct extensible_t* a,
  108. const struct extensible_t* ds
  109. ) __attribute__((unused,always_inline));
  110. /**
  111. * Auto-generated copy method.
  112. */
  113. static __inline__ void
  114. copy_tw_extensible (
  115. struct tw_extensible_t* a,
  116. const struct tw_extensible_t* ds
  117. ) __attribute__((unused,always_inline));
  118. /**
  119. * Auto-generated copy method.
  120. */
  121. static __inline__ void
  122. copy_tw_niels (
  123. struct tw_niels_t* a,
  124. const struct tw_niels_t* ds
  125. ) __attribute__((unused,always_inline));
  126. /**
  127. * Auto-generated copy method.
  128. */
  129. static __inline__ void
  130. copy_tw_pniels (
  131. struct tw_pniels_t* a,
  132. const struct tw_pniels_t* ds
  133. ) __attribute__((unused,always_inline));
  134. /**
  135. * Returns 1/sqrt(+- x).
  136. *
  137. * The Legendre symbol of the result is the same as that of the
  138. * input.
  139. *
  140. * If x=0, returns 0.
  141. */
  142. void
  143. p448_isr (
  144. struct p448_t* a,
  145. const struct p448_t* x
  146. );
  147. /**
  148. * Returns 1/x.
  149. *
  150. * If x=0, returns 0.
  151. */
  152. void
  153. p448_inverse (
  154. struct p448_t* a,
  155. const struct p448_t* x
  156. );
  157. /**
  158. * Add two points on a twisted Edwards curve, one in Extensible form
  159. * and the other in half-Niels form.
  160. */
  161. void
  162. add_tw_niels_to_tw_extensible (
  163. struct tw_extensible_t* d,
  164. const struct tw_niels_t* e
  165. );
  166. /**
  167. * Add two points on a twisted Edwards curve, one in Extensible form
  168. * and the other in half-Niels form.
  169. */
  170. void
  171. sub_tw_niels_from_tw_extensible (
  172. struct tw_extensible_t* d,
  173. const struct tw_niels_t* e
  174. );
  175. /**
  176. * Add two points on a twisted Edwards curve, one in Extensible form
  177. * and the other in projective Niels form.
  178. */
  179. void
  180. add_tw_pniels_to_tw_extensible (
  181. struct tw_extensible_t* e,
  182. const struct tw_pniels_t* a
  183. );
  184. /**
  185. * Add two points on a twisted Edwards curve, one in Extensible form
  186. * and the other in projective Niels form.
  187. */
  188. void
  189. sub_tw_pniels_from_tw_extensible (
  190. struct tw_extensible_t* e,
  191. const struct tw_pniels_t* a
  192. );
  193. /**
  194. * Double a point on a twisted Edwards curve, in "extensible" coordinates.
  195. */
  196. void
  197. double_tw_extensible (
  198. struct tw_extensible_t* a
  199. );
  200. /**
  201. * Double a point on an Edwards curve, in "extensible" coordinates.
  202. */
  203. void
  204. double_extensible (
  205. struct extensible_t* a
  206. );
  207. /**
  208. * Double a point, and transfer it to the twisted curve.
  209. *
  210. * That is, apply the 4-isogeny.
  211. */
  212. void
  213. twist_and_double (
  214. struct tw_extensible_t* b,
  215. const struct extensible_t* a
  216. );
  217. /**
  218. * Double a point, and transfer it to the untwisted curve.
  219. *
  220. * That is, apply the dual isogeny.
  221. */
  222. void
  223. untwist_and_double (
  224. struct extensible_t* b,
  225. const struct tw_extensible_t* a
  226. );
  227. void
  228. convert_tw_affine_to_tw_pniels (
  229. struct tw_pniels_t* b,
  230. const struct tw_affine_t* a
  231. );
  232. void
  233. convert_tw_affine_to_tw_extensible (
  234. struct tw_extensible_t* b,
  235. const struct tw_affine_t* a
  236. );
  237. void
  238. convert_affine_to_extensible (
  239. struct extensible_t* b,
  240. const struct affine_t* a
  241. );
  242. void
  243. convert_tw_extensible_to_tw_pniels (
  244. struct tw_pniels_t* b,
  245. const struct tw_extensible_t* a
  246. );
  247. void
  248. convert_tw_pniels_to_tw_extensible (
  249. struct tw_extensible_t* e,
  250. const struct tw_pniels_t* d
  251. );
  252. void
  253. convert_tw_niels_to_tw_extensible (
  254. struct tw_extensible_t* e,
  255. const struct tw_niels_t* d
  256. );
  257. void
  258. montgomery_step (
  259. struct montgomery_t* a
  260. );
  261. void
  262. serialize_montgomery (
  263. struct p448_t* sign,
  264. struct p448_t* ser,
  265. const struct montgomery_t* a,
  266. const struct p448_t* sbz
  267. );
  268. /**
  269. * Serialize a point on an Edwards curve.
  270. *
  271. * The serialized form would be sqrt((z-y)/(z+y)) with sign of xz.
  272. *
  273. * It would be on 4y^2/(1-d) = x^3 + 2(1+d)/(1-d) * x^2 + x.
  274. *
  275. * But 4/(1-d) isn't square, so we need to twist it:
  276. *
  277. * -x is on 4y^2/(d-1) = x^3 + 2(d+1)/(d-1) * x^2 + x
  278. */
  279. void
  280. serialize_extensible (
  281. struct p448_t* b,
  282. const struct extensible_t* a
  283. );
  284. /**
  285. *
  286. */
  287. void
  288. untwist_and_double_and_serialize (
  289. struct p448_t* b,
  290. const struct tw_extensible_t* a
  291. );
  292. /**
  293. * Expensive transfer from untwisted to twisted. Roughly equivalent to halve and isogeny.
  294. * Correctly transfers point of order 2.
  295. *
  296. * Can't have x=+1 (it's not even). There is code to fix the exception that would otherwise
  297. * occur at (0,1).
  298. *
  299. * Input point must be even.
  300. */
  301. void
  302. twist (
  303. struct tw_extensible_t* b,
  304. const struct extensible_t* a
  305. );
  306. /**
  307. * Deserialize a point to an untwisted affine curve.
  308. */
  309. mask_t
  310. deserialize_affine (
  311. struct affine_t* a,
  312. const struct p448_t* sz
  313. );
  314. /**
  315. * Deserialize a point and transfer it to the twist.
  316. *
  317. * Not guaranteed to preserve the 4-torsion component.
  318. *
  319. * Refuses to deserialize +-1, which are the points of order 2.
  320. */
  321. mask_t
  322. deserialize_and_twist_approx (
  323. struct tw_extensible_t* a,
  324. const struct p448_t* sdm1,
  325. const struct p448_t* sz
  326. );
  327. void
  328. set_identity_extensible (
  329. struct extensible_t* a
  330. );
  331. void
  332. set_identity_tw_extensible (
  333. struct tw_extensible_t* a
  334. );
  335. void
  336. set_identity_affine (
  337. struct affine_t* a
  338. );
  339. mask_t
  340. eq_affine (
  341. const struct affine_t* a,
  342. const struct affine_t* b
  343. );
  344. mask_t
  345. eq_extensible (
  346. const struct extensible_t* a,
  347. const struct extensible_t* b
  348. );
  349. mask_t
  350. eq_tw_extensible (
  351. const struct tw_extensible_t* a,
  352. const struct tw_extensible_t* b
  353. );
  354. void
  355. elligator_2s_inject (
  356. struct affine_t* a,
  357. const struct p448_t* r
  358. );
  359. mask_t
  360. validate_affine (
  361. const struct affine_t* a
  362. );
  363. /**
  364. * Check the invariants for struct tw_extensible_t.
  365. * PERF: This function was automatically generated
  366. * with no regard for speed.
  367. */
  368. mask_t
  369. validate_tw_extensible (
  370. const struct tw_extensible_t* ext
  371. );
  372. void
  373. copy_affine (
  374. struct affine_t* a,
  375. const struct affine_t* ds
  376. ) {
  377. p448_copy ( &a->x, &ds->x );
  378. p448_copy ( &a->y, &ds->y );
  379. }
  380. void
  381. copy_tw_affine (
  382. struct tw_affine_t* a,
  383. const struct tw_affine_t* ds
  384. ) {
  385. p448_copy ( &a->x, &ds->x );
  386. p448_copy ( &a->y, &ds->y );
  387. }
  388. void
  389. copy_montgomery (
  390. struct montgomery_t* a,
  391. const struct montgomery_t* ds
  392. ) {
  393. p448_copy ( &a->z0, &ds->z0 );
  394. p448_copy ( &a->xd, &ds->xd );
  395. p448_copy ( &a->zd, &ds->zd );
  396. p448_copy ( &a->xa, &ds->xa );
  397. p448_copy ( &a->za, &ds->za );
  398. }
  399. void
  400. copy_extensible (
  401. struct extensible_t* a,
  402. const struct extensible_t* ds
  403. ) {
  404. p448_copy ( &a->x, &ds->x );
  405. p448_copy ( &a->y, &ds->y );
  406. p448_copy ( &a->z, &ds->z );
  407. p448_copy ( &a->t, &ds->t );
  408. p448_copy ( &a->u, &ds->u );
  409. }
  410. void
  411. copy_tw_extensible (
  412. struct tw_extensible_t* a,
  413. const struct tw_extensible_t* ds
  414. ) {
  415. p448_copy ( &a->x, &ds->x );
  416. p448_copy ( &a->y, &ds->y );
  417. p448_copy ( &a->z, &ds->z );
  418. p448_copy ( &a->t, &ds->t );
  419. p448_copy ( &a->u, &ds->u );
  420. }
  421. void
  422. copy_tw_niels (
  423. struct tw_niels_t* a,
  424. const struct tw_niels_t* ds
  425. ) {
  426. p448_copy ( &a->a, &ds->a );
  427. p448_copy ( &a->b, &ds->b );
  428. p448_copy ( &a->c, &ds->c );
  429. }
  430. void
  431. copy_tw_pniels (
  432. struct tw_pniels_t* a,
  433. const struct tw_pniels_t* ds
  434. ) {
  435. copy_tw_niels( &a->n, &ds->n );
  436. p448_copy ( &a->z, &ds->z );
  437. }
  438. #ifdef __cplusplus
  439. }; /* extern "C" */
  440. #endif
  441. #endif /* __CC_INCLUDED_EC_POINT_H__ */