1
0

curve25519-donna-32bit.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583
  1. /*
  2. Public domain by Andrew M. <liquidsun@gmail.com>
  3. See: https://github.com/floodyberry/curve25519-donna
  4. 32 bit integer curve25519 implementation
  5. */
  6. #if !defined(ED25519_SSE2) && !defined(ED25519_64BIT)
  7. typedef uint32_t bignum25519[10];
  8. typedef uint32_t bignum25519align16[12];
  9. static const uint32_t reduce_mask_25 = (1 << 25) - 1;
  10. static const uint32_t reduce_mask_26 = (1 << 26) - 1;
  11. /* out = in */
  12. DONNA_INLINE static void
  13. curve25519_copy(bignum25519 out, const bignum25519 in) {
  14. out[0] = in[0];
  15. out[1] = in[1];
  16. out[2] = in[2];
  17. out[3] = in[3];
  18. out[4] = in[4];
  19. out[5] = in[5];
  20. out[6] = in[6];
  21. out[7] = in[7];
  22. out[8] = in[8];
  23. out[9] = in[9];
  24. }
  25. /* out = a + b */
  26. DONNA_INLINE static void
  27. curve25519_add(bignum25519 out, const bignum25519 a, const bignum25519 b) {
  28. out[0] = a[0] + b[0];
  29. out[1] = a[1] + b[1];
  30. out[2] = a[2] + b[2];
  31. out[3] = a[3] + b[3];
  32. out[4] = a[4] + b[4];
  33. out[5] = a[5] + b[5];
  34. out[6] = a[6] + b[6];
  35. out[7] = a[7] + b[7];
  36. out[8] = a[8] + b[8];
  37. out[9] = a[9] + b[9];
  38. }
  39. DONNA_INLINE static void
  40. curve25519_add_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
  41. uint32_t c;
  42. out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
  43. out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
  44. out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
  45. out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
  46. out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
  47. out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
  48. out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
  49. out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
  50. out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
  51. out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
  52. out[0] += 19 * c;
  53. }
  54. DONNA_INLINE static void
  55. curve25519_add_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
  56. uint32_t c;
  57. out[0] = a[0] + b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
  58. out[1] = a[1] + b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
  59. out[2] = a[2] + b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
  60. out[3] = a[3] + b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
  61. out[4] = a[4] + b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
  62. out[5] = a[5] + b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
  63. out[6] = a[6] + b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
  64. out[7] = a[7] + b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
  65. out[8] = a[8] + b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
  66. out[9] = a[9] + b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
  67. out[0] += 19 * c;
  68. }
  69. /* multiples of p */
  70. static const uint32_t twoP0 = 0x07ffffda;
  71. static const uint32_t twoP13579 = 0x03fffffe;
  72. static const uint32_t twoP2468 = 0x07fffffe;
  73. static const uint32_t fourP0 = 0x0fffffb4;
  74. static const uint32_t fourP13579 = 0x07fffffc;
  75. static const uint32_t fourP2468 = 0x0ffffffc;
  76. /* out = a - b */
  77. DONNA_INLINE static void
  78. curve25519_sub(bignum25519 out, const bignum25519 a, const bignum25519 b) {
  79. uint32_t c;
  80. out[0] = twoP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
  81. out[1] = twoP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
  82. out[2] = twoP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
  83. out[3] = twoP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
  84. out[4] = twoP2468 + a[4] - b[4] + c;
  85. out[5] = twoP13579 + a[5] - b[5] ;
  86. out[6] = twoP2468 + a[6] - b[6] ;
  87. out[7] = twoP13579 + a[7] - b[7] ;
  88. out[8] = twoP2468 + a[8] - b[8] ;
  89. out[9] = twoP13579 + a[9] - b[9] ;
  90. }
  91. /* out = a - b, where a is the result of a basic op (add,sub) */
  92. DONNA_INLINE static void
  93. curve25519_sub_after_basic(bignum25519 out, const bignum25519 a, const bignum25519 b) {
  94. uint32_t c;
  95. out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
  96. out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
  97. out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
  98. out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
  99. out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
  100. out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
  101. out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
  102. out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
  103. out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
  104. out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
  105. out[0] += 19 * c;
  106. }
  107. DONNA_INLINE static void
  108. curve25519_sub_reduce(bignum25519 out, const bignum25519 a, const bignum25519 b) {
  109. uint32_t c;
  110. out[0] = fourP0 + a[0] - b[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
  111. out[1] = fourP13579 + a[1] - b[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
  112. out[2] = fourP2468 + a[2] - b[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
  113. out[3] = fourP13579 + a[3] - b[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
  114. out[4] = fourP2468 + a[4] - b[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
  115. out[5] = fourP13579 + a[5] - b[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
  116. out[6] = fourP2468 + a[6] - b[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
  117. out[7] = fourP13579 + a[7] - b[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
  118. out[8] = fourP2468 + a[8] - b[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
  119. out[9] = fourP13579 + a[9] - b[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
  120. out[0] += 19 * c;
  121. }
  122. /* out = -a */
  123. DONNA_INLINE static void
  124. curve25519_neg(bignum25519 out, const bignum25519 a) {
  125. uint32_t c;
  126. out[0] = twoP0 - a[0] ; c = (out[0] >> 26); out[0] &= reduce_mask_26;
  127. out[1] = twoP13579 - a[1] + c; c = (out[1] >> 25); out[1] &= reduce_mask_25;
  128. out[2] = twoP2468 - a[2] + c; c = (out[2] >> 26); out[2] &= reduce_mask_26;
  129. out[3] = twoP13579 - a[3] + c; c = (out[3] >> 25); out[3] &= reduce_mask_25;
  130. out[4] = twoP2468 - a[4] + c; c = (out[4] >> 26); out[4] &= reduce_mask_26;
  131. out[5] = twoP13579 - a[5] + c; c = (out[5] >> 25); out[5] &= reduce_mask_25;
  132. out[6] = twoP2468 - a[6] + c; c = (out[6] >> 26); out[6] &= reduce_mask_26;
  133. out[7] = twoP13579 - a[7] + c; c = (out[7] >> 25); out[7] &= reduce_mask_25;
  134. out[8] = twoP2468 - a[8] + c; c = (out[8] >> 26); out[8] &= reduce_mask_26;
  135. out[9] = twoP13579 - a[9] + c; c = (out[9] >> 25); out[9] &= reduce_mask_25;
  136. out[0] += 19 * c;
  137. }
  138. /* out = a * b */
  139. #define curve25519_mul_noinline curve25519_mul
  140. static void
  141. curve25519_mul(bignum25519 out, const bignum25519 a, const bignum25519 b) {
  142. uint32_t r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
  143. uint32_t s0,s1,s2,s3,s4,s5,s6,s7,s8,s9;
  144. uint64_t m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
  145. uint32_t p;
  146. r0 = b[0];
  147. r1 = b[1];
  148. r2 = b[2];
  149. r3 = b[3];
  150. r4 = b[4];
  151. r5 = b[5];
  152. r6 = b[6];
  153. r7 = b[7];
  154. r8 = b[8];
  155. r9 = b[9];
  156. s0 = a[0];
  157. s1 = a[1];
  158. s2 = a[2];
  159. s3 = a[3];
  160. s4 = a[4];
  161. s5 = a[5];
  162. s6 = a[6];
  163. s7 = a[7];
  164. s8 = a[8];
  165. s9 = a[9];
  166. m1 = mul32x32_64(r0, s1) + mul32x32_64(r1, s0);
  167. m3 = mul32x32_64(r0, s3) + mul32x32_64(r1, s2) + mul32x32_64(r2, s1) + mul32x32_64(r3, s0);
  168. m5 = mul32x32_64(r0, s5) + mul32x32_64(r1, s4) + mul32x32_64(r2, s3) + mul32x32_64(r3, s2) + mul32x32_64(r4, s1) + mul32x32_64(r5, s0);
  169. m7 = mul32x32_64(r0, s7) + mul32x32_64(r1, s6) + mul32x32_64(r2, s5) + mul32x32_64(r3, s4) + mul32x32_64(r4, s3) + mul32x32_64(r5, s2) + mul32x32_64(r6, s1) + mul32x32_64(r7, s0);
  170. m9 = mul32x32_64(r0, s9) + mul32x32_64(r1, s8) + mul32x32_64(r2, s7) + mul32x32_64(r3, s6) + mul32x32_64(r4, s5) + mul32x32_64(r5, s4) + mul32x32_64(r6, s3) + mul32x32_64(r7, s2) + mul32x32_64(r8, s1) + mul32x32_64(r9, s0);
  171. r1 *= 2;
  172. r3 *= 2;
  173. r5 *= 2;
  174. r7 *= 2;
  175. m0 = mul32x32_64(r0, s0);
  176. m2 = mul32x32_64(r0, s2) + mul32x32_64(r1, s1) + mul32x32_64(r2, s0);
  177. m4 = mul32x32_64(r0, s4) + mul32x32_64(r1, s3) + mul32x32_64(r2, s2) + mul32x32_64(r3, s1) + mul32x32_64(r4, s0);
  178. m6 = mul32x32_64(r0, s6) + mul32x32_64(r1, s5) + mul32x32_64(r2, s4) + mul32x32_64(r3, s3) + mul32x32_64(r4, s2) + mul32x32_64(r5, s1) + mul32x32_64(r6, s0);
  179. m8 = mul32x32_64(r0, s8) + mul32x32_64(r1, s7) + mul32x32_64(r2, s6) + mul32x32_64(r3, s5) + mul32x32_64(r4, s4) + mul32x32_64(r5, s3) + mul32x32_64(r6, s2) + mul32x32_64(r7, s1) + mul32x32_64(r8, s0);
  180. r1 *= 19;
  181. r2 *= 19;
  182. r3 = (r3 / 2) * 19;
  183. r4 *= 19;
  184. r5 = (r5 / 2) * 19;
  185. r6 *= 19;
  186. r7 = (r7 / 2) * 19;
  187. r8 *= 19;
  188. r9 *= 19;
  189. m1 += (mul32x32_64(r9, s2) + mul32x32_64(r8, s3) + mul32x32_64(r7, s4) + mul32x32_64(r6, s5) + mul32x32_64(r5, s6) + mul32x32_64(r4, s7) + mul32x32_64(r3, s8) + mul32x32_64(r2, s9));
  190. m3 += (mul32x32_64(r9, s4) + mul32x32_64(r8, s5) + mul32x32_64(r7, s6) + mul32x32_64(r6, s7) + mul32x32_64(r5, s8) + mul32x32_64(r4, s9));
  191. m5 += (mul32x32_64(r9, s6) + mul32x32_64(r8, s7) + mul32x32_64(r7, s8) + mul32x32_64(r6, s9));
  192. m7 += (mul32x32_64(r9, s8) + mul32x32_64(r8, s9));
  193. r3 *= 2;
  194. r5 *= 2;
  195. r7 *= 2;
  196. r9 *= 2;
  197. m0 += (mul32x32_64(r9, s1) + mul32x32_64(r8, s2) + mul32x32_64(r7, s3) + mul32x32_64(r6, s4) + mul32x32_64(r5, s5) + mul32x32_64(r4, s6) + mul32x32_64(r3, s7) + mul32x32_64(r2, s8) + mul32x32_64(r1, s9));
  198. m2 += (mul32x32_64(r9, s3) + mul32x32_64(r8, s4) + mul32x32_64(r7, s5) + mul32x32_64(r6, s6) + mul32x32_64(r5, s7) + mul32x32_64(r4, s8) + mul32x32_64(r3, s9));
  199. m4 += (mul32x32_64(r9, s5) + mul32x32_64(r8, s6) + mul32x32_64(r7, s7) + mul32x32_64(r6, s8) + mul32x32_64(r5, s9));
  200. m6 += (mul32x32_64(r9, s7) + mul32x32_64(r8, s8) + mul32x32_64(r7, s9));
  201. m8 += (mul32x32_64(r9, s9));
  202. r0 = (uint32_t)m0 & reduce_mask_26; c = (m0 >> 26);
  203. m1 += c; r1 = (uint32_t)m1 & reduce_mask_25; c = (m1 >> 25);
  204. m2 += c; r2 = (uint32_t)m2 & reduce_mask_26; c = (m2 >> 26);
  205. m3 += c; r3 = (uint32_t)m3 & reduce_mask_25; c = (m3 >> 25);
  206. m4 += c; r4 = (uint32_t)m4 & reduce_mask_26; c = (m4 >> 26);
  207. m5 += c; r5 = (uint32_t)m5 & reduce_mask_25; c = (m5 >> 25);
  208. m6 += c; r6 = (uint32_t)m6 & reduce_mask_26; c = (m6 >> 26);
  209. m7 += c; r7 = (uint32_t)m7 & reduce_mask_25; c = (m7 >> 25);
  210. m8 += c; r8 = (uint32_t)m8 & reduce_mask_26; c = (m8 >> 26);
  211. m9 += c; r9 = (uint32_t)m9 & reduce_mask_25; p = (uint32_t)(m9 >> 25);
  212. m0 = r0 + mul32x32_64(p,19); r0 = (uint32_t)m0 & reduce_mask_26; p = (uint32_t)(m0 >> 26);
  213. r1 += p;
  214. out[0] = r0;
  215. out[1] = r1;
  216. out[2] = r2;
  217. out[3] = r3;
  218. out[4] = r4;
  219. out[5] = r5;
  220. out[6] = r6;
  221. out[7] = r7;
  222. out[8] = r8;
  223. out[9] = r9;
  224. }
  225. /* out = in*in */
  226. static void
  227. curve25519_square(bignum25519 out, const bignum25519 in) {
  228. uint32_t r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
  229. uint32_t d6,d7,d8,d9;
  230. uint64_t m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
  231. uint32_t p;
  232. r0 = in[0];
  233. r1 = in[1];
  234. r2 = in[2];
  235. r3 = in[3];
  236. r4 = in[4];
  237. r5 = in[5];
  238. r6 = in[6];
  239. r7 = in[7];
  240. r8 = in[8];
  241. r9 = in[9];
  242. m0 = mul32x32_64(r0, r0);
  243. r0 *= 2;
  244. m1 = mul32x32_64(r0, r1);
  245. m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
  246. r1 *= 2;
  247. m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
  248. m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
  249. r2 *= 2;
  250. m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
  251. m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
  252. r3 *= 2;
  253. m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
  254. m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
  255. m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
  256. d6 = r6 * 19;
  257. d7 = r7 * 2 * 19;
  258. d8 = r8 * 19;
  259. d9 = r9 * 2 * 19;
  260. m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
  261. m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
  262. m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
  263. m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
  264. m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
  265. m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
  266. m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
  267. m7 += (mul32x32_64(d9, r8 ));
  268. m8 += (mul32x32_64(d9, r9 ));
  269. r0 = (uint32_t)m0 & reduce_mask_26; c = (m0 >> 26);
  270. m1 += c; r1 = (uint32_t)m1 & reduce_mask_25; c = (m1 >> 25);
  271. m2 += c; r2 = (uint32_t)m2 & reduce_mask_26; c = (m2 >> 26);
  272. m3 += c; r3 = (uint32_t)m3 & reduce_mask_25; c = (m3 >> 25);
  273. m4 += c; r4 = (uint32_t)m4 & reduce_mask_26; c = (m4 >> 26);
  274. m5 += c; r5 = (uint32_t)m5 & reduce_mask_25; c = (m5 >> 25);
  275. m6 += c; r6 = (uint32_t)m6 & reduce_mask_26; c = (m6 >> 26);
  276. m7 += c; r7 = (uint32_t)m7 & reduce_mask_25; c = (m7 >> 25);
  277. m8 += c; r8 = (uint32_t)m8 & reduce_mask_26; c = (m8 >> 26);
  278. m9 += c; r9 = (uint32_t)m9 & reduce_mask_25; p = (uint32_t)(m9 >> 25);
  279. m0 = r0 + mul32x32_64(p,19); r0 = (uint32_t)m0 & reduce_mask_26; p = (uint32_t)(m0 >> 26);
  280. r1 += p;
  281. out[0] = r0;
  282. out[1] = r1;
  283. out[2] = r2;
  284. out[3] = r3;
  285. out[4] = r4;
  286. out[5] = r5;
  287. out[6] = r6;
  288. out[7] = r7;
  289. out[8] = r8;
  290. out[9] = r9;
  291. }
  292. /* out = in ^ (2 * count) */
  293. static void
  294. curve25519_square_times(bignum25519 out, const bignum25519 in, int count) {
  295. uint32_t r0,r1,r2,r3,r4,r5,r6,r7,r8,r9;
  296. uint32_t d6,d7,d8,d9;
  297. uint64_t m0,m1,m2,m3,m4,m5,m6,m7,m8,m9,c;
  298. uint32_t p;
  299. r0 = in[0];
  300. r1 = in[1];
  301. r2 = in[2];
  302. r3 = in[3];
  303. r4 = in[4];
  304. r5 = in[5];
  305. r6 = in[6];
  306. r7 = in[7];
  307. r8 = in[8];
  308. r9 = in[9];
  309. do {
  310. m0 = mul32x32_64(r0, r0);
  311. r0 *= 2;
  312. m1 = mul32x32_64(r0, r1);
  313. m2 = mul32x32_64(r0, r2) + mul32x32_64(r1, r1 * 2);
  314. r1 *= 2;
  315. m3 = mul32x32_64(r0, r3) + mul32x32_64(r1, r2 );
  316. m4 = mul32x32_64(r0, r4) + mul32x32_64(r1, r3 * 2) + mul32x32_64(r2, r2);
  317. r2 *= 2;
  318. m5 = mul32x32_64(r0, r5) + mul32x32_64(r1, r4 ) + mul32x32_64(r2, r3);
  319. m6 = mul32x32_64(r0, r6) + mul32x32_64(r1, r5 * 2) + mul32x32_64(r2, r4) + mul32x32_64(r3, r3 * 2);
  320. r3 *= 2;
  321. m7 = mul32x32_64(r0, r7) + mul32x32_64(r1, r6 ) + mul32x32_64(r2, r5) + mul32x32_64(r3, r4 );
  322. m8 = mul32x32_64(r0, r8) + mul32x32_64(r1, r7 * 2) + mul32x32_64(r2, r6) + mul32x32_64(r3, r5 * 2) + mul32x32_64(r4, r4 );
  323. m9 = mul32x32_64(r0, r9) + mul32x32_64(r1, r8 ) + mul32x32_64(r2, r7) + mul32x32_64(r3, r6 ) + mul32x32_64(r4, r5 * 2);
  324. d6 = r6 * 19;
  325. d7 = r7 * 2 * 19;
  326. d8 = r8 * 19;
  327. d9 = r9 * 2 * 19;
  328. m0 += (mul32x32_64(d9, r1 ) + mul32x32_64(d8, r2 ) + mul32x32_64(d7, r3 ) + mul32x32_64(d6, r4 * 2) + mul32x32_64(r5, r5 * 2 * 19));
  329. m1 += (mul32x32_64(d9, r2 / 2) + mul32x32_64(d8, r3 ) + mul32x32_64(d7, r4 ) + mul32x32_64(d6, r5 * 2));
  330. m2 += (mul32x32_64(d9, r3 ) + mul32x32_64(d8, r4 * 2) + mul32x32_64(d7, r5 * 2) + mul32x32_64(d6, r6 ));
  331. m3 += (mul32x32_64(d9, r4 ) + mul32x32_64(d8, r5 * 2) + mul32x32_64(d7, r6 ));
  332. m4 += (mul32x32_64(d9, r5 * 2) + mul32x32_64(d8, r6 * 2) + mul32x32_64(d7, r7 ));
  333. m5 += (mul32x32_64(d9, r6 ) + mul32x32_64(d8, r7 * 2));
  334. m6 += (mul32x32_64(d9, r7 * 2) + mul32x32_64(d8, r8 ));
  335. m7 += (mul32x32_64(d9, r8 ));
  336. m8 += (mul32x32_64(d9, r9 ));
  337. r0 = (uint32_t)m0 & reduce_mask_26; c = (m0 >> 26);
  338. m1 += c; r1 = (uint32_t)m1 & reduce_mask_25; c = (m1 >> 25);
  339. m2 += c; r2 = (uint32_t)m2 & reduce_mask_26; c = (m2 >> 26);
  340. m3 += c; r3 = (uint32_t)m3 & reduce_mask_25; c = (m3 >> 25);
  341. m4 += c; r4 = (uint32_t)m4 & reduce_mask_26; c = (m4 >> 26);
  342. m5 += c; r5 = (uint32_t)m5 & reduce_mask_25; c = (m5 >> 25);
  343. m6 += c; r6 = (uint32_t)m6 & reduce_mask_26; c = (m6 >> 26);
  344. m7 += c; r7 = (uint32_t)m7 & reduce_mask_25; c = (m7 >> 25);
  345. m8 += c; r8 = (uint32_t)m8 & reduce_mask_26; c = (m8 >> 26);
  346. m9 += c; r9 = (uint32_t)m9 & reduce_mask_25; p = (uint32_t)(m9 >> 25);
  347. m0 = r0 + mul32x32_64(p,19); r0 = (uint32_t)m0 & reduce_mask_26; p = (uint32_t)(m0 >> 26);
  348. r1 += p;
  349. } while (--count);
  350. out[0] = r0;
  351. out[1] = r1;
  352. out[2] = r2;
  353. out[3] = r3;
  354. out[4] = r4;
  355. out[5] = r5;
  356. out[6] = r6;
  357. out[7] = r7;
  358. out[8] = r8;
  359. out[9] = r9;
  360. }
  361. /* Take a little-endian, 32-byte number and expand it into polynomial form */
  362. static void
  363. curve25519_expand(bignum25519 out, const unsigned char in[32]) {
  364. static const union { uint8_t b[2]; uint16_t s; } endian_check = {{1,0}};
  365. uint32_t x0,x1,x2,x3,x4,x5,x6,x7;
  366. if (endian_check.s == 1) {
  367. x0 = *(uint32_t *)(in + 0);
  368. x1 = *(uint32_t *)(in + 4);
  369. x2 = *(uint32_t *)(in + 8);
  370. x3 = *(uint32_t *)(in + 12);
  371. x4 = *(uint32_t *)(in + 16);
  372. x5 = *(uint32_t *)(in + 20);
  373. x6 = *(uint32_t *)(in + 24);
  374. x7 = *(uint32_t *)(in + 28);
  375. } else {
  376. #define F(s) \
  377. ((((uint32_t)in[s + 0]) ) | \
  378. (((uint32_t)in[s + 1]) << 8) | \
  379. (((uint32_t)in[s + 2]) << 16) | \
  380. (((uint32_t)in[s + 3]) << 24))
  381. x0 = F(0);
  382. x1 = F(4);
  383. x2 = F(8);
  384. x3 = F(12);
  385. x4 = F(16);
  386. x5 = F(20);
  387. x6 = F(24);
  388. x7 = F(28);
  389. #undef F
  390. }
  391. out[0] = ( x0 ) & 0x3ffffff;
  392. out[1] = ((((uint64_t)x1 << 32) | x0) >> 26) & 0x1ffffff;
  393. out[2] = ((((uint64_t)x2 << 32) | x1) >> 19) & 0x3ffffff;
  394. out[3] = ((((uint64_t)x3 << 32) | x2) >> 13) & 0x1ffffff;
  395. out[4] = (( x3) >> 6) & 0x3ffffff;
  396. out[5] = ( x4 ) & 0x1ffffff;
  397. out[6] = ((((uint64_t)x5 << 32) | x4) >> 25) & 0x3ffffff;
  398. out[7] = ((((uint64_t)x6 << 32) | x5) >> 19) & 0x1ffffff;
  399. out[8] = ((((uint64_t)x7 << 32) | x6) >> 12) & 0x3ffffff;
  400. out[9] = (( x7) >> 6) & 0x1ffffff;
  401. }
  402. /* Take a fully reduced polynomial form number and contract it into a
  403. * little-endian, 32-byte array
  404. */
  405. static void
  406. curve25519_contract(unsigned char out[32], const bignum25519 in) {
  407. bignum25519 f;
  408. curve25519_copy(f, in);
  409. #define carry_pass() \
  410. f[1] += f[0] >> 26; f[0] &= reduce_mask_26; \
  411. f[2] += f[1] >> 25; f[1] &= reduce_mask_25; \
  412. f[3] += f[2] >> 26; f[2] &= reduce_mask_26; \
  413. f[4] += f[3] >> 25; f[3] &= reduce_mask_25; \
  414. f[5] += f[4] >> 26; f[4] &= reduce_mask_26; \
  415. f[6] += f[5] >> 25; f[5] &= reduce_mask_25; \
  416. f[7] += f[6] >> 26; f[6] &= reduce_mask_26; \
  417. f[8] += f[7] >> 25; f[7] &= reduce_mask_25; \
  418. f[9] += f[8] >> 26; f[8] &= reduce_mask_26;
  419. #define carry_pass_full() \
  420. carry_pass() \
  421. f[0] += 19 * (f[9] >> 25); f[9] &= reduce_mask_25;
  422. #define carry_pass_final() \
  423. carry_pass() \
  424. f[9] &= reduce_mask_25;
  425. carry_pass_full()
  426. carry_pass_full()
  427. /* now t is between 0 and 2^255-1, properly carried. */
  428. /* case 1: between 0 and 2^255-20. case 2: between 2^255-19 and 2^255-1. */
  429. f[0] += 19;
  430. carry_pass_full()
  431. /* now between 19 and 2^255-1 in both cases, and offset by 19. */
  432. f[0] += (reduce_mask_26 + 1) - 19;
  433. f[1] += (reduce_mask_25 + 1) - 1;
  434. f[2] += (reduce_mask_26 + 1) - 1;
  435. f[3] += (reduce_mask_25 + 1) - 1;
  436. f[4] += (reduce_mask_26 + 1) - 1;
  437. f[5] += (reduce_mask_25 + 1) - 1;
  438. f[6] += (reduce_mask_26 + 1) - 1;
  439. f[7] += (reduce_mask_25 + 1) - 1;
  440. f[8] += (reduce_mask_26 + 1) - 1;
  441. f[9] += (reduce_mask_25 + 1) - 1;
  442. /* now between 2^255 and 2^256-20, and offset by 2^255. */
  443. carry_pass_final()
  444. #undef carry_pass
  445. #undef carry_full
  446. #undef carry_final
  447. f[1] <<= 2;
  448. f[2] <<= 3;
  449. f[3] <<= 5;
  450. f[4] <<= 6;
  451. f[6] <<= 1;
  452. f[7] <<= 3;
  453. f[8] <<= 4;
  454. f[9] <<= 6;
  455. #define F(i, s) \
  456. out[s+0] |= (unsigned char )(f[i] & 0xff); \
  457. out[s+1] = (unsigned char )((f[i] >> 8) & 0xff); \
  458. out[s+2] = (unsigned char )((f[i] >> 16) & 0xff); \
  459. out[s+3] = (unsigned char )((f[i] >> 24) & 0xff);
  460. out[0] = 0;
  461. out[16] = 0;
  462. F(0,0);
  463. F(1,3);
  464. F(2,6);
  465. F(3,9);
  466. F(4,12);
  467. F(5,16);
  468. F(6,19);
  469. F(7,22);
  470. F(8,25);
  471. F(9,28);
  472. #undef F
  473. }
  474. /* out = (flag) ? in : out */
  475. DONNA_INLINE static void
  476. curve25519_move_conditional_bytes(uint8_t out[96], const uint8_t in[96], uint32_t flag) {
  477. const uint32_t nb = flag - 1, b = ~nb;
  478. const uint32_t *inl = (const uint32_t *)in;
  479. uint32_t *outl = (uint32_t *)out;
  480. outl[0] = (outl[0] & nb) | (inl[0] & b);
  481. outl[1] = (outl[1] & nb) | (inl[1] & b);
  482. outl[2] = (outl[2] & nb) | (inl[2] & b);
  483. outl[3] = (outl[3] & nb) | (inl[3] & b);
  484. outl[4] = (outl[4] & nb) | (inl[4] & b);
  485. outl[5] = (outl[5] & nb) | (inl[5] & b);
  486. outl[6] = (outl[6] & nb) | (inl[6] & b);
  487. outl[7] = (outl[7] & nb) | (inl[7] & b);
  488. outl[8] = (outl[8] & nb) | (inl[8] & b);
  489. outl[9] = (outl[9] & nb) | (inl[9] & b);
  490. outl[10] = (outl[10] & nb) | (inl[10] & b);
  491. outl[11] = (outl[11] & nb) | (inl[11] & b);
  492. outl[12] = (outl[12] & nb) | (inl[12] & b);
  493. outl[13] = (outl[13] & nb) | (inl[13] & b);
  494. outl[14] = (outl[14] & nb) | (inl[14] & b);
  495. outl[15] = (outl[15] & nb) | (inl[15] & b);
  496. outl[16] = (outl[16] & nb) | (inl[16] & b);
  497. outl[17] = (outl[17] & nb) | (inl[17] & b);
  498. outl[18] = (outl[18] & nb) | (inl[18] & b);
  499. outl[19] = (outl[19] & nb) | (inl[19] & b);
  500. outl[20] = (outl[20] & nb) | (inl[20] & b);
  501. outl[21] = (outl[21] & nb) | (inl[21] & b);
  502. outl[22] = (outl[22] & nb) | (inl[22] & b);
  503. outl[23] = (outl[23] & nb) | (inl[23] & b);
  504. }
  505. /* if (iswap) swap(a, b) */
  506. DONNA_INLINE static void
  507. curve25519_swap_conditional(bignum25519 a, bignum25519 b, uint32_t iswap) {
  508. const uint32_t swap = (uint32_t)(-(int32_t)iswap);
  509. uint32_t x0,x1,x2,x3,x4,x5,x6,x7,x8,x9;
  510. x0 = swap & (a[0] ^ b[0]); a[0] ^= x0; b[0] ^= x0;
  511. x1 = swap & (a[1] ^ b[1]); a[1] ^= x1; b[1] ^= x1;
  512. x2 = swap & (a[2] ^ b[2]); a[2] ^= x2; b[2] ^= x2;
  513. x3 = swap & (a[3] ^ b[3]); a[3] ^= x3; b[3] ^= x3;
  514. x4 = swap & (a[4] ^ b[4]); a[4] ^= x4; b[4] ^= x4;
  515. x5 = swap & (a[5] ^ b[5]); a[5] ^= x5; b[5] ^= x5;
  516. x6 = swap & (a[6] ^ b[6]); a[6] ^= x6; b[6] ^= x6;
  517. x7 = swap & (a[7] ^ b[7]); a[7] ^= x7; b[7] ^= x7;
  518. x8 = swap & (a[8] ^ b[8]); a[8] ^= x8; b[8] ^= x8;
  519. x9 = swap & (a[9] ^ b[9]); a[9] ^= x9; b[9] ^= x9;
  520. }
  521. #endif /* !defined(ED25519_SSE2) && !defined(ED25519_64BIT) */