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.
 
 
 
 
 

457 lines
10 KiB

  1. /* Copyright (c) 2014 Cryptography Research, Inc.
  2. * Released under the MIT License. See LICENSE.txt for license information.
  3. */
  4. #include "p448.h"
  5. #include "x86-64-arith.h"
  6. void
  7. p448_mul (
  8. p448_t *__restrict__ cs,
  9. const p448_t *as,
  10. const p448_t *bs
  11. ) {
  12. const uint64_t *a = as->limb, *b = bs->limb;
  13. uint64_t *c = cs->limb;
  14. __uint128_t accum0 = 0, accum1 = 0, accum2;
  15. uint64_t mask = (1ull<<56) - 1;
  16. uint64_t aa[4] __attribute__((aligned(32))), bb[4] __attribute__((aligned(32))), bbb[4] __attribute__((aligned(32)));
  17. /* For some reason clang doesn't vectorize this without prompting? */
  18. unsigned int i;
  19. for (i=0; i<sizeof(aa)/sizeof(uint64xn_t); i++) {
  20. ((uint64xn_t*)aa)[i] = ((const uint64xn_t*)a)[i] + ((const uint64xn_t*)(&a[4]))[i];
  21. ((uint64xn_t*)bb)[i] = ((const uint64xn_t*)b)[i] + ((const uint64xn_t*)(&b[4]))[i];
  22. ((uint64xn_t*)bbb)[i] = ((const uint64xn_t*)bb)[i] + ((const uint64xn_t*)(&b[4]))[i];
  23. }
  24. /*
  25. for (int i=0; i<4; i++) {
  26. aa[i] = a[i] + a[i+4];
  27. bb[i] = b[i] + b[i+4];
  28. }
  29. */
  30. accum2 = widemul(&a[0],&b[3]);
  31. accum0 = widemul(&aa[0],&bb[3]);
  32. accum1 = widemul(&a[4],&b[7]);
  33. mac(&accum2, &a[1], &b[2]);
  34. mac(&accum0, &aa[1], &bb[2]);
  35. mac(&accum1, &a[5], &b[6]);
  36. mac(&accum2, &a[2], &b[1]);
  37. mac(&accum0, &aa[2], &bb[1]);
  38. mac(&accum1, &a[6], &b[5]);
  39. mac(&accum2, &a[3], &b[0]);
  40. mac(&accum0, &aa[3], &bb[0]);
  41. mac(&accum1, &a[7], &b[4]);
  42. accum0 -= accum2;
  43. accum1 += accum2;
  44. c[3] = ((uint64_t)(accum1)) & mask;
  45. c[7] = ((uint64_t)(accum0)) & mask;
  46. accum0 >>= 56;
  47. accum1 >>= 56;
  48. mac(&accum0, &aa[1],&bb[3]);
  49. mac(&accum1, &a[5], &b[7]);
  50. mac(&accum0, &aa[2], &bb[2]);
  51. mac(&accum1, &a[6], &b[6]);
  52. mac(&accum0, &aa[3], &bb[1]);
  53. accum1 += accum0;
  54. accum2 = widemul(&a[0],&b[0]);
  55. accum1 -= accum2;
  56. accum0 += accum2;
  57. msb(&accum0, &a[1], &b[3]);
  58. msb(&accum0, &a[2], &b[2]);
  59. mac(&accum1, &a[7], &b[5]);
  60. msb(&accum0, &a[3], &b[1]);
  61. mac(&accum1, &aa[0], &bb[0]);
  62. mac(&accum0, &a[4], &b[4]);
  63. c[0] = ((uint64_t)(accum0)) & mask;
  64. c[4] = ((uint64_t)(accum1)) & mask;
  65. accum0 >>= 56;
  66. accum1 >>= 56;
  67. accum2 = widemul(&a[2],&b[7]);
  68. mac(&accum0, &a[6], &bb[3]);
  69. mac(&accum1, &aa[2], &bbb[3]);
  70. mac(&accum2, &a[3], &b[6]);
  71. mac(&accum0, &a[7], &bb[2]);
  72. mac(&accum1, &aa[3], &bbb[2]);
  73. mac(&accum2, &a[0],&b[1]);
  74. mac(&accum1, &aa[0], &bb[1]);
  75. mac(&accum0, &a[4], &b[5]);
  76. mac(&accum2, &a[1], &b[0]);
  77. mac(&accum1, &aa[1], &bb[0]);
  78. mac(&accum0, &a[5], &b[4]);
  79. accum1 -= accum2;
  80. accum0 += accum2;
  81. c[1] = ((uint64_t)(accum0)) & mask;
  82. c[5] = ((uint64_t)(accum1)) & mask;
  83. accum0 >>= 56;
  84. accum1 >>= 56;
  85. accum2 = widemul(&a[3],&b[7]);
  86. mac(&accum0, &a[7], &bb[3]);
  87. mac(&accum1, &aa[3], &bbb[3]);
  88. mac(&accum2, &a[0],&b[2]);
  89. mac(&accum1, &aa[0], &bb[2]);
  90. mac(&accum0, &a[4], &b[6]);
  91. mac(&accum2, &a[1], &b[1]);
  92. mac(&accum1, &aa[1], &bb[1]);
  93. mac(&accum0, &a[5], &b[5]);
  94. mac(&accum2, &a[2], &b[0]);
  95. mac(&accum1, &aa[2], &bb[0]);
  96. mac(&accum0, &a[6], &b[4]);
  97. accum1 -= accum2;
  98. accum0 += accum2;
  99. c[2] = ((uint64_t)(accum0)) & mask;
  100. c[6] = ((uint64_t)(accum1)) & mask;
  101. accum0 >>= 56;
  102. accum1 >>= 56;
  103. accum0 += c[3];
  104. accum1 += c[7];
  105. c[3] = ((uint64_t)(accum0)) & mask;
  106. c[7] = ((uint64_t)(accum1)) & mask;
  107. /* we could almost stop here, but it wouldn't be stable, so... */
  108. accum0 >>= 56;
  109. accum1 >>= 56;
  110. c[4] += ((uint64_t)(accum0)) + ((uint64_t)(accum1));
  111. c[0] += ((uint64_t)(accum1));
  112. }
  113. void
  114. p448_mulw (
  115. p448_t *__restrict__ cs,
  116. const p448_t *as,
  117. uint64_t b
  118. ) {
  119. const uint64_t *a = as->limb;
  120. uint64_t *c = cs->limb;
  121. __uint128_t accum0, accum4;
  122. uint64_t mask = (1ull<<56) - 1;
  123. accum0 = widemul_rm(b, &a[0]);
  124. accum4 = widemul_rm(b, &a[4]);
  125. c[0] = accum0 & mask; accum0 >>= 56;
  126. c[4] = accum4 & mask; accum4 >>= 56;
  127. mac_rm(&accum0, b, &a[1]);
  128. mac_rm(&accum4, b, &a[5]);
  129. c[1] = accum0 & mask; accum0 >>= 56;
  130. c[5] = accum4 & mask; accum4 >>= 56;
  131. mac_rm(&accum0, b, &a[2]);
  132. mac_rm(&accum4, b, &a[6]);
  133. c[2] = accum0 & mask; accum0 >>= 56;
  134. c[6] = accum4 & mask; accum4 >>= 56;
  135. mac_rm(&accum0, b, &a[3]);
  136. mac_rm(&accum4, b, &a[7]);
  137. c[3] = accum0 & mask; accum0 >>= 56;
  138. c[7] = accum4 & mask; accum4 >>= 56;
  139. accum0 += accum4 + c[4];
  140. c[4] = accum0 & mask;
  141. c[5] += accum0 >> 56;
  142. accum4 += c[0];
  143. c[0] = accum4 & mask;
  144. c[1] += accum4 >> 56;
  145. }
  146. void
  147. p448_sqr (
  148. p448_t *__restrict__ cs,
  149. const p448_t *as
  150. ) {
  151. const uint64_t *a = as->limb;
  152. uint64_t *c = cs->limb;
  153. __uint128_t accum0 = 0, accum1 = 0, accum2;
  154. uint64_t mask = (1ull<<56) - 1;
  155. uint64_t aa[4] __attribute__((aligned(32)));
  156. /* For some reason clang doesn't vectorize this without prompting? */
  157. unsigned int i;
  158. for (i=0; i<sizeof(aa)/sizeof(uint64xn_t); i++) {
  159. ((uint64xn_t*)aa)[i] = ((const uint64xn_t*)a)[i] + ((const uint64xn_t*)(&a[4]))[i];
  160. }
  161. accum2 = widemul(&a[0],&a[3]);
  162. accum0 = widemul(&aa[0],&aa[3]);
  163. accum1 = widemul(&a[4],&a[7]);
  164. mac(&accum2, &a[1], &a[2]);
  165. mac(&accum0, &aa[1], &aa[2]);
  166. mac(&accum1, &a[5], &a[6]);
  167. accum0 -= accum2;
  168. accum1 += accum2;
  169. c[3] = ((uint64_t)(accum1))<<1 & mask;
  170. c[7] = ((uint64_t)(accum0))<<1 & mask;
  171. accum0 >>= 55;
  172. accum1 >>= 55;
  173. mac2(&accum0, &aa[1],&aa[3]);
  174. mac2(&accum1, &a[5], &a[7]);
  175. mac(&accum0, &aa[2], &aa[2]);
  176. accum1 += accum0;
  177. msb2(&accum0, &a[1], &a[3]);
  178. mac(&accum1, &a[6], &a[6]);
  179. accum2 = widemul(&a[0],&a[0]);
  180. accum1 -= accum2;
  181. accum0 += accum2;
  182. msb(&accum0, &a[2], &a[2]);
  183. mac(&accum1, &aa[0], &aa[0]);
  184. mac(&accum0, &a[4], &a[4]);
  185. c[0] = ((uint64_t)(accum0)) & mask;
  186. c[4] = ((uint64_t)(accum1)) & mask;
  187. accum0 >>= 56;
  188. accum1 >>= 56;
  189. accum2 = widemul2(&aa[2],&aa[3]);
  190. msb2(&accum0, &a[2], &a[3]);
  191. mac2(&accum1, &a[6], &a[7]);
  192. accum1 += accum2;
  193. accum0 += accum2;
  194. accum2 = widemul2(&a[0],&a[1]);
  195. mac2(&accum1, &aa[0], &aa[1]);
  196. mac2(&accum0, &a[4], &a[5]);
  197. accum1 -= accum2;
  198. accum0 += accum2;
  199. c[1] = ((uint64_t)(accum0)) & mask;
  200. c[5] = ((uint64_t)(accum1)) & mask;
  201. accum0 >>= 56;
  202. accum1 >>= 56;
  203. accum2 = widemul(&aa[3],&aa[3]);
  204. msb(&accum0, &a[3], &a[3]);
  205. mac(&accum1, &a[7], &a[7]);
  206. accum1 += accum2;
  207. accum0 += accum2;
  208. accum2 = widemul2(&a[0],&a[2]);
  209. mac2(&accum1, &aa[0], &aa[2]);
  210. mac2(&accum0, &a[4], &a[6]);
  211. mac(&accum2, &a[1], &a[1]);
  212. mac(&accum1, &aa[1], &aa[1]);
  213. mac(&accum0, &a[5], &a[5]);
  214. accum1 -= accum2;
  215. accum0 += accum2;
  216. c[2] = ((uint64_t)(accum0)) & mask;
  217. c[6] = ((uint64_t)(accum1)) & mask;
  218. accum0 >>= 56;
  219. accum1 >>= 56;
  220. accum0 += c[3];
  221. accum1 += c[7];
  222. c[3] = ((uint64_t)(accum0)) & mask;
  223. c[7] = ((uint64_t)(accum1)) & mask;
  224. /* we could almost stop here, but it wouldn't be stable, so... */
  225. accum0 >>= 56;
  226. accum1 >>= 56;
  227. c[4] += ((uint64_t)(accum0)) + ((uint64_t)(accum1));
  228. c[0] += ((uint64_t)(accum1));
  229. }
  230. void
  231. p448_strong_reduce (
  232. p448_t *a
  233. ) {
  234. uint64_t mask = (1ull<<56)-1;
  235. /* first, clear high */
  236. a->limb[4] += a->limb[7]>>56;
  237. a->limb[0] += a->limb[7]>>56;
  238. a->limb[7] &= mask;
  239. /* now the total is less than 2^448 - 2^(448-56) + 2^(448-56+8) < 2p */
  240. /* compute total_value - p. No need to reduce mod p. */
  241. __int128_t scarry = 0;
  242. int i;
  243. for (i=0; i<8; i++) {
  244. scarry = scarry + a->limb[i] - ((i==4)?mask-1:mask);
  245. a->limb[i] = scarry & mask;
  246. scarry >>= 56;
  247. }
  248. /* uncommon case: it was >= p, so now scarry = 0 and this = x
  249. * common case: it was < p, so now scarry = -1 and this = x - p + 2^448
  250. * so let's add back in p. will carry back off the top for 2^448.
  251. */
  252. assert(is_zero(scarry) | is_zero(scarry+1));
  253. uint64_t scarry_mask = scarry & mask;
  254. __uint128_t carry = 0;
  255. /* add it back */
  256. for (i=0; i<8; i++) {
  257. carry = carry + a->limb[i] + ((i==4)?(scarry_mask&~1):scarry_mask);
  258. a->limb[i] = carry & mask;
  259. carry >>= 56;
  260. }
  261. assert(is_zero(carry + scarry));
  262. }
  263. mask_t
  264. p448_is_zero (
  265. const struct p448_t *a
  266. ) {
  267. struct p448_t b;
  268. p448_copy(&b,a);
  269. p448_strong_reduce(&b);
  270. uint64_t any = 0;
  271. int i;
  272. for (i=0; i<8; i++) {
  273. any |= b.limb[i];
  274. }
  275. return is_zero(any);
  276. }
  277. void
  278. p448_serialize (
  279. uint8_t *serial,
  280. const struct p448_t *x
  281. ) {
  282. int i,j;
  283. p448_t red;
  284. p448_copy(&red, x);
  285. p448_strong_reduce(&red);
  286. for (i=0; i<8; i++) {
  287. for (j=0; j<7; j++) {
  288. serial[7*i+j] = red.limb[i];
  289. red.limb[i] >>= 8;
  290. }
  291. assert(red.limb[i] == 0);
  292. }
  293. }
  294. mask_t
  295. p448_deserialize (
  296. p448_t *x,
  297. const uint8_t serial[56]
  298. ) {
  299. int i,j;
  300. for (i=0; i<8; i++) {
  301. word_t out = 0;
  302. for (j=0; j<7; j++) {
  303. out |= ((word_t)serial[7*i+j])<<(8*j);
  304. }
  305. x->limb[i] = out;
  306. }
  307. /* Check for reduction.
  308. *
  309. * The idea is to create a variable ge which is all ones (rather, 56 ones)
  310. * if and only if the low $i$ words of $x$ are >= those of p.
  311. *
  312. * Remember p = little_endian(1111,1111,1111,1111,1110,1111,1111,1111)
  313. */
  314. word_t ge = -1, mask = (1ull<<56)-1;
  315. for (i=0; i<4; i++) {
  316. ge &= x->limb[i];
  317. }
  318. /* At this point, ge = 1111 iff bottom are all 1111. Now propagate if 1110, or set if 1111 */
  319. ge = (ge & (x->limb[4] + 1)) | is_zero(x->limb[4] ^ mask);
  320. /* Propagate the rest */
  321. for (i=5; i<8; i++) {
  322. ge &= x->limb[i];
  323. }
  324. return ~is_zero(ge ^ mask);
  325. }
  326. void
  327. simultaneous_invert_p448(
  328. struct p448_t *__restrict__ out,
  329. const struct p448_t *in,
  330. unsigned int n
  331. ) {
  332. if (n==0) {
  333. return;
  334. } else if (n==1) {
  335. p448_inverse(out,in);
  336. return;
  337. }
  338. p448_copy(&out[1], &in[0]);
  339. int i;
  340. for (i=1; i<(int) (n-1); i++) {
  341. p448_mul(&out[i+1], &out[i], &in[i]);
  342. }
  343. p448_mul(&out[0], &out[n-1], &in[n-1]);
  344. struct p448_t tmp;
  345. p448_inverse(&tmp, &out[0]);
  346. p448_copy(&out[0], &tmp);
  347. /* at this point, out[0] = product(in[i]) ^ -1
  348. * out[i] = product(in[0]..in[i-1]) if i != 0
  349. */
  350. for (i=n-1; i>0; i--) {
  351. p448_mul(&tmp, &out[i], &out[0]);
  352. p448_copy(&out[i], &tmp);
  353. p448_mul(&tmp, &out[0], &in[i]);
  354. p448_copy(&out[0], &tmp);
  355. }
  356. }