Nat128.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. #if !BESTHTTP_DISABLE_ALTERNATE_SSL && (!UNITY_WEBGL || UNITY_EDITOR)
  2. using System;
  3. using System.Diagnostics;
  4. using Org.BouncyCastle.Crypto.Utilities;
  5. namespace Org.BouncyCastle.Math.Raw
  6. {
  7. internal abstract class Nat128
  8. {
  9. private const ulong M = 0xFFFFFFFFUL;
  10. public static uint Add(uint[] x, uint[] y, uint[] z)
  11. {
  12. ulong c = 0;
  13. c += (ulong)x[0] + y[0];
  14. z[0] = (uint)c;
  15. c >>= 32;
  16. c += (ulong)x[1] + y[1];
  17. z[1] = (uint)c;
  18. c >>= 32;
  19. c += (ulong)x[2] + y[2];
  20. z[2] = (uint)c;
  21. c >>= 32;
  22. c += (ulong)x[3] + y[3];
  23. z[3] = (uint)c;
  24. c >>= 32;
  25. return (uint)c;
  26. }
  27. public static uint AddBothTo(uint[] x, uint[] y, uint[] z)
  28. {
  29. ulong c = 0;
  30. c += (ulong)x[0] + y[0] + z[0];
  31. z[0] = (uint)c;
  32. c >>= 32;
  33. c += (ulong)x[1] + y[1] + z[1];
  34. z[1] = (uint)c;
  35. c >>= 32;
  36. c += (ulong)x[2] + y[2] + z[2];
  37. z[2] = (uint)c;
  38. c >>= 32;
  39. c += (ulong)x[3] + y[3] + z[3];
  40. z[3] = (uint)c;
  41. c >>= 32;
  42. return (uint)c;
  43. }
  44. public static uint AddTo(uint[] x, uint[] z)
  45. {
  46. ulong c = 0;
  47. c += (ulong)x[0] + z[0];
  48. z[0] = (uint)c;
  49. c >>= 32;
  50. c += (ulong)x[1] + z[1];
  51. z[1] = (uint)c;
  52. c >>= 32;
  53. c += (ulong)x[2] + z[2];
  54. z[2] = (uint)c;
  55. c >>= 32;
  56. c += (ulong)x[3] + z[3];
  57. z[3] = (uint)c;
  58. c >>= 32;
  59. return (uint)c;
  60. }
  61. public static uint AddTo(uint[] x, int xOff, uint[] z, int zOff, uint cIn)
  62. {
  63. ulong c = cIn;
  64. c += (ulong)x[xOff + 0] + z[zOff + 0];
  65. z[zOff + 0] = (uint)c;
  66. c >>= 32;
  67. c += (ulong)x[xOff + 1] + z[zOff + 1];
  68. z[zOff + 1] = (uint)c;
  69. c >>= 32;
  70. c += (ulong)x[xOff + 2] + z[zOff + 2];
  71. z[zOff + 2] = (uint)c;
  72. c >>= 32;
  73. c += (ulong)x[xOff + 3] + z[zOff + 3];
  74. z[zOff + 3] = (uint)c;
  75. c >>= 32;
  76. return (uint)c;
  77. }
  78. public static uint AddToEachOther(uint[] u, int uOff, uint[] v, int vOff)
  79. {
  80. ulong c = 0;
  81. c += (ulong)u[uOff + 0] + v[vOff + 0];
  82. u[uOff + 0] = (uint)c;
  83. v[vOff + 0] = (uint)c;
  84. c >>= 32;
  85. c += (ulong)u[uOff + 1] + v[vOff + 1];
  86. u[uOff + 1] = (uint)c;
  87. v[vOff + 1] = (uint)c;
  88. c >>= 32;
  89. c += (ulong)u[uOff + 2] + v[vOff + 2];
  90. u[uOff + 2] = (uint)c;
  91. v[vOff + 2] = (uint)c;
  92. c >>= 32;
  93. c += (ulong)u[uOff + 3] + v[vOff + 3];
  94. u[uOff + 3] = (uint)c;
  95. v[vOff + 3] = (uint)c;
  96. c >>= 32;
  97. return (uint)c;
  98. }
  99. public static void Copy(uint[] x, uint[] z)
  100. {
  101. z[0] = x[0];
  102. z[1] = x[1];
  103. z[2] = x[2];
  104. z[3] = x[3];
  105. }
  106. public static void Copy64(ulong[] x, ulong[] z)
  107. {
  108. z[0] = x[0];
  109. z[1] = x[1];
  110. }
  111. public static uint[] Create()
  112. {
  113. return new uint[4];
  114. }
  115. public static ulong[] Create64()
  116. {
  117. return new ulong[2];
  118. }
  119. public static uint[] CreateExt()
  120. {
  121. return new uint[8];
  122. }
  123. public static ulong[] CreateExt64()
  124. {
  125. return new ulong[4];
  126. }
  127. public static bool Diff(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  128. {
  129. bool pos = Gte(x, xOff, y, yOff);
  130. if (pos)
  131. {
  132. Sub(x, xOff, y, yOff, z, zOff);
  133. }
  134. else
  135. {
  136. Sub(y, yOff, x, xOff, z, zOff);
  137. }
  138. return pos;
  139. }
  140. public static bool Eq(uint[] x, uint[] y)
  141. {
  142. for (int i = 3; i >= 0; --i)
  143. {
  144. if (x[i] != y[i])
  145. return false;
  146. }
  147. return true;
  148. }
  149. public static bool Eq64(ulong[] x, ulong[] y)
  150. {
  151. for (int i = 1; i >= 0; --i)
  152. {
  153. if (x[i] != y[i])
  154. return false;
  155. }
  156. return true;
  157. }
  158. public static uint[] FromBigInteger(BigInteger x)
  159. {
  160. if (x.SignValue < 0 || x.BitLength > 128)
  161. throw new ArgumentException();
  162. uint[] z = Create();
  163. int i = 0;
  164. while (x.SignValue != 0)
  165. {
  166. z[i++] = (uint)x.IntValue;
  167. x = x.ShiftRight(32);
  168. }
  169. return z;
  170. }
  171. public static ulong[] FromBigInteger64(BigInteger x)
  172. {
  173. if (x.SignValue < 0 || x.BitLength > 128)
  174. throw new ArgumentException();
  175. ulong[] z = Create64();
  176. int i = 0;
  177. while (x.SignValue != 0)
  178. {
  179. z[i++] = (ulong)x.LongValue;
  180. x = x.ShiftRight(64);
  181. }
  182. return z;
  183. }
  184. public static uint GetBit(uint[] x, int bit)
  185. {
  186. if (bit == 0)
  187. {
  188. return x[0] & 1;
  189. }
  190. if ((bit & 127) != bit)
  191. {
  192. return 0;
  193. }
  194. int w = bit >> 5;
  195. int b = bit & 31;
  196. return (x[w] >> b) & 1;
  197. }
  198. public static bool Gte(uint[] x, uint[] y)
  199. {
  200. for (int i = 3; i >= 0; --i)
  201. {
  202. uint x_i = x[i], y_i = y[i];
  203. if (x_i < y_i)
  204. return false;
  205. if (x_i > y_i)
  206. return true;
  207. }
  208. return true;
  209. }
  210. public static bool Gte(uint[] x, int xOff, uint[] y, int yOff)
  211. {
  212. for (int i = 3; i >= 0; --i)
  213. {
  214. uint x_i = x[xOff + i], y_i = y[yOff + i];
  215. if (x_i < y_i)
  216. return false;
  217. if (x_i > y_i)
  218. return true;
  219. }
  220. return true;
  221. }
  222. public static bool IsOne(uint[] x)
  223. {
  224. if (x[0] != 1)
  225. {
  226. return false;
  227. }
  228. for (int i = 1; i < 4; ++i)
  229. {
  230. if (x[i] != 0)
  231. {
  232. return false;
  233. }
  234. }
  235. return true;
  236. }
  237. public static bool IsOne64(ulong[] x)
  238. {
  239. if (x[0] != 1UL)
  240. {
  241. return false;
  242. }
  243. for (int i = 1; i < 2; ++i)
  244. {
  245. if (x[i] != 0UL)
  246. {
  247. return false;
  248. }
  249. }
  250. return true;
  251. }
  252. public static bool IsZero(uint[] x)
  253. {
  254. for (int i = 0; i < 4; ++i)
  255. {
  256. if (x[i] != 0)
  257. {
  258. return false;
  259. }
  260. }
  261. return true;
  262. }
  263. public static bool IsZero64(ulong[] x)
  264. {
  265. for (int i = 0; i < 2; ++i)
  266. {
  267. if (x[i] != 0UL)
  268. {
  269. return false;
  270. }
  271. }
  272. return true;
  273. }
  274. public static void Mul(uint[] x, uint[] y, uint[] zz)
  275. {
  276. ulong y_0 = y[0];
  277. ulong y_1 = y[1];
  278. ulong y_2 = y[2];
  279. ulong y_3 = y[3];
  280. {
  281. ulong c = 0, x_0 = x[0];
  282. c += x_0 * y_0;
  283. zz[0] = (uint)c;
  284. c >>= 32;
  285. c += x_0 * y_1;
  286. zz[1] = (uint)c;
  287. c >>= 32;
  288. c += x_0 * y_2;
  289. zz[2] = (uint)c;
  290. c >>= 32;
  291. c += x_0 * y_3;
  292. zz[3] = (uint)c;
  293. c >>= 32;
  294. zz[4] = (uint)c;
  295. }
  296. for (int i = 1; i < 4; ++i)
  297. {
  298. ulong c = 0, x_i = x[i];
  299. c += x_i * y_0 + zz[i + 0];
  300. zz[i + 0] = (uint)c;
  301. c >>= 32;
  302. c += x_i * y_1 + zz[i + 1];
  303. zz[i + 1] = (uint)c;
  304. c >>= 32;
  305. c += x_i * y_2 + zz[i + 2];
  306. zz[i + 2] = (uint)c;
  307. c >>= 32;
  308. c += x_i * y_3 + zz[i + 3];
  309. zz[i + 3] = (uint)c;
  310. c >>= 32;
  311. zz[i + 4] = (uint)c;
  312. }
  313. }
  314. public static void Mul(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  315. {
  316. ulong y_0 = y[yOff + 0];
  317. ulong y_1 = y[yOff + 1];
  318. ulong y_2 = y[yOff + 2];
  319. ulong y_3 = y[yOff + 3];
  320. {
  321. ulong c = 0, x_0 = x[xOff + 0];
  322. c += x_0 * y_0;
  323. zz[zzOff + 0] = (uint)c;
  324. c >>= 32;
  325. c += x_0 * y_1;
  326. zz[zzOff + 1] = (uint)c;
  327. c >>= 32;
  328. c += x_0 * y_2;
  329. zz[zzOff + 2] = (uint)c;
  330. c >>= 32;
  331. c += x_0 * y_3;
  332. zz[zzOff + 3] = (uint)c;
  333. c >>= 32;
  334. zz[zzOff + 4] = (uint)c;
  335. }
  336. for (int i = 1; i < 4; ++i)
  337. {
  338. ++zzOff;
  339. ulong c = 0, x_i = x[xOff + i];
  340. c += x_i * y_0 + zz[zzOff + 0];
  341. zz[zzOff + 0] = (uint)c;
  342. c >>= 32;
  343. c += x_i * y_1 + zz[zzOff + 1];
  344. zz[zzOff + 1] = (uint)c;
  345. c >>= 32;
  346. c += x_i * y_2 + zz[zzOff + 2];
  347. zz[zzOff + 2] = (uint)c;
  348. c >>= 32;
  349. c += x_i * y_3 + zz[zzOff + 3];
  350. zz[zzOff + 3] = (uint)c;
  351. c >>= 32;
  352. zz[zzOff + 4] = (uint)c;
  353. }
  354. }
  355. public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
  356. {
  357. ulong y_0 = y[0];
  358. ulong y_1 = y[1];
  359. ulong y_2 = y[2];
  360. ulong y_3 = y[3];
  361. ulong zc = 0;
  362. for (int i = 0; i < 4; ++i)
  363. {
  364. ulong c = 0, x_i = x[i];
  365. c += x_i * y_0 + zz[i + 0];
  366. zz[i + 0] = (uint)c;
  367. c >>= 32;
  368. c += x_i * y_1 + zz[i + 1];
  369. zz[i + 1] = (uint)c;
  370. c >>= 32;
  371. c += x_i * y_2 + zz[i + 2];
  372. zz[i + 2] = (uint)c;
  373. c >>= 32;
  374. c += x_i * y_3 + zz[i + 3];
  375. zz[i + 3] = (uint)c;
  376. c >>= 32;
  377. c += zc + zz[i + 4];
  378. zz[i + 4] = (uint)c;
  379. zc = c >> 32;
  380. }
  381. return (uint)zc;
  382. }
  383. public static uint MulAddTo(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  384. {
  385. ulong y_0 = y[yOff + 0];
  386. ulong y_1 = y[yOff + 1];
  387. ulong y_2 = y[yOff + 2];
  388. ulong y_3 = y[yOff + 3];
  389. ulong zc = 0;
  390. for (int i = 0; i < 4; ++i)
  391. {
  392. ulong c = 0, x_i = x[xOff + i];
  393. c += x_i * y_0 + zz[zzOff + 0];
  394. zz[zzOff + 0] = (uint)c;
  395. c >>= 32;
  396. c += x_i * y_1 + zz[zzOff + 1];
  397. zz[zzOff + 1] = (uint)c;
  398. c >>= 32;
  399. c += x_i * y_2 + zz[zzOff + 2];
  400. zz[zzOff + 2] = (uint)c;
  401. c >>= 32;
  402. c += x_i * y_3 + zz[zzOff + 3];
  403. zz[zzOff + 3] = (uint)c;
  404. c >>= 32;
  405. c += zc + zz[zzOff + 4];
  406. zz[zzOff + 4] = (uint)c;
  407. zc = c >> 32;
  408. ++zzOff;
  409. }
  410. return (uint)zc;
  411. }
  412. public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  413. {
  414. Debug.Assert(w >> 31 == 0);
  415. ulong c = 0, wVal = w;
  416. ulong x0 = x[xOff + 0];
  417. c += wVal * x0 + y[yOff + 0];
  418. z[zOff + 0] = (uint)c;
  419. c >>= 32;
  420. ulong x1 = x[xOff + 1];
  421. c += wVal * x1 + x0 + y[yOff + 1];
  422. z[zOff + 1] = (uint)c;
  423. c >>= 32;
  424. ulong x2 = x[xOff + 2];
  425. c += wVal * x2 + x1 + y[yOff + 2];
  426. z[zOff + 2] = (uint)c;
  427. c >>= 32;
  428. ulong x3 = x[xOff + 3];
  429. c += wVal * x3 + x2 + y[yOff + 3];
  430. z[zOff + 3] = (uint)c;
  431. c >>= 32;
  432. c += x3;
  433. return c;
  434. }
  435. public static uint MulWordAddExt(uint x, uint[] yy, int yyOff, uint[] zz, int zzOff)
  436. {
  437. Debug.Assert(yyOff <= 4);
  438. Debug.Assert(zzOff <= 4);
  439. ulong c = 0, xVal = x;
  440. c += xVal * yy[yyOff + 0] + zz[zzOff + 0];
  441. zz[zzOff + 0] = (uint)c;
  442. c >>= 32;
  443. c += xVal * yy[yyOff + 1] + zz[zzOff + 1];
  444. zz[zzOff + 1] = (uint)c;
  445. c >>= 32;
  446. c += xVal * yy[yyOff + 2] + zz[zzOff + 2];
  447. zz[zzOff + 2] = (uint)c;
  448. c >>= 32;
  449. c += xVal * yy[yyOff + 3] + zz[zzOff + 3];
  450. zz[zzOff + 3] = (uint)c;
  451. c >>= 32;
  452. return (uint)c;
  453. }
  454. public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
  455. {
  456. Debug.Assert(x >> 31 == 0);
  457. Debug.Assert(zOff <= 0);
  458. ulong c = 0, xVal = x;
  459. ulong y00 = y & M;
  460. c += xVal * y00 + z[zOff + 0];
  461. z[zOff + 0] = (uint)c;
  462. c >>= 32;
  463. ulong y01 = y >> 32;
  464. c += xVal * y01 + y00 + z[zOff + 1];
  465. z[zOff + 1] = (uint)c;
  466. c >>= 32;
  467. c += y01 + z[zOff + 2];
  468. z[zOff + 2] = (uint)c;
  469. c >>= 32;
  470. c += z[zOff + 3];
  471. z[zOff + 3] = (uint)c;
  472. c >>= 32;
  473. return (uint)c;
  474. }
  475. public static uint Mul33WordAdd(uint x, uint y, uint[] z, int zOff)
  476. {
  477. Debug.Assert(x >> 31 == 0);
  478. Debug.Assert(zOff <= 1);
  479. ulong c = 0, yVal = y;
  480. c += yVal * x + z[zOff + 0];
  481. z[zOff + 0] = (uint)c;
  482. c >>= 32;
  483. c += yVal + z[zOff + 1];
  484. z[zOff + 1] = (uint)c;
  485. c >>= 32;
  486. c += z[zOff + 2];
  487. z[zOff + 2] = (uint)c;
  488. c >>= 32;
  489. return c == 0 ? 0 : Nat.IncAt(4, z, zOff, 3);
  490. }
  491. public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
  492. {
  493. Debug.Assert(zOff <= 1);
  494. ulong c = 0, xVal = x;
  495. c += xVal * y + z[zOff + 0];
  496. z[zOff + 0] = (uint)c;
  497. c >>= 32;
  498. c += xVal * (y >> 32) + z[zOff + 1];
  499. z[zOff + 1] = (uint)c;
  500. c >>= 32;
  501. c += z[zOff + 2];
  502. z[zOff + 2] = (uint)c;
  503. c >>= 32;
  504. return c == 0 ? 0 : Nat.IncAt(4, z, zOff, 3);
  505. }
  506. public static uint MulWordsAdd(uint x, uint y, uint[] z, int zOff)
  507. {
  508. Debug.Assert(zOff <= 2);
  509. ulong c = 0, xVal = x, yVal = y;
  510. c += yVal * xVal + z[zOff + 0];
  511. z[zOff + 0] = (uint)c;
  512. c >>= 32;
  513. c += z[zOff + 1];
  514. z[zOff + 1] = (uint)c;
  515. c >>= 32;
  516. return c == 0 ? 0 : Nat.IncAt(4, z, zOff, 2);
  517. }
  518. public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
  519. {
  520. ulong c = 0, xVal = x;
  521. int i = 0;
  522. do
  523. {
  524. c += xVal * y[i];
  525. z[zOff + i] = (uint)c;
  526. c >>= 32;
  527. }
  528. while (++i < 4);
  529. return (uint)c;
  530. }
  531. public static void Square(uint[] x, uint[] zz)
  532. {
  533. ulong x_0 = x[0];
  534. ulong zz_1;
  535. uint c = 0, w;
  536. {
  537. int i = 3, j = 8;
  538. do
  539. {
  540. ulong xVal = x[i--];
  541. ulong p = xVal * xVal;
  542. zz[--j] = (c << 31) | (uint)(p >> 33);
  543. zz[--j] = (uint)(p >> 1);
  544. c = (uint)p;
  545. }
  546. while (i > 0);
  547. {
  548. ulong p = x_0 * x_0;
  549. zz_1 = (ulong)(c << 31) | (p >> 33);
  550. zz[0] = (uint)p;
  551. c = (uint)(p >> 32) & 1;
  552. }
  553. }
  554. ulong x_1 = x[1];
  555. ulong zz_2 = zz[2];
  556. {
  557. zz_1 += x_1 * x_0;
  558. w = (uint)zz_1;
  559. zz[1] = (w << 1) | c;
  560. c = w >> 31;
  561. zz_2 += zz_1 >> 32;
  562. }
  563. ulong x_2 = x[2];
  564. ulong zz_3 = zz[3];
  565. ulong zz_4 = zz[4];
  566. {
  567. zz_2 += x_2 * x_0;
  568. w = (uint)zz_2;
  569. zz[2] = (w << 1) | c;
  570. c = w >> 31;
  571. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  572. zz_4 += zz_3 >> 32;
  573. zz_3 &= M;
  574. }
  575. ulong x_3 = x[3];
  576. ulong zz_5 = zz[5];
  577. ulong zz_6 = zz[6];
  578. {
  579. zz_3 += x_3 * x_0;
  580. w = (uint)zz_3;
  581. zz[3] = (w << 1) | c;
  582. c = w >> 31;
  583. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  584. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  585. zz_6 += zz_5 >> 32;
  586. }
  587. w = (uint)zz_4;
  588. zz[4] = (w << 1) | c;
  589. c = w >> 31;
  590. w = (uint)zz_5;
  591. zz[5] = (w << 1) | c;
  592. c = w >> 31;
  593. w = (uint)zz_6;
  594. zz[6] = (w << 1) | c;
  595. c = w >> 31;
  596. w = zz[7] + (uint)(zz_6 >> 32);
  597. zz[7] = (w << 1) | c;
  598. }
  599. public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
  600. {
  601. ulong x_0 = x[xOff + 0];
  602. ulong zz_1;
  603. uint c = 0, w;
  604. {
  605. int i = 3, j = 8;
  606. do
  607. {
  608. ulong xVal = x[xOff + i--];
  609. ulong p = xVal * xVal;
  610. zz[zzOff + --j] = (c << 31) | (uint)(p >> 33);
  611. zz[zzOff + --j] = (uint)(p >> 1);
  612. c = (uint)p;
  613. }
  614. while (i > 0);
  615. {
  616. ulong p = x_0 * x_0;
  617. zz_1 = (ulong)(c << 31) | (p >> 33);
  618. zz[zzOff + 0] = (uint)p;
  619. c = (uint)(p >> 32) & 1;
  620. }
  621. }
  622. ulong x_1 = x[xOff + 1];
  623. ulong zz_2 = zz[zzOff + 2];
  624. {
  625. zz_1 += x_1 * x_0;
  626. w = (uint)zz_1;
  627. zz[zzOff + 1] = (w << 1) | c;
  628. c = w >> 31;
  629. zz_2 += zz_1 >> 32;
  630. }
  631. ulong x_2 = x[xOff + 2];
  632. ulong zz_3 = zz[zzOff + 3];
  633. ulong zz_4 = zz[zzOff + 4];
  634. {
  635. zz_2 += x_2 * x_0;
  636. w = (uint)zz_2;
  637. zz[zzOff + 2] = (w << 1) | c;
  638. c = w >> 31;
  639. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  640. zz_4 += zz_3 >> 32;
  641. zz_3 &= M;
  642. }
  643. ulong x_3 = x[xOff + 3];
  644. ulong zz_5 = zz[zzOff + 5];
  645. ulong zz_6 = zz[zzOff + 6];
  646. {
  647. zz_3 += x_3 * x_0;
  648. w = (uint)zz_3;
  649. zz[zzOff + 3] = (w << 1) | c;
  650. c = w >> 31;
  651. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  652. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  653. zz_6 += zz_5 >> 32;
  654. }
  655. w = (uint)zz_4;
  656. zz[zzOff + 4] = (w << 1) | c;
  657. c = w >> 31;
  658. w = (uint)zz_5;
  659. zz[zzOff + 5] = (w << 1) | c;
  660. c = w >> 31;
  661. w = (uint)zz_6;
  662. zz[zzOff + 6] = (w << 1) | c;
  663. c = w >> 31;
  664. w = zz[zzOff + 7] + (uint)(zz_6 >> 32);
  665. zz[zzOff + 7] = (w << 1) | c;
  666. }
  667. public static int Sub(uint[] x, uint[] y, uint[] z)
  668. {
  669. long c = 0;
  670. c += (long)x[0] - y[0];
  671. z[0] = (uint)c;
  672. c >>= 32;
  673. c += (long)x[1] - y[1];
  674. z[1] = (uint)c;
  675. c >>= 32;
  676. c += (long)x[2] - y[2];
  677. z[2] = (uint)c;
  678. c >>= 32;
  679. c += (long)x[3] - y[3];
  680. z[3] = (uint)c;
  681. c >>= 32;
  682. return (int)c;
  683. }
  684. public static int Sub(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  685. {
  686. long c = 0;
  687. c += (long)x[xOff + 0] - y[yOff + 0];
  688. z[zOff + 0] = (uint)c;
  689. c >>= 32;
  690. c += (long)x[xOff + 1] - y[yOff + 1];
  691. z[zOff + 1] = (uint)c;
  692. c >>= 32;
  693. c += (long)x[xOff + 2] - y[yOff + 2];
  694. z[zOff + 2] = (uint)c;
  695. c >>= 32;
  696. c += (long)x[xOff + 3] - y[yOff + 3];
  697. z[zOff + 3] = (uint)c;
  698. c >>= 32;
  699. return (int)c;
  700. }
  701. public static int SubBothFrom(uint[] x, uint[] y, uint[] z)
  702. {
  703. long c = 0;
  704. c += (long)z[0] - x[0] - y[0];
  705. z[0] = (uint)c;
  706. c >>= 32;
  707. c += (long)z[1] - x[1] - y[1];
  708. z[1] = (uint)c;
  709. c >>= 32;
  710. c += (long)z[2] - x[2] - y[2];
  711. z[2] = (uint)c;
  712. c >>= 32;
  713. c += (long)z[3] - x[3] - y[3];
  714. z[3] = (uint)c;
  715. c >>= 32;
  716. return (int)c;
  717. }
  718. public static int SubFrom(uint[] x, uint[] z)
  719. {
  720. long c = 0;
  721. c += (long)z[0] - x[0];
  722. z[0] = (uint)c;
  723. c >>= 32;
  724. c += (long)z[1] - x[1];
  725. z[1] = (uint)c;
  726. c >>= 32;
  727. c += (long)z[2] - x[2];
  728. z[2] = (uint)c;
  729. c >>= 32;
  730. c += (long)z[3] - x[3];
  731. z[3] = (uint)c;
  732. c >>= 32;
  733. return (int)c;
  734. }
  735. public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff)
  736. {
  737. long c = 0;
  738. c += (long)z[zOff + 0] - x[xOff + 0];
  739. z[zOff + 0] = (uint)c;
  740. c >>= 32;
  741. c += (long)z[zOff + 1] - x[xOff + 1];
  742. z[zOff + 1] = (uint)c;
  743. c >>= 32;
  744. c += (long)z[zOff + 2] - x[xOff + 2];
  745. z[zOff + 2] = (uint)c;
  746. c >>= 32;
  747. c += (long)z[zOff + 3] - x[xOff + 3];
  748. z[zOff + 3] = (uint)c;
  749. c >>= 32;
  750. return (int)c;
  751. }
  752. public static BigInteger ToBigInteger(uint[] x)
  753. {
  754. byte[] bs = new byte[16];
  755. for (int i = 0; i < 4; ++i)
  756. {
  757. uint x_i = x[i];
  758. if (x_i != 0)
  759. {
  760. Pack.UInt32_To_BE(x_i, bs, (3 - i) << 2);
  761. }
  762. }
  763. return new BigInteger(1, bs);
  764. }
  765. public static BigInteger ToBigInteger64(ulong[] x)
  766. {
  767. byte[] bs = new byte[16];
  768. for (int i = 0; i < 2; ++i)
  769. {
  770. ulong x_i = x[i];
  771. if (x_i != 0UL)
  772. {
  773. Pack.UInt64_To_BE(x_i, bs, (1 - i) << 3);
  774. }
  775. }
  776. return new BigInteger(1, bs);
  777. }
  778. public static void Zero(uint[] z)
  779. {
  780. z[0] = 0;
  781. z[1] = 0;
  782. z[2] = 0;
  783. z[3] = 0;
  784. }
  785. }
  786. }
  787. #endif