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.
 
 
 
 
 

746 lines
21 KiB

  1. /**
  2. * @cond internal
  3. * @file ec_point.c
  4. * @copyright
  5. * Copyright (c) 2014 Cryptography Research, Inc. \n
  6. * Released under the MIT License. See LICENSE.txt for license information.
  7. * @author Mike Hamburg
  8. * @warning This file was automatically generated.
  9. */
  10. #include "ec_point.h"
  11. void
  12. p448_isr (
  13. struct p448_t* a,
  14. const struct p448_t* x
  15. ) {
  16. struct p448_t L0, L1, L2;
  17. p448_sqr ( &L1, x );
  18. p448_mul ( &L2, x, &L1 );
  19. p448_sqr ( &L1, &L2 );
  20. p448_mul ( &L2, x, &L1 );
  21. p448_sqrn ( &L1, &L2, 3 );
  22. p448_mul ( &L0, &L2, &L1 );
  23. p448_sqrn ( &L1, &L0, 3 );
  24. p448_mul ( &L0, &L2, &L1 );
  25. p448_sqrn ( &L2, &L0, 9 );
  26. p448_mul ( &L1, &L0, &L2 );
  27. p448_sqr ( &L0, &L1 );
  28. p448_mul ( &L2, x, &L0 );
  29. p448_sqrn ( &L0, &L2, 18 );
  30. p448_mul ( &L2, &L1, &L0 );
  31. p448_sqrn ( &L0, &L2, 37 );
  32. p448_mul ( &L1, &L2, &L0 );
  33. p448_sqrn ( &L0, &L1, 37 );
  34. p448_mul ( &L1, &L2, &L0 );
  35. p448_sqrn ( &L0, &L1, 111 );
  36. p448_mul ( &L2, &L1, &L0 );
  37. p448_sqr ( &L0, &L2 );
  38. p448_mul ( &L1, x, &L0 );
  39. p448_sqrn ( &L0, &L1, 223 );
  40. p448_mul ( a, &L2, &L0 );
  41. }
  42. void
  43. p448_inverse (
  44. struct p448_t* a,
  45. const struct p448_t* x
  46. ) {
  47. struct p448_t L0, L1;
  48. p448_isr ( &L0, x );
  49. p448_sqr ( &L1, &L0 );
  50. p448_sqr ( &L0, &L1 );
  51. p448_mul ( a, x, &L0 );
  52. }
  53. void
  54. add_tw_niels_to_tw_extensible (
  55. struct tw_extensible_t* d,
  56. const struct tw_niels_t* e
  57. ) {
  58. struct p448_t L0, L1;
  59. p448_bias ( &d->y, 2 );
  60. p448_bias ( &d->z, 2 );
  61. p448_sub ( &L1, &d->y, &d->x );
  62. p448_mul ( &L0, &e->a, &L1 );
  63. p448_add ( &L1, &d->x, &d->y );
  64. p448_mul ( &d->y, &e->b, &L1 );
  65. p448_bias ( &d->y, 2 );
  66. p448_mul ( &L1, &d->u, &d->t );
  67. p448_mul ( &d->x, &e->c, &L1 );
  68. p448_add ( &d->u, &L0, &d->y );
  69. p448_sub ( &d->t, &d->y, &L0 );
  70. p448_sub ( &d->y, &d->z, &d->x );
  71. p448_add ( &L0, &d->x, &d->z );
  72. p448_mul ( &d->z, &L0, &d->y );
  73. p448_mul ( &d->x, &d->y, &d->t );
  74. p448_mul ( &d->y, &L0, &d->u );
  75. }
  76. void
  77. sub_tw_niels_from_tw_extensible (
  78. struct tw_extensible_t* d,
  79. const struct tw_niels_t* e
  80. ) {
  81. struct p448_t L0, L1;
  82. p448_bias ( &d->y, 2 );
  83. p448_bias ( &d->z, 2 );
  84. p448_sub ( &L1, &d->y, &d->x );
  85. p448_mul ( &L0, &e->b, &L1 );
  86. p448_add ( &L1, &d->x, &d->y );
  87. p448_mul ( &d->y, &e->a, &L1 );
  88. p448_bias ( &d->y, 2 );
  89. p448_mul ( &L1, &d->u, &d->t );
  90. p448_mul ( &d->x, &e->c, &L1 );
  91. p448_add ( &d->u, &L0, &d->y );
  92. p448_sub ( &d->t, &d->y, &L0 );
  93. p448_add ( &d->y, &d->x, &d->z );
  94. p448_sub ( &L0, &d->z, &d->x );
  95. p448_mul ( &d->z, &L0, &d->y );
  96. p448_mul ( &d->x, &d->y, &d->t );
  97. p448_mul ( &d->y, &L0, &d->u );
  98. }
  99. void
  100. add_tw_pniels_to_tw_extensible (
  101. struct tw_extensible_t* e,
  102. const struct tw_pniels_t* a
  103. ) {
  104. struct p448_t L0;
  105. p448_mul ( &L0, &e->z, &a->z );
  106. p448_copy ( &e->z, &L0 );
  107. add_tw_niels_to_tw_extensible( e, &a->n );
  108. }
  109. void
  110. sub_tw_pniels_from_tw_extensible (
  111. struct tw_extensible_t* e,
  112. const struct tw_pniels_t* a
  113. ) {
  114. struct p448_t L0;
  115. p448_mul ( &L0, &e->z, &a->z );
  116. p448_copy ( &e->z, &L0 );
  117. sub_tw_niels_from_tw_extensible( e, &a->n );
  118. }
  119. void
  120. double_tw_extensible (
  121. struct tw_extensible_t* a
  122. ) {
  123. struct p448_t L0, L1, L2;
  124. p448_sqr ( &L2, &a->x );
  125. p448_sqr ( &L0, &a->y );
  126. p448_add ( &a->u, &L2, &L0 );
  127. p448_add ( &a->t, &a->y, &a->x );
  128. p448_sqr ( &L1, &a->t );
  129. p448_bias ( &L1, 3 );
  130. p448_sub ( &a->t, &L1, &a->u );
  131. p448_sub ( &L1, &L0, &L2 );
  132. p448_bias ( &L1, 2 );
  133. p448_sqr ( &a->x, &a->z );
  134. p448_bias ( &a->x, 2 );
  135. p448_add ( &a->z, &a->x, &a->x );
  136. p448_sub ( &L0, &a->z, &L1 );
  137. p448_mul ( &a->z, &L1, &L0 );
  138. p448_mul ( &a->x, &L0, &a->t );
  139. p448_mul ( &a->y, &L1, &a->u );
  140. }
  141. void
  142. double_extensible (
  143. struct extensible_t* a
  144. ) {
  145. struct p448_t L0, L1, L2;
  146. p448_sqr ( &L2, &a->x );
  147. p448_sqr ( &L0, &a->y );
  148. p448_add ( &L1, &L2, &L0 );
  149. p448_add ( &a->t, &a->y, &a->x );
  150. p448_sqr ( &a->u, &a->t );
  151. p448_bias ( &a->u, 3 );
  152. p448_sub ( &a->t, &a->u, &L1 );
  153. p448_sub ( &a->u, &L0, &L2 );
  154. p448_bias ( &a->u, 2 );
  155. p448_sqr ( &a->x, &a->z );
  156. p448_bias ( &a->x, 2 );
  157. p448_add ( &a->z, &a->x, &a->x );
  158. p448_sub ( &L0, &a->z, &L1 );
  159. p448_mul ( &a->z, &L1, &L0 );
  160. p448_mul ( &a->x, &L0, &a->t );
  161. p448_mul ( &a->y, &L1, &a->u );
  162. }
  163. void
  164. twist_and_double (
  165. struct tw_extensible_t* b,
  166. const struct extensible_t* a
  167. ) {
  168. struct p448_t L0;
  169. p448_sqr ( &b->x, &a->x );
  170. p448_sqr ( &b->z, &a->y );
  171. p448_add ( &b->u, &b->x, &b->z );
  172. p448_add ( &b->t, &a->y, &a->x );
  173. p448_sqr ( &L0, &b->t );
  174. p448_bias ( &L0, 3 );
  175. p448_sub ( &b->t, &L0, &b->u );
  176. p448_sub ( &L0, &b->z, &b->x );
  177. p448_bias ( &L0, 2 );
  178. p448_sqr ( &b->x, &a->z );
  179. p448_bias ( &b->x, 2 );
  180. p448_add ( &b->z, &b->x, &b->x );
  181. p448_sub ( &b->y, &b->z, &b->u );
  182. p448_mul ( &b->z, &L0, &b->y );
  183. p448_mul ( &b->x, &b->y, &b->t );
  184. p448_mul ( &b->y, &L0, &b->u );
  185. }
  186. void
  187. untwist_and_double (
  188. struct extensible_t* b,
  189. const struct tw_extensible_t* a
  190. ) {
  191. struct p448_t L0;
  192. p448_sqr ( &b->x, &a->x );
  193. p448_sqr ( &b->z, &a->y );
  194. p448_add ( &L0, &b->x, &b->z );
  195. p448_add ( &b->t, &a->y, &a->x );
  196. p448_sqr ( &b->u, &b->t );
  197. p448_bias ( &b->u, 3 );
  198. p448_sub ( &b->t, &b->u, &L0 );
  199. p448_sub ( &b->u, &b->z, &b->x );
  200. p448_bias ( &b->u, 2 );
  201. p448_sqr ( &b->x, &a->z );
  202. p448_bias ( &b->x, 2 );
  203. p448_add ( &b->z, &b->x, &b->x );
  204. p448_sub ( &b->y, &b->z, &b->u );
  205. p448_mul ( &b->z, &L0, &b->y );
  206. p448_mul ( &b->x, &b->y, &b->t );
  207. p448_mul ( &b->y, &L0, &b->u );
  208. }
  209. void
  210. convert_tw_affine_to_tw_pniels (
  211. struct tw_pniels_t* b,
  212. const struct tw_affine_t* a
  213. ) {
  214. p448_sub ( &b->n.a, &a->y, &a->x );
  215. p448_bias ( &b->n.a, 2 );
  216. p448_weak_reduce( &b->n.a );
  217. p448_add ( &b->n.b, &a->x, &a->y );
  218. p448_weak_reduce( &b->n.b );
  219. p448_mul ( &b->n.c, &a->y, &a->x );
  220. p448_mulw ( &b->z, &b->n.c, 78164 );
  221. p448_neg ( &b->n.c, &b->z );
  222. p448_bias ( &b->n.c, 2 );
  223. p448_weak_reduce( &b->n.c );
  224. p448_set_ui( &b->z, 2 );
  225. }
  226. void
  227. convert_tw_affine_to_tw_extensible (
  228. struct tw_extensible_t* b,
  229. const struct tw_affine_t* a
  230. ) {
  231. p448_copy ( &b->x, &a->x );
  232. p448_copy ( &b->y, &a->y );
  233. p448_set_ui( &b->z, 1 );
  234. p448_copy ( &b->t, &a->x );
  235. p448_copy ( &b->u, &a->y );
  236. }
  237. void
  238. convert_affine_to_extensible (
  239. struct extensible_t* b,
  240. const struct affine_t* a
  241. ) {
  242. p448_copy ( &b->x, &a->x );
  243. p448_copy ( &b->y, &a->y );
  244. p448_set_ui( &b->z, 1 );
  245. p448_copy ( &b->t, &a->x );
  246. p448_copy ( &b->u, &a->y );
  247. }
  248. void
  249. convert_tw_extensible_to_tw_pniels (
  250. struct tw_pniels_t* b,
  251. const struct tw_extensible_t* a
  252. ) {
  253. p448_sub ( &b->n.a, &a->y, &a->x );
  254. p448_bias ( &b->n.a, 2 );
  255. p448_weak_reduce( &b->n.a );
  256. p448_add ( &b->n.b, &a->x, &a->y );
  257. p448_weak_reduce( &b->n.b );
  258. p448_mul ( &b->n.c, &a->u, &a->t );
  259. p448_mulw ( &b->z, &b->n.c, 78164 );
  260. p448_neg ( &b->n.c, &b->z );
  261. p448_bias ( &b->n.c, 2 );
  262. p448_weak_reduce( &b->n.c );
  263. p448_add ( &b->z, &a->z, &a->z );
  264. p448_weak_reduce( &b->z );
  265. }
  266. void
  267. convert_tw_pniels_to_tw_extensible (
  268. struct tw_extensible_t* e,
  269. const struct tw_pniels_t* d
  270. ) {
  271. p448_add ( &e->u, &d->n.b, &d->n.a );
  272. p448_sub ( &e->t, &d->n.b, &d->n.a );
  273. p448_bias ( &e->t, 2 );
  274. p448_mul ( &e->x, &d->z, &e->t );
  275. p448_mul ( &e->y, &d->z, &e->u );
  276. p448_sqr ( &e->z, &d->z );
  277. }
  278. void
  279. convert_tw_niels_to_tw_extensible (
  280. struct tw_extensible_t* e,
  281. const struct tw_niels_t* d
  282. ) {
  283. p448_add ( &e->y, &d->b, &d->a );
  284. p448_weak_reduce( &e->y );
  285. p448_sub ( &e->x, &d->b, &d->a );
  286. p448_bias ( &e->x, 2 );
  287. p448_weak_reduce( &e->x );
  288. p448_set_ui( &e->z, 1 );
  289. p448_copy ( &e->t, &e->x );
  290. p448_copy ( &e->u, &e->y );
  291. }
  292. void
  293. montgomery_step (
  294. struct montgomery_t* a
  295. ) {
  296. struct p448_t L0, L1;
  297. p448_bias ( &a->xd, 2 );
  298. p448_bias ( &a->xa, 2 );
  299. p448_add ( &L0, &a->zd, &a->xd );
  300. p448_sub ( &L1, &a->xd, &a->zd );
  301. p448_sub ( &a->zd, &a->xa, &a->za );
  302. p448_mul ( &a->xd, &L0, &a->zd );
  303. p448_bias ( &a->xd, 2 );
  304. p448_add ( &a->zd, &a->za, &a->xa );
  305. p448_mul ( &a->za, &L1, &a->zd );
  306. p448_add ( &a->xa, &a->za, &a->xd );
  307. p448_sqr ( &a->zd, &a->xa );
  308. p448_mul ( &a->xa, &a->z0, &a->zd );
  309. p448_sub ( &a->zd, &a->xd, &a->za );
  310. p448_sqr ( &a->za, &a->zd );
  311. p448_sqr ( &a->xd, &L0 );
  312. p448_bias ( &a->xd, 2 );
  313. p448_sqr ( &L0, &L1 );
  314. p448_mulw ( &a->zd, &a->xd, 39082 );
  315. p448_bias ( &a->zd, 4 );
  316. p448_sub ( &L1, &a->xd, &L0 );
  317. p448_mul ( &a->xd, &L0, &a->zd );
  318. p448_sub ( &L0, &a->zd, &L1 );
  319. p448_mul ( &a->zd, &L0, &L1 );
  320. }
  321. void
  322. serialize_montgomery (
  323. struct p448_t* sign,
  324. struct p448_t* ser,
  325. const struct montgomery_t* a,
  326. const struct p448_t* sbz
  327. ) {
  328. struct p448_t L0, L1, L2, L3;
  329. p448_mul ( &L2, &a->z0, &a->zd );
  330. p448_bias ( &L2, 2 );
  331. p448_sub ( &L0, &L2, &a->xd );
  332. p448_mul ( &L2, &a->za, &L0 );
  333. p448_bias ( &L2, 2 );
  334. p448_mul ( &L1, &a->z0, &a->xd );
  335. p448_bias ( &L1, 2 );
  336. p448_sub ( &L0, &L1, &a->zd );
  337. p448_mul ( &L3, &a->xa, &L0 );
  338. p448_add ( &L1, &L3, &L2 );
  339. p448_sub ( &L0, &L2, &L3 );
  340. p448_mul ( &L2, &L0, &L1 );
  341. p448_mul ( &L0, sbz, &L2 );
  342. p448_mul ( &L2, &a->zd, &L0 );
  343. p448_mul ( sign, &L2, &a->zd );
  344. p448_mul ( ser, &L2, &a->xd );
  345. p448_mul ( &L2, sign, ser );
  346. p448_isr ( &L1, &L2 );
  347. p448_mul ( ser, sign, &L1 );
  348. p448_sqr ( &L0, &L1 );
  349. p448_mul ( sign, &L2, &L0 );
  350. }
  351. void
  352. serialize_extensible (
  353. struct p448_t* b,
  354. const struct extensible_t* a
  355. ) {
  356. struct p448_t L0, L1, L2;
  357. p448_sub ( &L0, &a->y, &a->z );
  358. p448_bias ( &L0, 2 );
  359. p448_add ( b, &a->z, &a->y );
  360. p448_mul ( &L1, &a->z, &a->x );
  361. p448_mul ( &L2, &L0, &L1 );
  362. p448_mul ( &L1, &L2, &L0 );
  363. p448_mul ( &L0, &L2, b );
  364. p448_mul ( &L2, &L1, &L0 );
  365. p448_isr ( &L0, &L2 );
  366. p448_mul ( b, &L1, &L0 );
  367. p448_sqr ( &L1, &L0 );
  368. p448_mul ( &L0, &L2, &L1 );
  369. }
  370. void
  371. untwist_and_double_and_serialize (
  372. struct p448_t* b,
  373. const struct tw_extensible_t* a
  374. ) {
  375. struct p448_t L0, L1, L2, L3;
  376. p448_mul ( &L3, &a->y, &a->x );
  377. p448_add ( b, &a->y, &a->x );
  378. p448_sqr ( &L1, b );
  379. p448_add ( &L2, &L3, &L3 );
  380. p448_sub ( b, &L1, &L2 );
  381. p448_bias ( b, 3 );
  382. p448_sqr ( &L2, &a->z );
  383. p448_sqr ( &L1, &L2 );
  384. p448_add ( &L2, b, b );
  385. p448_mulw ( b, &L2, 39082 );
  386. p448_neg ( &L2, b );
  387. p448_bias ( &L2, 2 );
  388. p448_mulw ( &L0, &L2, 39082 );
  389. p448_neg ( b, &L0 );
  390. p448_bias ( b, 2 );
  391. p448_mul ( &L0, &L2, &L1 );
  392. p448_mul ( &L2, b, &L0 );
  393. p448_isr ( &L0, &L2 );
  394. p448_mul ( &L1, b, &L0 );
  395. p448_sqr ( b, &L0 );
  396. p448_mul ( &L0, &L2, b );
  397. p448_mul ( b, &L1, &L3 );
  398. }
  399. void
  400. twist (
  401. struct tw_extensible_t* b,
  402. const struct extensible_t* a
  403. ) {
  404. mask_t L0, L1;
  405. p448_sqr ( &b->y, &a->z );
  406. p448_sqr ( &b->z, &a->x );
  407. p448_sub ( &b->u, &b->y, &b->z );
  408. p448_bias ( &b->u, 2 );
  409. p448_sub ( &b->z, &a->z, &a->x );
  410. p448_bias ( &b->z, 2 );
  411. p448_mul ( &b->y, &b->z, &a->y );
  412. p448_sub ( &b->z, &a->z, &a->y );
  413. p448_bias ( &b->z, 2 );
  414. p448_mul ( &b->x, &b->z, &b->y );
  415. p448_mul ( &b->t, &b->x, &b->u );
  416. p448_mul ( &b->y, &b->x, &b->t );
  417. p448_isr ( &b->t, &b->y );
  418. p448_mul ( &b->u, &b->x, &b->t );
  419. p448_sqr ( &b->x, &b->t );
  420. p448_mul ( &b->t, &b->y, &b->x );
  421. p448_mul ( &b->x, &a->x, &b->u );
  422. p448_mul ( &b->y, &a->y, &b->u );
  423. L1 = p448_is_zero( &b->z );
  424. L0 = - L1;
  425. p448_addw ( &b->y, L0 );
  426. p448_weak_reduce( &b->y );
  427. p448_set_ui( &b->z, 1 );
  428. p448_copy ( &b->t, &b->x );
  429. p448_copy ( &b->u, &b->y );
  430. }
  431. mask_t
  432. deserialize_affine (
  433. struct affine_t* a,
  434. const struct p448_t* sz
  435. ) {
  436. struct p448_t L0, L1, L2, L3;
  437. p448_sqr ( &L1, sz );
  438. p448_copy ( &L3, &L1 );
  439. p448_addw ( &L3, 1 );
  440. p448_sqr ( &a->x, &L3 );
  441. p448_mulw ( &L3, &a->x, 39082 );
  442. p448_neg ( &a->x, &L3 );
  443. p448_add ( &L3, &L1, &L1 );
  444. p448_bias ( &L3, 1 );
  445. p448_add ( &a->y, &L3, &L3 );
  446. p448_add ( &L3, &a->y, &a->x );
  447. p448_copy ( &a->y, &L1 );
  448. p448_subw ( &a->y, 1 );
  449. p448_neg ( &a->x, &a->y );
  450. p448_bias ( &a->x, 2 );
  451. p448_mul ( &a->y, &a->x, &L3 );
  452. p448_sqr ( &L2, &a->x );
  453. p448_mul ( &L0, &L2, &a->y );
  454. p448_mul ( &a->y, &a->x, &L0 );
  455. p448_isr ( &L3, &a->y );
  456. p448_mul ( &a->y, &L2, &L3 );
  457. p448_sqr ( &L2, &L3 );
  458. p448_mul ( &L3, &L0, &L2 );
  459. p448_mul ( &L0, &a->x, &L3 );
  460. p448_bias ( &L0, 1 );
  461. p448_add ( &L2, &a->y, &a->y );
  462. p448_mul ( &a->x, sz, &L2 );
  463. p448_addw ( &L1, 1 );
  464. p448_mul ( &a->y, &L1, &L3 );
  465. p448_subw ( &L0, 1 );
  466. return p448_is_zero( &L0 );
  467. }
  468. mask_t
  469. deserialize_and_twist_approx (
  470. struct tw_extensible_t* a,
  471. const struct p448_t* sdm1,
  472. const struct p448_t* sz
  473. ) {
  474. struct p448_t L0, L1;
  475. p448_sqr ( &a->z, sz );
  476. p448_copy ( &a->y, &a->z );
  477. p448_addw ( &a->y, 1 );
  478. p448_sqr ( &a->x, &a->y );
  479. p448_mulw ( &a->y, &a->x, 39082 );
  480. p448_neg ( &a->x, &a->y );
  481. p448_add ( &a->y, &a->z, &a->z );
  482. p448_bias ( &a->y, 1 );
  483. p448_add ( &a->u, &a->y, &a->y );
  484. p448_add ( &a->y, &a->u, &a->x );
  485. p448_sqr ( &a->x, &a->z );
  486. p448_subw ( &a->x, 1 );
  487. p448_neg ( &a->u, &a->x );
  488. p448_bias ( &a->u, 2 );
  489. p448_mul ( &a->x, sdm1, &a->u );
  490. p448_mul ( &L0, &a->x, &a->y );
  491. p448_mul ( &a->t, &L0, &a->y );
  492. p448_mul ( &a->u, &a->x, &a->t );
  493. p448_mul ( &a->t, &a->u, &L0 );
  494. p448_mul ( &a->y, &a->x, &a->t );
  495. p448_isr ( &L0, &a->y );
  496. p448_mul ( &a->y, &a->u, &L0 );
  497. p448_sqr ( &L1, &L0 );
  498. p448_mul ( &a->u, &a->t, &L1 );
  499. p448_mul ( &a->t, &a->x, &a->u );
  500. p448_bias ( &a->t, 1 );
  501. p448_add ( &a->x, sz, sz );
  502. p448_mul ( &L0, &a->u, &a->x );
  503. p448_copy ( &a->x, &a->z );
  504. p448_subw ( &a->x, 1 );
  505. p448_neg ( &L1, &a->x );
  506. p448_bias ( &L1, 2 );
  507. p448_mul ( &a->x, &L1, &L0 );
  508. p448_mul ( &L0, &a->u, &a->y );
  509. p448_addw ( &a->z, 1 );
  510. p448_mul ( &a->y, &a->z, &L0 );
  511. p448_subw ( &a->t, 1 );
  512. mask_t ret = p448_is_zero( &a->t );
  513. p448_set_ui( &a->z, 1 );
  514. p448_copy ( &a->t, &a->x );
  515. p448_copy ( &a->u, &a->y );
  516. return ret;
  517. }
  518. void
  519. set_identity_extensible (
  520. struct extensible_t* a
  521. ) {
  522. p448_set_ui( &a->x, 0 );
  523. p448_set_ui( &a->y, 1 );
  524. p448_set_ui( &a->z, 1 );
  525. p448_set_ui( &a->t, 0 );
  526. p448_set_ui( &a->u, 0 );
  527. }
  528. void
  529. set_identity_tw_extensible (
  530. struct tw_extensible_t* a
  531. ) {
  532. p448_set_ui( &a->x, 0 );
  533. p448_set_ui( &a->y, 1 );
  534. p448_set_ui( &a->z, 1 );
  535. p448_set_ui( &a->t, 0 );
  536. p448_set_ui( &a->u, 0 );
  537. }
  538. void
  539. set_identity_affine (
  540. struct affine_t* a
  541. ) {
  542. p448_set_ui( &a->x, 0 );
  543. p448_set_ui( &a->y, 1 );
  544. }
  545. mask_t
  546. eq_affine (
  547. const struct affine_t* a,
  548. const struct affine_t* b
  549. ) {
  550. mask_t L1, L2;
  551. struct p448_t L0;
  552. p448_sub ( &L0, &a->x, &b->x );
  553. p448_bias ( &L0, 2 );
  554. L2 = p448_is_zero( &L0 );
  555. p448_sub ( &L0, &a->y, &b->y );
  556. p448_bias ( &L0, 2 );
  557. L1 = p448_is_zero( &L0 );
  558. return L2 & L1;
  559. }
  560. mask_t
  561. eq_extensible (
  562. const struct extensible_t* a,
  563. const struct extensible_t* b
  564. ) {
  565. mask_t L3, L4;
  566. struct p448_t L0, L1, L2;
  567. p448_mul ( &L2, &b->z, &a->x );
  568. p448_mul ( &L1, &a->z, &b->x );
  569. p448_sub ( &L0, &L2, &L1 );
  570. p448_bias ( &L0, 2 );
  571. L4 = p448_is_zero( &L0 );
  572. p448_mul ( &L2, &b->z, &a->y );
  573. p448_mul ( &L1, &a->z, &b->y );
  574. p448_sub ( &L0, &L2, &L1 );
  575. p448_bias ( &L0, 2 );
  576. L3 = p448_is_zero( &L0 );
  577. return L4 & L3;
  578. }
  579. mask_t
  580. eq_tw_extensible (
  581. const struct tw_extensible_t* a,
  582. const struct tw_extensible_t* b
  583. ) {
  584. mask_t L3, L4;
  585. struct p448_t L0, L1, L2;
  586. p448_mul ( &L2, &b->z, &a->x );
  587. p448_mul ( &L1, &a->z, &b->x );
  588. p448_sub ( &L0, &L2, &L1 );
  589. p448_bias ( &L0, 2 );
  590. L4 = p448_is_zero( &L0 );
  591. p448_mul ( &L2, &b->z, &a->y );
  592. p448_mul ( &L1, &a->z, &b->y );
  593. p448_sub ( &L0, &L2, &L1 );
  594. p448_bias ( &L0, 2 );
  595. L3 = p448_is_zero( &L0 );
  596. return L4 & L3;
  597. }
  598. void
  599. elligator_2s_inject (
  600. struct affine_t* a,
  601. const struct p448_t* r
  602. ) {
  603. mask_t L0, L1;
  604. struct p448_t L2, L3, L4, L5, L6, L7, L8, L9;
  605. p448_sqr ( &a->x, r );
  606. p448_sqr ( &L3, &a->x );
  607. p448_copy ( &a->y, &L3 );
  608. p448_subw ( &a->y, 1 );
  609. p448_neg ( &L9, &a->y );
  610. p448_bias ( &L9, 2 );
  611. p448_sqr ( &L2, &L9 );
  612. p448_bias ( &L2, 1 );
  613. p448_mulw ( &L7, &L2, 1527402724 );
  614. p448_bias ( &L7, 2 );
  615. p448_mulw ( &L8, &L3, 6108985600 );
  616. p448_add ( &a->y, &L8, &L7 );
  617. p448_mulw ( &L8, &L2, 6109454568 );
  618. p448_sub ( &L7, &a->y, &L8 );
  619. p448_mulw ( &L4, &a->y, 78160 );
  620. p448_mul ( &L6, &L7, &L9 );
  621. p448_mul ( &L8, &L6, &L4 );
  622. p448_mul ( &L4, &L7, &L8 );
  623. p448_isr ( &L5, &L4 );
  624. p448_mul ( &L4, &L6, &L5 );
  625. p448_sqr ( &L6, &L5 );
  626. p448_mul ( &L5, &L8, &L6 );
  627. p448_mul ( &L8, &L7, &L5 );
  628. p448_mul ( &L7, &L8, &L5 );
  629. p448_copy ( &L6, &a->x );
  630. p448_subw ( &L6, 1 );
  631. p448_addw ( &a->x, 1 );
  632. p448_mul ( &L5, &a->x, &L8 );
  633. p448_sub ( &a->x, &L6, &L5 );
  634. p448_bias ( &a->x, 3 );
  635. p448_mul ( &L5, &L4, &a->x );
  636. p448_mulw ( &L4, &L5, 78160 );
  637. p448_neg ( &a->x, &L4 );
  638. p448_bias ( &a->x, 2 );
  639. p448_weak_reduce( &a->x );
  640. p448_add ( &L4, &L3, &L3 );
  641. p448_add ( &L3, &L4, &L2 );
  642. p448_subw ( &L3, 2 );
  643. p448_mul ( &L2, &L3, &L8 );
  644. p448_mulw ( &L3, &L2, 3054649120 );
  645. p448_add ( &L2, &L3, &a->y );
  646. p448_mul ( &a->y, &L7, &L2 );
  647. L1 = p448_is_zero( &L9 );
  648. L0 = - L1;
  649. p448_addw ( &a->y, L0 );
  650. p448_weak_reduce( &a->y );
  651. }
  652. mask_t
  653. validate_affine (
  654. const struct affine_t* a
  655. ) {
  656. struct p448_t L0, L1, L2, L3;
  657. p448_sqr ( &L0, &a->y );
  658. p448_sqr ( &L2, &a->x );
  659. p448_add ( &L3, &L2, &L0 );
  660. p448_subw ( &L3, 1 );
  661. p448_mulw ( &L1, &L2, 39081 );
  662. p448_neg ( &L2, &L1 );
  663. p448_bias ( &L2, 2 );
  664. p448_mul ( &L1, &L0, &L2 );
  665. p448_sub ( &L0, &L3, &L1 );
  666. p448_bias ( &L0, 3 );
  667. return p448_is_zero( &L0 );
  668. }
  669. mask_t
  670. validate_tw_extensible (
  671. const struct tw_extensible_t* ext
  672. ) {
  673. mask_t L4, L5;
  674. struct p448_t L0, L1, L2, L3;
  675. /*
  676. * Check invariant:
  677. * 0 = -x*y + z*t*u
  678. */
  679. p448_mul ( &L0, &ext->t, &ext->u );
  680. p448_mul ( &L2, &ext->z, &L0 );
  681. p448_addw ( &L2, 0 );
  682. p448_mul ( &L1, &ext->x, &ext->y );
  683. p448_neg ( &L0, &L1 );
  684. p448_add ( &L1, &L0, &L2 );
  685. p448_bias ( &L1, 2 );
  686. L5 = p448_is_zero( &L1 );
  687. /*
  688. * Check invariant:
  689. * 0 = d*t^2*u^2 + x^2 - y^2 + z^2 - t^2*u^2
  690. */
  691. p448_sqr ( &L2, &ext->y );
  692. p448_neg ( &L0, &L2 );
  693. p448_addw ( &L0, 0 );
  694. p448_sqr ( &L1, &ext->x );
  695. p448_bias ( &L1, 4 );
  696. p448_add ( &L2, &L1, &L0 );
  697. p448_sqr ( &L3, &ext->u );
  698. p448_sqr ( &L1, &ext->t );
  699. p448_mul ( &L0, &L1, &L3 );
  700. p448_mulw ( &L1, &L0, 39081 );
  701. p448_neg ( &L3, &L1 );
  702. p448_add ( &L1, &L3, &L2 );
  703. p448_neg ( &L3, &L0 );
  704. p448_add ( &L2, &L3, &L1 );
  705. p448_sqr ( &L1, &ext->z );
  706. p448_add ( &L0, &L1, &L2 );
  707. L4 = p448_is_zero( &L0 );
  708. return L5 & L4;
  709. }