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.
 
 
 
 
 

812 lines
23 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. typedef decaf_word_t gf[DECAF_448_LIMBS] __attribute__((aligned(32)));
  32. static const gf ZERO = {0}, ONE = {1}, TWO = {2};
  33. #define LMASK ((((decaf_word_t)1)<<LBITS)-1)
  34. #if WBITS == 64
  35. static const gf P = { LMASK, LMASK, LMASK, LMASK, LMASK-1, LMASK, LMASK, LMASK };
  36. #else
  37. static const gf P = { LMASK, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK,
  38. LMASK-1, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK, LMASK };
  39. #endif
  40. static const int EDWARDS_D = -39081;
  41. const decaf_448_scalar_t decaf_448_scalar_p = {{{
  42. SC_LIMB(0x2378c292ab5844f3),
  43. SC_LIMB(0x216cc2728dc58f55),
  44. SC_LIMB(0xc44edb49aed63690),
  45. SC_LIMB(0xffffffff7cca23e9),
  46. SC_LIMB(0xffffffffffffffff),
  47. SC_LIMB(0xffffffffffffffff),
  48. SC_LIMB(0x3fffffffffffffff)
  49. }}}, decaf_448_scalar_one = {{{1}}}, decaf_448_scalar_zero = {{{0}}};
  50. static const decaf_448_scalar_t decaf_448_scalar_r2 = {{{
  51. SC_LIMB(0xe3539257049b9b60),
  52. SC_LIMB(0x7af32c4bc1b195d9),
  53. SC_LIMB(0x0d66de2388ea1859),
  54. SC_LIMB(0xae17cf725ee4d838),
  55. SC_LIMB(0x1a9cc14ba3c47c44),
  56. SC_LIMB(0x2052bcb7e4d070af),
  57. SC_LIMB(0x3402a939f823b729)
  58. }}};
  59. static const decaf_word_t DECAF_MONTGOMERY_FACTOR = (decaf_word_t)(0x3bd440fae918bc5ull);
  60. /** base = twist of Goldilocks base point (~,19). */
  61. const decaf_448_point_t decaf_448_point_base = {{
  62. { LIMB(0xb39a2d57e08c7b),LIMB(0xb38639c75ff281),
  63. LIMB(0x2ec981082b3288),LIMB(0x99fe8607e5237c),
  64. LIMB(0x0e33fbb1fadd1f),LIMB(0xe714f67055eb4a),
  65. LIMB(0xc9ae06d64067dd),LIMB(0xf7be45054760fa) },
  66. { LIMB(0xbd8715f551617f),LIMB(0x8c17fbeca8f5fc),
  67. LIMB(0xaae0eec209c06f),LIMB(0xce41ad80cbe6b8),
  68. LIMB(0xdf360b5c828c00),LIMB(0xaf25b6bbb40e3b),
  69. LIMB(0x8ed37f0ce4ed31),LIMB(0x72a1c3214557b9) },
  70. { 1 },
  71. { LIMB(0x97ca9c8ed8bde9),LIMB(0xf0b780da83304c),
  72. LIMB(0x0d79c0a7729a69),LIMB(0xc18d3f24aebc1c),
  73. LIMB(0x1fbb5389b3fda5),LIMB(0xbb24f674635948),
  74. LIMB(0x723a55709a3983),LIMB(0xe1c0107a823dd4) }
  75. }};
  76. struct decaf_448_precomputed_s {
  77. decaf_448_point_t p[1];
  78. };
  79. const struct decaf_448_precomputed_s *decaf_448_precomputed_base =
  80. (const struct decaf_448_precomputed_s *)decaf_448_point_base;
  81. const size_t sizeof_decaf_448_precomputed_s = sizeof(struct decaf_448_precomputed_s);
  82. const size_t alignof_decaf_448_precomputed_s = 32;
  83. #if (defined(__OPTIMIZE__) && !defined(__OPTIMIZE_SIZE__)) || defined(DECAF_FORCE_UNROLL)
  84. #if DECAF_448_LIMBS==8
  85. #define FOR_LIMB(i,op) { unsigned int i=0; \
  86. op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; \
  87. }
  88. #elif DECAF_448_LIMBS==16
  89. #define FOR_LIMB(i,op) { unsigned int i=0; \
  90. op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; \
  91. op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; op;i++; \
  92. }
  93. #else
  94. #define FOR_LIMB(i,op) { unsigned int i=0; for (i=0; i<DECAF_448_LIMBS; i++) { op; }}
  95. #endif
  96. #else
  97. #define FOR_LIMB(i,op) { unsigned int i=0; for (i=0; i<DECAF_448_LIMBS; i++) { op; }}
  98. #endif
  99. /** Copy x = y */
  100. sv gf_cpy(gf x, const gf y) { FOR_LIMB(i, x[i] = y[i]); }
  101. /** Mostly-unoptimized multiply (PERF), but at least it's unrolled. */
  102. static inline void gf_mul (gf c, const gf a, const gf b) {
  103. field_mul((field_t *)c, (const field_t *)a, (const field_t *)b);
  104. }
  105. /** No dedicated square (PERF) */
  106. static inline void gf_sqr (gf c, const gf a) {
  107. field_sqr((field_t *)c, (const field_t *)a);
  108. }
  109. /** Inverse square root using addition chain. */
  110. sv gf_isqrt(gf y, const gf x) {
  111. field_isr((field_t *)y, (const field_t *)x);
  112. }
  113. /** Add mod p. Conservatively always weak-reduce. (PERF) */
  114. static inline void gf_add ( gf c, const gf a, const gf b ) {
  115. field_add((field_t *)c, (const field_t *)a, (const field_t *)b);
  116. }
  117. /** Subtract mod p. Conservatively always weak-reduce. (PERF) */
  118. static inline void gf_sub ( gf c, const gf a, const gf b ) {
  119. field_sub((field_t *)c, (const field_t *)a, (const field_t *)b);
  120. }
  121. /** Add mod p. Conservatively always weak-reduce. (PERF) */
  122. static inline void gf_bias ( gf c, int amt) {
  123. field_bias((field_t *)c, amt);
  124. }
  125. /** Subtract mod p. Bias by 2 and don't reduce */
  126. static inline void gf_sub_nr ( gf c, const gf a, const gf b ) {
  127. ANALYZE_THIS_ROUTINE_CAREFULLY; //TODO
  128. field_sub_nr((field_t *)c, (const field_t *)a, (const field_t *)b);
  129. gf_bias(c, 2);
  130. }
  131. /** Subtract mod p. Bias by amt but don't reduce. */
  132. static inline void gf_sub_nr_x ( gf c, const gf a, const gf b, int amt ) {
  133. ANALYZE_THIS_ROUTINE_CAREFULLY; //TODO
  134. field_sub_nr((field_t *)c, (const field_t *)a, (const field_t *)b);
  135. gf_bias(c, amt);
  136. }
  137. /** Add mod p. Don't reduce. */
  138. static inline void gf_add_nr ( gf c, const gf a, const gf b ) {
  139. ANALYZE_THIS_ROUTINE_CAREFULLY; //TODO
  140. field_add_nr((field_t *)c, (const field_t *)a, (const field_t *)b);
  141. }
  142. /** Constant time, x = is_z ? z : y */
  143. sv cond_sel(gf x, const gf y, const gf z, decaf_bool_t is_z) {
  144. FOR_LIMB(i, x[i] = (y[i] & ~is_z) | (z[i] & is_z) );
  145. }
  146. /** Constant time, if (neg) x=-x; */
  147. sv cond_neg(gf x, decaf_bool_t neg) {
  148. gf y;
  149. gf_sub(y,ZERO,x);
  150. cond_sel(x,x,y,neg);
  151. }
  152. /** Constant time, if (swap) (x,y) = (y,x); */
  153. sv cond_swap(gf x, gf y, decaf_bool_t swap) {
  154. FOR_LIMB(i, {
  155. decaf_word_t s = (x[i] ^ y[i]) & swap;
  156. x[i] ^= s;
  157. y[i] ^= s;
  158. });
  159. }
  160. /**
  161. * Mul by signed int. Not constant-time WRT the sign of that int.
  162. * Just uses a full mul (PERF)
  163. */
  164. static inline void gf_mlw(gf c, const gf a, int w) {
  165. if (w>0) {
  166. field_mulw((field_t *)c, (const field_t *)a, w);
  167. } else {
  168. field_mulw((field_t *)c, (const field_t *)a, -w);
  169. gf_sub(c,ZERO,c);
  170. }
  171. }
  172. /** Canonicalize */
  173. static inline void gf_canon ( gf a ) {
  174. field_strong_reduce((field_t *)a);
  175. }
  176. /** Compare a==b */
  177. static decaf_word_t __attribute__((noinline)) gf_eq(const gf a, const gf b) {
  178. gf c;
  179. gf_sub(c,a,b);
  180. gf_canon(c);
  181. decaf_word_t ret=0;
  182. FOR_LIMB(i, ret |= c[i] );
  183. /* Hope the compiler is too dumb to optimize this, thus noinline */
  184. return ((decaf_dword_t)ret - 1) >> WBITS;
  185. }
  186. /** Return high bit of x = low bit of 2x mod p */
  187. static decaf_word_t hibit(const gf x) {
  188. gf y;
  189. gf_add(y,x,x);
  190. gf_canon(y);
  191. return -(y[0]&1);
  192. }
  193. /* a = use_c ? c : b */
  194. sv decaf_448_cond_sel (
  195. decaf_448_point_t a,
  196. const decaf_448_point_t b,
  197. const decaf_448_point_t c,
  198. decaf_bool_t use_c
  199. ) {
  200. cond_sel(a->x, b->x, c->x, use_c);
  201. cond_sel(a->y, b->y, c->y, use_c);
  202. cond_sel(a->z, b->z, c->z, use_c);
  203. cond_sel(a->t, b->t, c->t, use_c);
  204. }
  205. /** {extra,accum} - sub +? p
  206. * Must have extra <= 1
  207. */
  208. sv decaf_448_subx(
  209. decaf_448_scalar_t out,
  210. const decaf_word_t accum[DECAF_448_SCALAR_LIMBS],
  211. const decaf_448_scalar_t sub,
  212. const decaf_448_scalar_t p,
  213. decaf_word_t extra
  214. ) {
  215. decaf_sdword_t chain = 0;
  216. unsigned int i;
  217. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  218. chain = (chain + accum[i]) - sub->limb[i];
  219. out->limb[i] = chain;
  220. chain >>= WBITS;
  221. }
  222. decaf_bool_t borrow = chain+extra; /* = 0 or -1 */
  223. chain = 0;
  224. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  225. chain = (chain + out->limb[i]) + (p->limb[i] & borrow);
  226. out->limb[i] = chain;
  227. chain >>= WBITS;
  228. }
  229. }
  230. sv decaf_448_montmul (
  231. decaf_448_scalar_t out,
  232. const decaf_448_scalar_t a,
  233. const decaf_448_scalar_t b,
  234. const decaf_448_scalar_t p,
  235. decaf_word_t montgomery_factor
  236. ) {
  237. unsigned int i,j;
  238. decaf_word_t accum[DECAF_448_SCALAR_LIMBS+1] = {0};
  239. decaf_word_t hi_carry = 0;
  240. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  241. decaf_word_t mand = a->limb[i];
  242. const decaf_word_t *mier = b->limb;
  243. decaf_dword_t chain = 0;
  244. for (j=0; j<DECAF_448_SCALAR_LIMBS; j++) {
  245. chain += ((decaf_dword_t)mand)*mier[j] + accum[j];
  246. accum[j] = chain;
  247. chain >>= WBITS;
  248. }
  249. accum[j] = chain;
  250. mand = accum[0] * montgomery_factor;
  251. chain = 0;
  252. mier = p->limb;
  253. for (j=0; j<DECAF_448_SCALAR_LIMBS; j++) {
  254. chain += (decaf_dword_t)mand*mier[j] + accum[j];
  255. if (j) accum[j-1] = chain;
  256. chain >>= WBITS;
  257. }
  258. chain += accum[j];
  259. chain += hi_carry;
  260. accum[j-1] = chain;
  261. hi_carry = chain >> WBITS;
  262. }
  263. decaf_448_subx(out, accum, p, p, hi_carry);
  264. }
  265. void decaf_448_scalar_mul (
  266. decaf_448_scalar_t out,
  267. const decaf_448_scalar_t a,
  268. const decaf_448_scalar_t b
  269. ) {
  270. decaf_448_montmul(out,a,b,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  271. decaf_448_montmul(out,out,decaf_448_scalar_r2,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  272. }
  273. void decaf_448_scalar_sub (
  274. decaf_448_scalar_t out,
  275. const decaf_448_scalar_t a,
  276. const decaf_448_scalar_t b
  277. ) {
  278. decaf_448_subx(out, a->limb, b, decaf_448_scalar_p, 0);
  279. }
  280. void decaf_448_scalar_add (
  281. decaf_448_scalar_t out,
  282. const decaf_448_scalar_t a,
  283. const decaf_448_scalar_t b
  284. ) {
  285. decaf_dword_t chain = 0;
  286. unsigned int i;
  287. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  288. chain = (chain + a->limb[i]) + b->limb[i];
  289. out->limb[i] = chain;
  290. chain >>= WBITS;
  291. }
  292. decaf_448_subx(out, out->limb, decaf_448_scalar_p, decaf_448_scalar_p, chain);
  293. }
  294. void decaf_448_scalar_copy (
  295. decaf_448_scalar_t out,
  296. const decaf_448_scalar_t a
  297. ) {
  298. unsigned int i;
  299. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  300. out->limb[i] = a->limb[i];
  301. }
  302. }
  303. decaf_bool_t decaf_448_scalar_eq (
  304. const decaf_448_scalar_t a,
  305. const decaf_448_scalar_t b
  306. ) {
  307. decaf_word_t diff = 0;
  308. unsigned int i;
  309. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  310. diff |= a->limb[i] ^ b->limb[i];
  311. }
  312. return (((decaf_dword_t)diff)-1)>>WBITS;
  313. }
  314. /* *** API begins here *** */
  315. /** identity = (0,1) */
  316. const decaf_448_point_t decaf_448_point_identity = {{{0},{1},{1},{0}}};
  317. void decaf_448_point_encode( unsigned char ser[DECAF_448_SER_BYTES], const decaf_448_point_t p ) {
  318. /* Can shave off one mul here; not important but makes consistent with paper */
  319. gf a, b, c, d;
  320. gf_mlw ( a, p->y, 1-EDWARDS_D );
  321. gf_mul ( c, a, p->t );
  322. gf_mul ( a, p->x, p->z );
  323. gf_sub ( d, c, a );
  324. gf_add ( a, p->z, p->y );
  325. gf_sub ( b, p->z, p->y );
  326. gf_mul ( c, b, a );
  327. gf_mlw ( b, c, -EDWARDS_D );
  328. gf_isqrt ( a, b );
  329. gf_mlw ( b, a, -EDWARDS_D );
  330. gf_mul ( c, b, a );
  331. gf_mul ( a, c, d );
  332. gf_add ( d, b, b );
  333. gf_mul ( c, d, p->z );
  334. cond_neg ( b, ~hibit(c) );
  335. gf_mul ( c, b, p->y );
  336. gf_add ( a, a, c );
  337. cond_neg ( a, hibit(a) );
  338. gf_canon(a);
  339. int i, k=0, bits=0;
  340. decaf_dword_t buf=0;
  341. for (i=0; i<DECAF_448_LIMBS; i++) {
  342. buf |= (decaf_dword_t)a[i]<<bits;
  343. for (bits += LBITS; (bits>=8 || i==DECAF_448_LIMBS-1) && k<DECAF_448_SER_BYTES; bits-=8, buf>>=8) {
  344. ser[k++]=buf;
  345. }
  346. }
  347. }
  348. /**
  349. * Deserialize a bool, return TRUE if < p.
  350. */
  351. static decaf_bool_t gf_deser(gf s, const unsigned char ser[DECAF_448_SER_BYTES]) {
  352. unsigned int i, k=0, bits=0;
  353. decaf_dword_t buf=0;
  354. for (i=0; i<DECAF_448_SER_BYTES; i++) {
  355. buf |= (decaf_dword_t)ser[i]<<bits;
  356. for (bits += 8; (bits>=LBITS || i==DECAF_448_SER_BYTES-1) && k<DECAF_448_LIMBS; bits-=LBITS, buf>>=LBITS) {
  357. s[k++] = buf & LMASK;
  358. }
  359. }
  360. decaf_sdword_t accum = 0;
  361. FOR_LIMB(i, accum = (accum + s[i] - P[i]) >> WBITS );
  362. return accum;
  363. }
  364. /* Constant-time add or subtract */
  365. sv decaf_448_point_add_sub (
  366. decaf_448_point_t p,
  367. const decaf_448_point_t q,
  368. const decaf_448_point_t r,
  369. decaf_bool_t do_sub
  370. ) {
  371. /* Twisted Edward formulas, complete when 4-torsion isn't involved */
  372. gf a, b, c, d;
  373. gf_sub_nr ( b, q->y, q->x );
  374. gf_sub_nr ( c, r->y, r->x );
  375. gf_add_nr ( d, r->y, r->x );
  376. cond_swap(c,d,do_sub);
  377. gf_mul ( a, c, b );
  378. gf_add_nr ( b, q->y, q->x );
  379. gf_mul ( p->y, d, b );
  380. gf_mul ( b, r->t, q->t );
  381. gf_mlw ( p->x, b, 2-2*EDWARDS_D );
  382. gf_add_nr ( b, a, p->y );
  383. gf_sub_nr ( c, p->y, a );
  384. gf_mul ( a, q->z, r->z );
  385. gf_add_nr ( a, a, a );
  386. gf_add_nr ( p->y, a, p->x );
  387. gf_sub_nr ( a, a, p->x );
  388. cond_swap(a,p->y,do_sub);
  389. gf_mul ( p->z, a, p->y );
  390. gf_mul ( p->x, p->y, c );
  391. gf_mul ( p->y, a, b );
  392. gf_mul ( p->t, b, c );
  393. }
  394. decaf_bool_t decaf_448_point_decode (
  395. decaf_448_point_t p,
  396. const unsigned char ser[DECAF_448_SER_BYTES],
  397. decaf_bool_t allow_identity
  398. ) {
  399. gf s, a, b, c, d, e;
  400. decaf_bool_t succ = gf_deser(s, ser), zero = gf_eq(s, ZERO);
  401. succ &= allow_identity | ~zero;
  402. succ &= ~hibit(s);
  403. gf_sqr ( a, s );
  404. gf_sub ( p->z, ONE, a );
  405. gf_sqr ( b, p->z );
  406. gf_mlw ( c, a, 4-4*EDWARDS_D );
  407. gf_add ( c, c, b );
  408. gf_mul ( b, c, a );
  409. gf_isqrt ( d, b );
  410. gf_sqr ( e, d );
  411. gf_mul ( a, e, b );
  412. gf_add ( a, a, ONE );
  413. succ &= ~gf_eq ( a, ZERO );
  414. gf_mul ( b, c, d );
  415. cond_neg ( d, hibit(b) );
  416. gf_add ( p->x, s, s );
  417. gf_mul ( c, d, s );
  418. gf_sub ( b, TWO, p->z );
  419. gf_mul ( a, b, c );
  420. gf_mul ( p->y,a,p->z );
  421. gf_mul ( p->t,p->x,a );
  422. p->y[0] -= zero;
  423. /* TODO: do something safe if ~succ? */
  424. return succ;
  425. }
  426. void decaf_448_point_sub (
  427. decaf_448_point_t p,
  428. const decaf_448_point_t q,
  429. const decaf_448_point_t r
  430. ) {
  431. gf a, b, c, d;
  432. gf_sub_nr ( b, q->y, q->x );
  433. gf_sub_nr ( d, r->y, r->x );
  434. gf_add_nr ( c, r->y, r->x );
  435. gf_mul ( a, c, b );
  436. gf_add_nr ( b, q->y, q->x );
  437. gf_mul ( p->y, d, b );
  438. gf_mul ( b, r->t, q->t );
  439. gf_mlw ( p->x, b, 2-2*EDWARDS_D );
  440. gf_add_nr ( b, a, p->y );
  441. gf_sub_nr ( c, p->y, a );
  442. gf_mul ( a, q->z, r->z );
  443. gf_add_nr ( a, a, a );
  444. gf_sub_nr ( p->y, a, p->x );
  445. gf_add_nr ( a, a, p->x );
  446. gf_mul ( p->z, a, p->y );
  447. gf_mul ( p->x, p->y, c );
  448. gf_mul ( p->y, a, b );
  449. gf_mul ( p->t, b, c );
  450. }
  451. void decaf_448_point_add (
  452. decaf_448_point_t p,
  453. const decaf_448_point_t q,
  454. const decaf_448_point_t r
  455. ) {
  456. gf a, b, c, d;
  457. gf_sub_nr ( b, q->y, q->x );
  458. gf_sub_nr ( c, r->y, r->x );
  459. gf_add_nr ( d, r->y, r->x );
  460. gf_mul ( a, c, b );
  461. gf_add_nr ( b, q->y, q->x );
  462. gf_mul ( p->y, d, b );
  463. gf_mul ( b, r->t, q->t );
  464. gf_mlw ( p->x, b, 2-2*EDWARDS_D );
  465. gf_add_nr ( b, a, p->y );
  466. gf_sub_nr ( c, p->y, a );
  467. gf_mul ( a, q->z, r->z );
  468. gf_add_nr ( a, a, a );
  469. gf_add_nr ( p->y, a, p->x );
  470. gf_sub_nr ( a, a, p->x );
  471. gf_mul ( p->z, a, p->y );
  472. gf_mul ( p->x, p->y, c );
  473. gf_mul ( p->y, a, b );
  474. gf_mul ( p->t, b, c );
  475. }
  476. /* No dedicated point double yet (PERF) */
  477. void decaf_448_point_double(decaf_448_point_t p, const decaf_448_point_t q) {
  478. gf a, b, c, d;
  479. gf_sqr ( c, q->x );
  480. gf_sqr ( a, q->y );
  481. gf_add_nr ( d, c, a );
  482. gf_add_nr ( p->t, q->y, q->x );
  483. gf_sqr ( b, p->t );
  484. gf_sub_nr_x ( b, b, d, 3 );
  485. gf_sub_nr ( p->t, a, c );
  486. gf_sqr ( p->x, q->z );
  487. gf_add_nr ( p->z, p->x, p->x );
  488. gf_sub_nr_x ( a, p->z, p->t, 4 );
  489. gf_mul ( p->x, a, b );
  490. gf_mul ( p->z, p->t, a );
  491. gf_mul ( p->y, p->t, d );
  492. gf_mul ( p->t, b, d );
  493. }
  494. void decaf_448_point_copy (
  495. decaf_448_point_t a,
  496. const decaf_448_point_t b
  497. ) {
  498. gf_cpy(a->x, b->x);
  499. gf_cpy(a->y, b->y);
  500. gf_cpy(a->z, b->z);
  501. gf_cpy(a->t, b->t);
  502. }
  503. decaf_bool_t decaf_448_scalar_decode(
  504. decaf_448_scalar_t s,
  505. const unsigned char ser[DECAF_448_SER_BYTES]
  506. ) {
  507. unsigned int i,j,k=0;
  508. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  509. decaf_word_t out = 0;
  510. for (j=0; j<sizeof(decaf_word_t); j++,k++) {
  511. out |= ((decaf_word_t)ser[k])<<(8*j);
  512. }
  513. s->limb[i] = out;
  514. }
  515. decaf_sdword_t accum = 0;
  516. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  517. accum = (accum + s->limb[i] - decaf_448_scalar_p->limb[i]) >> WBITS;
  518. }
  519. decaf_448_scalar_mul(s,s,decaf_448_scalar_one); /* ham-handed reduce */
  520. return accum;
  521. }
  522. void decaf_bzero (
  523. void *s,
  524. size_t size
  525. ) {
  526. #ifdef __STDC_LIB_EXT1__
  527. memset_s(s, size, 0, size);
  528. #else
  529. volatile uint8_t *destroy = (volatile uint8_t *)s;
  530. unsigned i;
  531. for (i=0; i<size; i++) {
  532. destroy[i] = 0;
  533. }
  534. #endif
  535. }
  536. void decaf_448_scalar_destroy (
  537. decaf_448_scalar_t scalar
  538. ) {
  539. decaf_bzero(scalar, sizeof(decaf_448_scalar_t));
  540. }
  541. static inline void ignore_result ( decaf_bool_t boo ) {
  542. (void)boo;
  543. }
  544. void decaf_448_scalar_decode_long(
  545. decaf_448_scalar_t s,
  546. const unsigned char *ser,
  547. size_t ser_len
  548. ) {
  549. if (ser_len == 0) {
  550. decaf_448_scalar_copy(s, decaf_448_scalar_zero);
  551. return;
  552. }
  553. size_t i;
  554. unsigned char tmp[DECAF_448_SER_BYTES] = {0};
  555. decaf_448_scalar_t t1, t2;
  556. i = ser_len - (ser_len%DECAF_448_SER_BYTES);
  557. if (i==ser_len) i -= DECAF_448_SER_BYTES;
  558. memcpy(tmp, ser+i, ser_len - i);
  559. ignore_result( decaf_448_scalar_decode(t1, tmp) );
  560. decaf_bzero(tmp, sizeof(tmp));
  561. while (i) {
  562. i -= DECAF_448_SER_BYTES;
  563. decaf_448_montmul(t1,t1,decaf_448_scalar_r2,decaf_448_scalar_p,DECAF_MONTGOMERY_FACTOR);
  564. ignore_result( decaf_448_scalar_decode(t2, ser+i) );
  565. decaf_448_scalar_add(t1, t1, t2);
  566. }
  567. decaf_448_scalar_copy(s, t1);
  568. decaf_448_scalar_destroy(t1);
  569. decaf_448_scalar_destroy(t2);
  570. }
  571. void decaf_448_scalar_encode(
  572. unsigned char ser[DECAF_448_SER_BYTES],
  573. const decaf_448_scalar_t s
  574. ) {
  575. unsigned int i,j,k=0;
  576. for (i=0; i<DECAF_448_SCALAR_LIMBS; i++) {
  577. for (j=0; j<sizeof(decaf_word_t); j++,k++) {
  578. ser[k] = s->limb[i] >> (8*j);
  579. }
  580. }
  581. }
  582. void decaf_448_point_scalarmul (
  583. decaf_448_point_t a,
  584. const decaf_448_point_t b,
  585. const decaf_448_scalar_t scalar
  586. ) {
  587. /* w=2 signed window uses about 1.5 adds per bit.
  588. * I figured a few extra lines was worth the 25% speedup.
  589. */
  590. decaf_448_point_t w,b3,tmp;
  591. decaf_448_point_double(w,b);
  592. /* b3 = b*3 */
  593. decaf_448_point_add(b3,w,b);
  594. int i;
  595. for (i=DECAF_448_SCALAR_BITS &~ 1; i>0; i-=2) {
  596. decaf_word_t bits = scalar->limb[i/WBITS]>>(i%WBITS);
  597. decaf_448_cond_sel(tmp,b,b3,((bits^(bits>>1))&1)-1);
  598. decaf_448_point_double(w,w);
  599. decaf_448_point_add_sub(w,w,tmp,((bits>>1)&1)-1);
  600. decaf_448_point_double(w,w);
  601. }
  602. decaf_448_point_add_sub(w,w,b,((scalar->limb[0]>>1)&1)-1);
  603. /* low bit is special because fo signed window */
  604. decaf_448_cond_sel(tmp,b,decaf_448_point_identity,-(scalar->limb[0]&1));
  605. decaf_448_point_sub(a,w,tmp);
  606. }
  607. void decaf_448_point_double_scalarmul (
  608. decaf_448_point_t a,
  609. const decaf_448_point_t b,
  610. const decaf_448_scalar_t scalarb,
  611. const decaf_448_point_t c,
  612. const decaf_448_scalar_t scalarc
  613. ) {
  614. /* w=2 signed window uses about 1.5 adds per bit.
  615. * I figured a few extra lines was worth the 25% speedup.
  616. * NB: if adapting this function to scalarmul by a
  617. * possibly-odd number of unmasked bits, may need to mask.
  618. */
  619. decaf_448_point_t w,b3,c3,tmp;
  620. decaf_448_point_double(w,b);
  621. decaf_448_point_double(tmp,c);
  622. /* b3 = b*3 */
  623. decaf_448_point_add(b3,w,b);
  624. decaf_448_point_add(c3,tmp,c);
  625. decaf_448_point_add(w,w,tmp);
  626. int i;
  627. for (i=DECAF_448_SCALAR_BITS &~ 1; i>0; i-=2) {
  628. decaf_448_point_double(w,w);
  629. decaf_word_t bits = scalarb->limb[i/WBITS]>>(i%WBITS);
  630. decaf_448_cond_sel(tmp,b,b3,((bits^(bits>>1))&1)-1);
  631. decaf_448_point_add_sub(w,w,tmp,((bits>>1)&1)-1);
  632. bits = scalarc->limb[i/WBITS]>>(i%WBITS);
  633. decaf_448_cond_sel(tmp,c,c3,((bits^(bits>>1))&1)-1);
  634. decaf_448_point_add_sub(w,w,tmp,((bits>>1)&1)-1);
  635. decaf_448_point_double(w,w);
  636. }
  637. decaf_448_point_add_sub(w,w,b,((scalarb->limb[0]>>1)&1)-1);
  638. decaf_448_point_add_sub(w,w,c,((scalarc->limb[0]>>1)&1)-1);
  639. /* low bit is special because of signed window */
  640. decaf_448_cond_sel(tmp,b,decaf_448_point_identity,-(scalarb->limb[0]&1));
  641. decaf_448_point_sub(w,w,tmp);
  642. decaf_448_cond_sel(tmp,c,decaf_448_point_identity,-(scalarc->limb[0]&1));
  643. decaf_448_point_sub(a,w,tmp);
  644. }
  645. decaf_bool_t decaf_448_point_eq ( const decaf_448_point_t p, const decaf_448_point_t q ) {
  646. /* equality mod 2-torsion compares x/y */
  647. gf a, b;
  648. gf_mul ( a, p->y, q->x );
  649. gf_mul ( b, q->y, p->x );
  650. return gf_eq(a,b);
  651. }
  652. void decaf_448_point_from_hash_nonuniform (
  653. decaf_448_point_t p,
  654. const unsigned char ser[DECAF_448_SER_BYTES]
  655. ) {
  656. gf r,urr,a,b,c,dee,e,ur2_d,udr2_1;
  657. (void)gf_deser(r,ser);
  658. gf_canon(r);
  659. gf_sqr(a,r);
  660. gf_mlw(urr,a,QUADRATIC_NONRESIDUE);
  661. gf_mlw(dee,ONE,EDWARDS_D);
  662. gf_add(a,urr,ONE);
  663. gf_sub(ur2_d,dee,urr);
  664. gf_mul(c,a,ur2_d);
  665. gf_mlw(b,urr,-EDWARDS_D);
  666. gf_add(udr2_1,b,ONE);
  667. gf_mul(a,c,udr2_1);
  668. gf_mlw(c,a,EDWARDS_D+1);
  669. gf_isqrt(b,c); /* FIELD: if 5 mod 8, multiply result by u. */
  670. gf_sqr(a,b);
  671. gf_mul(e,a,c);
  672. decaf_bool_t square = gf_eq(e,ONE);
  673. gf_mul(a,b,r);
  674. cond_sel(b,a,b,square);
  675. gf_mlw(a,b,EDWARDS_D+1);
  676. cond_swap(ur2_d,udr2_1,~square);
  677. gf_mul(e,ur2_d,a);
  678. cond_neg(e,hibit(e)^square);
  679. gf_mul(b,udr2_1,a);
  680. gf_sqr(c,b);
  681. gf_sqr(a,e);
  682. gf_sub(a,ONE,a);
  683. gf_add(e,e,e);
  684. gf_add(b,dee,c);
  685. gf_sub(c,dee,c);
  686. gf_mul(p->x,e,c);
  687. gf_mul(p->z,a,c);
  688. gf_mul(p->y,b,a);
  689. gf_mul(p->t,b,e);
  690. }
  691. void decaf_448_point_from_hash_uniform (
  692. decaf_448_point_t pt,
  693. const unsigned char hashed_data[2*DECAF_448_SER_BYTES]
  694. ) {
  695. decaf_448_point_t pt2;
  696. decaf_448_point_from_hash_nonuniform(pt,hashed_data);
  697. decaf_448_point_from_hash_nonuniform(pt2,&hashed_data[DECAF_448_SER_BYTES]);
  698. decaf_448_point_add(pt,pt,pt2);
  699. }
  700. decaf_bool_t decaf_448_point_valid (
  701. const decaf_448_point_t p
  702. ) {
  703. gf a,b,c;
  704. gf_mul(a,p->x,p->y);
  705. gf_mul(b,p->z,p->t);
  706. decaf_bool_t out = gf_eq(a,b);
  707. gf_sqr(a,p->x);
  708. gf_sqr(b,p->y);
  709. gf_sub(a,b,a);
  710. gf_sqr(b,p->t);
  711. gf_mlw(c,b,1-EDWARDS_D);
  712. gf_sqr(b,p->z);
  713. gf_sub(b,b,c);
  714. out &= gf_eq(a,b);
  715. out &= ~gf_eq(p->z,ZERO);
  716. return out;
  717. }
  718. void decaf_448_precompute (
  719. decaf_448_precomputed_s *a,
  720. const decaf_448_point_t b
  721. ) {
  722. decaf_448_point_copy(a->p[0],b);
  723. }
  724. void decaf_448_precomputed_scalarmul (
  725. decaf_448_point_t a,
  726. const decaf_448_precomputed_s *b,
  727. const decaf_448_scalar_t scalar
  728. ) {
  729. decaf_448_point_scalarmul(a,b->p[0],scalar);
  730. }