Nat128.cs 24 KB

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