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.
 
 
 
 
 

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