Nat192.cs 30 KB

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