Nat128.cs 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874
  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[] FromBigInteger(BigInteger x)
  172. {
  173. if (x.SignValue < 0 || x.BitLength > 128)
  174. throw new ArgumentException();
  175. uint[] z = Create();
  176. int i = 0;
  177. while (x.SignValue != 0)
  178. {
  179. z[i++] = (uint)x.IntValue;
  180. x = x.ShiftRight(32);
  181. }
  182. return z;
  183. }
  184. public static ulong[] FromBigInteger64(BigInteger x)
  185. {
  186. if (x.SignValue < 0 || x.BitLength > 128)
  187. throw new ArgumentException();
  188. ulong[] z = Create64();
  189. int i = 0;
  190. while (x.SignValue != 0)
  191. {
  192. z[i++] = (ulong)x.LongValue;
  193. x = x.ShiftRight(64);
  194. }
  195. return z;
  196. }
  197. public static uint GetBit(uint[] x, int bit)
  198. {
  199. if (bit == 0)
  200. {
  201. return x[0] & 1;
  202. }
  203. if ((bit & 127) != bit)
  204. {
  205. return 0;
  206. }
  207. int w = bit >> 5;
  208. int b = bit & 31;
  209. return (x[w] >> b) & 1;
  210. }
  211. public static bool Gte(uint[] x, uint[] y)
  212. {
  213. for (int i = 3; i >= 0; --i)
  214. {
  215. uint x_i = x[i], y_i = y[i];
  216. if (x_i < y_i)
  217. return false;
  218. if (x_i > y_i)
  219. return true;
  220. }
  221. return true;
  222. }
  223. public static bool Gte(uint[] x, int xOff, uint[] y, int yOff)
  224. {
  225. for (int i = 3; i >= 0; --i)
  226. {
  227. uint x_i = x[xOff + i], y_i = y[yOff + i];
  228. if (x_i < y_i)
  229. return false;
  230. if (x_i > y_i)
  231. return true;
  232. }
  233. return true;
  234. }
  235. public static bool IsOne(uint[] x)
  236. {
  237. if (x[0] != 1)
  238. {
  239. return false;
  240. }
  241. for (int i = 1; i < 4; ++i)
  242. {
  243. if (x[i] != 0)
  244. {
  245. return false;
  246. }
  247. }
  248. return true;
  249. }
  250. public static bool IsOne64(ulong[] x)
  251. {
  252. if (x[0] != 1UL)
  253. {
  254. return false;
  255. }
  256. for (int i = 1; i < 2; ++i)
  257. {
  258. if (x[i] != 0UL)
  259. {
  260. return false;
  261. }
  262. }
  263. return true;
  264. }
  265. public static bool IsZero(uint[] x)
  266. {
  267. for (int i = 0; i < 4; ++i)
  268. {
  269. if (x[i] != 0)
  270. {
  271. return false;
  272. }
  273. }
  274. return true;
  275. }
  276. public static bool IsZero64(ulong[] x)
  277. {
  278. for (int i = 0; i < 2; ++i)
  279. {
  280. if (x[i] != 0UL)
  281. {
  282. return false;
  283. }
  284. }
  285. return true;
  286. }
  287. public static void Mul(uint[] x, uint[] y, uint[] zz)
  288. {
  289. ulong y_0 = y[0];
  290. ulong y_1 = y[1];
  291. ulong y_2 = y[2];
  292. ulong y_3 = y[3];
  293. {
  294. ulong c = 0, x_0 = x[0];
  295. c += x_0 * y_0;
  296. zz[0] = (uint)c;
  297. c >>= 32;
  298. c += x_0 * y_1;
  299. zz[1] = (uint)c;
  300. c >>= 32;
  301. c += x_0 * y_2;
  302. zz[2] = (uint)c;
  303. c >>= 32;
  304. c += x_0 * y_3;
  305. zz[3] = (uint)c;
  306. c >>= 32;
  307. zz[4] = (uint)c;
  308. }
  309. for (int i = 1; i < 4; ++i)
  310. {
  311. ulong c = 0, x_i = x[i];
  312. c += x_i * y_0 + zz[i + 0];
  313. zz[i + 0] = (uint)c;
  314. c >>= 32;
  315. c += x_i * y_1 + zz[i + 1];
  316. zz[i + 1] = (uint)c;
  317. c >>= 32;
  318. c += x_i * y_2 + zz[i + 2];
  319. zz[i + 2] = (uint)c;
  320. c >>= 32;
  321. c += x_i * y_3 + zz[i + 3];
  322. zz[i + 3] = (uint)c;
  323. c >>= 32;
  324. zz[i + 4] = (uint)c;
  325. }
  326. }
  327. public static void Mul(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  328. {
  329. ulong y_0 = y[yOff + 0];
  330. ulong y_1 = y[yOff + 1];
  331. ulong y_2 = y[yOff + 2];
  332. ulong y_3 = y[yOff + 3];
  333. {
  334. ulong c = 0, x_0 = x[xOff + 0];
  335. c += x_0 * y_0;
  336. zz[zzOff + 0] = (uint)c;
  337. c >>= 32;
  338. c += x_0 * y_1;
  339. zz[zzOff + 1] = (uint)c;
  340. c >>= 32;
  341. c += x_0 * y_2;
  342. zz[zzOff + 2] = (uint)c;
  343. c >>= 32;
  344. c += x_0 * y_3;
  345. zz[zzOff + 3] = (uint)c;
  346. c >>= 32;
  347. zz[zzOff + 4] = (uint)c;
  348. }
  349. for (int i = 1; i < 4; ++i)
  350. {
  351. ++zzOff;
  352. ulong c = 0, x_i = x[xOff + i];
  353. c += x_i * y_0 + zz[zzOff + 0];
  354. zz[zzOff + 0] = (uint)c;
  355. c >>= 32;
  356. c += x_i * y_1 + zz[zzOff + 1];
  357. zz[zzOff + 1] = (uint)c;
  358. c >>= 32;
  359. c += x_i * y_2 + zz[zzOff + 2];
  360. zz[zzOff + 2] = (uint)c;
  361. c >>= 32;
  362. c += x_i * y_3 + zz[zzOff + 3];
  363. zz[zzOff + 3] = (uint)c;
  364. c >>= 32;
  365. zz[zzOff + 4] = (uint)c;
  366. }
  367. }
  368. public static uint MulAddTo(uint[] x, uint[] y, uint[] zz)
  369. {
  370. ulong y_0 = y[0];
  371. ulong y_1 = y[1];
  372. ulong y_2 = y[2];
  373. ulong y_3 = y[3];
  374. ulong zc = 0;
  375. for (int i = 0; i < 4; ++i)
  376. {
  377. ulong c = 0, x_i = x[i];
  378. c += x_i * y_0 + zz[i + 0];
  379. zz[i + 0] = (uint)c;
  380. c >>= 32;
  381. c += x_i * y_1 + zz[i + 1];
  382. zz[i + 1] = (uint)c;
  383. c >>= 32;
  384. c += x_i * y_2 + zz[i + 2];
  385. zz[i + 2] = (uint)c;
  386. c >>= 32;
  387. c += x_i * y_3 + zz[i + 3];
  388. zz[i + 3] = (uint)c;
  389. c >>= 32;
  390. c += zc + zz[i + 4];
  391. zz[i + 4] = (uint)c;
  392. zc = c >> 32;
  393. }
  394. return (uint)zc;
  395. }
  396. public static uint MulAddTo(uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  397. {
  398. ulong y_0 = y[yOff + 0];
  399. ulong y_1 = y[yOff + 1];
  400. ulong y_2 = y[yOff + 2];
  401. ulong y_3 = y[yOff + 3];
  402. ulong zc = 0;
  403. for (int i = 0; i < 4; ++i)
  404. {
  405. ulong c = 0, x_i = x[xOff + i];
  406. c += x_i * y_0 + zz[zzOff + 0];
  407. zz[zzOff + 0] = (uint)c;
  408. c >>= 32;
  409. c += x_i * y_1 + zz[zzOff + 1];
  410. zz[zzOff + 1] = (uint)c;
  411. c >>= 32;
  412. c += x_i * y_2 + zz[zzOff + 2];
  413. zz[zzOff + 2] = (uint)c;
  414. c >>= 32;
  415. c += x_i * y_3 + zz[zzOff + 3];
  416. zz[zzOff + 3] = (uint)c;
  417. c >>= 32;
  418. c += zc + zz[zzOff + 4];
  419. zz[zzOff + 4] = (uint)c;
  420. zc = c >> 32;
  421. ++zzOff;
  422. }
  423. return (uint)zc;
  424. }
  425. public static ulong Mul33Add(uint w, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  426. {
  427. Debug.Assert(w >> 31 == 0);
  428. ulong c = 0, wVal = w;
  429. ulong x0 = x[xOff + 0];
  430. c += wVal * x0 + y[yOff + 0];
  431. z[zOff + 0] = (uint)c;
  432. c >>= 32;
  433. ulong x1 = x[xOff + 1];
  434. c += wVal * x1 + x0 + y[yOff + 1];
  435. z[zOff + 1] = (uint)c;
  436. c >>= 32;
  437. ulong x2 = x[xOff + 2];
  438. c += wVal * x2 + x1 + y[yOff + 2];
  439. z[zOff + 2] = (uint)c;
  440. c >>= 32;
  441. ulong x3 = x[xOff + 3];
  442. c += wVal * x3 + x2 + y[yOff + 3];
  443. z[zOff + 3] = (uint)c;
  444. c >>= 32;
  445. c += x3;
  446. return c;
  447. }
  448. public static uint MulWordAddExt(uint x, uint[] yy, int yyOff, uint[] zz, int zzOff)
  449. {
  450. Debug.Assert(yyOff <= 4);
  451. Debug.Assert(zzOff <= 4);
  452. ulong c = 0, xVal = x;
  453. c += xVal * yy[yyOff + 0] + zz[zzOff + 0];
  454. zz[zzOff + 0] = (uint)c;
  455. c >>= 32;
  456. c += xVal * yy[yyOff + 1] + zz[zzOff + 1];
  457. zz[zzOff + 1] = (uint)c;
  458. c >>= 32;
  459. c += xVal * yy[yyOff + 2] + zz[zzOff + 2];
  460. zz[zzOff + 2] = (uint)c;
  461. c >>= 32;
  462. c += xVal * yy[yyOff + 3] + zz[zzOff + 3];
  463. zz[zzOff + 3] = (uint)c;
  464. c >>= 32;
  465. return (uint)c;
  466. }
  467. public static uint Mul33DWordAdd(uint x, ulong y, uint[] z, int zOff)
  468. {
  469. Debug.Assert(x >> 31 == 0);
  470. Debug.Assert(zOff <= 0);
  471. ulong c = 0, xVal = x;
  472. ulong y00 = y & M;
  473. c += xVal * y00 + z[zOff + 0];
  474. z[zOff + 0] = (uint)c;
  475. c >>= 32;
  476. ulong y01 = y >> 32;
  477. c += xVal * y01 + y00 + z[zOff + 1];
  478. z[zOff + 1] = (uint)c;
  479. c >>= 32;
  480. c += y01 + z[zOff + 2];
  481. z[zOff + 2] = (uint)c;
  482. c >>= 32;
  483. c += z[zOff + 3];
  484. z[zOff + 3] = (uint)c;
  485. c >>= 32;
  486. return (uint)c;
  487. }
  488. public static uint Mul33WordAdd(uint x, uint y, uint[] z, int zOff)
  489. {
  490. Debug.Assert(x >> 31 == 0);
  491. Debug.Assert(zOff <= 1);
  492. ulong c = 0, yVal = y;
  493. c += yVal * x + z[zOff + 0];
  494. z[zOff + 0] = (uint)c;
  495. c >>= 32;
  496. c += yVal + z[zOff + 1];
  497. z[zOff + 1] = (uint)c;
  498. c >>= 32;
  499. c += z[zOff + 2];
  500. z[zOff + 2] = (uint)c;
  501. c >>= 32;
  502. return c == 0 ? 0 : Nat.IncAt(4, z, zOff, 3);
  503. }
  504. public static uint MulWordDwordAdd(uint x, ulong y, uint[] z, int zOff)
  505. {
  506. Debug.Assert(zOff <= 1);
  507. ulong c = 0, xVal = x;
  508. c += xVal * y + z[zOff + 0];
  509. z[zOff + 0] = (uint)c;
  510. c >>= 32;
  511. c += xVal * (y >> 32) + z[zOff + 1];
  512. z[zOff + 1] = (uint)c;
  513. c >>= 32;
  514. c += z[zOff + 2];
  515. z[zOff + 2] = (uint)c;
  516. c >>= 32;
  517. return c == 0 ? 0 : Nat.IncAt(4, z, zOff, 3);
  518. }
  519. public static uint MulWordsAdd(uint x, uint y, uint[] z, int zOff)
  520. {
  521. Debug.Assert(zOff <= 2);
  522. ulong c = 0, xVal = x, yVal = y;
  523. c += yVal * xVal + z[zOff + 0];
  524. z[zOff + 0] = (uint)c;
  525. c >>= 32;
  526. c += z[zOff + 1];
  527. z[zOff + 1] = (uint)c;
  528. c >>= 32;
  529. return c == 0 ? 0 : Nat.IncAt(4, z, zOff, 2);
  530. }
  531. public static uint MulWord(uint x, uint[] y, uint[] z, int zOff)
  532. {
  533. ulong c = 0, xVal = x;
  534. int i = 0;
  535. do
  536. {
  537. c += xVal * y[i];
  538. z[zOff + i] = (uint)c;
  539. c >>= 32;
  540. }
  541. while (++i < 4);
  542. return (uint)c;
  543. }
  544. public static void Square(uint[] x, uint[] zz)
  545. {
  546. ulong x_0 = x[0];
  547. ulong zz_1;
  548. uint c = 0, w;
  549. {
  550. int i = 3, j = 8;
  551. do
  552. {
  553. ulong xVal = x[i--];
  554. ulong p = xVal * xVal;
  555. zz[--j] = (c << 31) | (uint)(p >> 33);
  556. zz[--j] = (uint)(p >> 1);
  557. c = (uint)p;
  558. }
  559. while (i > 0);
  560. {
  561. ulong p = x_0 * x_0;
  562. zz_1 = (ulong)(c << 31) | (p >> 33);
  563. zz[0] = (uint)p;
  564. c = (uint)(p >> 32) & 1;
  565. }
  566. }
  567. ulong x_1 = x[1];
  568. ulong zz_2 = zz[2];
  569. {
  570. zz_1 += x_1 * x_0;
  571. w = (uint)zz_1;
  572. zz[1] = (w << 1) | c;
  573. c = w >> 31;
  574. zz_2 += zz_1 >> 32;
  575. }
  576. ulong x_2 = x[2];
  577. ulong zz_3 = zz[3];
  578. ulong zz_4 = zz[4];
  579. {
  580. zz_2 += x_2 * x_0;
  581. w = (uint)zz_2;
  582. zz[2] = (w << 1) | c;
  583. c = w >> 31;
  584. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  585. zz_4 += zz_3 >> 32;
  586. zz_3 &= M;
  587. }
  588. ulong x_3 = x[3];
  589. ulong zz_5 = zz[5] + (zz_4 >> 32); zz_4 &= M;
  590. ulong zz_6 = zz[6] + (zz_5 >> 32); zz_5 &= M;
  591. {
  592. zz_3 += x_3 * x_0;
  593. w = (uint)zz_3;
  594. zz[3] = (w << 1) | c;
  595. c = w >> 31;
  596. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  597. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  598. zz_6 += zz_5 >> 32;
  599. }
  600. w = (uint)zz_4;
  601. zz[4] = (w << 1) | c;
  602. c = w >> 31;
  603. w = (uint)zz_5;
  604. zz[5] = (w << 1) | c;
  605. c = w >> 31;
  606. w = (uint)zz_6;
  607. zz[6] = (w << 1) | c;
  608. c = w >> 31;
  609. w = zz[7] + (uint)(zz_6 >> 32);
  610. zz[7] = (w << 1) | c;
  611. }
  612. public static void Square(uint[] x, int xOff, uint[] zz, int zzOff)
  613. {
  614. ulong x_0 = x[xOff + 0];
  615. ulong zz_1;
  616. uint c = 0, w;
  617. {
  618. int i = 3, j = 8;
  619. do
  620. {
  621. ulong xVal = x[xOff + i--];
  622. ulong p = xVal * xVal;
  623. zz[zzOff + --j] = (c << 31) | (uint)(p >> 33);
  624. zz[zzOff + --j] = (uint)(p >> 1);
  625. c = (uint)p;
  626. }
  627. while (i > 0);
  628. {
  629. ulong p = x_0 * x_0;
  630. zz_1 = (ulong)(c << 31) | (p >> 33);
  631. zz[zzOff + 0] = (uint)p;
  632. c = (uint)(p >> 32) & 1;
  633. }
  634. }
  635. ulong x_1 = x[xOff + 1];
  636. ulong zz_2 = zz[zzOff + 2];
  637. {
  638. zz_1 += x_1 * x_0;
  639. w = (uint)zz_1;
  640. zz[zzOff + 1] = (w << 1) | c;
  641. c = w >> 31;
  642. zz_2 += zz_1 >> 32;
  643. }
  644. ulong x_2 = x[xOff + 2];
  645. ulong zz_3 = zz[zzOff + 3];
  646. ulong zz_4 = zz[zzOff + 4];
  647. {
  648. zz_2 += x_2 * x_0;
  649. w = (uint)zz_2;
  650. zz[zzOff + 2] = (w << 1) | c;
  651. c = w >> 31;
  652. zz_3 += (zz_2 >> 32) + x_2 * x_1;
  653. zz_4 += zz_3 >> 32;
  654. zz_3 &= M;
  655. }
  656. ulong x_3 = x[xOff + 3];
  657. ulong zz_5 = zz[zzOff + 5] + (zz_4 >> 32); zz_4 &= M;
  658. ulong zz_6 = zz[zzOff + 6] + (zz_5 >> 32); zz_5 &= M;
  659. {
  660. zz_3 += x_3 * x_0;
  661. w = (uint)zz_3;
  662. zz[zzOff + 3] = (w << 1) | c;
  663. c = w >> 31;
  664. zz_4 += (zz_3 >> 32) + x_3 * x_1;
  665. zz_5 += (zz_4 >> 32) + x_3 * x_2;
  666. zz_6 += zz_5 >> 32;
  667. }
  668. w = (uint)zz_4;
  669. zz[zzOff + 4] = (w << 1) | c;
  670. c = w >> 31;
  671. w = (uint)zz_5;
  672. zz[zzOff + 5] = (w << 1) | c;
  673. c = w >> 31;
  674. w = (uint)zz_6;
  675. zz[zzOff + 6] = (w << 1) | c;
  676. c = w >> 31;
  677. w = zz[zzOff + 7] + (uint)(zz_6 >> 32);
  678. zz[zzOff + 7] = (w << 1) | c;
  679. }
  680. public static int Sub(uint[] x, uint[] y, uint[] z)
  681. {
  682. long c = 0;
  683. c += (long)x[0] - y[0];
  684. z[0] = (uint)c;
  685. c >>= 32;
  686. c += (long)x[1] - y[1];
  687. z[1] = (uint)c;
  688. c >>= 32;
  689. c += (long)x[2] - y[2];
  690. z[2] = (uint)c;
  691. c >>= 32;
  692. c += (long)x[3] - y[3];
  693. z[3] = (uint)c;
  694. c >>= 32;
  695. return (int)c;
  696. }
  697. public static int Sub(uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  698. {
  699. long c = 0;
  700. c += (long)x[xOff + 0] - y[yOff + 0];
  701. z[zOff + 0] = (uint)c;
  702. c >>= 32;
  703. c += (long)x[xOff + 1] - y[yOff + 1];
  704. z[zOff + 1] = (uint)c;
  705. c >>= 32;
  706. c += (long)x[xOff + 2] - y[yOff + 2];
  707. z[zOff + 2] = (uint)c;
  708. c >>= 32;
  709. c += (long)x[xOff + 3] - y[yOff + 3];
  710. z[zOff + 3] = (uint)c;
  711. c >>= 32;
  712. return (int)c;
  713. }
  714. public static int SubBothFrom(uint[] x, uint[] y, uint[] z)
  715. {
  716. long c = 0;
  717. c += (long)z[0] - x[0] - y[0];
  718. z[0] = (uint)c;
  719. c >>= 32;
  720. c += (long)z[1] - x[1] - y[1];
  721. z[1] = (uint)c;
  722. c >>= 32;
  723. c += (long)z[2] - x[2] - y[2];
  724. z[2] = (uint)c;
  725. c >>= 32;
  726. c += (long)z[3] - x[3] - y[3];
  727. z[3] = (uint)c;
  728. c >>= 32;
  729. return (int)c;
  730. }
  731. public static int SubFrom(uint[] x, uint[] z)
  732. {
  733. long c = 0;
  734. c += (long)z[0] - x[0];
  735. z[0] = (uint)c;
  736. c >>= 32;
  737. c += (long)z[1] - x[1];
  738. z[1] = (uint)c;
  739. c >>= 32;
  740. c += (long)z[2] - x[2];
  741. z[2] = (uint)c;
  742. c >>= 32;
  743. c += (long)z[3] - x[3];
  744. z[3] = (uint)c;
  745. c >>= 32;
  746. return (int)c;
  747. }
  748. public static int SubFrom(uint[] x, int xOff, uint[] z, int zOff)
  749. {
  750. long c = 0;
  751. c += (long)z[zOff + 0] - x[xOff + 0];
  752. z[zOff + 0] = (uint)c;
  753. c >>= 32;
  754. c += (long)z[zOff + 1] - x[xOff + 1];
  755. z[zOff + 1] = (uint)c;
  756. c >>= 32;
  757. c += (long)z[zOff + 2] - x[xOff + 2];
  758. z[zOff + 2] = (uint)c;
  759. c >>= 32;
  760. c += (long)z[zOff + 3] - x[xOff + 3];
  761. z[zOff + 3] = (uint)c;
  762. c >>= 32;
  763. return (int)c;
  764. }
  765. public static BigInteger ToBigInteger(uint[] x)
  766. {
  767. byte[] bs = new byte[16];
  768. for (int i = 0; i < 4; ++i)
  769. {
  770. uint x_i = x[i];
  771. if (x_i != 0)
  772. {
  773. Pack.UInt32_To_BE(x_i, bs, (3 - i) << 2);
  774. }
  775. }
  776. return new BigInteger(1, bs);
  777. }
  778. public static BigInteger ToBigInteger64(ulong[] x)
  779. {
  780. byte[] bs = new byte[16];
  781. for (int i = 0; i < 2; ++i)
  782. {
  783. ulong x_i = x[i];
  784. if (x_i != 0UL)
  785. {
  786. Pack.UInt64_To_BE(x_i, bs, (1 - i) << 3);
  787. }
  788. }
  789. return new BigInteger(1, bs);
  790. }
  791. public static void Zero(uint[] z)
  792. {
  793. z[0] = 0;
  794. z[1] = 0;
  795. z[2] = 0;
  796. z[3] = 0;
  797. }
  798. }
  799. }
  800. #pragma warning restore
  801. #endif