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.
 
 
 
 
 

1581 lines
45 KiB

  1. /* Copyright (c) 2015 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. /**
  5. * @file decaf.c
  6. * @author Mike Hamburg
  7. * @brief Decaf high-level functions.
  8. */
  9. #define __STDC_WANT_LIB_EXT1__ 1 /* for memset_s */
  10. #include "decaf.h"
  11. #include <string.h>
  12. #include "field.h"
  13. #define WBITS DECAF_WORD_BITS
  14. #if WBITS == 64
  15. #define LBITS 56
  16. typedef __uint128_t decaf_dword_t;
  17. typedef __int128_t decaf_sdword_t;
  18. #define LIMB(x) (x##ull)
  19. #define SC_LIMB(x) (x##ull)
  20. #elif WBITS == 32
  21. typedef uint64_t decaf_dword_t;
  22. typedef int64_t decaf_sdword_t;
  23. #define LBITS 28
  24. #define LIMB(x) (x##ull)&((1ull<<LBITS)-1), (x##ull)>>LBITS
  25. #define SC_LIMB(x) (x##ull)&((1ull<<32)-1), (x##ull)>>32
  26. #else
  27. #error "Only supporting 32- and 64-bit platforms right now"
  28. #endif
  29. //static const int QUADRATIC_NONRESIDUE = -1;
  30. #define sv static void
  31. #define snv static void __attribute__((noinline))
  32. #define siv static inline void __attribute__((always_inline))
  33. static const gf ZERO = {{{0}}}, ONE = {{{1}}}, TWO = {{{2}}};
  34. #define LMASK ((((decaf_word_t)1)<<LBITS)-1)
  35. #if WBITS == 64
  36. static const gf P = {{{ LMASK, LMASK, LMASK, LMASK, LMASK-1, LMASK, LMASK, LMASK }}};
  37. #else
  38. static const gf P = {{{ LMASK, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK,
  39. LMASK-1, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK }}};
  40. #endif
  41. static const int EDWARDS_D = -39081;
  42. const decaf_448_scalar_t decaf_448_scalar_p = {{{
  43. SC_LIMB(0x2378c292ab5844f3),
  44. SC_LIMB(0x216cc2728dc58f55),
  45. SC_LIMB(0xc44edb49aed63690),
  46. SC_LIMB(0xffffffff7cca23e9),
  47. SC_LIMB(0xffffffffffffffff),
  48. SC_LIMB(0xffffffffffffffff),
  49. SC_LIMB(0x3fffffffffffffff)
  50. }}}, decaf_448_scalar_one = {{{1}}}, decaf_448_scalar_zero = {{{0}}};
  51. static const decaf_448_scalar_t decaf_448_scalar_r2 = {{{
  52. SC_LIMB(0xe3539257049b9b60),
  53. SC_LIMB(0x7af32c4bc1b195d9),
  54. SC_LIMB(0x0d66de2388ea1859),
  55. SC_LIMB(0xae17cf725ee4d838),
  56. SC_LIMB(0x1a9cc14ba3c47c44),
  57. SC_LIMB(0x2052bcb7e4d070af),
  58. SC_LIMB(0x3402a939f823b729)
  59. }}};
  60. static const decaf_448_scalar_t decaf_448_scalar_r1 = {{{
  61. SC_LIMB(0x721cf5b5529eec34),
  62. SC_LIMB(0x7a4cf635c8e9c2ab),
  63. SC_LIMB(0xeec492d944a725bf),
  64. SC_LIMB(0x000000020cd77058),
  65. SC_LIMB(0),
  66. SC_LIMB(0),
  67. SC_LIMB(0)
  68. }}};
  69. static const decaf_word_t DECAF_MONTGOMERY_FACTOR = (decaf_word_t)(0x3bd440fae918bc5ull);
  70. /** base = twist of Goldilocks base point (~,19). */
  71. const decaf_448_point_t decaf_448_point_base = {{
  72. {{{ LIMB(0xb39a2d57e08c7b),LIMB(0xb38639c75ff281),
  73. LIMB(0x2ec981082b3288),LIMB(0x99fe8607e5237c),
  74. LIMB(0x0e33fbb1fadd1f),LIMB(0xe714f67055eb4a),
  75. LIMB(0xc9ae06d64067dd),LIMB(0xf7be45054760fa) }}},
  76. {{{ LIMB(0xbd8715f551617f),LIMB(0x8c17fbeca8f5fc),
  77. LIMB(0xaae0eec209c06f),LIMB(0xce41ad80cbe6b8),
  78. LIMB(0xdf360b5c828c00),LIMB(0xaf25b6bbb40e3b),
  79. LIMB(0x8ed37f0ce4ed31),LIMB(0x72a1c3214557b9) }}},
  80. {{{ 1 }}},
  81. {{{ LIMB(0x97ca9c8ed8bde9),LIMB(0xf0b780da83304c),
  82. LIMB(0x0d79c0a7729a69),LIMB(0xc18d3f24aebc1c),
  83. LIMB(0x1fbb5389b3fda5),LIMB(0xbb24f674635948),
  84. LIMB(0x723a55709a3983),LIMB(0xe1c0107a823dd4) }}}
  85. }};
  86. /* Projective Niels coordinates */
  87. typedef struct { gf a, b, c; } niels_s, niels_t[1];
  88. typedef struct { niels_t n; gf z; } pniels_s, pniels_t[1];
  89. struct decaf_448_precomputed_s { niels_t table [5<<4]; /* MAGIC */ };
  90. extern const decaf_word_t decaf_448_precomputed_base_as_words[];
  91. const decaf_448_precomputed_s *decaf_448_precomputed_base =
  92. (const decaf_448_precomputed_s *) &decaf_448_precomputed_base_as_words;
  93. const size_t sizeof_decaf_448_precomputed_s = sizeof(decaf_448_precomputed_s);
  94. const size_t alignof_decaf_448_precomputed_s = 32;
  95. #ifdef __clang__
  96. #if 100*__clang_major__ + __clang_minor__ > 305
  97. #define VECTORIZE _Pragma("clang loop unroll(disable) vectorize(enable) vectorize_width(8)")
  98. #endif
  99. #endif
  100. #ifndef VECTORIZE
  101. #define VECTORIZE
  102. #endif
  103. #define FOR_LIMB(i,op) { unsigned int i=0; for (i=0; i<DECAF_448_LIMBS; i++) { op; }}
  104. #define FOR_LIMB_V(i,op) { unsigned int i=0; VECTORIZE for (i=0; i<DECAF_448_LIMBS; i++) { op; }}
  105. /** Copy x = y */
  106. siv gf_cpy(gf x, const gf y) { FOR_LIMB_V(i, x->limb[i] = y->limb[i]); }
  107. /** Mostly-unoptimized multiply, but at least it's unrolled. */
  108. siv gf_mul (gf c, const gf a, const gf b) {
  109. field_mul((field_t *)c, (const field_t *)a, (const field_t *)b);
  110. }
  111. /** Dedicated square */
  112. siv gf_sqr (gf c, const gf a) {
  113. field_sqr((field_t *)c, (const field_t *)a);
  114. }
  115. /** Inverse square root using addition chain. */
  116. siv gf_isqrt(gf y, const gf x) {
  117. field_isr((field_t *)y, (const field_t *)x);
  118. }
  119. /** Add mod p. Conservatively always weak-reduce. */
  120. snv gf_add ( gf_s *__restrict__ c, const gf a, const gf b ) {
  121. field_add((field_t *)c, (const field_t *)a, (const field_t *)b);
  122. }
  123. /** Subtract mod p. Conservatively always weak-reduce. */
  124. snv gf_sub ( gf c, const gf a, const gf b ) {
  125. field_sub((field_t *)c, (const field_t *)a, (const field_t *)b);
  126. }
  127. /** Add mod p. Conservatively always weak-reduce.) */
  128. siv gf_bias ( gf c, int amt) {
  129. field_bias((field_t *)c, amt);
  130. }
  131. /** Subtract mod p. Bias by 2 and don't reduce */
  132. siv gf_sub_nr ( gf_s *__restrict__ c, const gf a, const gf b ) {
  133. // FOR_LIMB_V(i, c->limb[i] = a->limb[i] - b->limb[i] + 2*P->limb[i] );
  134. ANALYZE_THIS_ROUTINE_CAREFULLY; //TODO
  135. field_sub_nr((field_t *)c, (const field_t *)a, (const field_t *)b);
  136. gf_bias(c, 2);
  137. }
  138. /** Subtract mod p. Bias by amt but don't reduce. */
  139. siv gf_sub_nr_x ( gf c, const gf a, const gf b, int amt ) {
  140. ANALYZE_THIS_ROUTINE_CAREFULLY; //TODO
  141. field_sub_nr((field_t *)c, (const field_t *)a, (const field_t *)b);
  142. gf_bias(c, amt);
  143. }
  144. /** Add mod p. Don't reduce. */
  145. siv gf_add_nr ( gf c, const gf a, const gf b ) {
  146. // FOR_LIMB_V(i, c->limb[i] = a->limb[i] + b->limb[i]);
  147. ANALYZE_THIS_ROUTINE_CAREFULLY; //TODO
  148. field_add_nr((field_t *)c, (const field_t *)a, (const field_t *)b);
  149. }
  150. /** Constant time, x = is_z ? z : y */
  151. siv cond_sel(gf x, const gf y, const gf z, decaf_bool_t is_z) {
  152. big_register_t br_mask = br_set_to_mask(is_z);
  153. big_register_t *out = (big_register_t *)x;
  154. const big_register_t *y_ = (const big_register_t *)y, *z_ = (const big_register_t *)z;
  155. word_t k;
  156. for (k=0; k<sizeof(gf)/sizeof(big_register_t); k++) {
  157. out[k] = (~br_mask & y_[k]) | (br_mask & z_[k]);
  158. }
  159. /*
  160. constant_time_select(x,z,y,sizeof(gf),is_z);
  161. */
  162. }
  163. /** Constant time, if (neg) x=-x; */
  164. sv cond_neg(gf x, decaf_bool_t neg) {
  165. gf y;
  166. gf_sub(y,ZERO,x);
  167. cond_sel(x,x,y,neg);
  168. }
  169. /** Constant time, if (swap) (x,y) = (y,x); */
  170. siv cond_swap(gf x, gf_s *__restrict__ y, decaf_bool_t swap) {
  171. FOR_LIMB_V(i, {
  172. decaf_word_t s = (x->limb[i] ^ y->limb[i]) & swap;
  173. x->limb[i] ^= s;
  174. y->limb[i] ^= s;
  175. });
  176. }
  177. /**
  178. * Mul by signed int. Not constant-time WRT the sign of that int.
  179. * Just uses a full mul (PERF)
  180. */
  181. siv gf_mlw(gf c, const gf a, int w) {
  182. if (w>0) {
  183. field_mulw((field_t *)c, (const field_t *)a, w);
  184. } else {
  185. field_mulw((field_t *)c, (const field_t *)a, -w);
  186. gf_sub(c,ZERO,c);
  187. }
  188. }
  189. /** Canonicalize */
  190. siv gf_canon ( gf a ) {
  191. field_strong_reduce((field_t *)a);
  192. }
  193. /** Compare a==b */
  194. static decaf_word_t __attribute__((noinline)) gf_eq(const gf a, const gf b) {
  195. gf c;
  196. gf_sub(c,a,b);
  197. gf_canon(c);
  198. decaf_word_t ret=0;
  199. FOR_LIMB(i, ret |= c->limb[i] );
  200. /* Hope the compiler is too dumb to optimize this, thus noinline */
  201. return ((decaf_dword_t)ret - 1) >> WBITS;
  202. }
  203. /** Return high bit of x = low bit of 2x mod p */
  204. static decaf_word_t hibit(const gf x) {
  205. gf y;
  206. gf_add(y,x,x);
  207. gf_canon(y);
  208. return -(y->limb[0]&1);
  209. }
  210. /** Return high bit of x/2 = low bit of x mod p */
  211. static inline decaf_word_t lobit(gf x) {
  212. gf_canon(x);
  213. return -(x->limb[0]&1);
  214. }
  215. /* a = use_c ? c : b */
  216. sv decaf_448_cond_sel (
  217. decaf_448_point_t a,
  218. const decaf_448_point_t b,
  219. const decaf_448_point_t c,
  220. decaf_bool_t use_c
  221. ) {
  222. cond_sel(a->x, b->x, c->x, use_c);
  223. cond_sel(a->y, b->y, c->y, use_c);
  224. cond_sel(a->z, b->z, c->z, use_c);
  225. cond_sel(a->t, b->t, c->t, use_c);
  226. }
  227. /** {extra,accum} - sub +? p
  228. * Must have extra <= 1
  229. */
  230. snv decaf_448_subx(
  231. decaf_448_scalar_t out,
  232. const decaf_word_t accum[DECAF_448_SCALAR_LIMBS],
  233. const decaf_448_scalar_t sub,
  234. const decaf_448_scalar_t p,
  235. decaf_word_t extra
  236. ) {
  237. decaf_sdword_t chain = 0;
  238. unsigned int i;
  239. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  240. chain = (chain + accum[i]) - sub->limb[i];
  241. out->limb[i] = chain;
  242. chain >>= WBITS;
  243. }
  244. decaf_bool_t borrow = chain+extra; /* = 0 or -1 */
  245. chain = 0;
  246. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  247. chain = (chain + out->limb[i]) + (p->limb[i] & borrow);
  248. out->limb[i] = chain;
  249. chain >>= WBITS;
  250. }
  251. }
  252. snv decaf_448_montmul (
  253. decaf_448_scalar_t out,
  254. const decaf_448_scalar_t a,
  255. const decaf_448_scalar_t b,
  256. const decaf_448_scalar_t p,
  257. decaf_word_t montgomery_factor
  258. ) {
  259. unsigned int i,j;
  260. decaf_word_t accum[DECAF_448_SCALAR_LIMBS+1] = {0};
  261. decaf_word_t hi_carry = 0;
  262. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  263. decaf_word_t mand = a->limb[i];
  264. const decaf_word_t *mier = b->limb;
  265. decaf_dword_t chain = 0;
  266. for (j=0; j<DECAF_448_SCALAR_LIMBS; j++) {
  267. chain += ((decaf_dword_t)mand)*mier[j] + accum[j];
  268. accum[j] = chain;
  269. chain >>= WBITS;
  270. }
  271. accum[j] = chain;
  272. mand = accum[0] * montgomery_factor;
  273. chain = 0;
  274. mier = p->limb;
  275. for (j=0; j<DECAF_448_SCALAR_LIMBS; j++) {
  276. chain += (decaf_dword_t)mand*mier[j] + accum[j];
  277. if (j) accum[j-1] = chain;
  278. chain >>= WBITS;
  279. }
  280. chain += accum[j];
  281. chain += hi_carry;
  282. accum[j-1] = chain;
  283. hi_carry = chain >> WBITS;
  284. }
  285. decaf_448_subx(out, accum, p, p, hi_carry);
  286. }
  287. void decaf_448_scalar_mul (
  288. decaf_448_scalar_t out,
  289. const decaf_448_scalar_t a,
  290. const decaf_448_scalar_t b
  291. ) {
  292. decaf_448_montmul(out,a,b,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  293. decaf_448_montmul(out,out,decaf_448_scalar_r2,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  294. }
  295. /* PERF: could implement this */
  296. siv decaf_448_montsqr (
  297. decaf_448_scalar_t out,
  298. const decaf_448_scalar_t a
  299. ) {
  300. decaf_448_montmul(out,a,a,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  301. }
  302. decaf_bool_t decaf_448_scalar_invert (
  303. decaf_448_scalar_t out,
  304. const decaf_448_scalar_t a
  305. ) {
  306. decaf_448_scalar_t chain[7], tmp;
  307. decaf_448_montmul(chain[0],a,decaf_448_scalar_r2,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  308. unsigned int i,j;
  309. /* Addition chain generated by a not-too-clever SAGE script. First part: compute a^(2^222-1) */
  310. struct { uint8_t widx, sidx, sct, midx; } muls [] = {
  311. {2,0,1,0}, {3,2,1,0}, {4,3,1,0}, {5,4,1,0}, /* 0x3,7,f,1f */
  312. {1,5,1,0}, {1,1,3,3}, {6,1,9,1}, {1,6,1,0}, {6,1,18,6}, /* a^(2^37-1) */
  313. {1,6,37,6}, {1,1,37,6}, {1,1,111,1} /* a^(2^222-1) */
  314. };
  315. /* Second part: sliding window */
  316. struct { uint8_t sct, midx; } muls1 [] = {
  317. {6, 5}, {4, 2}, {3, 0}, {2, 0}, {4, 0}, {8, 5},
  318. {2, 0}, {5, 3}, {4, 0}, {4, 0}, {5, 3}, {3, 2},
  319. {3, 2}, {3, 2}, {2, 0}, {3, 0}, {4, 2}, {2, 0},
  320. {4, 3}, {3, 2}, {2, 0}, {3, 2}, {5, 2}, {3, 2},
  321. {2, 0}, {3, 0}, {7, 0}, {5, 0}, {3, 2}, {3, 2},
  322. {4, 2}, {5, 0}, {5, 3}, {3, 0}, {2, 0}, {5, 2},
  323. {4, 3}, {4, 0}, {3, 2}, {7, 4}, {2, 0}, {2, 0},
  324. {2, 0}, {2, 0}, {3, 0}, {5, 2}, {5, 4}, {5, 2},
  325. {5, 0}, {2, 0}, {3, 0}, {3, 0}, {2, 0}, {2, 0},
  326. {2, 0}, {3, 2}, {2, 0}, {3, 2}, {5, 0}, {4, 0},
  327. {6, 4}, {4, 0}
  328. };
  329. for (i=0; i<sizeof(muls)/sizeof(muls[0]); i++) {
  330. decaf_448_montsqr(tmp, chain[muls[i].sidx]);
  331. for (j=1; j<muls[i].sct; j++) {
  332. decaf_448_montsqr(tmp, tmp);
  333. }
  334. decaf_448_montmul(chain[muls[i].widx], tmp, chain[muls[i].midx], decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  335. }
  336. for (i=0; i<sizeof(muls1)/sizeof(muls1[0]); i++) {
  337. decaf_448_montsqr(tmp, chain[1]);
  338. for (j=1; j<muls1[i].sct; j++) {
  339. decaf_448_montsqr(tmp, tmp);
  340. }
  341. decaf_448_montmul(chain[1], tmp, chain[muls1[i].midx], decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  342. }
  343. decaf_448_montmul(out,chain[1],decaf_448_scalar_one,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  344. for (i=0; i<sizeof(chain)/sizeof(chain[0]); i++) {
  345. decaf_448_scalar_destroy(chain[i]);
  346. }
  347. return ~decaf_448_scalar_eq(out,decaf_448_scalar_zero);
  348. }
  349. void decaf_448_scalar_sub (
  350. decaf_448_scalar_t out,
  351. const decaf_448_scalar_t a,
  352. const decaf_448_scalar_t b
  353. ) {
  354. decaf_448_subx(out, a->limb, b, decaf_448_scalar_p, 0);
  355. }
  356. void decaf_448_scalar_add (
  357. decaf_448_scalar_t out,
  358. const decaf_448_scalar_t a,
  359. const decaf_448_scalar_t b
  360. ) {
  361. decaf_dword_t chain = 0;
  362. unsigned int i;
  363. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  364. chain = (chain + a->limb[i]) + b->limb[i];
  365. out->limb[i] = chain;
  366. chain >>= WBITS;
  367. }
  368. decaf_448_subx(out, out->limb, decaf_448_scalar_p, decaf_448_scalar_p, chain);
  369. }
  370. snv decaf_448_halve (
  371. decaf_448_scalar_t out,
  372. const decaf_448_scalar_t a,
  373. const decaf_448_scalar_t p
  374. ) {
  375. decaf_word_t mask = -(a->limb[0] & 1);
  376. decaf_dword_t chain = 0;
  377. unsigned int i;
  378. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  379. chain = (chain + a->limb[i]) + (p->limb[i] & mask);
  380. out->limb[i] = chain;
  381. chain >>= WBITS;
  382. }
  383. for (i=0; i<DECAF_448_SCALAR_LIMBS-1; i++) {
  384. out->limb[i] = out->limb[i]>>1 | out->limb[i+1]<<(WBITS-1);
  385. }
  386. out->limb[i] = out->limb[i]>>1 | chain<<(WBITS-1);
  387. }
  388. void decaf_448_scalar_copy (
  389. decaf_448_scalar_t out,
  390. const decaf_448_scalar_t a
  391. ) {
  392. unsigned int i;
  393. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  394. out->limb[i] = a->limb[i];
  395. }
  396. }
  397. decaf_bool_t decaf_448_scalar_eq (
  398. const decaf_448_scalar_t a,
  399. const decaf_448_scalar_t b
  400. ) {
  401. decaf_word_t diff = 0;
  402. unsigned int i;
  403. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  404. diff |= a->limb[i] ^ b->limb[i];
  405. }
  406. return (((decaf_dword_t)diff)-1)>>WBITS;
  407. }
  408. /* *** API begins here *** */
  409. /** identity = (0,1) */
  410. const decaf_448_point_t decaf_448_point_identity = {{{{{0}}},{{{1}}},{{{1}}},{{{0}}}}};
  411. static void gf_encode ( unsigned char ser[DECAF_448_SER_BYTES], gf a ) {
  412. gf_canon(a);
  413. int i, k=0, bits=0;
  414. decaf_dword_t buf=0;
  415. for (i=0; i<DECAF_448_LIMBS; i++) {
  416. buf |= (decaf_dword_t)a->limb[i]<<bits;
  417. for (bits += LBITS; (bits>=8 || i==DECAF_448_LIMBS-1) && k<DECAF_448_SER_BYTES; bits-=8, buf>>=8) {
  418. ser[k++]=buf;
  419. }
  420. }
  421. }
  422. void decaf_448_point_encode( unsigned char ser[DECAF_448_SER_BYTES], const decaf_448_point_t p ) {
  423. /* Can shave off one mul here; not important but makes consistent with paper */
  424. gf a, b, c, d;
  425. gf_mlw ( a, p->y, 1-EDWARDS_D );
  426. gf_mul ( c, a, p->t );
  427. gf_mul ( a, p->x, p->z );
  428. gf_sub ( d, c, a );
  429. gf_add ( a, p->z, p->y );
  430. gf_sub ( b, p->z, p->y );
  431. gf_mul ( c, b, a );
  432. gf_mlw ( b, c, -EDWARDS_D );
  433. gf_isqrt ( a, b );
  434. gf_mlw ( b, a, -EDWARDS_D );
  435. gf_mul ( c, b, a );
  436. gf_mul ( a, c, d );
  437. gf_add ( d, b, b );
  438. gf_mul ( c, d, p->z );
  439. cond_neg ( b, ~hibit(c) );
  440. gf_mul ( c, b, p->y );
  441. gf_add ( a, a, c );
  442. cond_neg ( a, hibit(a) );
  443. gf_encode(ser, a);
  444. }
  445. /**
  446. * Deserialize a bool, return TRUE if < p.
  447. */
  448. static decaf_bool_t gf_deser(gf s, const unsigned char ser[DECAF_448_SER_BYTES]) {
  449. unsigned int i, k=0, bits=0;
  450. decaf_dword_t buf=0;
  451. for (i=0; i<DECAF_448_SER_BYTES; i++) {
  452. buf |= (decaf_dword_t)ser[i]<<bits;
  453. for (bits += 8; (bits>=LBITS || i==DECAF_448_SER_BYTES-1) && k<DECAF_448_LIMBS; bits-=LBITS, buf>>=LBITS) {
  454. s->limb[k++] = buf & LMASK;
  455. }
  456. }
  457. decaf_sdword_t accum = 0;
  458. FOR_LIMB(i, accum = (accum + s->limb[i] - P->limb[i]) >> WBITS );
  459. return accum;
  460. }
  461. /* Constant-time add or subtract */
  462. sv decaf_448_point_add_sub (
  463. decaf_448_point_t p,
  464. const decaf_448_point_t q,
  465. const decaf_448_point_t r,
  466. decaf_bool_t do_sub
  467. ) {
  468. /* Twisted Edward formulas, complete when 4-torsion isn't involved */
  469. gf a, b, c, d;
  470. gf_sub_nr ( b, q->y, q->x );
  471. gf_sub_nr ( c, r->y, r->x );
  472. gf_add_nr ( d, r->y, r->x );
  473. cond_swap(c,d,do_sub);
  474. gf_mul ( a, c, b );
  475. gf_add_nr ( b, q->y, q->x );
  476. gf_mul ( p->y, d, b );
  477. gf_mul ( b, r->t, q->t );
  478. gf_mlw ( p->x, b, 2-2*EDWARDS_D );
  479. gf_add_nr ( b, a, p->y );
  480. gf_sub_nr ( c, p->y, a );
  481. gf_mul ( a, q->z, r->z );
  482. gf_add_nr ( a, a, a );
  483. gf_add_nr ( p->y, a, p->x );
  484. gf_sub_nr ( a, a, p->x );
  485. cond_swap(a,p->y,do_sub);
  486. gf_mul ( p->z, a, p->y );
  487. gf_mul ( p->x, p->y, c );
  488. gf_mul ( p->y, a, b );
  489. gf_mul ( p->t, b, c );
  490. }
  491. decaf_bool_t decaf_448_point_decode (
  492. decaf_448_point_t p,
  493. const unsigned char ser[DECAF_448_SER_BYTES],
  494. decaf_bool_t allow_identity
  495. ) {
  496. gf s, a, b, c, d, e;
  497. decaf_bool_t succ = gf_deser(s, ser), zero = gf_eq(s, ZERO);
  498. succ &= allow_identity | ~zero;
  499. succ &= ~hibit(s);
  500. gf_sqr ( a, s );
  501. gf_sub ( p->z, ONE, a );
  502. gf_sqr ( b, p->z );
  503. gf_mlw ( c, a, 4-4*EDWARDS_D );
  504. gf_add ( c, c, b );
  505. gf_mul ( b, c, a );
  506. gf_isqrt ( d, b );
  507. gf_sqr ( e, d );
  508. gf_mul ( a, e, b );
  509. gf_add ( a, a, ONE );
  510. succ &= ~gf_eq ( a, ZERO );
  511. gf_mul ( b, c, d );
  512. cond_neg ( d, hibit(b) );
  513. gf_add ( p->x, s, s );
  514. gf_mul ( c, d, s );
  515. gf_sub ( b, TWO, p->z );
  516. gf_mul ( a, b, c );
  517. gf_mul ( p->y,a,p->z );
  518. gf_mul ( p->t,p->x,a );
  519. p->y->limb[0] -= zero;
  520. /* TODO: do something safe if ~succ? */
  521. return succ;
  522. }
  523. void decaf_448_point_sub (
  524. decaf_448_point_t p,
  525. const decaf_448_point_t q,
  526. const decaf_448_point_t r
  527. ) {
  528. gf a, b, c, d;
  529. gf_sub_nr ( b, q->y, q->x );
  530. gf_sub_nr ( d, r->y, r->x );
  531. gf_add_nr ( c, r->y, r->x );
  532. gf_mul ( a, c, b );
  533. gf_add_nr ( b, q->y, q->x );
  534. gf_mul ( p->y, d, b );
  535. gf_mul ( b, r->t, q->t );
  536. gf_mlw ( p->x, b, 2-2*EDWARDS_D );
  537. gf_add_nr ( b, a, p->y );
  538. gf_sub_nr ( c, p->y, a );
  539. gf_mul ( a, q->z, r->z );
  540. gf_add_nr ( a, a, a );
  541. gf_sub_nr ( p->y, a, p->x );
  542. gf_add_nr ( a, a, p->x );
  543. gf_mul ( p->z, a, p->y );
  544. gf_mul ( p->x, p->y, c );
  545. gf_mul ( p->y, a, b );
  546. gf_mul ( p->t, b, c );
  547. }
  548. void decaf_448_point_add (
  549. decaf_448_point_t p,
  550. const decaf_448_point_t q,
  551. const decaf_448_point_t r
  552. ) {
  553. gf a, b, c, d;
  554. gf_sub_nr ( b, q->y, q->x );
  555. gf_sub_nr ( c, r->y, r->x );
  556. gf_add_nr ( d, r->y, r->x );
  557. gf_mul ( a, c, b );
  558. gf_add_nr ( b, q->y, q->x );
  559. gf_mul ( p->y, d, b );
  560. gf_mul ( b, r->t, q->t );
  561. gf_mlw ( p->x, b, 2-2*EDWARDS_D );
  562. gf_add_nr ( b, a, p->y );
  563. gf_sub_nr ( c, p->y, a );
  564. gf_mul ( a, q->z, r->z );
  565. gf_add_nr ( a, a, a );
  566. gf_add_nr ( p->y, a, p->x );
  567. gf_sub_nr ( a, a, p->x );
  568. gf_mul ( p->z, a, p->y );
  569. gf_mul ( p->x, p->y, c );
  570. gf_mul ( p->y, a, b );
  571. gf_mul ( p->t, b, c );
  572. }
  573. snv decaf_448_point_double_internal (
  574. decaf_448_point_t p,
  575. const decaf_448_point_t q,
  576. decaf_bool_t before_double
  577. ) {
  578. gf a, b, c, d;
  579. gf_sqr ( c, q->x );
  580. gf_sqr ( a, q->y );
  581. gf_add_nr ( d, c, a );
  582. gf_add_nr ( p->t, q->y, q->x );
  583. gf_sqr ( b, p->t );
  584. gf_sub_nr_x ( b, b, d, 3 );
  585. gf_sub_nr ( p->t, a, c );
  586. gf_sqr ( p->x, q->z );
  587. gf_add_nr ( p->z, p->x, p->x );
  588. gf_sub_nr_x ( a, p->z, p->t, 4 );
  589. gf_mul ( p->x, a, b );
  590. gf_mul ( p->z, p->t, a );
  591. gf_mul ( p->y, p->t, d );
  592. if (!before_double) gf_mul ( p->t, b, d );
  593. }
  594. void decaf_448_point_double(decaf_448_point_t p, const decaf_448_point_t q) {
  595. decaf_448_point_double_internal(p,q,0);
  596. }
  597. void decaf_448_point_copy (
  598. decaf_448_point_t a,
  599. const decaf_448_point_t b
  600. ) {
  601. gf_cpy(a->x, b->x);
  602. gf_cpy(a->y, b->y);
  603. gf_cpy(a->z, b->z);
  604. gf_cpy(a->t, b->t);
  605. }
  606. siv decaf_448_scalar_decode_short (
  607. decaf_448_scalar_t s,
  608. const unsigned char ser[DECAF_448_SER_BYTES],
  609. unsigned int nbytes
  610. ) {
  611. unsigned int i,j,k=0;
  612. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  613. decaf_word_t out = 0;
  614. for (j=0; j<sizeof(decaf_word_t) && k<nbytes; j++,k++) {
  615. out |= ((decaf_word_t)ser[k])<<(8*j);
  616. }
  617. s->limb[i] = out;
  618. }
  619. }
  620. decaf_bool_t decaf_448_scalar_decode(
  621. decaf_448_scalar_t s,
  622. const unsigned char ser[DECAF_448_SER_BYTES]
  623. ) {
  624. unsigned int i;
  625. decaf_448_scalar_decode_short(s, ser, DECAF_448_SER_BYTES);
  626. decaf_sdword_t accum = 0;
  627. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  628. accum = (accum + s->limb[i] - decaf_448_scalar_p->limb[i]) >> WBITS;
  629. }
  630. decaf_448_montmul(s,s,decaf_448_scalar_r1,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR); /* ham-handed reduce */
  631. return accum;
  632. }
  633. void decaf_bzero (
  634. void *s,
  635. size_t size
  636. ) {
  637. #ifdef __STDC_LIB_EXT1__
  638. memset_s(s, size, 0, size);
  639. #else
  640. volatile uint8_t *destroy = (volatile uint8_t *)s;
  641. unsigned i;
  642. for (i=0; i<size; i++) {
  643. destroy[i] = 0;
  644. }
  645. #endif
  646. }
  647. void decaf_448_scalar_destroy (
  648. decaf_448_scalar_t scalar
  649. ) {
  650. decaf_bzero(scalar, sizeof(decaf_448_scalar_t));
  651. }
  652. static inline void ignore_result ( decaf_bool_t boo ) {
  653. (void)boo;
  654. }
  655. void decaf_448_scalar_decode_long(
  656. decaf_448_scalar_t s,
  657. const unsigned char *ser,
  658. size_t ser_len
  659. ) {
  660. if (ser_len == 0) {
  661. decaf_448_scalar_copy(s, decaf_448_scalar_zero);
  662. return;
  663. }
  664. size_t i;
  665. decaf_448_scalar_t t1, t2;
  666. i = ser_len - (ser_len%DECAF_448_SER_BYTES);
  667. if (i==ser_len) i -= DECAF_448_SER_BYTES;
  668. decaf_448_scalar_decode_short(t1, &ser[i], ser_len-i);
  669. if (ser_len == sizeof(*ser)) {
  670. assert(i==0);
  671. /* ham-handed reduce */
  672. decaf_448_montmul(s,t1,decaf_448_scalar_r1,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  673. decaf_448_scalar_destroy(t1);
  674. return;
  675. }
  676. while (i) {
  677. i -= DECAF_448_SER_BYTES;
  678. decaf_448_montmul(t1,t1,decaf_448_scalar_r2,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  679. ignore_result( decaf_448_scalar_decode(t2, ser+i) );
  680. decaf_448_scalar_add(t1, t1, t2);
  681. }
  682. decaf_448_scalar_copy(s, t1);
  683. decaf_448_scalar_destroy(t1);
  684. decaf_448_scalar_destroy(t2);
  685. }
  686. void decaf_448_scalar_encode(
  687. unsigned char ser[DECAF_448_SER_BYTES],
  688. const decaf_448_scalar_t s
  689. ) {
  690. unsigned int i,j,k=0;
  691. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  692. for (j=0; j<sizeof(decaf_word_t); j++,k++) {
  693. ser[k] = s->limb[i] >> (8*j);
  694. }
  695. }
  696. }
  697. /* Operations on [p]niels */
  698. siv cond_neg_niels (
  699. niels_t n,
  700. decaf_bool_t neg
  701. ) {
  702. cond_swap(n->a, n->b, neg);
  703. cond_neg(n->c, neg);
  704. }
  705. static void pt_to_pniels (
  706. pniels_t b,
  707. const decaf_448_point_t a
  708. ) {
  709. gf_sub ( b->n->a, a->y, a->x );
  710. gf_add ( b->n->b, a->x, a->y );
  711. gf_mlw ( b->n->c, a->t, 2*EDWARDS_D-2 );
  712. gf_add ( b->z, a->z, a->z );
  713. }
  714. static void pniels_to_pt (
  715. decaf_448_point_t e,
  716. const pniels_t d
  717. ) {
  718. gf eu;
  719. gf_add ( eu, d->n->b, d->n->a );
  720. gf_sub ( e->y, d->n->b, d->n->a );
  721. gf_mul ( e->t, e->y, eu);
  722. gf_mul ( e->x, d->z, e->y );
  723. gf_mul ( e->y, d->z, eu );
  724. gf_sqr ( e->z, d->z );
  725. }
  726. snv niels_to_pt (
  727. decaf_448_point_t e,
  728. const niels_t n
  729. ) {
  730. gf_add ( e->y, n->b, n->a );
  731. gf_sub ( e->x, n->b, n->a );
  732. gf_mul ( e->t, e->y, e->x );
  733. gf_cpy ( e->z, ONE );
  734. }
  735. snv add_niels_to_pt (
  736. decaf_448_point_t d,
  737. const niels_t e,
  738. decaf_bool_t before_double
  739. ) {
  740. gf a, b, c;
  741. gf_sub_nr ( b, d->y, d->x );
  742. gf_mul ( a, e->a, b );
  743. gf_add_nr ( b, d->x, d->y );
  744. gf_mul ( d->y, e->b, b );
  745. gf_mul ( d->x, e->c, d->t );
  746. gf_add_nr ( c, a, d->y );
  747. gf_sub_nr ( b, d->y, a );
  748. gf_sub_nr ( d->y, d->z, d->x );
  749. gf_add_nr ( a, d->x, d->z );
  750. gf_mul ( d->z, a, d->y );
  751. gf_mul ( d->x, d->y, b );
  752. gf_mul ( d->y, a, c );
  753. if (!before_double) gf_mul ( d->t, b, c );
  754. }
  755. snv sub_niels_from_pt (
  756. decaf_448_point_t d,
  757. const niels_t e,
  758. decaf_bool_t before_double
  759. ) {
  760. gf a, b, c;
  761. gf_sub_nr ( b, d->y, d->x );
  762. gf_mul ( a, e->b, b );
  763. gf_add_nr ( b, d->x, d->y );
  764. gf_mul ( d->y, e->a, b );
  765. gf_mul ( d->x, e->c, d->t );
  766. gf_add_nr ( c, a, d->y );
  767. gf_sub_nr ( b, d->y, a );
  768. gf_add_nr ( d->y, d->z, d->x );
  769. gf_sub_nr ( a, d->z, d->x );
  770. gf_mul ( d->z, a, d->y );
  771. gf_mul ( d->x, d->y, b );
  772. gf_mul ( d->y, a, c );
  773. if (!before_double) gf_mul ( d->t, b, c );
  774. }
  775. sv add_pniels_to_pt (
  776. decaf_448_point_t p,
  777. const pniels_t pn,
  778. decaf_bool_t before_double
  779. ) {
  780. gf L0;
  781. gf_mul ( L0, p->z, pn->z );
  782. gf_cpy ( p->z, L0 );
  783. add_niels_to_pt( p, pn->n, before_double );
  784. }
  785. sv sub_pniels_from_pt (
  786. decaf_448_point_t p,
  787. const pniels_t pn,
  788. decaf_bool_t before_double
  789. ) {
  790. gf L0;
  791. gf_mul ( L0, p->z, pn->z );
  792. gf_cpy ( p->z, L0 );
  793. sub_niels_from_pt( p, pn->n, before_double );
  794. }
  795. extern const decaf_448_scalar_t decaf_448_point_scalarmul_adjustment;
  796. /* TODO: get rid of big_register_t dependencies? */
  797. siv constant_time_lookup_xx (
  798. void *__restrict__ out_,
  799. const void *table_,
  800. word_t elem_bytes,
  801. word_t n_table,
  802. word_t idx
  803. ) {
  804. big_register_t big_one = br_set_to_mask(1), big_i = br_set_to_mask(idx);
  805. big_register_t *out = (big_register_t *)out_;
  806. const unsigned char *table = (const unsigned char *)table_;
  807. word_t j,k;
  808. big_register_t br_mask = br_is_zero(big_i);
  809. for (k=0; k<elem_bytes/sizeof(big_register_t); k++)
  810. out[k] = br_mask & *(const big_register_t*)(&table[k*sizeof(big_register_t)]);
  811. big_i-=big_one;
  812. for (j=1; j<n_table; j++, big_i-=big_one) {
  813. br_mask = br_is_zero(big_i);
  814. for (k=0; k<elem_bytes/sizeof(big_register_t); k++) {
  815. out[k] |= br_mask & *(const big_register_t*)(&table[k*sizeof(big_register_t)+j*elem_bytes]);
  816. }
  817. }
  818. }
  819. void decaf_448_point_scalarmul (
  820. decaf_448_point_t a,
  821. const decaf_448_point_t b,
  822. const decaf_448_scalar_t scalar
  823. ) {
  824. const int WINDOW = 5, /* PERF: Make 4 on non hugevector platforms? */
  825. WINDOW_MASK = (1<<WINDOW)-1,
  826. WINDOW_T_MASK = WINDOW_MASK >> 1,
  827. NTABLE = 1<<(WINDOW-1);
  828. decaf_448_scalar_t scalar2;
  829. decaf_448_scalar_add(scalar2, scalar, decaf_448_point_scalarmul_adjustment);
  830. decaf_448_halve(scalar2,scalar2,decaf_448_scalar_p);
  831. /* Set up a precomputed table with odd multiples of b. */
  832. pniels_t pn, multiples[NTABLE];
  833. decaf_448_point_t tmp;
  834. decaf_448_point_double(tmp, b);
  835. pt_to_pniels(pn, tmp);
  836. pt_to_pniels(multiples[0], b);
  837. decaf_448_point_copy(tmp, b);
  838. int i,j;
  839. for (i=1; i<NTABLE; i++) {
  840. add_pniels_to_pt(tmp, pn, 0);
  841. pt_to_pniels(multiples[i], tmp);
  842. }
  843. /* Initialize. */
  844. i = DECAF_448_SCALAR_BITS - ((DECAF_448_SCALAR_BITS-1) % WINDOW) - 1;
  845. int bits = scalar2->limb[i/WBITS] >> (i%WBITS) & WINDOW_MASK,
  846. inv = (bits>>(WINDOW-1))-1;
  847. bits ^= inv;
  848. constant_time_lookup_xx(pn, multiples, sizeof(pn), NTABLE, bits & WINDOW_T_MASK);
  849. cond_neg_niels(pn->n, inv);
  850. pniels_to_pt(tmp, pn);
  851. for (i-=WINDOW; i>=0; i-=WINDOW) {
  852. /* Using Hisil et al's lookahead method instead of extensible here
  853. * for no particular reason. Double WINDOW times, but only compute t on
  854. * the last one.
  855. */
  856. for (j=0; j<WINDOW-1; j++)
  857. decaf_448_point_double_internal(tmp, tmp, -1);
  858. decaf_448_point_double(tmp, tmp);
  859. /* Fetch another block of bits */
  860. bits = scalar2->limb[i/WBITS] >> (i%WBITS);
  861. if (i%WBITS >= WBITS-WINDOW) {
  862. bits ^= scalar2->limb[i/WBITS+1] << (WBITS - (i%WBITS));
  863. }
  864. bits &= WINDOW_MASK;
  865. inv = (bits>>(WINDOW-1))-1;
  866. bits ^= inv;
  867. /* Add in from table. Compute t only on last iteration. */
  868. constant_time_lookup_xx(pn, multiples, sizeof(pn), NTABLE, bits & WINDOW_T_MASK);
  869. cond_neg_niels(pn->n, inv);
  870. add_pniels_to_pt(tmp, pn, i ? -1 : 0);
  871. }
  872. /* Write out the answer */
  873. decaf_448_point_copy(a,tmp);
  874. }
  875. void decaf_448_point_double_scalarmul (
  876. decaf_448_point_t a,
  877. const decaf_448_point_t b,
  878. const decaf_448_scalar_t scalarb,
  879. const decaf_448_point_t c,
  880. const decaf_448_scalar_t scalarc
  881. ) {
  882. /* w=2 signed window uses about 1.5 adds per bit.
  883. * I figured a few extra lines was worth the 25% speedup.
  884. * NB: if adapting this function to scalarmul by a
  885. * possibly-odd number of unmasked bits, may need to mask.
  886. */
  887. decaf_448_point_t w,b3,c3,tmp;
  888. decaf_448_point_double(w,b);
  889. decaf_448_point_double(tmp,c);
  890. /* b3 = b*3 */
  891. decaf_448_point_add(b3,w,b);
  892. decaf_448_point_add(c3,tmp,c);
  893. decaf_448_point_add(w,w,tmp);
  894. int i;
  895. for (i=DECAF_448_SCALAR_BITS &~ 1; i>0; i-=2) {
  896. decaf_448_point_double(w,w);
  897. decaf_word_t bits = scalarb->limb[i/WBITS]>>(i%WBITS);
  898. decaf_448_cond_sel(tmp,b,b3,((bits^(bits>>1))&1)-1);
  899. decaf_448_point_add_sub(w,w,tmp,((bits>>1)&1)-1);
  900. bits = scalarc->limb[i/WBITS]>>(i%WBITS);
  901. decaf_448_cond_sel(tmp,c,c3,((bits^(bits>>1))&1)-1);
  902. decaf_448_point_add_sub(w,w,tmp,((bits>>1)&1)-1);
  903. decaf_448_point_double(w,w);
  904. }
  905. decaf_448_point_add_sub(w,w,b,((scalarb->limb[0]>>1)&1)-1);
  906. decaf_448_point_add_sub(w,w,c,((scalarc->limb[0]>>1)&1)-1);
  907. /* low bit is special because of signed window */
  908. decaf_448_cond_sel(tmp,b,decaf_448_point_identity,-(scalarb->limb[0]&1));
  909. decaf_448_point_sub(w,w,tmp);
  910. decaf_448_cond_sel(tmp,c,decaf_448_point_identity,-(scalarc->limb[0]&1));
  911. decaf_448_point_sub(a,w,tmp);
  912. }
  913. decaf_bool_t decaf_448_point_eq ( const decaf_448_point_t p, const decaf_448_point_t q ) {
  914. /* equality mod 2-torsion compares x/y */
  915. gf a, b;
  916. gf_mul ( a, p->y, q->x );
  917. gf_mul ( b, q->y, p->x );
  918. return gf_eq(a,b);
  919. }
  920. void decaf_448_point_from_hash_nonuniform (
  921. decaf_448_point_t p,
  922. const unsigned char ser[DECAF_448_SER_BYTES]
  923. ) {
  924. gf r,urr,a,b,c,dee,e,ur2_d,udr2_1;
  925. (void)gf_deser(r,ser);
  926. gf_canon(r);
  927. gf_sqr(a,r);
  928. /* gf_mlw(urr,a,QUADRATIC_NONRESIDUE); */
  929. gf_sub(urr,ZERO,a);
  930. gf_mlw(dee,ONE,EDWARDS_D);
  931. gf_add(a,urr,ONE);
  932. gf_sub(ur2_d,dee,urr);
  933. gf_mul(c,a,ur2_d);
  934. gf_mlw(b,urr,-EDWARDS_D);
  935. gf_add(udr2_1,b,ONE);
  936. gf_mul(a,c,udr2_1);
  937. gf_mlw(c,a,EDWARDS_D+1);
  938. gf_isqrt(b,c); /* FIELD: if 5 mod 8, multiply result by u. */
  939. gf_sqr(a,b);
  940. gf_mul(e,a,c);
  941. decaf_bool_t square = gf_eq(e,ONE);
  942. gf_mul(a,b,r);
  943. cond_sel(b,a,b,square);
  944. gf_mlw(a,b,EDWARDS_D+1);
  945. cond_swap(ur2_d,udr2_1,~square);
  946. gf_mul(e,ur2_d,a);
  947. cond_neg(e,hibit(e)^square);
  948. gf_mul(b,udr2_1,a);
  949. gf_sqr(c,b);
  950. gf_sqr(a,e);
  951. gf_sub(a,ONE,a);
  952. gf_add(e,e,e);
  953. gf_add(b,dee,c);
  954. gf_sub(c,dee,c);
  955. gf_mul(p->x,e,c);
  956. gf_mul(p->z,a,c);
  957. gf_mul(p->y,b,a);
  958. gf_mul(p->t,b,e);
  959. }
  960. void decaf_448_point_from_hash_uniform (
  961. decaf_448_point_t pt,
  962. const unsigned char hashed_data[2*DECAF_448_SER_BYTES]
  963. ) {
  964. decaf_448_point_t pt2;
  965. decaf_448_point_from_hash_nonuniform(pt,hashed_data);
  966. decaf_448_point_from_hash_nonuniform(pt2,&hashed_data[DECAF_448_SER_BYTES]);
  967. decaf_448_point_add(pt,pt,pt2);
  968. }
  969. decaf_bool_t decaf_448_point_valid (
  970. const decaf_448_point_t p
  971. ) {
  972. gf a,b,c;
  973. gf_mul(a,p->x,p->y);
  974. gf_mul(b,p->z,p->t);
  975. decaf_bool_t out = gf_eq(a,b);
  976. gf_sqr(a,p->x);
  977. gf_sqr(b,p->y);
  978. gf_sub(a,b,a);
  979. gf_sqr(b,p->t);
  980. gf_mlw(c,b,1-EDWARDS_D);
  981. gf_sqr(b,p->z);
  982. gf_sub(b,b,c);
  983. out &= gf_eq(a,b);
  984. out &= ~gf_eq(p->z,ZERO);
  985. return out;
  986. }
  987. static void gf_batch_invert (
  988. gf *__restrict__ out,
  989. /* const */ gf *in,
  990. unsigned int n
  991. ) {
  992. // if (n==0) {
  993. // return;
  994. // } else if (n==1) {
  995. // field_inverse(out[0],in[0]);
  996. // return;
  997. // }
  998. assert(n>1);
  999. gf_cpy(out[1], in[0]);
  1000. int i;
  1001. for (i=1; i<(int) (n-1); i++) {
  1002. gf_mul(out[i+1], out[i], in[i]);
  1003. }
  1004. gf_mul(out[0], out[n-1], in[n-1]);
  1005. gf t1, t2;
  1006. gf_isqrt(t1, out[0]);
  1007. gf_sqr(t2, t1);
  1008. gf_sqr(t1, t2);
  1009. gf_mul(t2, t1, out[0]);
  1010. gf_cpy(out[0], t2);
  1011. for (i=n-1; i>0; i--) {
  1012. gf_mul(t1, out[i], out[0]);
  1013. gf_cpy(out[i], t1);
  1014. gf_mul(t1, out[0], in[i]);
  1015. gf_cpy(out[0], t1);
  1016. }
  1017. }
  1018. static void batch_normalize_niels (
  1019. niels_t *table,
  1020. gf *zs,
  1021. gf *zis,
  1022. int n
  1023. ) {
  1024. int i;
  1025. gf product;
  1026. gf_batch_invert(zis, zs, n);
  1027. for (i=0; i<n; i++) {
  1028. gf_mul(product, table[i]->a, zis[i]);
  1029. gf_canon(product);
  1030. gf_cpy(table[i]->a, product);
  1031. gf_mul(product, table[i]->b, zis[i]);
  1032. gf_canon(product);
  1033. gf_cpy(table[i]->b, product);
  1034. gf_mul(product, table[i]->c, zis[i]);
  1035. gf_canon(product);
  1036. gf_cpy(table[i]->c, product);
  1037. }
  1038. }
  1039. void
  1040. decaf_448_precompute (
  1041. decaf_448_precomputed_s *table,
  1042. const decaf_448_point_t base
  1043. ) {
  1044. const unsigned int n = 5, t = 5, s = 18; // TODO MAGIC
  1045. assert(n*t*s >= DECAF_448_SCALAR_BITS);
  1046. decaf_448_point_t working, start, doubles[t-1];
  1047. decaf_448_point_copy(working, base);
  1048. pniels_t pn_tmp;
  1049. gf zs[n<<(t-1)], zis[n<<(t-1)];
  1050. unsigned int i,j,k;
  1051. /* Compute n tables */
  1052. for (i=0; i<n; i++) {
  1053. /* Doubling phase */
  1054. for (j=0; j<t; j++) {
  1055. if (j) decaf_448_point_add(start, start, working);
  1056. else decaf_448_point_copy(start, working);
  1057. if (j==t-1 && i==n-1) break;
  1058. decaf_448_point_double(working, working);
  1059. if (j<t-1) decaf_448_point_copy(doubles[j], working);
  1060. for (k=0; k<s-1; k++)
  1061. decaf_448_point_double_internal(working, working, k<s-2);
  1062. }
  1063. /* Gray-code phase */
  1064. for (j=0;; j++) {
  1065. int gray = j ^ (j>>1);
  1066. int idx = (((i+1)<<(t-1))-1) ^ gray;
  1067. pt_to_pniels(pn_tmp, start);
  1068. memcpy(table->table[idx], pn_tmp->n, sizeof(pn_tmp->n));
  1069. gf_cpy(zs[idx], pn_tmp->z);
  1070. if (j >= (1u<<(t-1)) - 1) break;
  1071. int delta = (j+1) ^ ((j+1)>>1) ^ gray;
  1072. for (k=0; delta>1; k++)
  1073. delta >>=1;
  1074. if (gray & (1<<k)) {
  1075. decaf_448_point_add(start, start, doubles[k]);
  1076. } else {
  1077. decaf_448_point_sub(start, start, doubles[k]);
  1078. }
  1079. }
  1080. }
  1081. batch_normalize_niels(table->table,zs,zis,n<<(t-1));
  1082. }
  1083. extern const decaf_448_scalar_t decaf_448_precomputed_scalarmul_adjustment;
  1084. siv constant_time_lookup_niels (
  1085. niels_s *__restrict__ ni,
  1086. const niels_t *table,
  1087. int nelts,
  1088. int idx
  1089. ) {
  1090. constant_time_lookup_xx(ni, table, sizeof(niels_s), nelts, idx);
  1091. }
  1092. void decaf_448_precomputed_scalarmul (
  1093. decaf_448_point_t out,
  1094. const decaf_448_precomputed_s *table,
  1095. const decaf_448_scalar_t scalar
  1096. ) {
  1097. int i;
  1098. unsigned j,k;
  1099. const unsigned int n = 5, t = 5, s = 18; // TODO MAGIC
  1100. decaf_448_scalar_t scalar1x;
  1101. decaf_448_scalar_add(scalar1x, scalar, decaf_448_precomputed_scalarmul_adjustment);
  1102. decaf_448_halve(scalar1x,scalar1x,decaf_448_scalar_p);
  1103. niels_t ni;
  1104. for (i=s-1; i>=0; i--) {
  1105. if (i != (int)s-1) decaf_448_point_double(out,out);
  1106. for (j=0; j<n; j++) {
  1107. int tab = 0;
  1108. for (k=0; k<t; k++) {
  1109. unsigned int bit = i + s*(k + j*t);
  1110. if (bit < SCALAR_WORDS * WBITS) {
  1111. tab |= (scalar1x->limb[bit/WBITS] >> (bit%WBITS) & 1) << k;
  1112. }
  1113. }
  1114. decaf_bool_t invert = (tab>>(t-1))-1;
  1115. tab ^= invert;
  1116. tab &= (1<<(t-1)) - 1;
  1117. constant_time_lookup_niels(ni, &table->table[j<<(t-1)], 1<<(t-1), tab);
  1118. cond_neg_niels(ni, invert);
  1119. if ((i!=s-1)||j) {
  1120. add_niels_to_pt(out, ni, j==n-1 && i);
  1121. } else {
  1122. niels_to_pt(out, ni);
  1123. }
  1124. }
  1125. }
  1126. }
  1127. decaf_bool_t decaf_448_direct_scalarmul (
  1128. uint8_t scaled[DECAF_448_SER_BYTES],
  1129. const uint8_t base[DECAF_448_SER_BYTES],
  1130. const decaf_448_scalar_t scalar,
  1131. decaf_bool_t allow_identity,
  1132. decaf_bool_t short_circuit
  1133. ) {
  1134. /* The Montgomery ladder does not short-circuit return on invalid points,
  1135. * since it detects them during recompress.
  1136. */
  1137. (void)short_circuit;
  1138. gf s0, x0, xa, za, xd, zd, xs, zs, L0, L1;
  1139. decaf_bool_t succ = gf_deser ( s0, base );
  1140. succ &= allow_identity |~ gf_eq( s0, ZERO);
  1141. /* Prepare the Montgomery ladder: Q = 1:0, P+Q = P */
  1142. gf_sqr ( xa, s0 );
  1143. gf_cpy ( x0, xa );
  1144. gf_cpy ( za, ONE );
  1145. gf_cpy ( xd, ONE );
  1146. gf_cpy ( zd, ZERO );
  1147. int j;
  1148. decaf_bool_t pflip = 0;
  1149. for (j=DECAF_448_SCALAR_BITS+1; j>=0; j--) {
  1150. /* FIXME: -1, but the test cases use too many bits */
  1151. /* Augmented Montgomery ladder */
  1152. decaf_bool_t flip = -((scalar->limb[j/WBITS]>>(j%WBITS))&1);
  1153. /* Differential add first... */
  1154. gf_add_nr ( xs, xa, za );
  1155. gf_sub_nr ( zs, xa, za );
  1156. gf_add_nr ( xa, xd, zd );
  1157. gf_sub_nr ( za, xd, zd );
  1158. cond_sel(L0,xa,xs,flip^pflip);
  1159. cond_sel(L1,za,zs,flip^pflip);
  1160. gf_mul ( xd, xa, zs );
  1161. gf_mul ( zd, xs, za );
  1162. gf_add_nr ( xs, xd, zd );
  1163. gf_sub_nr ( zd, xd, zd );
  1164. gf_mul ( zs, zd, s0 );
  1165. gf_sqr ( xa, xs );
  1166. gf_sqr ( za, zs );
  1167. /* ... and then double */
  1168. gf_sqr ( zd, L0 );
  1169. gf_sqr ( L0, L1 );
  1170. gf_sub_nr ( L1, zd, L0 );
  1171. gf_mul ( xd, L0, zd );
  1172. gf_mlw ( zd, L1, 1-EDWARDS_D );
  1173. gf_add_nr ( L0, L0, zd );
  1174. gf_mul ( zd, L0, L1 );
  1175. pflip = flip;
  1176. }
  1177. cond_swap(xa,xd,pflip);
  1178. cond_swap(za,zd,pflip);
  1179. /* OK, time to reserialize! Should be easy (heh, but seriously, TODO: simplify) */
  1180. gf xz_d, xz_a, xz_s, den, L2, L3;
  1181. mask_t zcase, output_zero, sflip, za_zero;
  1182. gf_mul(xz_s, xs, zs);
  1183. gf_mul(xz_d, xd, zd);
  1184. gf_mul(xz_a, xa, za);
  1185. output_zero = gf_eq(xz_d, ZERO);
  1186. xz_d->limb[0] -= output_zero; /* make xz_d always nonzero */
  1187. zcase = output_zero | gf_eq(xz_a, ZERO);
  1188. za_zero = gf_eq(za, ZERO);
  1189. /* Curve test in zcase, compute x0^2 + (2d-4)x0 + 1
  1190. * (we know that x0 = s0^2 is square).
  1191. */
  1192. gf_add(L0,x0,ONE);
  1193. gf_sqr(L1,L0);
  1194. gf_mlw(L0,x0,-4*EDWARDS_D);
  1195. gf_add(L1,L1,L0);
  1196. cond_sel(xz_a,xz_a,L1,zcase);
  1197. /* Compute denominator = x0 xa za xd zd */
  1198. gf_mul(L0, x0, xz_a);
  1199. gf_mul(L1, L0, xz_d);
  1200. gf_isqrt(den, L1);
  1201. /* Check that the square root came out OK. */
  1202. gf_sqr(L2, den);
  1203. gf_mul(L3, L0, L2); /* x0 xa za den^2 = 1/xz_d, for later */
  1204. gf_mul(L0, L1, L2);
  1205. gf_add(L0, L0, ONE);
  1206. succ &= ~hibit(s0) & ~gf_eq(L0, ZERO);
  1207. /* Compute y/x for input and output point. */
  1208. gf_mul(L1, x0, xd);
  1209. gf_sub(L1, zd, L1);
  1210. gf_mul(L0, za, L1); /* L0 = "opq" */
  1211. gf_mul(L1, x0, zd);
  1212. gf_sub(L1, L1, xd);
  1213. gf_mul(L2, xa, L1); /* L2 = "pqr" */
  1214. gf_sub(L1, L0, L2);
  1215. gf_add(L0, L0, L2);
  1216. gf_mul(L2, L1, den); /* L2 = y0 / x0 */
  1217. gf_mul(L1, L0, den); /* L1 = yO / xO */
  1218. sflip = (lobit(L1) ^ lobit(L2)) | za_zero;
  1219. /* OK, done with y-coordinates */
  1220. /* If xa==0 or za ==0: return 0
  1221. * Else if za == 0: return s0 * (sflip ? zd : xd)^2 * L3
  1222. * Else if zd == 0: return s0 * (sflip ? zd : xd)^2 * L3
  1223. * Else if pflip: return xs * zs * (sflip ? zd : xd) * L3
  1224. * Else: return s0 * xs * zs * (sflip ? zd : xd) * den
  1225. */
  1226. cond_sel(xd, xd, zd, sflip); /* xd = actual xd we care about */
  1227. cond_sel(den,den,L3,pflip|zcase);
  1228. cond_sel(xz_s,xz_s,xd,zcase);
  1229. cond_sel(s0,s0,ONE,pflip&~zcase);
  1230. cond_sel(s0,s0,ZERO,output_zero);
  1231. gf_mul(L0,xd,den);
  1232. gf_mul(L1,L0,s0);
  1233. gf_mul(L0,L1,xz_s);
  1234. cond_neg(L0,hibit(L0));
  1235. gf_encode(scaled, L0);
  1236. return succ;
  1237. }
  1238. /**
  1239. * @cond internal
  1240. * Control for variable-time scalar multiply algorithms.
  1241. */
  1242. struct smvt_control {
  1243. int power, addend;
  1244. };
  1245. static int recode_wnaf (
  1246. struct smvt_control *control, /* [nbits/(tableBits+1) + 3] */
  1247. const decaf_448_scalar_t scalar,
  1248. unsigned int tableBits
  1249. ) {
  1250. int current = 0, i, j;
  1251. unsigned int position = 0;
  1252. /* PERF: negate scalar if it's large
  1253. * PERF: this is a pretty simplistic algorithm. I'm sure there's a faster one...
  1254. */
  1255. for (i=DECAF_448_SCALAR_BITS-1; i >= 0; i--) {
  1256. int bit = (scalar->limb[i/WORD_BITS] >> (i%WORD_BITS)) & 1;
  1257. current = 2*current + bit;
  1258. /*
  1259. * Sizing: |current| >= 2^(tableBits+1) -> |current| = 2^0
  1260. * So current loses (tableBits+1) bits every time. It otherwise gains
  1261. * 1 bit per iteration. The number of iterations is
  1262. * (nbits + 2 + tableBits), and an additional control word is added at
  1263. * the end. So the total number of control words is at most
  1264. * ceil((nbits+1) / (tableBits+1)) + 2 = floor((nbits)/(tableBits+1)) + 2.
  1265. * There's also the stopper with power -1, for a total of +3.
  1266. */
  1267. if (current >= (2<<tableBits) || current <= -1 - (2<<tableBits)) {
  1268. int delta = (current + 1) >> 1; /* |delta| < 2^tablebits */
  1269. current = -(current & 1);
  1270. for (j=i; (delta & 1) == 0; j++) {
  1271. delta >>= 1;
  1272. }
  1273. control[position].power = j+1;
  1274. control[position].addend = delta;
  1275. position++;
  1276. assert(position <= DECAF_448_SCALAR_BITS/(tableBits+1) + 2);
  1277. }
  1278. }
  1279. if (current) {
  1280. for (j=0; (current & 1) == 0; j++) {
  1281. current >>= 1;
  1282. }
  1283. control[position].power = j;
  1284. control[position].addend = current;
  1285. position++;
  1286. assert(position <= DECAF_448_SCALAR_BITS/(tableBits+1) + 2);
  1287. }
  1288. control[position].power = -1;
  1289. control[position].addend = 0;
  1290. return position;
  1291. }
  1292. sv prepare_wnaf_table(
  1293. pniels_t *output,
  1294. const decaf_448_point_t working,
  1295. unsigned int tbits
  1296. ) {
  1297. decaf_448_point_t tmp;
  1298. int i;
  1299. pt_to_pniels(output[0], working);
  1300. if (tbits == 0) return;
  1301. decaf_448_point_double(tmp,working);
  1302. pniels_t twop;
  1303. pt_to_pniels(twop, tmp);
  1304. add_pniels_to_pt(tmp, output[0],0);
  1305. pt_to_pniels(output[1], tmp);
  1306. for (i=2; i < 1<<tbits; i++) {
  1307. add_pniels_to_pt(tmp, twop,0);
  1308. pt_to_pniels(output[i], tmp);
  1309. }
  1310. }
  1311. extern const decaf_word_t decaf_448_precomputed_wnaf_as_words[];
  1312. static const niels_t *decaf_448_wnaf_base = (const niels_t *)decaf_448_precomputed_wnaf_as_words;
  1313. const size_t sizeof_decaf_448_precomputed_wnafs __attribute((visibility("hidden"))) = sizeof(niels_t)<<5;
  1314. void decaf_448_precompute_wnafs (
  1315. niels_t out[1<<5],
  1316. const decaf_448_point_t base
  1317. ) __attribute__ ((visibility ("hidden")));
  1318. void decaf_448_precompute_wnafs (
  1319. niels_t out[1<<5],
  1320. const decaf_448_point_t base
  1321. ) {
  1322. // TODO MAGIC
  1323. pniels_t tmp[1<<5];
  1324. gf zs[1<<5], zis[1<<5];
  1325. int i;
  1326. prepare_wnaf_table(tmp,base,5);
  1327. for (i=0; i<1<<5; i++) {
  1328. memcpy(out[i], tmp[i]->n, sizeof(niels_t));
  1329. gf_cpy(zs[i], tmp[i]->z);
  1330. }
  1331. batch_normalize_niels(out, zs, zis, 1<<5);
  1332. }
  1333. void decaf_448_base_double_scalarmul_non_secret (
  1334. decaf_448_point_t combo,
  1335. const decaf_448_scalar_t scalar1,
  1336. const decaf_448_point_t base2,
  1337. const decaf_448_scalar_t scalar2
  1338. ) {
  1339. const int table_bits_var = 3, table_bits_pre = 5; // TODO MAGIC
  1340. struct smvt_control control_var[DECAF_448_SCALAR_BITS/(table_bits_var+1)+3];
  1341. struct smvt_control control_pre[DECAF_448_SCALAR_BITS/(table_bits_pre+1)+3];
  1342. int ncb_pre = recode_wnaf(control_pre, scalar1, table_bits_pre);
  1343. int ncb_var = recode_wnaf(control_var, scalar2, table_bits_var);
  1344. pniels_t precmp_var[1<<table_bits_var];
  1345. prepare_wnaf_table(precmp_var, base2, table_bits_var);
  1346. int contp=0, contv=0, i = control_var[0].power;
  1347. if (i < 0) {
  1348. decaf_448_point_copy(combo, decaf_448_point_identity);
  1349. return;
  1350. } else if (i > control_pre[0].power) {
  1351. pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
  1352. contv++;
  1353. } else if (i == control_pre[0].power && i >=0 ) {
  1354. pniels_to_pt(combo, precmp_var[control_var[0].addend >> 1]);
  1355. add_niels_to_pt(combo, decaf_448_wnaf_base[control_pre[0].addend >> 1], i);
  1356. contv++; contp++;
  1357. } else {
  1358. i = control_pre[0].power;
  1359. niels_to_pt(combo, decaf_448_wnaf_base[control_pre[0].addend >> 1]);
  1360. contp++;
  1361. }
  1362. for (i--; i >= 0; i--) {
  1363. int cv = (i==control_var[contv].power), cp = (i==control_pre[contp].power);
  1364. decaf_448_point_double_internal(combo,combo,i && !(cv||cp));
  1365. if (cv) {
  1366. assert(control_var[contv].addend);
  1367. if (control_var[contv].addend > 0) {
  1368. add_pniels_to_pt(combo, precmp_var[control_var[contv].addend >> 1], i&&!cp);
  1369. } else {
  1370. sub_pniels_from_pt(combo, precmp_var[(-control_var[contv].addend) >> 1], i&&!cp);
  1371. }
  1372. contv++;
  1373. }
  1374. if (cp) {
  1375. assert(control_pre[contp].addend);
  1376. if (control_pre[contp].addend > 0) {
  1377. add_niels_to_pt(combo, decaf_448_wnaf_base[control_pre[contp].addend >> 1], i);
  1378. } else {
  1379. sub_niels_from_pt(combo, decaf_448_wnaf_base[(-control_pre[contp].addend) >> 1], i);
  1380. }
  1381. contp++;
  1382. }
  1383. }
  1384. assert(contv == ncb_var); (void)ncb_var;
  1385. assert(contp == ncb_pre); (void)ncb_pre;
  1386. }