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.
 
 
 
 
 

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