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.
 
 
 
 
 

963 lines
27 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_sub ( &L1, &d->y, &d->x );
  60. p448_bias ( &L1, 2 );
  61. p448_weak_reduce( &L1 );
  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_mul ( &L1, &d->u, &d->t );
  66. p448_mul ( &d->x, &e->c, &L1 );
  67. p448_add ( &d->u, &L0, &d->y );
  68. p448_sub ( &d->t, &d->y, &L0 );
  69. p448_bias ( &d->t, 2 );
  70. p448_weak_reduce( &d->t );
  71. p448_sub ( &d->y, &d->z, &d->x );
  72. p448_bias ( &d->y, 2 );
  73. p448_weak_reduce( &d->y );
  74. p448_add ( &L0, &d->x, &d->z );
  75. p448_mul ( &d->z, &L0, &d->y );
  76. p448_mul ( &d->x, &d->y, &d->t );
  77. p448_mul ( &d->y, &L0, &d->u );
  78. }
  79. void
  80. sub_tw_niels_from_tw_extensible (
  81. struct tw_extensible_t* d,
  82. const struct tw_niels_t* e
  83. ) {
  84. struct p448_t L0, L1;
  85. p448_sub ( &L1, &d->y, &d->x );
  86. p448_bias ( &L1, 2 );
  87. p448_weak_reduce( &L1 );
  88. p448_mul ( &L0, &e->b, &L1 );
  89. p448_add ( &L1, &d->x, &d->y );
  90. p448_mul ( &d->y, &e->a, &L1 );
  91. p448_mul ( &L1, &d->u, &d->t );
  92. p448_mul ( &d->x, &e->c, &L1 );
  93. p448_add ( &d->u, &L0, &d->y );
  94. p448_sub ( &d->t, &d->y, &L0 );
  95. p448_bias ( &d->t, 2 );
  96. p448_weak_reduce( &d->t );
  97. p448_add ( &d->y, &d->x, &d->z );
  98. p448_sub ( &L0, &d->z, &d->x );
  99. p448_bias ( &L0, 2 );
  100. p448_weak_reduce( &L0 );
  101. p448_mul ( &d->z, &L0, &d->y );
  102. p448_mul ( &d->x, &d->y, &d->t );
  103. p448_mul ( &d->y, &L0, &d->u );
  104. }
  105. void
  106. add_tw_pniels_to_tw_extensible (
  107. struct tw_extensible_t* e,
  108. const struct tw_pniels_t* a
  109. ) {
  110. struct p448_t L0;
  111. p448_mul ( &L0, &e->z, &a->z );
  112. p448_copy ( &e->z, &L0 );
  113. add_tw_niels_to_tw_extensible( e, &a->n );
  114. }
  115. void
  116. sub_tw_pniels_from_tw_extensible (
  117. struct tw_extensible_t* e,
  118. const struct tw_pniels_t* a
  119. ) {
  120. struct p448_t L0;
  121. p448_mul ( &L0, &e->z, &a->z );
  122. p448_copy ( &e->z, &L0 );
  123. sub_tw_niels_from_tw_extensible( e, &a->n );
  124. }
  125. void
  126. double_tw_extensible (
  127. struct tw_extensible_t* a
  128. ) {
  129. struct p448_t L0, L1, L2;
  130. p448_sqr ( &L2, &a->x );
  131. p448_sqr ( &L0, &a->y );
  132. p448_add ( &a->u, &L2, &L0 );
  133. p448_add ( &a->t, &a->y, &a->x );
  134. p448_sqr ( &L1, &a->t );
  135. p448_sub ( &a->t, &L1, &a->u );
  136. p448_bias ( &a->t, 3 );
  137. p448_weak_reduce( &a->t );
  138. p448_sub ( &L1, &L0, &L2 );
  139. p448_bias ( &L1, 2 );
  140. p448_weak_reduce( &L1 );
  141. p448_sqr ( &a->x, &a->z );
  142. p448_bias ( &a->x, 1 );
  143. p448_add ( &a->z, &a->x, &a->x );
  144. p448_sub ( &L0, &a->z, &L1 );
  145. p448_weak_reduce( &L0 );
  146. p448_mul ( &a->z, &L1, &L0 );
  147. p448_mul ( &a->x, &L0, &a->t );
  148. p448_mul ( &a->y, &L1, &a->u );
  149. }
  150. void
  151. double_extensible (
  152. struct extensible_t* a
  153. ) {
  154. struct p448_t L0, L1, L2;
  155. p448_sqr ( &L2, &a->x );
  156. p448_sqr ( &L0, &a->y );
  157. p448_add ( &L1, &L2, &L0 );
  158. p448_add ( &a->t, &a->y, &a->x );
  159. p448_sqr ( &a->u, &a->t );
  160. p448_sub ( &a->t, &a->u, &L1 );
  161. p448_bias ( &a->t, 3 );
  162. p448_weak_reduce( &a->t );
  163. p448_sub ( &a->u, &L0, &L2 );
  164. p448_bias ( &a->u, 2 );
  165. p448_weak_reduce( &a->u );
  166. p448_sqr ( &a->x, &a->z );
  167. p448_bias ( &a->x, 2 );
  168. p448_add ( &a->z, &a->x, &a->x );
  169. p448_sub ( &L0, &a->z, &L1 );
  170. p448_weak_reduce( &L0 );
  171. p448_mul ( &a->z, &L1, &L0 );
  172. p448_mul ( &a->x, &L0, &a->t );
  173. p448_mul ( &a->y, &L1, &a->u );
  174. }
  175. void
  176. twist_and_double (
  177. struct tw_extensible_t* b,
  178. const struct extensible_t* a
  179. ) {
  180. struct p448_t L0;
  181. p448_sqr ( &b->x, &a->x );
  182. p448_sqr ( &b->z, &a->y );
  183. p448_add ( &b->u, &b->x, &b->z );
  184. p448_add ( &b->t, &a->y, &a->x );
  185. p448_sqr ( &L0, &b->t );
  186. p448_sub ( &b->t, &L0, &b->u );
  187. p448_bias ( &b->t, 3 );
  188. p448_weak_reduce( &b->t );
  189. p448_sub ( &L0, &b->z, &b->x );
  190. p448_bias ( &L0, 2 );
  191. p448_weak_reduce( &L0 );
  192. p448_sqr ( &b->x, &a->z );
  193. p448_bias ( &b->x, 2 );
  194. p448_add ( &b->z, &b->x, &b->x );
  195. p448_sub ( &b->y, &b->z, &b->u );
  196. p448_weak_reduce( &b->y );
  197. p448_mul ( &b->z, &L0, &b->y );
  198. p448_mul ( &b->x, &b->y, &b->t );
  199. p448_mul ( &b->y, &L0, &b->u );
  200. }
  201. void
  202. untwist_and_double (
  203. struct extensible_t* b,
  204. const struct tw_extensible_t* a
  205. ) {
  206. struct p448_t L0;
  207. p448_sqr ( &b->x, &a->x );
  208. p448_sqr ( &b->z, &a->y );
  209. p448_add ( &L0, &b->x, &b->z );
  210. p448_add ( &b->t, &a->y, &a->x );
  211. p448_sqr ( &b->u, &b->t );
  212. p448_sub ( &b->t, &b->u, &L0 );
  213. p448_bias ( &b->t, 3 );
  214. p448_weak_reduce( &b->t );
  215. p448_sub ( &b->u, &b->z, &b->x );
  216. p448_bias ( &b->u, 2 );
  217. p448_weak_reduce( &b->u );
  218. p448_sqr ( &b->x, &a->z );
  219. p448_bias ( &b->x, 1 );
  220. p448_add ( &b->z, &b->x, &b->x );
  221. p448_sub ( &b->y, &b->z, &b->u );
  222. p448_weak_reduce( &b->y );
  223. p448_mul ( &b->z, &L0, &b->y );
  224. p448_mul ( &b->x, &b->y, &b->t );
  225. p448_mul ( &b->y, &L0, &b->u );
  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. p448_sub ( &b->n.a, &a->y, &a->x );
  233. p448_bias ( &b->n.a, 2 );
  234. p448_weak_reduce( &b->n.a );
  235. p448_add ( &b->n.b, &a->x, &a->y );
  236. p448_weak_reduce( &b->n.b );
  237. p448_mul ( &b->n.c, &a->y, &a->x );
  238. p448_mulw ( &b->z, &b->n.c, 78164 );
  239. p448_neg ( &b->n.c, &b->z );
  240. p448_bias ( &b->n.c, 2 );
  241. p448_weak_reduce( &b->n.c );
  242. p448_set_ui( &b->z, 2 );
  243. }
  244. void
  245. convert_tw_affine_to_tw_extensible (
  246. struct tw_extensible_t* b,
  247. const struct tw_affine_t* a
  248. ) {
  249. p448_copy ( &b->x, &a->x );
  250. p448_copy ( &b->y, &a->y );
  251. p448_set_ui( &b->z, 1 );
  252. p448_copy ( &b->t, &a->x );
  253. p448_copy ( &b->u, &a->y );
  254. }
  255. void
  256. convert_affine_to_extensible (
  257. struct extensible_t* b,
  258. const struct affine_t* a
  259. ) {
  260. p448_copy ( &b->x, &a->x );
  261. p448_copy ( &b->y, &a->y );
  262. p448_set_ui( &b->z, 1 );
  263. p448_copy ( &b->t, &a->x );
  264. p448_copy ( &b->u, &a->y );
  265. }
  266. void
  267. convert_tw_extensible_to_tw_pniels (
  268. struct tw_pniels_t* b,
  269. const struct tw_extensible_t* a
  270. ) {
  271. p448_sub ( &b->n.a, &a->y, &a->x );
  272. p448_bias ( &b->n.a, 2 );
  273. p448_weak_reduce( &b->n.a );
  274. p448_add ( &b->n.b, &a->x, &a->y );
  275. p448_weak_reduce( &b->n.b );
  276. p448_mul ( &b->n.c, &a->u, &a->t );
  277. p448_mulw ( &b->z, &b->n.c, 78164 );
  278. p448_neg ( &b->n.c, &b->z );
  279. p448_bias ( &b->n.c, 2 );
  280. p448_weak_reduce( &b->n.c );
  281. p448_add ( &b->z, &a->z, &a->z );
  282. p448_weak_reduce( &b->z );
  283. }
  284. void
  285. convert_tw_pniels_to_tw_extensible (
  286. struct tw_extensible_t* e,
  287. const struct tw_pniels_t* d
  288. ) {
  289. p448_add ( &e->u, &d->n.b, &d->n.a );
  290. p448_sub ( &e->t, &d->n.b, &d->n.a );
  291. p448_bias ( &e->t, 2 );
  292. p448_weak_reduce( &e->t );
  293. p448_mul ( &e->x, &d->z, &e->t );
  294. p448_mul ( &e->y, &d->z, &e->u );
  295. p448_sqr ( &e->z, &d->z );
  296. }
  297. void
  298. convert_tw_niels_to_tw_extensible (
  299. struct tw_extensible_t* e,
  300. const struct tw_niels_t* d
  301. ) {
  302. p448_add ( &e->y, &d->b, &d->a );
  303. p448_weak_reduce( &e->y );
  304. p448_sub ( &e->x, &d->b, &d->a );
  305. p448_bias ( &e->x, 2 );
  306. p448_weak_reduce( &e->x );
  307. p448_set_ui( &e->z, 1 );
  308. p448_copy ( &e->t, &e->x );
  309. p448_copy ( &e->u, &e->y );
  310. }
  311. void
  312. montgomery_step (
  313. struct montgomery_t* a
  314. ) {
  315. struct p448_t L0, L1;
  316. p448_add ( &L0, &a->zd, &a->xd );
  317. p448_sub ( &L1, &a->xd, &a->zd );
  318. p448_bias ( &L1, 2 );
  319. p448_weak_reduce( &L1 );
  320. p448_sub ( &a->zd, &a->xa, &a->za );
  321. p448_bias ( &a->zd, 2 );
  322. p448_weak_reduce( &a->zd );
  323. p448_mul ( &a->xd, &L0, &a->zd );
  324. p448_add ( &a->zd, &a->za, &a->xa );
  325. p448_mul ( &a->za, &L1, &a->zd );
  326. p448_add ( &a->xa, &a->za, &a->xd );
  327. p448_sqr ( &a->zd, &a->xa );
  328. p448_mul ( &a->xa, &a->z0, &a->zd );
  329. p448_sub ( &a->zd, &a->xd, &a->za );
  330. p448_bias ( &a->zd, 2 );
  331. p448_weak_reduce( &a->zd );
  332. p448_sqr ( &a->za, &a->zd );
  333. p448_sqr ( &a->xd, &L0 );
  334. p448_sqr ( &L0, &L1 );
  335. p448_mulw ( &a->zd, &a->xd, 39082 );
  336. p448_sub ( &L1, &a->xd, &L0 );
  337. p448_bias ( &L1, 2 );
  338. p448_weak_reduce( &L1 );
  339. p448_mul ( &a->xd, &L0, &a->zd );
  340. p448_sub ( &L0, &a->zd, &L1 );
  341. p448_bias ( &L0, 2 );
  342. p448_weak_reduce( &L0 );
  343. p448_mul ( &a->zd, &L0, &L1 );
  344. }
  345. void
  346. deserialize_montgomery (
  347. struct montgomery_t* a,
  348. const struct p448_t* sbz
  349. ) {
  350. p448_sqr ( &a->z0, sbz );
  351. p448_set_ui( &a->xd, 1 );
  352. p448_set_ui( &a->zd, 0 );
  353. p448_set_ui( &a->xa, 1 );
  354. p448_copy ( &a->za, &a->z0 );
  355. }
  356. mask_t
  357. serialize_montgomery (
  358. struct p448_t* b,
  359. const struct montgomery_t* a,
  360. const struct p448_t* sbz
  361. ) {
  362. mask_t L4, L5, L6;
  363. struct p448_t L0, L1, L2, L3;
  364. p448_mul ( &L3, &a->z0, &a->zd );
  365. p448_sub ( &L1, &L3, &a->xd );
  366. p448_bias ( &L1, 2 );
  367. p448_weak_reduce( &L1 );
  368. p448_mul ( &L3, &a->za, &L1 );
  369. p448_mul ( &L2, &a->z0, &a->xd );
  370. p448_sub ( &L1, &L2, &a->zd );
  371. p448_bias ( &L1, 2 );
  372. p448_weak_reduce( &L1 );
  373. p448_mul ( &L0, &a->xa, &L1 );
  374. p448_add ( &L2, &L0, &L3 );
  375. p448_sub ( &L1, &L3, &L0 );
  376. p448_bias ( &L1, 2 );
  377. p448_weak_reduce( &L1 );
  378. p448_mul ( &L3, &L1, &L2 );
  379. p448_copy ( &L2, &a->z0 );
  380. p448_addw ( &L2, 1 );
  381. p448_sqr ( &L1, &L2 );
  382. p448_mulw ( &L2, &L1, 39082 );
  383. p448_neg ( &L1, &L2 );
  384. p448_add ( &L2, &a->z0, &a->z0 );
  385. p448_bias ( &L2, 1 );
  386. p448_add ( &L0, &L2, &L2 );
  387. p448_add ( &L2, &L0, &L1 );
  388. p448_weak_reduce( &L2 );
  389. p448_mul ( &L0, &a->xd, &L2 );
  390. L5 = p448_is_zero( &a->zd );
  391. L6 = - L5;
  392. p448_mask ( &L1, &L0, L5 );
  393. p448_add ( &L2, &L1, &a->zd );
  394. L4 = ~ L5;
  395. p448_mul ( &L1, sbz, &L3 );
  396. p448_addw ( &L1, L6 );
  397. p448_mul ( &L3, &L2, &L1 );
  398. p448_mul ( &L1, &L3, &L2 );
  399. p448_mul ( &L2, &L3, &a->xd );
  400. p448_mul ( &L3, &L1, &L2 );
  401. p448_isr ( &L0, &L3 );
  402. p448_mul ( &L2, &L1, &L0 );
  403. p448_sqr ( &L1, &L0 );
  404. p448_mul ( &L0, &L3, &L1 );
  405. p448_mask ( b, &L2, L4 );
  406. p448_subw ( &L0, 1 );
  407. p448_bias ( &L0, 1 );
  408. L5 = p448_is_zero( &L0 );
  409. L4 = p448_is_zero( sbz );
  410. return L5 | L4;
  411. }
  412. void
  413. serialize_extensible (
  414. struct p448_t* b,
  415. const struct extensible_t* a
  416. ) {
  417. struct p448_t L0, L1, L2;
  418. p448_sub ( &L0, &a->y, &a->z );
  419. p448_bias ( &L0, 2 );
  420. p448_weak_reduce( &L0 );
  421. p448_add ( b, &a->z, &a->y );
  422. p448_mul ( &L1, &a->z, &a->x );
  423. p448_mul ( &L2, &L0, &L1 );
  424. p448_mul ( &L1, &L2, &L0 );
  425. p448_mul ( &L0, &L2, b );
  426. p448_mul ( &L2, &L1, &L0 );
  427. p448_isr ( &L0, &L2 );
  428. p448_mul ( b, &L1, &L0 );
  429. p448_sqr ( &L1, &L0 );
  430. p448_mul ( &L0, &L2, &L1 );
  431. }
  432. void
  433. untwist_and_double_and_serialize (
  434. struct p448_t* b,
  435. const struct tw_extensible_t* a
  436. ) {
  437. struct p448_t L0, L1, L2, L3;
  438. p448_mul ( &L3, &a->y, &a->x );
  439. p448_add ( b, &a->y, &a->x );
  440. p448_sqr ( &L1, b );
  441. p448_add ( &L2, &L3, &L3 );
  442. p448_sub ( b, &L1, &L2 );
  443. p448_bias ( b, 3 );
  444. p448_weak_reduce( b );
  445. p448_sqr ( &L2, &a->z );
  446. p448_sqr ( &L1, &L2 );
  447. p448_add ( &L2, b, b );
  448. p448_mulw ( b, &L2, 39082 );
  449. p448_neg ( &L2, b );
  450. p448_bias ( &L2, 2 );
  451. p448_mulw ( &L0, &L2, 39082 );
  452. p448_neg ( b, &L0 );
  453. p448_bias ( b, 2 );
  454. p448_mul ( &L0, &L2, &L1 );
  455. p448_mul ( &L2, b, &L0 );
  456. p448_isr ( &L0, &L2 );
  457. p448_mul ( &L1, b, &L0 );
  458. p448_sqr ( b, &L0 );
  459. p448_mul ( &L0, &L2, b );
  460. p448_mul ( b, &L1, &L3 );
  461. }
  462. void
  463. twist_even (
  464. struct tw_extensible_t* b,
  465. const struct extensible_t* a
  466. ) {
  467. mask_t L0, L1;
  468. p448_sqr ( &b->y, &a->z );
  469. p448_sqr ( &b->z, &a->x );
  470. p448_sub ( &b->u, &b->y, &b->z );
  471. p448_bias ( &b->u, 2 );
  472. p448_weak_reduce( &b->u );
  473. p448_sub ( &b->z, &a->z, &a->x );
  474. p448_bias ( &b->z, 2 );
  475. p448_weak_reduce( &b->z );
  476. p448_mul ( &b->y, &b->z, &a->y );
  477. p448_sub ( &b->z, &a->z, &a->y );
  478. p448_bias ( &b->z, 2 );
  479. p448_weak_reduce( &b->z );
  480. p448_mul ( &b->x, &b->z, &b->y );
  481. p448_mul ( &b->t, &b->x, &b->u );
  482. p448_mul ( &b->y, &b->x, &b->t );
  483. p448_isr ( &b->t, &b->y );
  484. p448_mul ( &b->u, &b->x, &b->t );
  485. p448_sqr ( &b->x, &b->t );
  486. p448_mul ( &b->t, &b->y, &b->x );
  487. p448_mul ( &b->x, &a->x, &b->u );
  488. p448_mul ( &b->y, &a->y, &b->u );
  489. L1 = p448_is_zero( &b->z );
  490. L0 = - L1;
  491. p448_addw ( &b->y, L0 );
  492. p448_weak_reduce( &b->y );
  493. p448_set_ui( &b->z, 1 );
  494. p448_copy ( &b->t, &b->x );
  495. p448_copy ( &b->u, &b->y );
  496. }
  497. void
  498. test_only_twist (
  499. struct tw_extensible_t* b,
  500. const struct extensible_t* a
  501. ) {
  502. mask_t L2, L3;
  503. struct p448_t L0, L1;
  504. p448_sqr ( &b->u, &a->z );
  505. p448_sqr ( &b->y, &a->x );
  506. p448_sub ( &b->z, &b->u, &b->y );
  507. p448_bias ( &b->z, 2 );
  508. p448_add ( &b->y, &b->z, &b->z );
  509. p448_add ( &b->u, &b->y, &b->y );
  510. p448_weak_reduce( &b->u );
  511. p448_sub ( &b->y, &a->z, &a->x );
  512. p448_bias ( &b->y, 2 );
  513. p448_weak_reduce( &b->y );
  514. p448_mul ( &b->x, &b->y, &a->y );
  515. p448_sub ( &b->z, &a->z, &a->y );
  516. p448_bias ( &b->z, 2 );
  517. p448_weak_reduce( &b->z );
  518. p448_mul ( &b->t, &b->z, &b->x );
  519. p448_mul ( &L1, &b->t, &b->u );
  520. p448_mul ( &b->x, &b->t, &L1 );
  521. p448_isr ( &L0, &b->x );
  522. p448_mul ( &b->u, &b->t, &L0 );
  523. p448_sqr ( &L1, &L0 );
  524. p448_mul ( &b->t, &b->x, &L1 );
  525. p448_add ( &L1, &a->y, &a->x );
  526. p448_weak_reduce( &L1 );
  527. p448_sub ( &L0, &a->x, &a->y );
  528. p448_bias ( &L0, 2 );
  529. p448_weak_reduce( &L0 );
  530. p448_mul ( &b->x, &b->t, &L0 );
  531. p448_add ( &L0, &b->x, &L1 );
  532. p448_sub ( &b->t, &L1, &b->x );
  533. p448_bias ( &b->t, 2 );
  534. p448_weak_reduce( &b->t );
  535. p448_mul ( &b->x, &L0, &b->u );
  536. L2 = p448_is_zero( &b->y );
  537. L3 = - L2;
  538. p448_addw ( &b->x, L3 );
  539. p448_weak_reduce( &b->x );
  540. p448_mul ( &b->y, &b->t, &b->u );
  541. L2 = p448_is_zero( &b->z );
  542. L3 = - L2;
  543. p448_addw ( &b->y, L3 );
  544. p448_weak_reduce( &b->y );
  545. L3 = p448_is_zero( &a->y );
  546. L2 = L3 + 1;
  547. p448_set_ui( &b->z, L2 );
  548. p448_copy ( &b->t, &b->x );
  549. p448_copy ( &b->u, &b->y );
  550. }
  551. mask_t
  552. is_square (
  553. const struct p448_t* x
  554. ) {
  555. mask_t L2, L3;
  556. struct p448_t L0, L1;
  557. p448_isr ( &L0, x );
  558. p448_sqr ( &L1, &L0 );
  559. p448_mul ( &L0, x, &L1 );
  560. p448_subw ( &L0, 1 );
  561. p448_bias ( &L0, 1 );
  562. L3 = p448_is_zero( &L0 );
  563. L2 = p448_is_zero( x );
  564. return L3 | L2;
  565. }
  566. mask_t
  567. is_even_pt (
  568. const struct extensible_t* a
  569. ) {
  570. struct p448_t L0, L1, L2;
  571. p448_sqr ( &L2, &a->z );
  572. p448_sqr ( &L1, &a->x );
  573. p448_sub ( &L0, &L2, &L1 );
  574. p448_bias ( &L0, 2 );
  575. p448_weak_reduce( &L0 );
  576. return is_square ( &L0 );
  577. }
  578. mask_t
  579. is_even_tw (
  580. const struct tw_extensible_t* a
  581. ) {
  582. struct p448_t L0, L1, L2;
  583. p448_sqr ( &L2, &a->z );
  584. p448_sqr ( &L1, &a->x );
  585. p448_add ( &L0, &L1, &L2 );
  586. p448_weak_reduce( &L0 );
  587. return is_square ( &L0 );
  588. }
  589. mask_t
  590. deserialize_affine (
  591. struct affine_t* a,
  592. const struct p448_t* sz
  593. ) {
  594. struct p448_t L0, L1, L2, L3;
  595. p448_sqr ( &L1, sz );
  596. p448_copy ( &L3, &L1 );
  597. p448_addw ( &L3, 1 );
  598. p448_sqr ( &a->x, &L3 );
  599. p448_mulw ( &L3, &a->x, 39082 );
  600. p448_neg ( &a->x, &L3 );
  601. p448_add ( &L3, &L1, &L1 );
  602. p448_bias ( &L3, 1 );
  603. p448_add ( &a->y, &L3, &L3 );
  604. p448_add ( &L3, &a->y, &a->x );
  605. p448_weak_reduce( &L3 );
  606. p448_copy ( &a->y, &L1 );
  607. p448_subw ( &a->y, 1 );
  608. p448_neg ( &a->x, &a->y );
  609. p448_bias ( &a->x, 2 );
  610. p448_weak_reduce( &a->x );
  611. p448_mul ( &a->y, &a->x, &L3 );
  612. p448_sqr ( &L2, &a->x );
  613. p448_mul ( &L0, &L2, &a->y );
  614. p448_mul ( &a->y, &a->x, &L0 );
  615. p448_isr ( &L3, &a->y );
  616. p448_mul ( &a->y, &L2, &L3 );
  617. p448_sqr ( &L2, &L3 );
  618. p448_mul ( &L3, &L0, &L2 );
  619. p448_mul ( &L0, &a->x, &L3 );
  620. p448_add ( &L2, &a->y, &a->y );
  621. p448_mul ( &a->x, sz, &L2 );
  622. p448_addw ( &L1, 1 );
  623. p448_mul ( &a->y, &L1, &L3 );
  624. p448_subw ( &L0, 1 );
  625. p448_bias ( &L0, 1 );
  626. return p448_is_zero( &L0 );
  627. }
  628. mask_t
  629. deserialize_and_twist_approx (
  630. struct tw_extensible_t* a,
  631. const struct p448_t* sdm1,
  632. const struct p448_t* sz
  633. ) {
  634. struct p448_t L0, L1;
  635. p448_sqr ( &a->z, sz );
  636. p448_copy ( &a->y, &a->z );
  637. p448_addw ( &a->y, 1 );
  638. p448_sqr ( &a->x, &a->y );
  639. p448_mulw ( &a->y, &a->x, 39082 );
  640. p448_neg ( &a->x, &a->y );
  641. p448_add ( &a->y, &a->z, &a->z );
  642. p448_bias ( &a->y, 1 );
  643. p448_add ( &a->u, &a->y, &a->y );
  644. p448_add ( &a->y, &a->u, &a->x );
  645. p448_weak_reduce( &a->y );
  646. p448_sqr ( &a->x, &a->z );
  647. p448_subw ( &a->x, 1 );
  648. p448_neg ( &a->u, &a->x );
  649. p448_bias ( &a->u, 2 );
  650. p448_weak_reduce( &a->u );
  651. p448_mul ( &a->x, sdm1, &a->u );
  652. p448_mul ( &L0, &a->x, &a->y );
  653. p448_mul ( &a->t, &L0, &a->y );
  654. p448_mul ( &a->u, &a->x, &a->t );
  655. p448_mul ( &a->t, &a->u, &L0 );
  656. p448_mul ( &a->y, &a->x, &a->t );
  657. p448_isr ( &L0, &a->y );
  658. p448_mul ( &a->y, &a->u, &L0 );
  659. p448_sqr ( &L1, &L0 );
  660. p448_mul ( &a->u, &a->t, &L1 );
  661. p448_mul ( &a->t, &a->x, &a->u );
  662. p448_add ( &a->x, sz, sz );
  663. p448_mul ( &L0, &a->u, &a->x );
  664. p448_copy ( &a->x, &a->z );
  665. p448_subw ( &a->x, 1 );
  666. p448_neg ( &L1, &a->x );
  667. p448_bias ( &L1, 2 );
  668. p448_weak_reduce( &L1 );
  669. p448_mul ( &a->x, &L1, &L0 );
  670. p448_mul ( &L0, &a->u, &a->y );
  671. p448_addw ( &a->z, 1 );
  672. p448_mul ( &a->y, &a->z, &L0 );
  673. p448_subw ( &a->t, 1 );
  674. p448_bias ( &a->t, 1 );
  675. mask_t ret = p448_is_zero( &a->t );
  676. p448_set_ui( &a->z, 1 );
  677. p448_copy ( &a->t, &a->x );
  678. p448_copy ( &a->u, &a->y );
  679. return ret;
  680. }
  681. void
  682. set_identity_extensible (
  683. struct extensible_t* a
  684. ) {
  685. p448_set_ui( &a->x, 0 );
  686. p448_set_ui( &a->y, 1 );
  687. p448_set_ui( &a->z, 1 );
  688. p448_set_ui( &a->t, 0 );
  689. p448_set_ui( &a->u, 0 );
  690. }
  691. void
  692. set_identity_tw_extensible (
  693. struct tw_extensible_t* a
  694. ) {
  695. p448_set_ui( &a->x, 0 );
  696. p448_set_ui( &a->y, 1 );
  697. p448_set_ui( &a->z, 1 );
  698. p448_set_ui( &a->t, 0 );
  699. p448_set_ui( &a->u, 0 );
  700. }
  701. void
  702. set_identity_affine (
  703. struct affine_t* a
  704. ) {
  705. p448_set_ui( &a->x, 0 );
  706. p448_set_ui( &a->y, 1 );
  707. }
  708. mask_t
  709. eq_affine (
  710. const struct affine_t* a,
  711. const struct affine_t* b
  712. ) {
  713. mask_t L1, L2;
  714. struct p448_t L0;
  715. p448_sub ( &L0, &a->x, &b->x );
  716. p448_bias ( &L0, 2 );
  717. L2 = p448_is_zero( &L0 );
  718. p448_sub ( &L0, &a->y, &b->y );
  719. p448_bias ( &L0, 2 );
  720. L1 = p448_is_zero( &L0 );
  721. return L2 & L1;
  722. }
  723. mask_t
  724. eq_extensible (
  725. const struct extensible_t* a,
  726. const struct extensible_t* b
  727. ) {
  728. mask_t L3, L4;
  729. struct p448_t L0, L1, L2;
  730. p448_mul ( &L2, &b->z, &a->x );
  731. p448_mul ( &L1, &a->z, &b->x );
  732. p448_sub ( &L0, &L2, &L1 );
  733. p448_bias ( &L0, 2 );
  734. L4 = p448_is_zero( &L0 );
  735. p448_mul ( &L2, &b->z, &a->y );
  736. p448_mul ( &L1, &a->z, &b->y );
  737. p448_sub ( &L0, &L2, &L1 );
  738. p448_bias ( &L0, 2 );
  739. L3 = p448_is_zero( &L0 );
  740. return L4 & L3;
  741. }
  742. mask_t
  743. eq_tw_extensible (
  744. const struct tw_extensible_t* a,
  745. const struct tw_extensible_t* b
  746. ) {
  747. mask_t L3, L4;
  748. struct p448_t L0, L1, L2;
  749. p448_mul ( &L2, &b->z, &a->x );
  750. p448_mul ( &L1, &a->z, &b->x );
  751. p448_sub ( &L0, &L2, &L1 );
  752. p448_bias ( &L0, 2 );
  753. L4 = p448_is_zero( &L0 );
  754. p448_mul ( &L2, &b->z, &a->y );
  755. p448_mul ( &L1, &a->z, &b->y );
  756. p448_sub ( &L0, &L2, &L1 );
  757. p448_bias ( &L0, 2 );
  758. L3 = p448_is_zero( &L0 );
  759. return L4 & L3;
  760. }
  761. void
  762. elligator_2s_inject (
  763. struct affine_t* a,
  764. const struct p448_t* r
  765. ) {
  766. mask_t L0, L1;
  767. struct p448_t L2, L3, L4, L5, L6, L7, L8;
  768. p448_sqr ( &a->x, r );
  769. p448_sqr ( &L3, &a->x );
  770. p448_copy ( &a->y, &L3 );
  771. p448_subw ( &a->y, 1 );
  772. p448_neg ( &L4, &a->y );
  773. p448_bias ( &L4, 2 );
  774. p448_weak_reduce( &L4 );
  775. p448_sqr ( &L2, &L4 );
  776. p448_mulw ( &L7, &L2, 1527402724 );
  777. p448_mulw ( &L8, &L3, 6108985600 );
  778. p448_add ( &a->y, &L8, &L7 );
  779. p448_weak_reduce( &a->y );
  780. p448_mulw ( &L8, &L2, 6109454568 );
  781. p448_sub ( &L7, &a->y, &L8 );
  782. p448_bias ( &L7, 2 );
  783. p448_weak_reduce( &L7 );
  784. p448_mulw ( &L6, &a->y, 78160 );
  785. p448_mul ( &L5, &L7, &L6 );
  786. p448_mul ( &L8, &L5, &L4 );
  787. p448_mul ( &L4, &L5, &L6 );
  788. p448_mul ( &L5, &L7, &L8 );
  789. p448_mul ( &L8, &L5, &L4 );
  790. p448_mul ( &L4, &L7, &L8 );
  791. p448_isr ( &L6, &L4 );
  792. p448_mul ( &L4, &L5, &L6 );
  793. p448_sqr ( &L5, &L6 );
  794. p448_mul ( &L6, &L8, &L5 );
  795. p448_mul ( &L8, &L7, &L6 );
  796. p448_mul ( &L7, &L8, &L6 );
  797. p448_copy ( &L6, &a->x );
  798. p448_subw ( &L6, 1 );
  799. p448_addw ( &a->x, 1 );
  800. p448_mul ( &L5, &a->x, &L8 );
  801. p448_sub ( &a->x, &L6, &L5 );
  802. p448_bias ( &a->x, 3 );
  803. p448_weak_reduce( &a->x );
  804. p448_mul ( &L5, &L4, &a->x );
  805. p448_mulw ( &L4, &L5, 78160 );
  806. p448_neg ( &a->x, &L4 );
  807. p448_bias ( &a->x, 2 );
  808. p448_weak_reduce( &a->x );
  809. p448_add ( &L4, &L3, &L3 );
  810. p448_add ( &L3, &L4, &L2 );
  811. p448_subw ( &L3, 2 );
  812. p448_bias ( &L3, 1 );
  813. p448_weak_reduce( &L3 );
  814. p448_mul ( &L2, &L3, &L8 );
  815. p448_mulw ( &L3, &L2, 3054649120 );
  816. p448_add ( &L2, &L3, &a->y );
  817. p448_mul ( &a->y, &L7, &L2 );
  818. L1 = p448_is_zero( &L8 );
  819. L0 = - L1;
  820. p448_addw ( &a->y, L0 );
  821. p448_weak_reduce( &a->y );
  822. }
  823. mask_t
  824. validate_affine (
  825. const struct affine_t* a
  826. ) {
  827. struct p448_t L0, L1, L2, L3;
  828. p448_sqr ( &L0, &a->y );
  829. p448_sqr ( &L2, &a->x );
  830. p448_add ( &L3, &L2, &L0 );
  831. p448_subw ( &L3, 1 );
  832. p448_mulw ( &L1, &L2, 39081 );
  833. p448_neg ( &L2, &L1 );
  834. p448_bias ( &L2, 2 );
  835. p448_mul ( &L1, &L0, &L2 );
  836. p448_sub ( &L0, &L3, &L1 );
  837. p448_bias ( &L0, 3 );
  838. return p448_is_zero( &L0 );
  839. }
  840. mask_t
  841. validate_tw_extensible (
  842. const struct tw_extensible_t* ext
  843. ) {
  844. mask_t L4, L5;
  845. struct p448_t L0, L1, L2, L3;
  846. /*
  847. * Check invariant:
  848. * 0 = -x*y + z*t*u
  849. */
  850. p448_mul ( &L1, &ext->t, &ext->u );
  851. p448_mul ( &L2, &ext->z, &L1 );
  852. p448_addw ( &L2, 0 );
  853. p448_mul ( &L0, &ext->x, &ext->y );
  854. p448_neg ( &L1, &L0 );
  855. p448_add ( &L0, &L1, &L2 );
  856. p448_bias ( &L0, 2 );
  857. L5 = p448_is_zero( &L0 );
  858. /*
  859. * Check invariant:
  860. * 0 = d*t^2*u^2 + x^2 - y^2 + z^2 - t^2*u^2
  861. */
  862. p448_sqr ( &L2, &ext->y );
  863. p448_neg ( &L1, &L2 );
  864. p448_addw ( &L1, 0 );
  865. p448_sqr ( &L0, &ext->x );
  866. p448_add ( &L2, &L0, &L1 );
  867. p448_sqr ( &L3, &ext->u );
  868. p448_sqr ( &L0, &ext->t );
  869. p448_mul ( &L1, &L0, &L3 );
  870. p448_mulw ( &L0, &L1, 39081 );
  871. p448_neg ( &L3, &L0 );
  872. p448_add ( &L0, &L3, &L2 );
  873. p448_neg ( &L3, &L1 );
  874. p448_add ( &L2, &L3, &L0 );
  875. p448_sqr ( &L1, &ext->z );
  876. p448_add ( &L0, &L1, &L2 );
  877. p448_bias ( &L0, 4 );
  878. L4 = p448_is_zero( &L0 );
  879. return L5 & L4;
  880. }
  881. mask_t
  882. validate_extensible (
  883. const struct extensible_t* ext
  884. ) {
  885. mask_t L4, L5;
  886. struct p448_t L0, L1, L2, L3;
  887. /*
  888. * Check invariant:
  889. * 0 = d*t^2*u^2 - x^2 - y^2 + z^2
  890. */
  891. p448_sqr ( &L2, &ext->y );
  892. p448_neg ( &L1, &L2 );
  893. p448_addw ( &L1, 0 );
  894. p448_sqr ( &L0, &ext->z );
  895. p448_add ( &L2, &L0, &L1 );
  896. p448_sqr ( &L3, &ext->u );
  897. p448_sqr ( &L0, &ext->t );
  898. p448_mul ( &L1, &L0, &L3 );
  899. p448_mulw ( &L3, &L1, 39081 );
  900. p448_neg ( &L0, &L3 );
  901. p448_add ( &L1, &L0, &L2 );
  902. p448_sqr ( &L0, &ext->x );
  903. p448_neg ( &L2, &L0 );
  904. p448_add ( &L0, &L2, &L1 );
  905. p448_bias ( &L0, 4 );
  906. L5 = p448_is_zero( &L0 );
  907. /*
  908. * Check invariant:
  909. * 0 = -x*y + z*t*u
  910. */
  911. p448_mul ( &L1, &ext->t, &ext->u );
  912. p448_mul ( &L2, &ext->z, &L1 );
  913. p448_addw ( &L2, 0 );
  914. p448_mul ( &L0, &ext->x, &ext->y );
  915. p448_neg ( &L1, &L0 );
  916. p448_add ( &L0, &L1, &L2 );
  917. p448_bias ( &L0, 2 );
  918. L4 = p448_is_zero( &L0 );
  919. return L5 & L4;
  920. }