Nat.cs 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057
  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 Nat
  8. {
  9. private const ulong M = 0xFFFFFFFFUL;
  10. public static uint Add(int len, uint[] x, uint[] y, uint[] z)
  11. {
  12. ulong c = 0;
  13. for (int i = 0; i < len; ++i)
  14. {
  15. c += (ulong)x[i] + y[i];
  16. z[i] = (uint)c;
  17. c >>= 32;
  18. }
  19. return (uint)c;
  20. }
  21. public static uint Add33At(int len, uint x, uint[] z, int zPos)
  22. {
  23. Debug.Assert(zPos <= (len - 2));
  24. ulong c = (ulong)z[zPos + 0] + x;
  25. z[zPos + 0] = (uint)c;
  26. c >>= 32;
  27. c += (ulong)z[zPos + 1] + 1;
  28. z[zPos + 1] = (uint)c;
  29. c >>= 32;
  30. return c == 0 ? 0 : IncAt(len, z, zPos + 2);
  31. }
  32. public static uint Add33At(int len, uint x, uint[] z, int zOff, int zPos)
  33. {
  34. Debug.Assert(zPos <= (len - 2));
  35. ulong c = (ulong)z[zOff + zPos] + x;
  36. z[zOff + zPos] = (uint)c;
  37. c >>= 32;
  38. c += (ulong)z[zOff + zPos + 1] + 1;
  39. z[zOff + zPos + 1] = (uint)c;
  40. c >>= 32;
  41. return c == 0 ? 0 : IncAt(len, z, zOff, zPos + 2);
  42. }
  43. public static uint Add33To(int len, uint x, uint[] z)
  44. {
  45. ulong c = (ulong)z[0] + x;
  46. z[0] = (uint)c;
  47. c >>= 32;
  48. c += (ulong)z[1] + 1;
  49. z[1] = (uint)c;
  50. c >>= 32;
  51. return c == 0 ? 0 : IncAt(len, z, 2);
  52. }
  53. public static uint Add33To(int len, uint x, uint[] z, int zOff)
  54. {
  55. ulong c = (ulong)z[zOff + 0] + x;
  56. z[zOff + 0] = (uint)c;
  57. c >>= 32;
  58. c += (ulong)z[zOff + 1] + 1;
  59. z[zOff + 1] = (uint)c;
  60. c >>= 32;
  61. return c == 0 ? 0 : IncAt(len, z, zOff, 2);
  62. }
  63. public static uint AddBothTo(int len, uint[] x, uint[] y, uint[] z)
  64. {
  65. ulong c = 0;
  66. for (int i = 0; i < len; ++i)
  67. {
  68. c += (ulong)x[i] + y[i] + z[i];
  69. z[i] = (uint)c;
  70. c >>= 32;
  71. }
  72. return (uint)c;
  73. }
  74. public static uint AddBothTo(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  75. {
  76. ulong c = 0;
  77. for (int i = 0; i < len; ++i)
  78. {
  79. c += (ulong)x[xOff + i] + y[yOff + i] + z[zOff + i];
  80. z[zOff + i] = (uint)c;
  81. c >>= 32;
  82. }
  83. return (uint)c;
  84. }
  85. public static uint AddDWordAt(int len, ulong x, uint[] z, int zPos)
  86. {
  87. Debug.Assert(zPos <= (len - 2));
  88. ulong c = (ulong)z[zPos + 0] + (x & M);
  89. z[zPos + 0] = (uint)c;
  90. c >>= 32;
  91. c += (ulong)z[zPos + 1] + (x >> 32);
  92. z[zPos + 1] = (uint)c;
  93. c >>= 32;
  94. return c == 0 ? 0 : IncAt(len, z, zPos + 2);
  95. }
  96. public static uint AddDWordAt(int len, ulong x, uint[] z, int zOff, int zPos)
  97. {
  98. Debug.Assert(zPos <= (len - 2));
  99. ulong c = (ulong)z[zOff + zPos] + (x & M);
  100. z[zOff + zPos] = (uint)c;
  101. c >>= 32;
  102. c += (ulong)z[zOff + zPos + 1] + (x >> 32);
  103. z[zOff + zPos + 1] = (uint)c;
  104. c >>= 32;
  105. return c == 0 ? 0 : IncAt(len, z, zOff, zPos + 2);
  106. }
  107. public static uint AddDWordTo(int len, ulong x, uint[] z)
  108. {
  109. ulong c = (ulong)z[0] + (x & M);
  110. z[0] = (uint)c;
  111. c >>= 32;
  112. c += (ulong)z[1] + (x >> 32);
  113. z[1] = (uint)c;
  114. c >>= 32;
  115. return c == 0 ? 0 : IncAt(len, z, 2);
  116. }
  117. public static uint AddDWordTo(int len, ulong x, uint[] z, int zOff)
  118. {
  119. ulong c = (ulong)z[zOff + 0] + (x & M);
  120. z[zOff + 0] = (uint)c;
  121. c >>= 32;
  122. c += (ulong)z[zOff + 1] + (x >> 32);
  123. z[zOff + 1] = (uint)c;
  124. c >>= 32;
  125. return c == 0 ? 0 : IncAt(len, z, zOff, 2);
  126. }
  127. public static uint AddTo(int len, uint[] x, uint[] z)
  128. {
  129. ulong c = 0;
  130. for (int i = 0; i < len; ++i)
  131. {
  132. c += (ulong)x[i] + z[i];
  133. z[i] = (uint)c;
  134. c >>= 32;
  135. }
  136. return (uint)c;
  137. }
  138. public static uint AddTo(int len, uint[] x, int xOff, uint[] z, int zOff)
  139. {
  140. ulong c = 0;
  141. for (int i = 0; i < len; ++i)
  142. {
  143. c += (ulong)x[xOff + i] + z[zOff + i];
  144. z[zOff + i] = (uint)c;
  145. c >>= 32;
  146. }
  147. return (uint)c;
  148. }
  149. public static uint AddWordAt(int len, uint x, uint[] z, int zPos)
  150. {
  151. Debug.Assert(zPos <= (len - 1));
  152. ulong c = (ulong)x + z[zPos];
  153. z[zPos] = (uint)c;
  154. c >>= 32;
  155. return c == 0 ? 0 : IncAt(len, z, zPos + 1);
  156. }
  157. public static uint AddWordAt(int len, uint x, uint[] z, int zOff, int zPos)
  158. {
  159. Debug.Assert(zPos <= (len - 1));
  160. ulong c = (ulong)x + z[zOff + zPos];
  161. z[zOff + zPos] = (uint)c;
  162. c >>= 32;
  163. return c == 0 ? 0 : IncAt(len, z, zOff, zPos + 1);
  164. }
  165. public static uint AddWordTo(int len, uint x, uint[] z)
  166. {
  167. ulong c = (ulong)x + z[0];
  168. z[0] = (uint)c;
  169. c >>= 32;
  170. return c == 0 ? 0 : IncAt(len, z, 1);
  171. }
  172. public static uint AddWordTo(int len, uint x, uint[] z, int zOff)
  173. {
  174. ulong c = (ulong)x + z[zOff];
  175. z[zOff] = (uint)c;
  176. c >>= 32;
  177. return c == 0 ? 0 : IncAt(len, z, zOff, 1);
  178. }
  179. public static void Copy(int len, uint[] x, uint[] z)
  180. {
  181. Array.Copy(x, 0, z, 0, len);
  182. }
  183. public static uint[] Copy(int len, uint[] x)
  184. {
  185. uint[] z = new uint[len];
  186. Array.Copy(x, 0, z, 0, len);
  187. return z;
  188. }
  189. public static uint[] Create(int len)
  190. {
  191. return new uint[len];
  192. }
  193. public static ulong[] Create64(int len)
  194. {
  195. return new ulong[len];
  196. }
  197. public static int Dec(int len, uint[] z)
  198. {
  199. for (int i = 0; i < len; ++i)
  200. {
  201. if (--z[i] != uint.MaxValue)
  202. {
  203. return 0;
  204. }
  205. }
  206. return -1;
  207. }
  208. public static int Dec(int len, uint[] x, uint[] z)
  209. {
  210. int i = 0;
  211. while (i < len)
  212. {
  213. uint c = x[i] - 1;
  214. z[i] = c;
  215. ++i;
  216. if (c != uint.MaxValue)
  217. {
  218. while (i < len)
  219. {
  220. z[i] = x[i];
  221. ++i;
  222. }
  223. return 0;
  224. }
  225. }
  226. return -1;
  227. }
  228. public static int DecAt(int len, uint[] z, int zPos)
  229. {
  230. Debug.Assert(zPos <= len);
  231. for (int i = zPos; i < len; ++i)
  232. {
  233. if (--z[i] != uint.MaxValue)
  234. {
  235. return 0;
  236. }
  237. }
  238. return -1;
  239. }
  240. public static int DecAt(int len, uint[] z, int zOff, int zPos)
  241. {
  242. Debug.Assert(zPos <= len);
  243. for (int i = zPos; i < len; ++i)
  244. {
  245. if (--z[zOff + i] != uint.MaxValue)
  246. {
  247. return 0;
  248. }
  249. }
  250. return -1;
  251. }
  252. public static bool Eq(int len, uint[] x, uint[] y)
  253. {
  254. for (int i = len - 1; i >= 0; --i)
  255. {
  256. if (x[i] != y[i])
  257. {
  258. return false;
  259. }
  260. }
  261. return true;
  262. }
  263. public static uint[] FromBigInteger(int bits, BigInteger x)
  264. {
  265. if (x.SignValue < 0 || x.BitLength > bits)
  266. throw new ArgumentException();
  267. int len = (bits + 31) >> 5;
  268. uint[] z = Create(len);
  269. int i = 0;
  270. while (x.SignValue != 0)
  271. {
  272. z[i++] = (uint)x.IntValue;
  273. x = x.ShiftRight(32);
  274. }
  275. return z;
  276. }
  277. public static uint GetBit(uint[] x, int bit)
  278. {
  279. if (bit == 0)
  280. {
  281. return x[0] & 1;
  282. }
  283. int w = bit >> 5;
  284. if (w < 0 || w >= x.Length)
  285. {
  286. return 0;
  287. }
  288. int b = bit & 31;
  289. return (x[w] >> b) & 1;
  290. }
  291. public static bool Gte(int len, uint[] x, uint[] y)
  292. {
  293. for (int i = len - 1; i >= 0; --i)
  294. {
  295. uint x_i = x[i], y_i = y[i];
  296. if (x_i < y_i)
  297. return false;
  298. if (x_i > y_i)
  299. return true;
  300. }
  301. return true;
  302. }
  303. public static uint Inc(int len, uint[] z)
  304. {
  305. for (int i = 0; i < len; ++i)
  306. {
  307. if (++z[i] != uint.MinValue)
  308. {
  309. return 0;
  310. }
  311. }
  312. return 1;
  313. }
  314. public static uint Inc(int len, uint[] x, uint[] z)
  315. {
  316. int i = 0;
  317. while (i < len)
  318. {
  319. uint c = x[i] + 1;
  320. z[i] = c;
  321. ++i;
  322. if (c != 0)
  323. {
  324. while (i < len)
  325. {
  326. z[i] = x[i];
  327. ++i;
  328. }
  329. return 0;
  330. }
  331. }
  332. return 1;
  333. }
  334. public static uint IncAt(int len, uint[] z, int zPos)
  335. {
  336. Debug.Assert(zPos <= len);
  337. for (int i = zPos; i < len; ++i)
  338. {
  339. if (++z[i] != uint.MinValue)
  340. {
  341. return 0;
  342. }
  343. }
  344. return 1;
  345. }
  346. public static uint IncAt(int len, uint[] z, int zOff, int zPos)
  347. {
  348. Debug.Assert(zPos <= len);
  349. for (int i = zPos; i < len; ++i)
  350. {
  351. if (++z[zOff + i] != uint.MinValue)
  352. {
  353. return 0;
  354. }
  355. }
  356. return 1;
  357. }
  358. public static bool IsOne(int len, uint[] x)
  359. {
  360. if (x[0] != 1)
  361. {
  362. return false;
  363. }
  364. for (int i = 1; i < len; ++i)
  365. {
  366. if (x[i] != 0)
  367. {
  368. return false;
  369. }
  370. }
  371. return true;
  372. }
  373. public static bool IsZero(int len, uint[] x)
  374. {
  375. if (x[0] != 0)
  376. {
  377. return false;
  378. }
  379. for (int i = 1; i < len; ++i)
  380. {
  381. if (x[i] != 0)
  382. {
  383. return false;
  384. }
  385. }
  386. return true;
  387. }
  388. public static void Mul(int len, uint[] x, uint[] y, uint[] zz)
  389. {
  390. zz[len] = (uint)MulWord(len, x[0], y, zz);
  391. for (int i = 1; i < len; ++i)
  392. {
  393. zz[i + len] = (uint)MulWordAddTo(len, x[i], y, 0, zz, i);
  394. }
  395. }
  396. public static void Mul(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] zz, int zzOff)
  397. {
  398. zz[zzOff + len] = (uint)MulWord(len, x[xOff], y, yOff, zz, zzOff);
  399. for (int i = 1; i < len; ++i)
  400. {
  401. zz[zzOff + i + len] = (uint)MulWordAddTo(len, x[xOff + i], y, yOff, zz, zzOff + i);
  402. }
  403. }
  404. public static uint Mul31BothAdd(int len, uint a, uint[] x, uint b, uint[] y, uint[] z, int zOff)
  405. {
  406. ulong c = 0, aVal = (ulong)a, bVal = (ulong)b;
  407. int i = 0;
  408. do
  409. {
  410. c += aVal * x[i] + bVal * y[i] + z[zOff + i];
  411. z[zOff + i] = (uint)c;
  412. c >>= 32;
  413. }
  414. while (++i < len);
  415. return (uint)c;
  416. }
  417. public static uint MulWord(int len, uint x, uint[] y, uint[] z)
  418. {
  419. ulong c = 0, xVal = (ulong)x;
  420. int i = 0;
  421. do
  422. {
  423. c += xVal * y[i];
  424. z[i] = (uint)c;
  425. c >>= 32;
  426. }
  427. while (++i < len);
  428. return (uint)c;
  429. }
  430. public static uint MulWord(int len, uint x, uint[] y, int yOff, uint[] z, int zOff)
  431. {
  432. ulong c = 0, xVal = (ulong)x;
  433. int i = 0;
  434. do
  435. {
  436. c += xVal * y[yOff + i];
  437. z[zOff + i] = (uint)c;
  438. c >>= 32;
  439. }
  440. while (++i < len);
  441. return (uint)c;
  442. }
  443. public static uint MulWordAddTo(int len, uint x, uint[] y, int yOff, uint[] z, int zOff)
  444. {
  445. ulong c = 0, xVal = (ulong)x;
  446. int i = 0;
  447. do
  448. {
  449. c += xVal * y[yOff + i] + z[zOff + i];
  450. z[zOff + i] = (uint)c;
  451. c >>= 32;
  452. }
  453. while (++i < len);
  454. return (uint)c;
  455. }
  456. public static uint MulWordDwordAddAt(int len, uint x, ulong y, uint[] z, int zPos)
  457. {
  458. Debug.Assert(zPos <= (len - 3));
  459. ulong c = 0, xVal = (ulong)x;
  460. c += xVal * (uint)y + z[zPos + 0];
  461. z[zPos + 0] = (uint)c;
  462. c >>= 32;
  463. c += xVal * (y >> 32) + z[zPos + 1];
  464. z[zPos + 1] = (uint)c;
  465. c >>= 32;
  466. c += (ulong)z[zPos + 2];
  467. z[zPos + 2] = (uint)c;
  468. c >>= 32;
  469. return c == 0 ? 0 : IncAt(len, z, zPos + 3);
  470. }
  471. public static uint ShiftDownBit(int len, uint[] z, uint c)
  472. {
  473. int i = len;
  474. while (--i >= 0)
  475. {
  476. uint next = z[i];
  477. z[i] = (next >> 1) | (c << 31);
  478. c = next;
  479. }
  480. return c << 31;
  481. }
  482. public static uint ShiftDownBit(int len, uint[] z, int zOff, uint c)
  483. {
  484. int i = len;
  485. while (--i >= 0)
  486. {
  487. uint next = z[zOff + i];
  488. z[zOff + i] = (next >> 1) | (c << 31);
  489. c = next;
  490. }
  491. return c << 31;
  492. }
  493. public static uint ShiftDownBit(int len, uint[] x, uint c, uint[] z)
  494. {
  495. int i = len;
  496. while (--i >= 0)
  497. {
  498. uint next = x[i];
  499. z[i] = (next >> 1) | (c << 31);
  500. c = next;
  501. }
  502. return c << 31;
  503. }
  504. public static uint ShiftDownBit(int len, uint[] x, int xOff, uint c, uint[] z, int zOff)
  505. {
  506. int i = len;
  507. while (--i >= 0)
  508. {
  509. uint next = x[xOff + i];
  510. z[zOff + i] = (next >> 1) | (c << 31);
  511. c = next;
  512. }
  513. return c << 31;
  514. }
  515. public static uint ShiftDownBits(int len, uint[] z, int bits, uint c)
  516. {
  517. Debug.Assert(bits > 0 && bits < 32);
  518. int i = len;
  519. while (--i >= 0)
  520. {
  521. uint next = z[i];
  522. z[i] = (next >> bits) | (c << -bits);
  523. c = next;
  524. }
  525. return c << -bits;
  526. }
  527. public static uint ShiftDownBits(int len, uint[] z, int zOff, int bits, uint c)
  528. {
  529. Debug.Assert(bits > 0 && bits < 32);
  530. int i = len;
  531. while (--i >= 0)
  532. {
  533. uint next = z[zOff + i];
  534. z[zOff + i] = (next >> bits) | (c << -bits);
  535. c = next;
  536. }
  537. return c << -bits;
  538. }
  539. public static uint ShiftDownBits(int len, uint[] x, int bits, uint c, uint[] z)
  540. {
  541. Debug.Assert(bits > 0 && bits < 32);
  542. int i = len;
  543. while (--i >= 0)
  544. {
  545. uint next = x[i];
  546. z[i] = (next >> bits) | (c << -bits);
  547. c = next;
  548. }
  549. return c << -bits;
  550. }
  551. public static uint ShiftDownBits(int len, uint[] x, int xOff, int bits, uint c, uint[] z, int zOff)
  552. {
  553. Debug.Assert(bits > 0 && bits < 32);
  554. int i = len;
  555. while (--i >= 0)
  556. {
  557. uint next = x[xOff + i];
  558. z[zOff + i] = (next >> bits) | (c << -bits);
  559. c = next;
  560. }
  561. return c << -bits;
  562. }
  563. public static uint ShiftDownWord(int len, uint[] z, uint c)
  564. {
  565. int i = len;
  566. while (--i >= 0)
  567. {
  568. uint next = z[i];
  569. z[i] = c;
  570. c = next;
  571. }
  572. return c;
  573. }
  574. public static uint ShiftUpBit(int len, uint[] z, uint c)
  575. {
  576. for (int i = 0; i < len; ++i)
  577. {
  578. uint next = z[i];
  579. z[i] = (next << 1) | (c >> 31);
  580. c = next;
  581. }
  582. return c >> 31;
  583. }
  584. public static uint ShiftUpBit(int len, uint[] z, int zOff, uint c)
  585. {
  586. for (int i = 0; i < len; ++i)
  587. {
  588. uint next = z[zOff + i];
  589. z[zOff + i] = (next << 1) | (c >> 31);
  590. c = next;
  591. }
  592. return c >> 31;
  593. }
  594. public static uint ShiftUpBit(int len, uint[] x, uint c, uint[] z)
  595. {
  596. for (int i = 0; i < len; ++i)
  597. {
  598. uint next = x[i];
  599. z[i] = (next << 1) | (c >> 31);
  600. c = next;
  601. }
  602. return c >> 31;
  603. }
  604. public static uint ShiftUpBit(int len, uint[] x, int xOff, uint c, uint[] z, int zOff)
  605. {
  606. for (int i = 0; i < len; ++i)
  607. {
  608. uint next = x[xOff + i];
  609. z[zOff + i] = (next << 1) | (c >> 31);
  610. c = next;
  611. }
  612. return c >> 31;
  613. }
  614. public static ulong ShiftUpBit64(int len, ulong[] x, int xOff, ulong c, ulong[] z, int zOff)
  615. {
  616. for (int i = 0; i < len; ++i)
  617. {
  618. ulong next = x[xOff + i];
  619. z[zOff + i] = (next << 1) | (c >> 63);
  620. c = next;
  621. }
  622. return c >> 63;
  623. }
  624. public static uint ShiftUpBits(int len, uint[] z, int bits, uint c)
  625. {
  626. Debug.Assert(bits > 0 && bits < 32);
  627. for (int i = 0; i < len; ++i)
  628. {
  629. uint next = z[i];
  630. z[i] = (next << bits) | (c >> -bits);
  631. c = next;
  632. }
  633. return c >> -bits;
  634. }
  635. public static uint ShiftUpBits(int len, uint[] z, int zOff, int bits, uint c)
  636. {
  637. Debug.Assert(bits > 0 && bits < 32);
  638. for (int i = 0; i < len; ++i)
  639. {
  640. uint next = z[zOff + i];
  641. z[zOff + i] = (next << bits) | (c >> -bits);
  642. c = next;
  643. }
  644. return c >> -bits;
  645. }
  646. public static ulong ShiftUpBits64(int len, ulong[] z, int zOff, int bits, ulong c)
  647. {
  648. Debug.Assert(bits > 0 && bits < 64);
  649. for (int i = 0; i < len; ++i)
  650. {
  651. ulong next = z[zOff + i];
  652. z[zOff + i] = (next << bits) | (c >> -bits);
  653. c = next;
  654. }
  655. return c >> -bits;
  656. }
  657. public static uint ShiftUpBits(int len, uint[] x, int bits, uint c, uint[] z)
  658. {
  659. Debug.Assert(bits > 0 && bits < 32);
  660. for (int i = 0; i < len; ++i)
  661. {
  662. uint next = x[i];
  663. z[i] = (next << bits) | (c >> -bits);
  664. c = next;
  665. }
  666. return c >> -bits;
  667. }
  668. public static uint ShiftUpBits(int len, uint[] x, int xOff, int bits, uint c, uint[] z, int zOff)
  669. {
  670. Debug.Assert(bits > 0 && bits < 32);
  671. for (int i = 0; i < len; ++i)
  672. {
  673. uint next = x[xOff + i];
  674. z[zOff + i] = (next << bits) | (c >> -bits);
  675. c = next;
  676. }
  677. return c >> -bits;
  678. }
  679. public static ulong ShiftUpBits64(int len, ulong[] x, int xOff, int bits, ulong c, ulong[] z, int zOff)
  680. {
  681. Debug.Assert(bits > 0 && bits < 64);
  682. for (int i = 0; i < len; ++i)
  683. {
  684. ulong next = x[xOff + i];
  685. z[zOff + i] = (next << bits) | (c >> -bits);
  686. c = next;
  687. }
  688. return c >> -bits;
  689. }
  690. public static void Square(int len, uint[] x, uint[] zz)
  691. {
  692. int extLen = len << 1;
  693. uint c = 0;
  694. int j = len, k = extLen;
  695. do
  696. {
  697. ulong xVal = (ulong)x[--j];
  698. ulong p = xVal * xVal;
  699. zz[--k] = (c << 31) | (uint)(p >> 33);
  700. zz[--k] = (uint)(p >> 1);
  701. c = (uint)p;
  702. }
  703. while (j > 0);
  704. for (int i = 1; i < len; ++i)
  705. {
  706. c = SquareWordAdd(x, i, zz);
  707. AddWordAt(extLen, c, zz, i << 1);
  708. }
  709. ShiftUpBit(extLen, zz, x[0] << 31);
  710. }
  711. public static void Square(int len, uint[] x, int xOff, uint[] zz, int zzOff)
  712. {
  713. int extLen = len << 1;
  714. uint c = 0;
  715. int j = len, k = extLen;
  716. do
  717. {
  718. ulong xVal = (ulong)x[xOff + --j];
  719. ulong p = xVal * xVal;
  720. zz[zzOff + --k] = (c << 31) | (uint)(p >> 33);
  721. zz[zzOff + --k] = (uint)(p >> 1);
  722. c = (uint)p;
  723. }
  724. while (j > 0);
  725. for (int i = 1; i < len; ++i)
  726. {
  727. c = SquareWordAdd(x, xOff, i, zz, zzOff);
  728. AddWordAt(extLen, c, zz, zzOff, i << 1);
  729. }
  730. ShiftUpBit(extLen, zz, zzOff, x[xOff] << 31);
  731. }
  732. public static uint SquareWordAdd(uint[] x, int xPos, uint[] z)
  733. {
  734. ulong c = 0, xVal = (ulong)x[xPos];
  735. int i = 0;
  736. do
  737. {
  738. c += xVal * x[i] + z[xPos + i];
  739. z[xPos + i] = (uint)c;
  740. c >>= 32;
  741. }
  742. while (++i < xPos);
  743. return (uint)c;
  744. }
  745. public static uint SquareWordAdd(uint[] x, int xOff, int xPos, uint[] z, int zOff)
  746. {
  747. ulong c = 0, xVal = (ulong)x[xOff + xPos];
  748. int i = 0;
  749. do
  750. {
  751. c += xVal * (x[xOff + i] & M) + (z[xPos + zOff] & M);
  752. z[xPos + zOff] = (uint)c;
  753. c >>= 32;
  754. ++zOff;
  755. }
  756. while (++i < xPos);
  757. return (uint)c;
  758. }
  759. public static int Sub(int len, uint[] x, uint[] y, uint[] z)
  760. {
  761. long c = 0;
  762. for (int i = 0; i < len; ++i)
  763. {
  764. c += (long)x[i] - y[i];
  765. z[i] = (uint)c;
  766. c >>= 32;
  767. }
  768. return (int)c;
  769. }
  770. public static int Sub(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  771. {
  772. long c = 0;
  773. for (int i = 0; i < len; ++i)
  774. {
  775. c += (long)x[xOff + i] - y[yOff + i];
  776. z[zOff + i] = (uint)c;
  777. c >>= 32;
  778. }
  779. return (int)c;
  780. }
  781. public static int Sub33At(int len, uint x, uint[] z, int zPos)
  782. {
  783. Debug.Assert(zPos <= (len - 2));
  784. long c = (long)z[zPos + 0] - x;
  785. z[zPos + 0] = (uint)c;
  786. c >>= 32;
  787. c += (long)z[zPos + 1] - 1;
  788. z[zPos + 1] = (uint)c;
  789. c >>= 32;
  790. return c == 0 ? 0 : DecAt(len, z, zPos + 2);
  791. }
  792. public static int Sub33At(int len, uint x, uint[] z, int zOff, int zPos)
  793. {
  794. Debug.Assert(zPos <= (len - 2));
  795. long c = (long)z[zOff + zPos] - x;
  796. z[zOff + zPos] = (uint)c;
  797. c >>= 32;
  798. c += (long)z[zOff + zPos + 1] - 1;
  799. z[zOff + zPos + 1] = (uint)c;
  800. c >>= 32;
  801. return c == 0 ? 0 : DecAt(len, z, zOff, zPos + 2);
  802. }
  803. public static int Sub33From(int len, uint x, uint[] z)
  804. {
  805. long c = (long)z[0] - x;
  806. z[0] = (uint)c;
  807. c >>= 32;
  808. c += (long)z[1] - 1;
  809. z[1] = (uint)c;
  810. c >>= 32;
  811. return c == 0 ? 0 : DecAt(len, z, 2);
  812. }
  813. public static int Sub33From(int len, uint x, uint[] z, int zOff)
  814. {
  815. long c = (long)z[zOff + 0] - x;
  816. z[zOff + 0] = (uint)c;
  817. c >>= 32;
  818. c += (long)z[zOff + 1] - 1;
  819. z[zOff + 1] = (uint)c;
  820. c >>= 32;
  821. return c == 0 ? 0 : DecAt(len, z, zOff, 2);
  822. }
  823. public static int SubBothFrom(int len, uint[] x, uint[] y, uint[] z)
  824. {
  825. long c = 0;
  826. for (int i = 0; i < len; ++i)
  827. {
  828. c += (long)z[i] - x[i] - y[i];
  829. z[i] = (uint)c;
  830. c >>= 32;
  831. }
  832. return (int)c;
  833. }
  834. public static int SubBothFrom(int len, uint[] x, int xOff, uint[] y, int yOff, uint[] z, int zOff)
  835. {
  836. long c = 0;
  837. for (int i = 0; i < len; ++i)
  838. {
  839. c += (long)z[zOff + i] - x[xOff + i] - y[yOff + i];
  840. z[zOff + i] = (uint)c;
  841. c >>= 32;
  842. }
  843. return (int)c;
  844. }
  845. public static int SubDWordAt(int len, ulong x, uint[] z, int zPos)
  846. {
  847. Debug.Assert(zPos <= (len - 2));
  848. long c = (long)z[zPos + 0] - (long)(x & M);
  849. z[zPos + 0] = (uint)c;
  850. c >>= 32;
  851. c += (long)z[zPos + 1] - (long)(x >> 32);
  852. z[zPos + 1] = (uint)c;
  853. c >>= 32;
  854. return c == 0 ? 0 : DecAt(len, z, zPos + 2);
  855. }
  856. public static int SubDWordAt(int len, ulong x, uint[] z, int zOff, int zPos)
  857. {
  858. Debug.Assert(zPos <= (len - 2));
  859. long c = (long)z[zOff + zPos] - (long)(x & M);
  860. z[zOff + zPos] = (uint)c;
  861. c >>= 32;
  862. c += (long)z[zOff + zPos + 1] - (long)(x >> 32);
  863. z[zOff + zPos + 1] = (uint)c;
  864. c >>= 32;
  865. return c == 0 ? 0 : DecAt(len, z, zOff, zPos + 2);
  866. }
  867. public static int SubDWordFrom(int len, ulong x, uint[] z)
  868. {
  869. long c = (long)z[0] - (long)(x & M);
  870. z[0] = (uint)c;
  871. c >>= 32;
  872. c += (long)z[1] - (long)(x >> 32);
  873. z[1] = (uint)c;
  874. c >>= 32;
  875. return c == 0 ? 0 : DecAt(len, z, 2);
  876. }
  877. public static int SubDWordFrom(int len, ulong x, uint[] z, int zOff)
  878. {
  879. long c = (long)z[zOff + 0] - (long)(x & M);
  880. z[zOff + 0] = (uint)c;
  881. c >>= 32;
  882. c += (long)z[zOff + 1] - (long)(x >> 32);
  883. z[zOff + 1] = (uint)c;
  884. c >>= 32;
  885. return c == 0 ? 0 : DecAt(len, z, zOff, 2);
  886. }
  887. public static int SubFrom(int len, uint[] x, uint[] z)
  888. {
  889. long c = 0;
  890. for (int i = 0; i < len; ++i)
  891. {
  892. c += (long)z[i] - x[i];
  893. z[i] = (uint)c;
  894. c >>= 32;
  895. }
  896. return (int)c;
  897. }
  898. public static int SubFrom(int len, uint[] x, int xOff, uint[] z, int zOff)
  899. {
  900. long c = 0;
  901. for (int i = 0; i < len; ++i)
  902. {
  903. c += (long)z[zOff + i] - x[xOff + i];
  904. z[zOff + i] = (uint)c;
  905. c >>= 32;
  906. }
  907. return (int)c;
  908. }
  909. public static int SubWordAt(int len, uint x, uint[] z, int zPos)
  910. {
  911. Debug.Assert(zPos <= (len - 1));
  912. long c = (long)z[zPos] - x;
  913. z[zPos] = (uint)c;
  914. c >>= 32;
  915. return c == 0 ? 0 : DecAt(len, z, zPos + 1);
  916. }
  917. public static int SubWordAt(int len, uint x, uint[] z, int zOff, int zPos)
  918. {
  919. Debug.Assert(zPos <= (len - 1));
  920. long c = (long)z[zOff + zPos] - x;
  921. z[zOff + zPos] = (uint)c;
  922. c >>= 32;
  923. return c == 0 ? 0 : DecAt(len, z, zOff, zPos + 1);
  924. }
  925. public static int SubWordFrom(int len, uint x, uint[] z)
  926. {
  927. long c = (long)z[0] - x;
  928. z[0] = (uint)c;
  929. c >>= 32;
  930. return c == 0 ? 0 : DecAt(len, z, 1);
  931. }
  932. public static int SubWordFrom(int len, uint x, uint[] z, int zOff)
  933. {
  934. long c = (long)z[zOff + 0] - x;
  935. z[zOff + 0] = (uint)c;
  936. c >>= 32;
  937. return c == 0 ? 0 : DecAt(len, z, zOff, 1);
  938. }
  939. public static BigInteger ToBigInteger(int len, uint[] x)
  940. {
  941. byte[] bs = new byte[len << 2];
  942. for (int i = 0; i < len; ++i)
  943. {
  944. uint x_i = x[i];
  945. if (x_i != 0)
  946. {
  947. Pack.UInt32_To_BE(x_i, bs, (len - 1 - i) << 2);
  948. }
  949. }
  950. return new BigInteger(1, bs);
  951. }
  952. public static void Zero(int len, uint[] z)
  953. {
  954. for (int i = 0; i < len; ++i)
  955. {
  956. z[i] = 0;
  957. }
  958. }
  959. }
  960. }
  961. #endif