Nat224.cs 35 KB

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